OLD | NEW |
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 <vector> | 5 #include <vector> |
6 | 6 |
7 #include "base/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
8 #include "chrome/browser/media/router/media_source.h" | 8 #include "chrome/browser/media/router/media_source.h" |
9 #include "chrome/browser/media/router/media_source_helper.h" | 9 #include "chrome/browser/media/router/media_source_helper.h" |
10 #include "chrome/browser/media/router/mock_media_router.h" | 10 #include "chrome/browser/media/router/mock_media_router.h" |
11 #include "chrome/browser/media/router/mock_screen_availability_listener.h" | 11 #include "chrome/browser/media/router/mock_screen_availability_listener.h" |
12 #include "chrome/browser/media/router/presentation_service_delegate_impl.h" | 12 #include "chrome/browser/media/router/presentation_service_delegate_impl.h" |
13 #include "chrome/browser/media/router/test_helper.h" | 13 #include "chrome/browser/media/router/test_helper.h" |
14 #include "chrome/grit/generated_resources.h" | 14 #include "chrome/grit/generated_resources.h" |
15 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 15 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
16 #include "content/public/browser/presentation_screen_availability_listener.h" | 16 #include "content/public/browser/presentation_screen_availability_listener.h" |
17 #include "content/public/browser/presentation_session.h" | 17 #include "content/public/browser/presentation_session.h" |
18 #include "content/public/browser/render_process_host.h" | 18 #include "content/public/browser/render_process_host.h" |
19 #include "content/public/browser/web_contents.h" | 19 #include "content/public/browser/web_contents.h" |
20 #include "content/public/test/web_contents_tester.h" | 20 #include "content/public/test/web_contents_tester.h" |
21 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
22 #include "ui/base/l10n/l10n_util.h" | 22 #include "ui/base/l10n/l10n_util.h" |
23 | 23 |
24 using ::testing::_; | 24 using ::testing::_; |
25 using ::testing::Mock; | 25 using ::testing::Mock; |
26 using ::testing::Return; | 26 using ::testing::Return; |
| 27 using ::testing::SaveArg; |
27 using ::testing::StrictMock; | 28 using ::testing::StrictMock; |
28 | 29 |
29 namespace media_router { | 30 namespace media_router { |
30 | 31 |
31 class MockDelegateObserver | 32 class MockDelegateObserver |
32 : public content::PresentationServiceDelegate::Observer { | 33 : public content::PresentationServiceDelegate::Observer { |
33 public: | 34 public: |
34 MOCK_METHOD0(OnDelegateDestroyed, void()); | 35 MOCK_METHOD0(OnDelegateDestroyed, void()); |
35 MOCK_METHOD1(OnDefaultPresentationStarted, | 36 MOCK_METHOD1(OnDefaultPresentationStarted, |
36 void(const content::PresentationSessionInfo&)); | 37 void(const content::PresentationSessionInfo&)); |
37 }; | 38 }; |
38 | 39 |
39 class MockDefaultMediaSourceObserver | 40 class MockDefaultPresentationRequestObserver |
40 : public PresentationServiceDelegateImpl::DefaultMediaSourceObserver { | 41 : public PresentationServiceDelegateImpl:: |
| 42 DefaultPresentationRequestObserver { |
41 public: | 43 public: |
42 MOCK_METHOD2(OnDefaultMediaSourceChanged, | 44 MOCK_METHOD1(OnDefaultPresentationChanged, void(const PresentationRequest*)); |
43 void(const MediaSource&, const GURL&)); | |
44 }; | 45 }; |
45 | 46 |
46 class PresentationServiceDelegateImplTest | 47 class PresentationServiceDelegateImplTest |
47 : public ChromeRenderViewHostTestHarness { | 48 : public ChromeRenderViewHostTestHarness { |
48 public: | 49 public: |
49 void SetUp() override { | 50 void SetUp() override { |
50 ChromeRenderViewHostTestHarness::SetUp(); | 51 ChromeRenderViewHostTestHarness::SetUp(); |
51 content::WebContents* wc = web_contents(); | 52 content::WebContents* wc = web_contents(); |
52 ASSERT_TRUE(wc); | 53 ASSERT_TRUE(wc); |
53 PresentationServiceDelegateImpl::CreateForWebContents(wc); | 54 PresentationServiceDelegateImpl::CreateForWebContents(wc); |
54 delegate_impl_ = PresentationServiceDelegateImpl::FromWebContents(wc); | 55 delegate_impl_ = PresentationServiceDelegateImpl::FromWebContents(wc); |
55 delegate_impl_->SetMediaRouterForTest(&router_); | 56 delegate_impl_->SetMediaRouterForTest(&router_); |
56 } | 57 } |
57 | 58 |
| 59 MOCK_METHOD1(OnDefaultPresentationStarted, |
| 60 void(const content::PresentationSessionInfo& session_info)); |
| 61 |
58 PresentationServiceDelegateImpl* delegate_impl_; | 62 PresentationServiceDelegateImpl* delegate_impl_; |
59 MockMediaRouter router_; | 63 MockMediaRouter router_; |
60 }; | 64 }; |
61 | 65 |
62 TEST_F(PresentationServiceDelegateImplTest, AddScreenAvailabilityListener) { | 66 TEST_F(PresentationServiceDelegateImplTest, AddScreenAvailabilityListener) { |
63 ON_CALL(router_, RegisterMediaSinksObserver(_)).WillByDefault(Return(true)); | 67 ON_CALL(router_, RegisterMediaSinksObserver(_)).WillByDefault(Return(true)); |
64 | 68 |
65 std::string presentation_url1("http://url1"); | 69 std::string presentation_url1("http://url1"); |
66 std::string presentation_url2("http://url2"); | 70 std::string presentation_url2("http://url2"); |
67 MediaSource source1 = MediaSourceForPresentationUrl(presentation_url1); | 71 MediaSource source1 = MediaSourceForPresentationUrl(presentation_url1); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 EXPECT_TRUE(delegate_impl_->HasScreenAvailabilityListenerForTest( | 116 EXPECT_TRUE(delegate_impl_->HasScreenAvailabilityListenerForTest( |
113 render_process_id, render_frame_id, source1.id())); | 117 render_process_id, render_frame_id, source1.id())); |
114 | 118 |
115 EXPECT_CALL(router_, UnregisterMediaSinksObserver(_)).Times(1); | 119 EXPECT_CALL(router_, UnregisterMediaSinksObserver(_)).Times(1); |
116 delegate_impl_->RemoveScreenAvailabilityListener(render_process_id, | 120 delegate_impl_->RemoveScreenAvailabilityListener(render_process_id, |
117 render_frame_id, &listener1); | 121 render_frame_id, &listener1); |
118 EXPECT_FALSE(delegate_impl_->HasScreenAvailabilityListenerForTest( | 122 EXPECT_FALSE(delegate_impl_->HasScreenAvailabilityListenerForTest( |
119 render_process_id, render_frame_id, source1.id())); | 123 render_process_id, render_frame_id, source1.id())); |
120 } | 124 } |
121 | 125 |
| 126 // TODO(imcheng): Add a test to set default presentation URL in a different |
| 127 // RenderFrameHost and verify that it works. |
122 TEST_F(PresentationServiceDelegateImplTest, SetDefaultPresentationUrl) { | 128 TEST_F(PresentationServiceDelegateImplTest, SetDefaultPresentationUrl) { |
| 129 EXPECT_FALSE(delegate_impl_->GetDefaultPresentationRequest()); |
| 130 |
| 131 GURL frame_url("http://www.google.com"); |
| 132 content::WebContentsTester::For(web_contents())->NavigateAndCommit(frame_url); |
| 133 content::RenderFrameHost* main_frame = web_contents()->GetMainFrame(); |
| 134 ASSERT_TRUE(main_frame); |
| 135 int render_process_id = main_frame->GetProcess()->GetID(); |
| 136 int routing_id = main_frame->GetRoutingID(); |
| 137 |
| 138 auto callback = base::Bind( |
| 139 &PresentationServiceDelegateImplTest::OnDefaultPresentationStarted, |
| 140 base::Unretained(this)); |
| 141 std::string presentation_url1("http://foo"); |
| 142 delegate_impl_->SetDefaultPresentationUrl(render_process_id, routing_id, |
| 143 presentation_url1, callback); |
| 144 const PresentationRequest* request = |
| 145 delegate_impl_->GetDefaultPresentationRequest(); |
| 146 ASSERT_TRUE(request); |
| 147 EXPECT_EQ(presentation_url1, request->presentation_url()); |
| 148 EXPECT_EQ(RenderFrameHostId(render_process_id, routing_id), |
| 149 request->render_frame_host_id()); |
| 150 EXPECT_EQ(frame_url, request->frame_url()); |
| 151 |
| 152 // Set to a new default presentation URL |
| 153 std::string presentation_url2("https://youtube.com"); |
| 154 delegate_impl_->SetDefaultPresentationUrl(render_process_id, routing_id, |
| 155 presentation_url2, callback); |
| 156 request = delegate_impl_->GetDefaultPresentationRequest(); |
| 157 ASSERT_TRUE(request); |
| 158 EXPECT_EQ(presentation_url2, request->presentation_url()); |
| 159 EXPECT_EQ(RenderFrameHostId(render_process_id, routing_id), |
| 160 request->render_frame_host_id()); |
| 161 EXPECT_EQ(frame_url, request->frame_url()); |
| 162 |
| 163 // Remove default presentation URL. |
| 164 delegate_impl_->SetDefaultPresentationUrl(render_process_id, routing_id, "", |
| 165 callback); |
| 166 request = delegate_impl_->GetDefaultPresentationRequest(); |
| 167 EXPECT_FALSE(request); |
| 168 } |
| 169 |
| 170 TEST_F(PresentationServiceDelegateImplTest, DefaultPresentationUrlCallback) { |
| 171 content::RenderFrameHost* main_frame = web_contents()->GetMainFrame(); |
| 172 ASSERT_TRUE(main_frame); |
| 173 int render_process_id = main_frame->GetProcess()->GetID(); |
| 174 int routing_id = main_frame->GetRoutingID(); |
| 175 |
| 176 auto callback = base::Bind( |
| 177 &PresentationServiceDelegateImplTest::OnDefaultPresentationStarted, |
| 178 base::Unretained(this)); |
| 179 std::string presentation_url1("http://foo"); |
| 180 delegate_impl_->SetDefaultPresentationUrl(render_process_id, routing_id, |
| 181 presentation_url1, callback); |
| 182 |
| 183 const PresentationRequest* request = |
| 184 delegate_impl_->GetDefaultPresentationRequest(); |
| 185 ASSERT_TRUE(request); |
| 186 |
| 187 // Should not trigger callback since route response is error. |
| 188 delegate_impl_->OnRouteResponse(*request, nullptr, "", "Error"); |
| 189 EXPECT_TRUE(Mock::VerifyAndClearExpectations(this)); |
| 190 |
| 191 // Should not trigger callback since request doesn't match. |
| 192 std::string presentation_url2("http://bar"); |
| 193 PresentationRequest different_request(RenderFrameHostId(100, 200), |
| 194 presentation_url2, |
| 195 GURL("http://anotherFrameUrl")); |
| 196 MediaRoute different_route("differentRouteId", |
| 197 MediaSourceForPresentationUrl(presentation_url2), |
| 198 "mediaSinkId", "", true, "", true); |
| 199 delegate_impl_->OnRouteResponse(different_request, &different_route, |
| 200 "differentPresentationId", ""); |
| 201 EXPECT_TRUE(Mock::VerifyAndClearExpectations(this)); |
| 202 |
| 203 // Should trigger callback since request matches. |
| 204 MediaRoute route("routeId", MediaSourceForPresentationUrl(presentation_url1), |
| 205 "mediaSinkId", "", true, "", true); |
| 206 EXPECT_CALL(*this, OnDefaultPresentationStarted(_)).Times(1); |
| 207 delegate_impl_->OnRouteResponse(*request, &route, "presentationId", ""); |
| 208 } |
| 209 |
| 210 TEST_F(PresentationServiceDelegateImplTest, |
| 211 DefaultPresentationRequestObserver) { |
| 212 auto callback = base::Bind( |
| 213 &PresentationServiceDelegateImplTest::OnDefaultPresentationStarted, |
| 214 base::Unretained(this)); |
| 215 |
| 216 StrictMock<MockDefaultPresentationRequestObserver> observer; |
| 217 delegate_impl_->AddDefaultPresentationRequestObserver(&observer); |
| 218 |
123 content::WebContentsTester::For(web_contents()) | 219 content::WebContentsTester::For(web_contents()) |
124 ->NavigateAndCommit(GURL("http://www.google.com")); | 220 ->NavigateAndCommit(GURL("http://www.google.com")); |
125 content::RenderFrameHost* main_frame = web_contents()->GetMainFrame(); | 221 content::RenderFrameHost* main_frame = web_contents()->GetMainFrame(); |
126 ASSERT_TRUE(main_frame); | 222 ASSERT_TRUE(main_frame); |
127 int render_process_id = main_frame->GetProcess()->GetID(); | 223 int render_process_id = main_frame->GetProcess()->GetID(); |
128 int routing_id = main_frame->GetRoutingID(); | 224 int routing_id = main_frame->GetRoutingID(); |
129 | 225 |
130 std::string presentation_url1("http://foo"); | 226 std::string url1("http://foo"); |
131 delegate_impl_->SetDefaultPresentationUrl(render_process_id, routing_id, | 227 const PresentationRequest* observed_request = nullptr; |
132 presentation_url1); | 228 EXPECT_CALL(observer, OnDefaultPresentationChanged(_)) |
133 EXPECT_TRUE(delegate_impl_->default_source().Equals( | 229 .WillOnce(SaveArg<0>(&observed_request)); |
134 MediaSourceForPresentationUrl(presentation_url1))); | 230 delegate_impl_->SetDefaultPresentationUrl(render_process_id, routing_id, url1, |
| 231 callback); |
| 232 |
| 233 const PresentationRequest* request = |
| 234 delegate_impl_->GetDefaultPresentationRequest(); |
| 235 ASSERT_TRUE(request); |
| 236 EXPECT_EQ(request, observed_request); |
| 237 |
| 238 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&observer)); |
| 239 observed_request = nullptr; |
| 240 request = nullptr; |
| 241 |
| 242 std::string url2("http://youtube.com"); |
| 243 EXPECT_CALL(observer, OnDefaultPresentationChanged(_)) |
| 244 .WillOnce(SaveArg<0>(&observed_request)); |
| 245 delegate_impl_->SetDefaultPresentationUrl(render_process_id, routing_id, url2, |
| 246 callback); |
| 247 request = delegate_impl_->GetDefaultPresentationRequest(); |
| 248 ASSERT_TRUE(request); |
| 249 EXPECT_EQ(request, observed_request); |
| 250 |
| 251 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&observer)); |
| 252 observed_request = nullptr; |
| 253 request = nullptr; |
135 | 254 |
136 // Remove default presentation URL. | 255 // Remove default presentation URL. |
137 delegate_impl_->SetDefaultPresentationUrl(render_process_id, routing_id, ""); | 256 EXPECT_CALL(observer, OnDefaultPresentationChanged(nullptr)).Times(1); |
138 EXPECT_TRUE(delegate_impl_->default_source().Empty()); | 257 delegate_impl_->SetDefaultPresentationUrl(render_process_id, routing_id, "", |
139 | 258 callback); |
140 // Set to a new default presentation URL | |
141 std::string presentation_url2("https://youtube.com"); | |
142 delegate_impl_->SetDefaultPresentationUrl(render_process_id, routing_id, | |
143 presentation_url2); | |
144 EXPECT_TRUE(delegate_impl_->default_source().Equals( | |
145 MediaSourceForPresentationUrl(presentation_url2))); | |
146 } | |
147 | |
148 TEST_F(PresentationServiceDelegateImplTest, DefaultMediaSourceObserver) { | |
149 content::WebContentsTester::For(web_contents()) | |
150 ->NavigateAndCommit(GURL("http://www.google.com")); | |
151 content::RenderFrameHost* main_frame = web_contents()->GetMainFrame(); | |
152 ASSERT_TRUE(main_frame); | |
153 int render_process_id = main_frame->GetProcess()->GetID(); | |
154 int routing_id = main_frame->GetRoutingID(); | |
155 StrictMock<MockDefaultMediaSourceObserver> observer1; | |
156 StrictMock<MockDefaultMediaSourceObserver> observer2; | |
157 delegate_impl_->AddDefaultMediaSourceObserver(&observer1); | |
158 delegate_impl_->AddDefaultMediaSourceObserver(&observer2); | |
159 std::string url1("http://foo"); | |
160 EXPECT_CALL(observer1, OnDefaultMediaSourceChanged( | |
161 Equals(MediaSourceForPresentationUrl(url1)), | |
162 GURL("http://www.google.com"))).Times(1); | |
163 EXPECT_CALL(observer2, OnDefaultMediaSourceChanged( | |
164 Equals(MediaSourceForPresentationUrl(url1)), | |
165 GURL("http://www.google.com"))).Times(1); | |
166 delegate_impl_->SetDefaultPresentationUrl(render_process_id, | |
167 routing_id, url1); | |
168 | |
169 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&observer1)); | |
170 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&observer2)); | |
171 | |
172 delegate_impl_->RemoveDefaultMediaSourceObserver(&observer2); | |
173 std::string url2("http://youtube.com"); | |
174 EXPECT_CALL(observer1, OnDefaultMediaSourceChanged( | |
175 Equals(MediaSourceForPresentationUrl(url2)), | |
176 GURL("http://www.google.com"))).Times(1); | |
177 delegate_impl_->SetDefaultPresentationUrl(render_process_id, | |
178 routing_id, url2); | |
179 | |
180 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&observer1)); | |
181 // Remove default presentation URL. | |
182 EXPECT_CALL(observer1, OnDefaultMediaSourceChanged( | |
183 Equals(MediaSource()), | |
184 GURL("http://www.google.com"))).Times(1); | |
185 delegate_impl_->SetDefaultPresentationUrl(render_process_id, routing_id, ""); | |
186 } | 259 } |
187 | 260 |
188 TEST_F(PresentationServiceDelegateImplTest, Reset) { | 261 TEST_F(PresentationServiceDelegateImplTest, Reset) { |
189 ON_CALL(router_, RegisterMediaSinksObserver(_)).WillByDefault(Return(true)); | 262 ON_CALL(router_, RegisterMediaSinksObserver(_)).WillByDefault(Return(true)); |
190 | 263 |
191 std::string presentation_url1("http://url1"); | 264 std::string presentation_url1("http://url1"); |
192 MediaSource source = MediaSourceForPresentationUrl(presentation_url1); | 265 MediaSource source = MediaSourceForPresentationUrl(presentation_url1); |
193 MockScreenAvailabilityListener listener1(presentation_url1); | 266 MockScreenAvailabilityListener listener1(presentation_url1); |
194 int render_process_id = 1; | 267 int render_process_id = 1; |
195 int render_frame_id = 0; | 268 int render_frame_id = 0; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 MockScreenAvailabilityListener listener(presentation_url); | 302 MockScreenAvailabilityListener listener(presentation_url); |
230 const int render_process_id = 10; | 303 const int render_process_id = 10; |
231 const int render_frame_id = 1; | 304 const int render_frame_id = 1; |
232 | 305 |
233 EXPECT_CALL(router_, RegisterMediaSinksObserver(_)).Times(1); | 306 EXPECT_CALL(router_, RegisterMediaSinksObserver(_)).Times(1); |
234 EXPECT_FALSE(delegate_impl_->AddScreenAvailabilityListener( | 307 EXPECT_FALSE(delegate_impl_->AddScreenAvailabilityListener( |
235 render_process_id, render_frame_id, &listener)); | 308 render_process_id, render_frame_id, &listener)); |
236 } | 309 } |
237 | 310 |
238 } // namespace media_router | 311 } // namespace media_router |
OLD | NEW |