Suppress a Valgrind report.
[chromium-blink-merge.git] / extensions / browser / process_manager_unittest.cc
blob133b6ff0a434d421173736d6f1aa44e794f78a50
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
3 // found in the LICENSE file.
5 #include "extensions/browser/process_manager.h"
7 #include "content/public/browser/content_browser_client.h"
8 #include "content/public/browser/notification_service.h"
9 #include "content/public/browser/site_instance.h"
10 #include "content/public/common/content_client.h"
11 #include "content/public/test/test_browser_context.h"
12 #include "extensions/browser/extension_registry.h"
13 #include "extensions/browser/extensions_test.h"
14 #include "extensions/browser/notification_types.h"
15 #include "extensions/browser/process_manager_delegate.h"
16 #include "extensions/browser/test_extensions_browser_client.h"
18 using content::BrowserContext;
19 using content::SiteInstance;
20 using content::TestBrowserContext;
22 namespace extensions {
24 namespace {
26 // An incognito version of a TestBrowserContext.
27 class TestBrowserContextIncognito : public TestBrowserContext {
28 public:
29 TestBrowserContextIncognito() {}
30 ~TestBrowserContextIncognito() override {}
32 // TestBrowserContext implementation.
33 bool IsOffTheRecord() const override { return true; }
35 private:
36 DISALLOW_COPY_AND_ASSIGN(TestBrowserContextIncognito);
39 // A trivial ProcessManagerDelegate.
40 class TestProcessManagerDelegate : public ProcessManagerDelegate {
41 public:
42 TestProcessManagerDelegate()
43 : is_background_page_allowed_(true),
44 defer_creating_startup_background_hosts_(false) {}
45 ~TestProcessManagerDelegate() override {}
47 // ProcessManagerDelegate implementation.
48 bool IsBackgroundPageAllowed(BrowserContext* context) const override {
49 return is_background_page_allowed_;
51 bool DeferCreatingStartupBackgroundHosts(
52 BrowserContext* context) const override {
53 return defer_creating_startup_background_hosts_;
56 bool is_background_page_allowed_;
57 bool defer_creating_startup_background_hosts_;
60 } // namespace
62 class ProcessManagerTest : public ExtensionsTest {
63 public:
64 ProcessManagerTest()
65 : notification_service_(content::NotificationService::Create()),
66 extension_registry_(browser_context()) {
67 extensions_browser_client()->SetIncognitoContext(&incognito_context_);
68 extensions_browser_client()->set_process_manager_delegate(
69 &process_manager_delegate_);
72 ~ProcessManagerTest() override {}
74 // Use original_context() to make it clear it is a non-incognito context.
75 BrowserContext* original_context() { return browser_context(); }
76 BrowserContext* incognito_context() { return &incognito_context_; }
77 ExtensionRegistry* extension_registry() { return &extension_registry_; }
78 TestProcessManagerDelegate* process_manager_delegate() {
79 return &process_manager_delegate_;
82 // Returns true if the notification |type| is registered for |manager| with
83 // source |context|. Pass NULL for |context| for all sources.
84 static bool IsRegistered(ProcessManager* manager,
85 int type,
86 BrowserContext* context) {
87 return manager->registrar_.IsRegistered(
88 manager, type, content::Source<BrowserContext>(context));
91 private:
92 scoped_ptr<content::NotificationService> notification_service_;
93 TestBrowserContextIncognito incognito_context_;
94 ExtensionRegistry extension_registry_; // Shared between BrowserContexts.
95 TestProcessManagerDelegate process_manager_delegate_;
97 DISALLOW_COPY_AND_ASSIGN(ProcessManagerTest);
100 // Test that notification registration works properly.
101 TEST_F(ProcessManagerTest, ExtensionNotificationRegistration) {
102 // Test for a normal context ProcessManager.
103 scoped_ptr<ProcessManager> manager1(ProcessManager::CreateForTesting(
104 original_context(), extension_registry()));
106 EXPECT_EQ(original_context(), manager1->GetBrowserContext());
107 EXPECT_EQ(0u, manager1->background_hosts().size());
109 // It observes other notifications from this context.
110 EXPECT_TRUE(IsRegistered(manager1.get(),
111 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED,
112 original_context()));
113 EXPECT_TRUE(IsRegistered(manager1.get(),
114 extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED,
115 original_context()));
116 EXPECT_TRUE(
117 IsRegistered(manager1.get(),
118 extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
119 original_context()));
120 EXPECT_TRUE(IsRegistered(manager1.get(),
121 extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED,
122 original_context()));
124 // Test for an incognito context ProcessManager.
125 scoped_ptr<ProcessManager> manager2(
126 ProcessManager::CreateIncognitoForTesting(incognito_context(),
127 original_context(),
128 extension_registry()));
130 EXPECT_EQ(incognito_context(), manager2->GetBrowserContext());
131 EXPECT_EQ(0u, manager2->background_hosts().size());
133 // Some notifications are observed for the original context.
134 EXPECT_TRUE(IsRegistered(manager2.get(),
135 extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED,
136 original_context()));
138 // Some notifications are observed for the incognito context.
139 EXPECT_TRUE(IsRegistered(manager2.get(),
140 extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED,
141 incognito_context()));
143 // Some are not observed at all.
144 EXPECT_FALSE(
145 IsRegistered(manager2.get(),
146 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED,
147 original_context()));
150 // Test that startup background hosts are created when the extension system
151 // becomes ready.
153 // NOTE: This test and those that follow do not try to create ExtensionsHosts
154 // because ExtensionHost is tightly coupled to WebContents and can't be
155 // constructed in unit tests.
156 TEST_F(ProcessManagerTest, CreateBackgroundHostsOnExtensionsReady) {
157 scoped_ptr<ProcessManager> manager(ProcessManager::CreateForTesting(
158 original_context(), extension_registry()));
159 ASSERT_FALSE(manager->startup_background_hosts_created_for_test());
161 // Simulate the extension system becoming ready.
162 content::NotificationService::current()->Notify(
163 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED,
164 content::Source<BrowserContext>(original_context()),
165 content::NotificationService::NoDetails());
166 EXPECT_TRUE(manager->startup_background_hosts_created_for_test());
169 // Test that startup background hosts can be created explicitly before the
170 // extension system is ready (this is the normal pattern in Chrome).
171 TEST_F(ProcessManagerTest, CreateBackgroundHostsExplicitly) {
172 scoped_ptr<ProcessManager> manager(ProcessManager::CreateForTesting(
173 original_context(), extension_registry()));
174 ASSERT_FALSE(manager->startup_background_hosts_created_for_test());
176 // Embedder explicitly asks for hosts to be created. Chrome does this on
177 // normal startup.
178 manager->MaybeCreateStartupBackgroundHosts();
179 EXPECT_TRUE(manager->startup_background_hosts_created_for_test());
182 // Test that the embedder can defer background host creation. Chrome does this
183 // when the profile is created asynchronously, which may take a while.
184 TEST_F(ProcessManagerTest, CreateBackgroundHostsDeferred) {
185 scoped_ptr<ProcessManager> manager(ProcessManager::CreateForTesting(
186 original_context(), extension_registry()));
187 ASSERT_FALSE(manager->startup_background_hosts_created_for_test());
189 // Don't create background hosts if the delegate says to defer them.
190 process_manager_delegate()->defer_creating_startup_background_hosts_ = true;
191 manager->MaybeCreateStartupBackgroundHosts();
192 EXPECT_FALSE(manager->startup_background_hosts_created_for_test());
194 // The extension system becoming ready still doesn't create the hosts.
195 content::NotificationService::current()->Notify(
196 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED,
197 content::Source<BrowserContext>(original_context()),
198 content::NotificationService::NoDetails());
199 EXPECT_FALSE(manager->startup_background_hosts_created_for_test());
201 // Once the embedder is ready the background hosts can be created.
202 process_manager_delegate()->defer_creating_startup_background_hosts_ = false;
203 manager->MaybeCreateStartupBackgroundHosts();
204 EXPECT_TRUE(manager->startup_background_hosts_created_for_test());
207 // Test that the embedder can disallow background host creation.
208 // Chrome OS does this in guest mode.
209 TEST_F(ProcessManagerTest, IsBackgroundHostAllowed) {
210 scoped_ptr<ProcessManager> manager(ProcessManager::CreateForTesting(
211 original_context(), extension_registry()));
212 ASSERT_FALSE(manager->startup_background_hosts_created_for_test());
214 // Don't create background hosts if the delegate disallows them.
215 process_manager_delegate()->is_background_page_allowed_ = false;
216 manager->MaybeCreateStartupBackgroundHosts();
217 EXPECT_FALSE(manager->startup_background_hosts_created_for_test());
219 // The extension system becoming ready still doesn't create the hosts.
220 content::NotificationService::current()->Notify(
221 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED,
222 content::Source<BrowserContext>(original_context()),
223 content::NotificationService::NoDetails());
224 EXPECT_FALSE(manager->startup_background_hosts_created_for_test());
227 // Test that extensions get grouped in the right SiteInstance (and therefore
228 // process) based on their URLs.
229 TEST_F(ProcessManagerTest, ProcessGrouping) {
230 // Extensions in different browser contexts should always be different
231 // SiteInstances.
232 scoped_ptr<ProcessManager> manager1(ProcessManager::CreateForTesting(
233 original_context(), extension_registry()));
234 // NOTE: This context is not associated with the TestExtensionsBrowserClient.
235 // That's OK because we're not testing regular vs. incognito behavior.
236 TestBrowserContext another_context;
237 ExtensionRegistry another_registry(&another_context);
238 scoped_ptr<ProcessManager> manager2(
239 ProcessManager::CreateForTesting(&another_context, &another_registry));
241 // Extensions with common origins ("scheme://id/") should be grouped in the
242 // same SiteInstance.
243 GURL ext1_url1("chrome-extension://ext1_id/index.html");
244 GURL ext1_url2("chrome-extension://ext1_id/monkey/monkey.html");
245 GURL ext2_url1("chrome-extension://ext2_id/index.html");
247 scoped_refptr<SiteInstance> site11 =
248 manager1->GetSiteInstanceForURL(ext1_url1);
249 scoped_refptr<SiteInstance> site12 =
250 manager1->GetSiteInstanceForURL(ext1_url2);
251 EXPECT_EQ(site11, site12);
253 scoped_refptr<SiteInstance> site21 =
254 manager1->GetSiteInstanceForURL(ext2_url1);
255 EXPECT_NE(site11, site21);
257 scoped_refptr<SiteInstance> other_profile_site =
258 manager2->GetSiteInstanceForURL(ext1_url1);
259 EXPECT_NE(site11, other_profile_site);
262 } // namespace extensions