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 "base/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
6 #include "base/message_loop/message_loop.h" | 6 #include "base/message_loop/message_loop.h" |
7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
8 #include "content/browser/presentation/presentation_service_impl.h" | 8 #include "content/browser/presentation/presentation_service_impl.h" |
9 #include "content/public/browser/presentation_service_delegate.h" | 9 #include "content/public/browser/presentation_service_delegate.h" |
10 #include "content/public/browser/presentation_session.h" | 10 #include "content/public/browser/presentation_session.h" |
11 #include "content/test/test_render_frame_host.h" | 11 #include "content/test/test_render_frame_host.h" |
12 #include "content/test/test_render_view_host.h" | 12 #include "content/test/test_render_view_host.h" |
13 #include "content/test/test_web_contents.h" | 13 #include "content/test/test_web_contents.h" |
14 #include "mojo/public/cpp/bindings/interface_ptr.h" | 14 #include "mojo/public/cpp/bindings/interface_ptr.h" |
15 #include "testing/gmock/include/gmock/gmock.h" | 15 #include "testing/gmock/include/gmock/gmock.h" |
16 | 16 |
17 using ::testing::_; | 17 using ::testing::_; |
18 using ::testing::Eq; | 18 using ::testing::Eq; |
19 using ::testing::InvokeWithoutArgs; | 19 using ::testing::InvokeWithoutArgs; |
20 using ::testing::Mock; | 20 using ::testing::Mock; |
21 using ::testing::Return; | 21 using ::testing::Return; |
22 using ::testing::SaveArg; | 22 using ::testing::SaveArg; |
23 | 23 |
24 namespace content { | 24 namespace content { |
25 | 25 |
| 26 namespace { |
| 27 |
| 28 bool ArePresentationSessionsEqual( |
| 29 const presentation::PresentationSessionInfo& expected, |
| 30 const presentation::PresentationSessionInfo& actual) { |
| 31 return expected.url == actual.url && expected.id == actual.id; |
| 32 } |
| 33 } // namespace |
| 34 |
26 class MockPresentationServiceDelegate : public PresentationServiceDelegate { | 35 class MockPresentationServiceDelegate : public PresentationServiceDelegate { |
27 public: | 36 public: |
28 MOCK_METHOD1(AddObserver, | 37 MOCK_METHOD3(AddObserver, |
29 void(PresentationServiceDelegate::Observer* observer)); | 38 void(int render_process_id, |
30 MOCK_METHOD1(RemoveObserver, | 39 int render_frame_id, |
31 void(PresentationServiceDelegate::Observer* observer)); | 40 PresentationServiceDelegate::Observer* observer)); |
| 41 MOCK_METHOD2(RemoveObserver, |
| 42 void(int render_process_id, int render_frame_id)); |
32 MOCK_METHOD3(AddScreenAvailabilityListener, | 43 MOCK_METHOD3(AddScreenAvailabilityListener, |
33 bool( | 44 bool( |
34 int render_process_id, | 45 int render_process_id, |
35 int routing_id, | 46 int routing_id, |
36 PresentationScreenAvailabilityListener* listener)); | 47 PresentationScreenAvailabilityListener* listener)); |
37 MOCK_METHOD3(RemoveScreenAvailabilityListener, | 48 MOCK_METHOD3(RemoveScreenAvailabilityListener, |
38 void( | 49 void( |
39 int render_process_id, | 50 int render_process_id, |
40 int routing_id, | 51 int routing_id, |
41 PresentationScreenAvailabilityListener* listener)); | 52 PresentationScreenAvailabilityListener* listener)); |
(...skipping 20 matching lines...) Expand all Loading... |
62 int render_process_id, | 73 int render_process_id, |
63 int render_frame_id, | 74 int render_frame_id, |
64 const std::string& presentation_url, | 75 const std::string& presentation_url, |
65 const std::string& presentation_id, | 76 const std::string& presentation_id, |
66 const PresentationSessionSuccessCallback& success_cb, | 77 const PresentationSessionSuccessCallback& success_cb, |
67 const PresentationSessionErrorCallback& error_cb)); | 78 const PresentationSessionErrorCallback& error_cb)); |
68 }; | 79 }; |
69 | 80 |
70 class PresentationServiceImplTest : public RenderViewHostImplTestHarness { | 81 class PresentationServiceImplTest : public RenderViewHostImplTestHarness { |
71 public: | 82 public: |
72 PresentationServiceImplTest() : callback_count_(0) {} | 83 PresentationServiceImplTest() |
| 84 : callback_count_(0), default_session_started_count_(0) {} |
73 | 85 |
74 void SetUp() override { | 86 void SetUp() override { |
75 RenderViewHostImplTestHarness::SetUp(); | 87 RenderViewHostImplTestHarness::SetUp(); |
76 | 88 |
77 auto request = mojo::GetProxy(&service_ptr_); | 89 auto request = mojo::GetProxy(&service_ptr_); |
78 EXPECT_CALL(mock_delegate_, AddObserver(_)).Times(1); | 90 |
| 91 EXPECT_CALL(mock_delegate_, AddObserver(_, _, _)).Times(1); |
79 service_impl_.reset(new PresentationServiceImpl( | 92 service_impl_.reset(new PresentationServiceImpl( |
80 contents()->GetMainFrame(), contents(), &mock_delegate_)); | 93 contents()->GetMainFrame(), contents(), &mock_delegate_)); |
81 service_impl_->Bind(request.Pass()); | 94 service_impl_->Bind(request.Pass()); |
82 } | 95 } |
83 | 96 |
84 void TearDown() override { | 97 void TearDown() override { |
85 service_ptr_.reset(); | 98 service_ptr_.reset(); |
86 if (service_impl_.get()) { | 99 if (service_impl_.get()) { |
87 EXPECT_CALL(mock_delegate_, RemoveObserver(Eq(service_impl_.get()))) | 100 EXPECT_CALL(mock_delegate_, RemoveObserver(_, _)).Times(1); |
88 .Times(1); | |
89 service_impl_.reset(); | 101 service_impl_.reset(); |
90 } | 102 } |
91 | |
92 RenderViewHostImplTestHarness::TearDown(); | 103 RenderViewHostImplTestHarness::TearDown(); |
93 } | 104 } |
94 | 105 |
95 void ListenForScreenAvailabilityAndWait( | 106 void ListenForScreenAvailabilityAndWait( |
96 const std::string& presentation_url, | 107 const std::string& presentation_url, |
97 const base::Callback<void(const std::string&, bool)>& callback, | 108 const base::Callback<void(const std::string&, bool)>& callback, |
98 bool delegate_success) { | 109 bool delegate_success) { |
99 base::RunLoop run_loop; | 110 base::RunLoop run_loop; |
100 // This will call to |service_impl_| via mojo. Process the message | 111 // This will call to |service_impl_| via mojo. Process the message |
101 // using RunLoop. | 112 // using RunLoop. |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 bool expected, | 159 bool expected, |
149 const std::string& presentation_url, | 160 const std::string& presentation_url, |
150 bool available) { | 161 bool available) { |
151 ++callback_count_; | 162 ++callback_count_; |
152 EXPECT_EQ(expected, available); | 163 EXPECT_EQ(expected, available); |
153 if (!run_loop_quit_closure_.is_null()) | 164 if (!run_loop_quit_closure_.is_null()) |
154 run_loop_quit_closure_.Run(); | 165 run_loop_quit_closure_.Run(); |
155 } | 166 } |
156 | 167 |
157 void ExpectReset() { | 168 void ExpectReset() { |
158 EXPECT_CALL(mock_delegate_, Reset(_, _)) | 169 EXPECT_CALL(mock_delegate_, Reset(_, _)).Times(1); |
159 .Times(1); | |
160 } | 170 } |
161 | 171 |
162 void ExpectCleanState() { | 172 void ExpectCleanState() { |
163 EXPECT_TRUE(service_impl_->availability_contexts_.empty()); | 173 EXPECT_TRUE(service_impl_->availability_contexts_.empty()); |
164 EXPECT_TRUE(service_impl_->default_presentation_url_.empty()); | 174 EXPECT_TRUE(service_impl_->default_presentation_url_.empty()); |
165 EXPECT_TRUE(service_impl_->default_presentation_id_.empty()); | 175 EXPECT_TRUE(service_impl_->default_presentation_id_.empty()); |
166 EXPECT_TRUE(service_impl_->queued_start_session_requests_.empty()); | 176 EXPECT_TRUE(service_impl_->queued_start_session_requests_.empty()); |
| 177 EXPECT_FALSE(service_impl_->default_session_start_context_.get()); |
167 } | 178 } |
168 | 179 |
169 void ExpectNewSessionMojoCallbackSuccess( | 180 void ExpectNewSessionMojoCallbackSuccess( |
170 presentation::PresentationSessionInfoPtr info, | 181 presentation::PresentationSessionInfoPtr info, |
171 presentation::PresentationErrorPtr error) { | 182 presentation::PresentationErrorPtr error) { |
172 EXPECT_FALSE(info.is_null()); | 183 EXPECT_FALSE(info.is_null()); |
173 EXPECT_TRUE(error.is_null()); | 184 EXPECT_TRUE(error.is_null()); |
174 if (!run_loop_quit_closure_.is_null()) | 185 if (!run_loop_quit_closure_.is_null()) |
175 run_loop_quit_closure_.Run(); | 186 run_loop_quit_closure_.Run(); |
176 } | 187 } |
177 | 188 |
178 void ExpectNewSessionMojoCallbackError( | 189 void ExpectNewSessionMojoCallbackError( |
179 presentation::PresentationSessionInfoPtr info, | 190 presentation::PresentationSessionInfoPtr info, |
180 presentation::PresentationErrorPtr error) { | 191 presentation::PresentationErrorPtr error) { |
181 EXPECT_TRUE(info.is_null()); | 192 EXPECT_TRUE(info.is_null()); |
182 EXPECT_FALSE(error.is_null()); | 193 EXPECT_FALSE(error.is_null()); |
183 if (!run_loop_quit_closure_.is_null()) | 194 if (!run_loop_quit_closure_.is_null()) |
184 run_loop_quit_closure_.Run(); | 195 run_loop_quit_closure_.Run(); |
185 } | 196 } |
186 | 197 |
| 198 void ExpectDefaultSessionStarted( |
| 199 const presentation::PresentationSessionInfo& expected_session, |
| 200 presentation::PresentationSessionInfoPtr actual_session) { |
| 201 ASSERT_TRUE(!actual_session.is_null()); |
| 202 EXPECT_TRUE(ArePresentationSessionsEqual( |
| 203 expected_session, *actual_session)); |
| 204 ++default_session_started_count_; |
| 205 if (!run_loop_quit_closure_.is_null()) |
| 206 run_loop_quit_closure_.Run(); |
| 207 } |
| 208 |
| 209 void ExpectDefaultSessionNull( |
| 210 presentation::PresentationSessionInfoPtr actual_session) { |
| 211 EXPECT_TRUE(actual_session.is_null()); |
| 212 ++default_session_started_count_; |
| 213 if (!run_loop_quit_closure_.is_null()) |
| 214 run_loop_quit_closure_.Run(); |
| 215 } |
| 216 |
187 MockPresentationServiceDelegate mock_delegate_; | 217 MockPresentationServiceDelegate mock_delegate_; |
188 scoped_ptr<PresentationServiceImpl> service_impl_; | 218 scoped_ptr<PresentationServiceImpl> service_impl_; |
189 mojo::InterfacePtr<presentation::PresentationService> service_ptr_; | 219 mojo::InterfacePtr<presentation::PresentationService> service_ptr_; |
190 base::Closure run_loop_quit_closure_; | 220 base::Closure run_loop_quit_closure_; |
191 int callback_count_; | 221 int callback_count_; |
| 222 int default_session_started_count_; |
192 }; | 223 }; |
193 | 224 |
194 TEST_F(PresentationServiceImplTest, ListenForScreenAvailability) { | 225 TEST_F(PresentationServiceImplTest, ListenForScreenAvailability) { |
195 std::string presentation_url("http://fooUrl"); | 226 std::string presentation_url("http://fooUrl"); |
196 ListenForScreenAvailabilityAndWait( | 227 ListenForScreenAvailabilityAndWait( |
197 presentation_url, | 228 presentation_url, |
198 base::Bind( | 229 base::Bind( |
199 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback, | 230 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback, |
200 base::Unretained(this), true), | 231 base::Unretained(this), true), |
201 true); | 232 true); |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
287 ListenForScreenAvailabilityAndWait( | 318 ListenForScreenAvailabilityAndWait( |
288 presentation_url, | 319 presentation_url, |
289 base::Bind(&PresentationServiceImplTest::ShouldNotBeCalled, | 320 base::Bind(&PresentationServiceImplTest::ShouldNotBeCalled, |
290 base::Unretained(this)), | 321 base::Unretained(this)), |
291 true); | 322 true); |
292 | 323 |
293 ExpectReset(); | 324 ExpectReset(); |
294 | 325 |
295 // Since the frame matched the service, |service_impl_| will be deleted. | 326 // Since the frame matched the service, |service_impl_| will be deleted. |
296 PresentationServiceImpl* service = service_impl_.release(); | 327 PresentationServiceImpl* service = service_impl_.release(); |
297 EXPECT_CALL(mock_delegate_, RemoveObserver(Eq(service))).Times(1); | 328 EXPECT_CALL(mock_delegate_, RemoveObserver(_, _)).Times(1); |
298 service->RenderFrameDeleted(contents()->GetMainFrame()); | 329 service->RenderFrameDeleted(contents()->GetMainFrame()); |
299 } | 330 } |
300 | 331 |
301 TEST_F(PresentationServiceImplTest, NotThisRenderFrameDeleted) { | 332 TEST_F(PresentationServiceImplTest, NotThisRenderFrameDeleted) { |
302 std::string presentation_url("http://fooUrl"); | 333 std::string presentation_url("http://fooUrl"); |
303 ListenForScreenAvailabilityAndWait( | 334 ListenForScreenAvailabilityAndWait( |
304 presentation_url, | 335 presentation_url, |
305 base::Bind( | 336 base::Bind( |
306 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback, | 337 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback, |
307 base::Unretained(this), | 338 base::Unretained(this), |
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
574 | 605 |
575 // Running the callback means the first request is done. It should now | 606 // Running the callback means the first request is done. It should now |
576 // move on to the queued request. | 607 // move on to the queued request. |
577 EXPECT_CALL(mock_delegate_, StartSession( | 608 EXPECT_CALL(mock_delegate_, StartSession( |
578 _, _, Eq(presentation_url2), Eq(presentation_id2), _, _)) | 609 _, _, Eq(presentation_url2), Eq(presentation_id2), _, _)) |
579 .Times(1); | 610 .Times(1); |
580 success_cb.Run(PresentationSessionInfo(presentation_url1, presentation_id1)); | 611 success_cb.Run(PresentationSessionInfo(presentation_url1, presentation_id1)); |
581 SaveQuitClosureAndRunLoop(); | 612 SaveQuitClosureAndRunLoop(); |
582 } | 613 } |
583 | 614 |
| 615 TEST_F(PresentationServiceImplTest, ListenForDefaultSessionStart) { |
| 616 std::string presentation_url1("http://fooUrl1"); |
| 617 std::string presentation_id1("presentationId1"); |
| 618 presentation::PresentationSessionInfo expected_session; |
| 619 expected_session.url = presentation_url1; |
| 620 expected_session.id = presentation_id1; |
| 621 service_ptr_->ListenForDefaultSessionStart( |
| 622 base::Bind(&PresentationServiceImplTest::ExpectDefaultSessionStarted, |
| 623 base::Unretained(this), |
| 624 expected_session)); |
| 625 RunLoopFor(base::TimeDelta::FromMilliseconds(50)); |
| 626 service_impl_->OnDefaultPresentationStarted( |
| 627 content::PresentationSessionInfo(presentation_url1, presentation_id1)); |
| 628 SaveQuitClosureAndRunLoop(); |
| 629 EXPECT_EQ(1, default_session_started_count_); |
| 630 } |
| 631 |
| 632 TEST_F(PresentationServiceImplTest, ListenForDefaultSessionStartAfterSet) { |
| 633 // Note that the callback will only pick up presentation_url2/id2 since |
| 634 // ListenForDefaultSessionStart wasn't called yet when the DPU was still |
| 635 // presentation_url1. |
| 636 std::string presentation_url1("http://fooUrl1"); |
| 637 std::string presentation_id1("presentationId1"); |
| 638 std::string presentation_url2("http://fooUrl2"); |
| 639 std::string presentation_id2("presentationId2"); |
| 640 service_impl_->OnDefaultPresentationStarted( |
| 641 content::PresentationSessionInfo(presentation_url1, presentation_id1)); |
| 642 |
| 643 presentation::PresentationSessionInfo expected_session; |
| 644 expected_session.url = presentation_url2; |
| 645 expected_session.id = presentation_id2; |
| 646 service_ptr_->ListenForDefaultSessionStart( |
| 647 base::Bind(&PresentationServiceImplTest::ExpectDefaultSessionStarted, |
| 648 base::Unretained(this), |
| 649 expected_session)); |
| 650 RunLoopFor(base::TimeDelta::FromMilliseconds(50)); |
| 651 service_impl_->OnDefaultPresentationStarted( |
| 652 content::PresentationSessionInfo(presentation_url2, presentation_id2)); |
| 653 SaveQuitClosureAndRunLoop(); |
| 654 EXPECT_EQ(1, default_session_started_count_); |
| 655 } |
| 656 |
| 657 TEST_F(PresentationServiceImplTest, DefaultSessionStartReset) { |
| 658 service_ptr_->ListenForDefaultSessionStart( |
| 659 base::Bind(&PresentationServiceImplTest::ExpectDefaultSessionNull, |
| 660 base::Unretained(this))); |
| 661 base::RunLoop().RunUntilIdle(); |
| 662 |
| 663 ExpectReset(); |
| 664 service_impl_->Reset(); |
| 665 ExpectCleanState(); |
| 666 SaveQuitClosureAndRunLoop(); |
| 667 EXPECT_EQ(1, default_session_started_count_); |
| 668 } |
| 669 |
584 } // namespace content | 670 } // namespace content |
OLD | NEW |