OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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_event_router_forwarder.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/message_loop.h" | |
9 #include "base/system_monitor/system_monitor.h" | |
10 #include "base/test/thread_test_helper.h" | |
11 #include "chrome/browser/profiles/profile_manager.h" | |
12 #include "chrome/test/base/testing_browser_process.h" | |
13 #include "chrome/test/base/testing_profile.h" | |
14 #include "chrome/test/base/testing_profile_manager.h" | |
15 #include "content/public/test/test_browser_thread.h" | |
16 #include "googleurl/src/gurl.h" | |
17 #include "testing/gmock/include/gmock/gmock.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | |
19 | |
20 using content::BrowserThread; | |
21 | |
22 namespace { | |
23 | |
24 const char kEventName[] = "event_name"; | |
25 const char kEventArgs[] = "event_args"; | |
26 const char kExt[] = "extension"; | |
27 | |
28 class MockExtensionEventRouterForwarder : public ExtensionEventRouterForwarder { | |
29 public: | |
30 MOCK_METHOD6(CallExtensionEventRouter, | |
31 void(Profile*, const std::string&, const std::string&, const std::string&, | |
32 Profile*, const GURL&)); | |
33 | |
34 protected: | |
35 virtual ~MockExtensionEventRouterForwarder() {} | |
36 }; | |
37 | |
38 } // namespace | |
39 | |
40 class ExtensionEventRouterForwarderTest : public testing::Test { | |
41 protected: | |
42 ExtensionEventRouterForwarderTest() | |
43 : ui_thread_(BrowserThread::UI, &message_loop_), | |
44 io_thread_(BrowserThread::IO), | |
45 profile_manager_( | |
46 static_cast<TestingBrowserProcess*>(g_browser_process)) { | |
47 #if defined(OS_MACOSX) | |
48 base::SystemMonitor::AllocateSystemIOPorts(); | |
49 #endif | |
50 dummy.reset(new base::SystemMonitor); | |
51 } | |
52 | |
53 virtual void SetUp() { | |
54 ASSERT_TRUE(profile_manager_.SetUp()); | |
55 | |
56 // Inject a BrowserProcess with a ProfileManager. | |
57 ASSERT_TRUE(io_thread_.Start()); | |
58 | |
59 profile1_ = profile_manager_.CreateTestingProfile("one"); | |
60 profile2_ = profile_manager_.CreateTestingProfile("two"); | |
61 } | |
62 | |
63 TestingProfile* CreateIncognitoProfile(TestingProfile* base) { | |
64 TestingProfile* incognito = new TestingProfile; // Owned by |base|. | |
65 incognito->set_incognito(true); | |
66 base->SetOffTheRecordProfile(incognito); | |
67 return incognito; | |
68 } | |
69 | |
70 MessageLoopForUI message_loop_; | |
71 content::TestBrowserThread ui_thread_; | |
72 content::TestBrowserThread io_thread_; | |
73 TestingProfileManager profile_manager_; | |
74 scoped_ptr<base::SystemMonitor> dummy; | |
75 // Profiles are weak pointers, owned by ProfileManager in |browser_process_|. | |
76 TestingProfile* profile1_; | |
77 TestingProfile* profile2_; | |
78 }; | |
79 | |
80 TEST_F(ExtensionEventRouterForwarderTest, BroadcastRendererUI) { | |
81 scoped_refptr<MockExtensionEventRouterForwarder> event_router( | |
82 new MockExtensionEventRouterForwarder); | |
83 GURL url; | |
84 EXPECT_CALL(*event_router, | |
85 CallExtensionEventRouter( | |
86 profile1_, "", kEventName, kEventArgs, profile1_, url)); | |
87 EXPECT_CALL(*event_router, | |
88 CallExtensionEventRouter( | |
89 profile2_, "", kEventName, kEventArgs, profile2_, url)); | |
90 event_router->BroadcastEventToRenderers(kEventName, kEventArgs, url); | |
91 } | |
92 | |
93 TEST_F(ExtensionEventRouterForwarderTest, BroadcastRendererUIIncognito) { | |
94 scoped_refptr<MockExtensionEventRouterForwarder> event_router( | |
95 new MockExtensionEventRouterForwarder); | |
96 using ::testing::_; | |
97 GURL url; | |
98 Profile* incognito = CreateIncognitoProfile(profile1_); | |
99 EXPECT_CALL(*event_router, | |
100 CallExtensionEventRouter( | |
101 profile1_, "", kEventName, kEventArgs, profile1_, url)); | |
102 EXPECT_CALL(*event_router, | |
103 CallExtensionEventRouter(incognito, _, _, _, _, _)).Times(0); | |
104 EXPECT_CALL(*event_router, | |
105 CallExtensionEventRouter( | |
106 profile2_, "", kEventName, kEventArgs, profile2_, url)); | |
107 event_router->BroadcastEventToRenderers(kEventName, kEventArgs, url); | |
108 } | |
109 | |
110 // This is the canonical test for passing control flow from the IO thread | |
111 // to the UI thread. Repeating this for all public functions of | |
112 // ExtensionEventRouterForwarder would not increase coverage. | |
113 TEST_F(ExtensionEventRouterForwarderTest, BroadcastRendererIO) { | |
114 scoped_refptr<MockExtensionEventRouterForwarder> event_router( | |
115 new MockExtensionEventRouterForwarder); | |
116 GURL url; | |
117 EXPECT_CALL(*event_router, | |
118 CallExtensionEventRouter( | |
119 profile1_, "", kEventName, kEventArgs, profile1_, url)); | |
120 EXPECT_CALL(*event_router, | |
121 CallExtensionEventRouter( | |
122 profile2_, "", kEventName, kEventArgs, profile2_, url)); | |
123 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
124 base::Bind( | |
125 &MockExtensionEventRouterForwarder::BroadcastEventToRenderers, | |
126 event_router.get(), | |
127 std::string(kEventName), std::string(kEventArgs), url)); | |
128 | |
129 // Wait for IO thread's message loop to be processed | |
130 scoped_refptr<base::ThreadTestHelper> helper( | |
131 new base::ThreadTestHelper( | |
132 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO))); | |
133 ASSERT_TRUE(helper->Run()); | |
134 | |
135 MessageLoop::current()->RunAllPending(); | |
136 } | |
137 | |
138 TEST_F(ExtensionEventRouterForwarderTest, UnicastRendererUIRestricted) { | |
139 scoped_refptr<MockExtensionEventRouterForwarder> event_router( | |
140 new MockExtensionEventRouterForwarder); | |
141 using ::testing::_; | |
142 GURL url; | |
143 EXPECT_CALL(*event_router, | |
144 CallExtensionEventRouter( | |
145 profile1_, "", kEventName, kEventArgs, profile1_, url)); | |
146 EXPECT_CALL(*event_router, | |
147 CallExtensionEventRouter(profile2_, _, _, _, _, _)).Times(0); | |
148 event_router->DispatchEventToRenderers(kEventName, kEventArgs, | |
149 profile1_, true, url); | |
150 } | |
151 | |
152 TEST_F(ExtensionEventRouterForwarderTest, | |
153 UnicastRendererUIRestrictedIncognito1) { | |
154 scoped_refptr<MockExtensionEventRouterForwarder> event_router( | |
155 new MockExtensionEventRouterForwarder); | |
156 Profile* incognito = CreateIncognitoProfile(profile1_); | |
157 using ::testing::_; | |
158 GURL url; | |
159 EXPECT_CALL(*event_router, | |
160 CallExtensionEventRouter( | |
161 profile1_, "", kEventName, kEventArgs, profile1_, url)); | |
162 EXPECT_CALL(*event_router, | |
163 CallExtensionEventRouter(incognito, _, _, _, _, _)).Times(0); | |
164 EXPECT_CALL(*event_router, | |
165 CallExtensionEventRouter(profile2_, _, _, _, _, _)).Times(0); | |
166 event_router->DispatchEventToRenderers(kEventName, kEventArgs, | |
167 profile1_, true, url); | |
168 } | |
169 | |
170 TEST_F(ExtensionEventRouterForwarderTest, | |
171 UnicastRendererUIRestrictedIncognito2) { | |
172 scoped_refptr<MockExtensionEventRouterForwarder> event_router( | |
173 new MockExtensionEventRouterForwarder); | |
174 Profile* incognito = CreateIncognitoProfile(profile1_); | |
175 using ::testing::_; | |
176 GURL url; | |
177 EXPECT_CALL(*event_router, | |
178 CallExtensionEventRouter(profile1_, _, _, _, _, _)).Times(0); | |
179 EXPECT_CALL(*event_router, | |
180 CallExtensionEventRouter( | |
181 incognito, "", kEventName, kEventArgs, incognito, url)); | |
182 EXPECT_CALL(*event_router, | |
183 CallExtensionEventRouter(profile2_, _, _, _, _, _)).Times(0); | |
184 event_router->DispatchEventToRenderers(kEventName, kEventArgs, | |
185 incognito, true, url); | |
186 } | |
187 | |
188 TEST_F(ExtensionEventRouterForwarderTest, UnicastRendererUIUnrestricted) { | |
189 scoped_refptr<MockExtensionEventRouterForwarder> event_router( | |
190 new MockExtensionEventRouterForwarder); | |
191 using ::testing::_; | |
192 GURL url; | |
193 EXPECT_CALL(*event_router, | |
194 CallExtensionEventRouter( | |
195 profile1_, "", kEventName, kEventArgs, NULL, url)); | |
196 EXPECT_CALL(*event_router, | |
197 CallExtensionEventRouter(profile2_, _, _, _, _, _)).Times(0); | |
198 event_router->DispatchEventToRenderers(kEventName, kEventArgs, | |
199 profile1_, false, url); | |
200 } | |
201 | |
202 TEST_F(ExtensionEventRouterForwarderTest, | |
203 UnicastRendererUIUnrestrictedIncognito) { | |
204 scoped_refptr<MockExtensionEventRouterForwarder> event_router( | |
205 new MockExtensionEventRouterForwarder); | |
206 Profile* incognito = CreateIncognitoProfile(profile1_); | |
207 using ::testing::_; | |
208 GURL url; | |
209 EXPECT_CALL(*event_router, | |
210 CallExtensionEventRouter( | |
211 profile1_, "", kEventName, kEventArgs, NULL, url)); | |
212 EXPECT_CALL(*event_router, | |
213 CallExtensionEventRouter(incognito, _, _, _, _, _)).Times(0); | |
214 EXPECT_CALL(*event_router, | |
215 CallExtensionEventRouter(profile2_, _, _, _, _, _)).Times(0); | |
216 event_router->DispatchEventToRenderers(kEventName, kEventArgs, | |
217 profile1_, false, url); | |
218 } | |
219 | |
220 TEST_F(ExtensionEventRouterForwarderTest, BroadcastExtensionUI) { | |
221 scoped_refptr<MockExtensionEventRouterForwarder> event_router( | |
222 new MockExtensionEventRouterForwarder); | |
223 GURL url; | |
224 EXPECT_CALL(*event_router, | |
225 CallExtensionEventRouter( | |
226 profile1_, kExt, kEventName, kEventArgs, profile1_, url)); | |
227 EXPECT_CALL(*event_router, | |
228 CallExtensionEventRouter( | |
229 profile2_, kExt, kEventName, kEventArgs, profile2_, url)); | |
230 event_router->BroadcastEventToExtension(kExt, kEventName, kEventArgs, url); | |
231 } | |
232 | |
233 TEST_F(ExtensionEventRouterForwarderTest, UnicastExtensionUIRestricted) { | |
234 scoped_refptr<MockExtensionEventRouterForwarder> event_router( | |
235 new MockExtensionEventRouterForwarder); | |
236 using ::testing::_; | |
237 GURL url; | |
238 EXPECT_CALL(*event_router, | |
239 CallExtensionEventRouter( | |
240 profile1_, kExt, kEventName, kEventArgs, profile1_, url)); | |
241 EXPECT_CALL(*event_router, | |
242 CallExtensionEventRouter(profile2_, _, _, _, _, _)).Times(0); | |
243 event_router->DispatchEventToExtension(kExt, kEventName, kEventArgs, | |
244 profile1_, true, url); | |
245 } | |
246 | |
247 TEST_F(ExtensionEventRouterForwarderTest, UnicastExtensionUIUnrestricted) { | |
248 scoped_refptr<MockExtensionEventRouterForwarder> event_router( | |
249 new MockExtensionEventRouterForwarder); | |
250 using ::testing::_; | |
251 GURL url; | |
252 EXPECT_CALL(*event_router, | |
253 CallExtensionEventRouter( | |
254 profile1_, kExt, kEventName, kEventArgs, NULL, url)); | |
255 EXPECT_CALL(*event_router, | |
256 CallExtensionEventRouter(profile2_, _, _, _, _, _)).Times(0); | |
257 event_router->DispatchEventToExtension(kExt, kEventName, kEventArgs, | |
258 profile1_, false, url); | |
259 } | |
OLD | NEW |