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 | |
42 class LazyBackgroundPageApiTest : public ExtensionApiTest { | 22 class LazyBackgroundPageApiTest : public ExtensionApiTest { |
43 public: | 23 public: |
44 void SetUpCommandLine(CommandLine* command_line) { | 24 void SetUpCommandLine(CommandLine* command_line) { |
45 ExtensionApiTest::SetUpCommandLine(command_line); | 25 ExtensionApiTest::SetUpCommandLine(command_line); |
46 command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis); | 26 command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis); |
47 } | 27 } |
48 }; | 28 }; |
49 | 29 |
50 IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, BrowserActionCreateTab) { | 30 IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, BrowserActionCreateTab) { |
51 FilePath extdir = test_data_dir_.AppendASCII("lazy_background_page"). | 31 FilePath extdir = test_data_dir_.AppendASCII("lazy_background_page"). |
52 AppendASCII("browser_action_create_tab"); | 32 AppendASCII("browser_action_create_tab"); |
53 ASSERT_TRUE(LoadExtension(extdir)); | 33 ASSERT_TRUE(LoadExtension(extdir)); |
54 | 34 |
55 // Lazy Background Page doesn't exist yet. | 35 // 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. | |
60 ExtensionProcessManager* pm = | 36 ExtensionProcessManager* pm = |
61 browser()->profile()->GetExtensionProcessManager(); | 37 browser()->profile()->GetExtensionProcessManager(); |
62 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_)); | 38 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_)); |
63 int num_tabs_before = browser()->tab_count(); | 39 int num_tabs_before = browser()->tab_count(); |
64 | 40 |
65 // Observe background page being created and closed after | 41 // Observe background page being created and closed after |
66 // the browser action is clicked. | 42 // the browser action is clicked. |
67 LazyBackgroundObserver page_complete; | 43 ui_test_utils::WindowedNotificationObserver bg_pg_created( |
| 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()); |
68 BrowserActionTestUtil(browser()).Press(0); | 49 BrowserActionTestUtil(browser()).Press(0); |
69 page_complete.Wait(); | 50 bg_pg_created.Wait(); |
| 51 bg_pg_closed.Wait(); |
70 | 52 |
71 // Background page created a new tab before it closed. | 53 // Background page created a new tab before it closed. |
72 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_)); | 54 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_)); |
73 EXPECT_EQ(num_tabs_before + 1, browser()->tab_count()); | 55 EXPECT_EQ(num_tabs_before + 1, browser()->tab_count()); |
74 EXPECT_EQ("chrome://extensions/", | 56 EXPECT_EQ("chrome://extensions/", |
75 browser()->GetSelectedWebContents()->GetURL().spec()); | 57 browser()->GetSelectedWebContents()->GetURL().spec()); |
76 } | 58 } |
77 | 59 |
78 IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, | 60 IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, |
79 BrowserActionCreateTabAfterCallback) { | 61 BrowserActionCreateTabAfterCallback) { |
80 FilePath extdir = test_data_dir_.AppendASCII("lazy_background_page"). | 62 FilePath extdir = test_data_dir_.AppendASCII("lazy_background_page"). |
81 AppendASCII("browser_action_with_callback"); | 63 AppendASCII("browser_action_with_callback"); |
82 ASSERT_TRUE(LoadExtension(extdir)); | 64 ASSERT_TRUE(LoadExtension(extdir)); |
83 | 65 |
84 // Lazy Background Page doesn't exist yet. | 66 // Lazy Background Page doesn't exist yet. |
85 ExtensionProcessManager* pm = | 67 ExtensionProcessManager* pm = |
86 browser()->profile()->GetExtensionProcessManager(); | 68 browser()->profile()->GetExtensionProcessManager(); |
87 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_)); | 69 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_)); |
88 int num_tabs_before = browser()->tab_count(); | 70 int num_tabs_before = browser()->tab_count(); |
89 | 71 |
90 // Observe background page being created and closed after | 72 // Observe background page being created and closed after |
91 // the browser action is clicked. | 73 // the browser action is clicked. |
92 LazyBackgroundObserver page_complete; | 74 ui_test_utils::WindowedNotificationObserver bg_pg_created( |
| 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()); |
93 BrowserActionTestUtil(browser()).Press(0); | 80 BrowserActionTestUtil(browser()).Press(0); |
94 page_complete.Wait(); | 81 bg_pg_created.Wait(); |
| 82 bg_pg_closed.Wait(); |
95 | 83 |
96 // Background page is closed after creating a new tab. | 84 // Background page is closed after creating a new tab. |
97 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_)); | 85 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_)); |
98 EXPECT_EQ(num_tabs_before + 1, browser()->tab_count()); | 86 EXPECT_EQ(num_tabs_before + 1, browser()->tab_count()); |
99 } | 87 } |
100 | 88 |
101 IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, BroadcastEvent) { | 89 IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, |
102 ASSERT_TRUE(StartTestServer()); | 90 BroadcastEvent) { |
103 | |
104 FilePath extdir = test_data_dir_.AppendASCII("lazy_background_page"). | 91 FilePath extdir = test_data_dir_.AppendASCII("lazy_background_page"). |
105 AppendASCII("broadcast_event"); | 92 AppendASCII("broadcast_event"); |
106 const Extension* extension = LoadExtension(extdir); | 93 ASSERT_TRUE(LoadExtension(extdir)); |
107 ASSERT_TRUE(extension); | |
108 | 94 |
109 // Lazy Background Page doesn't exist yet. | 95 // Lazy Background Page doesn't exist yet. |
110 ExtensionProcessManager* pm = | 96 ExtensionProcessManager* pm = |
111 browser()->profile()->GetExtensionProcessManager(); | 97 browser()->profile()->GetExtensionProcessManager(); |
112 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_)); | 98 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_)); |
113 int num_page_actions = browser()->window()->GetLocationBar()-> | 99 int num_page_actions = browser()->window()->GetLocationBar()-> |
114 GetLocationBarForTesting()->PageActionVisibleCount(); | 100 GetLocationBarForTesting()->PageActionVisibleCount(); |
115 | 101 |
116 // Open a tab to a URL that will trigger the page action to show. | 102 // Open a tab to a URL that will trigger the page action to show. |
117 LazyBackgroundObserver page_complete; | 103 // stegosaurus.html doesn't actually exist but that doesn't seem to matter. |
118 ui_test_utils::WindowedNotificationObserver page_action_changed( | 104 GURL stego_url = GURL("stegosaurus.html"); |
119 chrome::NOTIFICATION_EXTENSION_PAGE_ACTION_VISIBILITY_CHANGED, | 105 ui_test_utils::NavigateToURL(browser(), stego_url); |
120 content::NotificationService::AllSources()); | |
121 ui_test_utils::NavigateToURL( | |
122 browser(), test_server()->GetURL("files/extensions/test_file.html")); | |
123 page_complete.Wait(); | |
124 | 106 |
| 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. |
125 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_)); | 110 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_)); |
126 | 111 EXPECT_EQ(num_page_actions, // should be + 1 |
127 // Page action is shown. | |
128 page_action_changed.Wait(); | |
129 EXPECT_EQ(num_page_actions + 1, | |
130 browser()->window()->GetLocationBar()-> | 112 browser()->window()->GetLocationBar()-> |
131 GetLocationBarForTesting()->PageActionVisibleCount()); | 113 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_)); | |
149 } | 114 } |
150 | 115 |
151 // TODO: background page with timer. | 116 // TODO: background page with timer. |
152 // TODO: background page that interacts with popup. | 117 // TODO: background page that interacts with popup. |
153 // TODO: background page with menu. | 118 // TODO: background page with menu. |
OLD | NEW |