1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stddef.h>
#include <stdint.h>
#include <string>
#include <utility>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "base/run_loop.h"
#include "base/synchronization/waitable_event.h"
#include "base/test/histogram_tester.h"
#include "base/thread_task_runner_handle.h"
#include "chrome/browser/media/router/issue.h"
#include "chrome/browser/media/router/media_route.h"
#include "chrome/browser/media/router/media_router_metrics.h"
#include "chrome/browser/media/router/media_router_mojo_test.h"
#include "chrome/browser/media/router/media_router_type_converters.h"
#include "chrome/browser/media/router/mock_media_router.h"
#include "chrome/browser/media/router/presentation_session_messages_observer.h"
#include "chrome/browser/media/router/test_helper.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/process_manager.h"
#include "extensions/browser/process_manager_factory.h"
#include "media/base/gmock_callback_support.h"
#include "mojo/message_pump/message_pump_mojo.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using testing::_;
using testing::Eq;
using testing::Invoke;
using testing::InvokeWithoutArgs;
using testing::IsEmpty;
using testing::Mock;
using testing::Not;
using testing::Pointee;
using testing::Return;
using testing::ReturnRef;
using testing::SaveArg;
namespace media_router {
namespace {
const char kDescription[] = "description";
const char kError[] = "error";
const char kExtensionId[] = "extension1234";
const char kMessage[] = "message";
const char kSource[] = "source1";
const char kSource2[] = "source2";
const char kRouteId[] = "routeId";
const char kRouteId2[] = "routeId2";
const char kJoinableRouteId[] = "joinableRouteId";
const char kJoinableRouteId2[] = "joinableRouteId2";
const char kSinkId[] = "sink";
const char kSinkId2[] = "sink2";
const char kSinkName[] = "sinkName";
const char kPresentationId[] = "presentationId";
const char kOrigin[] = "http://origin/";
const int kInvalidTabId = -1;
const uint8_t kBinaryMessage[] = {0x01, 0x02, 0x03, 0x04};
const int kTimeoutMillis = 5 * 1000;
bool ArePresentationSessionMessagesEqual(
const content::PresentationSessionMessage* expected,
const content::PresentationSessionMessage* actual) {
if (expected->type != actual->type)
return false;
return expected->is_binary() ? *expected->data == *actual->data
: expected->message == actual->message;
}
interfaces::IssuePtr CreateMojoIssue(const std::string& title) {
interfaces::IssuePtr mojoIssue = interfaces::Issue::New();
mojoIssue->title = title;
mojoIssue->message = "msg";
mojoIssue->route_id = "";
mojoIssue->default_action = interfaces::Issue::ActionType::DISMISS;
mojoIssue->secondary_actions =
mojo::Array<interfaces::Issue::ActionType>::New(0);
mojoIssue->severity = interfaces::Issue::Severity::WARNING;
mojoIssue->is_blocking = false;
mojoIssue->help_url = "";
return mojoIssue;
}
} // namespace
class RouteResponseCallbackHandler {
public:
void Invoke(const RouteRequestResult& result) {
DoInvoke(result.route(), result.presentation_id(), result.error(),
result.result_code());
}
MOCK_METHOD4(DoInvoke,
void(const MediaRoute* route,
const std::string& presentation_id,
const std::string& error_text,
RouteRequestResult::ResultCode result_code));
};
class SendMessageCallbackHandler {
public:
MOCK_METHOD1(Invoke, void(bool));
};
class ListenForMessagesCallbackHandler {
public:
ListenForMessagesCallbackHandler(
ScopedVector<content::PresentationSessionMessage> expected_messages,
bool pass_ownership)
: expected_messages_(std::move(expected_messages)),
pass_ownership_(pass_ownership) {}
void Invoke(const ScopedVector<content::PresentationSessionMessage>& messages,
bool pass_ownership) {
InvokeObserver();
EXPECT_EQ(pass_ownership_, pass_ownership);
EXPECT_EQ(messages.size(), expected_messages_.size());
for (size_t i = 0; i < expected_messages_.size(); ++i) {
EXPECT_TRUE(ArePresentationSessionMessagesEqual(expected_messages_[i],
messages[i]));
}
}
MOCK_METHOD0(InvokeObserver, void());
private:
ScopedVector<content::PresentationSessionMessage> expected_messages_;
bool pass_ownership_;
};
template <typename T>
void StoreAndRun(T* result, const base::Closure& closure, const T& result_val) {
*result = result_val;
closure.Run();
}
class MediaRouterMojoImplTest : public MediaRouterMojoTest {
public:
MediaRouterMojoImplTest() {}
~MediaRouterMojoImplTest() override {}
};
// ProcessManager with a mocked method subset, for testing extension suspend
// handling.
class TestProcessManager : public extensions::ProcessManager {
public:
explicit TestProcessManager(content::BrowserContext* context)
: extensions::ProcessManager(
context,
context,
extensions::ExtensionRegistry::Get(context)) {}
~TestProcessManager() override {}
static scoped_ptr<KeyedService> Create(content::BrowserContext* context) {
return make_scoped_ptr(new TestProcessManager(context));
}
MOCK_METHOD1(IsEventPageSuspended, bool(const std::string& ext_id));
MOCK_METHOD2(WakeEventPage,
bool(const std::string& extension_id,
const base::Callback<void(bool)>& callback));
private:
DISALLOW_COPY_AND_ASSIGN(TestProcessManager);
};
// Mockable class for awaiting RegisterMediaRouteProvider callbacks.
class RegisterMediaRouteProviderHandler {
public:
MOCK_METHOD1(Invoke, void(const std::string& instance_id));
};
TEST_F(MediaRouterMojoImplTest, CreateRoute) {
MediaSource media_source(kSource);
MediaRoute expected_route(kRouteId, media_source,
kSinkId, "", false, "", false);
// Use a lambda function as an invocation target here to work around
// a limitation with GMock::Invoke that prevents it from using move-only types
// in runnable parameter lists.
EXPECT_CALL(mock_media_route_provider_,
CreateRoute(mojo::String(kSource), mojo::String(kSinkId), _,
mojo::String(kOrigin), kInvalidTabId, _, _))
.WillOnce(Invoke(
[](const mojo::String& source, const mojo::String& sink,
const mojo::String& presentation_id, const mojo::String& origin,
int tab_id, int64_t timeout_millis,
const interfaces::MediaRouteProvider::CreateRouteCallback& cb) {
interfaces::MediaRoutePtr route = interfaces::MediaRoute::New();
route->media_source = kSource;
route->media_sink_id = kSinkId;
route->media_route_id = kRouteId;
route->description = kDescription;
route->is_local = true;
route->for_display = true;
cb.Run(std::move(route), mojo::String(),
interfaces::RouteRequestResultCode::OK);
}));
base::RunLoop run_loop;
RouteResponseCallbackHandler handler;
EXPECT_CALL(handler, DoInvoke(Pointee(Equals(expected_route)), Not(""), "",
RouteRequestResult::OK))
.WillOnce(InvokeWithoutArgs([&run_loop]() { run_loop.Quit(); }));
std::vector<MediaRouteResponseCallback> route_response_callbacks;
route_response_callbacks.push_back(base::Bind(
&RouteResponseCallbackHandler::Invoke, base::Unretained(&handler)));
router()->CreateRoute(kSource, kSinkId, GURL(kOrigin), nullptr,
route_response_callbacks,
base::TimeDelta::FromMilliseconds(kTimeoutMillis));
run_loop.Run();
}
TEST_F(MediaRouterMojoImplTest, CreateRouteFails) {
EXPECT_CALL(
mock_media_route_provider_,
CreateRoute(mojo::String(kSource), mojo::String(kSinkId), _,
mojo::String(kOrigin), kInvalidTabId, kTimeoutMillis, _))
.WillOnce(Invoke(
[](const mojo::String& source, const mojo::String& sink,
const mojo::String& presentation_id, const mojo::String& origin,
int tab_id, int64_t timeout_millis,
const interfaces::MediaRouteProvider::CreateRouteCallback& cb) {
cb.Run(interfaces::MediaRoutePtr(), mojo::String(kError),
interfaces::RouteRequestResultCode::TIMED_OUT);
}));
RouteResponseCallbackHandler handler;
base::RunLoop run_loop;
EXPECT_CALL(handler,
DoInvoke(nullptr, "", kError, RouteRequestResult::TIMED_OUT))
.WillOnce(InvokeWithoutArgs([&run_loop]() { run_loop.Quit(); }));
std::vector<MediaRouteResponseCallback> route_response_callbacks;
route_response_callbacks.push_back(base::Bind(
&RouteResponseCallbackHandler::Invoke, base::Unretained(&handler)));
router()->CreateRoute(kSource, kSinkId, GURL(kOrigin), nullptr,
route_response_callbacks,
base::TimeDelta::FromMilliseconds(kTimeoutMillis));
run_loop.Run();
}
TEST_F(MediaRouterMojoImplTest, JoinRoute) {
MediaSource media_source(kSource);
MediaRoute expected_route(kRouteId, media_source,
kSinkId, "", false, "", false);
interfaces::MediaRoutePtr route = interfaces::MediaRoute::New();
route->media_source = kSource;
route->media_sink_id = kSinkId;
route->media_route_id = kRouteId;
route->description = kDescription;
route->is_local = true;
route->for_display = true;
// Use a lambda function as an invocation target here to work around
// a limitation with GMock::Invoke that prevents it from using move-only types
// in runnable parameter lists.
EXPECT_CALL(
mock_media_route_provider_,
JoinRoute(mojo::String(kSource), mojo::String(kPresentationId),
mojo::String(kOrigin), kInvalidTabId, kTimeoutMillis, _))
.WillOnce(Invoke([&route](
const mojo::String& source, const mojo::String& presentation_id,
const mojo::String& origin, int tab_id, int64_t timeout_millis,
const interfaces::MediaRouteProvider::JoinRouteCallback& cb) {
cb.Run(std::move(route), mojo::String(),
interfaces::RouteRequestResultCode::OK);
}));
RouteResponseCallbackHandler handler;
base::RunLoop run_loop;
EXPECT_CALL(handler, DoInvoke(Pointee(Equals(expected_route)), Not(""), "",
RouteRequestResult::OK))
.WillOnce(InvokeWithoutArgs([&run_loop]() { run_loop.Quit(); }));
std::vector<MediaRouteResponseCallback> route_response_callbacks;
route_response_callbacks.push_back(base::Bind(
&RouteResponseCallbackHandler::Invoke, base::Unretained(&handler)));
router()->JoinRoute(kSource, kPresentationId, GURL(kOrigin), nullptr,
route_response_callbacks,
base::TimeDelta::FromMilliseconds(kTimeoutMillis));
run_loop.Run();
}
TEST_F(MediaRouterMojoImplTest, JoinRouteFails) {
EXPECT_CALL(
mock_media_route_provider_,
JoinRoute(mojo::String(kSource), mojo::String(kPresentationId),
mojo::String(kOrigin), kInvalidTabId, kTimeoutMillis, _))
.WillOnce(Invoke(
[](const mojo::String& source, const mojo::String& presentation_id,
const mojo::String& origin, int tab_id, int64_t timeout_millis,
const interfaces::MediaRouteProvider::JoinRouteCallback& cb) {
cb.Run(interfaces::MediaRoutePtr(), mojo::String(kError),
interfaces::RouteRequestResultCode::TIMED_OUT);
}));
RouteResponseCallbackHandler handler;
base::RunLoop run_loop;
EXPECT_CALL(handler,
DoInvoke(nullptr, "", kError, RouteRequestResult::TIMED_OUT))
.WillOnce(InvokeWithoutArgs([&run_loop]() { run_loop.Quit(); }));
std::vector<MediaRouteResponseCallback> route_response_callbacks;
route_response_callbacks.push_back(base::Bind(
&RouteResponseCallbackHandler::Invoke, base::Unretained(&handler)));
router()->JoinRoute(kSource, kPresentationId, GURL(kOrigin), nullptr,
route_response_callbacks,
base::TimeDelta::FromMilliseconds(kTimeoutMillis));
run_loop.Run();
}
TEST_F(MediaRouterMojoImplTest, ConnectRouteByRouteId) {
MediaSource media_source(kSource);
MediaRoute expected_route(kRouteId, media_source,
kSinkId, "", false, "", false);
interfaces::MediaRoutePtr route = interfaces::MediaRoute::New();
route->media_source = kSource;
route->media_sink_id = kSinkId;
route->media_route_id = kRouteId;
route->description = kDescription;
route->is_local = true;
route->for_display = true;
// Use a lambda function as an invocation target here to work around
// a limitation with GMock::Invoke that prevents it from using move-only types
// in runnable parameter lists.
EXPECT_CALL(mock_media_route_provider_,
ConnectRouteByRouteId(
mojo::String(kSource), mojo::String(kRouteId), _,
mojo::String(kOrigin), kInvalidTabId, kTimeoutMillis, _))
.WillOnce(Invoke([&route](
const mojo::String& source, const mojo::String& route_id,
const mojo::String& presentation_id, const mojo::String& origin,
int tab_id, int64_t timeout_millis,
const interfaces::MediaRouteProvider::JoinRouteCallback& cb) {
cb.Run(std::move(route), mojo::String(),
interfaces::RouteRequestResultCode::OK);
}));
RouteResponseCallbackHandler handler;
base::RunLoop run_loop;
EXPECT_CALL(handler, DoInvoke(Pointee(Equals(expected_route)), Not(""), "",
RouteRequestResult::OK))
.WillOnce(InvokeWithoutArgs([&run_loop]() { run_loop.Quit(); }));
std::vector<MediaRouteResponseCallback> route_response_callbacks;
route_response_callbacks.push_back(base::Bind(
&RouteResponseCallbackHandler::Invoke, base::Unretained(&handler)));
router()->ConnectRouteByRouteId(
kSource, kRouteId, GURL(kOrigin), nullptr, route_response_callbacks,
base::TimeDelta::FromMilliseconds(kTimeoutMillis));
run_loop.Run();
}
TEST_F(MediaRouterMojoImplTest, ConnectRouteByRouteIdFails) {
EXPECT_CALL(mock_media_route_provider_,
ConnectRouteByRouteId(
mojo::String(kSource), mojo::String(kRouteId), _,
mojo::String(kOrigin), kInvalidTabId, kTimeoutMillis, _))
.WillOnce(Invoke(
[](const mojo::String& source, const mojo::String& route_id,
const mojo::String& presentation_id, const mojo::String& origin,
int tab_id, int64_t timeout_millis,
const interfaces::MediaRouteProvider::JoinRouteCallback& cb) {
cb.Run(interfaces::MediaRoutePtr(), mojo::String(kError),
interfaces::RouteRequestResultCode::TIMED_OUT);
}));
RouteResponseCallbackHandler handler;
base::RunLoop run_loop;
EXPECT_CALL(handler,
DoInvoke(nullptr, "", kError, RouteRequestResult::TIMED_OUT))
.WillOnce(InvokeWithoutArgs([&run_loop]() { run_loop.Quit(); }));
std::vector<MediaRouteResponseCallback> route_response_callbacks;
route_response_callbacks.push_back(base::Bind(
&RouteResponseCallbackHandler::Invoke, base::Unretained(&handler)));
router()->ConnectRouteByRouteId(
kSource, kRouteId, GURL(kOrigin), nullptr, route_response_callbacks,
base::TimeDelta::FromMilliseconds(kTimeoutMillis));
run_loop.Run();
}
TEST_F(MediaRouterMojoImplTest, DetachRoute) {
base::RunLoop run_loop;
EXPECT_CALL(mock_media_route_provider_, DetachRoute(mojo::String(kRouteId)))
.WillOnce(InvokeWithoutArgs([&run_loop]() {
run_loop.Quit();
}));
router()->DetachRoute(kRouteId);
run_loop.Run();
}
TEST_F(MediaRouterMojoImplTest, TerminateRoute) {
base::RunLoop run_loop;
EXPECT_CALL(mock_media_route_provider_,
TerminateRoute(mojo::String(kRouteId)))
.WillOnce(InvokeWithoutArgs([&run_loop]() {
run_loop.Quit();
}));
router()->TerminateRoute(kRouteId);
run_loop.Run();
}
TEST_F(MediaRouterMojoImplTest, HandleIssue) {
MockIssuesObserver issue_observer1(router());
MockIssuesObserver issue_observer2(router());
issue_observer1.RegisterObserver();
issue_observer2.RegisterObserver();
interfaces::IssuePtr mojo_issue1 = CreateMojoIssue("title 1");
const Issue& expected_issue1 = mojo_issue1.To<Issue>();
const Issue* issue;
EXPECT_CALL(issue_observer1,
OnIssueUpdated(Pointee(EqualsIssue(expected_issue1))))
.WillOnce(SaveArg<0>(&issue));
base::RunLoop run_loop;
EXPECT_CALL(issue_observer2,
OnIssueUpdated(Pointee(EqualsIssue(expected_issue1))))
.WillOnce(InvokeWithoutArgs([&run_loop]() {
run_loop.Quit();
}));
media_router_proxy_->OnIssue(std::move(mojo_issue1));
run_loop.Run();
EXPECT_TRUE(Mock::VerifyAndClearExpectations(&issue_observer1));
EXPECT_TRUE(Mock::VerifyAndClearExpectations(&issue_observer2));
EXPECT_CALL(issue_observer1, OnIssueUpdated(nullptr));
EXPECT_CALL(issue_observer2, OnIssueUpdated(nullptr));
router()->ClearIssue(issue->id());
EXPECT_TRUE(Mock::VerifyAndClearExpectations(&issue_observer1));
EXPECT_TRUE(Mock::VerifyAndClearExpectations(&issue_observer2));
router()->UnregisterIssuesObserver(&issue_observer1);
interfaces::IssuePtr mojo_issue2 = CreateMojoIssue("title 2");
const Issue& expected_issue2 = mojo_issue2.To<Issue>();
EXPECT_CALL(issue_observer2,
OnIssueUpdated(Pointee(EqualsIssue(expected_issue2))));
router()->AddIssue(expected_issue2);
EXPECT_TRUE(Mock::VerifyAndClearExpectations(&issue_observer2));
EXPECT_CALL(issue_observer2, OnIssueUpdated(nullptr));
router()->ClearIssue(issue->id());
EXPECT_TRUE(Mock::VerifyAndClearExpectations(&issue_observer2));
base::RunLoop run_loop2;
EXPECT_CALL(issue_observer2,
OnIssueUpdated(Pointee(EqualsIssue(expected_issue2))))
.WillOnce(InvokeWithoutArgs([&run_loop2]() {
run_loop2.Quit();
}));
media_router_proxy_->OnIssue(std::move(mojo_issue2));
run_loop2.Run();
issue_observer1.UnregisterObserver();
issue_observer2.UnregisterObserver();
}
TEST_F(MediaRouterMojoImplTest, RegisterAndUnregisterMediaSinksObserver) {
router()->OnSinkAvailabilityUpdated(
interfaces::MediaRouter::SinkAvailability::AVAILABLE);
MediaSource media_source(kSource);
// These should only be called once even if there is more than one observer
// for a given source.
EXPECT_CALL(mock_media_route_provider_,
StartObservingMediaSinks(mojo::String(kSource)));
EXPECT_CALL(mock_media_route_provider_,
StartObservingMediaSinks(mojo::String(kSource2)));
scoped_ptr<MockMediaSinksObserver> sinks_observer(
new MockMediaSinksObserver(router(), media_source));
EXPECT_TRUE(sinks_observer->Init());
scoped_ptr<MockMediaSinksObserver> extra_sinks_observer(
new MockMediaSinksObserver(router(), media_source));
EXPECT_TRUE(extra_sinks_observer->Init());
scoped_ptr<MockMediaSinksObserver> unrelated_sinks_observer(
new MockMediaSinksObserver(router(), MediaSource(kSource2)));
EXPECT_TRUE(unrelated_sinks_observer->Init());
ProcessEventLoop();
std::vector<MediaSink> expected_sinks;
expected_sinks.push_back(
MediaSink(kSinkId, kSinkName, MediaSink::IconType::CAST));
expected_sinks.push_back(
MediaSink(kSinkId2, kSinkName, MediaSink::IconType::CAST));
mojo::Array<interfaces::MediaSinkPtr> mojo_sinks(2);
mojo_sinks[0] = interfaces::MediaSink::New();
mojo_sinks[0]->sink_id = kSinkId;
mojo_sinks[0]->name = kSinkName;
mojo_sinks[0]->icon_type =
media_router::interfaces::MediaSink::IconType::CAST;
mojo_sinks[1] = interfaces::MediaSink::New();
mojo_sinks[1]->sink_id = kSinkId2;
mojo_sinks[1]->name = kSinkName;
mojo_sinks[1]->icon_type =
media_router::interfaces::MediaSink::IconType::CAST;
base::RunLoop run_loop;
EXPECT_CALL(*sinks_observer, OnSinksReceived(SequenceEquals(expected_sinks)));
EXPECT_CALL(*extra_sinks_observer,
OnSinksReceived(SequenceEquals(expected_sinks)))
.WillOnce(InvokeWithoutArgs([&run_loop]() { run_loop.Quit(); }));
media_router_proxy_->OnSinksReceived(media_source.id(),
std::move(mojo_sinks));
run_loop.Run();
// Since the MediaRouterMojoImpl has already received results for
// |media_source|, return cached results to observers that are subsequently
// registered.
scoped_ptr<MockMediaSinksObserver> cached_sinks_observer(
new MockMediaSinksObserver(router(), media_source));
EXPECT_CALL(*cached_sinks_observer,
OnSinksReceived(SequenceEquals(expected_sinks)));
EXPECT_TRUE(cached_sinks_observer->Init());
base::RunLoop run_loop2;
EXPECT_CALL(mock_media_route_provider_,
StopObservingMediaSinks(mojo::String(kSource)));
EXPECT_CALL(mock_media_route_provider_,
StopObservingMediaSinks(mojo::String(kSource2)))
.WillOnce(InvokeWithoutArgs([&run_loop2]() {
run_loop2.Quit();
}));
sinks_observer.reset();
extra_sinks_observer.reset();
unrelated_sinks_observer.reset();
cached_sinks_observer.reset();
run_loop2.Run();
}
TEST_F(MediaRouterMojoImplTest,
RegisterMediaSinksObserverWithAvailabilityChange) {
// When availability is UNAVAILABLE, no calls should be made to MRPM.
router()->OnSinkAvailabilityUpdated(
interfaces::MediaRouter::SinkAvailability::UNAVAILABLE);
MediaSource media_source(kSource);
scoped_ptr<MockMediaSinksObserver> sinks_observer(
new MockMediaSinksObserver(router(), media_source));
EXPECT_CALL(*sinks_observer, OnSinksReceived(IsEmpty()));
EXPECT_TRUE(sinks_observer->Init());
MediaSource media_source2(kSource2);
scoped_ptr<MockMediaSinksObserver> sinks_observer2(
new MockMediaSinksObserver(router(), media_source2));
EXPECT_CALL(*sinks_observer2, OnSinksReceived(IsEmpty()));
EXPECT_TRUE(sinks_observer2->Init());
EXPECT_CALL(mock_media_route_provider_,
StartObservingMediaSinks(mojo::String(kSource)))
.Times(0);
EXPECT_CALL(mock_media_route_provider_,
StartObservingMediaSinks(mojo::String(kSource2)))
.Times(0);
ProcessEventLoop();
EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_media_route_provider_));
// When availability transitions AVAILABLE, existing sink queries should be
// sent to MRPM.
router()->OnSinkAvailabilityUpdated(
interfaces::MediaRouter::SinkAvailability::AVAILABLE);
EXPECT_CALL(mock_media_route_provider_,
StartObservingMediaSinks(mojo::String(kSource)))
.Times(1);
EXPECT_CALL(mock_media_route_provider_,
StartObservingMediaSinks(mojo::String(kSource2)))
.Times(1);
ProcessEventLoop();
EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_media_route_provider_));
// No change in availability status; no calls should be made to MRPM.
router()->OnSinkAvailabilityUpdated(
interfaces::MediaRouter::SinkAvailability::AVAILABLE);
EXPECT_CALL(mock_media_route_provider_,
StartObservingMediaSinks(mojo::String(kSource)))
.Times(0);
EXPECT_CALL(mock_media_route_provider_,
StartObservingMediaSinks(mojo::String(kSource2)))
.Times(0);
ProcessEventLoop();
EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_media_route_provider_));
// When availability is UNAVAILABLE, queries are already removed from MRPM.
// Unregistering observer won't result in call to MRPM to remove query.
router()->OnSinkAvailabilityUpdated(
interfaces::MediaRouter::SinkAvailability::UNAVAILABLE);
EXPECT_CALL(mock_media_route_provider_,
StopObservingMediaSinks(mojo::String(kSource)))
.Times(0);
sinks_observer.reset();
ProcessEventLoop();
EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_media_route_provider_));
// When availability is AVAILABLE, call is made to MRPM to remove query when
// observer is unregistered.
router()->OnSinkAvailabilityUpdated(
interfaces::MediaRouter::SinkAvailability::AVAILABLE);
EXPECT_CALL(mock_media_route_provider_,
StopObservingMediaSinks(mojo::String(kSource2)));
sinks_observer2.reset();
ProcessEventLoop();
}
TEST_F(MediaRouterMojoImplTest, RegisterAndUnregisterMediaRoutesObserver) {
MockMediaRouter mock_router;
MediaSource media_source(kSource);
MediaSource different_media_source(kSource2);
EXPECT_CALL(mock_media_route_provider_,
StartObservingMediaRoutes(mojo::String(media_source.id()))).Times(2);
EXPECT_CALL(mock_media_route_provider_,
StartObservingMediaRoutes(
mojo::String(different_media_source.id()))).Times(1);
MediaRoutesObserver* observer_captured;
EXPECT_CALL(mock_router, RegisterMediaRoutesObserver(_))
.Times(3)
.WillRepeatedly(SaveArg<0>(&observer_captured));
MockMediaRoutesObserver routes_observer(&mock_router, media_source.id());
EXPECT_EQ(observer_captured, &routes_observer);
MockMediaRoutesObserver extra_routes_observer(&mock_router,
media_source.id());
EXPECT_EQ(observer_captured, &extra_routes_observer);
MockMediaRoutesObserver different_routes_observer(&mock_router,
different_media_source.id());
EXPECT_EQ(observer_captured, &different_routes_observer);
router()->RegisterMediaRoutesObserver(&routes_observer);
router()->RegisterMediaRoutesObserver(&extra_routes_observer);
router()->RegisterMediaRoutesObserver(&different_routes_observer);
std::vector<MediaRoute> expected_routes;
expected_routes.push_back(MediaRoute(kRouteId, media_source, kSinkId,
kDescription, false, "", false));
expected_routes.push_back(MediaRoute(kRouteId2, media_source, kSinkId,
kDescription, false, "", false));
std::vector<MediaRoute::Id> expected_joinable_route_ids;
expected_joinable_route_ids.push_back(kJoinableRouteId);
expected_joinable_route_ids.push_back(kJoinableRouteId2);
mojo::Array<mojo::String> mojo_joinable_routes(2);
mojo_joinable_routes[0] = kJoinableRouteId;
mojo_joinable_routes[1] = kJoinableRouteId2;
mojo::Array<interfaces::MediaRoutePtr> mojo_routes(2);
mojo_routes[0] = interfaces::MediaRoute::New();
mojo_routes[0]->media_route_id = kRouteId;
mojo_routes[0]->media_source = kSource;
mojo_routes[0]->media_sink_id = kSinkId;
mojo_routes[0]->description = kDescription;
mojo_routes[0]->is_local = false;
mojo_routes[1] = interfaces::MediaRoute::New();
mojo_routes[1]->media_route_id = kRouteId2;
mojo_routes[1]->media_source = kSource;
mojo_routes[1]->media_sink_id = kSinkId;
mojo_routes[1]->description = kDescription;
mojo_routes[1]->is_local = false;
EXPECT_CALL(routes_observer,
OnRoutesUpdated(SequenceEquals(expected_routes),
expected_joinable_route_ids));
EXPECT_CALL(extra_routes_observer,
OnRoutesUpdated(SequenceEquals(expected_routes),
expected_joinable_route_ids));
EXPECT_CALL(different_routes_observer,
OnRoutesUpdated(SequenceEquals(expected_routes),
expected_joinable_route_ids)).Times(0);
media_router_proxy_->OnRoutesUpdated(std::move(mojo_routes),
media_source.id(),
std::move(mojo_joinable_routes));
ProcessEventLoop();
EXPECT_CALL(mock_router, UnregisterMediaRoutesObserver(&routes_observer));
EXPECT_CALL(mock_router,
UnregisterMediaRoutesObserver(&extra_routes_observer));
EXPECT_CALL(mock_router,
UnregisterMediaRoutesObserver(&different_routes_observer));
router()->UnregisterMediaRoutesObserver(&routes_observer);
router()->UnregisterMediaRoutesObserver(&extra_routes_observer);
router()->UnregisterMediaRoutesObserver(&different_routes_observer);
EXPECT_CALL(mock_media_route_provider_,
StopObservingMediaRoutes(
mojo::String(media_source.id()))).Times(1);
EXPECT_CALL(mock_media_route_provider_,
StopObservingMediaRoutes(
mojo::String(different_media_source.id())));
ProcessEventLoop();
}
TEST_F(MediaRouterMojoImplTest, SendRouteMessage) {
EXPECT_CALL(
mock_media_route_provider_,
SendRouteMessage(mojo::String(kRouteId), mojo::String(kMessage), _))
.WillOnce(Invoke([](
const MediaRoute::Id& route_id, const std::string& message,
const interfaces::MediaRouteProvider::SendRouteMessageCallback& cb) {
cb.Run(true);
}));
base::RunLoop run_loop;
SendMessageCallbackHandler handler;
EXPECT_CALL(handler, Invoke(true))
.WillOnce(InvokeWithoutArgs([&run_loop]() {
run_loop.Quit();
}));
router()->SendRouteMessage(kRouteId, kMessage,
base::Bind(&SendMessageCallbackHandler::Invoke,
base::Unretained(&handler)));
run_loop.Run();
}
TEST_F(MediaRouterMojoImplTest, SendRouteBinaryMessage) {
scoped_ptr<std::vector<uint8_t>> expected_binary_data(
new std::vector<uint8_t>(kBinaryMessage,
kBinaryMessage + arraysize(kBinaryMessage)));
EXPECT_CALL(mock_media_route_provider_,
SendRouteBinaryMessageInternal(mojo::String(kRouteId), _, _))
.WillOnce(Invoke([](
const MediaRoute::Id& route_id, const std::vector<uint8_t>& data,
const interfaces::MediaRouteProvider::SendRouteMessageCallback& cb) {
EXPECT_EQ(
0, memcmp(kBinaryMessage, &(data[0]), arraysize(kBinaryMessage)));
cb.Run(true);
}));
base::RunLoop run_loop;
SendMessageCallbackHandler handler;
EXPECT_CALL(handler, Invoke(true))
.WillOnce(InvokeWithoutArgs([&run_loop]() {
run_loop.Quit();
}));
router()->SendRouteBinaryMessage(
kRouteId, std::move(expected_binary_data),
base::Bind(&SendMessageCallbackHandler::Invoke,
base::Unretained(&handler)));
run_loop.Run();
}
TEST_F(MediaRouterMojoImplTest, PresentationSessionMessagesSingleObserver) {
mojo::Array<interfaces::RouteMessagePtr> mojo_messages(2);
mojo_messages[0] = interfaces::RouteMessage::New();
mojo_messages[0]->type = interfaces::RouteMessage::Type::TEXT;
mojo_messages[0]->message = "text";
mojo_messages[1] = interfaces::RouteMessage::New();
mojo_messages[1]->type = interfaces::RouteMessage::Type::BINARY;
mojo_messages[1]->data.push_back(1);
ScopedVector<content::PresentationSessionMessage> expected_messages;
scoped_ptr<content::PresentationSessionMessage> message;
message.reset(new content::PresentationSessionMessage(
content::PresentationMessageType::TEXT));
message->message = "text";
expected_messages.push_back(std::move(message));
message.reset(new content::PresentationSessionMessage(
content::PresentationMessageType::ARRAY_BUFFER));
message->data.reset(new std::vector<uint8_t>(1, 1));
expected_messages.push_back(std::move(message));
base::RunLoop run_loop;
MediaRoute::Id expected_route_id("foo");
interfaces::MediaRouteProvider::ListenForRouteMessagesCallback mojo_callback;
EXPECT_CALL(mock_media_route_provider_,
ListenForRouteMessages(Eq(expected_route_id), _))
.WillOnce(DoAll(SaveArg<1>(&mojo_callback),
InvokeWithoutArgs([&run_loop]() {
run_loop.Quit();
})));
// |pass_ownership| param is "true" here because there is only one observer.
ListenForMessagesCallbackHandler handler(std::move(expected_messages), true);
EXPECT_CALL(handler, InvokeObserver());
// Creating PresentationSessionMessagesObserver will register itself to the
// MediaRouter, which in turn will start listening for route messages.
scoped_ptr<PresentationSessionMessagesObserver> observer(
new PresentationSessionMessagesObserver(
base::Bind(&ListenForMessagesCallbackHandler::Invoke,
base::Unretained(&handler)),
expected_route_id, router()));
run_loop.Run();
base::RunLoop run_loop2;
// Simulate messages by invoking the saved mojo callback.
// We expect one more ListenForRouteMessages call since |observer| was
// still registered when the first set of messages arrived.
mojo_callback.Run(std::move(mojo_messages), false);
interfaces::MediaRouteProvider::ListenForRouteMessagesCallback
mojo_callback_2;
EXPECT_CALL(mock_media_route_provider_, ListenForRouteMessages(_, _))
.WillOnce(DoAll(SaveArg<1>(&mojo_callback_2),
InvokeWithoutArgs([&run_loop2]() {
run_loop2.Quit();
})));
run_loop2.Run();
base::RunLoop run_loop3;
// Stop listening for messages. In particular, MediaRouterMojoImpl will not
// call ListenForRouteMessages again when it sees there are no more observers.
mojo::Array<interfaces::RouteMessagePtr> mojo_messages_2(1);
mojo_messages_2[0] = interfaces::RouteMessage::New();
mojo_messages_2[0]->type = interfaces::RouteMessage::Type::TEXT;
mojo_messages_2[0]->message = "foo";
observer.reset();
mojo_callback_2.Run(std::move(mojo_messages_2), false);
EXPECT_CALL(mock_media_route_provider_, StopListeningForRouteMessages(_))
.WillOnce(InvokeWithoutArgs([&run_loop3]() {
run_loop3.Quit();
}));
run_loop3.Run();
}
TEST_F(MediaRouterMojoImplTest, PresentationSessionMessagesMultipleObservers) {
mojo::Array<interfaces::RouteMessagePtr> mojo_messages(2);
mojo_messages[0] = interfaces::RouteMessage::New();
mojo_messages[0]->type = interfaces::RouteMessage::Type::TEXT;
mojo_messages[0]->message = "text";
mojo_messages[1] = interfaces::RouteMessage::New();
mojo_messages[1]->type = interfaces::RouteMessage::Type::BINARY;
mojo_messages[1]->data.push_back(1);
ScopedVector<content::PresentationSessionMessage> expected_messages;
scoped_ptr<content::PresentationSessionMessage> message;
message.reset(new content::PresentationSessionMessage(
content::PresentationMessageType::TEXT));
message->message = "text";
expected_messages.push_back(std::move(message));
message.reset(new content::PresentationSessionMessage(
content::PresentationMessageType::ARRAY_BUFFER));
message->data.reset(new std::vector<uint8_t>(1, 1));
expected_messages.push_back(std::move(message));
base::RunLoop run_loop;
MediaRoute::Id expected_route_id("foo");
interfaces::MediaRouteProvider::ListenForRouteMessagesCallback mojo_callback;
EXPECT_CALL(mock_media_route_provider_,
ListenForRouteMessages(Eq(expected_route_id), _))
.WillOnce(DoAll(SaveArg<1>(&mojo_callback),
InvokeWithoutArgs([&run_loop]() {
run_loop.Quit();
})));
// |pass_ownership| param is "false" here because there are more than one
// observers.
ListenForMessagesCallbackHandler handler(std::move(expected_messages), false);
EXPECT_CALL(handler, InvokeObserver()).Times(2);
// Creating PresentationSessionMessagesObserver will register itself to the
// MediaRouter, which in turn will start listening for route messages.
scoped_ptr<PresentationSessionMessagesObserver> observer1(
new PresentationSessionMessagesObserver(
base::Bind(&ListenForMessagesCallbackHandler::Invoke,
base::Unretained(&handler)),
expected_route_id, router()));
scoped_ptr<PresentationSessionMessagesObserver> observer2(
new PresentationSessionMessagesObserver(
base::Bind(&ListenForMessagesCallbackHandler::Invoke,
base::Unretained(&handler)),
expected_route_id, router()));
run_loop.Run();
base::RunLoop run_loop2;
// Simulate messages by invoking the saved mojo callback.
// We expect one more ListenForRouteMessages call since |observer| was
// still registered when the first set of messages arrived.
mojo_callback.Run(std::move(mojo_messages), false);
interfaces::MediaRouteProvider::ListenForRouteMessagesCallback
mojo_callback_2;
EXPECT_CALL(mock_media_route_provider_, ListenForRouteMessages(_, _))
.WillOnce(DoAll(SaveArg<1>(&mojo_callback_2),
InvokeWithoutArgs([&run_loop2]() {
run_loop2.Quit();
})));
run_loop2.Run();
base::RunLoop run_loop3;
// Stop listening for messages. In particular, MediaRouterMojoImpl will not
// call ListenForRouteMessages again when it sees there are no more observers.
mojo::Array<interfaces::RouteMessagePtr> mojo_messages_2(1);
mojo_messages_2[0] = interfaces::RouteMessage::New();
mojo_messages_2[0]->type = interfaces::RouteMessage::Type::TEXT;
mojo_messages_2[0]->message = "foo";
observer1.reset();
observer2.reset();
mojo_callback_2.Run(std::move(mojo_messages_2), false);
EXPECT_CALL(mock_media_route_provider_, StopListeningForRouteMessages(_))
.WillOnce(InvokeWithoutArgs([&run_loop3]() {
run_loop3.Quit();
}));
run_loop3.Run();
}
TEST_F(MediaRouterMojoImplTest, PresentationSessionMessagesError) {
MediaRoute::Id expected_route_id("foo");
interfaces::MediaRouteProvider::ListenForRouteMessagesCallback mojo_callback;
base::RunLoop run_loop;
EXPECT_CALL(mock_media_route_provider_,
ListenForRouteMessages(Eq(expected_route_id), _))
.WillOnce(DoAll(SaveArg<1>(&mojo_callback),
InvokeWithoutArgs([&run_loop]() {
run_loop.Quit();
})));
ListenForMessagesCallbackHandler handler(
ScopedVector<content::PresentationSessionMessage>(), true);
// Creating PresentationSessionMessagesObserver will register itself to the
// MediaRouter, which in turn will start listening for route messages.
scoped_ptr<PresentationSessionMessagesObserver> observer1(
new PresentationSessionMessagesObserver(
base::Bind(&ListenForMessagesCallbackHandler::Invoke,
base::Unretained(&handler)),
expected_route_id, router()));
run_loop.Run();
mojo_callback.Run(mojo::Array<interfaces::RouteMessagePtr>(0), true);
ProcessEventLoop();
}
TEST_F(MediaRouterMojoImplTest, PresentationConnectionStateChangedCallback) {
using PresentationConnectionState =
interfaces::MediaRouter::PresentationConnectionState;
MediaRoute::Id route_id("route-id");
const std::string kPresentationUrl("http://foo.fakeUrl");
const std::string kPresentationId("pid");
content::PresentationSessionInfo connection(kPresentationUrl,
kPresentationId);
MockPresentationConnectionStateChangedCallback callback;
scoped_ptr<PresentationConnectionStateSubscription> subscription =
router()->AddPresentationConnectionStateChangedCallback(
route_id,
base::Bind(&MockPresentationConnectionStateChangedCallback::Run,
base::Unretained(&callback)));
base::RunLoop run_loop;
EXPECT_CALL(callback, Run(content::PRESENTATION_CONNECTION_STATE_CLOSED))
.WillOnce(InvokeWithoutArgs([&run_loop]() {
run_loop.Quit();
}));
media_router_proxy_->OnPresentationConnectionStateChanged(
route_id, PresentationConnectionState::CLOSED);
run_loop.Run();
EXPECT_TRUE(Mock::VerifyAndClearExpectations(&callback));
base::RunLoop run_loop2;
// Right now we don't keep track of previous state so the callback will be
// invoked with the same state again.
EXPECT_CALL(callback, Run(content::PRESENTATION_CONNECTION_STATE_CLOSED))
.WillOnce(InvokeWithoutArgs([&run_loop2]() {
run_loop2.Quit();
}));
media_router_proxy_->OnPresentationConnectionStateChanged(
route_id, PresentationConnectionState::CLOSED);
run_loop2.Run();
// Callback has been removed, so we don't expect it to be called anymore.
subscription.reset();
EXPECT_TRUE(router()->presentation_connection_state_callbacks_.empty());
EXPECT_CALL(callback, Run(content::PRESENTATION_CONNECTION_STATE_CLOSED))
.Times(0);
media_router_proxy_->OnPresentationConnectionStateChanged(
route_id, PresentationConnectionState::CLOSED);
ProcessEventLoop();
}
TEST_F(MediaRouterMojoImplTest, QueuedWhileAsleep) {
base::RunLoop run_loop;
EXPECT_CALL(mock_event_page_tracker_, IsEventPageSuspended(extension_id()))
.Times(2)
.WillRepeatedly(Return(true));
EXPECT_CALL(mock_event_page_tracker_, WakeEventPage(extension_id(), _))
.Times(2)
.WillOnce(Return(true))
.WillOnce(DoAll(InvokeWithoutArgs([&run_loop]() {
run_loop.Quit();
}), Return(true)));
router()->DetachRoute(kRouteId);
router()->DetachRoute(kRouteId2);
run_loop.Run();
EXPECT_CALL(mock_event_page_tracker_, IsEventPageSuspended(extension_id()))
.Times(1)
.WillRepeatedly(Return(false));
EXPECT_CALL(mock_media_route_provider_, DetachRoute(mojo::String(kRouteId)));
EXPECT_CALL(mock_media_route_provider_, DetachRoute(mojo::String(kRouteId2)));
ConnectProviderManagerService();
ProcessEventLoop();
}
class MediaRouterMojoExtensionTest : public ::testing::Test {
public:
MediaRouterMojoExtensionTest()
: process_manager_(nullptr),
message_loop_(mojo::common::MessagePumpMojo::Create())
{}
~MediaRouterMojoExtensionTest() override {}
protected:
void SetUp() override {
profile_.reset(new TestingProfile);
// Set up a mock ProcessManager instance.
extensions::ProcessManagerFactory::GetInstance()->SetTestingFactory(
profile_.get(), &TestProcessManager::Create);
process_manager_ = static_cast<TestProcessManager*>(
extensions::ProcessManager::Get(profile_.get()));
DCHECK(process_manager_);
// Create MR and its proxy, so that it can be accessed through Mojo.
media_router_.reset(new MediaRouterMojoImpl(process_manager_));
ProcessEventLoop();
}
void TearDown() override {
media_router_.reset();
profile_.reset();
// Explicitly delete the TestingBrowserProcess before |message_loop_|.
// This allows it to do cleanup before |message_loop_| goes away.
TestingBrowserProcess::DeleteInstance();
}
// Constructs bindings so that |media_router_| delegates calls to
// |mojo_media_router_|, which are then handled by
// |mock_media_route_provider_service_|.
void BindMediaRouteProvider() {
binding_.reset(new mojo::Binding<interfaces::MediaRouteProvider>(
&mock_media_route_provider_,
mojo::GetProxy(&media_route_provider_proxy_)));
media_router_->BindToMojoRequest(mojo::GetProxy(&media_router_proxy_),
kExtensionId);
}
void ResetMediaRouteProvider() {
binding_.reset();
media_router_->BindToMojoRequest(mojo::GetProxy(&media_router_proxy_),
kExtensionId);
}
void RegisterMediaRouteProvider() {
media_router_proxy_->RegisterMediaRouteProvider(
std::move(media_route_provider_proxy_),
base::Bind(&RegisterMediaRouteProviderHandler::Invoke,
base::Unretained(&provide_handler_)));
}
void ProcessEventLoop() {
message_loop_.RunUntilIdle();
}
void ExpectWakeReasonBucketCount(MediaRouteProviderWakeReason reason,
int expected_count) {
histogram_tester_.ExpectBucketCount("MediaRouter.Provider.WakeReason",
static_cast<int>(reason),
expected_count);
}
scoped_ptr<MediaRouterMojoImpl> media_router_;
RegisterMediaRouteProviderHandler provide_handler_;
TestProcessManager* process_manager_;
testing::StrictMock<MockMediaRouteProvider> mock_media_route_provider_;
interfaces::MediaRouterPtr media_router_proxy_;
private:
scoped_ptr<TestingProfile> profile_;
base::MessageLoop message_loop_;
interfaces::MediaRouteProviderPtr media_route_provider_proxy_;
scoped_ptr<mojo::Binding<interfaces::MediaRouteProvider>> binding_;
base::HistogramTester histogram_tester_;
DISALLOW_COPY_AND_ASSIGN(MediaRouterMojoExtensionTest);
};
TEST_F(MediaRouterMojoExtensionTest, DeferredBindingAndSuspension) {
// DetachRoute is called before *any* extension has connected.
// It should be queued.
media_router_->DetachRoute(kRouteId);
BindMediaRouteProvider();
base::RunLoop run_loop, run_loop2;
// |mojo_media_router| signals its readiness to the MR by registering
// itself via RegisterMediaRouteProvider().
// Now that the |media_router| and |mojo_media_router| are fully initialized,
// the queued DetachRoute() call should be executed.
EXPECT_CALL(provide_handler_, Invoke(testing::Not("")))
.WillOnce(InvokeWithoutArgs([&run_loop]() {
run_loop.Quit();
}));
EXPECT_CALL(*process_manager_, IsEventPageSuspended(kExtensionId))
.WillOnce(Return(false));
EXPECT_CALL(mock_media_route_provider_, DetachRoute(mojo::String(kRouteId)))
.WillOnce(InvokeWithoutArgs([&run_loop2]() {
run_loop2.Quit();
}));
RegisterMediaRouteProvider();
run_loop.Run();
run_loop2.Run();
base::RunLoop run_loop3;
// Extension is suspended and re-awoken.
ResetMediaRouteProvider();
EXPECT_CALL(*process_manager_, IsEventPageSuspended(kExtensionId))
.WillOnce(Return(true));
EXPECT_CALL(*process_manager_, WakeEventPage(kExtensionId, _))
.WillOnce(testing::DoAll(media::RunCallback<1>(true),
InvokeWithoutArgs([&run_loop3]() {
run_loop3.Quit();
}),
Return(true)));
media_router_->DetachRoute(kRouteId2);
run_loop3.Run();
base::RunLoop run_loop4, run_loop5;
// RegisterMediaRouteProvider() is called.
// The queued DetachRoute(kRouteId2) call should be executed.
EXPECT_CALL(provide_handler_, Invoke(testing::Not("")))
.WillOnce(InvokeWithoutArgs([&run_loop4]() {
run_loop4.Quit();
}));
EXPECT_CALL(*process_manager_, IsEventPageSuspended(kExtensionId))
.WillOnce(Return(false));
EXPECT_CALL(mock_media_route_provider_, DetachRoute(mojo::String(kRouteId2)))
.WillOnce(InvokeWithoutArgs([&run_loop5]() {
run_loop5.Quit();
}));
BindMediaRouteProvider();
RegisterMediaRouteProvider();
run_loop4.Run();
run_loop5.Run();
ExpectWakeReasonBucketCount(MediaRouteProviderWakeReason::DETACH_ROUTE, 1);
}
TEST_F(MediaRouterMojoExtensionTest, AttemptedWakeupTooManyTimes) {
BindMediaRouteProvider();
// DetachRoute is called while extension is suspended. It should be queued.
// Schedule a component extension wakeup.
EXPECT_CALL(*process_manager_, IsEventPageSuspended(kExtensionId))
.WillOnce(Return(true));
EXPECT_CALL(*process_manager_, WakeEventPage(kExtensionId, _))
.WillOnce(testing::DoAll(media::RunCallback<1>(true), Return(true)));
media_router_->DetachRoute(kRouteId);
EXPECT_EQ(1u, media_router_->pending_requests_.size());
ExpectWakeReasonBucketCount(MediaRouteProviderWakeReason::DETACH_ROUTE, 1);
// Media route provider fails to connect to media router before extension is
// suspended again, and |OnConnectionError| is invoked. Retry the wakeup.
EXPECT_CALL(*process_manager_, WakeEventPage(kExtensionId, _))
.Times(MediaRouterMojoImpl::kMaxWakeupAttemptCount - 1)
.WillRepeatedly(
testing::DoAll(media::RunCallback<1>(true), Return(true)));
for (int i = 0; i < MediaRouterMojoImpl::kMaxWakeupAttemptCount - 1; ++i)
media_router_->OnConnectionError();
// We have already tried |kMaxWakeupAttemptCount| times. If we get an error
// again, we will give up and the pending request queue will be drained.
media_router_->OnConnectionError();
EXPECT_TRUE(media_router_->pending_requests_.empty());
ExpectWakeReasonBucketCount(MediaRouteProviderWakeReason::CONNECTION_ERROR,
MediaRouterMojoImpl::kMaxWakeupAttemptCount - 1);
// Requests that comes in after queue is drained should be queued.
EXPECT_CALL(*process_manager_, IsEventPageSuspended(kExtensionId))
.WillOnce(Return(true));
EXPECT_CALL(*process_manager_, WakeEventPage(kExtensionId, _))
.WillOnce(testing::DoAll(media::RunCallback<1>(true), Return(true)));
media_router_->DetachRoute(kRouteId);
EXPECT_EQ(1u, media_router_->pending_requests_.size());
}
TEST_F(MediaRouterMojoExtensionTest, WakeupFailedDrainsQueue) {
BindMediaRouteProvider();
// DetachRoute is called while extension is suspended. It should be queued.
// Schedule a component extension wakeup.
EXPECT_CALL(*process_manager_, IsEventPageSuspended(kExtensionId))
.WillOnce(Return(true));
base::Callback<void(bool)> extension_wakeup_callback;
EXPECT_CALL(*process_manager_, WakeEventPage(kExtensionId, _))
.WillOnce(
testing::DoAll(SaveArg<1>(&extension_wakeup_callback), Return(true)));
media_router_->DetachRoute(kRouteId);
EXPECT_EQ(1u, media_router_->pending_requests_.size());
// Extension wakeup callback returning false is an non-retryable error.
// Queue should be drained.
extension_wakeup_callback.Run(false);
EXPECT_TRUE(media_router_->pending_requests_.empty());
// Requests that comes in after queue is drained should be queued.
EXPECT_CALL(*process_manager_, IsEventPageSuspended(kExtensionId))
.WillOnce(Return(true));
EXPECT_CALL(*process_manager_, WakeEventPage(kExtensionId, _))
.WillOnce(testing::DoAll(media::RunCallback<1>(true), Return(true)));
media_router_->DetachRoute(kRouteId);
EXPECT_EQ(1u, media_router_->pending_requests_.size());
ExpectWakeReasonBucketCount(MediaRouteProviderWakeReason::DETACH_ROUTE, 1);
}
TEST_F(MediaRouterMojoExtensionTest, DropOldestPendingRequest) {
const size_t kMaxPendingRequests = MediaRouterMojoImpl::kMaxPendingRequests;
// Request is queued.
media_router_->DetachRoute(kRouteId);
EXPECT_EQ(1u, media_router_->pending_requests_.size());
for (size_t i = 0; i < kMaxPendingRequests; ++i)
media_router_->DetachRoute(kRouteId2);
// The request queue size should not exceed |kMaxPendingRequests|.
EXPECT_EQ(kMaxPendingRequests, media_router_->pending_requests_.size());
base::RunLoop run_loop, run_loop2;
size_t count = 0;
// The oldest request should have been dropped, so we don't expect to see
// DetachRoute(kRouteId) here.
BindMediaRouteProvider();
EXPECT_CALL(provide_handler_, Invoke(testing::Not("")))
.WillOnce(InvokeWithoutArgs([&run_loop]() {
run_loop.Quit();
}));
EXPECT_CALL(*process_manager_, IsEventPageSuspended(kExtensionId));
EXPECT_CALL(mock_media_route_provider_, DetachRoute(mojo::String(kRouteId2)))
.Times(kMaxPendingRequests)
.WillRepeatedly(InvokeWithoutArgs([&run_loop2, &count]() {
if (++count == MediaRouterMojoImpl::kMaxPendingRequests)
run_loop2.Quit();
}));
RegisterMediaRouteProvider();
run_loop.Run();
run_loop2.Run();
}
} // namespace media_router
|