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 char kEventName
[] = "event_name";
29 const char kExt
[] = "extension";
31 class MockEventRouterForwarder
: public EventRouterForwarder
{
33 MOCK_METHOD5(CallEventRouter
,
34 void(Profile
*, const std::string
&, const std::string
&, Profile
*,
37 virtual void CallEventRouter(
38 Profile
* profile
, const std::string
& extension_id
,
39 const std::string
& event_name
, scoped_ptr
<base::ListValue
> event_args
,
40 Profile
* restrict_to_profile
, const GURL
& event_url
) {
41 CallEventRouter(profile
, extension_id
, event_name
,
42 restrict_to_profile
, event_url
);
46 virtual ~MockEventRouterForwarder() {}
49 static void BroadcastEventToRenderers(EventRouterForwarder
* event_router
,
50 const std::string
& event_name
,
52 scoped_ptr
<base::ListValue
> args(new base::ListValue());
53 event_router
->BroadcastEventToRenderers(event_name
, args
.Pass(), url
);
56 static void DispatchEventToRenderers(EventRouterForwarder
* event_router
,
57 const std::string
& event_name
,
59 bool use_profile_to_restrict_events
,
61 scoped_ptr
<base::ListValue
> args(new base::ListValue());
62 event_router
->DispatchEventToRenderers(event_name
, args
.Pass(), profile
,
63 use_profile_to_restrict_events
, url
);
66 static void BroadcastEventToExtension(EventRouterForwarder
* event_router
,
67 const std::string
& extension
,
68 const std::string
& event_name
,
70 scoped_ptr
<base::ListValue
> args(new base::ListValue());
71 event_router
->BroadcastEventToExtension(extension
, event_name
, args
.Pass(),
75 static void DispatchEventToExtension(EventRouterForwarder
* event_router
,
76 const std::string
& extension
,
77 const std::string
& event_name
,
79 bool use_profile_to_restrict_events
,
81 scoped_ptr
<base::ListValue
> args(new base::ListValue());
82 event_router
->DispatchEventToExtension(
83 extension
, event_name
, args
.Pass(), profile
,
84 use_profile_to_restrict_events
, url
);
89 class EventRouterForwarderTest
: public testing::Test
{
91 EventRouterForwarderTest()
92 : thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD
),
94 TestingBrowserProcess::GetGlobal()) {
95 #if defined(OS_MACOSX)
96 base::PowerMonitorDeviceSource::AllocateSystemIOPorts();
98 scoped_ptr
<base::PowerMonitorSource
> power_monitor_source(
99 new base::PowerMonitorDeviceSource());
100 dummy
.reset(new base::PowerMonitor(power_monitor_source
.Pass()));
103 void SetUp() override
{
104 ASSERT_TRUE(profile_manager_
.SetUp());
106 // Inject a BrowserProcess with a ProfileManager.
107 profile1_
= profile_manager_
.CreateTestingProfile("one");
108 profile2_
= profile_manager_
.CreateTestingProfile("two");
111 content::TestBrowserThreadBundle thread_bundle_
;
112 TestingProfileManager profile_manager_
;
113 scoped_ptr
<base::PowerMonitor
> dummy
;
114 // Profiles are weak pointers, owned by ProfileManager in |browser_process_|.
115 TestingProfile
* profile1_
;
116 TestingProfile
* profile2_
;
119 TEST_F(EventRouterForwarderTest
, BroadcastRendererUI
) {
120 scoped_refptr
<MockEventRouterForwarder
> event_router(
121 new MockEventRouterForwarder
);
123 EXPECT_CALL(*event_router
.get(),
124 CallEventRouter(profile1_
, "", kEventName
, profile1_
, url
));
125 EXPECT_CALL(*event_router
.get(),
126 CallEventRouter(profile2_
, "", kEventName
, profile2_
, url
));
127 BroadcastEventToRenderers(event_router
.get(), kEventName
, url
);
130 TEST_F(EventRouterForwarderTest
, BroadcastRendererUIIncognito
) {
131 scoped_refptr
<MockEventRouterForwarder
> event_router(
132 new MockEventRouterForwarder
);
135 Profile
* incognito
= profile1_
->GetOffTheRecordProfile();
136 EXPECT_CALL(*event_router
.get(),
137 CallEventRouter(profile1_
, "", kEventName
, profile1_
, url
));
138 EXPECT_CALL(*event_router
.get(), CallEventRouter(incognito
, _
, _
, _
, _
))
140 EXPECT_CALL(*event_router
.get(),
141 CallEventRouter(profile2_
, "", kEventName
, profile2_
, url
));
142 BroadcastEventToRenderers(event_router
.get(), kEventName
, url
);
145 // This is the canonical test for passing control flow from the IO thread
146 // to the UI thread. Repeating this for all public functions of
147 // EventRouterForwarder would not increase coverage.
148 TEST_F(EventRouterForwarderTest
, BroadcastRendererIO
) {
149 scoped_refptr
<MockEventRouterForwarder
> event_router(
150 new MockEventRouterForwarder
);
152 EXPECT_CALL(*event_router
.get(),
153 CallEventRouter(profile1_
, "", kEventName
, profile1_
, url
));
154 EXPECT_CALL(*event_router
.get(),
155 CallEventRouter(profile2_
, "", kEventName
, profile2_
, url
));
156 BrowserThread::PostTask(BrowserThread::IO
, FROM_HERE
,
158 &BroadcastEventToRenderers
, base::Unretained(event_router
.get()),
161 // Wait for IO thread's message loop to be processed
162 scoped_refptr
<base::ThreadTestHelper
> helper(new base::ThreadTestHelper(
163 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
).get()));
164 ASSERT_TRUE(helper
->Run());
166 base::MessageLoop::current()->RunUntilIdle();
169 TEST_F(EventRouterForwarderTest
, UnicastRendererUIRestricted
) {
170 scoped_refptr
<MockEventRouterForwarder
> event_router(
171 new MockEventRouterForwarder
);
174 EXPECT_CALL(*event_router
.get(),
175 CallEventRouter(profile1_
, "", kEventName
, profile1_
, url
));
176 EXPECT_CALL(*event_router
.get(), CallEventRouter(profile2_
, _
, _
, _
, _
))
178 DispatchEventToRenderers(event_router
.get(), kEventName
, profile1_
, true,
182 TEST_F(EventRouterForwarderTest
, UnicastRendererUIRestrictedIncognito1
) {
183 scoped_refptr
<MockEventRouterForwarder
> event_router(
184 new MockEventRouterForwarder
);
185 Profile
* incognito
= profile1_
->GetOffTheRecordProfile();
188 EXPECT_CALL(*event_router
.get(),
189 CallEventRouter(profile1_
, "", kEventName
, profile1_
, url
));
190 EXPECT_CALL(*event_router
.get(), CallEventRouter(incognito
, _
, _
, _
, _
))
192 EXPECT_CALL(*event_router
.get(), CallEventRouter(profile2_
, _
, _
, _
, _
))
194 DispatchEventToRenderers(event_router
.get(), kEventName
, profile1_
, true,
198 TEST_F(EventRouterForwarderTest
, UnicastRendererUIRestrictedIncognito2
) {
199 scoped_refptr
<MockEventRouterForwarder
> event_router(
200 new MockEventRouterForwarder
);
201 Profile
* incognito
= profile1_
->GetOffTheRecordProfile();
204 EXPECT_CALL(*event_router
.get(), CallEventRouter(profile1_
, _
, _
, _
, _
))
206 EXPECT_CALL(*event_router
.get(),
207 CallEventRouter(incognito
, "", kEventName
, incognito
, url
));
208 EXPECT_CALL(*event_router
.get(), CallEventRouter(profile2_
, _
, _
, _
, _
))
210 DispatchEventToRenderers(event_router
.get(), kEventName
, incognito
, true,
214 TEST_F(EventRouterForwarderTest
, UnicastRendererUIUnrestricted
) {
215 scoped_refptr
<MockEventRouterForwarder
> event_router(
216 new MockEventRouterForwarder
);
219 EXPECT_CALL(*event_router
.get(),
220 CallEventRouter(profile1_
, "", kEventName
, NULL
, url
));
221 EXPECT_CALL(*event_router
.get(), CallEventRouter(profile2_
, _
, _
, _
, _
))
223 DispatchEventToRenderers(event_router
.get(), kEventName
, profile1_
, false,
227 TEST_F(EventRouterForwarderTest
, UnicastRendererUIUnrestrictedIncognito
) {
228 scoped_refptr
<MockEventRouterForwarder
> event_router(
229 new MockEventRouterForwarder
);
230 Profile
* incognito
= profile1_
->GetOffTheRecordProfile();
233 EXPECT_CALL(*event_router
.get(),
234 CallEventRouter(profile1_
, "", kEventName
, NULL
, url
));
235 EXPECT_CALL(*event_router
.get(), CallEventRouter(incognito
, _
, _
, _
, _
))
237 EXPECT_CALL(*event_router
.get(), CallEventRouter(profile2_
, _
, _
, _
, _
))
239 DispatchEventToRenderers(event_router
.get(), kEventName
, profile1_
, false,
243 TEST_F(EventRouterForwarderTest
, BroadcastExtensionUI
) {
244 scoped_refptr
<MockEventRouterForwarder
> event_router(
245 new MockEventRouterForwarder
);
247 EXPECT_CALL(*event_router
.get(),
248 CallEventRouter(profile1_
, kExt
, kEventName
, profile1_
, url
));
249 EXPECT_CALL(*event_router
.get(),
250 CallEventRouter(profile2_
, kExt
, kEventName
, profile2_
, url
));
251 BroadcastEventToExtension(event_router
.get(), kExt
, kEventName
, url
);
254 TEST_F(EventRouterForwarderTest
, UnicastExtensionUIRestricted
) {
255 scoped_refptr
<MockEventRouterForwarder
> event_router(
256 new MockEventRouterForwarder
);
259 EXPECT_CALL(*event_router
.get(),
260 CallEventRouter(profile1_
, kExt
, kEventName
, profile1_
, url
));
261 EXPECT_CALL(*event_router
.get(), CallEventRouter(profile2_
, _
, _
, _
, _
))
263 DispatchEventToExtension(event_router
.get(), kExt
, kEventName
, profile1_
,
267 TEST_F(EventRouterForwarderTest
, UnicastExtensionUIUnrestricted
) {
268 scoped_refptr
<MockEventRouterForwarder
> event_router(
269 new MockEventRouterForwarder
);
272 EXPECT_CALL(*event_router
.get(),
273 CallEventRouter(profile1_
, kExt
, kEventName
, NULL
, url
));
274 EXPECT_CALL(*event_router
.get(), CallEventRouter(profile2_
, _
, _
, _
, _
))
276 DispatchEventToExtension(event_router
.get(), kExt
, kEventName
, profile1_
,
280 } // namespace extensions