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

Side by Side Diff: trunk/src/extensions/browser/process_manager_unittest.cc

Issue 399153002: Revert 283678 "Refactor code that defers extension background pa..." (Closed) Base URL: svn://svn.chromium.org/chrome/
Patch Set: Created 6 years, 5 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "extensions/browser/process_manager.h" 5 #include "extensions/browser/process_manager.h"
6 6
7 #include "chrome/browser/chrome_notification_types.h" 7 #include "chrome/browser/chrome_notification_types.h"
8 #include "components/keyed_service/content/browser_context_dependency_manager.h"
9 #include "content/public/browser/content_browser_client.h" 8 #include "content/public/browser/content_browser_client.h"
10 #include "content/public/browser/notification_service.h" 9 #include "content/public/browser/notification_service.h"
11 #include "content/public/browser/site_instance.h" 10 #include "content/public/browser/site_instance.h"
12 #include "content/public/test/test_browser_context.h" 11 #include "content/public/test/test_browser_context.h"
13 #include "extensions/browser/process_manager_delegate.h"
14 #include "extensions/browser/test_extensions_browser_client.h" 12 #include "extensions/browser/test_extensions_browser_client.h"
15 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
16 14
17 using content::BrowserContext; 15 using content::BrowserContext;
18 using content::SiteInstance; 16 using content::SiteInstance;
19 using content::TestBrowserContext; 17 using content::TestBrowserContext;
20 18
21 namespace extensions { 19 namespace extensions {
22 20
23 namespace { 21 namespace {
24 22
25 // An incognito version of a TestBrowserContext. 23 // An incognito version of a TestBrowserContext.
26 class TestBrowserContextIncognito : public TestBrowserContext { 24 class TestBrowserContextIncognito : public TestBrowserContext {
27 public: 25 public:
28 TestBrowserContextIncognito() {} 26 TestBrowserContextIncognito() {}
29 virtual ~TestBrowserContextIncognito() {} 27 virtual ~TestBrowserContextIncognito() {}
30 28
31 // TestBrowserContext implementation. 29 // TestBrowserContext implementation.
32 virtual bool IsOffTheRecord() const OVERRIDE { return true; } 30 virtual bool IsOffTheRecord() const OVERRIDE { return true; }
33 31
34 private: 32 private:
35 DISALLOW_COPY_AND_ASSIGN(TestBrowserContextIncognito); 33 DISALLOW_COPY_AND_ASSIGN(TestBrowserContextIncognito);
36 }; 34 };
37 35
38 // A trivial ProcessManagerDelegate.
39 class TestProcessManagerDelegate : public ProcessManagerDelegate {
40 public:
41 TestProcessManagerDelegate()
42 : is_background_page_allowed_(true),
43 defer_creating_startup_background_hosts_(false) {}
44 virtual ~TestProcessManagerDelegate() {}
45
46 // ProcessManagerDelegate implementation.
47 virtual bool IsBackgroundPageAllowed(BrowserContext* context) const OVERRIDE {
48 return is_background_page_allowed_;
49 }
50 virtual bool DeferCreatingStartupBackgroundHosts(
51 BrowserContext* context) const OVERRIDE {
52 return defer_creating_startup_background_hosts_;
53 }
54
55 bool is_background_page_allowed_;
56 bool defer_creating_startup_background_hosts_;
57 };
58
59 } // namespace 36 } // namespace
60 37
61 class ProcessManagerTest : public testing::Test { 38 class ProcessManagerTest : public testing::Test {
62 public: 39 public:
63 ProcessManagerTest() : extensions_browser_client_(&original_context_) { 40 ProcessManagerTest() : extensions_browser_client_(&original_context_) {
64 extensions_browser_client_.SetIncognitoContext(&incognito_context_); 41 extensions_browser_client_.SetIncognitoContext(&incognito_context_);
65 extensions_browser_client_.set_process_manager_delegate(
66 &process_manager_delegate_);
67 ExtensionsBrowserClient::Set(&extensions_browser_client_); 42 ExtensionsBrowserClient::Set(&extensions_browser_client_);
68 } 43 }
69 44
70 virtual ~ProcessManagerTest() { 45 virtual ~ProcessManagerTest() {
71 ExtensionsBrowserClient::Set(NULL); 46 ExtensionsBrowserClient::Set(NULL);
72 } 47 }
73 48
74 BrowserContext* original_context() { return &original_context_; } 49 BrowserContext* original_context() { return &original_context_; }
75 BrowserContext* incognito_context() { return &incognito_context_; } 50 BrowserContext* incognito_context() { return &incognito_context_; }
76 51
77 // testing::Test implementation.
78 virtual void SetUp() OVERRIDE {
79 // Needed for ExtensionRegistry.
80 BrowserContextDependencyManager::GetInstance()
81 ->CreateBrowserContextServicesForTest(&original_context_);
82 BrowserContextDependencyManager::GetInstance()
83 ->CreateBrowserContextServicesForTest(&incognito_context_);
84 }
85
86 virtual void TearDown() OVERRIDE {
87 // Needed to clean up ExtensionRegistry.
88 BrowserContextDependencyManager::GetInstance()
89 ->DestroyBrowserContextServices(&incognito_context_);
90 BrowserContextDependencyManager::GetInstance()
91 ->DestroyBrowserContextServices(&original_context_);
92 }
93
94 // Returns true if the notification |type| is registered for |manager| with 52 // Returns true if the notification |type| is registered for |manager| with
95 // source |context|. Pass NULL for |context| for all sources. 53 // source |context|. Pass NULL for |context| for all sources.
96 static bool IsRegistered(ProcessManager* manager, 54 static bool IsRegistered(ProcessManager* manager,
97 int type, 55 int type,
98 BrowserContext* context) { 56 BrowserContext* context) {
99 return manager->registrar_.IsRegistered( 57 return manager->registrar_.IsRegistered(
100 manager, type, content::Source<BrowserContext>(context)); 58 manager, type, content::Source<BrowserContext>(context));
101 } 59 }
102 60
103 protected: 61 private:
104 TestBrowserContext original_context_; 62 TestBrowserContext original_context_;
105 TestBrowserContextIncognito incognito_context_; 63 TestBrowserContextIncognito incognito_context_;
106 TestProcessManagerDelegate process_manager_delegate_;
107 TestExtensionsBrowserClient extensions_browser_client_; 64 TestExtensionsBrowserClient extensions_browser_client_;
108 65
109 DISALLOW_COPY_AND_ASSIGN(ProcessManagerTest); 66 DISALLOW_COPY_AND_ASSIGN(ProcessManagerTest);
110 }; 67 };
111 68
112 // Test that notification registration works properly. 69 // Test that notification registration works properly.
113 TEST_F(ProcessManagerTest, ExtensionNotificationRegistration) { 70 TEST_F(ProcessManagerTest, ExtensionNotificationRegistration) {
114 // Test for a normal context ProcessManager. 71 // Test for a normal context ProcessManager.
115 scoped_ptr<ProcessManager> manager1( 72 scoped_ptr<ProcessManager> manager1(
116 ProcessManager::Create(original_context())); 73 ProcessManager::Create(original_context()));
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 EXPECT_FALSE(IsRegistered(manager2.get(), 118 EXPECT_FALSE(IsRegistered(manager2.get(),
162 chrome::NOTIFICATION_EXTENSIONS_READY, 119 chrome::NOTIFICATION_EXTENSIONS_READY,
163 original_context())); 120 original_context()));
164 121
165 // This notification is observed for incognito contexts only. 122 // This notification is observed for incognito contexts only.
166 EXPECT_TRUE(IsRegistered(manager2.get(), 123 EXPECT_TRUE(IsRegistered(manager2.get(),
167 chrome::NOTIFICATION_PROFILE_DESTROYED, 124 chrome::NOTIFICATION_PROFILE_DESTROYED,
168 incognito_context())); 125 incognito_context()));
169 } 126 }
170 127
171 // Test that startup background hosts are created when the extension system
172 // becomes ready.
173 //
174 // NOTE: This test and those that follow do not try to create ExtensionsHosts
175 // because ExtensionHost is tightly coupled to WebContents and can't be
176 // constructed in unit tests.
177 TEST_F(ProcessManagerTest, CreateBackgroundHostsOnExtensionsReady) {
178 scoped_ptr<ProcessManager> manager(
179 ProcessManager::Create(original_context()));
180 ASSERT_FALSE(manager->startup_background_hosts_created_for_test());
181
182 // Simulate the extension system becoming ready.
183 content::NotificationService::current()->Notify(
184 chrome::NOTIFICATION_EXTENSIONS_READY,
185 content::Source<BrowserContext>(original_context()),
186 content::NotificationService::NoDetails());
187 EXPECT_TRUE(manager->startup_background_hosts_created_for_test());
188 }
189
190 // Test that startup background hosts can be created explicitly before the
191 // extension system is ready (this is the normal pattern in Chrome).
192 TEST_F(ProcessManagerTest, CreateBackgroundHostsExplicitly) {
193 scoped_ptr<ProcessManager> manager(
194 ProcessManager::Create(original_context()));
195 ASSERT_FALSE(manager->startup_background_hosts_created_for_test());
196
197 // Embedder explicitly asks for hosts to be created. Chrome does this on
198 // normal startup.
199 manager->MaybeCreateStartupBackgroundHosts();
200 EXPECT_TRUE(manager->startup_background_hosts_created_for_test());
201 }
202
203 // Test that the embedder can defer background host creation. Chrome does this
204 // when the profile is created asynchronously, which may take a while.
205 TEST_F(ProcessManagerTest, CreateBackgroundHostsDeferred) {
206 scoped_ptr<ProcessManager> manager(
207 ProcessManager::Create(original_context()));
208 ASSERT_FALSE(manager->startup_background_hosts_created_for_test());
209
210 // Don't create background hosts if the delegate says to defer them.
211 process_manager_delegate_.defer_creating_startup_background_hosts_ = true;
212 manager->MaybeCreateStartupBackgroundHosts();
213 EXPECT_FALSE(manager->startup_background_hosts_created_for_test());
214
215 // The extension system becoming ready still doesn't create the hosts.
216 content::NotificationService::current()->Notify(
217 chrome::NOTIFICATION_EXTENSIONS_READY,
218 content::Source<BrowserContext>(original_context()),
219 content::NotificationService::NoDetails());
220 EXPECT_FALSE(manager->startup_background_hosts_created_for_test());
221
222 // Once the embedder is ready the background hosts can be created.
223 process_manager_delegate_.defer_creating_startup_background_hosts_ = false;
224 manager->MaybeCreateStartupBackgroundHosts();
225 EXPECT_TRUE(manager->startup_background_hosts_created_for_test());
226 }
227
228 // Test that the embedder can disallow background host creation.
229 // Chrome OS does this in guest mode.
230 TEST_F(ProcessManagerTest, IsBackgroundHostAllowed) {
231 scoped_ptr<ProcessManager> manager(
232 ProcessManager::Create(original_context()));
233 ASSERT_FALSE(manager->startup_background_hosts_created_for_test());
234
235 // Don't create background hosts if the delegate disallows them.
236 process_manager_delegate_.is_background_page_allowed_ = false;
237 manager->MaybeCreateStartupBackgroundHosts();
238 EXPECT_FALSE(manager->startup_background_hosts_created_for_test());
239
240 // The extension system becoming ready still doesn't create the hosts.
241 content::NotificationService::current()->Notify(
242 chrome::NOTIFICATION_EXTENSIONS_READY,
243 content::Source<BrowserContext>(original_context()),
244 content::NotificationService::NoDetails());
245 EXPECT_FALSE(manager->startup_background_hosts_created_for_test());
246 }
247
248 // Test that extensions get grouped in the right SiteInstance (and therefore 128 // Test that extensions get grouped in the right SiteInstance (and therefore
249 // process) based on their URLs. 129 // process) based on their URLs.
250 TEST_F(ProcessManagerTest, ProcessGrouping) { 130 TEST_F(ProcessManagerTest, ProcessGrouping) {
251 content::ContentBrowserClient content_browser_client; 131 content::ContentBrowserClient content_browser_client;
252 content::SetBrowserClientForTesting(&content_browser_client); 132 content::SetBrowserClientForTesting(&content_browser_client);
253 133
254 // Extensions in different browser contexts should always be different 134 // Extensions in different browser contexts should always be different
255 // SiteInstances. 135 // SiteInstances.
256 scoped_ptr<ProcessManager> manager1( 136 scoped_ptr<ProcessManager> manager1(
257 ProcessManager::Create(original_context())); 137 ProcessManager::Create(original_context()));
(...skipping 17 matching lines...) Expand all
275 scoped_refptr<SiteInstance> site21 = 155 scoped_refptr<SiteInstance> site21 =
276 manager1->GetSiteInstanceForURL(ext2_url1); 156 manager1->GetSiteInstanceForURL(ext2_url1);
277 EXPECT_NE(site11, site21); 157 EXPECT_NE(site11, site21);
278 158
279 scoped_refptr<SiteInstance> other_profile_site = 159 scoped_refptr<SiteInstance> other_profile_site =
280 manager2->GetSiteInstanceForURL(ext1_url1); 160 manager2->GetSiteInstanceForURL(ext1_url1);
281 EXPECT_NE(site11, other_profile_site); 161 EXPECT_NE(site11, other_profile_site);
282 } 162 }
283 163
284 } // namespace extensions 164 } // namespace extensions
OLDNEW
« no previous file with comments | « trunk/src/extensions/browser/process_manager_delegate.h ('k') | trunk/src/extensions/browser/test_extensions_browser_client.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698