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/file_path.h" | 6 #include "base/file_path.h" |
7 #include "chrome/browser/extensions/browser_action_test_util.h" | 7 #include "chrome/browser/extensions/browser_action_test_util.h" |
8 #include "chrome/browser/extensions/extension_apitest.h" | 8 #include "chrome/browser/extensions/extension_apitest.h" |
9 #include "chrome/browser/extensions/extension_service.h" | 9 #include "chrome/browser/extensions/extension_service.h" |
10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
11 #include "chrome/browser/ui/browser.h" | 11 #include "chrome/browser/ui/browser.h" |
12 #include "chrome/browser/ui/browser_window.h" | 12 #include "chrome/browser/ui/browser_window.h" |
13 #include "chrome/browser/ui/omnibox/location_bar.h" | 13 #include "chrome/browser/ui/omnibox/location_bar.h" |
14 #include "chrome/common/chrome_notification_types.h" | 14 #include "chrome/common/chrome_notification_types.h" |
15 #include "chrome/common/chrome_switches.h" | 15 #include "chrome/common/chrome_switches.h" |
16 #include "chrome/common/extensions/extension.h" | 16 #include "chrome/common/extensions/extension.h" |
17 #include "chrome/test/base/ui_test_utils.h" | 17 #include "chrome/test/base/ui_test_utils.h" |
18 #include "content/public/browser/notification_service.h" | 18 #include "content/public/browser/notification_service.h" |
19 #include "content/public/browser/web_contents.h" | 19 #include "content/public/browser/web_contents.h" |
20 #include "googleurl/src/gurl.h" | 20 #include "googleurl/src/gurl.h" |
21 | 21 |
| 22 namespace { |
| 23 // Helper class to wait for a lazy background page to load and close again. |
| 24 class LazyBackgroundObserver { |
| 25 public: |
| 26 LazyBackgroundObserver() |
| 27 : page_created_(chrome::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY, |
| 28 content::NotificationService::AllSources()), |
| 29 page_closed_(chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, |
| 30 content::NotificationService::AllSources()) { |
| 31 } |
| 32 void Wait() { |
| 33 page_created_.Wait(); |
| 34 page_closed_.Wait(); |
| 35 } |
| 36 private: |
| 37 ui_test_utils::WindowedNotificationObserver page_created_; |
| 38 ui_test_utils::WindowedNotificationObserver page_closed_; |
| 39 }; |
| 40 } // namespace |
| 41 |
22 class LazyBackgroundPageApiTest : public ExtensionApiTest { | 42 class LazyBackgroundPageApiTest : public ExtensionApiTest { |
23 public: | 43 public: |
24 void SetUpCommandLine(CommandLine* command_line) { | 44 void SetUpCommandLine(CommandLine* command_line) { |
25 ExtensionApiTest::SetUpCommandLine(command_line); | 45 ExtensionApiTest::SetUpCommandLine(command_line); |
26 command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis); | 46 command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis); |
27 } | 47 } |
28 }; | 48 }; |
29 | 49 |
30 IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, BrowserActionCreateTab) { | 50 IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, BrowserActionCreateTab) { |
31 FilePath extdir = test_data_dir_.AppendASCII("lazy_background_page"). | 51 FilePath extdir = test_data_dir_.AppendASCII("lazy_background_page"). |
32 AppendASCII("browser_action_create_tab"); | 52 AppendASCII("browser_action_create_tab"); |
33 ASSERT_TRUE(LoadExtension(extdir)); | 53 ASSERT_TRUE(LoadExtension(extdir)); |
34 | 54 |
35 // Lazy Background Page doesn't exist yet. | 55 // Lazy Background Page doesn't exist yet. |
| 56 // Note: We actually loaded and destroyed the page to dispatch the onInstalled |
| 57 // event. LoadExtension waits for the load to finish, after which onInstalled |
| 58 // is dispatched. Since the extension isn't listening to it, we immediately |
| 59 // tear it down again. |
36 ExtensionProcessManager* pm = | 60 ExtensionProcessManager* pm = |
37 browser()->profile()->GetExtensionProcessManager(); | 61 browser()->profile()->GetExtensionProcessManager(); |
38 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_)); | 62 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_)); |
39 int num_tabs_before = browser()->tab_count(); | 63 int num_tabs_before = browser()->tab_count(); |
40 | 64 |
41 // Observe background page being created and closed after | 65 // Observe background page being created and closed after |
42 // the browser action is clicked. | 66 // the browser action is clicked. |
43 ui_test_utils::WindowedNotificationObserver bg_pg_created( | 67 LazyBackgroundObserver page_complete; |
44 chrome::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY, | |
45 content::NotificationService::AllSources()); | |
46 ui_test_utils::WindowedNotificationObserver bg_pg_closed( | |
47 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, | |
48 content::NotificationService::AllSources()); | |
49 BrowserActionTestUtil(browser()).Press(0); | 68 BrowserActionTestUtil(browser()).Press(0); |
50 bg_pg_created.Wait(); | 69 page_complete.Wait(); |
51 bg_pg_closed.Wait(); | |
52 | 70 |
53 // Background page created a new tab before it closed. | 71 // Background page created a new tab before it closed. |
54 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_)); | 72 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_)); |
55 EXPECT_EQ(num_tabs_before + 1, browser()->tab_count()); | 73 EXPECT_EQ(num_tabs_before + 1, browser()->tab_count()); |
56 EXPECT_EQ("chrome://extensions/", | 74 EXPECT_EQ("chrome://extensions/", |
57 browser()->GetSelectedWebContents()->GetURL().spec()); | 75 browser()->GetSelectedWebContents()->GetURL().spec()); |
58 } | 76 } |
59 | 77 |
60 IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, | 78 IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, |
61 BrowserActionCreateTabAfterCallback) { | 79 BrowserActionCreateTabAfterCallback) { |
62 FilePath extdir = test_data_dir_.AppendASCII("lazy_background_page"). | 80 FilePath extdir = test_data_dir_.AppendASCII("lazy_background_page"). |
63 AppendASCII("browser_action_with_callback"); | 81 AppendASCII("browser_action_with_callback"); |
64 ASSERT_TRUE(LoadExtension(extdir)); | 82 ASSERT_TRUE(LoadExtension(extdir)); |
65 | 83 |
66 // Lazy Background Page doesn't exist yet. | 84 // Lazy Background Page doesn't exist yet. |
67 ExtensionProcessManager* pm = | 85 ExtensionProcessManager* pm = |
68 browser()->profile()->GetExtensionProcessManager(); | 86 browser()->profile()->GetExtensionProcessManager(); |
69 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_)); | 87 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_)); |
70 int num_tabs_before = browser()->tab_count(); | 88 int num_tabs_before = browser()->tab_count(); |
71 | 89 |
72 // Observe background page being created and closed after | 90 // Observe background page being created and closed after |
73 // the browser action is clicked. | 91 // the browser action is clicked. |
74 ui_test_utils::WindowedNotificationObserver bg_pg_created( | 92 LazyBackgroundObserver page_complete; |
75 chrome::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY, | |
76 content::NotificationService::AllSources()); | |
77 ui_test_utils::WindowedNotificationObserver bg_pg_closed( | |
78 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, | |
79 content::NotificationService::AllSources()); | |
80 BrowserActionTestUtil(browser()).Press(0); | 93 BrowserActionTestUtil(browser()).Press(0); |
81 bg_pg_created.Wait(); | 94 page_complete.Wait(); |
82 bg_pg_closed.Wait(); | |
83 | 95 |
84 // Background page is closed after creating a new tab. | 96 // Background page is closed after creating a new tab. |
85 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_)); | 97 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_)); |
86 EXPECT_EQ(num_tabs_before + 1, browser()->tab_count()); | 98 EXPECT_EQ(num_tabs_before + 1, browser()->tab_count()); |
87 } | 99 } |
88 | 100 |
89 IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, | 101 IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, BroadcastEvent) { |
90 BroadcastEvent) { | 102 ASSERT_TRUE(StartTestServer()); |
| 103 |
91 FilePath extdir = test_data_dir_.AppendASCII("lazy_background_page"). | 104 FilePath extdir = test_data_dir_.AppendASCII("lazy_background_page"). |
92 AppendASCII("broadcast_event"); | 105 AppendASCII("broadcast_event"); |
93 ASSERT_TRUE(LoadExtension(extdir)); | 106 const Extension* extension = LoadExtension(extdir); |
| 107 ASSERT_TRUE(extension); |
94 | 108 |
95 // Lazy Background Page doesn't exist yet. | 109 // Lazy Background Page doesn't exist yet. |
96 ExtensionProcessManager* pm = | 110 ExtensionProcessManager* pm = |
97 browser()->profile()->GetExtensionProcessManager(); | 111 browser()->profile()->GetExtensionProcessManager(); |
98 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_)); | 112 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_)); |
99 int num_page_actions = browser()->window()->GetLocationBar()-> | 113 int num_page_actions = browser()->window()->GetLocationBar()-> |
100 GetLocationBarForTesting()->PageActionVisibleCount(); | 114 GetLocationBarForTesting()->PageActionVisibleCount(); |
101 | 115 |
102 // Open a tab to a URL that will trigger the page action to show. | 116 // Open a tab to a URL that will trigger the page action to show. |
103 // stegosaurus.html doesn't actually exist but that doesn't seem to matter. | 117 LazyBackgroundObserver page_complete; |
104 GURL stego_url = GURL("stegosaurus.html"); | 118 ui_test_utils::WindowedNotificationObserver page_action_changed( |
105 ui_test_utils::NavigateToURL(browser(), stego_url); | 119 chrome::NOTIFICATION_EXTENSION_PAGE_ACTION_VISIBILITY_CHANGED, |
| 120 content::NotificationService::AllSources()); |
| 121 ui_test_utils::NavigateToURL( |
| 122 browser(), test_server()->GetURL("files/extensions/test_file.html")); |
| 123 page_complete.Wait(); |
106 | 124 |
107 // New page action is never shown because background page is never created. | |
108 // TODO(tessamac): Implement! Broadcast events (like tab updates) should | |
109 // cause lazy background pages to be created. | |
110 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_)); | 125 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_)); |
111 EXPECT_EQ(num_page_actions, // should be + 1 | 126 |
| 127 // Page action is shown. |
| 128 page_action_changed.Wait(); |
| 129 EXPECT_EQ(num_page_actions + 1, |
112 browser()->window()->GetLocationBar()-> | 130 browser()->window()->GetLocationBar()-> |
113 GetLocationBarForTesting()->PageActionVisibleCount()); | 131 GetLocationBarForTesting()->PageActionVisibleCount()); |
| 132 } |
| 133 |
| 134 IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, OnInstalled) { |
| 135 LazyBackgroundObserver page_complete; |
| 136 ResultCatcher catcher; |
| 137 FilePath extdir = test_data_dir_.AppendASCII("lazy_background_page"). |
| 138 AppendASCII("on_installed"); |
| 139 const Extension* extension = LoadExtension(extdir); |
| 140 ASSERT_TRUE(extension); |
| 141 |
| 142 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 143 page_complete.Wait(); |
| 144 |
| 145 // Lazy Background Page has been shut down. |
| 146 ExtensionProcessManager* pm = |
| 147 browser()->profile()->GetExtensionProcessManager(); |
| 148 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_)); |
114 } | 149 } |
115 | 150 |
116 // TODO: background page with timer. | 151 // TODO: background page with timer. |
117 // TODO: background page that interacts with popup. | 152 // TODO: background page that interacts with popup. |
118 // TODO: background page with menu. | 153 // TODO: background page with menu. |
OLD | NEW |