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

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

Issue 408523005: Reland: Refactor code that defers extension background page loading (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase 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 "content/public/browser/content_browser_client.h" 8 #include "content/public/browser/content_browser_client.h"
9 #include "content/public/browser/notification_service.h" 9 #include "content/public/browser/notification_service.h"
10 #include "content/public/browser/site_instance.h" 10 #include "content/public/browser/site_instance.h"
11 #include "content/public/test/test_browser_context.h" 11 #include "content/public/test/test_browser_context.h"
12 #include "extensions/browser/extension_registry.h"
13 #include "extensions/browser/process_manager_delegate.h"
12 #include "extensions/browser/test_extensions_browser_client.h" 14 #include "extensions/browser/test_extensions_browser_client.h"
13 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
14 16
15 using content::BrowserContext; 17 using content::BrowserContext;
16 using content::SiteInstance; 18 using content::SiteInstance;
17 using content::TestBrowserContext; 19 using content::TestBrowserContext;
18 20
19 namespace extensions { 21 namespace extensions {
20 22
21 namespace { 23 namespace {
22 24
23 // An incognito version of a TestBrowserContext. 25 // An incognito version of a TestBrowserContext.
24 class TestBrowserContextIncognito : public TestBrowserContext { 26 class TestBrowserContextIncognito : public TestBrowserContext {
25 public: 27 public:
26 TestBrowserContextIncognito() {} 28 TestBrowserContextIncognito() {}
27 virtual ~TestBrowserContextIncognito() {} 29 virtual ~TestBrowserContextIncognito() {}
28 30
29 // TestBrowserContext implementation. 31 // TestBrowserContext implementation.
30 virtual bool IsOffTheRecord() const OVERRIDE { return true; } 32 virtual bool IsOffTheRecord() const OVERRIDE { return true; }
31 33
32 private: 34 private:
33 DISALLOW_COPY_AND_ASSIGN(TestBrowserContextIncognito); 35 DISALLOW_COPY_AND_ASSIGN(TestBrowserContextIncognito);
34 }; 36 };
35 37
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
36 } // namespace 59 } // namespace
37 60
38 class ProcessManagerTest : public testing::Test { 61 class ProcessManagerTest : public testing::Test {
39 public: 62 public:
40 ProcessManagerTest() : extensions_browser_client_(&original_context_) { 63 ProcessManagerTest()
64 : extension_registry_(&original_context_),
65 extensions_browser_client_(&original_context_) {
41 extensions_browser_client_.SetIncognitoContext(&incognito_context_); 66 extensions_browser_client_.SetIncognitoContext(&incognito_context_);
67 extensions_browser_client_.set_process_manager_delegate(
68 &process_manager_delegate_);
42 ExtensionsBrowserClient::Set(&extensions_browser_client_); 69 ExtensionsBrowserClient::Set(&extensions_browser_client_);
43 } 70 }
44 71
45 virtual ~ProcessManagerTest() { 72 virtual ~ProcessManagerTest() {
46 ExtensionsBrowserClient::Set(NULL); 73 ExtensionsBrowserClient::Set(NULL);
47 } 74 }
48 75
49 BrowserContext* original_context() { return &original_context_; } 76 BrowserContext* original_context() { return &original_context_; }
50 BrowserContext* incognito_context() { return &incognito_context_; } 77 BrowserContext* incognito_context() { return &incognito_context_; }
78 ExtensionRegistry* extension_registry() { return &extension_registry_; }
79 TestProcessManagerDelegate* process_manager_delegate() {
80 return &process_manager_delegate_;
81 }
51 82
52 // Returns true if the notification |type| is registered for |manager| with 83 // Returns true if the notification |type| is registered for |manager| with
53 // source |context|. Pass NULL for |context| for all sources. 84 // source |context|. Pass NULL for |context| for all sources.
54 static bool IsRegistered(ProcessManager* manager, 85 static bool IsRegistered(ProcessManager* manager,
55 int type, 86 int type,
56 BrowserContext* context) { 87 BrowserContext* context) {
57 return manager->registrar_.IsRegistered( 88 return manager->registrar_.IsRegistered(
58 manager, type, content::Source<BrowserContext>(context)); 89 manager, type, content::Source<BrowserContext>(context));
59 } 90 }
60 91
61 private: 92 private:
62 TestBrowserContext original_context_; 93 TestBrowserContext original_context_;
63 TestBrowserContextIncognito incognito_context_; 94 TestBrowserContextIncognito incognito_context_;
95 ExtensionRegistry extension_registry_; // Shared between BrowserContexts.
96 TestProcessManagerDelegate process_manager_delegate_;
64 TestExtensionsBrowserClient extensions_browser_client_; 97 TestExtensionsBrowserClient extensions_browser_client_;
65 98
66 DISALLOW_COPY_AND_ASSIGN(ProcessManagerTest); 99 DISALLOW_COPY_AND_ASSIGN(ProcessManagerTest);
67 }; 100 };
68 101
69 // Test that notification registration works properly. 102 // Test that notification registration works properly.
70 TEST_F(ProcessManagerTest, ExtensionNotificationRegistration) { 103 TEST_F(ProcessManagerTest, ExtensionNotificationRegistration) {
71 // Test for a normal context ProcessManager. 104 // Test for a normal context ProcessManager.
72 scoped_ptr<ProcessManager> manager1( 105 scoped_ptr<ProcessManager> manager1(ProcessManager::CreateForTesting(
73 ProcessManager::Create(original_context())); 106 original_context(), extension_registry()));
74 107
75 EXPECT_EQ(original_context(), manager1->GetBrowserContext()); 108 EXPECT_EQ(original_context(), manager1->GetBrowserContext());
76 EXPECT_EQ(0u, manager1->background_hosts().size()); 109 EXPECT_EQ(0u, manager1->background_hosts().size());
77 110
78 // It observes other notifications from this context. 111 // It observes other notifications from this context.
79 EXPECT_TRUE(IsRegistered(manager1.get(), 112 EXPECT_TRUE(IsRegistered(manager1.get(),
80 chrome::NOTIFICATION_EXTENSIONS_READY, 113 chrome::NOTIFICATION_EXTENSIONS_READY,
81 original_context())); 114 original_context()));
82 EXPECT_TRUE(IsRegistered(manager1.get(), 115 EXPECT_TRUE(IsRegistered(manager1.get(),
83 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, 116 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED,
84 original_context())); 117 original_context()));
85 EXPECT_TRUE(IsRegistered(manager1.get(), 118 EXPECT_TRUE(IsRegistered(manager1.get(),
86 chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, 119 chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
87 original_context())); 120 original_context()));
88 EXPECT_TRUE(IsRegistered(manager1.get(), 121 EXPECT_TRUE(IsRegistered(manager1.get(),
89 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, 122 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED,
90 original_context())); 123 original_context()));
91 124
92 // Test for an incognito context ProcessManager. 125 // Test for an incognito context ProcessManager.
93 scoped_ptr<ProcessManager> manager2(ProcessManager::CreateIncognitoForTesting( 126 scoped_ptr<ProcessManager> manager2(
94 incognito_context(), original_context(), manager1.get())); 127 ProcessManager::CreateIncognitoForTesting(incognito_context(),
128 original_context(),
129 manager1.get(),
130 extension_registry()));
95 131
96 EXPECT_EQ(incognito_context(), manager2->GetBrowserContext()); 132 EXPECT_EQ(incognito_context(), manager2->GetBrowserContext());
97 EXPECT_EQ(0u, manager2->background_hosts().size()); 133 EXPECT_EQ(0u, manager2->background_hosts().size());
98 134
99 // Some notifications are observed for the original context. 135 // Some notifications are observed for the original context.
100 EXPECT_TRUE(IsRegistered(manager2.get(), 136 EXPECT_TRUE(IsRegistered(manager2.get(),
101 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, 137 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED,
102 original_context())); 138 original_context()));
103 139
104 // Some notifications are observed for the incognito context. 140 // Some notifications are observed for the incognito context.
(...skipping 13 matching lines...) Expand all
118 EXPECT_FALSE(IsRegistered(manager2.get(), 154 EXPECT_FALSE(IsRegistered(manager2.get(),
119 chrome::NOTIFICATION_EXTENSIONS_READY, 155 chrome::NOTIFICATION_EXTENSIONS_READY,
120 original_context())); 156 original_context()));
121 157
122 // This notification is observed for incognito contexts only. 158 // This notification is observed for incognito contexts only.
123 EXPECT_TRUE(IsRegistered(manager2.get(), 159 EXPECT_TRUE(IsRegistered(manager2.get(),
124 chrome::NOTIFICATION_PROFILE_DESTROYED, 160 chrome::NOTIFICATION_PROFILE_DESTROYED,
125 incognito_context())); 161 incognito_context()));
126 } 162 }
127 163
164 // Test that startup background hosts are created when the extension system
165 // becomes ready.
166 //
167 // NOTE: This test and those that follow do not try to create ExtensionsHosts
168 // because ExtensionHost is tightly coupled to WebContents and can't be
169 // constructed in unit tests.
170 TEST_F(ProcessManagerTest, CreateBackgroundHostsOnExtensionsReady) {
171 scoped_ptr<ProcessManager> manager(ProcessManager::CreateForTesting(
172 original_context(), extension_registry()));
173 ASSERT_FALSE(manager->startup_background_hosts_created_for_test());
174
175 // Simulate the extension system becoming ready.
176 content::NotificationService::current()->Notify(
177 chrome::NOTIFICATION_EXTENSIONS_READY,
178 content::Source<BrowserContext>(original_context()),
179 content::NotificationService::NoDetails());
180 EXPECT_TRUE(manager->startup_background_hosts_created_for_test());
181 }
182
183 // Test that startup background hosts can be created explicitly before the
184 // extension system is ready (this is the normal pattern in Chrome).
185 TEST_F(ProcessManagerTest, CreateBackgroundHostsExplicitly) {
186 scoped_ptr<ProcessManager> manager(ProcessManager::CreateForTesting(
187 original_context(), extension_registry()));
188 ASSERT_FALSE(manager->startup_background_hosts_created_for_test());
189
190 // Embedder explicitly asks for hosts to be created. Chrome does this on
191 // normal startup.
192 manager->MaybeCreateStartupBackgroundHosts();
193 EXPECT_TRUE(manager->startup_background_hosts_created_for_test());
194 }
195
196 // Test that the embedder can defer background host creation. Chrome does this
197 // when the profile is created asynchronously, which may take a while.
198 TEST_F(ProcessManagerTest, CreateBackgroundHostsDeferred) {
199 scoped_ptr<ProcessManager> manager(ProcessManager::CreateForTesting(
200 original_context(), extension_registry()));
201 ASSERT_FALSE(manager->startup_background_hosts_created_for_test());
202
203 // Don't create background hosts if the delegate says to defer them.
204 process_manager_delegate()->defer_creating_startup_background_hosts_ = true;
205 manager->MaybeCreateStartupBackgroundHosts();
206 EXPECT_FALSE(manager->startup_background_hosts_created_for_test());
207
208 // The extension system becoming ready still doesn't create the hosts.
209 content::NotificationService::current()->Notify(
210 chrome::NOTIFICATION_EXTENSIONS_READY,
211 content::Source<BrowserContext>(original_context()),
212 content::NotificationService::NoDetails());
213 EXPECT_FALSE(manager->startup_background_hosts_created_for_test());
214
215 // Once the embedder is ready the background hosts can be created.
216 process_manager_delegate()->defer_creating_startup_background_hosts_ = false;
217 manager->MaybeCreateStartupBackgroundHosts();
218 EXPECT_TRUE(manager->startup_background_hosts_created_for_test());
219 }
220
221 // Test that the embedder can disallow background host creation.
222 // Chrome OS does this in guest mode.
223 TEST_F(ProcessManagerTest, IsBackgroundHostAllowed) {
224 scoped_ptr<ProcessManager> manager(ProcessManager::CreateForTesting(
225 original_context(), extension_registry()));
226 ASSERT_FALSE(manager->startup_background_hosts_created_for_test());
227
228 // Don't create background hosts if the delegate disallows them.
229 process_manager_delegate()->is_background_page_allowed_ = false;
230 manager->MaybeCreateStartupBackgroundHosts();
231 EXPECT_FALSE(manager->startup_background_hosts_created_for_test());
232
233 // The extension system becoming ready still doesn't create the hosts.
234 content::NotificationService::current()->Notify(
235 chrome::NOTIFICATION_EXTENSIONS_READY,
236 content::Source<BrowserContext>(original_context()),
237 content::NotificationService::NoDetails());
238 EXPECT_FALSE(manager->startup_background_hosts_created_for_test());
239 }
240
128 // Test that extensions get grouped in the right SiteInstance (and therefore 241 // Test that extensions get grouped in the right SiteInstance (and therefore
129 // process) based on their URLs. 242 // process) based on their URLs.
130 TEST_F(ProcessManagerTest, ProcessGrouping) { 243 TEST_F(ProcessManagerTest, ProcessGrouping) {
131 content::ContentBrowserClient content_browser_client; 244 content::ContentBrowserClient content_browser_client;
132 content::SetBrowserClientForTesting(&content_browser_client); 245 content::SetBrowserClientForTesting(&content_browser_client);
133 246
134 // Extensions in different browser contexts should always be different 247 // Extensions in different browser contexts should always be different
135 // SiteInstances. 248 // SiteInstances.
136 scoped_ptr<ProcessManager> manager1( 249 scoped_ptr<ProcessManager> manager1(ProcessManager::CreateForTesting(
137 ProcessManager::Create(original_context())); 250 original_context(), extension_registry()));
138 // NOTE: This context is not associated with the TestExtensionsBrowserClient. 251 // NOTE: This context is not associated with the TestExtensionsBrowserClient.
139 // That's OK because we're not testing regular vs. incognito behavior. 252 // That's OK because we're not testing regular vs. incognito behavior.
140 TestBrowserContext another_context; 253 TestBrowserContext another_context;
141 scoped_ptr<ProcessManager> manager2(ProcessManager::Create(&another_context)); 254 ExtensionRegistry another_registry(&another_context);
255 scoped_ptr<ProcessManager> manager2(
256 ProcessManager::CreateForTesting(&another_context, &another_registry));
142 257
143 // Extensions with common origins ("scheme://id/") should be grouped in the 258 // Extensions with common origins ("scheme://id/") should be grouped in the
144 // same SiteInstance. 259 // same SiteInstance.
145 GURL ext1_url1("chrome-extension://ext1_id/index.html"); 260 GURL ext1_url1("chrome-extension://ext1_id/index.html");
146 GURL ext1_url2("chrome-extension://ext1_id/monkey/monkey.html"); 261 GURL ext1_url2("chrome-extension://ext1_id/monkey/monkey.html");
147 GURL ext2_url1("chrome-extension://ext2_id/index.html"); 262 GURL ext2_url1("chrome-extension://ext2_id/index.html");
148 263
149 scoped_refptr<SiteInstance> site11 = 264 scoped_refptr<SiteInstance> site11 =
150 manager1->GetSiteInstanceForURL(ext1_url1); 265 manager1->GetSiteInstanceForURL(ext1_url1);
151 scoped_refptr<SiteInstance> site12 = 266 scoped_refptr<SiteInstance> site12 =
152 manager1->GetSiteInstanceForURL(ext1_url2); 267 manager1->GetSiteInstanceForURL(ext1_url2);
153 EXPECT_EQ(site11, site12); 268 EXPECT_EQ(site11, site12);
154 269
155 scoped_refptr<SiteInstance> site21 = 270 scoped_refptr<SiteInstance> site21 =
156 manager1->GetSiteInstanceForURL(ext2_url1); 271 manager1->GetSiteInstanceForURL(ext2_url1);
157 EXPECT_NE(site11, site21); 272 EXPECT_NE(site11, site21);
158 273
159 scoped_refptr<SiteInstance> other_profile_site = 274 scoped_refptr<SiteInstance> other_profile_site =
160 manager2->GetSiteInstanceForURL(ext1_url1); 275 manager2->GetSiteInstanceForURL(ext1_url1);
161 EXPECT_NE(site11, other_profile_site); 276 EXPECT_NE(site11, other_profile_site);
162 } 277 }
163 278
164 } // namespace extensions 279 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/browser/process_manager_delegate.h ('k') | extensions/browser/test_extensions_browser_client.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698