Durable Storage: Refactor browser test and test the basic "deny" flow.
[chromium-blink-merge.git] / extensions / browser / process_manager_unittest.cc
blob34b74a9fd2d5bed36885e8fe278c9238b7bd3a47
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->browser_context());
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_HOST_DESTROYED,
115 original_context()));
117 // Test for an incognito context ProcessManager.
118 scoped_ptr<ProcessManager> manager2(
119 ProcessManager::CreateIncognitoForTesting(incognito_context(),
120 original_context(),
121 extension_registry()));
123 EXPECT_EQ(incognito_context(), manager2->browser_context());
124 EXPECT_EQ(0u, manager2->background_hosts().size());
126 // Some notifications are observed for the incognito context.
127 EXPECT_TRUE(IsRegistered(manager2.get(),
128 extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED,
129 incognito_context()));
131 // Some are not observed at all.
132 EXPECT_FALSE(
133 IsRegistered(manager2.get(),
134 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED,
135 original_context()));
138 // Test that startup background hosts are created when the extension system
139 // becomes ready.
141 // NOTE: This test and those that follow do not try to create ExtensionsHosts
142 // because ExtensionHost is tightly coupled to WebContents and can't be
143 // constructed in unit tests.
144 TEST_F(ProcessManagerTest, CreateBackgroundHostsOnExtensionsReady) {
145 scoped_ptr<ProcessManager> manager(ProcessManager::CreateForTesting(
146 original_context(), extension_registry()));
147 ASSERT_FALSE(manager->startup_background_hosts_created_for_test());
149 // Simulate the extension system becoming ready.
150 content::NotificationService::current()->Notify(
151 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED,
152 content::Source<BrowserContext>(original_context()),
153 content::NotificationService::NoDetails());
154 EXPECT_TRUE(manager->startup_background_hosts_created_for_test());
157 // Test that startup background hosts can be created explicitly before the
158 // extension system is ready (this is the normal pattern in Chrome).
159 TEST_F(ProcessManagerTest, CreateBackgroundHostsExplicitly) {
160 scoped_ptr<ProcessManager> manager(ProcessManager::CreateForTesting(
161 original_context(), extension_registry()));
162 ASSERT_FALSE(manager->startup_background_hosts_created_for_test());
164 // Embedder explicitly asks for hosts to be created. Chrome does this on
165 // normal startup.
166 manager->MaybeCreateStartupBackgroundHosts();
167 EXPECT_TRUE(manager->startup_background_hosts_created_for_test());
170 // Test that the embedder can defer background host creation. Chrome does this
171 // when the profile is created asynchronously, which may take a while.
172 TEST_F(ProcessManagerTest, CreateBackgroundHostsDeferred) {
173 scoped_ptr<ProcessManager> manager(ProcessManager::CreateForTesting(
174 original_context(), extension_registry()));
175 ASSERT_FALSE(manager->startup_background_hosts_created_for_test());
177 // Don't create background hosts if the delegate says to defer them.
178 process_manager_delegate()->defer_creating_startup_background_hosts_ = true;
179 manager->MaybeCreateStartupBackgroundHosts();
180 EXPECT_FALSE(manager->startup_background_hosts_created_for_test());
182 // The extension system becoming ready still doesn't create the hosts.
183 content::NotificationService::current()->Notify(
184 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED,
185 content::Source<BrowserContext>(original_context()),
186 content::NotificationService::NoDetails());
187 EXPECT_FALSE(manager->startup_background_hosts_created_for_test());
189 // Once the embedder is ready the background hosts can be created.
190 process_manager_delegate()->defer_creating_startup_background_hosts_ = false;
191 manager->MaybeCreateStartupBackgroundHosts();
192 EXPECT_TRUE(manager->startup_background_hosts_created_for_test());
195 // Test that the embedder can disallow background host creation.
196 // Chrome OS does this in guest mode.
197 TEST_F(ProcessManagerTest, IsBackgroundHostAllowed) {
198 scoped_ptr<ProcessManager> manager(ProcessManager::CreateForTesting(
199 original_context(), extension_registry()));
200 ASSERT_FALSE(manager->startup_background_hosts_created_for_test());
202 // Don't create background hosts if the delegate disallows them.
203 process_manager_delegate()->is_background_page_allowed_ = false;
204 manager->MaybeCreateStartupBackgroundHosts();
205 EXPECT_FALSE(manager->startup_background_hosts_created_for_test());
207 // The extension system becoming ready still doesn't create the hosts.
208 content::NotificationService::current()->Notify(
209 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED,
210 content::Source<BrowserContext>(original_context()),
211 content::NotificationService::NoDetails());
212 EXPECT_FALSE(manager->startup_background_hosts_created_for_test());
215 // Test that extensions get grouped in the right SiteInstance (and therefore
216 // process) based on their URLs.
217 TEST_F(ProcessManagerTest, ProcessGrouping) {
218 // Extensions in different browser contexts should always be different
219 // SiteInstances.
220 scoped_ptr<ProcessManager> manager1(ProcessManager::CreateForTesting(
221 original_context(), extension_registry()));
222 // NOTE: This context is not associated with the TestExtensionsBrowserClient.
223 // That's OK because we're not testing regular vs. incognito behavior.
224 TestBrowserContext another_context;
225 ExtensionRegistry another_registry(&another_context);
226 scoped_ptr<ProcessManager> manager2(
227 ProcessManager::CreateForTesting(&another_context, &another_registry));
229 // Extensions with common origins ("scheme://id/") should be grouped in the
230 // same SiteInstance.
231 GURL ext1_url1("chrome-extension://ext1_id/index.html");
232 GURL ext1_url2("chrome-extension://ext1_id/monkey/monkey.html");
233 GURL ext2_url1("chrome-extension://ext2_id/index.html");
235 scoped_refptr<SiteInstance> site11 =
236 manager1->GetSiteInstanceForURL(ext1_url1);
237 scoped_refptr<SiteInstance> site12 =
238 manager1->GetSiteInstanceForURL(ext1_url2);
239 EXPECT_EQ(site11, site12);
241 scoped_refptr<SiteInstance> site21 =
242 manager1->GetSiteInstanceForURL(ext2_url1);
243 EXPECT_NE(site11, site21);
245 scoped_refptr<SiteInstance> other_profile_site =
246 manager2->GetSiteInstanceForURL(ext1_url1);
247 EXPECT_NE(site11, other_profile_site);
250 } // namespace extensions