Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(351)

Side by Side Diff: chrome/browser/extensions/lazy_background_page_apitest.cc

Issue 9562017: Keep lazy background page alive while there are pending network requests or (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: review fixins Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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_host.h"
9 #include "chrome/browser/extensions/extension_service.h" 10 #include "chrome/browser/extensions/extension_service.h"
10 #include "chrome/browser/profiles/profile.h" 11 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/browser.h" 12 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/browser_window.h" 13 #include "chrome/browser/ui/browser_window.h"
13 #include "chrome/browser/ui/omnibox/location_bar.h" 14 #include "chrome/browser/ui/omnibox/location_bar.h"
14 #include "chrome/common/chrome_notification_types.h" 15 #include "chrome/common/chrome_notification_types.h"
15 #include "chrome/common/chrome_switches.h" 16 #include "chrome/common/chrome_switches.h"
16 #include "chrome/common/extensions/extension.h" 17 #include "chrome/common/extensions/extension.h"
17 #include "chrome/test/base/ui_test_utils.h" 18 #include "chrome/test/base/ui_test_utils.h"
18 #include "content/public/browser/notification_service.h" 19 #include "content/public/browser/notification_service.h"
19 #include "content/public/browser/web_contents.h" 20 #include "content/public/browser/web_contents.h"
20 #include "googleurl/src/gurl.h" 21 #include "googleurl/src/gurl.h"
22 #include "net/base/mock_host_resolver.h"
21 23
22 namespace { 24 namespace {
23 // Helper class to wait for a lazy background page to load and close again. 25 // Helper class to wait for a lazy background page to load and close again.
24 class LazyBackgroundObserver { 26 class LazyBackgroundObserver {
25 public: 27 public:
26 LazyBackgroundObserver() 28 LazyBackgroundObserver()
27 : page_created_(chrome::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY, 29 : page_created_(chrome::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY,
28 content::NotificationService::AllSources()), 30 content::NotificationService::AllSources()),
29 page_closed_(chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, 31 page_closed_(chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED,
30 content::NotificationService::AllSources()) { 32 content::NotificationService::AllSources()) {
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 ResultCatcher catcher; 141 ResultCatcher catcher;
140 ASSERT_TRUE(LoadExtensionAndWait("on_installed")); 142 ASSERT_TRUE(LoadExtensionAndWait("on_installed"));
141 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); 143 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
142 144
143 // Lazy Background Page has been shut down. 145 // Lazy Background Page has been shut down.
144 ExtensionProcessManager* pm = 146 ExtensionProcessManager* pm =
145 browser()->profile()->GetExtensionProcessManager(); 147 browser()->profile()->GetExtensionProcessManager();
146 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_)); 148 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_));
147 } 149 }
148 150
151 // Tests that the lazy background page stays alive until all visible views are
152 // closed.
153 IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, WaitForView) {
154 LazyBackgroundObserver page_complete;
155 ResultCatcher catcher;
156 FilePath extdir = test_data_dir_.AppendASCII("lazy_background_page").
157 AppendASCII("wait_for_view");
158 const Extension* extension = LoadExtension(extdir);
159 ASSERT_TRUE(extension);
160 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
161
162 // The extension should've opened a new tab to an extension page.
163 EXPECT_EQ(extension->GetResourceURL("extension_page.html").spec(),
164 browser()->GetSelectedWebContents()->GetURL().spec());
165
166 // Lazy Background Page still exists, because the extension created a new tab
167 // to an extension page.
168 ExtensionProcessManager* pm =
169 browser()->profile()->GetExtensionProcessManager();
170 EXPECT_TRUE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_));
171
172 // Close the new tab.
173 browser()->CloseTabContents(browser()->GetSelectedWebContents());
174 page_complete.Wait();
175
176 // Lazy Background Page has been shut down.
177 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_));
178 }
179
180 // Tests that the lazy background page stays alive until all network requests
181 // are complete.
182 IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, WaitForRequest) {
183 host_resolver()->AddRule("*", "127.0.0.1");
184 ASSERT_TRUE(StartTestServer());
185
186 LazyBackgroundObserver page_complete;
187 ResultCatcher catcher;
188 FilePath extdir = test_data_dir_.AppendASCII("lazy_background_page").
189 AppendASCII("wait_for_request");
190 const Extension* extension = LoadExtension(extdir);
191 ASSERT_TRUE(extension);
192 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
193
194 // Lazy Background Page still exists, because the extension started a request.
195 ExtensionProcessManager* pm =
196 browser()->profile()->GetExtensionProcessManager();
197 ExtensionHost* host =
198 pm->GetBackgroundHostForExtension(last_loaded_extension_id_);
199 ASSERT_TRUE(host);
200
201 // Abort the request.
202 bool result = false;
203 EXPECT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool(
204 host->render_view_host(), std::wstring(), L"abortRequest()", &result));
205 EXPECT_TRUE(result);
206 page_complete.Wait();
207
208 // Lazy Background Page has been shut down.
209 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_));
210 }
211
149 // TODO: background page with timer. 212 // TODO: background page with timer.
150 // TODO: background page that interacts with popup. 213 // TODO: background page that interacts with popup.
151 // TODO: background page with menu. 214 // TODO: background page with menu.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698