OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/strings/stringprintf.h" | 5 #include "base/strings/stringprintf.h" |
6 #include "base/strings/utf_string_conversions.h" | 6 #include "base/strings/utf_string_conversions.h" |
7 #include "chrome/browser/background/background_contents_service.h" | 7 #include "chrome/browser/background/background_contents_service.h" |
8 #include "chrome/browser/background/background_contents_service_factory.h" | 8 #include "chrome/browser/background/background_contents_service_factory.h" |
9 #include "chrome/browser/background/background_mode_manager.h" | 9 #include "chrome/browser/background/background_mode_manager.h" |
10 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
11 #include "chrome/browser/extensions/extension_apitest.h" | 11 #include "chrome/browser/extensions/extension_apitest.h" |
12 #include "chrome/browser/extensions/extension_service.h" | 12 #include "chrome/browser/extensions/extension_service.h" |
13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
15 #include "chrome/browser/ui/browser_window.h" | 15 #include "chrome/browser/ui/browser_window.h" |
16 #include "chrome/common/chrome_notification_types.h" | 16 #include "chrome/common/chrome_notification_types.h" |
17 #include "chrome/common/chrome_switches.h" | 17 #include "chrome/common/chrome_switches.h" |
18 #include "chrome/common/extensions/extension.h" | 18 #include "chrome/common/extensions/extension.h" |
19 #include "content/public/browser/notification_service.h" | 19 #include "content/public/browser/notification_service.h" |
20 #include "content/public/test/test_notification_tracker.h" | 20 #include "content/public/test/test_notification_tracker.h" |
21 #include "content/public/test/test_utils.h" | 21 #include "content/public/test/test_utils.h" |
22 #include "net/dns/mock_host_resolver.h" | 22 #include "net/dns/mock_host_resolver.h" |
| 23 #include "net/test/embedded_test_server/embedded_test_server.h" |
23 | 24 |
24 #if defined(OS_MACOSX) | 25 #if defined(OS_MACOSX) |
25 #include "base/mac/scoped_nsautorelease_pool.h" | 26 #include "base/mac/scoped_nsautorelease_pool.h" |
26 #endif | 27 #endif |
27 | 28 |
28 using extensions::Extension; | 29 using extensions::Extension; |
29 | 30 |
30 class AppBackgroundPageApiTest : public ExtensionApiTest { | 31 class AppBackgroundPageApiTest : public ExtensionApiTest { |
31 public: | 32 public: |
32 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 33 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 " \"app\": {" | 125 " \"app\": {" |
125 " \"urls\": [" | 126 " \"urls\": [" |
126 " \"http://a.com/\"" | 127 " \"http://a.com/\"" |
127 " ]," | 128 " ]," |
128 " \"launch\": {" | 129 " \"launch\": {" |
129 " \"web_url\": \"http://a.com:%d/\"" | 130 " \"web_url\": \"http://a.com:%d/\"" |
130 " }" | 131 " }" |
131 " }," | 132 " }," |
132 " \"permissions\": [\"background\"]" | 133 " \"permissions\": [\"background\"]" |
133 "}", | 134 "}", |
134 test_server()->host_port_pair().port()); | 135 embedded_test_server()->port()); |
135 | 136 |
136 base::FilePath app_dir; | 137 base::FilePath app_dir; |
137 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); | 138 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); |
138 ASSERT_TRUE(LoadExtension(app_dir)); | 139 ASSERT_TRUE(LoadExtension(app_dir)); |
139 // Background mode should not be active until a background page is created. | 140 // Background mode should not be active until a background page is created. |
140 ASSERT_TRUE(WaitForBackgroundMode(false)); | 141 ASSERT_TRUE(WaitForBackgroundMode(false)); |
141 ASSERT_TRUE(RunExtensionTest("app_background_page/basic")) << message_; | 142 ASSERT_TRUE(RunExtensionTest("app_background_page/basic")) << message_; |
142 // The test closes the background contents, so we should fall back to no | 143 // The test closes the background contents, so we should fall back to no |
143 // background mode at the end. | 144 // background mode at the end. |
144 ASSERT_TRUE(WaitForBackgroundMode(false)); | 145 ASSERT_TRUE(WaitForBackgroundMode(false)); |
(...skipping 11 matching lines...) Expand all Loading... |
156 " \"manifest_version\": 2," | 157 " \"manifest_version\": 2," |
157 " \"app\": {" | 158 " \"app\": {" |
158 " \"urls\": [" | 159 " \"urls\": [" |
159 " \"http://a.com/\"" | 160 " \"http://a.com/\"" |
160 " ]," | 161 " ]," |
161 " \"launch\": {" | 162 " \"launch\": {" |
162 " \"web_url\": \"http://a.com:%d/\"" | 163 " \"web_url\": \"http://a.com:%d/\"" |
163 " }" | 164 " }" |
164 " }" | 165 " }" |
165 "}", | 166 "}", |
166 test_server()->host_port_pair().port()); | 167 embedded_test_server()->port()); |
167 | 168 |
168 base::FilePath app_dir; | 169 base::FilePath app_dir; |
169 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); | 170 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); |
170 ASSERT_TRUE(LoadExtension(app_dir)); | 171 ASSERT_TRUE(LoadExtension(app_dir)); |
171 ASSERT_TRUE(RunExtensionTest("app_background_page/lacks_permission")) | 172 ASSERT_TRUE(RunExtensionTest("app_background_page/lacks_permission")) |
172 << message_; | 173 << message_; |
173 ASSERT_TRUE(WaitForBackgroundMode(false)); | 174 ASSERT_TRUE(WaitForBackgroundMode(false)); |
174 } | 175 } |
175 | 176 |
176 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, ManifestBackgroundPage) { | 177 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, ManifestBackgroundPage) { |
(...skipping 11 matching lines...) Expand all Loading... |
188 " ]," | 189 " ]," |
189 " \"launch\": {" | 190 " \"launch\": {" |
190 " \"web_url\": \"http://a.com:%d/\"" | 191 " \"web_url\": \"http://a.com:%d/\"" |
191 " }" | 192 " }" |
192 " }," | 193 " }," |
193 " \"permissions\": [\"background\"]," | 194 " \"permissions\": [\"background\"]," |
194 " \"background\": {" | 195 " \"background\": {" |
195 " \"page\": \"http://a.com:%d/test.html\"" | 196 " \"page\": \"http://a.com:%d/test.html\"" |
196 " }" | 197 " }" |
197 "}", | 198 "}", |
198 test_server()->host_port_pair().port(), | 199 embedded_test_server()->port(), |
199 test_server()->host_port_pair().port()); | 200 embedded_test_server()->port()); |
200 | 201 |
201 base::FilePath app_dir; | 202 base::FilePath app_dir; |
202 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); | 203 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); |
203 // Background mode should not be active now because no background app was | 204 // Background mode should not be active now because no background app was |
204 // loaded. | 205 // loaded. |
205 ASSERT_TRUE(LoadExtension(app_dir)); | 206 ASSERT_TRUE(LoadExtension(app_dir)); |
206 // Background mode be active now because a background page was created when | 207 // Background mode be active now because a background page was created when |
207 // the app was loaded. | 208 // the app was loaded. |
208 ASSERT_TRUE(WaitForBackgroundMode(true)); | 209 ASSERT_TRUE(WaitForBackgroundMode(true)); |
209 | 210 |
(...skipping 26 matching lines...) Expand all Loading... |
236 " ]," | 237 " ]," |
237 " \"launch\": {" | 238 " \"launch\": {" |
238 " \"web_url\": \"http://a.com:%d/test.html\"" | 239 " \"web_url\": \"http://a.com:%d/test.html\"" |
239 " }" | 240 " }" |
240 " }," | 241 " }," |
241 " \"permissions\": [\"background\"]," | 242 " \"permissions\": [\"background\"]," |
242 " \"background\": {" | 243 " \"background\": {" |
243 " \"allow_js_access\": false" | 244 " \"allow_js_access\": false" |
244 " }" | 245 " }" |
245 "}", | 246 "}", |
246 test_server()->host_port_pair().port()); | 247 embedded_test_server()->port()); |
247 | 248 |
248 base::FilePath app_dir; | 249 base::FilePath app_dir; |
249 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); | 250 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); |
250 ASSERT_TRUE(LoadExtension(app_dir)); | 251 ASSERT_TRUE(LoadExtension(app_dir)); |
251 | 252 |
252 // There isn't a background page loaded initially. | 253 // There isn't a background page loaded initially. |
253 const Extension* extension = GetSingleLoadedExtension(); | 254 const Extension* extension = GetSingleLoadedExtension(); |
254 ASSERT_FALSE( | 255 ASSERT_FALSE( |
255 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())-> | 256 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())-> |
256 GetAppBackgroundContents(ASCIIToUTF16(extension->id()))); | 257 GetAppBackgroundContents(ASCIIToUTF16(extension->id()))); |
(...skipping 24 matching lines...) Expand all Loading... |
281 " \"launch\": {" | 282 " \"launch\": {" |
282 " \"web_url\": \"http://a.com:%d/\"" | 283 " \"web_url\": \"http://a.com:%d/\"" |
283 " }" | 284 " }" |
284 " }," | 285 " }," |
285 " \"permissions\": [\"background\"]," | 286 " \"permissions\": [\"background\"]," |
286 " \"background\": {" | 287 " \"background\": {" |
287 " \"page\": \"http://a.com:%d/bg.html\"," | 288 " \"page\": \"http://a.com:%d/bg.html\"," |
288 " \"allow_js_access\": false" | 289 " \"allow_js_access\": false" |
289 " }" | 290 " }" |
290 "}", | 291 "}", |
291 test_server()->host_port_pair().port(), | 292 embedded_test_server()->port(), |
292 test_server()->host_port_pair().port()); | 293 embedded_test_server()->port()); |
293 | 294 |
294 base::FilePath app_dir; | 295 base::FilePath app_dir; |
295 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); | 296 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); |
296 ASSERT_TRUE(LoadExtension(app_dir)); | 297 ASSERT_TRUE(LoadExtension(app_dir)); |
297 | 298 |
298 // The background page should load, but window.open should return null. | 299 // The background page should load, but window.open should return null. |
299 const Extension* extension = GetSingleLoadedExtension(); | 300 const Extension* extension = GetSingleLoadedExtension(); |
300 ASSERT_TRUE( | 301 ASSERT_TRUE( |
301 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())-> | 302 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())-> |
302 GetAppBackgroundContents(ASCIIToUTF16(extension->id()))); | 303 GetAppBackgroundContents(ASCIIToUTF16(extension->id()))); |
(...skipping 14 matching lines...) Expand all Loading... |
317 " \"app\": {" | 318 " \"app\": {" |
318 " \"urls\": [" | 319 " \"urls\": [" |
319 " \"http://a.com/\"" | 320 " \"http://a.com/\"" |
320 " ]," | 321 " ]," |
321 " \"launch\": {" | 322 " \"launch\": {" |
322 " \"web_url\": \"http://a.com:%d/\"" | 323 " \"web_url\": \"http://a.com:%d/\"" |
323 " }" | 324 " }" |
324 " }," | 325 " }," |
325 " \"permissions\": [\"background\"]" | 326 " \"permissions\": [\"background\"]" |
326 "}", | 327 "}", |
327 test_server()->host_port_pair().port()); | 328 embedded_test_server()->port()); |
328 | 329 |
329 base::FilePath app_dir; | 330 base::FilePath app_dir; |
330 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); | 331 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); |
331 ASSERT_TRUE(LoadExtension(app_dir)); | 332 ASSERT_TRUE(LoadExtension(app_dir)); |
332 const Extension* extension = GetSingleLoadedExtension(); | 333 const Extension* extension = GetSingleLoadedExtension(); |
333 ASSERT_TRUE(RunExtensionTest("app_background_page/two_pages")) << message_; | 334 ASSERT_TRUE(RunExtensionTest("app_background_page/two_pages")) << message_; |
334 UnloadExtension(extension->id()); | 335 UnloadExtension(extension->id()); |
335 } | 336 } |
336 | 337 |
337 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, OpenTwoPagesWithManifest) { | 338 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, OpenTwoPagesWithManifest) { |
(...skipping 11 matching lines...) Expand all Loading... |
349 " ]," | 350 " ]," |
350 " \"launch\": {" | 351 " \"launch\": {" |
351 " \"web_url\": \"http://a.com:%d/\"" | 352 " \"web_url\": \"http://a.com:%d/\"" |
352 " }" | 353 " }" |
353 " }," | 354 " }," |
354 " \"background\": {" | 355 " \"background\": {" |
355 " \"page\": \"http://a.com:%d/bg.html\"" | 356 " \"page\": \"http://a.com:%d/bg.html\"" |
356 " }," | 357 " }," |
357 " \"permissions\": [\"background\"]" | 358 " \"permissions\": [\"background\"]" |
358 "}", | 359 "}", |
359 test_server()->host_port_pair().port(), | 360 embedded_test_server()->port(), |
360 test_server()->host_port_pair().port()); | 361 embedded_test_server()->port()); |
361 | 362 |
362 base::FilePath app_dir; | 363 base::FilePath app_dir; |
363 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); | 364 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); |
364 ASSERT_TRUE(LoadExtension(app_dir)); | 365 ASSERT_TRUE(LoadExtension(app_dir)); |
365 const Extension* extension = GetSingleLoadedExtension(); | 366 const Extension* extension = GetSingleLoadedExtension(); |
366 ASSERT_TRUE(RunExtensionTest("app_background_page/two_with_manifest")) << | 367 ASSERT_TRUE(RunExtensionTest("app_background_page/two_with_manifest")) << |
367 message_; | 368 message_; |
368 UnloadExtension(extension->id()); | 369 UnloadExtension(extension->id()); |
369 } | 370 } |
370 | 371 |
371 // Times out occasionally -- see crbug.com/108493 | 372 // Times out occasionally -- see crbug.com/108493 |
372 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, DISABLED_OpenPopupFromBGPage) { | 373 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, DISABLED_OpenPopupFromBGPage) { |
373 host_resolver()->AddRule("a.com", "127.0.0.1"); | 374 host_resolver()->AddRule("a.com", "127.0.0.1"); |
374 ASSERT_TRUE(StartTestServer()); | 375 ASSERT_TRUE(StartTestServer()); |
375 | 376 |
376 std::string app_manifest = base::StringPrintf( | 377 std::string app_manifest = base::StringPrintf( |
377 "{" | 378 "{" |
378 " \"name\": \"App\"," | 379 " \"name\": \"App\"," |
379 " \"version\": \"0.1\"," | 380 " \"version\": \"0.1\"," |
380 " \"manifest_version\": 2," | 381 " \"manifest_version\": 2," |
381 " \"app\": {" | 382 " \"app\": {" |
382 " \"urls\": [" | 383 " \"urls\": [" |
383 " \"http://a.com/\"" | 384 " \"http://a.com/\"" |
384 " ]," | 385 " ]," |
385 " \"launch\": {" | 386 " \"launch\": {" |
386 " \"web_url\": \"http://a.com:%d/\"" | 387 " \"web_url\": \"http://a.com:%d/\"" |
387 " }" | 388 " }" |
388 " }," | 389 " }," |
389 " \"background\": { \"page\": \"http://a.com:%d/files/extensions/api_test
/" | 390 " \"background\": { \"page\": \"http://a.com:%d/extensions/api_test/" |
390 "app_background_page/bg_open/bg_open_bg.html\" }," | 391 "app_background_page/bg_open/bg_open_bg.html\" }," |
391 " \"permissions\": [\"background\"]" | 392 " \"permissions\": [\"background\"]" |
392 "}", | 393 "}", |
393 test_server()->host_port_pair().port(), | 394 embedded_test_server()->port(), |
394 test_server()->host_port_pair().port()); | 395 embedded_test_server()->port()); |
395 | 396 |
396 base::FilePath app_dir; | 397 base::FilePath app_dir; |
397 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); | 398 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); |
398 ASSERT_TRUE(LoadExtension(app_dir)); | 399 ASSERT_TRUE(LoadExtension(app_dir)); |
399 ASSERT_TRUE(RunExtensionTest("app_background_page/bg_open")) << message_; | 400 ASSERT_TRUE(RunExtensionTest("app_background_page/bg_open")) << message_; |
400 } | 401 } |
401 | 402 |
402 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, DISABLED_OpenThenClose) { | 403 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, DISABLED_OpenThenClose) { |
403 host_resolver()->AddRule("a.com", "127.0.0.1"); | 404 host_resolver()->AddRule("a.com", "127.0.0.1"); |
404 ASSERT_TRUE(StartTestServer()); | 405 ASSERT_TRUE(StartTestServer()); |
405 | 406 |
406 std::string app_manifest = base::StringPrintf( | 407 std::string app_manifest = base::StringPrintf( |
407 "{" | 408 "{" |
408 " \"name\": \"App\"," | 409 " \"name\": \"App\"," |
409 " \"version\": \"0.1\"," | 410 " \"version\": \"0.1\"," |
410 " \"manifest_version\": 2," | 411 " \"manifest_version\": 2," |
411 " \"app\": {" | 412 " \"app\": {" |
412 " \"urls\": [" | 413 " \"urls\": [" |
413 " \"http://a.com/\"" | 414 " \"http://a.com/\"" |
414 " ]," | 415 " ]," |
415 " \"launch\": {" | 416 " \"launch\": {" |
416 " \"web_url\": \"http://a.com:%d/\"" | 417 " \"web_url\": \"http://a.com:%d/\"" |
417 " }" | 418 " }" |
418 " }," | 419 " }," |
419 " \"permissions\": [\"background\"]" | 420 " \"permissions\": [\"background\"]" |
420 "}", | 421 "}", |
421 test_server()->host_port_pair().port()); | 422 embedded_test_server()->port()); |
422 | 423 |
423 base::FilePath app_dir; | 424 base::FilePath app_dir; |
424 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); | 425 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); |
425 ASSERT_TRUE(LoadExtension(app_dir)); | 426 ASSERT_TRUE(LoadExtension(app_dir)); |
426 // There isn't a background page loaded initially. | 427 // There isn't a background page loaded initially. |
427 const Extension* extension = GetSingleLoadedExtension(); | 428 const Extension* extension = GetSingleLoadedExtension(); |
428 ASSERT_FALSE( | 429 ASSERT_FALSE( |
429 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())-> | 430 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())-> |
430 GetAppBackgroundContents(ASCIIToUTF16(extension->id()))); | 431 GetAppBackgroundContents(ASCIIToUTF16(extension->id()))); |
431 // Background mode should not be active until a background page is created. | 432 // Background mode should not be active until a background page is created. |
(...skipping 28 matching lines...) Expand all Loading... |
460 " ]," | 461 " ]," |
461 " \"launch\": {" | 462 " \"launch\": {" |
462 " \"web_url\": \"http://a.com:%d/\"" | 463 " \"web_url\": \"http://a.com:%d/\"" |
463 " }" | 464 " }" |
464 " }," | 465 " }," |
465 " \"permissions\": [\"background\"]," | 466 " \"permissions\": [\"background\"]," |
466 " \"background\": {" | 467 " \"background\": {" |
467 " \"page\": \"http://a.com:%d/test.html\"" | 468 " \"page\": \"http://a.com:%d/test.html\"" |
468 " }" | 469 " }" |
469 "}", | 470 "}", |
470 test_server()->host_port_pair().port(), | 471 embedded_test_server()->port(), |
471 test_server()->host_port_pair().port()); | 472 embedded_test_server()->port()); |
472 | 473 |
473 base::FilePath app_dir; | 474 base::FilePath app_dir; |
474 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); | 475 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); |
475 // Background mode should not be active now because no background app was | 476 // Background mode should not be active now because no background app was |
476 // loaded. | 477 // loaded. |
477 ASSERT_TRUE(LoadExtension(app_dir)); | 478 ASSERT_TRUE(LoadExtension(app_dir)); |
478 // Background mode be active now because a background page was created when | 479 // Background mode be active now because a background page was created when |
479 // the app was loaded. | 480 // the app was loaded. |
480 ASSERT_TRUE(WaitForBackgroundMode(true)); | 481 ASSERT_TRUE(WaitForBackgroundMode(true)); |
481 | 482 |
482 const Extension* extension = GetSingleLoadedExtension(); | 483 const Extension* extension = GetSingleLoadedExtension(); |
483 ASSERT_TRUE( | 484 ASSERT_TRUE( |
484 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())-> | 485 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())-> |
485 GetAppBackgroundContents(ASCIIToUTF16(extension->id()))); | 486 GetAppBackgroundContents(ASCIIToUTF16(extension->id()))); |
486 | 487 |
487 // Close all browsers - app should continue running. | 488 // Close all browsers - app should continue running. |
488 set_exit_when_last_browser_closes(false); | 489 set_exit_when_last_browser_closes(false); |
489 CloseBrowser(browser()); | 490 CloseBrowser(browser()); |
490 | 491 |
491 // Post a task to unload the extension - this should cause Chrome to exit | 492 // Post a task to unload the extension - this should cause Chrome to exit |
492 // cleanly (not crash). | 493 // cleanly (not crash). |
493 UnloadExtensionViaTask(extension->id()); | 494 UnloadExtensionViaTask(extension->id()); |
494 content::RunAllPendingInMessageLoop(); | 495 content::RunAllPendingInMessageLoop(); |
495 ASSERT_TRUE(WaitForBackgroundMode(false)); | 496 ASSERT_TRUE(WaitForBackgroundMode(false)); |
496 } | 497 } |
OLD | NEW |