| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 <memory> | 5 #include <memory> |
| 6 #include <utility> | 6 #include <utility> |
| 7 | 7 |
| 8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "content/public/test/test_browser_thread_bundle.h" | 9 #include "content/public/test/test_browser_thread_bundle.h" |
| 10 #include "content/renderer/presentation/presentation_dispatcher.h" | 10 #include "content/renderer/presentation/presentation_dispatcher.h" |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 // - Messages received | 41 // - Messages received |
| 42 // - Discarding queued messages when the frame navigates | 42 // - Discarding queued messages when the frame navigates |
| 43 // - Screen availability not supported | 43 // - Screen availability not supported |
| 44 // - Default presentation starting | 44 // - Default presentation starting |
| 45 | 45 |
| 46 namespace content { | 46 namespace content { |
| 47 | 47 |
| 48 class MockPresentationAvailabilityObserver | 48 class MockPresentationAvailabilityObserver |
| 49 : public WebPresentationAvailabilityObserver { | 49 : public WebPresentationAvailabilityObserver { |
| 50 public: | 50 public: |
| 51 explicit MockPresentationAvailabilityObserver(const WebVector<WebURL>& urls) | 51 explicit MockPresentationAvailabilityObserver(const std::vector<GURL>& urls) |
| 52 : urls_(urls) {} | 52 : urls_(urls) {} |
| 53 ~MockPresentationAvailabilityObserver() override {} | 53 ~MockPresentationAvailabilityObserver() override {} |
| 54 | 54 |
| 55 MOCK_METHOD1(availabilityChanged, void(bool is_available)); | 55 MOCK_METHOD1(availabilityChanged, void(bool is_available)); |
| 56 const WebVector<WebURL>& urls() const override { return urls_; } | 56 const WebVector<WebURL>& urls() const override { return urls_; } |
| 57 | 57 |
| 58 private: | 58 private: |
| 59 const WebVector<WebURL> urls_; | 59 const WebVector<WebURL> urls_; |
| 60 }; | 60 }; |
| 61 | 61 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 blink::mojom::PresentationConnectionPtr controller_conn_ptr, | 110 blink::mojom::PresentationConnectionPtr controller_conn_ptr, |
| 111 blink::mojom::PresentationConnectionRequest receiver_conn_request) | 111 blink::mojom::PresentationConnectionRequest receiver_conn_request) |
| 112 override { | 112 override { |
| 113 SetPresentationConnection(session.get(), controller_conn_ptr.get()); | 113 SetPresentationConnection(session.get(), controller_conn_ptr.get()); |
| 114 } | 114 } |
| 115 MOCK_METHOD2(SetPresentationConnection, | 115 MOCK_METHOD2(SetPresentationConnection, |
| 116 void(PresentationSessionInfo* session_info, | 116 void(PresentationSessionInfo* session_info, |
| 117 PresentationConnection* connection)); | 117 PresentationConnection* connection)); |
| 118 }; | 118 }; |
| 119 | 119 |
| 120 class MockPresentationAvailabilityCallbacks |
| 121 : public blink::WebCallbacks<bool, const blink::WebPresentationError&> { |
| 122 public: |
| 123 MOCK_METHOD1(onSuccess, void(bool value)); |
| 124 MOCK_METHOD1(onError, void(const blink::WebPresentationError&)); |
| 125 }; |
| 126 |
| 120 class TestWebPresentationConnectionCallback | 127 class TestWebPresentationConnectionCallback |
| 121 : public WebPresentationConnectionCallback { | 128 : public WebPresentationConnectionCallback { |
| 122 public: | 129 public: |
| 123 TestWebPresentationConnectionCallback(WebURL url, WebString id) | 130 TestWebPresentationConnectionCallback(WebURL url, WebString id) |
| 124 : url_(url), id_(id), callback_called_(false) {} | 131 : url_(url), id_(id), callback_called_(false) {} |
| 125 ~TestWebPresentationConnectionCallback() override { | 132 ~TestWebPresentationConnectionCallback() override { |
| 126 EXPECT_TRUE(callback_called_); | 133 EXPECT_TRUE(callback_called_); |
| 127 } | 134 } |
| 128 | 135 |
| 129 void onSuccess(const WebPresentationSessionInfo& info) override { | 136 void onSuccess(const WebPresentationSessionInfo& info) override { |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 mojo::MakeRequest(&presentation_service_)); | 184 mojo::MakeRequest(&presentation_service_)); |
| 178 } | 185 } |
| 179 } | 186 } |
| 180 | 187 |
| 181 MockPresentationService* mock_presentation_service_; | 188 MockPresentationService* mock_presentation_service_; |
| 182 std::unique_ptr<mojo::Binding<PresentationService>> mock_binding_; | 189 std::unique_ptr<mojo::Binding<PresentationService>> mock_binding_; |
| 183 }; | 190 }; |
| 184 | 191 |
| 185 class PresentationDispatcherTest : public ::testing::Test { | 192 class PresentationDispatcherTest : public ::testing::Test { |
| 186 public: | 193 public: |
| 194 enum class URLState { Available, Unavailable, Unsupported, Unknown }; |
| 195 |
| 187 PresentationDispatcherTest() | 196 PresentationDispatcherTest() |
| 188 : gurl1_(GURL("https://www.example.com/1.html")), | 197 : gurl1_(GURL("https://www.example.com/1.html")), |
| 189 gurl2_(GURL("https://www.example.com/2.html")), | 198 gurl2_(GURL("https://www.example.com/2.html")), |
| 190 gurls_({gurl1_, gurl2_}), | 199 gurl3_(GURL("https://www.example.com/3.html")), |
| 200 gurl4_(GURL("https://www.example.com/4.html")), |
| 201 gurls_({gurl1_, gurl2_, gurl3_, gurl4_}), |
| 191 url1_(WebURL(gurl1_)), | 202 url1_(WebURL(gurl1_)), |
| 192 url2_(WebURL(gurl2_)), | 203 url2_(WebURL(gurl2_)), |
| 204 url3_(WebURL(gurl3_)), |
| 205 url4_(WebURL(gurl4_)), |
| 193 urls_(WebVector<WebURL>(gurls_)), | 206 urls_(WebVector<WebURL>(gurls_)), |
| 194 presentation_id_(WebString::fromUTF8("test-id")), | 207 presentation_id_(WebString::fromUTF8("test-id")), |
| 195 array_buffer_(WebArrayBuffer::create(4, 1)), | 208 array_buffer_(WebArrayBuffer::create(4, 1)), |
| 196 observer_(urls_), | 209 observer_(gurls_), |
| 210 mock_observer1_({gurl1_, gurl2_, gurl3_}), |
| 211 mock_observer2_({gurl2_, gurl3_, gurl4_}), |
| 212 mock_observer3_({gurl2_, gurl3_}), |
| 213 mock_observers_({&mock_observer1_, &mock_observer2_, &mock_observer3_}), |
| 197 dispatcher_(&presentation_service_) {} | 214 dispatcher_(&presentation_service_) {} |
| 215 |
| 198 ~PresentationDispatcherTest() override {} | 216 ~PresentationDispatcherTest() override {} |
| 199 | 217 |
| 200 void SetUp() override { | 218 void SetUp() override { |
| 201 // Set some test data. | 219 // Set some test data. |
| 202 *array_buffer_data() = 42; | 220 *array_buffer_data() = 42; |
| 203 } | 221 } |
| 204 | 222 |
| 205 uint8_t* array_buffer_data() { | 223 uint8_t* array_buffer_data() { |
| 206 return static_cast<uint8_t*>(array_buffer_.data()); | 224 return static_cast<uint8_t*>(array_buffer_.data()); |
| 207 } | 225 } |
| 208 | 226 |
| 227 void ChangeURLState(const GURL& url, URLState state) { |
| 228 switch (state) { |
| 229 case URLState::Available: |
| 230 dispatcher_.OnScreenAvailabilityUpdated(url, true); |
| 231 break; |
| 232 case URLState::Unavailable: |
| 233 dispatcher_.OnScreenAvailabilityUpdated(url, false); |
| 234 break; |
| 235 case URLState::Unsupported: |
| 236 dispatcher_.OnScreenAvailabilityNotSupported(url); |
| 237 break; |
| 238 case URLState::Unknown: |
| 239 break; |
| 240 } |
| 241 } |
| 242 |
| 243 // Tests that PresenationService is called for getAvailability(urls), after |
| 244 // |urls| change state to |states|. This function takes ownership of |
| 245 // |mock_callback|. |
| 246 void TestGetAvailability( |
| 247 const std::vector<GURL>& urls, |
| 248 const std::vector<URLState>& states, |
| 249 MockPresentationAvailabilityCallbacks* mock_callback) { |
| 250 DCHECK_EQ(urls.size(), states.size()); |
| 251 |
| 252 for (const auto& url : urls) { |
| 253 EXPECT_CALL(presentation_service_, ListenForScreenAvailability(url)) |
| 254 .Times(1); |
| 255 EXPECT_CALL(presentation_service_, |
| 256 StopListeningForScreenAvailability(url)) |
| 257 .Times(1); |
| 258 } |
| 259 |
| 260 base::RunLoop run_loop; |
| 261 client()->getAvailability(urls, base::WrapUnique(mock_callback)); |
| 262 for (size_t i = 0; i < urls.size(); i++) |
| 263 ChangeURLState(urls[i], states[i]); |
| 264 |
| 265 run_loop.RunUntilIdle(); |
| 266 } |
| 267 |
| 268 blink::WebPresentationClient* client() { return &dispatcher_; } |
| 269 |
| 209 protected: | 270 protected: |
| 210 const GURL gurl1_; | 271 const GURL gurl1_; |
| 211 const GURL gurl2_; | 272 const GURL gurl2_; |
| 273 const GURL gurl3_; |
| 274 const GURL gurl4_; |
| 212 const std::vector<GURL> gurls_; | 275 const std::vector<GURL> gurls_; |
| 213 const WebURL url1_; | 276 const WebURL url1_; |
| 214 const WebURL url2_; | 277 const WebURL url2_; |
| 278 const WebURL url3_; |
| 279 const WebURL url4_; |
| 215 const WebVector<WebURL> urls_; | 280 const WebVector<WebURL> urls_; |
| 216 const WebString presentation_id_; | 281 const WebString presentation_id_; |
| 217 const WebArrayBuffer array_buffer_; | 282 const WebArrayBuffer array_buffer_; |
| 218 MockPresentationAvailabilityObserver observer_; | 283 MockPresentationAvailabilityObserver observer_; |
| 284 MockPresentationAvailabilityObserver mock_observer1_; |
| 285 MockPresentationAvailabilityObserver mock_observer2_; |
| 286 MockPresentationAvailabilityObserver mock_observer3_; |
| 287 std::vector<MockPresentationAvailabilityObserver*> mock_observers_; |
| 288 |
| 219 MockPresentationService presentation_service_; | 289 MockPresentationService presentation_service_; |
| 220 TestPresentationDispatcher dispatcher_; | 290 TestPresentationDispatcher dispatcher_; |
| 221 | 291 |
| 222 private: | 292 private: |
| 223 content::TestBrowserThreadBundle thread_bundle_; | 293 content::TestBrowserThreadBundle thread_bundle_; |
| 224 }; | 294 }; |
| 225 | 295 |
| 226 TEST_F(PresentationDispatcherTest, TestStartSession) { | 296 TEST_F(PresentationDispatcherTest, TestStartSession) { |
| 227 base::RunLoop run_loop; | 297 base::RunLoop run_loop; |
| 228 | 298 |
| 229 EXPECT_CALL(presentation_service_, StartSession(gurls_, _)) | 299 EXPECT_CALL(presentation_service_, StartSession(gurls_, _)) |
| 230 .WillOnce(Invoke([this]( | 300 .WillOnce(Invoke([this]( |
| 231 const std::vector<GURL>& presentation_urls, | 301 const std::vector<GURL>& presentation_urls, |
| 232 const PresentationService::StartSessionCallback& callback) { | 302 const PresentationService::StartSessionCallback& callback) { |
| 233 PresentationSessionInfoPtr session_info(PresentationSessionInfo::New()); | 303 PresentationSessionInfoPtr session_info(PresentationSessionInfo::New()); |
| 234 session_info->url = gurl1_; | 304 session_info->url = gurl1_; |
| 235 session_info->id = presentation_id_.utf8(); | 305 session_info->id = presentation_id_.utf8(); |
| 236 callback.Run(std::move(session_info), PresentationErrorPtr()); | 306 callback.Run(std::move(session_info), PresentationErrorPtr()); |
| 237 })); | 307 })); |
| 238 dispatcher_.startSession( | 308 client()->startSession( |
| 239 urls_, base::MakeUnique<TestWebPresentationConnectionCallback>( | 309 urls_, base::MakeUnique<TestWebPresentationConnectionCallback>( |
| 240 url1_, presentation_id_)); | 310 url1_, presentation_id_)); |
| 241 run_loop.RunUntilIdle(); | 311 run_loop.RunUntilIdle(); |
| 242 } | 312 } |
| 243 | 313 |
| 244 TEST_F(PresentationDispatcherTest, TestStartSessionError) { | 314 TEST_F(PresentationDispatcherTest, TestStartSessionError) { |
| 245 WebString error_message = WebString::fromUTF8("Test error message"); | 315 WebString error_message = WebString::fromUTF8("Test error message"); |
| 246 base::RunLoop run_loop; | 316 base::RunLoop run_loop; |
| 247 | 317 |
| 248 EXPECT_CALL(presentation_service_, StartSession(gurls_, _)) | 318 EXPECT_CALL(presentation_service_, StartSession(gurls_, _)) |
| 249 .WillOnce(Invoke([this, &error_message]( | 319 .WillOnce(Invoke([this, &error_message]( |
| 250 const std::vector<GURL>& presentation_urls, | 320 const std::vector<GURL>& presentation_urls, |
| 251 const PresentationService::StartSessionCallback& callback) { | 321 const PresentationService::StartSessionCallback& callback) { |
| 252 PresentationErrorPtr error(PresentationError::New()); | 322 PresentationErrorPtr error(PresentationError::New()); |
| 253 error->error_type = PresentationErrorType::NO_AVAILABLE_SCREENS; | 323 error->error_type = PresentationErrorType::NO_AVAILABLE_SCREENS; |
| 254 error->message = error_message.utf8(); | 324 error->message = error_message.utf8(); |
| 255 callback.Run(PresentationSessionInfoPtr(), std::move(error)); | 325 callback.Run(PresentationSessionInfoPtr(), std::move(error)); |
| 256 })); | 326 })); |
| 257 dispatcher_.startSession( | 327 client()->startSession( |
| 258 urls_, | 328 urls_, |
| 259 base::MakeUnique<TestWebPresentationConnectionErrorCallback>( | 329 base::MakeUnique<TestWebPresentationConnectionErrorCallback>( |
| 260 WebPresentationError::ErrorTypeNoAvailableScreens, error_message)); | 330 WebPresentationError::ErrorTypeNoAvailableScreens, error_message)); |
| 261 run_loop.RunUntilIdle(); | 331 run_loop.RunUntilIdle(); |
| 262 } | 332 } |
| 263 | 333 |
| 264 TEST_F(PresentationDispatcherTest, TestJoinSessionError) { | 334 TEST_F(PresentationDispatcherTest, TestJoinSessionError) { |
| 265 WebString error_message = WebString::fromUTF8("Test error message"); | 335 WebString error_message = WebString::fromUTF8("Test error message"); |
| 266 base::RunLoop run_loop; | 336 base::RunLoop run_loop; |
| 267 | 337 |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 377 TEST_F(PresentationDispatcherTest, TestTerminateSession) { | 447 TEST_F(PresentationDispatcherTest, TestTerminateSession) { |
| 378 base::RunLoop run_loop; | 448 base::RunLoop run_loop; |
| 379 EXPECT_CALL(presentation_service_, | 449 EXPECT_CALL(presentation_service_, |
| 380 Terminate(gurl1_, presentation_id_.utf8())); | 450 Terminate(gurl1_, presentation_id_.utf8())); |
| 381 dispatcher_.terminateSession(url1_, presentation_id_); | 451 dispatcher_.terminateSession(url1_, presentation_id_); |
| 382 run_loop.RunUntilIdle(); | 452 run_loop.RunUntilIdle(); |
| 383 } | 453 } |
| 384 | 454 |
| 385 TEST_F(PresentationDispatcherTest, TestListenForScreenAvailability) { | 455 TEST_F(PresentationDispatcherTest, TestListenForScreenAvailability) { |
| 386 base::RunLoop run_loop1; | 456 base::RunLoop run_loop1; |
| 387 EXPECT_CALL(presentation_service_, ListenForScreenAvailability(gurl1_)); | 457 for (const auto& gurl : gurls_) { |
| 458 EXPECT_CALL(presentation_service_, ListenForScreenAvailability(gurl)); |
| 459 EXPECT_CALL(presentation_service_, |
| 460 StopListeningForScreenAvailability(gurl)); |
| 461 } |
| 462 |
| 388 dispatcher_.getAvailability( | 463 dispatcher_.getAvailability( |
| 389 urls_, base::MakeUnique<WebPresentationAvailabilityCallbacks>()); | 464 urls_, base::MakeUnique<WebPresentationAvailabilityCallbacks>()); |
| 390 dispatcher_.OnScreenAvailabilityUpdated(url1_, true); | 465 dispatcher_.OnScreenAvailabilityUpdated(url1_, true); |
| 391 run_loop1.RunUntilIdle(); | 466 run_loop1.RunUntilIdle(); |
| 392 | 467 |
| 393 base::RunLoop run_loop2; | 468 base::RunLoop run_loop2; |
| 394 EXPECT_CALL(presentation_service_, ListenForScreenAvailability(gurl1_)); | 469 for (const auto& gurl : gurls_) |
| 395 dispatcher_.startListening(&observer_); | 470 EXPECT_CALL(presentation_service_, ListenForScreenAvailability(gurl)); |
| 471 |
| 472 client()->startListening(&observer_); |
| 396 run_loop2.RunUntilIdle(); | 473 run_loop2.RunUntilIdle(); |
| 397 | 474 |
| 398 base::RunLoop run_loop3; | 475 base::RunLoop run_loop3; |
| 399 EXPECT_CALL(observer_, availabilityChanged(false)); | 476 EXPECT_CALL(observer_, availabilityChanged(false)); |
| 400 dispatcher_.OnScreenAvailabilityUpdated(url1_, false); | 477 dispatcher_.OnScreenAvailabilityUpdated(url1_, false); |
| 401 EXPECT_CALL(observer_, availabilityChanged(true)); | 478 EXPECT_CALL(observer_, availabilityChanged(true)); |
| 402 dispatcher_.OnScreenAvailabilityUpdated(url1_, true); | 479 dispatcher_.OnScreenAvailabilityUpdated(url1_, true); |
| 403 EXPECT_CALL(presentation_service_, | 480 for (const auto& gurl : gurls_) { |
| 404 StopListeningForScreenAvailability(gurl1_)); | 481 EXPECT_CALL(presentation_service_, |
| 405 dispatcher_.stopListening(&observer_); | 482 StopListeningForScreenAvailability(gurl)); |
| 483 } |
| 484 client()->stopListening(&observer_); |
| 406 run_loop3.RunUntilIdle(); | 485 run_loop3.RunUntilIdle(); |
| 407 | 486 |
| 408 // After stopListening(), |observer_| should no longer be notified. | 487 // After stopListening(), |observer_| should no longer be notified. |
| 409 base::RunLoop run_loop4; | 488 base::RunLoop run_loop4; |
| 410 EXPECT_CALL(observer_, availabilityChanged(false)).Times(0); | 489 EXPECT_CALL(observer_, availabilityChanged(false)).Times(0); |
| 411 dispatcher_.OnScreenAvailabilityUpdated(url1_, false); | 490 dispatcher_.OnScreenAvailabilityUpdated(url1_, false); |
| 412 run_loop4.RunUntilIdle(); | 491 run_loop4.RunUntilIdle(); |
| 413 } | 492 } |
| 414 | 493 |
| 415 TEST_F(PresentationDispatcherTest, TestSetDefaultPresentationUrls) { | 494 TEST_F(PresentationDispatcherTest, TestSetDefaultPresentationUrls) { |
| 416 base::RunLoop run_loop; | 495 base::RunLoop run_loop; |
| 417 EXPECT_CALL(presentation_service_, SetDefaultPresentationUrls(gurls_)); | 496 EXPECT_CALL(presentation_service_, SetDefaultPresentationUrls(gurls_)); |
| 418 dispatcher_.setDefaultPresentationUrls(urls_); | 497 dispatcher_.setDefaultPresentationUrls(urls_); |
| 419 run_loop.RunUntilIdle(); | 498 run_loop.RunUntilIdle(); |
| 420 } | 499 } |
| 421 | 500 |
| 501 TEST_F(PresentationDispatcherTest, GetAvailabilityOneUrlNoAvailabilityChange) { |
| 502 auto* mock_callback = |
| 503 new testing::StrictMock<MockPresentationAvailabilityCallbacks>(); |
| 504 |
| 505 EXPECT_CALL(presentation_service_, ListenForScreenAvailability(gurl1_)) |
| 506 .Times(1); |
| 507 |
| 508 base::RunLoop run_loop; |
| 509 client()->getAvailability(std::vector<GURL>({gurl1_}), |
| 510 base::WrapUnique(mock_callback)); |
| 511 run_loop.RunUntilIdle(); |
| 512 } |
| 513 |
| 514 TEST_F(PresentationDispatcherTest, GetAvailabilityOneUrlBecomesAvailable) { |
| 515 auto* mock_callback = new MockPresentationAvailabilityCallbacks(); |
| 516 EXPECT_CALL(*mock_callback, onSuccess(true)); |
| 517 |
| 518 TestGetAvailability({url1_}, {URLState::Available}, mock_callback); |
| 519 } |
| 520 |
| 521 TEST_F(PresentationDispatcherTest, GetAvailabilityOneUrlBecomesUnavailable) { |
| 522 auto* mock_callback = new MockPresentationAvailabilityCallbacks(); |
| 523 EXPECT_CALL(*mock_callback, onSuccess(false)); |
| 524 |
| 525 TestGetAvailability({url1_}, {URLState::Unavailable}, mock_callback); |
| 526 } |
| 527 |
| 528 TEST_F(PresentationDispatcherTest, GetAvailabilityOneUrlBecomesNotSupported) { |
| 529 auto* mock_callback = new MockPresentationAvailabilityCallbacks(); |
| 530 EXPECT_CALL(*mock_callback, onError(_)); |
| 531 |
| 532 TestGetAvailability({url1_}, {URLState::Unsupported}, mock_callback); |
| 533 } |
| 534 |
| 535 TEST_F(PresentationDispatcherTest, |
| 536 GetAvailabilityMultipleUrlsAllBecomesAvailable) { |
| 537 auto* mock_callback = new MockPresentationAvailabilityCallbacks(); |
| 538 EXPECT_CALL(*mock_callback, onSuccess(true)).Times(1); |
| 539 |
| 540 TestGetAvailability({url1_, url2_}, |
| 541 {URLState::Available, URLState::Available}, |
| 542 mock_callback); |
| 543 } |
| 544 |
| 545 TEST_F(PresentationDispatcherTest, |
| 546 GetAvailabilityMultipleUrlsAllBecomesUnavailable) { |
| 547 auto* mock_callback = new MockPresentationAvailabilityCallbacks(); |
| 548 EXPECT_CALL(*mock_callback, onSuccess(false)).Times(1); |
| 549 |
| 550 TestGetAvailability({url1_, url2_}, |
| 551 {URLState::Unavailable, URLState::Unavailable}, |
| 552 mock_callback); |
| 553 } |
| 554 |
| 555 TEST_F(PresentationDispatcherTest, |
| 556 GetAvailabilityMultipleUrlsAllBecomesUnsupported) { |
| 557 auto* mock_callback = new MockPresentationAvailabilityCallbacks(); |
| 558 EXPECT_CALL(*mock_callback, onError(_)).Times(1); |
| 559 |
| 560 TestGetAvailability({url1_, url2_}, |
| 561 {URLState::Unsupported, URLState::Unsupported}, |
| 562 mock_callback); |
| 563 } |
| 564 |
| 565 TEST_F(PresentationDispatcherTest, |
| 566 GetAvailabilityReturnsDirectlyForAlreadyListeningUrls) { |
| 567 // First getAvailability() call. |
| 568 auto* mock_callback_1 = new MockPresentationAvailabilityCallbacks(); |
| 569 EXPECT_CALL(*mock_callback_1, onSuccess(false)).Times(1); |
| 570 |
| 571 std::vector<URLState> state_seq = {URLState::Unavailable, URLState::Available, |
| 572 URLState::Unavailable}; |
| 573 TestGetAvailability({url1_, url2_, url3_}, state_seq, mock_callback_1); |
| 574 |
| 575 // Second getAvailability() call. |
| 576 for (const auto& url : mock_observer3_.urls()) { |
| 577 EXPECT_CALL(presentation_service_, ListenForScreenAvailability((GURL)url)) |
| 578 .Times(1); |
| 579 } |
| 580 auto* mock_callback_2 = new MockPresentationAvailabilityCallbacks(); |
| 581 EXPECT_CALL(*mock_callback_2, onSuccess(true)).Times(1); |
| 582 |
| 583 base::RunLoop run_loop; |
| 584 client()->getAvailability(mock_observer3_.urls(), |
| 585 base::WrapUnique(mock_callback_2)); |
| 586 run_loop.RunUntilIdle(); |
| 587 } |
| 588 |
| 589 TEST_F(PresentationDispatcherTest, StartListeningListenToEachURLOnce) { |
| 590 for (const auto& gurl : gurls_) { |
| 591 EXPECT_CALL(presentation_service_, ListenForScreenAvailability(gurl)) |
| 592 .Times(1); |
| 593 } |
| 594 |
| 595 base::RunLoop run_loop; |
| 596 for (auto* mock_observer : mock_observers_) { |
| 597 client()->getAvailability( |
| 598 mock_observer->urls(), |
| 599 base::MakeUnique<WebPresentationAvailabilityCallbacks>()); |
| 600 client()->startListening(mock_observer); |
| 601 } |
| 602 run_loop.RunUntilIdle(); |
| 603 } |
| 604 |
| 605 TEST_F(PresentationDispatcherTest, StopListeningListenToEachURLOnce) { |
| 606 for (const auto& gurl : gurls_) { |
| 607 EXPECT_CALL(presentation_service_, ListenForScreenAvailability(gurl)) |
| 608 .Times(1); |
| 609 EXPECT_CALL(presentation_service_, StopListeningForScreenAvailability(gurl)) |
| 610 .Times(1); |
| 611 } |
| 612 |
| 613 EXPECT_CALL(mock_observer1_, availabilityChanged(false)); |
| 614 EXPECT_CALL(mock_observer2_, availabilityChanged(false)); |
| 615 EXPECT_CALL(mock_observer3_, availabilityChanged(false)); |
| 616 |
| 617 // Set up |availability_set_| and |listening_status_| |
| 618 base::RunLoop run_loop; |
| 619 for (auto* mock_observer : mock_observers_) { |
| 620 client()->getAvailability( |
| 621 mock_observer->urls(), |
| 622 base::MakeUnique<WebPresentationAvailabilityCallbacks>()); |
| 623 |
| 624 client()->startListening(mock_observer); |
| 625 } |
| 626 |
| 627 // Clean up callbacks. |
| 628 ChangeURLState(gurl2_, URLState::Unavailable); |
| 629 |
| 630 for (auto* mock_observer : mock_observers_) |
| 631 client()->stopListening(mock_observer); |
| 632 |
| 633 run_loop.RunUntilIdle(); |
| 634 } |
| 635 |
| 636 TEST_F(PresentationDispatcherTest, |
| 637 StopListeningDoesNotStopIfURLListenedByOthers) { |
| 638 for (const auto& gurl : gurls_) { |
| 639 EXPECT_CALL(presentation_service_, ListenForScreenAvailability(gurl)) |
| 640 .Times(1); |
| 641 } |
| 642 EXPECT_CALL(presentation_service_, StopListeningForScreenAvailability(gurl1_)) |
| 643 .Times(1); |
| 644 EXPECT_CALL(presentation_service_, StopListeningForScreenAvailability(gurl2_)) |
| 645 .Times(0); |
| 646 EXPECT_CALL(presentation_service_, StopListeningForScreenAvailability(gurl3_)) |
| 647 .Times(0); |
| 648 |
| 649 // Set up |availability_set_| and |listening_status_| |
| 650 base::RunLoop run_loop; |
| 651 for (auto& mock_observer : mock_observers_) { |
| 652 client()->getAvailability( |
| 653 mock_observer->urls(), |
| 654 base::MakeUnique<WebPresentationAvailabilityCallbacks>()); |
| 655 } |
| 656 |
| 657 for (auto* mock_observer : mock_observers_) |
| 658 client()->startListening(mock_observer); |
| 659 |
| 660 EXPECT_CALL(mock_observer1_, availabilityChanged(false)); |
| 661 EXPECT_CALL(mock_observer2_, availabilityChanged(false)); |
| 662 EXPECT_CALL(mock_observer3_, availabilityChanged(false)); |
| 663 |
| 664 // Clean up callbacks. |
| 665 ChangeURLState(gurl2_, URLState::Unavailable); |
| 666 client()->stopListening(&mock_observer1_); |
| 667 run_loop.RunUntilIdle(); |
| 668 } |
| 669 |
| 670 TEST_F(PresentationDispatcherTest, |
| 671 OnScreenAvailabilityUpdatedInvokesAvailabilityChanged) { |
| 672 for (const auto& gurl : gurls_) { |
| 673 EXPECT_CALL(presentation_service_, ListenForScreenAvailability(gurl)) |
| 674 .Times(1); |
| 675 } |
| 676 EXPECT_CALL(mock_observer1_, availabilityChanged(true)); |
| 677 |
| 678 base::RunLoop run_loop; |
| 679 for (auto* mock_observer : mock_observers_) { |
| 680 client()->getAvailability( |
| 681 mock_observer->urls(), |
| 682 base::MakeUnique<WebPresentationAvailabilityCallbacks>()); |
| 683 client()->startListening(mock_observer); |
| 684 } |
| 685 |
| 686 ChangeURLState(gurl1_, URLState::Available); |
| 687 run_loop.RunUntilIdle(); |
| 688 |
| 689 EXPECT_CALL(mock_observer1_, availabilityChanged(false)); |
| 690 |
| 691 base::RunLoop run_loop_2; |
| 692 ChangeURLState(gurl1_, URLState::Unavailable); |
| 693 run_loop_2.RunUntilIdle(); |
| 694 } |
| 695 |
| 696 TEST_F(PresentationDispatcherTest, |
| 697 OnScreenAvailabilityUpdatedInvokesMultipleAvailabilityChanged) { |
| 698 for (const auto& gurl : gurls_) { |
| 699 EXPECT_CALL(presentation_service_, ListenForScreenAvailability(gurl)) |
| 700 .Times(1); |
| 701 } |
| 702 for (auto* mock_observer : mock_observers_) |
| 703 EXPECT_CALL(*mock_observer, availabilityChanged(true)); |
| 704 |
| 705 base::RunLoop run_loop; |
| 706 for (auto* mock_observer : mock_observers_) { |
| 707 client()->getAvailability( |
| 708 mock_observer->urls(), |
| 709 base::MakeUnique<WebPresentationAvailabilityCallbacks>()); |
| 710 client()->startListening(mock_observer); |
| 711 } |
| 712 |
| 713 ChangeURLState(gurl2_, URLState::Available); |
| 714 run_loop.RunUntilIdle(); |
| 715 |
| 716 for (auto* mock_observer : mock_observers_) |
| 717 EXPECT_CALL(*mock_observer, availabilityChanged(false)); |
| 718 |
| 719 base::RunLoop run_loop_2; |
| 720 ChangeURLState(gurl2_, URLState::Unavailable); |
| 721 run_loop_2.RunUntilIdle(); |
| 722 } |
| 723 |
| 422 } // namespace content | 724 } // namespace content |
| OLD | NEW |