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

Side by Side Diff: chrome/browser/media/router/presentation_session_state_observer_unittest.cc

Issue 1430413003: [Media Router] Connection state change listening redesign part 2. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix compile Created 5 years, 1 month 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "base/bind.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "base/strings/stringprintf.h"
8 #include "chrome/browser/media/router/media_route.h"
9 #include "chrome/browser/media/router/media_source_helper.h"
10 #include "chrome/browser/media/router/mock_media_router.h"
11 #include "chrome/browser/media/router/presentation_session_state_observer.h"
12 #include "content/public/browser/presentation_session.h"
13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 using testing::_;
17
18 namespace media_router {
19
20 namespace {
21
22 const char kPresentationUrl[] = "http://foo";
23 const char kPresentationId[] = "presentationId";
24
25 MediaRoute::Id CreateRouteId(const char* presentation_url,
26 const char* presentation_id) {
27 return base::StringPrintf("urn:x-org.chromium:media:route:%s/cast-sink1/%s",
28 presentation_id, presentation_url);
29 }
30
31 MATCHER_P(PresentationSessionInfoEquals, expected, "") {
32 return arg.presentation_url == expected.presentation_url &&
33 arg.presentation_id == expected.presentation_id;
34 }
35
36 } // namespace
37
38 class PresentationSessionStateObserverTest : public testing::Test {
39 public:
40 PresentationSessionStateObserverTest() {}
41 ~PresentationSessionStateObserverTest() override {}
42
43 protected:
44 void SetUp() override {
45 EXPECT_CALL(router_, RegisterMediaRoutesObserver(_));
46 observer_.reset(new PresentationSessionStateObserver(
47 base::Bind(&PresentationSessionStateObserverTest::OnSessionStateChanged,
48 base::Unretained(this)),
49 &route_id_to_presentation_, &router_));
50 MediaRoute::Id route_id(CreateRouteId(kPresentationUrl, kPresentationId));
51 content::PresentationSessionInfo session_info(kPresentationUrl,
52 kPresentationId);
53 route_id_to_presentation_.Add(route_id, session_info);
54 }
55
56 void TearDown() override {
57 if (observer_) {
58 EXPECT_CALL(router_, UnregisterMediaRoutesObserver(_));
59 observer_.reset();
60 }
61 }
62
63 MOCK_METHOD2(OnSessionStateChanged,
64 void(const content::PresentationSessionInfo& session_info,
65 content::PresentationConnectionState new_state));
66
67 MockMediaRouter router_;
68 MediaRouteIdToPresentationSessionMapping route_id_to_presentation_;
69 scoped_ptr<PresentationSessionStateObserver> observer_;
70 };
71
72 TEST_F(PresentationSessionStateObserverTest, InvokeCallbackWithConnected) {
73 EXPECT_CALL(
74 *this, OnSessionStateChanged(
75 PresentationSessionInfoEquals(content::PresentationSessionInfo(
76 kPresentationUrl, kPresentationId)),
77 content::PRESENTATION_CONNECTION_STATE_CONNECTED));
78 MediaRoute::Id route_id(CreateRouteId(kPresentationUrl, kPresentationId));
79 observer_->OnPresentationSessionConnected(route_id);
80 }
81
82 TEST_F(PresentationSessionStateObserverTest, InvokeCallbackWithDisconnected) {
83 content::PresentationSessionInfo session_info(kPresentationUrl,
84 kPresentationId);
85 EXPECT_CALL(*this, OnSessionStateChanged(
86 PresentationSessionInfoEquals(session_info),
87 content::PRESENTATION_CONNECTION_STATE_CONNECTED));
88 MediaRoute::Id route_id(CreateRouteId(kPresentationUrl, kPresentationId));
89 observer_->OnPresentationSessionConnected(route_id);
90
91 // Route list update is expected to follow creation of route.
92 std::vector<MediaRoute> routes;
93 routes.push_back(MediaRoute(route_id,
94 MediaSourceForPresentationUrl(kPresentationUrl),
95 "sinkId", "Description", true, "", false));
96 observer_->OnRoutesUpdated(routes);
97
98 // New route list does not contain |route_id|, which means it is disconnected.
99 EXPECT_CALL(*this, OnSessionStateChanged(
100 PresentationSessionInfoEquals(session_info),
101 content::PRESENTATION_CONNECTION_STATE_CLOSED));
102 observer_->OnRoutesUpdated(std::vector<MediaRoute>());
103
104 // Note that it is normally not possible for |route_id| to reappear. But in
105 // case it does, test that it does NOT invoke the callback with CONNECTED.
106 EXPECT_TRUE(testing::Mock::VerifyAndClearExpectations(this));
107 observer_->OnRoutesUpdated(routes);
108 }
109
110 TEST_F(PresentationSessionStateObserverTest, Reset) {
111 content::PresentationSessionInfo session_info(kPresentationUrl,
112 kPresentationId);
113 EXPECT_CALL(*this, OnSessionStateChanged(
114 PresentationSessionInfoEquals(session_info),
115 content::PRESENTATION_CONNECTION_STATE_CONNECTED));
116 MediaRoute::Id route_id(CreateRouteId(kPresentationUrl, kPresentationId));
117 observer_->OnPresentationSessionConnected(route_id);
118
119 // Route list update is expected to follow creation of route.
120 std::vector<MediaRoute> routes;
121 routes.push_back(MediaRoute(route_id,
122 MediaSourceForPresentationUrl(kPresentationUrl),
123 "sinkId", "Description", true, "", false));
124 observer_->OnRoutesUpdated(routes);
125
126 // |route_id| is no longer being tracked.
127 observer_->Reset();
128 EXPECT_TRUE(testing::Mock::VerifyAndClearExpectations(this));
129 observer_->OnRoutesUpdated(std::vector<MediaRoute>());
130 }
131
132 } // namespace media_router
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698