Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / event_router_forwarder_unittest.cc
blobe1710184032c004cef161148bd0051b808ee99d5
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"
7 #include "base/bind.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"
20 #include "url/gurl.h"
22 using content::BrowserThread;
24 namespace extensions {
26 namespace {
28 const events::HistogramValue kHistogramValue = events::FOR_TEST;
29 const char kEventName[] = "event_name";
30 const char kExt[] = "extension";
32 class MockEventRouterForwarder : public EventRouterForwarder {
33 public:
34 MOCK_METHOD6(CallEventRouter,
35 void(Profile*,
36 const std::string&,
37 events::HistogramValue,
38 const std::string&,
39 Profile*,
40 const GURL&));
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);
53 protected:
54 virtual ~MockEventRouterForwarder() {}
57 static void BroadcastEventToRenderers(EventRouterForwarder* event_router,
58 events::HistogramValue histogram_value,
59 const std::string& event_name,
60 const GURL& url) {
61 scoped_ptr<base::ListValue> args(new base::ListValue());
62 event_router->BroadcastEventToRenderers(histogram_value, event_name,
63 args.Pass(), url);
66 static void DispatchEventToRenderers(EventRouterForwarder* event_router,
67 events::HistogramValue histogram_value,
68 const std::string& event_name,
69 void* profile,
70 bool use_profile_to_restrict_events,
71 const GURL& url) {
72 scoped_ptr<base::ListValue> args(new base::ListValue());
73 event_router->DispatchEventToRenderers(histogram_value, event_name,
74 args.Pass(), profile,
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,
82 const GURL& url) {
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,
92 void* profile,
93 bool use_profile_to_restrict_events,
94 const GURL& url) {
95 scoped_ptr<base::ListValue> args(new base::ListValue());
96 event_router->DispatchEventToExtension(extension, histogram_value, event_name,
97 args.Pass(), profile,
98 use_profile_to_restrict_events, url);
101 } // namespace
103 class EventRouterForwarderTest : public testing::Test {
104 protected:
105 EventRouterForwarderTest()
106 : thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD),
107 profile_manager_(
108 TestingBrowserProcess::GetGlobal()) {
109 #if defined(OS_MACOSX)
110 base::PowerMonitorDeviceSource::AllocateSystemIOPorts();
111 #endif
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);
136 GURL url;
137 EXPECT_CALL(*event_router.get(),
138 CallEventRouter(profile1_, "", kHistogramValue, kEventName,
139 profile1_, url));
140 EXPECT_CALL(*event_router.get(),
141 CallEventRouter(profile2_, "", kHistogramValue, kEventName,
142 profile2_, url));
143 BroadcastEventToRenderers(event_router.get(), kHistogramValue, kEventName,
144 url);
147 TEST_F(EventRouterForwarderTest, BroadcastRendererUIIncognito) {
148 scoped_refptr<MockEventRouterForwarder> event_router(
149 new MockEventRouterForwarder);
150 using ::testing::_;
151 GURL url;
152 Profile* incognito = profile1_->GetOffTheRecordProfile();
153 EXPECT_CALL(*event_router.get(),
154 CallEventRouter(profile1_, "", kHistogramValue, kEventName,
155 profile1_, url));
156 EXPECT_CALL(*event_router.get(), CallEventRouter(incognito, _, _, _, _, _))
157 .Times(0);
158 EXPECT_CALL(*event_router.get(),
159 CallEventRouter(profile2_, "", kHistogramValue, kEventName,
160 profile2_, url));
161 BroadcastEventToRenderers(event_router.get(), kHistogramValue, kEventName,
162 url);
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);
171 GURL url;
172 EXPECT_CALL(*event_router.get(),
173 CallEventRouter(profile1_, "", kHistogramValue, kEventName,
174 profile1_, url));
175 EXPECT_CALL(*event_router.get(),
176 CallEventRouter(profile2_, "", kHistogramValue, kEventName,
177 profile2_, url));
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);
194 using ::testing::_;
195 GURL url;
196 EXPECT_CALL(*event_router.get(),
197 CallEventRouter(profile1_, "", kHistogramValue, kEventName,
198 profile1_, url));
199 EXPECT_CALL(*event_router.get(), CallEventRouter(profile2_, _, _, _, _, _))
200 .Times(0);
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();
209 using ::testing::_;
210 GURL url;
211 EXPECT_CALL(*event_router.get(),
212 CallEventRouter(profile1_, "", kHistogramValue, kEventName,
213 profile1_, url));
214 EXPECT_CALL(*event_router.get(), CallEventRouter(incognito, _, _, _, _, _))
215 .Times(0);
216 EXPECT_CALL(*event_router.get(), CallEventRouter(profile2_, _, _, _, _, _))
217 .Times(0);
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();
226 using ::testing::_;
227 GURL url;
228 EXPECT_CALL(*event_router.get(), CallEventRouter(profile1_, _, _, _, _, _))
229 .Times(0);
230 EXPECT_CALL(*event_router.get(),
231 CallEventRouter(incognito, "", kHistogramValue, kEventName,
232 incognito, url));
233 EXPECT_CALL(*event_router.get(), CallEventRouter(profile2_, _, _, _, _, _))
234 .Times(0);
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);
242 using ::testing::_;
243 GURL url;
244 EXPECT_CALL(
245 *event_router.get(),
246 CallEventRouter(profile1_, "", kHistogramValue, kEventName, NULL, url));
247 EXPECT_CALL(*event_router.get(), CallEventRouter(profile2_, _, _, _, _, _))
248 .Times(0);
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();
257 using ::testing::_;
258 GURL url;
259 EXPECT_CALL(
260 *event_router.get(),
261 CallEventRouter(profile1_, "", kHistogramValue, kEventName, NULL, url));
262 EXPECT_CALL(*event_router.get(), CallEventRouter(incognito, _, _, _, _, _))
263 .Times(0);
264 EXPECT_CALL(*event_router.get(), CallEventRouter(profile2_, _, _, _, _, _))
265 .Times(0);
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);
273 GURL url;
274 EXPECT_CALL(*event_router.get(),
275 CallEventRouter(profile1_, kExt, kHistogramValue, kEventName,
276 profile1_, url));
277 EXPECT_CALL(*event_router.get(),
278 CallEventRouter(profile2_, kExt, kHistogramValue, kEventName,
279 profile2_, url));
280 BroadcastEventToExtension(event_router.get(), kExt, kHistogramValue,
281 kEventName, url);
284 TEST_F(EventRouterForwarderTest, UnicastExtensionUIRestricted) {
285 scoped_refptr<MockEventRouterForwarder> event_router(
286 new MockEventRouterForwarder);
287 using ::testing::_;
288 GURL url;
289 EXPECT_CALL(*event_router.get(),
290 CallEventRouter(profile1_, kExt, kHistogramValue, kEventName,
291 profile1_, url));
292 EXPECT_CALL(*event_router.get(), CallEventRouter(profile2_, _, _, _, _, _))
293 .Times(0);
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);
301 using ::testing::_;
302 GURL url;
303 EXPECT_CALL(
304 *event_router.get(),
305 CallEventRouter(profile1_, kExt, kHistogramValue, kEventName, NULL, url));
306 EXPECT_CALL(*event_router.get(), CallEventRouter(profile2_, _, _, _, _, _))
307 .Times(0);
308 DispatchEventToExtension(event_router.get(), kExt, kHistogramValue,
309 kEventName, profile1_, false, url);
312 } // namespace extensions