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 "chrome/browser/chrome_notification_types.h"
8 #include "content/public/browser/content_browser_client.h"
9 #include "content/public/browser/notification_service.h"
10 #include "content/public/browser/site_instance.h"
11 #include "content/public/test/test_browser_context.h"
12 #include "extensions/browser/test_extensions_browser_client.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 using content::BrowserContext;
16 using content::SiteInstance;
17 using content::TestBrowserContext;
19 namespace extensions {
23 // An incognito version of a TestBrowserContext.
24 class TestBrowserContextIncognito : public TestBrowserContext {
26 TestBrowserContextIncognito() {}
27 virtual ~TestBrowserContextIncognito() {}
29 // TestBrowserContext implementation.
30 virtual bool IsOffTheRecord() const OVERRIDE { return true; }
33 DISALLOW_COPY_AND_ASSIGN(TestBrowserContextIncognito);
38 class ProcessManagerTest : public testing::Test {
40 ProcessManagerTest() : extensions_browser_client_(&original_context_) {
41 extensions_browser_client_.SetIncognitoContext(&incognito_context_);
42 ExtensionsBrowserClient::Set(&extensions_browser_client_);
45 virtual ~ProcessManagerTest() {
46 ExtensionsBrowserClient::Set(NULL);
49 BrowserContext* original_context() { return &original_context_; }
50 BrowserContext* incognito_context() { return &incognito_context_; }
52 // Returns true if the notification |type| is registered for |manager| with
53 // source |context|. Pass NULL for |context| for all sources.
54 static bool IsRegistered(ProcessManager* manager,
56 BrowserContext* context) {
57 return manager->registrar_.IsRegistered(
58 manager, type, content::Source<BrowserContext>(context));
62 TestBrowserContext original_context_;
63 TestBrowserContextIncognito incognito_context_;
64 TestExtensionsBrowserClient extensions_browser_client_;
66 DISALLOW_COPY_AND_ASSIGN(ProcessManagerTest);
69 // Test that notification registration works properly.
70 TEST_F(ProcessManagerTest, ExtensionNotificationRegistration) {
71 // Test for a normal context ProcessManager.
72 scoped_ptr<ProcessManager> manager1(
73 ProcessManager::Create(original_context()));
75 EXPECT_EQ(original_context(), manager1->GetBrowserContext());
76 EXPECT_EQ(0u, manager1->background_hosts().size());
78 // It observes other notifications from this context.
79 EXPECT_TRUE(IsRegistered(manager1.get(),
80 chrome::NOTIFICATION_EXTENSIONS_READY,
82 EXPECT_TRUE(IsRegistered(manager1.get(),
83 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED,
85 EXPECT_TRUE(IsRegistered(manager1.get(),
86 chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
88 EXPECT_TRUE(IsRegistered(manager1.get(),
89 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED,
92 // Test for an incognito context ProcessManager.
93 scoped_ptr<ProcessManager> manager2(ProcessManager::CreateIncognitoForTesting(
94 incognito_context(), original_context(), manager1.get()));
96 EXPECT_EQ(incognito_context(), manager2->GetBrowserContext());
97 EXPECT_EQ(0u, manager2->background_hosts().size());
99 // Some notifications are observed for the original context.
100 EXPECT_TRUE(IsRegistered(manager2.get(),
101 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED,
102 original_context()));
104 // Some notifications are observed for the incognito context.
105 EXPECT_TRUE(IsRegistered(manager2.get(),
106 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED,
107 incognito_context()));
109 // Some notifications are observed for both incognito and original.
110 EXPECT_TRUE(IsRegistered(manager2.get(),
111 chrome::NOTIFICATION_PROFILE_DESTROYED,
112 original_context()));
113 EXPECT_TRUE(IsRegistered(manager2.get(),
114 chrome::NOTIFICATION_PROFILE_DESTROYED,
115 incognito_context()));
117 // Some are not observed at all.
118 EXPECT_FALSE(IsRegistered(manager2.get(),
119 chrome::NOTIFICATION_EXTENSIONS_READY,
120 original_context()));
122 // This notification is observed for incognito contexts only.
123 EXPECT_TRUE(IsRegistered(manager2.get(),
124 chrome::NOTIFICATION_PROFILE_DESTROYED,
125 incognito_context()));
128 // Test that extensions get grouped in the right SiteInstance (and therefore
129 // process) based on their URLs.
130 TEST_F(ProcessManagerTest, ProcessGrouping) {
131 content::ContentBrowserClient content_browser_client;
132 content::SetBrowserClientForTesting(&content_browser_client);
134 // Extensions in different browser contexts should always be different
136 scoped_ptr<ProcessManager> manager1(
137 ProcessManager::Create(original_context()));
138 // NOTE: This context is not associated with the TestExtensionsBrowserClient.
139 // That's OK because we're not testing regular vs. incognito behavior.
140 TestBrowserContext another_context;
141 scoped_ptr<ProcessManager> manager2(ProcessManager::Create(&another_context));
143 // Extensions with common origins ("scheme://id/") should be grouped in the
144 // same SiteInstance.
145 GURL ext1_url1("chrome-extension://ext1_id/index.html");
146 GURL ext1_url2("chrome-extension://ext1_id/monkey/monkey.html");
147 GURL ext2_url1("chrome-extension://ext2_id/index.html");
149 scoped_refptr<SiteInstance> site11 =
150 manager1->GetSiteInstanceForURL(ext1_url1);
151 scoped_refptr<SiteInstance> site12 =
152 manager1->GetSiteInstanceForURL(ext1_url2);
153 EXPECT_EQ(site11, site12);
155 scoped_refptr<SiteInstance> site21 =
156 manager1->GetSiteInstanceForURL(ext2_url1);
157 EXPECT_NE(site11, site21);
159 scoped_refptr<SiteInstance> other_profile_site =
160 manager2->GetSiteInstanceForURL(ext1_url1);
161 EXPECT_NE(site11, other_profile_site);
164 } // namespace extensions