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/command_line.h" | 5 #include "base/command_line.h" |
6 #include "base/files/file_path.h" | 6 #include "base/files/file_path.h" |
7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
8 #include "chrome/browser/bookmarks/bookmark_model.h" | 8 #include "chrome/browser/bookmarks/bookmark_model.h" |
9 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 9 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
10 #include "chrome/browser/bookmarks/bookmark_test_helpers.h" | 10 #include "chrome/browser/bookmarks/bookmark_test_helpers.h" |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 }; | 74 }; |
75 | 75 |
76 } // namespace | 76 } // namespace |
77 | 77 |
78 class LazyBackgroundPageApiTest : public ExtensionApiTest { | 78 class LazyBackgroundPageApiTest : public ExtensionApiTest { |
79 public: | 79 public: |
80 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 80 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
81 ExtensionApiTest::SetUpCommandLine(command_line); | 81 ExtensionApiTest::SetUpCommandLine(command_line); |
82 // Set shorter delays to prevent test timeouts. | 82 // Set shorter delays to prevent test timeouts. |
83 command_line->AppendSwitchASCII( | 83 command_line->AppendSwitchASCII( |
84 extensions::switches::kEventPageIdleTime, "1"); | 84 extensions::switches::kEventPageIdleTime, "1000"); |
85 command_line->AppendSwitchASCII( | 85 command_line->AppendSwitchASCII( |
86 extensions::switches::kEventPageSuspendingTime, "1"); | 86 extensions::switches::kEventPageSuspendingTime, "1000"); |
87 } | 87 } |
88 | 88 |
89 // Loads the extension, which temporarily starts the lazy background page | 89 // Loads the extension, which temporarily starts the lazy background page |
90 // to dispatch the onInstalled event. We wait until it shuts down again. | 90 // to dispatch the onInstalled event. We wait until it shuts down again. |
91 const Extension* LoadExtensionAndWait(const std::string& test_name) { | 91 const Extension* LoadExtensionAndWait(const std::string& test_name) { |
92 LazyBackgroundObserver page_complete; | 92 LazyBackgroundObserver page_complete; |
93 base::FilePath extdir = test_data_dir_.AppendASCII("lazy_background_page"). | 93 base::FilePath extdir = test_data_dir_.AppendASCII("lazy_background_page"). |
94 AppendASCII(test_name); | 94 AppendASCII(test_name); |
95 const Extension* extension = LoadExtension(extdir); | 95 const Extension* extension = LoadExtension(extdir); |
96 if (extension) | 96 if (extension) |
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
404 EXPECT_TRUE(pm->GetBackgroundHostForExtension(last_loaded_extension_id())); | 404 EXPECT_TRUE(pm->GetBackgroundHostForExtension(last_loaded_extension_id())); |
405 | 405 |
406 // Navigate away, closing the message channel and therefore the background | 406 // Navigate away, closing the message channel and therefore the background |
407 // page. | 407 // page. |
408 ui_test_utils::NavigateToURL(browser(), GURL("about:blank")); | 408 ui_test_utils::NavigateToURL(browser(), GURL("about:blank")); |
409 lazybg.WaitUntilClosed(); | 409 lazybg.WaitUntilClosed(); |
410 | 410 |
411 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id())); | 411 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id())); |
412 } | 412 } |
413 | 413 |
| 414 // Tests that a KeepaliveImpulse increments the keep alive count, but eventually |
| 415 // times out and background page will still close. |
| 416 IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, ImpulseAddsCount) { |
| 417 ASSERT_TRUE(StartEmbeddedTestServer()); |
| 418 const Extension* extension = LoadExtensionAndWait("messaging"); |
| 419 ASSERT_TRUE(extension); |
| 420 |
| 421 // Lazy Background Page doesn't exist yet. |
| 422 extensions::ProcessManager* pm = |
| 423 extensions::ExtensionSystem::Get(browser()->profile())->process_manager(); |
| 424 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id())); |
| 425 EXPECT_EQ(1, browser()->tab_strip_model()->count()); |
| 426 |
| 427 // Navigate to a page that opens a message channel to the background page. |
| 428 ResultCatcher catcher; |
| 429 LazyBackgroundObserver lazybg; |
| 430 ui_test_utils::NavigateToURL( |
| 431 browser(), embedded_test_server()->GetURL("/extensions/test_file.html")); |
| 432 lazybg.WaitUntilLoaded(); |
| 433 |
| 434 // Add an impulse and the keep alive count increases. |
| 435 int previous_keep_alive_count = pm->GetLazyKeepaliveCount(extension); |
| 436 pm->KeepaliveImpulse(extension); |
| 437 EXPECT_EQ(previous_keep_alive_count + 1, |
| 438 pm->GetLazyKeepaliveCount(extension)); |
| 439 |
| 440 // Navigate away, closing the message channel and therefore the background |
| 441 // page after the impulse times out. |
| 442 ui_test_utils::NavigateToURL(browser(), GURL("about:blank")); |
| 443 lazybg.WaitUntilClosed(); |
| 444 |
| 445 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id())); |
| 446 } |
| 447 |
414 // Tests that the lazy background page receives the unload event when we | 448 // Tests that the lazy background page receives the unload event when we |
415 // close it, and that it can execute simple API calls that don't require an | 449 // close it, and that it can execute simple API calls that don't require an |
416 // asynchronous response. | 450 // asynchronous response. |
417 IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, OnUnload) { | 451 IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, OnUnload) { |
418 ASSERT_TRUE(LoadExtensionAndWait("on_unload")); | 452 ASSERT_TRUE(LoadExtensionAndWait("on_unload")); |
419 | 453 |
420 // Lazy Background Page has been shut down. | 454 // Lazy Background Page has been shut down. |
421 extensions::ProcessManager* pm = | 455 extensions::ProcessManager* pm = |
422 extensions::ExtensionSystem::Get(browser()->profile())->process_manager(); | 456 extensions::ExtensionSystem::Get(browser()->profile())->process_manager(); |
423 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id())); | 457 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id())); |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
509 | 543 |
510 // Tests that the lazy background page will be unloaded if the onSuspend event | 544 // Tests that the lazy background page will be unloaded if the onSuspend event |
511 // handler calls an API function such as chrome.storage.local.set(). | 545 // handler calls an API function such as chrome.storage.local.set(). |
512 // See: http://crbug.com/296834 | 546 // See: http://crbug.com/296834 |
513 IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, OnSuspendUseStorageApi) { | 547 IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, OnSuspendUseStorageApi) { |
514 EXPECT_TRUE(LoadExtensionAndWait("on_suspend")); | 548 EXPECT_TRUE(LoadExtensionAndWait("on_suspend")); |
515 } | 549 } |
516 | 550 |
517 // TODO: background page with timer. | 551 // TODO: background page with timer. |
518 // TODO: background page that interacts with popup. | 552 // TODO: background page that interacts with popup. |
OLD | NEW |