Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(359)

Side by Side Diff: chrome/browser/extensions/event_router_forwarder_unittest.cc

Issue 10694085: Refactor extension event distribution to use Values instead of JSON strings. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixing memory leak in a test. Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/event_router_forwarder.h" 5 #include "chrome/browser/extensions/event_router_forwarder.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/message_loop.h" 8 #include "base/message_loop.h"
9 #include "base/system_monitor/system_monitor.h" 9 #include "base/system_monitor/system_monitor.h"
10 #include "base/test/thread_test_helper.h" 10 #include "base/test/thread_test_helper.h"
11 #include "chrome/browser/profiles/profile_manager.h" 11 #include "chrome/browser/profiles/profile_manager.h"
12 #include "chrome/test/base/testing_browser_process.h" 12 #include "chrome/test/base/testing_browser_process.h"
13 #include "chrome/test/base/testing_profile.h" 13 #include "chrome/test/base/testing_profile.h"
14 #include "chrome/test/base/testing_profile_manager.h" 14 #include "chrome/test/base/testing_profile_manager.h"
15 #include "content/public/test/test_browser_thread.h" 15 #include "content/public/test/test_browser_thread.h"
16 #include "googleurl/src/gurl.h" 16 #include "googleurl/src/gurl.h"
17 #include "testing/gmock/include/gmock/gmock.h" 17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
19 19
20 using content::BrowserThread; 20 using content::BrowserThread;
21 21
22 namespace extensions { 22 namespace extensions {
23 23
24 namespace { 24 namespace {
25 25
26 const char kEventName[] = "event_name"; 26 const char kEventName[] = "event_name";
27 const char kEventArgs[] = "event_args";
28 const char kExt[] = "extension"; 27 const char kExt[] = "extension";
29 28
30 class MockEventRouterForwarder : public EventRouterForwarder { 29 class MockEventRouterForwarder : public EventRouterForwarder {
31 public: 30 public:
32 MOCK_METHOD6(CallEventRouter, 31 MOCK_METHOD5(CallEventRouter,
33 void(Profile*, const std::string&, const std::string&, const std::string&, 32 void(Profile*, const std::string&, const std::string&, Profile*,
34 Profile*, const GURL&)); 33 const GURL&));
34
35 virtual void CallEventRouter(
36 Profile* profile, const std::string& extension_id,
37 const std::string& event_name, scoped_ptr<base::ListValue> event_args,
38 Profile* restrict_to_profile, const GURL& event_url) {
39 CallEventRouter(profile, extension_id, event_name,
40 restrict_to_profile, event_url);
41 }
35 42
36 protected: 43 protected:
37 virtual ~MockEventRouterForwarder() {} 44 virtual ~MockEventRouterForwarder() {}
38 }; 45 };
39 46
47 static void BroadcastEventToRenderers(EventRouterForwarder* event_router,
48 const std::string& event_name,
49 const GURL& url) {
50 scoped_ptr<ListValue> args(new ListValue());
51 event_router->BroadcastEventToRenderers(event_name, args.Pass(), url);
52 }
53
54 static void DispatchEventToRenderers(EventRouterForwarder* event_router,
55 const std::string& event_name,
56 void* profile,
57 bool use_profile_to_restrict_events,
58 const GURL& url) {
59 scoped_ptr<ListValue> args(new ListValue());
60 event_router->DispatchEventToRenderers(event_name, args.Pass(), profile,
61 use_profile_to_restrict_events, url);
62 }
63
64 static void BroadcastEventToExtension(EventRouterForwarder* event_router,
65 const std::string& extension,
66 const std::string& event_name,
67 const GURL& url) {
68 scoped_ptr<ListValue> args(new ListValue());
69 event_router->BroadcastEventToExtension(extension, event_name, args.Pass(),
70 url);
71 }
72
73 static void DispatchEventToExtension(EventRouterForwarder* event_router,
74 const std::string& extension,
75 const std::string& event_name,
76 void* profile,
77 bool use_profile_to_restrict_events,
78 const GURL& url) {
79 scoped_ptr<ListValue> args(new ListValue());
80 event_router->DispatchEventToExtension(
81 extension, event_name, args.Pass(), profile,
82 use_profile_to_restrict_events, url);
83 }
84
40 } // namespace 85 } // namespace
41 86
42 class EventRouterForwarderTest : public testing::Test { 87 class EventRouterForwarderTest : public testing::Test {
43 protected: 88 protected:
44 EventRouterForwarderTest() 89 EventRouterForwarderTest()
45 : ui_thread_(BrowserThread::UI, &message_loop_), 90 : ui_thread_(BrowserThread::UI, &message_loop_),
46 io_thread_(BrowserThread::IO), 91 io_thread_(BrowserThread::IO),
47 profile_manager_( 92 profile_manager_(
48 static_cast<TestingBrowserProcess*>(g_browser_process)) { 93 static_cast<TestingBrowserProcess*>(g_browser_process)) {
49 #if defined(OS_MACOSX) 94 #if defined(OS_MACOSX)
(...skipping 27 matching lines...) Expand all
77 // Profiles are weak pointers, owned by ProfileManager in |browser_process_|. 122 // Profiles are weak pointers, owned by ProfileManager in |browser_process_|.
78 TestingProfile* profile1_; 123 TestingProfile* profile1_;
79 TestingProfile* profile2_; 124 TestingProfile* profile2_;
80 }; 125 };
81 126
82 TEST_F(EventRouterForwarderTest, BroadcastRendererUI) { 127 TEST_F(EventRouterForwarderTest, BroadcastRendererUI) {
83 scoped_refptr<MockEventRouterForwarder> event_router( 128 scoped_refptr<MockEventRouterForwarder> event_router(
84 new MockEventRouterForwarder); 129 new MockEventRouterForwarder);
85 GURL url; 130 GURL url;
86 EXPECT_CALL(*event_router, 131 EXPECT_CALL(*event_router,
87 CallEventRouter( 132 CallEventRouter(profile1_, "", kEventName, profile1_, url));
88 profile1_, "", kEventName, kEventArgs, profile1_, url));
89 EXPECT_CALL(*event_router, 133 EXPECT_CALL(*event_router,
90 CallEventRouter( 134 CallEventRouter(profile2_, "", kEventName, profile2_, url));
91 profile2_, "", kEventName, kEventArgs, profile2_, url)); 135 BroadcastEventToRenderers(event_router.get(), kEventName, url);
92 event_router->BroadcastEventToRenderers(kEventName, kEventArgs, url);
93 } 136 }
94 137
95 TEST_F(EventRouterForwarderTest, BroadcastRendererUIIncognito) { 138 TEST_F(EventRouterForwarderTest, BroadcastRendererUIIncognito) {
96 scoped_refptr<MockEventRouterForwarder> event_router( 139 scoped_refptr<MockEventRouterForwarder> event_router(
97 new MockEventRouterForwarder); 140 new MockEventRouterForwarder);
98 using ::testing::_; 141 using ::testing::_;
99 GURL url; 142 GURL url;
100 Profile* incognito = CreateIncognitoProfile(profile1_); 143 Profile* incognito = CreateIncognitoProfile(profile1_);
101 EXPECT_CALL(*event_router, 144 EXPECT_CALL(*event_router,
102 CallEventRouter( 145 CallEventRouter(profile1_, "", kEventName, profile1_, url));
103 profile1_, "", kEventName, kEventArgs, profile1_, url));
104 EXPECT_CALL(*event_router, 146 EXPECT_CALL(*event_router,
105 CallEventRouter(incognito, _, _, _, _, _)).Times(0); 147 CallEventRouter(incognito, _, _, _, _)).Times(0);
106 EXPECT_CALL(*event_router, 148 EXPECT_CALL(*event_router,
107 CallEventRouter( 149 CallEventRouter(profile2_, "", kEventName, profile2_, url));
108 profile2_, "", kEventName, kEventArgs, profile2_, url)); 150 BroadcastEventToRenderers(event_router.get(), kEventName, url);
109 event_router->BroadcastEventToRenderers(kEventName, kEventArgs, url);
110 } 151 }
111 152
112 // This is the canonical test for passing control flow from the IO thread 153 // This is the canonical test for passing control flow from the IO thread
113 // to the UI thread. Repeating this for all public functions of 154 // to the UI thread. Repeating this for all public functions of
114 // EventRouterForwarder would not increase coverage. 155 // EventRouterForwarder would not increase coverage.
115 TEST_F(EventRouterForwarderTest, BroadcastRendererIO) { 156 TEST_F(EventRouterForwarderTest, BroadcastRendererIO) {
116 scoped_refptr<MockEventRouterForwarder> event_router( 157 scoped_refptr<MockEventRouterForwarder> event_router(
117 new MockEventRouterForwarder); 158 new MockEventRouterForwarder);
118 GURL url; 159 GURL url;
119 EXPECT_CALL(*event_router, 160 EXPECT_CALL(*event_router,
120 CallEventRouter( 161 CallEventRouter(profile1_, "", kEventName, profile1_, url));
121 profile1_, "", kEventName, kEventArgs, profile1_, url));
122 EXPECT_CALL(*event_router, 162 EXPECT_CALL(*event_router,
123 CallEventRouter( 163 CallEventRouter(profile2_, "", kEventName, profile2_, url));
124 profile2_, "", kEventName, kEventArgs, profile2_, url));
125 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, 164 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
126 base::Bind( 165 base::Bind(
127 &MockEventRouterForwarder::BroadcastEventToRenderers, 166 &BroadcastEventToRenderers, base::Unretained(event_router.get()),
128 event_router.get(), 167 kEventName, url));
129 std::string(kEventName), std::string(kEventArgs), url));
130 168
131 // Wait for IO thread's message loop to be processed 169 // Wait for IO thread's message loop to be processed
132 scoped_refptr<base::ThreadTestHelper> helper( 170 scoped_refptr<base::ThreadTestHelper> helper(
133 new base::ThreadTestHelper( 171 new base::ThreadTestHelper(
134 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO))); 172 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)));
135 ASSERT_TRUE(helper->Run()); 173 ASSERT_TRUE(helper->Run());
136 174
137 MessageLoop::current()->RunAllPending(); 175 MessageLoop::current()->RunAllPending();
138 } 176 }
139 177
140 TEST_F(EventRouterForwarderTest, UnicastRendererUIRestricted) { 178 TEST_F(EventRouterForwarderTest, UnicastRendererUIRestricted) {
141 scoped_refptr<MockEventRouterForwarder> event_router( 179 scoped_refptr<MockEventRouterForwarder> event_router(
142 new MockEventRouterForwarder); 180 new MockEventRouterForwarder);
143 using ::testing::_; 181 using ::testing::_;
144 GURL url; 182 GURL url;
145 EXPECT_CALL(*event_router, 183 EXPECT_CALL(*event_router,
146 CallEventRouter( 184 CallEventRouter(profile1_, "", kEventName, profile1_, url));
147 profile1_, "", kEventName, kEventArgs, profile1_, url));
148 EXPECT_CALL(*event_router, 185 EXPECT_CALL(*event_router,
149 CallEventRouter(profile2_, _, _, _, _, _)).Times(0); 186 CallEventRouter(profile2_, _, _, _, _)).Times(0);
150 event_router->DispatchEventToRenderers(kEventName, kEventArgs, 187 DispatchEventToRenderers(event_router.get(), kEventName, profile1_, true,
151 profile1_, true, url); 188 url);
152 } 189 }
153 190
154 TEST_F(EventRouterForwarderTest, UnicastRendererUIRestrictedIncognito1) { 191 TEST_F(EventRouterForwarderTest, UnicastRendererUIRestrictedIncognito1) {
155 scoped_refptr<MockEventRouterForwarder> event_router( 192 scoped_refptr<MockEventRouterForwarder> event_router(
156 new MockEventRouterForwarder); 193 new MockEventRouterForwarder);
157 Profile* incognito = CreateIncognitoProfile(profile1_); 194 Profile* incognito = CreateIncognitoProfile(profile1_);
158 using ::testing::_; 195 using ::testing::_;
159 GURL url; 196 GURL url;
160 EXPECT_CALL(*event_router, 197 EXPECT_CALL(*event_router,
161 CallEventRouter( 198 CallEventRouter(profile1_, "", kEventName, profile1_, url));
162 profile1_, "", kEventName, kEventArgs, profile1_, url));
163 EXPECT_CALL(*event_router, 199 EXPECT_CALL(*event_router,
164 CallEventRouter(incognito, _, _, _, _, _)).Times(0); 200 CallEventRouter(incognito, _, _, _, _)).Times(0);
165 EXPECT_CALL(*event_router, 201 EXPECT_CALL(*event_router,
166 CallEventRouter(profile2_, _, _, _, _, _)).Times(0); 202 CallEventRouter(profile2_, _, _, _, _)).Times(0);
167 event_router->DispatchEventToRenderers(kEventName, kEventArgs, 203 DispatchEventToRenderers(event_router.get(), kEventName, profile1_, true,
168 profile1_, true, url); 204 url);
169 } 205 }
170 206
171 TEST_F(EventRouterForwarderTest, UnicastRendererUIRestrictedIncognito2) { 207 TEST_F(EventRouterForwarderTest, UnicastRendererUIRestrictedIncognito2) {
172 scoped_refptr<MockEventRouterForwarder> event_router( 208 scoped_refptr<MockEventRouterForwarder> event_router(
173 new MockEventRouterForwarder); 209 new MockEventRouterForwarder);
174 Profile* incognito = CreateIncognitoProfile(profile1_); 210 Profile* incognito = CreateIncognitoProfile(profile1_);
175 using ::testing::_; 211 using ::testing::_;
176 GURL url; 212 GURL url;
177 EXPECT_CALL(*event_router, 213 EXPECT_CALL(*event_router,
178 CallEventRouter(profile1_, _, _, _, _, _)).Times(0); 214 CallEventRouter(profile1_, _, _, _, _)).Times(0);
179 EXPECT_CALL(*event_router, 215 EXPECT_CALL(*event_router,
180 CallEventRouter( 216 CallEventRouter(incognito, "", kEventName, incognito, url));
181 incognito, "", kEventName, kEventArgs, incognito, url));
182 EXPECT_CALL(*event_router, 217 EXPECT_CALL(*event_router,
183 CallEventRouter(profile2_, _, _, _, _, _)).Times(0); 218 CallEventRouter(profile2_, _, _, _, _)).Times(0);
184 event_router->DispatchEventToRenderers(kEventName, kEventArgs, 219 DispatchEventToRenderers(event_router.get(), kEventName, incognito, true,
185 incognito, true, url); 220 url);
186 } 221 }
187 222
188 TEST_F(EventRouterForwarderTest, UnicastRendererUIUnrestricted) { 223 TEST_F(EventRouterForwarderTest, UnicastRendererUIUnrestricted) {
189 scoped_refptr<MockEventRouterForwarder> event_router( 224 scoped_refptr<MockEventRouterForwarder> event_router(
190 new MockEventRouterForwarder); 225 new MockEventRouterForwarder);
191 using ::testing::_; 226 using ::testing::_;
192 GURL url; 227 GURL url;
193 EXPECT_CALL(*event_router, 228 EXPECT_CALL(*event_router,
194 CallEventRouter( 229 CallEventRouter(profile1_, "", kEventName, NULL, url));
195 profile1_, "", kEventName, kEventArgs, NULL, url));
196 EXPECT_CALL(*event_router, 230 EXPECT_CALL(*event_router,
197 CallEventRouter(profile2_, _, _, _, _, _)).Times(0); 231 CallEventRouter(profile2_, _, _, _, _)).Times(0);
198 event_router->DispatchEventToRenderers(kEventName, kEventArgs, 232 DispatchEventToRenderers(event_router.get(), kEventName, profile1_, false,
199 profile1_, false, url); 233 url);
200 } 234 }
201 235
202 TEST_F(EventRouterForwarderTest, UnicastRendererUIUnrestrictedIncognito) { 236 TEST_F(EventRouterForwarderTest, UnicastRendererUIUnrestrictedIncognito) {
203 scoped_refptr<MockEventRouterForwarder> event_router( 237 scoped_refptr<MockEventRouterForwarder> event_router(
204 new MockEventRouterForwarder); 238 new MockEventRouterForwarder);
205 Profile* incognito = CreateIncognitoProfile(profile1_); 239 Profile* incognito = CreateIncognitoProfile(profile1_);
206 using ::testing::_; 240 using ::testing::_;
207 GURL url; 241 GURL url;
208 EXPECT_CALL(*event_router, 242 EXPECT_CALL(*event_router,
209 CallEventRouter( 243 CallEventRouter(profile1_, "", kEventName, NULL, url));
210 profile1_, "", kEventName, kEventArgs, NULL, url));
211 EXPECT_CALL(*event_router, 244 EXPECT_CALL(*event_router,
212 CallEventRouter(incognito, _, _, _, _, _)).Times(0); 245 CallEventRouter(incognito, _, _, _, _)).Times(0);
213 EXPECT_CALL(*event_router, 246 EXPECT_CALL(*event_router,
214 CallEventRouter(profile2_, _, _, _, _, _)).Times(0); 247 CallEventRouter(profile2_, _, _, _, _)).Times(0);
215 event_router->DispatchEventToRenderers(kEventName, kEventArgs, 248 DispatchEventToRenderers(event_router.get(), kEventName, profile1_, false,
216 profile1_, false, url); 249 url);
217 } 250 }
218 251
219 TEST_F(EventRouterForwarderTest, BroadcastExtensionUI) { 252 TEST_F(EventRouterForwarderTest, BroadcastExtensionUI) {
220 scoped_refptr<MockEventRouterForwarder> event_router( 253 scoped_refptr<MockEventRouterForwarder> event_router(
221 new MockEventRouterForwarder); 254 new MockEventRouterForwarder);
222 GURL url; 255 GURL url;
223 EXPECT_CALL(*event_router, 256 EXPECT_CALL(*event_router,
224 CallEventRouter( 257 CallEventRouter(profile1_, kExt, kEventName, profile1_, url));
225 profile1_, kExt, kEventName, kEventArgs, profile1_, url));
226 EXPECT_CALL(*event_router, 258 EXPECT_CALL(*event_router,
227 CallEventRouter( 259 CallEventRouter(profile2_, kExt, kEventName, profile2_, url));
228 profile2_, kExt, kEventName, kEventArgs, profile2_, url)); 260 BroadcastEventToExtension(event_router.get(), kExt, kEventName, url);
229 event_router->BroadcastEventToExtension(kExt, kEventName, kEventArgs, url);
230 } 261 }
231 262
232 TEST_F(EventRouterForwarderTest, UnicastExtensionUIRestricted) { 263 TEST_F(EventRouterForwarderTest, UnicastExtensionUIRestricted) {
233 scoped_refptr<MockEventRouterForwarder> event_router( 264 scoped_refptr<MockEventRouterForwarder> event_router(
234 new MockEventRouterForwarder); 265 new MockEventRouterForwarder);
235 using ::testing::_; 266 using ::testing::_;
236 GURL url; 267 GURL url;
237 EXPECT_CALL(*event_router, 268 EXPECT_CALL(*event_router,
238 CallEventRouter( 269 CallEventRouter(profile1_, kExt, kEventName, profile1_, url));
239 profile1_, kExt, kEventName, kEventArgs, profile1_, url));
240 EXPECT_CALL(*event_router, 270 EXPECT_CALL(*event_router,
241 CallEventRouter(profile2_, _, _, _, _, _)).Times(0); 271 CallEventRouter(profile2_, _, _, _, _)).Times(0);
242 event_router->DispatchEventToExtension(kExt, kEventName, kEventArgs, 272 DispatchEventToExtension(event_router.get(), kExt, kEventName, profile1_,
243 profile1_, true, url); 273 true, url);
244 } 274 }
245 275
246 TEST_F(EventRouterForwarderTest, UnicastExtensionUIUnrestricted) { 276 TEST_F(EventRouterForwarderTest, UnicastExtensionUIUnrestricted) {
247 scoped_refptr<MockEventRouterForwarder> event_router( 277 scoped_refptr<MockEventRouterForwarder> event_router(
248 new MockEventRouterForwarder); 278 new MockEventRouterForwarder);
249 using ::testing::_; 279 using ::testing::_;
250 GURL url; 280 GURL url;
251 EXPECT_CALL(*event_router, 281 EXPECT_CALL(*event_router,
252 CallEventRouter( 282 CallEventRouter(profile1_, kExt, kEventName, NULL, url));
253 profile1_, kExt, kEventName, kEventArgs, NULL, url));
254 EXPECT_CALL(*event_router, 283 EXPECT_CALL(*event_router,
255 CallEventRouter(profile2_, _, _, _, _, _)).Times(0); 284 CallEventRouter(profile2_, _, _, _, _)).Times(0);
256 event_router->DispatchEventToExtension(kExt, kEventName, kEventArgs, 285 DispatchEventToExtension(event_router.get(), kExt, kEventName, profile1_,
257 profile1_, false, url); 286 false, url);
258 } 287 }
259 288
260 } // namespace extensions 289 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/event_router_forwarder.cc ('k') | chrome/browser/extensions/extension_devtools_bridge.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698