| 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/event_router_forwarder.h" | 5 #include "chrome/browser/extensions/event_router_forwarder.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/power_monitor/power_monitor.h" | 10 #include "base/power_monitor/power_monitor.h" |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 std::unique_ptr<base::PowerMonitor> dummy; | 130 std::unique_ptr<base::PowerMonitor> dummy; |
| 131 // Profiles are weak pointers, owned by ProfileManager in |browser_process_|. | 131 // Profiles are weak pointers, owned by ProfileManager in |browser_process_|. |
| 132 TestingProfile* profile1_; | 132 TestingProfile* profile1_; |
| 133 TestingProfile* profile2_; | 133 TestingProfile* profile2_; |
| 134 }; | 134 }; |
| 135 | 135 |
| 136 TEST_F(EventRouterForwarderTest, BroadcastRendererUI) { | 136 TEST_F(EventRouterForwarderTest, BroadcastRendererUI) { |
| 137 scoped_refptr<MockEventRouterForwarder> event_router( | 137 scoped_refptr<MockEventRouterForwarder> event_router( |
| 138 new MockEventRouterForwarder); | 138 new MockEventRouterForwarder); |
| 139 GURL url; | 139 GURL url; |
| 140 EXPECT_CALL(*event_router.get(), | 140 EXPECT_CALL(*event_router, CallEventRouter(profile1_, "", kHistogramValue, |
| 141 CallEventRouter(profile1_, "", kHistogramValue, kEventName, | 141 kEventName, profile1_, url)); |
| 142 profile1_, url)); | 142 EXPECT_CALL(*event_router, CallEventRouter(profile2_, "", kHistogramValue, |
| 143 EXPECT_CALL(*event_router.get(), | 143 kEventName, profile2_, url)); |
| 144 CallEventRouter(profile2_, "", kHistogramValue, kEventName, | |
| 145 profile2_, url)); | |
| 146 BroadcastEventToRenderers(event_router.get(), kHistogramValue, kEventName, | 144 BroadcastEventToRenderers(event_router.get(), kHistogramValue, kEventName, |
| 147 url); | 145 url); |
| 148 } | 146 } |
| 149 | 147 |
| 150 TEST_F(EventRouterForwarderTest, BroadcastRendererUIIncognito) { | 148 TEST_F(EventRouterForwarderTest, BroadcastRendererUIIncognito) { |
| 151 scoped_refptr<MockEventRouterForwarder> event_router( | 149 scoped_refptr<MockEventRouterForwarder> event_router( |
| 152 new MockEventRouterForwarder); | 150 new MockEventRouterForwarder); |
| 153 using ::testing::_; | 151 using ::testing::_; |
| 154 GURL url; | 152 GURL url; |
| 155 Profile* incognito = profile1_->GetOffTheRecordProfile(); | 153 Profile* incognito = profile1_->GetOffTheRecordProfile(); |
| 156 EXPECT_CALL(*event_router.get(), | 154 EXPECT_CALL(*event_router, CallEventRouter(profile1_, "", kHistogramValue, |
| 157 CallEventRouter(profile1_, "", kHistogramValue, kEventName, | 155 kEventName, profile1_, url)); |
| 158 profile1_, url)); | 156 EXPECT_CALL(*event_router, CallEventRouter(incognito, _, _, _, _, _)) |
| 159 EXPECT_CALL(*event_router.get(), CallEventRouter(incognito, _, _, _, _, _)) | |
| 160 .Times(0); | 157 .Times(0); |
| 161 EXPECT_CALL(*event_router.get(), | 158 EXPECT_CALL(*event_router, CallEventRouter(profile2_, "", kHistogramValue, |
| 162 CallEventRouter(profile2_, "", kHistogramValue, kEventName, | 159 kEventName, profile2_, url)); |
| 163 profile2_, url)); | |
| 164 BroadcastEventToRenderers(event_router.get(), kHistogramValue, kEventName, | 160 BroadcastEventToRenderers(event_router.get(), kHistogramValue, kEventName, |
| 165 url); | 161 url); |
| 166 } | 162 } |
| 167 | 163 |
| 168 // This is the canonical test for passing control flow from the IO thread | 164 // This is the canonical test for passing control flow from the IO thread |
| 169 // to the UI thread. Repeating this for all public functions of | 165 // to the UI thread. Repeating this for all public functions of |
| 170 // EventRouterForwarder would not increase coverage. | 166 // EventRouterForwarder would not increase coverage. |
| 171 TEST_F(EventRouterForwarderTest, BroadcastRendererIO) { | 167 TEST_F(EventRouterForwarderTest, BroadcastRendererIO) { |
| 172 scoped_refptr<MockEventRouterForwarder> event_router( | 168 scoped_refptr<MockEventRouterForwarder> event_router( |
| 173 new MockEventRouterForwarder); | 169 new MockEventRouterForwarder); |
| 174 GURL url; | 170 GURL url; |
| 175 EXPECT_CALL(*event_router.get(), | 171 EXPECT_CALL(*event_router, CallEventRouter(profile1_, "", kHistogramValue, |
| 176 CallEventRouter(profile1_, "", kHistogramValue, kEventName, | 172 kEventName, profile1_, url)); |
| 177 profile1_, url)); | 173 EXPECT_CALL(*event_router, CallEventRouter(profile2_, "", kHistogramValue, |
| 178 EXPECT_CALL(*event_router.get(), | 174 kEventName, profile2_, url)); |
| 179 CallEventRouter(profile2_, "", kHistogramValue, kEventName, | |
| 180 profile2_, url)); | |
| 181 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 175 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 182 base::Bind(&BroadcastEventToRenderers, | 176 base::Bind(&BroadcastEventToRenderers, |
| 183 base::Unretained(event_router.get()), | 177 base::Unretained(event_router.get()), |
| 184 kHistogramValue, kEventName, url)); | 178 kHistogramValue, kEventName, url)); |
| 185 | 179 |
| 186 // Wait for IO thread's message loop to be processed | 180 // Wait for IO thread's message loop to be processed |
| 187 scoped_refptr<base::ThreadTestHelper> helper(new base::ThreadTestHelper( | 181 scoped_refptr<base::ThreadTestHelper> helper(new base::ThreadTestHelper( |
| 188 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO).get())); | 182 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO).get())); |
| 189 ASSERT_TRUE(helper->Run()); | 183 ASSERT_TRUE(helper->Run()); |
| 190 | 184 |
| 191 base::RunLoop().RunUntilIdle(); | 185 base::RunLoop().RunUntilIdle(); |
| 192 } | 186 } |
| 193 | 187 |
| 194 TEST_F(EventRouterForwarderTest, UnicastRendererUIRestricted) { | 188 TEST_F(EventRouterForwarderTest, UnicastRendererUIRestricted) { |
| 195 scoped_refptr<MockEventRouterForwarder> event_router( | 189 scoped_refptr<MockEventRouterForwarder> event_router( |
| 196 new MockEventRouterForwarder); | 190 new MockEventRouterForwarder); |
| 197 using ::testing::_; | 191 using ::testing::_; |
| 198 GURL url; | 192 GURL url; |
| 199 EXPECT_CALL(*event_router.get(), | 193 EXPECT_CALL(*event_router, CallEventRouter(profile1_, "", kHistogramValue, |
| 200 CallEventRouter(profile1_, "", kHistogramValue, kEventName, | 194 kEventName, profile1_, url)); |
| 201 profile1_, url)); | 195 EXPECT_CALL(*event_router, CallEventRouter(profile2_, _, _, _, _, _)) |
| 202 EXPECT_CALL(*event_router.get(), CallEventRouter(profile2_, _, _, _, _, _)) | |
| 203 .Times(0); | 196 .Times(0); |
| 204 DispatchEventToRenderers(event_router.get(), kHistogramValue, kEventName, | 197 DispatchEventToRenderers(event_router.get(), kHistogramValue, kEventName, |
| 205 profile1_, true, url); | 198 profile1_, true, url); |
| 206 } | 199 } |
| 207 | 200 |
| 208 TEST_F(EventRouterForwarderTest, UnicastRendererUIRestrictedIncognito1) { | 201 TEST_F(EventRouterForwarderTest, UnicastRendererUIRestrictedIncognito1) { |
| 209 scoped_refptr<MockEventRouterForwarder> event_router( | 202 scoped_refptr<MockEventRouterForwarder> event_router( |
| 210 new MockEventRouterForwarder); | 203 new MockEventRouterForwarder); |
| 211 Profile* incognito = profile1_->GetOffTheRecordProfile(); | 204 Profile* incognito = profile1_->GetOffTheRecordProfile(); |
| 212 using ::testing::_; | 205 using ::testing::_; |
| 213 GURL url; | 206 GURL url; |
| 214 EXPECT_CALL(*event_router.get(), | 207 EXPECT_CALL(*event_router, CallEventRouter(profile1_, "", kHistogramValue, |
| 215 CallEventRouter(profile1_, "", kHistogramValue, kEventName, | 208 kEventName, profile1_, url)); |
| 216 profile1_, url)); | 209 EXPECT_CALL(*event_router, CallEventRouter(incognito, _, _, _, _, _)) |
| 217 EXPECT_CALL(*event_router.get(), CallEventRouter(incognito, _, _, _, _, _)) | |
| 218 .Times(0); | 210 .Times(0); |
| 219 EXPECT_CALL(*event_router.get(), CallEventRouter(profile2_, _, _, _, _, _)) | 211 EXPECT_CALL(*event_router, CallEventRouter(profile2_, _, _, _, _, _)) |
| 220 .Times(0); | 212 .Times(0); |
| 221 DispatchEventToRenderers(event_router.get(), kHistogramValue, kEventName, | 213 DispatchEventToRenderers(event_router.get(), kHistogramValue, kEventName, |
| 222 profile1_, true, url); | 214 profile1_, true, url); |
| 223 } | 215 } |
| 224 | 216 |
| 225 TEST_F(EventRouterForwarderTest, UnicastRendererUIRestrictedIncognito2) { | 217 TEST_F(EventRouterForwarderTest, UnicastRendererUIRestrictedIncognito2) { |
| 226 scoped_refptr<MockEventRouterForwarder> event_router( | 218 scoped_refptr<MockEventRouterForwarder> event_router( |
| 227 new MockEventRouterForwarder); | 219 new MockEventRouterForwarder); |
| 228 Profile* incognito = profile1_->GetOffTheRecordProfile(); | 220 Profile* incognito = profile1_->GetOffTheRecordProfile(); |
| 229 using ::testing::_; | 221 using ::testing::_; |
| 230 GURL url; | 222 GURL url; |
| 231 EXPECT_CALL(*event_router.get(), CallEventRouter(profile1_, _, _, _, _, _)) | 223 EXPECT_CALL(*event_router, CallEventRouter(profile1_, _, _, _, _, _)) |
| 232 .Times(0); | 224 .Times(0); |
| 233 EXPECT_CALL(*event_router.get(), | 225 EXPECT_CALL(*event_router, CallEventRouter(incognito, "", kHistogramValue, |
| 234 CallEventRouter(incognito, "", kHistogramValue, kEventName, | 226 kEventName, incognito, url)); |
| 235 incognito, url)); | 227 EXPECT_CALL(*event_router, CallEventRouter(profile2_, _, _, _, _, _)) |
| 236 EXPECT_CALL(*event_router.get(), CallEventRouter(profile2_, _, _, _, _, _)) | |
| 237 .Times(0); | 228 .Times(0); |
| 238 DispatchEventToRenderers(event_router.get(), kHistogramValue, kEventName, | 229 DispatchEventToRenderers(event_router.get(), kHistogramValue, kEventName, |
| 239 incognito, true, url); | 230 incognito, true, url); |
| 240 } | 231 } |
| 241 | 232 |
| 242 TEST_F(EventRouterForwarderTest, UnicastRendererUIUnrestricted) { | 233 TEST_F(EventRouterForwarderTest, UnicastRendererUIUnrestricted) { |
| 243 scoped_refptr<MockEventRouterForwarder> event_router( | 234 scoped_refptr<MockEventRouterForwarder> event_router( |
| 244 new MockEventRouterForwarder); | 235 new MockEventRouterForwarder); |
| 245 using ::testing::_; | 236 using ::testing::_; |
| 246 GURL url; | 237 GURL url; |
| 247 EXPECT_CALL( | 238 EXPECT_CALL(*event_router, CallEventRouter(profile1_, "", kHistogramValue, |
| 248 *event_router.get(), | 239 kEventName, NULL, url)); |
| 249 CallEventRouter(profile1_, "", kHistogramValue, kEventName, NULL, url)); | 240 EXPECT_CALL(*event_router, CallEventRouter(profile2_, _, _, _, _, _)) |
| 250 EXPECT_CALL(*event_router.get(), CallEventRouter(profile2_, _, _, _, _, _)) | |
| 251 .Times(0); | 241 .Times(0); |
| 252 DispatchEventToRenderers(event_router.get(), kHistogramValue, kEventName, | 242 DispatchEventToRenderers(event_router.get(), kHistogramValue, kEventName, |
| 253 profile1_, false, url); | 243 profile1_, false, url); |
| 254 } | 244 } |
| 255 | 245 |
| 256 TEST_F(EventRouterForwarderTest, UnicastRendererUIUnrestrictedIncognito) { | 246 TEST_F(EventRouterForwarderTest, UnicastRendererUIUnrestrictedIncognito) { |
| 257 scoped_refptr<MockEventRouterForwarder> event_router( | 247 scoped_refptr<MockEventRouterForwarder> event_router( |
| 258 new MockEventRouterForwarder); | 248 new MockEventRouterForwarder); |
| 259 Profile* incognito = profile1_->GetOffTheRecordProfile(); | 249 Profile* incognito = profile1_->GetOffTheRecordProfile(); |
| 260 using ::testing::_; | 250 using ::testing::_; |
| 261 GURL url; | 251 GURL url; |
| 262 EXPECT_CALL( | 252 EXPECT_CALL(*event_router, CallEventRouter(profile1_, "", kHistogramValue, |
| 263 *event_router.get(), | 253 kEventName, NULL, url)); |
| 264 CallEventRouter(profile1_, "", kHistogramValue, kEventName, NULL, url)); | 254 EXPECT_CALL(*event_router, CallEventRouter(incognito, _, _, _, _, _)) |
| 265 EXPECT_CALL(*event_router.get(), CallEventRouter(incognito, _, _, _, _, _)) | |
| 266 .Times(0); | 255 .Times(0); |
| 267 EXPECT_CALL(*event_router.get(), CallEventRouter(profile2_, _, _, _, _, _)) | 256 EXPECT_CALL(*event_router, CallEventRouter(profile2_, _, _, _, _, _)) |
| 268 .Times(0); | 257 .Times(0); |
| 269 DispatchEventToRenderers(event_router.get(), kHistogramValue, kEventName, | 258 DispatchEventToRenderers(event_router.get(), kHistogramValue, kEventName, |
| 270 profile1_, false, url); | 259 profile1_, false, url); |
| 271 } | 260 } |
| 272 | 261 |
| 273 TEST_F(EventRouterForwarderTest, BroadcastExtensionUI) { | 262 TEST_F(EventRouterForwarderTest, BroadcastExtensionUI) { |
| 274 scoped_refptr<MockEventRouterForwarder> event_router( | 263 scoped_refptr<MockEventRouterForwarder> event_router( |
| 275 new MockEventRouterForwarder); | 264 new MockEventRouterForwarder); |
| 276 GURL url; | 265 GURL url; |
| 277 EXPECT_CALL(*event_router.get(), | 266 EXPECT_CALL(*event_router, CallEventRouter(profile1_, kExt, kHistogramValue, |
| 278 CallEventRouter(profile1_, kExt, kHistogramValue, kEventName, | 267 kEventName, profile1_, url)); |
| 279 profile1_, url)); | 268 EXPECT_CALL(*event_router, CallEventRouter(profile2_, kExt, kHistogramValue, |
| 280 EXPECT_CALL(*event_router.get(), | 269 kEventName, profile2_, url)); |
| 281 CallEventRouter(profile2_, kExt, kHistogramValue, kEventName, | |
| 282 profile2_, url)); | |
| 283 BroadcastEventToExtension(event_router.get(), kExt, kHistogramValue, | 270 BroadcastEventToExtension(event_router.get(), kExt, kHistogramValue, |
| 284 kEventName, url); | 271 kEventName, url); |
| 285 } | 272 } |
| 286 | 273 |
| 287 TEST_F(EventRouterForwarderTest, UnicastExtensionUIRestricted) { | 274 TEST_F(EventRouterForwarderTest, UnicastExtensionUIRestricted) { |
| 288 scoped_refptr<MockEventRouterForwarder> event_router( | 275 scoped_refptr<MockEventRouterForwarder> event_router( |
| 289 new MockEventRouterForwarder); | 276 new MockEventRouterForwarder); |
| 290 using ::testing::_; | 277 using ::testing::_; |
| 291 GURL url; | 278 GURL url; |
| 292 EXPECT_CALL(*event_router.get(), | 279 EXPECT_CALL(*event_router, CallEventRouter(profile1_, kExt, kHistogramValue, |
| 293 CallEventRouter(profile1_, kExt, kHistogramValue, kEventName, | 280 kEventName, profile1_, url)); |
| 294 profile1_, url)); | 281 EXPECT_CALL(*event_router, CallEventRouter(profile2_, _, _, _, _, _)) |
| 295 EXPECT_CALL(*event_router.get(), CallEventRouter(profile2_, _, _, _, _, _)) | |
| 296 .Times(0); | 282 .Times(0); |
| 297 DispatchEventToExtension(event_router.get(), kExt, kHistogramValue, | 283 DispatchEventToExtension(event_router.get(), kExt, kHistogramValue, |
| 298 kEventName, profile1_, true, url); | 284 kEventName, profile1_, true, url); |
| 299 } | 285 } |
| 300 | 286 |
| 301 TEST_F(EventRouterForwarderTest, UnicastExtensionUIUnrestricted) { | 287 TEST_F(EventRouterForwarderTest, UnicastExtensionUIUnrestricted) { |
| 302 scoped_refptr<MockEventRouterForwarder> event_router( | 288 scoped_refptr<MockEventRouterForwarder> event_router( |
| 303 new MockEventRouterForwarder); | 289 new MockEventRouterForwarder); |
| 304 using ::testing::_; | 290 using ::testing::_; |
| 305 GURL url; | 291 GURL url; |
| 306 EXPECT_CALL( | 292 EXPECT_CALL(*event_router, CallEventRouter(profile1_, kExt, kHistogramValue, |
| 307 *event_router.get(), | 293 kEventName, NULL, url)); |
| 308 CallEventRouter(profile1_, kExt, kHistogramValue, kEventName, NULL, url)); | 294 EXPECT_CALL(*event_router, CallEventRouter(profile2_, _, _, _, _, _)) |
| 309 EXPECT_CALL(*event_router.get(), CallEventRouter(profile2_, _, _, _, _, _)) | |
| 310 .Times(0); | 295 .Times(0); |
| 311 DispatchEventToExtension(event_router.get(), kExt, kHistogramValue, | 296 DispatchEventToExtension(event_router.get(), kExt, kHistogramValue, |
| 312 kEventName, profile1_, false, url); | 297 kEventName, profile1_, false, url); |
| 313 } | 298 } |
| 314 | 299 |
| 315 } // namespace extensions | 300 } // namespace extensions |
| OLD | NEW |