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

Side by Side Diff: chrome/browser/media/router/mojo/media_router_mojo_impl_unittest.cc

Issue 2728293003: MediaRouter: Cache MediaRoutesObserver queries to prevent ext wake-ups (Closed)
Patch Set: Created 3 years, 9 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
« no previous file with comments | « chrome/browser/media/router/mojo/media_router_mojo_impl.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <stddef.h> 5 #include <stddef.h>
6 #include <stdint.h> 6 #include <stdint.h>
7 7
8 #include <memory> 8 #include <memory>
9 #include <string> 9 #include <string>
10 #include <utility> 10 #include <utility>
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 using testing::Eq; 49 using testing::Eq;
50 using testing::Invoke; 50 using testing::Invoke;
51 using testing::InvokeWithoutArgs; 51 using testing::InvokeWithoutArgs;
52 using testing::IsEmpty; 52 using testing::IsEmpty;
53 using testing::Mock; 53 using testing::Mock;
54 using testing::Not; 54 using testing::Not;
55 using testing::Pointee; 55 using testing::Pointee;
56 using testing::Return; 56 using testing::Return;
57 using testing::ReturnRef; 57 using testing::ReturnRef;
58 using testing::SaveArg; 58 using testing::SaveArg;
59 using testing::Sequence;
59 60
60 namespace media_router { 61 namespace media_router {
61 62
62 namespace { 63 namespace {
63 64
64 const char kDescription[] = "description"; 65 const char kDescription[] = "description";
65 const char kError[] = "error"; 66 const char kError[] = "error";
66 const char kMessage[] = "message"; 67 const char kMessage[] = "message";
67 const char kSource[] = "source1"; 68 const char kSource[] = "source1";
68 const char kSource2[] = "source2"; 69 const char kSource2[] = "source2";
(...skipping 730 matching lines...) Expand 10 before | Expand all | Expand 10 after
799 sinks_observer2.reset(); 800 sinks_observer2.reset();
800 ProcessEventLoop(); 801 ProcessEventLoop();
801 } 802 }
802 803
803 TEST_F(MediaRouterMojoImplTest, RegisterAndUnregisterMediaRoutesObserver) { 804 TEST_F(MediaRouterMojoImplTest, RegisterAndUnregisterMediaRoutesObserver) {
804 MockMediaRouter mock_router; 805 MockMediaRouter mock_router;
805 MediaSource media_source(kSource); 806 MediaSource media_source(kSource);
806 MediaSource different_media_source(kSource2); 807 MediaSource different_media_source(kSource2);
807 EXPECT_CALL(mock_media_route_provider_, 808 EXPECT_CALL(mock_media_route_provider_,
808 StartObservingMediaRoutes(media_source.id())) 809 StartObservingMediaRoutes(media_source.id()))
809 .Times(2); 810 .Times(1);
810 EXPECT_CALL( 811 EXPECT_CALL(
811 mock_media_route_provider_, 812 mock_media_route_provider_,
812 StartObservingMediaRoutes(different_media_source.id())) 813 StartObservingMediaRoutes(different_media_source.id()))
813 .Times(1); 814 .Times(1);
814 815
815 MediaRoutesObserver* observer_captured; 816 MediaRoutesObserver* observer_captured;
816 EXPECT_CALL(mock_router, RegisterMediaRoutesObserver(_)) 817 EXPECT_CALL(mock_router, RegisterMediaRoutesObserver(_))
817 .Times(3) 818 .Times(3)
818 .WillRepeatedly(SaveArg<0>(&observer_captured)); 819 .WillRepeatedly(SaveArg<0>(&observer_captured));
819 MockMediaRoutesObserver routes_observer(&mock_router, media_source.id()); 820 MockMediaRoutesObserver routes_observer(&mock_router, media_source.id());
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
862 router()->UnregisterMediaRoutesObserver(&different_routes_observer); 863 router()->UnregisterMediaRoutesObserver(&different_routes_observer);
863 EXPECT_CALL(mock_media_route_provider_, 864 EXPECT_CALL(mock_media_route_provider_,
864 StopObservingMediaRoutes(media_source.id())) 865 StopObservingMediaRoutes(media_source.id()))
865 .Times(1); 866 .Times(1);
866 EXPECT_CALL( 867 EXPECT_CALL(
867 mock_media_route_provider_, 868 mock_media_route_provider_,
868 StopObservingMediaRoutes(different_media_source.id())); 869 StopObservingMediaRoutes(different_media_source.id()));
869 ProcessEventLoop(); 870 ProcessEventLoop();
870 } 871 }
871 872
873 // Tests that multiple MediaRoutesObservers having the same query do not cause
874 // extra extension wake-ups because the OnRoutesUpdated() results are cached.
875 TEST_F(MediaRouterMojoImplTest, RegisterMediaRoutesObserver_DedupingWithCache) {
876 const MediaSource media_source = MediaSource(kSource);
877 std::vector<MediaRoute> expected_routes;
878 expected_routes.push_back(MediaRoute(kRouteId, media_source, kSinkId,
879 kDescription, false, "", false));
880 std::vector<MediaRoute::Id> expected_joinable_route_ids;
881 expected_joinable_route_ids.push_back(kJoinableRouteId);
882
883 Sequence sequence;
884
885 // Creating the first observer will wake-up the provider and ask it to start
886 // observing routes having source |kSource|. The provider will respond with
887 // the existing route.
888 EXPECT_CALL(mock_media_route_provider_,
889 StartObservingMediaRoutes(media_source.id()))
890 .Times(1);
891 std::unique_ptr<MockMediaRoutesObserver> observer1(
892 new MockMediaRoutesObserver(router(), media_source.id()));
893 ProcessEventLoop();
894 EXPECT_CALL(*observer1, OnRoutesUpdated(SequenceEquals(expected_routes),
895 expected_joinable_route_ids))
896 .Times(1);
897 media_router_proxy_->OnRoutesUpdated(expected_routes, media_source.id(),
898 expected_joinable_route_ids);
899 ProcessEventLoop();
900
901 // Creating two more observers will not wake up the provider. Instead, the
902 // cached route list will be returned.
903 std::unique_ptr<MockMediaRoutesObserver> observer2(
904 new MockMediaRoutesObserver(router(), media_source.id()));
905 std::unique_ptr<MockMediaRoutesObserver> observer3(
906 new MockMediaRoutesObserver(router(), media_source.id()));
907 EXPECT_CALL(*observer2, OnRoutesUpdated(SequenceEquals(expected_routes),
908 expected_joinable_route_ids))
909 .Times(1);
910 EXPECT_CALL(*observer3, OnRoutesUpdated(SequenceEquals(expected_routes),
911 expected_joinable_route_ids))
912 .Times(1);
913 ProcessEventLoop();
914
915 // Kill 2 of three observers, and expect nothing happens at the provider.
916 observer1.reset();
917 observer2.reset();
918 ProcessEventLoop();
919
920 // Kill the final observer, and expect the provider to be woken-up and called
921 // with the "stop observing" notification.
922 EXPECT_CALL(mock_media_route_provider_,
923 StopObservingMediaRoutes(media_source.id()))
924 .Times(1);
925 observer3.reset();
926 ProcessEventLoop();
927 }
928
872 TEST_F(MediaRouterMojoImplTest, SendRouteMessage) { 929 TEST_F(MediaRouterMojoImplTest, SendRouteMessage) {
873 EXPECT_CALL( 930 EXPECT_CALL(
874 mock_media_route_provider_, SendRouteMessage(kRouteId, kMessage, _)) 931 mock_media_route_provider_, SendRouteMessage(kRouteId, kMessage, _))
875 .WillOnce(Invoke([]( 932 .WillOnce(Invoke([](
876 const MediaRoute::Id& route_id, const std::string& message, 933 const MediaRoute::Id& route_id, const std::string& message,
877 const mojom::MediaRouteProvider::SendRouteMessageCallback& cb) { 934 const mojom::MediaRouteProvider::SendRouteMessageCallback& cb) {
878 cb.Run(true); 935 cb.Run(true);
879 })); 936 }));
880 937
881 base::RunLoop run_loop; 938 base::RunLoop run_loop;
(...skipping 632 matching lines...) Expand 10 before | Expand all | Expand 10 after
1514 EXPECT_CALL(mock_media_route_provider_, 1571 EXPECT_CALL(mock_media_route_provider_,
1515 UpdateMediaSinks(MediaSourceForDesktop().id())) 1572 UpdateMediaSinks(MediaSourceForDesktop().id()))
1516 .WillOnce(InvokeWithoutArgs([&run_loop2]() { 1573 .WillOnce(InvokeWithoutArgs([&run_loop2]() {
1517 run_loop2.Quit(); 1574 run_loop2.Quit();
1518 })); 1575 }));
1519 1576
1520 run_loop2.Run(); 1577 run_loop2.Run();
1521 } 1578 }
1522 1579
1523 } // namespace media_router 1580 } // namespace media_router
OLDNEW
« no previous file with comments | « chrome/browser/media/router/mojo/media_router_mojo_impl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698