OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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. | |
4 | |
5 #include "chrome/browser/extensions/extension_process_manager.h" | |
6 | |
7 #include "chrome/browser/chrome_notification_types.h" | |
8 #include "chrome/browser/extensions/extension_error_reporter.h" | |
9 #include "chrome/test/base/testing_profile.h" | |
10 #include "content/public/browser/notification_service.h" | |
11 #include "content/public/browser/render_process_host.h" | |
12 #include "content/public/browser/site_instance.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 #include "testing/platform_test.h" | |
15 | |
16 using content::SiteInstance; | |
17 | |
18 // make the test a PlatformTest to setup autorelease pools properly on mac | |
19 class ExtensionProcessManagerTest : public testing::Test { | |
20 public: | |
21 static void SetUpTestCase() { | |
22 ExtensionErrorReporter::Init(false); // no noisy errors | |
23 } | |
24 | |
25 virtual void SetUp() { | |
26 ExtensionErrorReporter::GetInstance()->ClearErrors(); | |
27 } | |
28 | |
29 // Returns true if the notification |type| is registered for |manager| with | |
30 // source |profile|. Pass NULL for |profile| for all sources. | |
31 static bool IsRegistered(ExtensionProcessManager* manager, | |
32 int type, | |
33 TestingProfile* profile) { | |
34 return manager->registrar_.IsRegistered( | |
35 manager, type, content::Source<Profile>(profile)); | |
36 } | |
37 }; | |
38 | |
39 // Test that notification registration works properly. | |
40 TEST_F(ExtensionProcessManagerTest, ExtensionNotificationRegistration) { | |
41 // Test for a normal profile. | |
42 scoped_ptr<TestingProfile> original_profile(new TestingProfile); | |
43 scoped_ptr<ExtensionProcessManager> manager1( | |
44 ExtensionProcessManager::Create(original_profile.get())); | |
45 | |
46 EXPECT_EQ(original_profile.get(), manager1->GetBrowserContext()); | |
47 EXPECT_EQ(0u, manager1->background_hosts().size()); | |
48 | |
49 // It observes other notifications from this profile. | |
50 EXPECT_TRUE(IsRegistered(manager1.get(), | |
51 chrome::NOTIFICATION_EXTENSIONS_READY, | |
52 original_profile.get())); | |
53 EXPECT_TRUE(IsRegistered(manager1.get(), | |
54 chrome::NOTIFICATION_EXTENSION_LOADED, | |
55 original_profile.get())); | |
56 EXPECT_TRUE(IsRegistered(manager1.get(), | |
57 chrome::NOTIFICATION_EXTENSION_UNLOADED, | |
58 original_profile.get())); | |
59 EXPECT_TRUE(IsRegistered(manager1.get(), | |
60 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, | |
61 original_profile.get())); | |
62 | |
63 // Now add an incognito profile associated with the master above. | |
64 TestingProfile::Builder builder; | |
65 builder.SetIncognito(); | |
66 scoped_ptr<TestingProfile> incognito_profile = builder.Build(); | |
67 incognito_profile->SetOriginalProfile(original_profile.get()); | |
68 scoped_ptr<ExtensionProcessManager> manager2( | |
69 ExtensionProcessManager::Create(incognito_profile.get())); | |
70 | |
71 EXPECT_EQ(incognito_profile.get(), manager2->GetBrowserContext()); | |
72 EXPECT_EQ(0u, manager2->background_hosts().size()); | |
73 | |
74 // Some notifications are observed for the original profile. | |
75 EXPECT_TRUE(IsRegistered(manager2.get(), | |
76 chrome::NOTIFICATION_EXTENSION_LOADED, | |
77 original_profile.get())); | |
78 | |
79 // Some notifications are observed for the incognito profile. | |
80 EXPECT_TRUE(IsRegistered(manager2.get(), | |
81 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, | |
82 incognito_profile.get())); | |
83 | |
84 // Some notifications are observed for both incognito and original. | |
85 EXPECT_TRUE(IsRegistered(manager2.get(), | |
86 chrome::NOTIFICATION_PROFILE_DESTROYED, | |
87 original_profile.get())); | |
88 EXPECT_TRUE(IsRegistered(manager2.get(), | |
89 chrome::NOTIFICATION_PROFILE_DESTROYED, | |
90 incognito_profile.get())); | |
91 | |
92 // Some are not observed at all. | |
93 EXPECT_FALSE(IsRegistered(manager2.get(), | |
94 chrome::NOTIFICATION_EXTENSIONS_READY, | |
95 original_profile.get())); | |
96 | |
97 // This notification is observed for incognito profiles only. | |
98 EXPECT_TRUE(IsRegistered(manager2.get(), | |
99 chrome::NOTIFICATION_PROFILE_DESTROYED, | |
100 incognito_profile.get())); | |
101 } | |
102 | |
103 // Test that extensions get grouped in the right SiteInstance (and therefore | |
104 // process) based on their URLs. | |
105 TEST_F(ExtensionProcessManagerTest, ProcessGrouping) { | |
106 // Extensions in different profiles should always be different SiteInstances. | |
107 // Note: we don't initialize these, since we're not testing that | |
108 // functionality. This means we can get away with a NULL UserScriptMaster. | |
109 TestingProfile profile1; | |
110 scoped_ptr<ExtensionProcessManager> manager1( | |
111 ExtensionProcessManager::Create(&profile1)); | |
112 | |
113 TestingProfile profile2; | |
114 scoped_ptr<ExtensionProcessManager> manager2( | |
115 ExtensionProcessManager::Create(&profile2)); | |
116 | |
117 // Extensions with common origins ("scheme://id/") should be grouped in the | |
118 // same SiteInstance. | |
119 GURL ext1_url1("chrome-extension://ext1_id/index.html"); | |
120 GURL ext1_url2("chrome-extension://ext1_id/monkey/monkey.html"); | |
121 GURL ext2_url1("chrome-extension://ext2_id/index.html"); | |
122 | |
123 scoped_refptr<SiteInstance> site11 = | |
124 manager1->GetSiteInstanceForURL(ext1_url1); | |
125 scoped_refptr<SiteInstance> site12 = | |
126 manager1->GetSiteInstanceForURL(ext1_url2); | |
127 EXPECT_EQ(site11, site12); | |
128 | |
129 scoped_refptr<SiteInstance> site21 = | |
130 manager1->GetSiteInstanceForURL(ext2_url1); | |
131 EXPECT_NE(site11, site21); | |
132 | |
133 scoped_refptr<SiteInstance> other_profile_site = | |
134 manager2->GetSiteInstanceForURL(ext1_url1); | |
135 EXPECT_NE(site11, other_profile_site); | |
136 } | |
OLD | NEW |