OLD | NEW |
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/extension_event_router_forwarder.h" | 5 #include "chrome/browser/extensions/extension_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 { | 22 namespace { |
23 | 23 |
24 const char kEventName[] = "event_name"; | 24 const char kEventName[] = "event_name"; |
25 const char kEventArgs[] = "event_args"; | |
26 const char kExt[] = "extension"; | 25 const char kExt[] = "extension"; |
27 | 26 |
28 class MockExtensionEventRouterForwarder : public ExtensionEventRouterForwarder { | 27 class MockExtensionEventRouterForwarder : public ExtensionEventRouterForwarder { |
29 public: | 28 public: |
30 MOCK_METHOD6(CallExtensionEventRouter, | 29 MOCK_METHOD6(CallExtensionEventRouter, |
31 void(Profile*, const std::string&, const std::string&, const std::string&, | 30 void(Profile*, const std::string&, const std::string&, base::ListValue*, |
32 Profile*, const GURL&)); | 31 Profile*, const GURL&)); |
33 | 32 |
34 protected: | 33 protected: |
35 virtual ~MockExtensionEventRouterForwarder() {} | 34 virtual ~MockExtensionEventRouterForwarder() {} |
36 }; | 35 }; |
37 | 36 |
38 } // namespace | 37 } // namespace |
39 | 38 |
40 class ExtensionEventRouterForwarderTest : public testing::Test { | 39 class ExtensionEventRouterForwarderTest : public testing::Test { |
41 protected: | 40 protected: |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 TestingProfile* profile1_; | 75 TestingProfile* profile1_; |
77 TestingProfile* profile2_; | 76 TestingProfile* profile2_; |
78 }; | 77 }; |
79 | 78 |
80 TEST_F(ExtensionEventRouterForwarderTest, BroadcastRendererUI) { | 79 TEST_F(ExtensionEventRouterForwarderTest, BroadcastRendererUI) { |
81 scoped_refptr<MockExtensionEventRouterForwarder> event_router( | 80 scoped_refptr<MockExtensionEventRouterForwarder> event_router( |
82 new MockExtensionEventRouterForwarder); | 81 new MockExtensionEventRouterForwarder); |
83 GURL url; | 82 GURL url; |
84 EXPECT_CALL(*event_router, | 83 EXPECT_CALL(*event_router, |
85 CallExtensionEventRouter( | 84 CallExtensionEventRouter( |
86 profile1_, "", kEventName, kEventArgs, profile1_, url)); | 85 profile1_, "", kEventName, NULL, profile1_, url)); |
87 EXPECT_CALL(*event_router, | 86 EXPECT_CALL(*event_router, |
88 CallExtensionEventRouter( | 87 CallExtensionEventRouter( |
89 profile2_, "", kEventName, kEventArgs, profile2_, url)); | 88 profile2_, "", kEventName, NULL, profile2_, url)); |
90 event_router->BroadcastEventToRenderers(kEventName, kEventArgs, url); | 89 event_router->BroadcastEventToRenderers(kEventName, NULL, url); |
91 } | 90 } |
92 | 91 |
93 TEST_F(ExtensionEventRouterForwarderTest, BroadcastRendererUIIncognito) { | 92 TEST_F(ExtensionEventRouterForwarderTest, BroadcastRendererUIIncognito) { |
94 scoped_refptr<MockExtensionEventRouterForwarder> event_router( | 93 scoped_refptr<MockExtensionEventRouterForwarder> event_router( |
95 new MockExtensionEventRouterForwarder); | 94 new MockExtensionEventRouterForwarder); |
96 using ::testing::_; | 95 using ::testing::_; |
97 GURL url; | 96 GURL url; |
98 Profile* incognito = CreateIncognitoProfile(profile1_); | 97 Profile* incognito = CreateIncognitoProfile(profile1_); |
99 EXPECT_CALL(*event_router, | 98 EXPECT_CALL(*event_router, |
100 CallExtensionEventRouter( | 99 CallExtensionEventRouter( |
101 profile1_, "", kEventName, kEventArgs, profile1_, url)); | 100 profile1_, "", kEventName, NULL, profile1_, url)); |
102 EXPECT_CALL(*event_router, | 101 EXPECT_CALL(*event_router, |
103 CallExtensionEventRouter(incognito, _, _, _, _, _)).Times(0); | 102 CallExtensionEventRouter(incognito, _, _, _, _, _)).Times(0); |
104 EXPECT_CALL(*event_router, | 103 EXPECT_CALL(*event_router, |
105 CallExtensionEventRouter( | 104 CallExtensionEventRouter( |
106 profile2_, "", kEventName, kEventArgs, profile2_, url)); | 105 profile2_, "", kEventName, NULL, profile2_, url)); |
107 event_router->BroadcastEventToRenderers(kEventName, kEventArgs, url); | 106 event_router->BroadcastEventToRenderers(kEventName, NULL, url); |
108 } | 107 } |
109 | 108 |
110 // This is the canonical test for passing control flow from the IO thread | 109 // 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 | 110 // to the UI thread. Repeating this for all public functions of |
112 // ExtensionEventRouterForwarder would not increase coverage. | 111 // ExtensionEventRouterForwarder would not increase coverage. |
113 TEST_F(ExtensionEventRouterForwarderTest, BroadcastRendererIO) { | 112 TEST_F(ExtensionEventRouterForwarderTest, BroadcastRendererIO) { |
114 scoped_refptr<MockExtensionEventRouterForwarder> event_router( | 113 scoped_refptr<MockExtensionEventRouterForwarder> event_router( |
115 new MockExtensionEventRouterForwarder); | 114 new MockExtensionEventRouterForwarder); |
116 GURL url; | 115 GURL url; |
117 EXPECT_CALL(*event_router, | 116 EXPECT_CALL(*event_router, |
118 CallExtensionEventRouter( | 117 CallExtensionEventRouter( |
119 profile1_, "", kEventName, kEventArgs, profile1_, url)); | 118 profile1_, "", kEventName, NULL, profile1_, url)); |
120 EXPECT_CALL(*event_router, | 119 EXPECT_CALL(*event_router, |
121 CallExtensionEventRouter( | 120 CallExtensionEventRouter( |
122 profile2_, "", kEventName, kEventArgs, profile2_, url)); | 121 profile2_, "", kEventName, NULL, profile2_, url)); |
123 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 122 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
124 base::Bind( | 123 base::Bind( |
125 &MockExtensionEventRouterForwarder::BroadcastEventToRenderers, | 124 &MockExtensionEventRouterForwarder::BroadcastEventToRenderers, |
126 event_router.get(), | 125 event_router.get(), std::string(kEventName), |
127 std::string(kEventName), std::string(kEventArgs), url)); | 126 static_cast<base::ListValue*>(NULL), url)); |
128 | 127 |
129 // Wait for IO thread's message loop to be processed | 128 // Wait for IO thread's message loop to be processed |
130 scoped_refptr<base::ThreadTestHelper> helper( | 129 scoped_refptr<base::ThreadTestHelper> helper( |
131 new base::ThreadTestHelper( | 130 new base::ThreadTestHelper( |
132 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO))); | 131 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO))); |
133 ASSERT_TRUE(helper->Run()); | 132 ASSERT_TRUE(helper->Run()); |
134 | 133 |
135 MessageLoop::current()->RunAllPending(); | 134 MessageLoop::current()->RunAllPending(); |
136 } | 135 } |
137 | 136 |
138 TEST_F(ExtensionEventRouterForwarderTest, UnicastRendererUIRestricted) { | 137 TEST_F(ExtensionEventRouterForwarderTest, UnicastRendererUIRestricted) { |
139 scoped_refptr<MockExtensionEventRouterForwarder> event_router( | 138 scoped_refptr<MockExtensionEventRouterForwarder> event_router( |
140 new MockExtensionEventRouterForwarder); | 139 new MockExtensionEventRouterForwarder); |
141 using ::testing::_; | 140 using ::testing::_; |
142 GURL url; | 141 GURL url; |
143 EXPECT_CALL(*event_router, | 142 EXPECT_CALL(*event_router, |
144 CallExtensionEventRouter( | 143 CallExtensionEventRouter( |
145 profile1_, "", kEventName, kEventArgs, profile1_, url)); | 144 profile1_, "", kEventName, NULL, profile1_, url)); |
146 EXPECT_CALL(*event_router, | 145 EXPECT_CALL(*event_router, |
147 CallExtensionEventRouter(profile2_, _, _, _, _, _)).Times(0); | 146 CallExtensionEventRouter(profile2_, _, _, _, _, _)).Times(0); |
148 event_router->DispatchEventToRenderers(kEventName, kEventArgs, | 147 event_router->DispatchEventToRenderers(kEventName, NULL, profile1_, true, |
149 profile1_, true, url); | 148 url); |
150 } | 149 } |
151 | 150 |
152 TEST_F(ExtensionEventRouterForwarderTest, | 151 TEST_F(ExtensionEventRouterForwarderTest, |
153 UnicastRendererUIRestrictedIncognito1) { | 152 UnicastRendererUIRestrictedIncognito1) { |
154 scoped_refptr<MockExtensionEventRouterForwarder> event_router( | 153 scoped_refptr<MockExtensionEventRouterForwarder> event_router( |
155 new MockExtensionEventRouterForwarder); | 154 new MockExtensionEventRouterForwarder); |
156 Profile* incognito = CreateIncognitoProfile(profile1_); | 155 Profile* incognito = CreateIncognitoProfile(profile1_); |
157 using ::testing::_; | 156 using ::testing::_; |
158 GURL url; | 157 GURL url; |
159 EXPECT_CALL(*event_router, | 158 EXPECT_CALL(*event_router, |
160 CallExtensionEventRouter( | 159 CallExtensionEventRouter( |
161 profile1_, "", kEventName, kEventArgs, profile1_, url)); | 160 profile1_, "", kEventName, NULL, profile1_, url)); |
162 EXPECT_CALL(*event_router, | 161 EXPECT_CALL(*event_router, |
163 CallExtensionEventRouter(incognito, _, _, _, _, _)).Times(0); | 162 CallExtensionEventRouter(incognito, _, _, _, _, _)).Times(0); |
164 EXPECT_CALL(*event_router, | 163 EXPECT_CALL(*event_router, |
165 CallExtensionEventRouter(profile2_, _, _, _, _, _)).Times(0); | 164 CallExtensionEventRouter(profile2_, _, _, _, _, _)).Times(0); |
166 event_router->DispatchEventToRenderers(kEventName, kEventArgs, | 165 event_router->DispatchEventToRenderers(kEventName, NULL, |
167 profile1_, true, url); | 166 profile1_, true, url); |
168 } | 167 } |
169 | 168 |
170 TEST_F(ExtensionEventRouterForwarderTest, | 169 TEST_F(ExtensionEventRouterForwarderTest, |
171 UnicastRendererUIRestrictedIncognito2) { | 170 UnicastRendererUIRestrictedIncognito2) { |
172 scoped_refptr<MockExtensionEventRouterForwarder> event_router( | 171 scoped_refptr<MockExtensionEventRouterForwarder> event_router( |
173 new MockExtensionEventRouterForwarder); | 172 new MockExtensionEventRouterForwarder); |
174 Profile* incognito = CreateIncognitoProfile(profile1_); | 173 Profile* incognito = CreateIncognitoProfile(profile1_); |
175 using ::testing::_; | 174 using ::testing::_; |
176 GURL url; | 175 GURL url; |
177 EXPECT_CALL(*event_router, | 176 EXPECT_CALL(*event_router, |
178 CallExtensionEventRouter(profile1_, _, _, _, _, _)).Times(0); | 177 CallExtensionEventRouter(profile1_, _, _, _, _, _)).Times(0); |
179 EXPECT_CALL(*event_router, | 178 EXPECT_CALL(*event_router, |
180 CallExtensionEventRouter( | 179 CallExtensionEventRouter( |
181 incognito, "", kEventName, kEventArgs, incognito, url)); | 180 incognito, "", kEventName, NULL, incognito, url)); |
182 EXPECT_CALL(*event_router, | 181 EXPECT_CALL(*event_router, |
183 CallExtensionEventRouter(profile2_, _, _, _, _, _)).Times(0); | 182 CallExtensionEventRouter(profile2_, _, _, _, _, _)).Times(0); |
184 event_router->DispatchEventToRenderers(kEventName, kEventArgs, | 183 event_router->DispatchEventToRenderers(kEventName, NULL, |
185 incognito, true, url); | 184 incognito, true, url); |
186 } | 185 } |
187 | 186 |
188 TEST_F(ExtensionEventRouterForwarderTest, UnicastRendererUIUnrestricted) { | 187 TEST_F(ExtensionEventRouterForwarderTest, UnicastRendererUIUnrestricted) { |
189 scoped_refptr<MockExtensionEventRouterForwarder> event_router( | 188 scoped_refptr<MockExtensionEventRouterForwarder> event_router( |
190 new MockExtensionEventRouterForwarder); | 189 new MockExtensionEventRouterForwarder); |
191 using ::testing::_; | 190 using ::testing::_; |
192 GURL url; | 191 GURL url; |
193 EXPECT_CALL(*event_router, | 192 EXPECT_CALL(*event_router, |
194 CallExtensionEventRouter( | 193 CallExtensionEventRouter(profile1_, "", kEventName, NULL, NULL, url)); |
195 profile1_, "", kEventName, kEventArgs, NULL, url)); | |
196 EXPECT_CALL(*event_router, | 194 EXPECT_CALL(*event_router, |
197 CallExtensionEventRouter(profile2_, _, _, _, _, _)).Times(0); | 195 CallExtensionEventRouter(profile2_, _, _, _, _, _)).Times(0); |
198 event_router->DispatchEventToRenderers(kEventName, kEventArgs, | 196 event_router->DispatchEventToRenderers(kEventName, NULL, |
199 profile1_, false, url); | 197 profile1_, false, url); |
200 } | 198 } |
201 | 199 |
202 TEST_F(ExtensionEventRouterForwarderTest, | 200 TEST_F(ExtensionEventRouterForwarderTest, |
203 UnicastRendererUIUnrestrictedIncognito) { | 201 UnicastRendererUIUnrestrictedIncognito) { |
204 scoped_refptr<MockExtensionEventRouterForwarder> event_router( | 202 scoped_refptr<MockExtensionEventRouterForwarder> event_router( |
205 new MockExtensionEventRouterForwarder); | 203 new MockExtensionEventRouterForwarder); |
206 Profile* incognito = CreateIncognitoProfile(profile1_); | 204 Profile* incognito = CreateIncognitoProfile(profile1_); |
207 using ::testing::_; | 205 using ::testing::_; |
208 GURL url; | 206 GURL url; |
209 EXPECT_CALL(*event_router, | 207 EXPECT_CALL(*event_router, |
210 CallExtensionEventRouter( | 208 CallExtensionEventRouter(profile1_, "", kEventName, NULL, NULL, url)); |
211 profile1_, "", kEventName, kEventArgs, NULL, url)); | |
212 EXPECT_CALL(*event_router, | 209 EXPECT_CALL(*event_router, |
213 CallExtensionEventRouter(incognito, _, _, _, _, _)).Times(0); | 210 CallExtensionEventRouter(incognito, _, _, _, _, _)).Times(0); |
214 EXPECT_CALL(*event_router, | 211 EXPECT_CALL(*event_router, |
215 CallExtensionEventRouter(profile2_, _, _, _, _, _)).Times(0); | 212 CallExtensionEventRouter(profile2_, _, _, _, _, _)).Times(0); |
216 event_router->DispatchEventToRenderers(kEventName, kEventArgs, | 213 event_router->DispatchEventToRenderers(kEventName, NULL, profile1_, false, |
217 profile1_, false, url); | 214 url); |
218 } | 215 } |
219 | 216 |
220 TEST_F(ExtensionEventRouterForwarderTest, BroadcastExtensionUI) { | 217 TEST_F(ExtensionEventRouterForwarderTest, BroadcastExtensionUI) { |
221 scoped_refptr<MockExtensionEventRouterForwarder> event_router( | 218 scoped_refptr<MockExtensionEventRouterForwarder> event_router( |
222 new MockExtensionEventRouterForwarder); | 219 new MockExtensionEventRouterForwarder); |
223 GURL url; | 220 GURL url; |
224 EXPECT_CALL(*event_router, | 221 EXPECT_CALL(*event_router, |
225 CallExtensionEventRouter( | 222 CallExtensionEventRouter( |
226 profile1_, kExt, kEventName, kEventArgs, profile1_, url)); | 223 profile1_, kExt, kEventName, NULL, profile1_, url)); |
227 EXPECT_CALL(*event_router, | 224 EXPECT_CALL(*event_router, |
228 CallExtensionEventRouter( | 225 CallExtensionEventRouter( |
229 profile2_, kExt, kEventName, kEventArgs, profile2_, url)); | 226 profile2_, kExt, kEventName, NULL, profile2_, url)); |
230 event_router->BroadcastEventToExtension(kExt, kEventName, kEventArgs, url); | 227 event_router->BroadcastEventToExtension(kExt, kEventName, NULL, url); |
231 } | 228 } |
232 | 229 |
233 TEST_F(ExtensionEventRouterForwarderTest, UnicastExtensionUIRestricted) { | 230 TEST_F(ExtensionEventRouterForwarderTest, UnicastExtensionUIRestricted) { |
234 scoped_refptr<MockExtensionEventRouterForwarder> event_router( | 231 scoped_refptr<MockExtensionEventRouterForwarder> event_router( |
235 new MockExtensionEventRouterForwarder); | 232 new MockExtensionEventRouterForwarder); |
236 using ::testing::_; | 233 using ::testing::_; |
237 GURL url; | 234 GURL url; |
238 EXPECT_CALL(*event_router, | 235 EXPECT_CALL(*event_router, |
239 CallExtensionEventRouter( | 236 CallExtensionEventRouter( |
240 profile1_, kExt, kEventName, kEventArgs, profile1_, url)); | 237 profile1_, kExt, kEventName, NULL, profile1_, url)); |
241 EXPECT_CALL(*event_router, | 238 EXPECT_CALL(*event_router, |
242 CallExtensionEventRouter(profile2_, _, _, _, _, _)).Times(0); | 239 CallExtensionEventRouter(profile2_, _, _, _, _, _)).Times(0); |
243 event_router->DispatchEventToExtension(kExt, kEventName, kEventArgs, | 240 event_router->DispatchEventToExtension(kExt, kEventName, NULL, |
244 profile1_, true, url); | 241 profile1_, true, url); |
245 } | 242 } |
246 | 243 |
247 TEST_F(ExtensionEventRouterForwarderTest, UnicastExtensionUIUnrestricted) { | 244 TEST_F(ExtensionEventRouterForwarderTest, UnicastExtensionUIUnrestricted) { |
248 scoped_refptr<MockExtensionEventRouterForwarder> event_router( | 245 scoped_refptr<MockExtensionEventRouterForwarder> event_router( |
249 new MockExtensionEventRouterForwarder); | 246 new MockExtensionEventRouterForwarder); |
250 using ::testing::_; | 247 using ::testing::_; |
251 GURL url; | 248 GURL url; |
252 EXPECT_CALL(*event_router, | 249 EXPECT_CALL(*event_router, |
253 CallExtensionEventRouter( | 250 CallExtensionEventRouter( |
254 profile1_, kExt, kEventName, kEventArgs, NULL, url)); | 251 profile1_, kExt, kEventName, NULL, NULL, url)); |
255 EXPECT_CALL(*event_router, | 252 EXPECT_CALL(*event_router, |
256 CallExtensionEventRouter(profile2_, _, _, _, _, _)).Times(0); | 253 CallExtensionEventRouter(profile2_, _, _, _, _, _)).Times(0); |
257 event_router->DispatchEventToExtension(kExt, kEventName, kEventArgs, | 254 event_router->DispatchEventToExtension(kExt, kEventName, NULL, |
258 profile1_, false, url); | 255 profile1_, false, url); |
259 } | 256 } |
OLD | NEW |