blob: 9c9585ba2a2c1580a42af58927ca9cfe84a567f2 [file] [log] [blame]
[email protected]eb75f7b2012-01-04 09:07:281// Copyright (c) 2012 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:
[email protected]78863852011-10-14 19:56:2620 void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
[email protected]1c3cf8b2010-05-11 23:18:2721 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\","
[email protected]1c0132c2011-12-03 04:19:3264 " \"manifest_version\": 2,"
[email protected]f08debf2010-10-23 10:13:0265 " \"app\": {"
66 " \"urls\": ["
67 " \"http://a.com/\""
68 " ],"
69 " \"launch\": {"
70 " \"web_url\": \"http://a.com:%d/\""
71 " }"
72 " },"
73 " \"permissions\": [\"background\"]"
74 "}",
75 test_server()->host_port_pair().port());
76
77 FilePath app_dir;
78 ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
79 ASSERT_TRUE(LoadExtension(app_dir));
[email protected]1c3cf8b2010-05-11 23:18:2780 ASSERT_TRUE(RunExtensionTest("app_background_page/basic")) << message_;
81}
82
[email protected]6aa25edc12011-10-06 02:16:1183// Crashy, http://crbug.com/69215.
[email protected]4b5e29f2011-01-11 07:44:0884IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, DISABLED_LacksPermission) {
[email protected]1c3cf8b2010-05-11 23:18:2785 host_resolver()->AddRule("a.com", "127.0.0.1");
[email protected]e18a19b2010-10-19 20:32:3086 ASSERT_TRUE(StartTestServer());
[email protected]1c3cf8b2010-05-11 23:18:2787
[email protected]1870d5cf2011-05-12 01:55:4088 std::string app_manifest = base::StringPrintf(
[email protected]f08debf2010-10-23 10:13:0289 "{"
90 " \"name\": \"App\","
91 " \"version\": \"0.1\","
[email protected]1c0132c2011-12-03 04:19:3292 " \"manifest_version\": 2,"
[email protected]f08debf2010-10-23 10:13:0293 " \"app\": {"
94 " \"urls\": ["
95 " \"http://a.com/\""
96 " ],"
97 " \"launch\": {"
98 " \"web_url\": \"http://a.com:%d/\""
99 " }"
100 " }"
101 "}",
102 test_server()->host_port_pair().port());
103
104 FilePath app_dir;
105 ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
106 ASSERT_TRUE(LoadExtension(app_dir));
[email protected]1c3cf8b2010-05-11 23:18:27107 ASSERT_TRUE(RunExtensionTest("app_background_page/lacks_permission"))
108 << message_;
109}
[email protected]41619a9a2011-04-13 20:07:32110
111IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, ManifestBackgroundPage) {
112 host_resolver()->AddRule("a.com", "127.0.0.1");
113 ASSERT_TRUE(StartTestServer());
114
[email protected]1870d5cf2011-05-12 01:55:40115 std::string app_manifest = base::StringPrintf(
[email protected]41619a9a2011-04-13 20:07:32116 "{"
117 " \"name\": \"App\","
118 " \"version\": \"0.1\","
[email protected]1c0132c2011-12-03 04:19:32119 " \"manifest_version\": 2,"
[email protected]41619a9a2011-04-13 20:07:32120 " \"app\": {"
121 " \"urls\": ["
122 " \"http://a.com/\""
123 " ],"
124 " \"launch\": {"
125 " \"web_url\": \"http://a.com:%d/\""
126 " }"
127 " },"
128 " \"permissions\": [\"background\"],"
[email protected]eb75f7b2012-01-04 09:07:28129 " \"background\": {"
130 " \"page\": \"http://a.com:%d/test.html\""
131 " }"
[email protected]41619a9a2011-04-13 20:07:32132 "}",
133 test_server()->host_port_pair().port(),
134 test_server()->host_port_pair().port());
135
136 FilePath app_dir;
137 ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
138 ASSERT_TRUE(LoadExtension(app_dir));
139
140 const Extension* extension = GetSingleLoadedExtension();
[email protected]d2fad142011-04-15 10:18:44141 ASSERT_TRUE(
142 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())->
143 GetAppBackgroundContents(ASCIIToUTF16(extension->id())));
[email protected]41619a9a2011-04-13 20:07:32144}
[email protected]6aa25edc12011-10-06 02:16:11145
146IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, OpenTwoBackgroundPages) {
147 host_resolver()->AddRule("a.com", "127.0.0.1");
148 ASSERT_TRUE(StartTestServer());
149
150 std::string app_manifest = base::StringPrintf(
151 "{"
152 " \"name\": \"App\","
153 " \"version\": \"0.1\","
[email protected]1c0132c2011-12-03 04:19:32154 " \"manifest_version\": 2,"
[email protected]6aa25edc12011-10-06 02:16:11155 " \"app\": {"
156 " \"urls\": ["
157 " \"http://a.com/\""
158 " ],"
159 " \"launch\": {"
160 " \"web_url\": \"http://a.com:%d/\""
161 " }"
162 " },"
163 " \"permissions\": [\"background\"]"
164 "}",
165 test_server()->host_port_pair().port());
166
167 FilePath app_dir;
168 ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
169 ASSERT_TRUE(LoadExtension(app_dir));
170 ASSERT_TRUE(RunExtensionTest("app_background_page/two_pages")) << message_;
171}
[email protected]78863852011-10-14 19:56:26172
173IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, OpenTwoPagesWithManifest) {
174 host_resolver()->AddRule("a.com", "127.0.0.1");
175 ASSERT_TRUE(StartTestServer());
176
177 std::string app_manifest = base::StringPrintf(
178 "{"
179 " \"name\": \"App\","
180 " \"version\": \"0.1\","
[email protected]1c0132c2011-12-03 04:19:32181 " \"manifest_version\": 2,"
[email protected]78863852011-10-14 19:56:26182 " \"app\": {"
183 " \"urls\": ["
184 " \"http://a.com/\""
185 " ],"
186 " \"launch\": {"
187 " \"web_url\": \"http://a.com:%d/\""
188 " }"
189 " },"
[email protected]eb75f7b2012-01-04 09:07:28190 " \"background\": {"
191 " \"page\": \"http://a.com:%d/bg.html\""
192 " },"
[email protected]78863852011-10-14 19:56:26193 " \"permissions\": [\"background\"]"
194 "}",
195 test_server()->host_port_pair().port(),
196 test_server()->host_port_pair().port());
197
198 FilePath app_dir;
199 ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
200 ASSERT_TRUE(LoadExtension(app_dir));
201 ASSERT_TRUE(RunExtensionTest("app_background_page/two_with_manifest")) <<
202 message_;
203}
[email protected]ae80a882011-12-14 05:59:05204
[email protected]eef2e6c2011-12-22 23:34:50205// Times out occasionally -- see crbug.com/108493
[email protected]378f4982011-12-23 01:55:48206IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, DISABLED_OpenPopupFromBGPage) {
[email protected]ae80a882011-12-14 05:59:05207 host_resolver()->AddRule("a.com", "127.0.0.1");
208 ASSERT_TRUE(StartTestServer());
209
210 std::string app_manifest = base::StringPrintf(
211 "{"
212 " \"name\": \"App\","
213 " \"version\": \"0.1\","
214 " \"manifest_version\": 2,"
215 " \"app\": {"
216 " \"urls\": ["
217 " \"http://a.com/\""
218 " ],"
219 " \"launch\": {"
220 " \"web_url\": \"http://a.com:%d/\""
221 " }"
222 " },"
[email protected]eb75f7b2012-01-04 09:07:28223 " \"background\": { \"page\": \"http://a.com:%d/files/extensions/api_test/"
224 "app_background_page/bg_open/bg_open_bg.html\" },"
[email protected]ae80a882011-12-14 05:59:05225 " \"permissions\": [\"background\"]"
226 "}",
227 test_server()->host_port_pair().port(),
228 test_server()->host_port_pair().port());
229
230 FilePath app_dir;
231 ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
232 ASSERT_TRUE(LoadExtension(app_dir));
233 ASSERT_TRUE(RunExtensionTest("app_background_page/bg_open")) << message_;
234}