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

Side by Side Diff: content/renderer/presentation/presentation_dispatcher_unittest.cc

Issue 2598063002: [Presentation API] Handle multiple Presentation URLs in PresentationRequest::getAvailability() (Closed)
Patch Set: resolve code review comments from Mark Created 3 years, 11 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
OLDNEW
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 29 matching lines...) Expand all
40 // - Messages received 40 // - Messages received
41 // - Discarding queued messages when the frame navigates 41 // - Discarding queued messages when the frame navigates
42 // - Screen availability not supported 42 // - Screen availability not supported
43 // - Default presentation starting 43 // - Default presentation starting
44 44
45 namespace content { 45 namespace content {
46 46
47 class MockPresentationAvailabilityObserver 47 class MockPresentationAvailabilityObserver
48 : public WebPresentationAvailabilityObserver { 48 : public WebPresentationAvailabilityObserver {
49 public: 49 public:
50 explicit MockPresentationAvailabilityObserver(const WebVector<WebURL>& urls) 50 explicit MockPresentationAvailabilityObserver(const std::vector<GURL>& gurls)
mark a. foltz 2017/01/17 21:04:24 Nit: s/gurls/urls/ for consistency
zhaobin 2017/01/18 03:38:57 Done.
51 : urls_(urls) {} 51 : urls_(gurls) {}
52 ~MockPresentationAvailabilityObserver() override {} 52 ~MockPresentationAvailabilityObserver() override {}
53 53
54 MOCK_METHOD1(availabilityChanged, void(bool is_available)); 54 MOCK_METHOD1(availabilityChanged, void(bool is_available));
55 const WebVector<WebURL>& urls() const override { return urls_; } 55 const WebVector<WebURL>& urls() const override { return urls_; }
56 56
57 private: 57 private:
58 const WebVector<WebURL> urls_; 58 const WebVector<WebURL> urls_;
59 }; 59 };
60 60
61 class MockPresentationService : public PresentationService { 61 class MockPresentationService : public PresentationService {
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 // *Internal method is to work around lack of support for move-only types in 98 // *Internal method is to work around lack of support for move-only types in
99 // GMock. 99 // GMock.
100 void ListenForConnectionMessages( 100 void ListenForConnectionMessages(
101 PresentationSessionInfoPtr session_info) override { 101 PresentationSessionInfoPtr session_info) override {
102 ListenForConnectionMessagesInternal(session_info.get()); 102 ListenForConnectionMessagesInternal(session_info.get());
103 } 103 }
104 MOCK_METHOD1(ListenForConnectionMessagesInternal, 104 MOCK_METHOD1(ListenForConnectionMessagesInternal,
105 void(PresentationSessionInfo* session_info)); 105 void(PresentationSessionInfo* session_info));
106 }; 106 };
107 107
108 class MockPresentationAvailabilityCallbacks
109 : public blink::WebCallbacks<bool, const blink::WebPresentationError&> {
110 public:
111 MOCK_METHOD1(onSuccess, void(bool value));
112 MOCK_METHOD1(onError, void(const blink::WebPresentationError&));
113 };
114
108 class TestWebPresentationConnectionCallback 115 class TestWebPresentationConnectionCallback
109 : public WebPresentationConnectionCallback { 116 : public WebPresentationConnectionCallback {
110 public: 117 public:
111 TestWebPresentationConnectionCallback(WebURL url, WebString id) 118 TestWebPresentationConnectionCallback(WebURL url, WebString id)
112 : url_(url), id_(id), callback_called_(false) {} 119 : url_(url), id_(id), callback_called_(false) {}
113 ~TestWebPresentationConnectionCallback() override { 120 ~TestWebPresentationConnectionCallback() override {
114 EXPECT_TRUE(callback_called_); 121 EXPECT_TRUE(callback_called_);
115 } 122 }
116 123
117 void onSuccess(const WebPresentationSessionInfo& info) override { 124 void onSuccess(const WebPresentationSessionInfo& info) override {
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 mojo::MakeRequest(&presentation_service_)); 172 mojo::MakeRequest(&presentation_service_));
166 } 173 }
167 } 174 }
168 175
169 MockPresentationService* mock_presentation_service_; 176 MockPresentationService* mock_presentation_service_;
170 std::unique_ptr<mojo::Binding<PresentationService>> mock_binding_; 177 std::unique_ptr<mojo::Binding<PresentationService>> mock_binding_;
171 }; 178 };
172 179
173 class PresentationDispatcherTest : public ::testing::Test { 180 class PresentationDispatcherTest : public ::testing::Test {
174 public: 181 public:
182 enum class URLState { Available, Unavailable, Unsupported, Unknown };
183
175 PresentationDispatcherTest() 184 PresentationDispatcherTest()
176 : gurl1_(GURL("https://www.example.com/1.html")), 185 : gurl1_(GURL("https://www.example.com/1.html")),
177 gurl2_(GURL("https://www.example.com/2.html")), 186 gurl2_(GURL("https://www.example.com/2.html")),
178 gurls_({gurl1_, gurl2_}), 187 gurl3_(GURL("https://www.example.com/3.html")),
188 gurl4_(GURL("https://www.example.com/4.html")),
189 gurls_({gurl1_, gurl2_, gurl3_, gurl4_}),
179 url1_(WebURL(gurl1_)), 190 url1_(WebURL(gurl1_)),
180 url2_(WebURL(gurl2_)), 191 url2_(WebURL(gurl2_)),
192 url3_(WebURL(gurl3_)),
193 url4_(WebURL(gurl4_)),
181 urls_(WebVector<WebURL>(gurls_)), 194 urls_(WebVector<WebURL>(gurls_)),
182 presentation_id_(WebString::fromUTF8("test-id")), 195 presentation_id_(WebString::fromUTF8("test-id")),
183 array_buffer_(WebArrayBuffer::create(4, 1)), 196 array_buffer_(WebArrayBuffer::create(4, 1)),
184 observer_(urls_), 197 observer_(gurls_),
198 mock_observer1_({gurl1_, gurl2_, gurl3_}),
199 mock_observer2_({gurl2_, gurl3_, gurl4_}),
200 mock_observer3_({gurl2_, gurl3_}),
201 mock_observers_({&mock_observer1_, &mock_observer2_, &mock_observer3_}),
185 dispatcher_(&presentation_service_) {} 202 dispatcher_(&presentation_service_) {}
203
186 ~PresentationDispatcherTest() override {} 204 ~PresentationDispatcherTest() override {}
187 205
188 void SetUp() override { 206 void SetUp() override {
189 // Set some test data. 207 // Set some test data.
190 *array_buffer_data() = 42; 208 *array_buffer_data() = 42;
191 } 209 }
192 210
193 uint8_t* array_buffer_data() { 211 uint8_t* array_buffer_data() {
194 return static_cast<uint8_t*>(array_buffer_.data()); 212 return static_cast<uint8_t*>(array_buffer_.data());
195 } 213 }
196 214
215 void ChangeURLState(const GURL& url, URLState state) {
216 switch (state) {
217 case URLState::Available:
218 dispatcher_.OnScreenAvailabilityUpdated(url, true);
219 break;
220 case URLState::Unavailable:
221 dispatcher_.OnScreenAvailabilityUpdated(url, false);
222 break;
223 case URLState::Unsupported:
224 dispatcher_.OnScreenAvailabilityNotSupported(url);
225 break;
226 case URLState::Unknown:
227 break;
228 }
229 }
230
231 // Tests that PresenationService is called for getAvailability(urls), after
232 // |urls| change state to |states|.
233 void TestGetAvailability(
234 const std::vector<GURL>& urls,
235 const std::vector<URLState>& states,
236 MockPresentationAvailabilityCallbacks* mock_callback) {
237 DCHECK_EQ(urls.size(), states.size());
238
239 for (const auto& url : urls) {
240 EXPECT_CALL(presentation_service_, ListenForScreenAvailability(url))
241 .Times(1);
242 }
243
244 base::RunLoop run_loop;
245 // This function takes ownership of |mock_callback|.
mark a. foltz 2017/01/17 21:04:24 This comment probably belongs with the function-le
zhaobin 2017/01/18 03:38:57 Done.
246 client()->getAvailability(urls, base::WrapUnique(mock_callback));
247 for (size_t i = 0; i < urls.size(); i++)
248 ChangeURLState(urls[i], states[i]);
249
250 run_loop.RunUntilIdle();
251 }
252
253 blink::WebPresentationClient* client() { return &dispatcher_; }
254
197 protected: 255 protected:
198 const GURL gurl1_; 256 const GURL gurl1_;
199 const GURL gurl2_; 257 const GURL gurl2_;
258 const GURL gurl3_;
259 const GURL gurl4_;
200 const std::vector<GURL> gurls_; 260 const std::vector<GURL> gurls_;
201 const WebURL url1_; 261 const WebURL url1_;
202 const WebURL url2_; 262 const WebURL url2_;
263 const WebURL url3_;
264 const WebURL url4_;
203 const WebVector<WebURL> urls_; 265 const WebVector<WebURL> urls_;
204 const WebString presentation_id_; 266 const WebString presentation_id_;
205 const WebArrayBuffer array_buffer_; 267 const WebArrayBuffer array_buffer_;
206 MockPresentationAvailabilityObserver observer_; 268 MockPresentationAvailabilityObserver observer_;
269 MockPresentationAvailabilityObserver mock_observer1_;
270 MockPresentationAvailabilityObserver mock_observer2_;
271 MockPresentationAvailabilityObserver mock_observer3_;
272 std::vector<MockPresentationAvailabilityObserver*> mock_observers_;
273
207 MockPresentationService presentation_service_; 274 MockPresentationService presentation_service_;
208 TestPresentationDispatcher dispatcher_; 275 TestPresentationDispatcher dispatcher_;
209 276
210 private: 277 private:
211 content::TestBrowserThreadBundle thread_bundle_; 278 content::TestBrowserThreadBundle thread_bundle_;
212 }; 279 };
213 280
214 TEST_F(PresentationDispatcherTest, TestStartSession) { 281 TEST_F(PresentationDispatcherTest, TestStartSession) {
215 base::RunLoop run_loop; 282 base::RunLoop run_loop;
216 283
217 EXPECT_CALL(presentation_service_, StartSession(gurls_, _)) 284 EXPECT_CALL(presentation_service_, StartSession(gurls_, _))
218 .WillOnce(Invoke([this]( 285 .WillOnce(Invoke([this](
219 const std::vector<GURL>& presentation_urls, 286 const std::vector<GURL>& presentation_urls,
220 const PresentationService::StartSessionCallback& callback) { 287 const PresentationService::StartSessionCallback& callback) {
221 PresentationSessionInfoPtr session_info(PresentationSessionInfo::New()); 288 PresentationSessionInfoPtr session_info(PresentationSessionInfo::New());
222 session_info->url = gurl1_; 289 session_info->url = gurl1_;
223 session_info->id = presentation_id_.utf8(); 290 session_info->id = presentation_id_.utf8();
224 callback.Run(std::move(session_info), PresentationErrorPtr()); 291 callback.Run(std::move(session_info), PresentationErrorPtr());
225 })); 292 }));
226 dispatcher_.startSession( 293 client()->startSession(
227 urls_, base::MakeUnique<TestWebPresentationConnectionCallback>( 294 urls_, base::MakeUnique<TestWebPresentationConnectionCallback>(
228 url1_, presentation_id_)); 295 url1_, presentation_id_));
229 run_loop.RunUntilIdle(); 296 run_loop.RunUntilIdle();
230 } 297 }
231 298
232 TEST_F(PresentationDispatcherTest, TestStartSessionError) { 299 TEST_F(PresentationDispatcherTest, TestStartSessionError) {
233 WebString error_message = WebString::fromUTF8("Test error message"); 300 WebString error_message = WebString::fromUTF8("Test error message");
234 base::RunLoop run_loop; 301 base::RunLoop run_loop;
235 302
236 EXPECT_CALL(presentation_service_, StartSession(gurls_, _)) 303 EXPECT_CALL(presentation_service_, StartSession(gurls_, _))
237 .WillOnce(Invoke([this, &error_message]( 304 .WillOnce(Invoke([this, &error_message](
238 const std::vector<GURL>& presentation_urls, 305 const std::vector<GURL>& presentation_urls,
239 const PresentationService::StartSessionCallback& callback) { 306 const PresentationService::StartSessionCallback& callback) {
240 PresentationErrorPtr error(PresentationError::New()); 307 PresentationErrorPtr error(PresentationError::New());
241 error->error_type = PresentationErrorType::NO_AVAILABLE_SCREENS; 308 error->error_type = PresentationErrorType::NO_AVAILABLE_SCREENS;
242 error->message = error_message.utf8(); 309 error->message = error_message.utf8();
243 callback.Run(PresentationSessionInfoPtr(), std::move(error)); 310 callback.Run(PresentationSessionInfoPtr(), std::move(error));
244 })); 311 }));
245 dispatcher_.startSession( 312 client()->startSession(
246 urls_, 313 urls_,
247 base::MakeUnique<TestWebPresentationConnectionErrorCallback>( 314 base::MakeUnique<TestWebPresentationConnectionErrorCallback>(
248 WebPresentationError::ErrorTypeNoAvailableScreens, error_message)); 315 WebPresentationError::ErrorTypeNoAvailableScreens, error_message));
249 run_loop.RunUntilIdle(); 316 run_loop.RunUntilIdle();
250 } 317 }
251 318
252 TEST_F(PresentationDispatcherTest, TestJoinSessionError) { 319 TEST_F(PresentationDispatcherTest, TestJoinSessionError) {
253 WebString error_message = WebString::fromUTF8("Test error message"); 320 WebString error_message = WebString::fromUTF8("Test error message");
254 base::RunLoop run_loop; 321 base::RunLoop run_loop;
255 322
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
365 TEST_F(PresentationDispatcherTest, TestTerminateSession) { 432 TEST_F(PresentationDispatcherTest, TestTerminateSession) {
366 base::RunLoop run_loop; 433 base::RunLoop run_loop;
367 EXPECT_CALL(presentation_service_, 434 EXPECT_CALL(presentation_service_,
368 Terminate(gurl1_, presentation_id_.utf8())); 435 Terminate(gurl1_, presentation_id_.utf8()));
369 dispatcher_.terminateSession(url1_, presentation_id_); 436 dispatcher_.terminateSession(url1_, presentation_id_);
370 run_loop.RunUntilIdle(); 437 run_loop.RunUntilIdle();
371 } 438 }
372 439
373 TEST_F(PresentationDispatcherTest, TestListenForScreenAvailability) { 440 TEST_F(PresentationDispatcherTest, TestListenForScreenAvailability) {
374 base::RunLoop run_loop1; 441 base::RunLoop run_loop1;
375 EXPECT_CALL(presentation_service_, ListenForScreenAvailability(gurl1_)); 442 for (const auto& gurl : gurls_)
443 EXPECT_CALL(presentation_service_, ListenForScreenAvailability(gurl));
376 dispatcher_.getAvailability( 444 dispatcher_.getAvailability(
377 urls_, base::MakeUnique<WebPresentationAvailabilityCallbacks>()); 445 urls_, base::MakeUnique<WebPresentationAvailabilityCallbacks>());
378 dispatcher_.OnScreenAvailabilityUpdated(url1_, true); 446 dispatcher_.OnScreenAvailabilityUpdated(url1_, true);
379 run_loop1.RunUntilIdle(); 447 run_loop1.RunUntilIdle();
380 448
381 base::RunLoop run_loop2; 449 base::RunLoop run_loop2;
382 EXPECT_CALL(presentation_service_, ListenForScreenAvailability(gurl1_)); 450 client()->startListening(&observer_);
383 dispatcher_.startListening(&observer_);
384 run_loop2.RunUntilIdle(); 451 run_loop2.RunUntilIdle();
385 452
386 base::RunLoop run_loop3; 453 base::RunLoop run_loop3;
387 EXPECT_CALL(observer_, availabilityChanged(false)); 454 EXPECT_CALL(observer_, availabilityChanged(false));
388 dispatcher_.OnScreenAvailabilityUpdated(url1_, false); 455 dispatcher_.OnScreenAvailabilityUpdated(url1_, false);
389 EXPECT_CALL(observer_, availabilityChanged(true)); 456 EXPECT_CALL(observer_, availabilityChanged(true));
390 dispatcher_.OnScreenAvailabilityUpdated(url1_, true); 457 dispatcher_.OnScreenAvailabilityUpdated(url1_, true);
391 EXPECT_CALL(presentation_service_, 458 for (const auto& gurl : gurls_) {
392 StopListeningForScreenAvailability(gurl1_)); 459 EXPECT_CALL(presentation_service_,
393 dispatcher_.stopListening(&observer_); 460 StopListeningForScreenAvailability(gurl));
461 }
462 client()->stopListening(&observer_);
394 run_loop3.RunUntilIdle(); 463 run_loop3.RunUntilIdle();
395 464
396 // After stopListening(), |observer_| should no longer be notified. 465 // After stopListening(), |observer_| should no longer be notified.
397 base::RunLoop run_loop4; 466 base::RunLoop run_loop4;
398 EXPECT_CALL(observer_, availabilityChanged(false)).Times(0); 467 EXPECT_CALL(observer_, availabilityChanged(false)).Times(0);
399 dispatcher_.OnScreenAvailabilityUpdated(url1_, false); 468 dispatcher_.OnScreenAvailabilityUpdated(url1_, false);
400 run_loop4.RunUntilIdle(); 469 run_loop4.RunUntilIdle();
401 } 470 }
402 471
403 TEST_F(PresentationDispatcherTest, TestSetDefaultPresentationUrls) { 472 TEST_F(PresentationDispatcherTest, TestSetDefaultPresentationUrls) {
404 base::RunLoop run_loop; 473 base::RunLoop run_loop;
405 EXPECT_CALL(presentation_service_, SetDefaultPresentationUrls(gurls_)); 474 EXPECT_CALL(presentation_service_, SetDefaultPresentationUrls(gurls_));
406 dispatcher_.setDefaultPresentationUrls(urls_); 475 dispatcher_.setDefaultPresentationUrls(urls_);
407 run_loop.RunUntilIdle(); 476 run_loop.RunUntilIdle();
408 } 477 }
409 478
479 TEST_F(PresentationDispatcherTest, GetAvailabilityOneUrlNoAvailabilityChange) {
480 auto* mock_callback =
481 new testing::StrictMock<MockPresentationAvailabilityCallbacks>();
482 TestGetAvailability({url1_}, {URLState::Unknown}, mock_callback);
483 }
484
485 TEST_F(PresentationDispatcherTest, GetAvailabilityOneUrlBecomesAvailable) {
486 auto* mock_callback = new MockPresentationAvailabilityCallbacks();
487 EXPECT_CALL(*mock_callback, onSuccess(true));
488
489 TestGetAvailability({url1_}, {URLState::Available}, mock_callback);
490 }
491
492 TEST_F(PresentationDispatcherTest, GetAvailabilityOneUrlBecomesUnavailable) {
493 auto* mock_callback = new MockPresentationAvailabilityCallbacks();
494 EXPECT_CALL(*mock_callback, onSuccess(false));
495
496 TestGetAvailability({url1_}, {URLState::Unavailable}, mock_callback);
497 }
498
499 TEST_F(PresentationDispatcherTest, GetAvailabilityOneUrlBecomesNotSupported) {
500 auto* mock_callback = new MockPresentationAvailabilityCallbacks();
501 EXPECT_CALL(*mock_callback, onError(_));
502 EXPECT_CALL(presentation_service_,
503 StopListeningForScreenAvailability(gurl1_));
504
505 TestGetAvailability({url1_}, {URLState::Unsupported}, mock_callback);
506 }
507
508 TEST_F(PresentationDispatcherTest,
509 GetAvailabilityMultipleUrlsAllBecomesAvailable) {
510 auto* mock_callback = new MockPresentationAvailabilityCallbacks();
511 EXPECT_CALL(*mock_callback, onSuccess(true)).Times(1);
512
513 TestGetAvailability({url1_, url2_},
514 {URLState::Available, URLState::Available},
515 mock_callback);
516 }
517
518 TEST_F(PresentationDispatcherTest,
519 GetAvailabilityMultipleUrlsAllBecomesUnavailable) {
520 auto* mock_callback = new MockPresentationAvailabilityCallbacks();
521 EXPECT_CALL(*mock_callback, onSuccess(false)).Times(1);
522
523 TestGetAvailability({url1_, url2_},
524 {URLState::Unavailable, URLState::Unavailable},
525 mock_callback);
526 }
527
528 TEST_F(PresentationDispatcherTest,
529 GetAvailabilityMultipleUrlsAllBecomesUnsupported) {
530 auto* mock_callback = new MockPresentationAvailabilityCallbacks();
531 EXPECT_CALL(*mock_callback, onError(_)).Times(1);
532 EXPECT_CALL(presentation_service_,
533 StopListeningForScreenAvailability(gurl1_));
534 EXPECT_CALL(presentation_service_,
535 StopListeningForScreenAvailability(gurl2_));
536
537 TestGetAvailability({url1_, url2_},
538 {URLState::Unsupported, URLState::Unsupported},
539 mock_callback);
540 }
541
542 TEST_F(PresentationDispatcherTest,
543 GetAvailabilityReturnsDirectlyForAlreadyListeningUrls) {
544 auto* mock_callback_1 = new MockPresentationAvailabilityCallbacks();
545 EXPECT_CALL(*mock_callback_1, onSuccess(false)).Times(1);
546
547 std::vector<URLState> state_seq = {URLState::Unavailable, URLState::Available,
548 URLState::Unavailable};
549 TestGetAvailability({url1_, url2_, url3_}, state_seq, mock_callback_1);
550
551 auto* mock_callback_2 = new MockPresentationAvailabilityCallbacks();
552 EXPECT_CALL(*mock_callback_2, onSuccess(true)).Times(1);
553
554 base::RunLoop run_loop;
555 client()->getAvailability(mock_observer3_.urls(),
556 base::WrapUnique(mock_callback_2));
557 run_loop.RunUntilIdle();
558 }
559
560 TEST_F(PresentationDispatcherTest, StartListeningListenToEachURLOnce) {
561 for (const auto& gurl : gurls_) {
562 EXPECT_CALL(presentation_service_, ListenForScreenAvailability(gurl))
563 .Times(1);
564 }
565
566 base::RunLoop run_loop;
567 for (auto* mock_observer : mock_observers_) {
568 client()->getAvailability(
569 mock_observer->urls(),
570 base::MakeUnique<WebPresentationAvailabilityCallbacks>());
571 client()->startListening(mock_observer);
572 }
573 run_loop.RunUntilIdle();
574 }
575
576 TEST_F(PresentationDispatcherTest, StopListeningListenToEachURLOnce) {
577 for (const auto& gurl : gurls_) {
578 EXPECT_CALL(presentation_service_, ListenForScreenAvailability(gurl))
579 .Times(1);
580 EXPECT_CALL(presentation_service_, StopListeningForScreenAvailability(gurl))
581 .Times(1);
582 }
583
584 // Set up |availability_set_| and |listening_status_|
585 base::RunLoop run_loop;
586 for (auto* mock_observer : mock_observers_) {
587 client()->getAvailability(
588 mock_observer->urls(),
589 base::MakeUnique<WebPresentationAvailabilityCallbacks>());
590 }
591 // Clean up callbacks.
592 ChangeURLState(gurl2_, URLState::Unavailable);
593
594 for (auto* mock_observer : mock_observers_)
595 client()->startListening(mock_observer);
596 for (auto* mock_observer : mock_observers_)
597 client()->stopListening(mock_observer);
598
599 run_loop.RunUntilIdle();
600 }
601
602 TEST_F(PresentationDispatcherTest,
603 StopListeningDoesNotStopIfURLListenedByOthers) {
604 for (const auto& gurl : gurls_) {
605 EXPECT_CALL(presentation_service_, ListenForScreenAvailability(gurl))
606 .Times(1);
607 }
608 EXPECT_CALL(presentation_service_, StopListeningForScreenAvailability(gurl1_))
609 .Times(1);
610 EXPECT_CALL(presentation_service_, StopListeningForScreenAvailability(gurl2_))
611 .Times(0);
612 EXPECT_CALL(presentation_service_, StopListeningForScreenAvailability(gurl3_))
613 .Times(0);
614
615 // Set up |availability_set_| and |listening_status_|
616 base::RunLoop run_loop;
617 for (auto& mock_observer : mock_observers_) {
618 client()->getAvailability(
619 mock_observer->urls(),
620 base::MakeUnique<WebPresentationAvailabilityCallbacks>());
621 }
622
623 for (auto* mock_observer : mock_observers_)
624 client()->startListening(mock_observer);
625
626 EXPECT_CALL(mock_observer1_, availabilityChanged(false));
627 EXPECT_CALL(mock_observer2_, availabilityChanged(false));
628
629 // Clean up callbacks.
630 ChangeURLState(gurl2_, URLState::Unavailable);
631 client()->stopListening(&mock_observer1_);
632 run_loop.RunUntilIdle();
633 }
634
635 TEST_F(PresentationDispatcherTest,
636 OnScreenAvailabilityUpdatedInvokesAvailabilityChanged) {
637 for (const auto& gurl : gurls_) {
638 EXPECT_CALL(presentation_service_, ListenForScreenAvailability(gurl))
639 .Times(1);
640 }
641 EXPECT_CALL(mock_observer1_, availabilityChanged(true));
642
643 base::RunLoop run_loop;
644 for (auto* mock_observer : mock_observers_) {
645 client()->getAvailability(
646 mock_observer->urls(),
647 base::MakeUnique<WebPresentationAvailabilityCallbacks>());
648 client()->startListening(mock_observer);
649 }
650
651 ChangeURLState(gurl1_, URLState::Available);
652 run_loop.RunUntilIdle();
653
654 EXPECT_CALL(mock_observer1_, availabilityChanged(false));
655
656 base::RunLoop run_loop_2;
657 ChangeURLState(gurl1_, URLState::Unavailable);
658 run_loop_2.RunUntilIdle();
659 }
660
661 TEST_F(PresentationDispatcherTest,
662 OnScreenAvailabilityUpdatedInvokesMultipleAvailabilityChanged) {
663 for (const auto& gurl : gurls_) {
664 EXPECT_CALL(presentation_service_, ListenForScreenAvailability(gurl))
665 .Times(1);
666 }
667 for (auto* mock_observer : mock_observers_)
668 EXPECT_CALL(*mock_observer, availabilityChanged(true));
669
670 base::RunLoop run_loop;
671 for (auto* mock_observer : mock_observers_) {
672 client()->getAvailability(
673 mock_observer->urls(),
674 base::MakeUnique<WebPresentationAvailabilityCallbacks>());
675 client()->startListening(mock_observer);
676 }
677
678 ChangeURLState(gurl2_, URLState::Available);
679 run_loop.RunUntilIdle();
680
681 for (auto* mock_observer : mock_observers_)
682 EXPECT_CALL(*mock_observer, availabilityChanged(false));
683
684 base::RunLoop run_loop_2;
685 ChangeURLState(gurl2_, URLState::Unavailable);
686 run_loop_2.RunUntilIdle();
687 }
688
410 } // namespace content 689 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698