OLD | NEW |
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 "extensions/browser/extensions_test_browser_context.h" |
12 #include "extensions/browser/test_extensions_browser_client.h" | 12 #include "extensions/browser/test_extensions_browser_client.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 | 14 |
15 using content::BrowserContext; | 15 using content::BrowserContext; |
16 using content::SiteInstance; | 16 using content::SiteInstance; |
17 using content::TestBrowserContext; | |
18 | 17 |
19 namespace extensions { | 18 namespace extensions { |
20 | 19 |
21 namespace { | 20 namespace { |
22 | 21 |
23 // An incognito version of a TestBrowserContext. | 22 // An incognito version of a ExtensionsTestBrowserContext. |
24 class TestBrowserContextIncognito : public TestBrowserContext { | 23 class TestBrowserContextIncognito : public ExtensionsTestBrowserContext { |
25 public: | 24 public: |
26 TestBrowserContextIncognito() {} | 25 TestBrowserContextIncognito() {} |
27 virtual ~TestBrowserContextIncognito() {} | 26 virtual ~TestBrowserContextIncognito() {} |
28 | 27 |
29 // TestBrowserContext implementation. | 28 // TestBrowserContext implementation. |
30 virtual bool IsOffTheRecord() const OVERRIDE { return true; } | 29 virtual bool IsOffTheRecord() const OVERRIDE { return true; } |
31 | 30 |
32 private: | 31 private: |
33 DISALLOW_COPY_AND_ASSIGN(TestBrowserContextIncognito); | 32 DISALLOW_COPY_AND_ASSIGN(TestBrowserContextIncognito); |
34 }; | 33 }; |
(...skipping 17 matching lines...) Expand all Loading... |
52 // Returns true if the notification |type| is registered for |manager| with | 51 // Returns true if the notification |type| is registered for |manager| with |
53 // source |context|. Pass NULL for |context| for all sources. | 52 // source |context|. Pass NULL for |context| for all sources. |
54 static bool IsRegistered(ProcessManager* manager, | 53 static bool IsRegistered(ProcessManager* manager, |
55 int type, | 54 int type, |
56 BrowserContext* context) { | 55 BrowserContext* context) { |
57 return manager->registrar_.IsRegistered( | 56 return manager->registrar_.IsRegistered( |
58 manager, type, content::Source<BrowserContext>(context)); | 57 manager, type, content::Source<BrowserContext>(context)); |
59 } | 58 } |
60 | 59 |
61 private: | 60 private: |
62 TestBrowserContext original_context_; | 61 ExtensionsTestBrowserContext original_context_; |
63 TestBrowserContextIncognito incognito_context_; | 62 TestBrowserContextIncognito incognito_context_; |
64 TestExtensionsBrowserClient extensions_browser_client_; | 63 TestExtensionsBrowserClient extensions_browser_client_; |
65 | 64 |
66 DISALLOW_COPY_AND_ASSIGN(ProcessManagerTest); | 65 DISALLOW_COPY_AND_ASSIGN(ProcessManagerTest); |
67 }; | 66 }; |
68 | 67 |
69 // Test that notification registration works properly. | 68 // Test that notification registration works properly. |
70 TEST_F(ProcessManagerTest, ExtensionNotificationRegistration) { | 69 TEST_F(ProcessManagerTest, ExtensionNotificationRegistration) { |
71 // Test for a normal context ProcessManager. | 70 // Test for a normal context ProcessManager. |
72 scoped_ptr<ProcessManager> manager1( | 71 scoped_ptr<ProcessManager> manager1( |
73 ProcessManager::Create(original_context())); | 72 ProcessManager::Create(original_context())); |
74 | 73 |
75 EXPECT_EQ(original_context(), manager1->GetBrowserContext()); | 74 EXPECT_EQ(original_context(), manager1->GetBrowserContext()); |
76 EXPECT_EQ(0u, manager1->background_hosts().size()); | 75 EXPECT_EQ(0u, manager1->background_hosts().size()); |
77 | 76 |
78 // It observes other notifications from this context. | 77 // It observes other notifications from this context. |
79 EXPECT_TRUE(IsRegistered(manager1.get(), | 78 EXPECT_TRUE(IsRegistered(manager1.get(), |
80 chrome::NOTIFICATION_EXTENSIONS_READY, | 79 chrome::NOTIFICATION_EXTENSIONS_READY, |
81 original_context())); | 80 original_context())); |
82 EXPECT_TRUE(IsRegistered(manager1.get(), | 81 EXPECT_TRUE(IsRegistered(manager1.get(), |
83 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, | |
84 original_context())); | |
85 EXPECT_TRUE(IsRegistered(manager1.get(), | |
86 chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, | |
87 original_context())); | |
88 EXPECT_TRUE(IsRegistered(manager1.get(), | |
89 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, | 82 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, |
90 original_context())); | 83 original_context())); |
91 | 84 |
92 // Test for an incognito context ProcessManager. | 85 // Test for an incognito context ProcessManager. |
93 scoped_ptr<ProcessManager> manager2(ProcessManager::CreateIncognitoForTesting( | 86 scoped_ptr<ProcessManager> manager2(ProcessManager::CreateIncognitoForTesting( |
94 incognito_context(), original_context(), manager1.get())); | 87 incognito_context(), original_context(), manager1.get())); |
95 | 88 |
96 EXPECT_EQ(incognito_context(), manager2->GetBrowserContext()); | 89 EXPECT_EQ(incognito_context(), manager2->GetBrowserContext()); |
97 EXPECT_EQ(0u, manager2->background_hosts().size()); | 90 EXPECT_EQ(0u, manager2->background_hosts().size()); |
98 | 91 |
99 // Some notifications are observed for the original context. | |
100 EXPECT_TRUE(IsRegistered(manager2.get(), | |
101 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, | |
102 original_context())); | |
103 | |
104 // Some notifications are observed for the incognito context. | 92 // Some notifications are observed for the incognito context. |
105 EXPECT_TRUE(IsRegistered(manager2.get(), | 93 EXPECT_TRUE(IsRegistered(manager2.get(), |
106 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, | 94 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, |
107 incognito_context())); | 95 incognito_context())); |
108 | 96 |
109 // Some notifications are observed for both incognito and original. | 97 // Some notifications are observed for both incognito and original. |
110 EXPECT_TRUE(IsRegistered(manager2.get(), | 98 EXPECT_TRUE(IsRegistered(manager2.get(), |
111 chrome::NOTIFICATION_PROFILE_DESTROYED, | 99 chrome::NOTIFICATION_PROFILE_DESTROYED, |
112 original_context())); | 100 original_context())); |
113 EXPECT_TRUE(IsRegistered(manager2.get(), | 101 EXPECT_TRUE(IsRegistered(manager2.get(), |
(...skipping 14 matching lines...) Expand all Loading... |
128 // Test that extensions get grouped in the right SiteInstance (and therefore | 116 // Test that extensions get grouped in the right SiteInstance (and therefore |
129 // process) based on their URLs. | 117 // process) based on their URLs. |
130 TEST_F(ProcessManagerTest, ProcessGrouping) { | 118 TEST_F(ProcessManagerTest, ProcessGrouping) { |
131 content::ContentBrowserClient content_browser_client; | 119 content::ContentBrowserClient content_browser_client; |
132 content::SetBrowserClientForTesting(&content_browser_client); | 120 content::SetBrowserClientForTesting(&content_browser_client); |
133 | 121 |
134 // Extensions in different browser contexts should always be different | 122 // Extensions in different browser contexts should always be different |
135 // SiteInstances. | 123 // SiteInstances. |
136 scoped_ptr<ProcessManager> manager1( | 124 scoped_ptr<ProcessManager> manager1( |
137 ProcessManager::Create(original_context())); | 125 ProcessManager::Create(original_context())); |
138 // NOTE: This context is not associated with the TestExtensionsBrowserClient. | 126 |
139 // That's OK because we're not testing regular vs. incognito behavior. | 127 ExtensionsTestBrowserContext another_context; |
140 TestBrowserContext another_context; | |
141 scoped_ptr<ProcessManager> manager2(ProcessManager::Create(&another_context)); | 128 scoped_ptr<ProcessManager> manager2(ProcessManager::Create(&another_context)); |
142 | 129 |
143 // Extensions with common origins ("scheme://id/") should be grouped in the | 130 // Extensions with common origins ("scheme://id/") should be grouped in the |
144 // same SiteInstance. | 131 // same SiteInstance. |
145 GURL ext1_url1("chrome-extension://ext1_id/index.html"); | 132 GURL ext1_url1("chrome-extension://ext1_id/index.html"); |
146 GURL ext1_url2("chrome-extension://ext1_id/monkey/monkey.html"); | 133 GURL ext1_url2("chrome-extension://ext1_id/monkey/monkey.html"); |
147 GURL ext2_url1("chrome-extension://ext2_id/index.html"); | 134 GURL ext2_url1("chrome-extension://ext2_id/index.html"); |
148 | 135 |
149 scoped_refptr<SiteInstance> site11 = | 136 scoped_refptr<SiteInstance> site11 = |
150 manager1->GetSiteInstanceForURL(ext1_url1); | 137 manager1->GetSiteInstanceForURL(ext1_url1); |
151 scoped_refptr<SiteInstance> site12 = | 138 scoped_refptr<SiteInstance> site12 = |
152 manager1->GetSiteInstanceForURL(ext1_url2); | 139 manager1->GetSiteInstanceForURL(ext1_url2); |
153 EXPECT_EQ(site11, site12); | 140 EXPECT_EQ(site11, site12); |
154 | 141 |
155 scoped_refptr<SiteInstance> site21 = | 142 scoped_refptr<SiteInstance> site21 = |
156 manager1->GetSiteInstanceForURL(ext2_url1); | 143 manager1->GetSiteInstanceForURL(ext2_url1); |
157 EXPECT_NE(site11, site21); | 144 EXPECT_NE(site11, site21); |
158 | 145 |
159 scoped_refptr<SiteInstance> other_profile_site = | 146 scoped_refptr<SiteInstance> other_profile_site = |
160 manager2->GetSiteInstanceForURL(ext1_url1); | 147 manager2->GetSiteInstanceForURL(ext1_url1); |
161 EXPECT_NE(site11, other_profile_site); | 148 EXPECT_NE(site11, other_profile_site); |
162 } | 149 } |
163 | 150 |
164 } // namespace extensions | 151 } // namespace extensions |
OLD | NEW |