1 // Copyright 2018 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "osp/impl/presentation/url_availability_requester.h"
6 
7 #include <chrono>
8 #include <memory>
9 #include <utility>
10 #include <vector>
11 
12 #include "gmock/gmock.h"
13 #include "gtest/gtest.h"
14 #include "osp/impl/quic/quic_client.h"
15 #include "osp/impl/quic/testing/quic_test_support.h"
16 #include "osp/msgs/osp_messages.h"
17 #include "osp/public/network_service_manager.h"
18 #include "osp/public/testing/message_demuxer_test_support.h"
19 #include "platform/test/fake_clock.h"
20 #include "platform/test/fake_task_runner.h"
21 #include "util/osp_logging.h"
22 
23 namespace openscreen {
24 namespace osp {
25 
26 using ::testing::_;
27 using ::testing::Invoke;
28 using ::testing::Mock;
29 using ::testing::Test;
30 
31 namespace {
32 
33 class MockReceiverObserver : public ReceiverObserver {
34  public:
35   ~MockReceiverObserver() override = default;
36 
37   MOCK_METHOD2(OnRequestFailed, void(const std::string&, const std::string&));
38   MOCK_METHOD2(OnReceiverAvailable,
39                void(const std::string&, const std::string&));
40   MOCK_METHOD2(OnReceiverUnavailable,
41                void(const std::string&, const std::string&));
42 };
43 
44 }  // namespace
45 
46 class UrlAvailabilityRequesterTest : public Test {
47  public:
UrlAvailabilityRequesterTest()48   UrlAvailabilityRequesterTest() {
49     fake_clock_ = std::make_unique<FakeClock>(
50         Clock::time_point(std::chrono::milliseconds(1298424)));
51     task_runner_ = std::make_unique<FakeTaskRunner>(fake_clock_.get());
52     quic_bridge_ =
53         std::make_unique<FakeQuicBridge>(task_runner_.get(), FakeClock::now);
54     info1_ = {service_id_, friendly_name_, 1, quic_bridge_->kReceiverEndpoint};
55   }
56 
SetUp()57   void SetUp() override {
58     NetworkServiceManager::Create(nullptr, nullptr,
59                                   std::move(quic_bridge_->quic_client),
60                                   std::move(quic_bridge_->quic_server));
61     availability_watch_ =
62         quic_bridge_->receiver_demuxer->SetDefaultMessageTypeWatch(
63             msgs::Type::kPresentationUrlAvailabilityRequest, &mock_callback_);
64   }
65 
TearDown()66   void TearDown() override {
67     availability_watch_ = MessageDemuxer::MessageWatch();
68     NetworkServiceManager::Dispose();
69   }
70 
71  protected:
ExpectIncomingConnection()72   std::unique_ptr<ProtocolConnection> ExpectIncomingConnection() {
73     std::unique_ptr<ProtocolConnection> stream;
74 
75     EXPECT_CALL(quic_bridge_->mock_server_observer, OnIncomingConnectionMock(_))
76         .WillOnce(
77             Invoke([&stream](std::unique_ptr<ProtocolConnection>& connection) {
78               stream = std::move(connection);
79             }));
80     quic_bridge_->RunTasksUntilIdle();
81 
82     return stream;
83   }
84 
ExpectStreamMessage(MockMessageCallback * mock_callback,msgs::PresentationUrlAvailabilityRequest * request)85   void ExpectStreamMessage(MockMessageCallback* mock_callback,
86                            msgs::PresentationUrlAvailabilityRequest* request) {
87     EXPECT_CALL(*mock_callback, OnStreamMessage(_, _, _, _, _, _))
88         .WillOnce(
89             Invoke([request](uint64_t endpoint_id, uint64_t cid,
90                              msgs::Type message_type, const uint8_t* buffer,
91                              size_t buffer_size, Clock::time_point now) {
92               ssize_t request_result_size =
93                   msgs::DecodePresentationUrlAvailabilityRequest(
94                       buffer, buffer_size, request);
95               OSP_DCHECK_GT(request_result_size, 0);
96               return request_result_size;
97             }));
98   }
99 
SendAvailabilityResponse(const msgs::PresentationUrlAvailabilityRequest & request,std::vector<msgs::UrlAvailability> && availabilities,ProtocolConnection * stream)100   void SendAvailabilityResponse(
101       const msgs::PresentationUrlAvailabilityRequest& request,
102       std::vector<msgs::UrlAvailability>&& availabilities,
103       ProtocolConnection* stream) {
104     msgs::PresentationUrlAvailabilityResponse response;
105     response.request_id = request.request_id;
106     response.url_availabilities = std::move(availabilities);
107     msgs::CborEncodeBuffer buffer;
108     ssize_t encode_result =
109         msgs::EncodePresentationUrlAvailabilityResponse(response, &buffer);
110     ASSERT_GT(encode_result, 0);
111     stream->Write(buffer.data(), buffer.size());
112   }
113 
SendAvailabilityEvent(uint64_t watch_id,std::vector<msgs::UrlAvailability> && availabilities,ProtocolConnection * stream)114   void SendAvailabilityEvent(
115       uint64_t watch_id,
116       std::vector<msgs::UrlAvailability>&& availabilities,
117       ProtocolConnection* stream) {
118     msgs::PresentationUrlAvailabilityEvent event;
119     event.watch_id = watch_id;
120     event.url_availabilities = std::move(availabilities);
121     msgs::CborEncodeBuffer buffer;
122     ssize_t encode_result =
123         msgs::EncodePresentationUrlAvailabilityEvent(event, &buffer);
124     ASSERT_GT(encode_result, 0);
125     stream->Write(buffer.data(), buffer.size());
126   }
127 
128   std::unique_ptr<FakeClock> fake_clock_;
129   std::unique_ptr<FakeTaskRunner> task_runner_;
130   MockMessageCallback mock_callback_;
131   MessageDemuxer::MessageWatch availability_watch_;
132   std::unique_ptr<FakeQuicBridge> quic_bridge_;
133   UrlAvailabilityRequester listener_{FakeClock::now};
134 
135   std::string url1_{"https://example.com/foo.html"};
136   std::string url2_{"https://example.com/bar.html"};
137   std::string service_id_{"asdf"};
138   std::string friendly_name_{"turtle"};
139   ServiceInfo info1_;
140 };
141 
TEST_F(UrlAvailabilityRequesterTest,AvailableObserverFirst)142 TEST_F(UrlAvailabilityRequesterTest, AvailableObserverFirst) {
143   MockReceiverObserver mock_observer;
144   listener_.AddObserver({url1_}, &mock_observer);
145 
146   listener_.AddReceiver(info1_);
147 
148   msgs::PresentationUrlAvailabilityRequest request;
149   ExpectStreamMessage(&mock_callback_, &request);
150 
151   std::unique_ptr<ProtocolConnection> stream = ExpectIncomingConnection();
152   ASSERT_TRUE(stream);
153 
154   EXPECT_EQ(std::vector<std::string>{url1_}, request.urls);
155   SendAvailabilityResponse(
156       request,
157       std::vector<msgs::UrlAvailability>{msgs::UrlAvailability::kAvailable},
158       stream.get());
159 
160   EXPECT_CALL(mock_observer, OnReceiverAvailable(url1_, service_id_));
161   EXPECT_CALL(mock_observer, OnReceiverUnavailable(url1_, service_id_))
162       .Times(0);
163   quic_bridge_->RunTasksUntilIdle();
164 }
165 
TEST_F(UrlAvailabilityRequesterTest,AvailableReceiverFirst)166 TEST_F(UrlAvailabilityRequesterTest, AvailableReceiverFirst) {
167   listener_.AddReceiver(info1_);
168 
169   MockReceiverObserver mock_observer;
170   listener_.AddObserver({url1_}, &mock_observer);
171 
172   msgs::PresentationUrlAvailabilityRequest request;
173   ExpectStreamMessage(&mock_callback_, &request);
174 
175   std::unique_ptr<ProtocolConnection> stream = ExpectIncomingConnection();
176   ASSERT_TRUE(stream);
177 
178   EXPECT_EQ(std::vector<std::string>{url1_}, request.urls);
179   SendAvailabilityResponse(
180       request,
181       std::vector<msgs::UrlAvailability>{msgs::UrlAvailability::kAvailable},
182       stream.get());
183 
184   EXPECT_CALL(mock_observer, OnReceiverAvailable(url1_, service_id_));
185   EXPECT_CALL(mock_observer, OnReceiverUnavailable(url1_, service_id_))
186       .Times(0);
187   quic_bridge_->RunTasksUntilIdle();
188 }
189 
TEST_F(UrlAvailabilityRequesterTest,Unavailable)190 TEST_F(UrlAvailabilityRequesterTest, Unavailable) {
191   listener_.AddReceiver(info1_);
192 
193   MockReceiverObserver mock_observer;
194   listener_.AddObserver({url1_}, &mock_observer);
195 
196   msgs::PresentationUrlAvailabilityRequest request;
197   ExpectStreamMessage(&mock_callback_, &request);
198 
199   std::unique_ptr<ProtocolConnection> stream = ExpectIncomingConnection();
200   ASSERT_TRUE(stream);
201 
202   EXPECT_EQ(std::vector<std::string>{url1_}, request.urls);
203   SendAvailabilityResponse(
204       request,
205       std::vector<msgs::UrlAvailability>{msgs::UrlAvailability::kUnavailable},
206       stream.get());
207 
208   EXPECT_CALL(mock_observer, OnReceiverAvailable(url1_, service_id_)).Times(0);
209   EXPECT_CALL(mock_observer, OnReceiverUnavailable(url1_, service_id_));
210   quic_bridge_->RunTasksUntilIdle();
211 }
212 
TEST_F(UrlAvailabilityRequesterTest,AvailabilityIsCached)213 TEST_F(UrlAvailabilityRequesterTest, AvailabilityIsCached) {
214   listener_.AddReceiver(info1_);
215 
216   MockReceiverObserver mock_observer1;
217   listener_.AddObserver({url1_}, &mock_observer1);
218 
219   msgs::PresentationUrlAvailabilityRequest request;
220   ExpectStreamMessage(&mock_callback_, &request);
221 
222   std::unique_ptr<ProtocolConnection> stream = ExpectIncomingConnection();
223   ASSERT_TRUE(stream);
224 
225   EXPECT_EQ(std::vector<std::string>{url1_}, request.urls);
226   SendAvailabilityResponse(
227       request,
228       std::vector<msgs::UrlAvailability>{msgs::UrlAvailability::kUnavailable},
229       stream.get());
230 
231   EXPECT_CALL(mock_observer1, OnReceiverAvailable(url1_, service_id_)).Times(0);
232   EXPECT_CALL(mock_observer1, OnReceiverUnavailable(url1_, service_id_));
233   quic_bridge_->RunTasksUntilIdle();
234 
235   MockReceiverObserver mock_observer2;
236   EXPECT_CALL(mock_observer2, OnReceiverAvailable(url1_, service_id_)).Times(0);
237   EXPECT_CALL(mock_observer2, OnReceiverUnavailable(url1_, service_id_));
238   listener_.AddObserver({url1_}, &mock_observer2);
239 }
240 
TEST_F(UrlAvailabilityRequesterTest,AvailabilityCacheIsTransient)241 TEST_F(UrlAvailabilityRequesterTest, AvailabilityCacheIsTransient) {
242   listener_.AddReceiver(info1_);
243 
244   MockReceiverObserver mock_observer1;
245   listener_.AddObserver({url1_}, &mock_observer1);
246 
247   msgs::PresentationUrlAvailabilityRequest request;
248   ExpectStreamMessage(&mock_callback_, &request);
249 
250   std::unique_ptr<ProtocolConnection> stream = ExpectIncomingConnection();
251   ASSERT_TRUE(stream);
252 
253   EXPECT_EQ(std::vector<std::string>{url1_}, request.urls);
254   SendAvailabilityResponse(
255       request,
256       std::vector<msgs::UrlAvailability>{msgs::UrlAvailability::kUnavailable},
257       stream.get());
258 
259   EXPECT_CALL(mock_observer1, OnReceiverAvailable(url1_, service_id_)).Times(0);
260   EXPECT_CALL(mock_observer1, OnReceiverUnavailable(url1_, service_id_));
261   quic_bridge_->RunTasksUntilIdle();
262 
263   listener_.RemoveObserverUrls({url1_}, &mock_observer1);
264   MockReceiverObserver mock_observer2;
265   EXPECT_CALL(mock_observer2, OnReceiverAvailable(url1_, service_id_)).Times(0);
266   EXPECT_CALL(mock_observer2, OnReceiverUnavailable(url1_, service_id_))
267       .Times(0);
268   listener_.AddObserver({url1_}, &mock_observer2);
269 }
270 
TEST_F(UrlAvailabilityRequesterTest,PartiallyCachedAnswer)271 TEST_F(UrlAvailabilityRequesterTest, PartiallyCachedAnswer) {
272   listener_.AddReceiver(info1_);
273 
274   MockReceiverObserver mock_observer1;
275   listener_.AddObserver({url1_}, &mock_observer1);
276 
277   msgs::PresentationUrlAvailabilityRequest request;
278   ExpectStreamMessage(&mock_callback_, &request);
279 
280   std::unique_ptr<ProtocolConnection> stream = ExpectIncomingConnection();
281   ASSERT_TRUE(stream);
282 
283   EXPECT_EQ(std::vector<std::string>{url1_}, request.urls);
284   SendAvailabilityResponse(
285       request,
286       std::vector<msgs::UrlAvailability>{msgs::UrlAvailability::kUnavailable},
287       stream.get());
288 
289   EXPECT_CALL(mock_observer1, OnReceiverAvailable(url1_, service_id_)).Times(0);
290   EXPECT_CALL(mock_observer1, OnReceiverUnavailable(url1_, service_id_));
291   quic_bridge_->RunTasksUntilIdle();
292 
293   MockReceiverObserver mock_observer2;
294   EXPECT_CALL(mock_observer2, OnReceiverAvailable(url1_, service_id_)).Times(0);
295   EXPECT_CALL(mock_observer2, OnReceiverUnavailable(url1_, service_id_));
296   listener_.AddObserver({url1_, url2_}, &mock_observer2);
297 
298   ExpectStreamMessage(&mock_callback_, &request);
299   quic_bridge_->RunTasksUntilIdle();
300 
301   EXPECT_EQ(std::vector<std::string>{url2_}, request.urls);
302   SendAvailabilityResponse(
303       request,
304       std::vector<msgs::UrlAvailability>{msgs::UrlAvailability::kUnavailable},
305       stream.get());
306 
307   EXPECT_CALL(mock_observer2, OnReceiverAvailable(url2_, service_id_)).Times(0);
308   EXPECT_CALL(mock_observer2, OnReceiverUnavailable(url2_, service_id_));
309   quic_bridge_->RunTasksUntilIdle();
310 }
311 
TEST_F(UrlAvailabilityRequesterTest,MultipleOverlappingObservers)312 TEST_F(UrlAvailabilityRequesterTest, MultipleOverlappingObservers) {
313   listener_.AddReceiver(info1_);
314 
315   MockReceiverObserver mock_observer1;
316   listener_.AddObserver({url1_}, &mock_observer1);
317 
318   msgs::PresentationUrlAvailabilityRequest request;
319   ExpectStreamMessage(&mock_callback_, &request);
320 
321   std::unique_ptr<ProtocolConnection> stream = ExpectIncomingConnection();
322   ASSERT_TRUE(stream);
323 
324   EXPECT_EQ(std::vector<std::string>{url1_}, request.urls);
325   SendAvailabilityResponse(
326       request,
327       std::vector<msgs::UrlAvailability>{msgs::UrlAvailability::kAvailable},
328       stream.get());
329 
330   EXPECT_CALL(mock_observer1, OnReceiverAvailable(url1_, service_id_));
331   EXPECT_CALL(mock_observer1, OnReceiverUnavailable(url1_, service_id_))
332       .Times(0);
333   quic_bridge_->RunTasksUntilIdle();
334 
335   MockReceiverObserver mock_observer2;
336   EXPECT_CALL(mock_observer2, OnReceiverAvailable(url1_, service_id_));
337   listener_.AddObserver({url1_, url2_}, &mock_observer2);
338   ExpectStreamMessage(&mock_callback_, &request);
339   quic_bridge_->RunTasksUntilIdle();
340 
341   EXPECT_EQ(std::vector<std::string>{url2_}, request.urls);
342   SendAvailabilityResponse(
343       request,
344       std::vector<msgs::UrlAvailability>{msgs::UrlAvailability::kUnavailable},
345       stream.get());
346 
347   EXPECT_CALL(mock_observer1, OnReceiverUnavailable(_, service_id_)).Times(0);
348   EXPECT_CALL(mock_observer2, OnReceiverAvailable(_, service_id_)).Times(0);
349   EXPECT_CALL(mock_observer2, OnReceiverUnavailable(url2_, service_id_));
350   quic_bridge_->RunTasksUntilIdle();
351 }
352 
TEST_F(UrlAvailabilityRequesterTest,RemoveObserverUrls)353 TEST_F(UrlAvailabilityRequesterTest, RemoveObserverUrls) {
354   listener_.AddReceiver(info1_);
355 
356   MockReceiverObserver mock_observer1;
357   listener_.AddObserver({url1_}, &mock_observer1);
358 
359   msgs::PresentationUrlAvailabilityRequest request;
360   ExpectStreamMessage(&mock_callback_, &request);
361   std::unique_ptr<ProtocolConnection> stream = ExpectIncomingConnection();
362   ASSERT_TRUE(stream);
363 
364   EXPECT_EQ(std::vector<std::string>{url1_}, request.urls);
365   uint64_t url1_watch_id = request.watch_id;
366   SendAvailabilityResponse(
367       request,
368       std::vector<msgs::UrlAvailability>{msgs::UrlAvailability::kAvailable},
369       stream.get());
370 
371   EXPECT_CALL(mock_observer1, OnReceiverAvailable(url1_, service_id_));
372   EXPECT_CALL(mock_observer1, OnReceiverUnavailable(url1_, service_id_))
373       .Times(0);
374   quic_bridge_->RunTasksUntilIdle();
375   Mock::VerifyAndClearExpectations(&mock_observer1);
376 
377   MockReceiverObserver mock_observer2;
378   EXPECT_CALL(mock_observer2, OnReceiverAvailable(url1_, service_id_));
379   listener_.AddObserver({url1_, url2_}, &mock_observer2);
380 
381   ExpectStreamMessage(&mock_callback_, &request);
382   quic_bridge_->RunTasksUntilIdle();
383   Mock::VerifyAndClearExpectations(&mock_observer2);
384 
385   EXPECT_EQ(std::vector<std::string>{url2_}, request.urls);
386 
387   listener_.RemoveObserverUrls({url1_}, &mock_observer1);
388   SendAvailabilityResponse(
389       request,
390       std::vector<msgs::UrlAvailability>{msgs::UrlAvailability::kUnavailable},
391       stream.get());
392 
393   EXPECT_CALL(mock_observer2, OnReceiverAvailable(_, service_id_)).Times(0);
394   EXPECT_CALL(mock_observer2, OnReceiverUnavailable(url2_, service_id_));
395   quic_bridge_->RunTasksUntilIdle();
396   Mock::VerifyAndClearExpectations(&mock_observer1);
397   Mock::VerifyAndClearExpectations(&mock_observer2);
398 
399   SendAvailabilityEvent(
400       url1_watch_id,
401       std::vector<msgs::UrlAvailability>{msgs::UrlAvailability::kUnavailable},
402       stream.get());
403 
404   EXPECT_CALL(mock_observer1, OnReceiverUnavailable(url1_, service_id_))
405       .Times(0);
406   EXPECT_CALL(mock_observer2, OnReceiverUnavailable(url1_, service_id_));
407   quic_bridge_->RunTasksUntilIdle();
408   Mock::VerifyAndClearExpectations(&mock_observer1);
409   Mock::VerifyAndClearExpectations(&mock_observer2);
410 }
411 
TEST_F(UrlAvailabilityRequesterTest,RemoveObserver)412 TEST_F(UrlAvailabilityRequesterTest, RemoveObserver) {
413   listener_.AddReceiver(info1_);
414 
415   MockReceiverObserver mock_observer1;
416   listener_.AddObserver({url1_}, &mock_observer1);
417 
418   msgs::PresentationUrlAvailabilityRequest request;
419   ExpectStreamMessage(&mock_callback_, &request);
420 
421   std::unique_ptr<ProtocolConnection> stream = ExpectIncomingConnection();
422   ASSERT_TRUE(stream);
423 
424   EXPECT_EQ(std::vector<std::string>{url1_}, request.urls);
425   uint64_t url1_watch_id = request.watch_id;
426 
427   SendAvailabilityResponse(
428       request,
429       std::vector<msgs::UrlAvailability>{msgs::UrlAvailability::kAvailable},
430       stream.get());
431 
432   EXPECT_CALL(mock_observer1, OnReceiverAvailable(url1_, service_id_));
433   EXPECT_CALL(mock_observer1, OnReceiverUnavailable(url1_, service_id_))
434       .Times(0);
435   quic_bridge_->RunTasksUntilIdle();
436   Mock::VerifyAndClearExpectations(&mock_observer1);
437 
438   MockReceiverObserver mock_observer2;
439   EXPECT_CALL(mock_observer2, OnReceiverAvailable(url1_, service_id_));
440   listener_.AddObserver({url1_, url2_}, &mock_observer2);
441 
442   ExpectStreamMessage(&mock_callback_, &request);
443   quic_bridge_->RunTasksUntilIdle();
444   Mock::VerifyAndClearExpectations(&mock_observer2);
445 
446   uint64_t url2_watch_id = request.watch_id;
447   EXPECT_EQ(std::vector<std::string>{url2_}, request.urls);
448 
449   listener_.RemoveObserver(&mock_observer1);
450   SendAvailabilityResponse(
451       request,
452       std::vector<msgs::UrlAvailability>{msgs::UrlAvailability::kUnavailable},
453       stream.get());
454 
455   EXPECT_CALL(mock_observer2, OnReceiverAvailable(_, service_id_)).Times(0);
456   EXPECT_CALL(mock_observer2, OnReceiverUnavailable(url2_, service_id_));
457   quic_bridge_->RunTasksUntilIdle();
458   Mock::VerifyAndClearExpectations(&mock_observer1);
459   Mock::VerifyAndClearExpectations(&mock_observer2);
460 
461   SendAvailabilityEvent(
462       url1_watch_id,
463       std::vector<msgs::UrlAvailability>{msgs::UrlAvailability::kUnavailable},
464       stream.get());
465 
466   EXPECT_CALL(mock_observer1, OnReceiverUnavailable(url1_, service_id_))
467       .Times(0);
468   EXPECT_CALL(mock_observer2, OnReceiverUnavailable(url1_, service_id_));
469   quic_bridge_->RunTasksUntilIdle();
470   Mock::VerifyAndClearExpectations(&mock_observer1);
471   Mock::VerifyAndClearExpectations(&mock_observer2);
472 
473   listener_.RemoveObserver(&mock_observer2);
474 
475   SendAvailabilityEvent(
476       url1_watch_id,
477       std::vector<msgs::UrlAvailability>{msgs::UrlAvailability::kUnavailable},
478       stream.get());
479 
480   SendAvailabilityEvent(
481       url2_watch_id,
482       std::vector<msgs::UrlAvailability>{msgs::UrlAvailability::kAvailable},
483       stream.get());
484 
485   EXPECT_CALL(mock_observer1, OnReceiverUnavailable(_, service_id_)).Times(0);
486   EXPECT_CALL(mock_observer2, OnReceiverUnavailable(_, service_id_)).Times(0);
487   quic_bridge_->RunTasksUntilIdle();
488   Mock::VerifyAndClearExpectations(&mock_observer1);
489   Mock::VerifyAndClearExpectations(&mock_observer2);
490 }
491 
TEST_F(UrlAvailabilityRequesterTest,EventUpdate)492 TEST_F(UrlAvailabilityRequesterTest, EventUpdate) {
493   listener_.AddReceiver(info1_);
494 
495   MockReceiverObserver mock_observer1;
496   listener_.AddObserver({url1_, url2_}, &mock_observer1);
497 
498   msgs::PresentationUrlAvailabilityRequest request;
499   ExpectStreamMessage(&mock_callback_, &request);
500 
501   std::unique_ptr<ProtocolConnection> stream = ExpectIncomingConnection();
502   ASSERT_TRUE(stream);
503 
504   EXPECT_EQ((std::vector<std::string>{url1_, url2_}), request.urls);
505   SendAvailabilityResponse(
506       request,
507       std::vector<msgs::UrlAvailability>{msgs::UrlAvailability::kAvailable,
508                                          msgs::UrlAvailability::kAvailable},
509       stream.get());
510 
511   EXPECT_CALL(mock_observer1, OnReceiverAvailable(url1_, service_id_));
512   EXPECT_CALL(mock_observer1, OnReceiverAvailable(url2_, service_id_));
513   EXPECT_CALL(mock_observer1, OnReceiverUnavailable(_, service_id_)).Times(0);
514   quic_bridge_->RunTasksUntilIdle();
515 
516   EXPECT_CALL(mock_callback_, OnStreamMessage(_, _, _, _, _, _)).Times(0);
517   SendAvailabilityEvent(
518       request.watch_id,
519       std::vector<msgs::UrlAvailability>{msgs::UrlAvailability::kAvailable,
520                                          msgs::UrlAvailability::kUnavailable},
521       stream.get());
522 
523   EXPECT_CALL(mock_observer1, OnReceiverUnavailable(url2_, service_id_));
524   quic_bridge_->RunTasksUntilIdle();
525 }
526 
TEST_F(UrlAvailabilityRequesterTest,RefreshWatches)527 TEST_F(UrlAvailabilityRequesterTest, RefreshWatches) {
528   listener_.AddReceiver(info1_);
529 
530   MockReceiverObserver mock_observer1;
531   listener_.AddObserver({url1_}, &mock_observer1);
532 
533   msgs::PresentationUrlAvailabilityRequest request;
534   ExpectStreamMessage(&mock_callback_, &request);
535 
536   std::unique_ptr<ProtocolConnection> stream = ExpectIncomingConnection();
537   ASSERT_TRUE(stream);
538 
539   EXPECT_EQ(std::vector<std::string>{url1_}, request.urls);
540   SendAvailabilityResponse(
541       request,
542       std::vector<msgs::UrlAvailability>{msgs::UrlAvailability::kAvailable},
543       stream.get());
544 
545   EXPECT_CALL(mock_observer1, OnReceiverAvailable(url1_, service_id_));
546   EXPECT_CALL(mock_observer1, OnReceiverUnavailable(_, service_id_)).Times(0);
547   quic_bridge_->RunTasksUntilIdle();
548 
549   fake_clock_->Advance(std::chrono::seconds(60));
550 
551   ExpectStreamMessage(&mock_callback_, &request);
552   listener_.RefreshWatches();
553   quic_bridge_->RunTasksUntilIdle();
554 
555   EXPECT_EQ(std::vector<std::string>{url1_}, request.urls);
556   SendAvailabilityResponse(
557       request,
558       std::vector<msgs::UrlAvailability>{msgs::UrlAvailability::kUnavailable},
559       stream.get());
560 
561   EXPECT_CALL(mock_observer1, OnReceiverUnavailable(url1_, service_id_));
562   quic_bridge_->RunTasksUntilIdle();
563 }
564 
TEST_F(UrlAvailabilityRequesterTest,ResponseAfterRemoveObserver)565 TEST_F(UrlAvailabilityRequesterTest, ResponseAfterRemoveObserver) {
566   listener_.AddReceiver(info1_);
567 
568   MockReceiverObserver mock_observer1;
569   listener_.AddObserver({url1_}, &mock_observer1);
570 
571   msgs::PresentationUrlAvailabilityRequest request;
572   ExpectStreamMessage(&mock_callback_, &request);
573 
574   std::unique_ptr<ProtocolConnection> stream = ExpectIncomingConnection();
575   ASSERT_TRUE(stream);
576 
577   EXPECT_EQ(std::vector<std::string>{url1_}, request.urls);
578   listener_.RemoveObserverUrls({url1_}, &mock_observer1);
579   SendAvailabilityResponse(
580       request,
581       std::vector<msgs::UrlAvailability>{msgs::UrlAvailability::kAvailable},
582       stream.get());
583 
584   EXPECT_CALL(mock_observer1, OnReceiverAvailable(url1_, service_id_)).Times(0);
585   EXPECT_CALL(mock_observer1, OnReceiverUnavailable(url1_, service_id_))
586       .Times(0);
587   quic_bridge_->RunTasksUntilIdle();
588 
589   MockReceiverObserver mock_observer2;
590   EXPECT_CALL(mock_observer2, OnReceiverAvailable(url1_, service_id_)).Times(0);
591   EXPECT_CALL(mock_observer2, OnReceiverUnavailable(url1_, service_id_))
592       .Times(0);
593   listener_.AddObserver({url1_}, &mock_observer2);
594 }
595 
TEST_F(UrlAvailabilityRequesterTest,EmptyCacheAfterRemoveObserverThenReceiver)596 TEST_F(UrlAvailabilityRequesterTest,
597        EmptyCacheAfterRemoveObserverThenReceiver) {
598   listener_.AddReceiver(info1_);
599 
600   MockReceiverObserver mock_observer1;
601   listener_.AddObserver({url1_}, &mock_observer1);
602 
603   msgs::PresentationUrlAvailabilityRequest request;
604   ExpectStreamMessage(&mock_callback_, &request);
605 
606   std::unique_ptr<ProtocolConnection> stream = ExpectIncomingConnection();
607   ASSERT_TRUE(stream);
608 
609   EXPECT_EQ(std::vector<std::string>{url1_}, request.urls);
610   SendAvailabilityResponse(
611       request,
612       std::vector<msgs::UrlAvailability>{msgs::UrlAvailability::kAvailable},
613       stream.get());
614 
615   EXPECT_CALL(mock_observer1, OnReceiverAvailable(url1_, service_id_));
616   EXPECT_CALL(mock_observer1, OnReceiverUnavailable(url1_, service_id_))
617       .Times(0);
618   quic_bridge_->RunTasksUntilIdle();
619 
620   listener_.RemoveObserverUrls({url1_}, &mock_observer1);
621   listener_.RemoveReceiver(info1_);
622   MockReceiverObserver mock_observer2;
623   EXPECT_CALL(mock_observer2, OnReceiverAvailable(url1_, service_id_)).Times(0);
624   EXPECT_CALL(mock_observer2, OnReceiverUnavailable(url1_, service_id_))
625       .Times(0);
626   listener_.AddObserver({url1_}, &mock_observer2);
627 }
628 
TEST_F(UrlAvailabilityRequesterTest,RemoveObserverInSteps)629 TEST_F(UrlAvailabilityRequesterTest, RemoveObserverInSteps) {
630   listener_.AddReceiver(info1_);
631 
632   MockReceiverObserver mock_observer1;
633   listener_.AddObserver({url1_, url2_}, &mock_observer1);
634 
635   msgs::PresentationUrlAvailabilityRequest request;
636   ExpectStreamMessage(&mock_callback_, &request);
637 
638   std::unique_ptr<ProtocolConnection> stream = ExpectIncomingConnection();
639   ASSERT_TRUE(stream);
640 
641   EXPECT_EQ((std::vector<std::string>{url1_, url2_}), request.urls);
642   listener_.RemoveObserverUrls({url1_}, &mock_observer1);
643   listener_.RemoveObserverUrls({url2_}, &mock_observer1);
644   SendAvailabilityResponse(
645       request,
646       std::vector<msgs::UrlAvailability>{msgs::UrlAvailability::kAvailable,
647                                          msgs::UrlAvailability::kAvailable},
648       stream.get());
649   SendAvailabilityEvent(
650       request.watch_id,
651       std::vector<msgs::UrlAvailability>{msgs::UrlAvailability::kUnavailable,
652                                          msgs::UrlAvailability::kUnavailable},
653       stream.get());
654 
655   EXPECT_CALL(mock_observer1, OnReceiverAvailable(url1_, service_id_)).Times(0);
656   EXPECT_CALL(mock_observer1, OnReceiverUnavailable(url1_, service_id_))
657       .Times(0);
658   EXPECT_CALL(mock_observer1, OnReceiverAvailable(url2_, service_id_)).Times(0);
659   EXPECT_CALL(mock_observer1, OnReceiverUnavailable(url2_, service_id_))
660       .Times(0);
661 
662   // NOTE: This message was generated between the two RemoveObserverUrls calls
663   // above.  So even though the request is internally cancelled almost
664   // immediately, this still went out on the wire.
665   ExpectStreamMessage(&mock_callback_, &request);
666 
667   quic_bridge_->RunTasksUntilIdle();
668   EXPECT_EQ((std::vector<std::string>{url2_}), request.urls);
669 
670   fake_clock_->Advance(std::chrono::seconds(60));
671 
672   listener_.RefreshWatches();
673   EXPECT_CALL(mock_callback_, OnStreamMessage(_, _, _, _, _, _)).Times(0);
674   quic_bridge_->RunTasksUntilIdle();
675 }
676 
677 }  // namespace osp
678 }  // namespace openscreen
679