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.
5 #include "chrome/browser/extensions/event_router_forwarder.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/power_monitor/power_monitor.h"
10 #include "base/power_monitor/power_monitor_device_source.h"
11 #include "base/test/thread_test_helper.h"
12 #include "chrome/browser/profiles/profile_manager.h"
13 #include "chrome/test/base/testing_browser_process.h"
14 #include "chrome/test/base/testing_profile.h"
15 #include "chrome/test/base/testing_profile_manager.h"
16 #include "content/public/test/test_browser_thread.h"
17 #include "content/public/test/test_browser_thread_bundle.h"
18 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h"
22 using content::BrowserThread
;
24 namespace extensions
{
28 const events::HistogramValue kHistogramValue
= events::FOR_TEST
;
29 const char kEventName
[] = "event_name";
30 const char kExt
[] = "extension";
32 class MockEventRouterForwarder
: public EventRouterForwarder
{
34 MOCK_METHOD6(CallEventRouter
,
37 events::HistogramValue
,
42 virtual void CallEventRouter(Profile
* profile
,
43 const std::string
& extension_id
,
44 events::HistogramValue histogram_value
,
45 const std::string
& event_name
,
46 scoped_ptr
<base::ListValue
> event_args
,
47 Profile
* restrict_to_profile
,
48 const GURL
& event_url
) {
49 CallEventRouter(profile
, extension_id
, histogram_value
, event_name
,
50 restrict_to_profile
, event_url
);
54 virtual ~MockEventRouterForwarder() {}
57 static void BroadcastEventToRenderers(EventRouterForwarder
* event_router
,
58 events::HistogramValue histogram_value
,
59 const std::string
& event_name
,
61 scoped_ptr
<base::ListValue
> args(new base::ListValue());
62 event_router
->BroadcastEventToRenderers(histogram_value
, event_name
,
66 static void DispatchEventToRenderers(EventRouterForwarder
* event_router
,
67 events::HistogramValue histogram_value
,
68 const std::string
& event_name
,
70 bool use_profile_to_restrict_events
,
72 scoped_ptr
<base::ListValue
> args(new base::ListValue());
73 event_router
->DispatchEventToRenderers(histogram_value
, event_name
,
75 use_profile_to_restrict_events
, url
);
78 static void BroadcastEventToExtension(EventRouterForwarder
* event_router
,
79 const std::string
& extension
,
80 events::HistogramValue histogram_value
,
81 const std::string
& event_name
,
83 scoped_ptr
<base::ListValue
> args(new base::ListValue());
84 event_router
->BroadcastEventToExtension(extension
, histogram_value
,
85 event_name
, args
.Pass(), url
);
88 static void DispatchEventToExtension(EventRouterForwarder
* event_router
,
89 const std::string
& extension
,
90 events::HistogramValue histogram_value
,
91 const std::string
& event_name
,
93 bool use_profile_to_restrict_events
,
95 scoped_ptr
<base::ListValue
> args(new base::ListValue());
96 event_router
->DispatchEventToExtension(extension
, histogram_value
, event_name
,
98 use_profile_to_restrict_events
, url
);
103 class EventRouterForwarderTest
: public testing::Test
{
105 EventRouterForwarderTest()
106 : thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD
),
108 TestingBrowserProcess::GetGlobal()) {
109 #if defined(OS_MACOSX)
110 base::PowerMonitorDeviceSource::AllocateSystemIOPorts();
112 scoped_ptr
<base::PowerMonitorSource
> power_monitor_source(
113 new base::PowerMonitorDeviceSource());
114 dummy
.reset(new base::PowerMonitor(power_monitor_source
.Pass()));
117 void SetUp() override
{
118 ASSERT_TRUE(profile_manager_
.SetUp());
120 // Inject a BrowserProcess with a ProfileManager.
121 profile1_
= profile_manager_
.CreateTestingProfile("one");
122 profile2_
= profile_manager_
.CreateTestingProfile("two");
125 content::TestBrowserThreadBundle thread_bundle_
;
126 TestingProfileManager profile_manager_
;
127 scoped_ptr
<base::PowerMonitor
> dummy
;
128 // Profiles are weak pointers, owned by ProfileManager in |browser_process_|.
129 TestingProfile
* profile1_
;
130 TestingProfile
* profile2_
;
133 TEST_F(EventRouterForwarderTest
, BroadcastRendererUI
) {
134 scoped_refptr
<MockEventRouterForwarder
> event_router(
135 new MockEventRouterForwarder
);
137 EXPECT_CALL(*event_router
.get(),
138 CallEventRouter(profile1_
, "", kHistogramValue
, kEventName
,
140 EXPECT_CALL(*event_router
.get(),
141 CallEventRouter(profile2_
, "", kHistogramValue
, kEventName
,
143 BroadcastEventToRenderers(event_router
.get(), kHistogramValue
, kEventName
,
147 TEST_F(EventRouterForwarderTest
, BroadcastRendererUIIncognito
) {
148 scoped_refptr
<MockEventRouterForwarder
> event_router(
149 new MockEventRouterForwarder
);
152 Profile
* incognito
= profile1_
->GetOffTheRecordProfile();
153 EXPECT_CALL(*event_router
.get(),
154 CallEventRouter(profile1_
, "", kHistogramValue
, kEventName
,
156 EXPECT_CALL(*event_router
.get(), CallEventRouter(incognito
, _
, _
, _
, _
, _
))
158 EXPECT_CALL(*event_router
.get(),
159 CallEventRouter(profile2_
, "", kHistogramValue
, kEventName
,
161 BroadcastEventToRenderers(event_router
.get(), kHistogramValue
, kEventName
,
165 // This is the canonical test for passing control flow from the IO thread
166 // to the UI thread. Repeating this for all public functions of
167 // EventRouterForwarder would not increase coverage.
168 TEST_F(EventRouterForwarderTest
, BroadcastRendererIO
) {
169 scoped_refptr
<MockEventRouterForwarder
> event_router(
170 new MockEventRouterForwarder
);
172 EXPECT_CALL(*event_router
.get(),
173 CallEventRouter(profile1_
, "", kHistogramValue
, kEventName
,
175 EXPECT_CALL(*event_router
.get(),
176 CallEventRouter(profile2_
, "", kHistogramValue
, kEventName
,
178 BrowserThread::PostTask(BrowserThread::IO
, FROM_HERE
,
179 base::Bind(&BroadcastEventToRenderers
,
180 base::Unretained(event_router
.get()),
181 kHistogramValue
, kEventName
, url
));
183 // Wait for IO thread's message loop to be processed
184 scoped_refptr
<base::ThreadTestHelper
> helper(new base::ThreadTestHelper(
185 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
).get()));
186 ASSERT_TRUE(helper
->Run());
188 base::MessageLoop::current()->RunUntilIdle();
191 TEST_F(EventRouterForwarderTest
, UnicastRendererUIRestricted
) {
192 scoped_refptr
<MockEventRouterForwarder
> event_router(
193 new MockEventRouterForwarder
);
196 EXPECT_CALL(*event_router
.get(),
197 CallEventRouter(profile1_
, "", kHistogramValue
, kEventName
,
199 EXPECT_CALL(*event_router
.get(), CallEventRouter(profile2_
, _
, _
, _
, _
, _
))
201 DispatchEventToRenderers(event_router
.get(), kHistogramValue
, kEventName
,
202 profile1_
, true, url
);
205 TEST_F(EventRouterForwarderTest
, UnicastRendererUIRestrictedIncognito1
) {
206 scoped_refptr
<MockEventRouterForwarder
> event_router(
207 new MockEventRouterForwarder
);
208 Profile
* incognito
= profile1_
->GetOffTheRecordProfile();
211 EXPECT_CALL(*event_router
.get(),
212 CallEventRouter(profile1_
, "", kHistogramValue
, kEventName
,
214 EXPECT_CALL(*event_router
.get(), CallEventRouter(incognito
, _
, _
, _
, _
, _
))
216 EXPECT_CALL(*event_router
.get(), CallEventRouter(profile2_
, _
, _
, _
, _
, _
))
218 DispatchEventToRenderers(event_router
.get(), kHistogramValue
, kEventName
,
219 profile1_
, true, url
);
222 TEST_F(EventRouterForwarderTest
, UnicastRendererUIRestrictedIncognito2
) {
223 scoped_refptr
<MockEventRouterForwarder
> event_router(
224 new MockEventRouterForwarder
);
225 Profile
* incognito
= profile1_
->GetOffTheRecordProfile();
228 EXPECT_CALL(*event_router
.get(), CallEventRouter(profile1_
, _
, _
, _
, _
, _
))
230 EXPECT_CALL(*event_router
.get(),
231 CallEventRouter(incognito
, "", kHistogramValue
, kEventName
,
233 EXPECT_CALL(*event_router
.get(), CallEventRouter(profile2_
, _
, _
, _
, _
, _
))
235 DispatchEventToRenderers(event_router
.get(), kHistogramValue
, kEventName
,
236 incognito
, true, url
);
239 TEST_F(EventRouterForwarderTest
, UnicastRendererUIUnrestricted
) {
240 scoped_refptr
<MockEventRouterForwarder
> event_router(
241 new MockEventRouterForwarder
);
246 CallEventRouter(profile1_
, "", kHistogramValue
, kEventName
, NULL
, url
));
247 EXPECT_CALL(*event_router
.get(), CallEventRouter(profile2_
, _
, _
, _
, _
, _
))
249 DispatchEventToRenderers(event_router
.get(), kHistogramValue
, kEventName
,
250 profile1_
, false, url
);
253 TEST_F(EventRouterForwarderTest
, UnicastRendererUIUnrestrictedIncognito
) {
254 scoped_refptr
<MockEventRouterForwarder
> event_router(
255 new MockEventRouterForwarder
);
256 Profile
* incognito
= profile1_
->GetOffTheRecordProfile();
261 CallEventRouter(profile1_
, "", kHistogramValue
, kEventName
, NULL
, url
));
262 EXPECT_CALL(*event_router
.get(), CallEventRouter(incognito
, _
, _
, _
, _
, _
))
264 EXPECT_CALL(*event_router
.get(), CallEventRouter(profile2_
, _
, _
, _
, _
, _
))
266 DispatchEventToRenderers(event_router
.get(), kHistogramValue
, kEventName
,
267 profile1_
, false, url
);
270 TEST_F(EventRouterForwarderTest
, BroadcastExtensionUI
) {
271 scoped_refptr
<MockEventRouterForwarder
> event_router(
272 new MockEventRouterForwarder
);
274 EXPECT_CALL(*event_router
.get(),
275 CallEventRouter(profile1_
, kExt
, kHistogramValue
, kEventName
,
277 EXPECT_CALL(*event_router
.get(),
278 CallEventRouter(profile2_
, kExt
, kHistogramValue
, kEventName
,
280 BroadcastEventToExtension(event_router
.get(), kExt
, kHistogramValue
,
284 TEST_F(EventRouterForwarderTest
, UnicastExtensionUIRestricted
) {
285 scoped_refptr
<MockEventRouterForwarder
> event_router(
286 new MockEventRouterForwarder
);
289 EXPECT_CALL(*event_router
.get(),
290 CallEventRouter(profile1_
, kExt
, kHistogramValue
, kEventName
,
292 EXPECT_CALL(*event_router
.get(), CallEventRouter(profile2_
, _
, _
, _
, _
, _
))
294 DispatchEventToExtension(event_router
.get(), kExt
, kHistogramValue
,
295 kEventName
, profile1_
, true, url
);
298 TEST_F(EventRouterForwarderTest
, UnicastExtensionUIUnrestricted
) {
299 scoped_refptr
<MockEventRouterForwarder
> event_router(
300 new MockEventRouterForwarder
);
305 CallEventRouter(profile1_
, kExt
, kHistogramValue
, kEventName
, NULL
, url
));
306 EXPECT_CALL(*event_router
.get(), CallEventRouter(profile2_
, _
, _
, _
, _
, _
))
308 DispatchEventToExtension(event_router
.get(), kExt
, kHistogramValue
,
309 kEventName
, profile1_
, false, url
);
312 } // namespace extensions