blob: ac1db73bc9b7b6687f6f01df6a0abf872a54444c [file] [log] [blame]
[email protected]41619a9a2011-04-13 20:07:321// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]1c3cf8b2010-05-11 23:18:272// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]1870d5cf2011-05-12 01:55:405#include "base/stringprintf.h"
[email protected]3ab9cb82011-06-03 18:02:076#include "base/utf_string_conversions.h"
[email protected]a07676b22011-06-17 16:36:537#include "chrome/browser/background/background_contents_service.h"
8#include "chrome/browser/background/background_contents_service_factory.h"
[email protected]1c3cf8b2010-05-11 23:18:279#include "chrome/browser/extensions/extension_apitest.h"
[email protected]41619a9a2011-04-13 20:07:3210#include "chrome/browser/extensions/extension_service.h"
11#include "chrome/browser/profiles/profile.h"
12#include "chrome/browser/ui/browser.h"
[email protected]1c3cf8b2010-05-11 23:18:2713#include "chrome/common/chrome_switches.h"
[email protected]41619a9a2011-04-13 20:07:3214#include "chrome/common/extensions/extension.h"
[email protected]af44e7fb2011-07-29 18:32:3215#include "chrome/test/base/ui_test_utils.h"
[email protected]1c3cf8b2010-05-11 23:18:2716#include "net/base/mock_host_resolver.h"
17
18class AppBackgroundPageApiTest : public ExtensionApiTest {
19 public:
20 void SetUpCommandLine(CommandLine* command_line) {
21 ExtensionApiTest::SetUpCommandLine(command_line);
[email protected]1c3cf8b2010-05-11 23:18:2722 command_line->AppendSwitch(switches::kDisablePopupBlocking);
[email protected]41619a9a2011-04-13 20:07:3223 command_line->AppendSwitch(switches::kAllowHTTPBackgroundPage);
[email protected]1c3cf8b2010-05-11 23:18:2724 }
[email protected]f08debf2010-10-23 10:13:0225
26 bool CreateApp(const std::string& app_manifest,
27 FilePath* app_dir) {
28 if (!app_dir_.CreateUniqueTempDir()) {
29 LOG(ERROR) << "Unable to create a temporary directory.";
30 return false;
31 }
32 FilePath manifest_path = app_dir_.path().AppendASCII("manifest.json");
33 int bytes_written = file_util::WriteFile(manifest_path,
34 app_manifest.data(),
35 app_manifest.size());
36 if (bytes_written != static_cast<int>(app_manifest.size())) {
37 LOG(ERROR) << "Unable to write complete manifest to file. Return code="
38 << bytes_written;
39 return false;
40 }
41 *app_dir = app_dir_.path();
42 return true;
43 }
44
45 private:
46 ScopedTempDir app_dir_;
[email protected]1c3cf8b2010-05-11 23:18:2747};
48
[email protected]5d5c1ed2011-09-02 00:01:1149// Disable on Mac only. http://crbug.com/95139
50#if defined(OS_MACOSX)
51#define MAYBE_Basic DISABLED_Basic
52#else
53#define MAYBE_Basic Basic
54#endif
55
56IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, MAYBE_Basic) {
[email protected]1c3cf8b2010-05-11 23:18:2757 host_resolver()->AddRule("a.com", "127.0.0.1");
[email protected]e18a19b2010-10-19 20:32:3058 ASSERT_TRUE(StartTestServer());
[email protected]1c3cf8b2010-05-11 23:18:2759
[email protected]1870d5cf2011-05-12 01:55:4060 std::string app_manifest = base::StringPrintf(
[email protected]f08debf2010-10-23 10:13:0261 "{"
62 " \"name\": \"App\","
63 " \"version\": \"0.1\","
64 " \"app\": {"
65 " \"urls\": ["
66 " \"http://a.com/\""
67 " ],"
68 " \"launch\": {"
69 " \"web_url\": \"http://a.com:%d/\""
70 " }"
71 " },"
72 " \"permissions\": [\"background\"]"
73 "}",
74 test_server()->host_port_pair().port());
75
76 FilePath app_dir;
77 ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
78 ASSERT_TRUE(LoadExtension(app_dir));
[email protected]1c3cf8b2010-05-11 23:18:2779 ASSERT_TRUE(RunExtensionTest("app_background_page/basic")) << message_;
80}
81
[email protected]6aa25edc12011-10-06 02:16:1182// Crashy, http://crbug.com/69215.
[email protected]4b5e29f2011-01-11 07:44:0883IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, DISABLED_LacksPermission) {
[email protected]1c3cf8b2010-05-11 23:18:2784 host_resolver()->AddRule("a.com", "127.0.0.1");
[email protected]e18a19b2010-10-19 20:32:3085 ASSERT_TRUE(StartTestServer());
[email protected]1c3cf8b2010-05-11 23:18:2786
[email protected]1870d5cf2011-05-12 01:55:4087 std::string app_manifest = base::StringPrintf(
[email protected]f08debf2010-10-23 10:13:0288 "{"
89 " \"name\": \"App\","
90 " \"version\": \"0.1\","
91 " \"app\": {"
92 " \"urls\": ["
93 " \"http://a.com/\""
94 " ],"
95 " \"launch\": {"
96 " \"web_url\": \"http://a.com:%d/\""
97 " }"
98 " }"
99 "}",
100 test_server()->host_port_pair().port());
101
102 FilePath app_dir;
103 ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
104 ASSERT_TRUE(LoadExtension(app_dir));
[email protected]1c3cf8b2010-05-11 23:18:27105 ASSERT_TRUE(RunExtensionTest("app_background_page/lacks_permission"))
106 << message_;
107}
[email protected]41619a9a2011-04-13 20:07:32108
109IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, ManifestBackgroundPage) {
110 host_resolver()->AddRule("a.com", "127.0.0.1");
111 ASSERT_TRUE(StartTestServer());
112
[email protected]1870d5cf2011-05-12 01:55:40113 std::string app_manifest = base::StringPrintf(
[email protected]41619a9a2011-04-13 20:07:32114 "{"
115 " \"name\": \"App\","
116 " \"version\": \"0.1\","
117 " \"app\": {"
118 " \"urls\": ["
119 " \"http://a.com/\""
120 " ],"
121 " \"launch\": {"
122 " \"web_url\": \"http://a.com:%d/\""
123 " }"
124 " },"
125 " \"permissions\": [\"background\"],"
126 " \"background_page\": \"http://a.com:%d/test.html\""
127 "}",
128 test_server()->host_port_pair().port(),
129 test_server()->host_port_pair().port());
130
131 FilePath app_dir;
132 ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
133 ASSERT_TRUE(LoadExtension(app_dir));
134
135 const Extension* extension = GetSingleLoadedExtension();
[email protected]d2fad142011-04-15 10:18:44136 ASSERT_TRUE(
137 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())->
138 GetAppBackgroundContents(ASCIIToUTF16(extension->id())));
[email protected]41619a9a2011-04-13 20:07:32139}
[email protected]6aa25edc12011-10-06 02:16:11140
141IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, OpenTwoBackgroundPages) {
142 host_resolver()->AddRule("a.com", "127.0.0.1");
143 ASSERT_TRUE(StartTestServer());
144
145 std::string app_manifest = base::StringPrintf(
146 "{"
147 " \"name\": \"App\","
148 " \"version\": \"0.1\","
149 " \"app\": {"
150 " \"urls\": ["
151 " \"http://a.com/\""
152 " ],"
153 " \"launch\": {"
154 " \"web_url\": \"http://a.com:%d/\""
155 " }"
156 " },"
157 " \"permissions\": [\"background\"]"
158 "}",
159 test_server()->host_port_pair().port());
160
161 FilePath app_dir;
162 ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
163 ASSERT_TRUE(LoadExtension(app_dir));
164 ASSERT_TRUE(RunExtensionTest("app_background_page/two_pages")) << message_;
165}