OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "base/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
6 #include "base/message_loop/message_loop.h" | 6 #include "base/message_loop/message_loop.h" |
7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
8 #include "base/test/test_timeouts.h" | 8 #include "base/test/test_timeouts.h" |
9 #include "content/browser/presentation/presentation_service_impl.h" | 9 #include "content/browser/presentation/presentation_service_impl.h" |
10 #include "content/public/browser/presentation_service_delegate.h" | 10 #include "content/public/browser/presentation_service_delegate.h" |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 const std::string& presentation_id, | 88 const std::string& presentation_id, |
89 const PresentationSessionSuccessCallback& success_cb, | 89 const PresentationSessionSuccessCallback& success_cb, |
90 const PresentationSessionErrorCallback& error_cb)); | 90 const PresentationSessionErrorCallback& error_cb)); |
91 MOCK_METHOD3(ListenForSessionMessages, | 91 MOCK_METHOD3(ListenForSessionMessages, |
92 void( | 92 void( |
93 int render_process_id, | 93 int render_process_id, |
94 int render_frame_id, | 94 int render_frame_id, |
95 const PresentationSessionMessageCallback& message_cb)); | 95 const PresentationSessionMessageCallback& message_cb)); |
96 }; | 96 }; |
97 | 97 |
| 98 class MockPresentationServiceClient : |
| 99 public presentation::PresentationServiceClient { |
| 100 public: |
| 101 MOCK_METHOD1(OnScreenAvailabilityUpdated, void(bool available)); |
| 102 }; |
| 103 |
98 class PresentationServiceImplTest : public RenderViewHostImplTestHarness { | 104 class PresentationServiceImplTest : public RenderViewHostImplTestHarness { |
99 public: | 105 public: |
100 PresentationServiceImplTest() | 106 PresentationServiceImplTest() |
101 : callback_count_(0), default_session_started_count_(0) {} | 107 : default_session_started_count_(0) {} |
102 | 108 |
103 void SetUp() override { | 109 void SetUp() override { |
104 RenderViewHostImplTestHarness::SetUp(); | 110 RenderViewHostImplTestHarness::SetUp(); |
105 | 111 |
106 auto request = mojo::GetProxy(&service_ptr_); | 112 auto request = mojo::GetProxy(&service_ptr_); |
107 | |
108 EXPECT_CALL(mock_delegate_, AddObserver(_, _, _)).Times(1); | 113 EXPECT_CALL(mock_delegate_, AddObserver(_, _, _)).Times(1); |
109 service_impl_.reset(new PresentationServiceImpl( | 114 service_impl_.reset(new PresentationServiceImpl( |
110 contents()->GetMainFrame(), contents(), &mock_delegate_)); | 115 contents()->GetMainFrame(), contents(), &mock_delegate_)); |
111 service_impl_->Bind(request.Pass()); | 116 service_impl_->Bind(request.Pass()); |
| 117 |
| 118 presentation::PresentationServiceClientPtr client_ptr; |
| 119 client_binding_.reset( |
| 120 new mojo::Binding<presentation::PresentationServiceClient>( |
| 121 &mock_client_, mojo::GetProxy(&client_ptr))); |
| 122 service_impl_->SetClient(client_ptr.Pass()); |
112 } | 123 } |
113 | 124 |
114 void TearDown() override { | 125 void TearDown() override { |
115 service_ptr_.reset(); | 126 service_ptr_.reset(); |
116 if (service_impl_.get()) { | 127 if (service_impl_.get()) { |
117 EXPECT_CALL(mock_delegate_, RemoveObserver(_, _)).Times(1); | 128 EXPECT_CALL(mock_delegate_, RemoveObserver(_, _)).Times(1); |
118 service_impl_.reset(); | 129 service_impl_.reset(); |
119 } | 130 } |
120 RenderViewHostImplTestHarness::TearDown(); | 131 RenderViewHostImplTestHarness::TearDown(); |
121 } | 132 } |
122 | 133 |
123 void ListenForScreenAvailabilityAndWait( | 134 void ListenForScreenAvailabilityAndWait(bool delegate_success) { |
124 const std::string& presentation_url, | |
125 const base::Callback<void(const std::string&, bool)>& callback, | |
126 bool delegate_success) { | |
127 base::RunLoop run_loop; | 135 base::RunLoop run_loop; |
128 // This will call to |service_impl_| via mojo. Process the message | 136 // This will call to |service_impl_| via mojo. Process the message |
129 // using RunLoop. | 137 // using RunLoop. |
130 // The callback shouldn't be invoked since there is no availability | 138 // The callback shouldn't be invoked since there is no availability |
131 // result yet. | 139 // result yet. |
132 EXPECT_CALL(mock_delegate_, AddScreenAvailabilityListener(_, _, _)) | 140 EXPECT_CALL(mock_delegate_, AddScreenAvailabilityListener(_, _, _)) |
133 .WillOnce(DoAll( | 141 .WillOnce(DoAll( |
134 InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit), | 142 InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit), |
135 Return(delegate_success))); | 143 Return(delegate_success))); |
136 service_ptr_->ListenForScreenAvailability(presentation_url, callback); | 144 service_ptr_->ListenForScreenAvailability(); |
137 run_loop.Run(); | 145 run_loop.Run(); |
138 | 146 |
139 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_delegate_)); | 147 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_delegate_)); |
140 } | 148 } |
141 | 149 |
142 void ExpectListenerDoesNotExist(const std::string& presentation_url) { | |
143 const auto& contexts = service_impl_->availability_contexts_; | |
144 auto it = contexts.find(presentation_url); | |
145 EXPECT_TRUE(it == contexts.end()); | |
146 } | |
147 | |
148 void RunLoopFor(base::TimeDelta duration) { | 150 void RunLoopFor(base::TimeDelta duration) { |
149 base::RunLoop run_loop; | 151 base::RunLoop run_loop; |
150 base::MessageLoop::current()->PostDelayedTask( | 152 base::MessageLoop::current()->PostDelayedTask( |
151 FROM_HERE, run_loop.QuitClosure(), duration); | 153 FROM_HERE, run_loop.QuitClosure(), duration); |
152 run_loop.Run(); | 154 run_loop.Run(); |
153 } | 155 } |
154 | 156 |
155 void SaveQuitClosureAndRunLoop() { | 157 void SaveQuitClosureAndRunLoop() { |
156 base::RunLoop run_loop; | 158 base::RunLoop run_loop; |
157 run_loop_quit_closure_ = run_loop.QuitClosure(); | 159 run_loop_quit_closure_ = run_loop.QuitClosure(); |
158 run_loop.Run(); | 160 run_loop.Run(); |
159 run_loop_quit_closure_.Reset(); | 161 run_loop_quit_closure_.Reset(); |
160 } | 162 } |
161 | 163 |
162 void ShouldNotBeCalled(const std::string& presentation_url, bool available) { | 164 void SimulateScreenAvailabilityChangeAndWait(bool available) { |
163 FAIL() << "Callback unexpectedly invoked with " | 165 auto* listener = service_impl_->screen_availability_listener_.get(); |
164 << "url = " << presentation_url << ", available = " << available; | 166 ASSERT_TRUE(listener); |
165 } | |
166 | 167 |
167 void SimulateScreenAvailabilityChange( | 168 base::RunLoop run_loop; |
168 const std::string& presentation_url, bool available) { | 169 EXPECT_CALL(mock_client_, OnScreenAvailabilityUpdated(available)) |
169 const auto& contexts = service_impl_->availability_contexts_; | 170 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); |
170 auto it = contexts.find(presentation_url); | 171 listener->OnScreenAvailabilityChanged(available); |
171 ASSERT_TRUE(it != contexts.end()); | 172 run_loop.Run(); |
172 it->second->OnScreenAvailabilityChanged(available); | |
173 } | |
174 | |
175 void ScreenAvailabilityChangedCallback( | |
176 bool expected, | |
177 const std::string& presentation_url, | |
178 bool available) { | |
179 ++callback_count_; | |
180 EXPECT_EQ(expected, available); | |
181 if (!run_loop_quit_closure_.is_null()) | |
182 run_loop_quit_closure_.Run(); | |
183 } | 173 } |
184 | 174 |
185 void ExpectReset() { | 175 void ExpectReset() { |
186 EXPECT_CALL(mock_delegate_, Reset(_, _)).Times(1); | 176 EXPECT_CALL(mock_delegate_, Reset(_, _)).Times(1); |
187 } | 177 } |
188 | 178 |
189 void ExpectCleanState() { | 179 void ExpectCleanState() { |
190 EXPECT_TRUE(service_impl_->availability_contexts_.empty()); | |
191 EXPECT_TRUE(service_impl_->default_presentation_url_.empty()); | 180 EXPECT_TRUE(service_impl_->default_presentation_url_.empty()); |
192 EXPECT_TRUE(service_impl_->default_presentation_id_.empty()); | 181 EXPECT_TRUE(service_impl_->default_presentation_id_.empty()); |
193 EXPECT_TRUE(service_impl_->queued_start_session_requests_.empty()); | 182 EXPECT_TRUE(service_impl_->queued_start_session_requests_.empty()); |
| 183 EXPECT_FALSE(service_impl_->screen_availability_listener_.get()); |
194 EXPECT_FALSE(service_impl_->default_session_start_context_.get()); | 184 EXPECT_FALSE(service_impl_->default_session_start_context_.get()); |
195 EXPECT_FALSE(service_impl_->on_session_messages_callback_.get()); | 185 EXPECT_FALSE(service_impl_->on_session_messages_callback_.get()); |
196 } | 186 } |
197 | 187 |
198 void ExpectNewSessionMojoCallbackSuccess( | 188 void ExpectNewSessionMojoCallbackSuccess( |
199 presentation::PresentationSessionInfoPtr info, | 189 presentation::PresentationSessionInfoPtr info, |
200 presentation::PresentationErrorPtr error) { | 190 presentation::PresentationErrorPtr error) { |
201 EXPECT_FALSE(info.is_null()); | 191 EXPECT_FALSE(info.is_null()); |
202 EXPECT_TRUE(error.is_null()); | 192 EXPECT_TRUE(error.is_null()); |
203 if (!run_loop_quit_closure_.is_null()) | 193 if (!run_loop_quit_closure_.is_null()) |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 messages->push_back( | 273 messages->push_back( |
284 content::PresentationSessionMessage::CreateBinaryMessage( | 274 content::PresentationSessionMessage::CreateBinaryMessage( |
285 presentation_url, presentation_id, | 275 presentation_url, presentation_id, |
286 scoped_ptr<std::vector<uint8_t>>( | 276 scoped_ptr<std::vector<uint8_t>>( |
287 new std::vector<uint8_t>(binary_data)))); | 277 new std::vector<uint8_t>(binary_data)))); |
288 message_cb.Run(messages.Pass()); | 278 message_cb.Run(messages.Pass()); |
289 SaveQuitClosureAndRunLoop(); | 279 SaveQuitClosureAndRunLoop(); |
290 } | 280 } |
291 | 281 |
292 MockPresentationServiceDelegate mock_delegate_; | 282 MockPresentationServiceDelegate mock_delegate_; |
| 283 |
293 scoped_ptr<PresentationServiceImpl> service_impl_; | 284 scoped_ptr<PresentationServiceImpl> service_impl_; |
294 mojo::InterfacePtr<presentation::PresentationService> service_ptr_; | 285 mojo::InterfacePtr<presentation::PresentationService> service_ptr_; |
| 286 |
| 287 MockPresentationServiceClient mock_client_; |
| 288 scoped_ptr<mojo::Binding<presentation::PresentationServiceClient>> |
| 289 client_binding_; |
| 290 |
295 base::Closure run_loop_quit_closure_; | 291 base::Closure run_loop_quit_closure_; |
296 int callback_count_; | |
297 int default_session_started_count_; | 292 int default_session_started_count_; |
298 mojo::Array<presentation::SessionMessagePtr> expected_msgs_; | 293 mojo::Array<presentation::SessionMessagePtr> expected_msgs_; |
299 }; | 294 }; |
300 | 295 |
301 TEST_F(PresentationServiceImplTest, ListenForScreenAvailability) { | 296 TEST_F(PresentationServiceImplTest, ListenForScreenAvailability) { |
302 std::string presentation_url("http://fooUrl"); | 297 ListenForScreenAvailabilityAndWait(true); |
303 ListenForScreenAvailabilityAndWait( | |
304 presentation_url, | |
305 base::Bind( | |
306 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback, | |
307 base::Unretained(this), true), | |
308 true); | |
309 | 298 |
310 // Different presentation URL. | 299 SimulateScreenAvailabilityChangeAndWait(true); |
311 ListenForScreenAvailabilityAndWait( | 300 SimulateScreenAvailabilityChangeAndWait(false); |
312 "http://barUrl", | 301 SimulateScreenAvailabilityChangeAndWait(true); |
313 base::Bind(&PresentationServiceImplTest::ShouldNotBeCalled, | |
314 base::Unretained(this)), | |
315 true); | |
316 | |
317 // Result now available; callback will be invoked with availability result. | |
318 SimulateScreenAvailabilityChange(presentation_url, true); | |
319 SaveQuitClosureAndRunLoop(); | |
320 | |
321 EXPECT_EQ(1, callback_count_); | |
322 | |
323 // Result updated but callback not invoked since it's been erased. | |
324 SimulateScreenAvailabilityChange(presentation_url, false); | |
325 | |
326 // Register another callback which should immediately invoke callback | |
327 // since updated result is available. | |
328 service_ptr_->ListenForScreenAvailability( | |
329 presentation_url, | |
330 base::Bind( | |
331 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback, | |
332 base::Unretained(this), | |
333 false)); | |
334 SaveQuitClosureAndRunLoop(); | |
335 EXPECT_EQ(2, callback_count_); | |
336 } | 302 } |
337 | 303 |
338 TEST_F(PresentationServiceImplTest, Reset) { | 304 TEST_F(PresentationServiceImplTest, Reset) { |
339 std::string presentation_url("http://fooUrl"); | 305 ListenForScreenAvailabilityAndWait(true); |
340 ListenForScreenAvailabilityAndWait( | |
341 presentation_url, | |
342 base::Bind(&PresentationServiceImplTest::ShouldNotBeCalled, | |
343 base::Unretained(this)), | |
344 true); | |
345 | 306 |
346 ExpectReset(); | 307 ExpectReset(); |
347 service_impl_->Reset(); | 308 service_impl_->Reset(); |
348 ExpectCleanState(); | 309 ExpectCleanState(); |
349 | |
350 EXPECT_EQ(0, callback_count_); | |
351 } | 310 } |
352 | 311 |
353 TEST_F(PresentationServiceImplTest, DidNavigateThisFrame) { | 312 TEST_F(PresentationServiceImplTest, DidNavigateThisFrame) { |
354 std::string presentation_url("http://fooUrl"); | 313 ListenForScreenAvailabilityAndWait(true); |
355 ListenForScreenAvailabilityAndWait( | |
356 presentation_url, | |
357 base::Bind(&PresentationServiceImplTest::ShouldNotBeCalled, | |
358 base::Unretained(this)), | |
359 true); | |
360 | 314 |
361 ExpectReset(); | 315 ExpectReset(); |
362 service_impl_->DidNavigateAnyFrame( | 316 service_impl_->DidNavigateAnyFrame( |
363 contents()->GetMainFrame(), | 317 contents()->GetMainFrame(), |
364 content::LoadCommittedDetails(), | 318 content::LoadCommittedDetails(), |
365 content::FrameNavigateParams()); | 319 content::FrameNavigateParams()); |
366 ExpectCleanState(); | 320 ExpectCleanState(); |
367 } | 321 } |
368 | 322 |
369 TEST_F(PresentationServiceImplTest, DidNavigateNotThisFrame) { | 323 TEST_F(PresentationServiceImplTest, DidNavigateNotThisFrame) { |
370 std::string presentation_url("http://fooUrl"); | 324 ListenForScreenAvailabilityAndWait(true); |
371 ListenForScreenAvailabilityAndWait( | |
372 presentation_url, | |
373 base::Bind( | |
374 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback, | |
375 base::Unretained(this), | |
376 true), | |
377 true); | |
378 | 325 |
379 // TODO(imcheng): How to get a different RenderFrameHost? | 326 // TODO(imcheng): How to get a different RenderFrameHost? |
380 service_impl_->DidNavigateAnyFrame( | 327 service_impl_->DidNavigateAnyFrame( |
381 nullptr, | 328 nullptr, |
382 content::LoadCommittedDetails(), | 329 content::LoadCommittedDetails(), |
383 content::FrameNavigateParams()); | 330 content::FrameNavigateParams()); |
384 | 331 |
385 // Availability is reported and callback is invoked since it was not | 332 // Availability is reported and callback is invoked since it was not |
386 // removed. | 333 // removed. |
387 SimulateScreenAvailabilityChange(presentation_url, true); | 334 SimulateScreenAvailabilityChangeAndWait(true); |
388 SaveQuitClosureAndRunLoop(); | |
389 EXPECT_EQ(1, callback_count_); | |
390 } | 335 } |
391 | 336 |
392 TEST_F(PresentationServiceImplTest, ThisRenderFrameDeleted) { | 337 TEST_F(PresentationServiceImplTest, ThisRenderFrameDeleted) { |
393 std::string presentation_url("http://fooUrl"); | 338 ListenForScreenAvailabilityAndWait(true); |
394 ListenForScreenAvailabilityAndWait( | |
395 presentation_url, | |
396 base::Bind(&PresentationServiceImplTest::ShouldNotBeCalled, | |
397 base::Unretained(this)), | |
398 true); | |
399 | 339 |
400 ExpectReset(); | 340 ExpectReset(); |
401 | 341 |
402 // Since the frame matched the service, |service_impl_| will be deleted. | 342 // Since the frame matched the service, |service_impl_| will be deleted. |
403 PresentationServiceImpl* service = service_impl_.release(); | 343 PresentationServiceImpl* service = service_impl_.release(); |
404 EXPECT_CALL(mock_delegate_, RemoveObserver(_, _)).Times(1); | 344 EXPECT_CALL(mock_delegate_, RemoveObserver(_, _)).Times(1); |
405 service->RenderFrameDeleted(contents()->GetMainFrame()); | 345 service->RenderFrameDeleted(contents()->GetMainFrame()); |
406 } | 346 } |
407 | 347 |
408 TEST_F(PresentationServiceImplTest, NotThisRenderFrameDeleted) { | 348 TEST_F(PresentationServiceImplTest, NotThisRenderFrameDeleted) { |
409 std::string presentation_url("http://fooUrl"); | 349 ListenForScreenAvailabilityAndWait(true); |
410 ListenForScreenAvailabilityAndWait( | |
411 presentation_url, | |
412 base::Bind( | |
413 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback, | |
414 base::Unretained(this), | |
415 true), | |
416 true); | |
417 | 350 |
418 // TODO(imcheng): How to get a different RenderFrameHost? | 351 // TODO(imcheng): How to get a different RenderFrameHost? |
419 service_impl_->RenderFrameDeleted(nullptr); | 352 service_impl_->RenderFrameDeleted(nullptr); |
420 | 353 |
421 // Availability is reported and callback should be invoked since listener | 354 // Availability is reported and callback should be invoked since listener |
422 // has not been deleted. | 355 // has not been deleted. |
423 SimulateScreenAvailabilityChange(presentation_url, true); | 356 SimulateScreenAvailabilityChangeAndWait(true); |
424 SaveQuitClosureAndRunLoop(); | |
425 EXPECT_EQ(1, callback_count_); | |
426 } | |
427 | |
428 TEST_F(PresentationServiceImplTest, ListenForScreenAvailabilityTwice) { | |
429 std::string presentation_url("http://fooUrl"); | |
430 ListenForScreenAvailabilityAndWait( | |
431 presentation_url, | |
432 base::Bind( | |
433 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback, | |
434 base::Unretained(this), | |
435 false), | |
436 true); | |
437 | |
438 // Second call should overwrite the callback from first call. | |
439 // It shouldn't result in an extra call to delegate. | |
440 service_ptr_->ListenForScreenAvailability( | |
441 presentation_url, | |
442 base::Bind( | |
443 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback, | |
444 base::Unretained(this), | |
445 false)); | |
446 | |
447 // Cannot use ListenForScreenAvailabilityAndWait here since the mock delegate | |
448 // won't be triggered again to quit the RunLoop. | |
449 RunLoopFor(base::TimeDelta::FromMilliseconds(50)); | |
450 | |
451 // Result now available; callback will be invoked with availability result. | |
452 SimulateScreenAvailabilityChange(presentation_url, false); | |
453 SaveQuitClosureAndRunLoop(); | |
454 | |
455 EXPECT_EQ(2, callback_count_); | |
456 } | 357 } |
457 | 358 |
458 TEST_F(PresentationServiceImplTest, DelegateFails) { | 359 TEST_F(PresentationServiceImplTest, DelegateFails) { |
459 std::string presentation_url("http://fooUrl"); | 360 ListenForScreenAvailabilityAndWait(false); |
460 ListenForScreenAvailabilityAndWait( | 361 ASSERT_FALSE(service_impl_->screen_availability_listener_.get()); |
461 presentation_url, | |
462 base::Bind(&PresentationServiceImplTest::ShouldNotBeCalled, | |
463 base::Unretained(this)), | |
464 false); | |
465 | |
466 ExpectListenerDoesNotExist(presentation_url); | |
467 } | 362 } |
468 | 363 |
469 TEST_F(PresentationServiceImplTest, SetDefaultPresentationUrl) { | 364 TEST_F(PresentationServiceImplTest, SetDefaultPresentationUrl) { |
470 std::string url1("http://fooUrl"); | 365 std::string url1("http://fooUrl"); |
471 std::string dpu_id("dpuId"); | 366 std::string dpu_id("dpuId"); |
472 EXPECT_CALL(mock_delegate_, | 367 EXPECT_CALL(mock_delegate_, |
473 SetDefaultPresentationUrl(_, _, Eq(url1), Eq(dpu_id))) | 368 SetDefaultPresentationUrl(_, _, Eq(url1), Eq(dpu_id))) |
474 .Times(1); | 369 .Times(1); |
475 service_impl_->SetDefaultPresentationURL(url1, dpu_id); | 370 service_impl_->SetDefaultPresentationURL(url1, dpu_id); |
476 EXPECT_EQ(url1, service_impl_->default_presentation_url_); | 371 EXPECT_EQ(url1, service_impl_->default_presentation_url_); |
477 | 372 |
478 // Now there should be a callback registered with the DPU. | 373 // Now there should be a listener for DPU = |url1|. |
479 ListenForScreenAvailabilityAndWait( | 374 ListenForScreenAvailabilityAndWait(true); |
480 url1, | 375 auto* listener = service_impl_->screen_availability_listener_.get(); |
481 base::Bind( | 376 ASSERT_TRUE(listener); |
482 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback, | 377 EXPECT_EQ(url1, listener->GetPresentationUrl()); |
483 base::Unretained(this), true), | |
484 true); | |
485 const auto& contexts = service_impl_->availability_contexts_; | |
486 auto it = contexts.find(url1); | |
487 ASSERT_TRUE(it != contexts.end()); | |
488 EXPECT_TRUE(it->second->HasPendingCallbacks()); | |
489 | 378 |
490 std::string url2("http://barUrl"); | 379 std::string url2("http://barUrl"); |
491 // Sets different DPU. | 380 // Sets different DPU. |
492 // Adds listener for url2 and removes listener for url1. | 381 // Adds listener for url2 and removes listener for url1. |
493 // Also, the callback from url1 is transferred to url2. | |
494 EXPECT_CALL( | 382 EXPECT_CALL( |
495 mock_delegate_, | 383 mock_delegate_, |
496 AddScreenAvailabilityListener(_, _, _)) | 384 AddScreenAvailabilityListener(_, _, _)) |
497 .WillOnce(Return(true)); | 385 .WillOnce(Return(true)); |
498 EXPECT_CALL( | 386 EXPECT_CALL( |
499 mock_delegate_, | 387 mock_delegate_, |
500 RemoveScreenAvailabilityListener(_, _, _)) | 388 RemoveScreenAvailabilityListener(_, _, _)) |
501 .Times(1); | 389 .Times(1); |
502 EXPECT_CALL(mock_delegate_, | 390 EXPECT_CALL(mock_delegate_, |
503 SetDefaultPresentationUrl(_, _, Eq(url2), Eq(dpu_id))) | 391 SetDefaultPresentationUrl(_, _, Eq(url2), Eq(dpu_id))) |
504 .Times(1); | 392 .Times(1); |
505 service_impl_->SetDefaultPresentationURL(url2, dpu_id); | 393 service_impl_->SetDefaultPresentationURL(url2, dpu_id); |
506 EXPECT_EQ(url2, service_impl_->default_presentation_url_); | 394 EXPECT_EQ(url2, service_impl_->default_presentation_url_); |
507 | 395 |
508 it = contexts.find(url2); | 396 listener = service_impl_->screen_availability_listener_.get(); |
509 ASSERT_TRUE(it != contexts.end()); | 397 ASSERT_TRUE(listener); |
510 EXPECT_TRUE(it->second->HasPendingCallbacks()); | 398 EXPECT_EQ(url2, listener->GetPresentationUrl()); |
511 } | 399 } |
512 | 400 |
513 TEST_F(PresentationServiceImplTest, SetSameDefaultPresentationUrl) { | 401 TEST_F(PresentationServiceImplTest, SetSameDefaultPresentationUrl) { |
514 std::string url("http://fooUrl"); | 402 std::string url("http://fooUrl"); |
515 std::string dpu_id("dpuId"); | 403 std::string dpu_id("dpuId"); |
516 EXPECT_CALL(mock_delegate_, | 404 EXPECT_CALL(mock_delegate_, |
517 SetDefaultPresentationUrl(_, _, Eq(url), Eq(dpu_id))) | 405 SetDefaultPresentationUrl(_, _, Eq(url), Eq(dpu_id))) |
518 .Times(1); | 406 .Times(1); |
519 service_impl_->SetDefaultPresentationURL(url, dpu_id); | 407 service_impl_->SetDefaultPresentationURL(url, dpu_id); |
520 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_delegate_)); | 408 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_delegate_)); |
521 EXPECT_EQ(url, service_impl_->default_presentation_url_); | 409 EXPECT_EQ(url, service_impl_->default_presentation_url_); |
522 | 410 |
523 // Same URL as before; no-ops. | 411 // Same URL as before; no-ops. |
524 service_impl_->SetDefaultPresentationURL(url, dpu_id); | 412 service_impl_->SetDefaultPresentationURL(url, dpu_id); |
525 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_delegate_)); | 413 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_delegate_)); |
526 EXPECT_EQ(url, service_impl_->default_presentation_url_); | 414 EXPECT_EQ(url, service_impl_->default_presentation_url_); |
527 } | 415 } |
528 | 416 |
529 TEST_F(PresentationServiceImplTest, ClearDefaultPresentationUrl) { | |
530 std::string url("http://fooUrl"); | |
531 std::string dpu_id("dpuId"); | |
532 EXPECT_CALL(mock_delegate_, | |
533 SetDefaultPresentationUrl(_, _, Eq(url), Eq(dpu_id))) | |
534 .Times(1); | |
535 service_impl_->SetDefaultPresentationURL(url, dpu_id); | |
536 EXPECT_EQ(url, service_impl_->default_presentation_url_); | |
537 | |
538 // Now there should be a callback registered with the DPU. | |
539 ListenForScreenAvailabilityAndWait( | |
540 url, | |
541 base::Bind( | |
542 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback, | |
543 base::Unretained(this), true), | |
544 true); | |
545 | |
546 const auto& contexts = service_impl_->availability_contexts_; | |
547 auto it = contexts.find(url); | |
548 ASSERT_TRUE(it != contexts.end()); | |
549 EXPECT_TRUE(it->second->HasPendingCallbacks()); | |
550 | |
551 // Clears the default presentation URL. Transfers the listener from url to | |
552 // "1-UA" mode. | |
553 EXPECT_CALL( | |
554 mock_delegate_, | |
555 AddScreenAvailabilityListener(_, _, _)) | |
556 .WillOnce(Return(true)); | |
557 EXPECT_CALL( | |
558 mock_delegate_, | |
559 RemoveScreenAvailabilityListener(_, _, _)) | |
560 .Times(1); | |
561 EXPECT_CALL( | |
562 mock_delegate_, | |
563 SetDefaultPresentationUrl(_, _, Eq(std::string()), Eq(std::string()))) | |
564 .Times(1); | |
565 service_impl_->SetDefaultPresentationURL(std::string(), std::string()); | |
566 EXPECT_TRUE(service_impl_->default_presentation_url_.empty()); | |
567 | |
568 it = contexts.find(url); | |
569 ASSERT_TRUE(it == contexts.end()); | |
570 } | |
571 | |
572 TEST_F(PresentationServiceImplTest, StartSessionSuccess) { | 417 TEST_F(PresentationServiceImplTest, StartSessionSuccess) { |
573 std::string presentation_url("http://fooUrl"); | 418 std::string presentation_url("http://fooUrl"); |
574 std::string presentation_id("presentationId"); | 419 std::string presentation_id("presentationId"); |
575 service_ptr_->StartSession( | 420 service_ptr_->StartSession( |
576 presentation_url, | 421 presentation_url, |
577 presentation_id, | 422 presentation_id, |
578 base::Bind( | 423 base::Bind( |
579 &PresentationServiceImplTest::ExpectNewSessionMojoCallbackSuccess, | 424 &PresentationServiceImplTest::ExpectNewSessionMojoCallbackSuccess, |
580 base::Unretained(this))); | 425 base::Unretained(this))); |
581 base::RunLoop run_loop; | 426 base::RunLoop run_loop; |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
777 RunLoopFor(TestTimeouts::tiny_timeout()); | 622 RunLoopFor(TestTimeouts::tiny_timeout()); |
778 | 623 |
779 ExpectReset(); | 624 ExpectReset(); |
780 service_impl_->Reset(); | 625 service_impl_->Reset(); |
781 ExpectCleanState(); | 626 ExpectCleanState(); |
782 SaveQuitClosureAndRunLoop(); | 627 SaveQuitClosureAndRunLoop(); |
783 EXPECT_EQ(1, default_session_started_count_); | 628 EXPECT_EQ(1, default_session_started_count_); |
784 } | 629 } |
785 | 630 |
786 } // namespace content | 631 } // namespace content |
OLD | NEW |