| 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_utils.h" | 10 #include "chrome/browser/bookmarks/bookmark_utils.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 #include "chrome/common/chrome_notification_types.h" | 23 #include "chrome/common/chrome_notification_types.h" |
| 24 #include "chrome/common/chrome_switches.h" | 24 #include "chrome/common/chrome_switches.h" |
| 25 #include "chrome/common/extensions/extension.h" | 25 #include "chrome/common/extensions/extension.h" |
| 26 #include "chrome/common/url_constants.h" | 26 #include "chrome/common/url_constants.h" |
| 27 #include "chrome/test/base/ui_test_utils.h" | 27 #include "chrome/test/base/ui_test_utils.h" |
| 28 #include "content/public/browser/notification_service.h" | 28 #include "content/public/browser/notification_service.h" |
| 29 #include "content/public/browser/web_contents.h" | 29 #include "content/public/browser/web_contents.h" |
| 30 #include "content/public/test/browser_test_utils.h" | 30 #include "content/public/test/browser_test_utils.h" |
| 31 #include "googleurl/src/gurl.h" | 31 #include "googleurl/src/gurl.h" |
| 32 #include "net/dns/mock_host_resolver.h" | 32 #include "net/dns/mock_host_resolver.h" |
| 33 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 33 | 34 |
| 34 using extensions::Extension; | 35 using extensions::Extension; |
| 35 | 36 |
| 36 namespace { | 37 namespace { |
| 37 | 38 |
| 38 // This unfortunate bit of silliness is necessary when loading an extension in | 39 // This unfortunate bit of silliness is necessary when loading an extension in |
| 39 // incognito. The goal is to load the extension, enable incognito, then wait | 40 // incognito. The goal is to load the extension, enable incognito, then wait |
| 40 // for both background pages to load and close. The problem is that enabling | 41 // for both background pages to load and close. The problem is that enabling |
| 41 // incognito involves reloading the extension - and the background pages may | 42 // incognito involves reloading the extension - and the background pages may |
| 42 // have already loaded once before then. So we wait until the extension is | 43 // have already loaded once before then. So we wait until the extension is |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 LazyBackgroundObserver page_complete; | 135 LazyBackgroundObserver page_complete; |
| 135 BrowserActionTestUtil(browser()).Press(0); | 136 BrowserActionTestUtil(browser()).Press(0); |
| 136 page_complete.Wait(); | 137 page_complete.Wait(); |
| 137 | 138 |
| 138 // Background page is closed after creating a new tab. | 139 // Background page is closed after creating a new tab. |
| 139 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_)); | 140 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_)); |
| 140 EXPECT_EQ(num_tabs_before + 1, browser()->tab_strip_model()->count()); | 141 EXPECT_EQ(num_tabs_before + 1, browser()->tab_strip_model()->count()); |
| 141 } | 142 } |
| 142 | 143 |
| 143 IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, BroadcastEvent) { | 144 IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, BroadcastEvent) { |
| 144 ASSERT_TRUE(StartTestServer()); | 145 ASSERT_TRUE(StartEmbeddedTestServer()); |
| 145 | 146 |
| 146 const Extension* extension = LoadExtensionAndWait("broadcast_event"); | 147 const Extension* extension = LoadExtensionAndWait("broadcast_event"); |
| 147 ASSERT_TRUE(extension); | 148 ASSERT_TRUE(extension); |
| 148 | 149 |
| 149 // Lazy Background Page doesn't exist yet. | 150 // Lazy Background Page doesn't exist yet. |
| 150 ExtensionProcessManager* pm = | 151 ExtensionProcessManager* pm = |
| 151 extensions::ExtensionSystem::Get(browser()->profile())->process_manager(); | 152 extensions::ExtensionSystem::Get(browser()->profile())->process_manager(); |
| 152 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_)); | 153 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_)); |
| 153 int num_page_actions = browser()->window()->GetLocationBar()-> | 154 int num_page_actions = browser()->window()->GetLocationBar()-> |
| 154 GetLocationBarForTesting()->PageActionVisibleCount(); | 155 GetLocationBarForTesting()->PageActionVisibleCount(); |
| 155 | 156 |
| 156 // Open a tab to a URL that will trigger the page action to show. | 157 // Open a tab to a URL that will trigger the page action to show. |
| 157 LazyBackgroundObserver page_complete; | 158 LazyBackgroundObserver page_complete; |
| 158 content::WindowedNotificationObserver page_action_changed( | 159 content::WindowedNotificationObserver page_action_changed( |
| 159 chrome::NOTIFICATION_EXTENSION_PAGE_ACTION_VISIBILITY_CHANGED, | 160 chrome::NOTIFICATION_EXTENSION_PAGE_ACTION_VISIBILITY_CHANGED, |
| 160 content::NotificationService::AllSources()); | 161 content::NotificationService::AllSources()); |
| 161 ui_test_utils::NavigateToURL( | 162 ui_test_utils::NavigateToURL( |
| 162 browser(), test_server()->GetURL("files/extensions/test_file.html")); | 163 browser(), embedded_test_server()->GetURL("/extensions/test_file.html")); |
| 163 page_complete.Wait(); | 164 page_complete.Wait(); |
| 164 | 165 |
| 165 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_)); | 166 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_)); |
| 166 | 167 |
| 167 // Page action is shown. | 168 // Page action is shown. |
| 168 page_action_changed.Wait(); | 169 page_action_changed.Wait(); |
| 169 EXPECT_EQ(num_page_actions + 1, | 170 EXPECT_EQ(num_page_actions + 1, |
| 170 browser()->window()->GetLocationBar()-> | 171 browser()->window()->GetLocationBar()-> |
| 171 GetLocationBarForTesting()->PageActionVisibleCount()); | 172 GetLocationBarForTesting()->PageActionVisibleCount()); |
| 172 } | 173 } |
| 173 | 174 |
| 174 IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, Filters) { | 175 IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, Filters) { |
| 175 const Extension* extension = LoadExtensionAndWait("filters"); | 176 const Extension* extension = LoadExtensionAndWait("filters"); |
| 176 ASSERT_TRUE(extension); | 177 ASSERT_TRUE(extension); |
| 177 | 178 |
| 178 // Lazy Background Page doesn't exist yet. | 179 // Lazy Background Page doesn't exist yet. |
| 179 ExtensionProcessManager* pm = | 180 ExtensionProcessManager* pm = |
| 180 extensions::ExtensionSystem::Get(browser()->profile())->process_manager(); | 181 extensions::ExtensionSystem::Get(browser()->profile())->process_manager(); |
| 181 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_)); | 182 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_)); |
| 182 | 183 |
| 183 // Open a tab to a URL that will fire a webNavigation event. | 184 // Open a tab to a URL that will fire a webNavigation event. |
| 184 LazyBackgroundObserver page_complete; | 185 LazyBackgroundObserver page_complete; |
| 185 ui_test_utils::NavigateToURL( | 186 ui_test_utils::NavigateToURL( |
| 186 browser(), test_server()->GetURL("files/extensions/test_file.html")); | 187 browser(), embedded_test_server()->GetURL("/extensions/test_file.html")); |
| 187 page_complete.Wait(); | 188 page_complete.Wait(); |
| 188 } | 189 } |
| 189 | 190 |
| 190 // Tests that the lazy background page receives the onInstalled event and shuts | 191 // Tests that the lazy background page receives the onInstalled event and shuts |
| 191 // down. | 192 // down. |
| 192 IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, OnInstalled) { | 193 IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, OnInstalled) { |
| 193 ResultCatcher catcher; | 194 ResultCatcher catcher; |
| 194 ASSERT_TRUE(LoadExtensionAndWait("on_installed")); | 195 ASSERT_TRUE(LoadExtensionAndWait("on_installed")); |
| 195 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 196 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 196 | 197 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 page_complete.Wait(); | 229 page_complete.Wait(); |
| 229 | 230 |
| 230 // Lazy Background Page has been shut down. | 231 // Lazy Background Page has been shut down. |
| 231 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_)); | 232 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_)); |
| 232 } | 233 } |
| 233 | 234 |
| 234 // Tests that the lazy background page stays alive until all network requests | 235 // Tests that the lazy background page stays alive until all network requests |
| 235 // are complete. | 236 // are complete. |
| 236 IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, WaitForRequest) { | 237 IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, WaitForRequest) { |
| 237 host_resolver()->AddRule("*", "127.0.0.1"); | 238 host_resolver()->AddRule("*", "127.0.0.1"); |
| 238 ASSERT_TRUE(StartTestServer()); | 239 ASSERT_TRUE(StartEmbeddedTestServer()); |
| 239 | 240 |
| 240 LazyBackgroundObserver page_complete; | 241 LazyBackgroundObserver page_complete; |
| 241 ResultCatcher catcher; | 242 ResultCatcher catcher; |
| 242 base::FilePath extdir = test_data_dir_.AppendASCII("lazy_background_page"). | 243 base::FilePath extdir = test_data_dir_.AppendASCII("lazy_background_page"). |
| 243 AppendASCII("wait_for_request"); | 244 AppendASCII("wait_for_request"); |
| 244 const Extension* extension = LoadExtension(extdir); | 245 const Extension* extension = LoadExtension(extdir); |
| 245 ASSERT_TRUE(extension); | 246 ASSERT_TRUE(extension); |
| 246 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 247 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 247 | 248 |
| 248 // Lazy Background Page still exists, because the extension started a request. | 249 // Lazy Background Page still exists, because the extension started a request. |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 369 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_)); | 370 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_)); |
| 370 EXPECT_FALSE(pmi->GetBackgroundHostForExtension(last_loaded_extension_id_)); | 371 EXPECT_FALSE(pmi->GetBackgroundHostForExtension(last_loaded_extension_id_)); |
| 371 EXPECT_TRUE(listener.was_satisfied()); | 372 EXPECT_TRUE(listener.was_satisfied()); |
| 372 EXPECT_TRUE(listener_incognito.was_satisfied()); | 373 EXPECT_TRUE(listener_incognito.was_satisfied()); |
| 373 } | 374 } |
| 374 } | 375 } |
| 375 | 376 |
| 376 // Tests that messages from the content script activate the lazy background | 377 // Tests that messages from the content script activate the lazy background |
| 377 // page, and keep it alive until all channels are closed. | 378 // page, and keep it alive until all channels are closed. |
| 378 IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, Messaging) { | 379 IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, Messaging) { |
| 379 ASSERT_TRUE(StartTestServer()); | 380 ASSERT_TRUE(StartEmbeddedTestServer()); |
| 380 ASSERT_TRUE(LoadExtensionAndWait("messaging")); | 381 ASSERT_TRUE(LoadExtensionAndWait("messaging")); |
| 381 | 382 |
| 382 // Lazy Background Page doesn't exist yet. | 383 // Lazy Background Page doesn't exist yet. |
| 383 ExtensionProcessManager* pm = | 384 ExtensionProcessManager* pm = |
| 384 extensions::ExtensionSystem::Get(browser()->profile())->process_manager(); | 385 extensions::ExtensionSystem::Get(browser()->profile())->process_manager(); |
| 385 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_)); | 386 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_)); |
| 386 EXPECT_EQ(1, browser()->tab_strip_model()->count()); | 387 EXPECT_EQ(1, browser()->tab_strip_model()->count()); |
| 387 | 388 |
| 388 // Navigate to a page that opens a message channel to the background page. | 389 // Navigate to a page that opens a message channel to the background page. |
| 389 ResultCatcher catcher; | 390 ResultCatcher catcher; |
| 390 LazyBackgroundObserver lazybg; | 391 LazyBackgroundObserver lazybg; |
| 391 ui_test_utils::NavigateToURL( | 392 ui_test_utils::NavigateToURL( |
| 392 browser(), test_server()->GetURL("files/extensions/test_file.html")); | 393 browser(), embedded_test_server()->GetURL("/extensions/test_file.html")); |
| 393 lazybg.WaitUntilLoaded(); | 394 lazybg.WaitUntilLoaded(); |
| 394 | 395 |
| 395 // Background page got the content script's message and is still loaded | 396 // Background page got the content script's message and is still loaded |
| 396 // until we close the channel. | 397 // until we close the channel. |
| 397 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 398 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 398 EXPECT_TRUE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_)); | 399 EXPECT_TRUE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_)); |
| 399 | 400 |
| 400 // Navigate away, closing the message channel and therefore the background | 401 // Navigate away, closing the message channel and therefore the background |
| 401 // page. | 402 // page. |
| 402 ui_test_utils::NavigateToURL(browser(), GURL("about:blank")); | 403 ui_test_utils::NavigateToURL(browser(), GURL("about:blank")); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 453 | 454 |
| 454 EXPECT_TRUE(event_page_ready.WaitUntilSatisfied()); | 455 EXPECT_TRUE(event_page_ready.WaitUntilSatisfied()); |
| 455 | 456 |
| 456 page_ready.Reply("go"); | 457 page_ready.Reply("go"); |
| 457 | 458 |
| 458 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 459 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 459 } | 460 } |
| 460 | 461 |
| 461 // TODO: background page with timer. | 462 // TODO: background page with timer. |
| 462 // TODO: background page that interacts with popup. | 463 // TODO: background page that interacts with popup. |
| OLD | NEW |