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 | |
35 class MockPresentationServiceDelegate : public PresentationServiceDelegate { | 26 class MockPresentationServiceDelegate : public PresentationServiceDelegate { |
36 public: | 27 public: |
37 MOCK_METHOD3(AddObserver, | 28 MOCK_METHOD1(AddObserver, |
38 void(int render_process_id, | 29 void(PresentationServiceDelegate::Observer* observer)); |
39 int render_frame_id, | 30 MOCK_METHOD1(RemoveObserver, |
40 PresentationServiceDelegate::Observer* observer)); | 31 void(PresentationServiceDelegate::Observer* observer)); |
41 MOCK_METHOD2(RemoveObserver, | |
42 void(int render_process_id, int render_frame_id)); | |
43 MOCK_METHOD3(AddScreenAvailabilityListener, | 32 MOCK_METHOD3(AddScreenAvailabilityListener, |
44 bool( | 33 bool( |
45 int render_process_id, | 34 int render_process_id, |
46 int routing_id, | 35 int routing_id, |
47 PresentationScreenAvailabilityListener* listener)); | 36 PresentationScreenAvailabilityListener* listener)); |
48 MOCK_METHOD3(RemoveScreenAvailabilityListener, | 37 MOCK_METHOD3(RemoveScreenAvailabilityListener, |
49 void( | 38 void( |
50 int render_process_id, | 39 int render_process_id, |
51 int routing_id, | 40 int routing_id, |
52 PresentationScreenAvailabilityListener* listener)); | 41 PresentationScreenAvailabilityListener* listener)); |
(...skipping 20 matching lines...) Expand all Loading... |
73 int render_process_id, | 62 int render_process_id, |
74 int render_frame_id, | 63 int render_frame_id, |
75 const std::string& presentation_url, | 64 const std::string& presentation_url, |
76 const std::string& presentation_id, | 65 const std::string& presentation_id, |
77 const PresentationSessionSuccessCallback& success_cb, | 66 const PresentationSessionSuccessCallback& success_cb, |
78 const PresentationSessionErrorCallback& error_cb)); | 67 const PresentationSessionErrorCallback& error_cb)); |
79 }; | 68 }; |
80 | 69 |
81 class PresentationServiceImplTest : public RenderViewHostImplTestHarness { | 70 class PresentationServiceImplTest : public RenderViewHostImplTestHarness { |
82 public: | 71 public: |
83 PresentationServiceImplTest() | 72 PresentationServiceImplTest() : callback_count_(0) {} |
84 : callback_count_(0), default_session_started_count_(0) {} | |
85 | 73 |
86 void SetUp() override { | 74 void SetUp() override { |
87 RenderViewHostImplTestHarness::SetUp(); | 75 RenderViewHostImplTestHarness::SetUp(); |
88 | 76 |
89 auto request = mojo::GetProxy(&service_ptr_); | 77 auto request = mojo::GetProxy(&service_ptr_); |
90 | 78 EXPECT_CALL(mock_delegate_, AddObserver(_)).Times(1); |
91 EXPECT_CALL(mock_delegate_, AddObserver(_, _, _)).Times(1); | |
92 service_impl_.reset(new PresentationServiceImpl( | 79 service_impl_.reset(new PresentationServiceImpl( |
93 contents()->GetMainFrame(), contents(), &mock_delegate_)); | 80 contents()->GetMainFrame(), contents(), &mock_delegate_)); |
94 service_impl_->Bind(request.Pass()); | 81 service_impl_->Bind(request.Pass()); |
95 } | 82 } |
96 | 83 |
97 void TearDown() override { | 84 void TearDown() override { |
98 service_ptr_.reset(); | 85 service_ptr_.reset(); |
99 if (service_impl_.get()) { | 86 if (service_impl_.get()) { |
100 EXPECT_CALL(mock_delegate_, RemoveObserver(_, _)).Times(1); | 87 EXPECT_CALL(mock_delegate_, RemoveObserver(Eq(service_impl_.get()))) |
| 88 .Times(1); |
101 service_impl_.reset(); | 89 service_impl_.reset(); |
102 } | 90 } |
| 91 |
103 RenderViewHostImplTestHarness::TearDown(); | 92 RenderViewHostImplTestHarness::TearDown(); |
104 } | 93 } |
105 | 94 |
106 void ListenForScreenAvailabilityAndWait( | 95 void ListenForScreenAvailabilityAndWait( |
107 const std::string& presentation_url, | 96 const std::string& presentation_url, |
108 const base::Callback<void(const std::string&, bool)>& callback, | 97 const base::Callback<void(const std::string&, bool)>& callback, |
109 bool delegate_success) { | 98 bool delegate_success) { |
110 base::RunLoop run_loop; | 99 base::RunLoop run_loop; |
111 // This will call to |service_impl_| via mojo. Process the message | 100 // This will call to |service_impl_| via mojo. Process the message |
112 // using RunLoop. | 101 // using RunLoop. |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 bool expected, | 148 bool expected, |
160 const std::string& presentation_url, | 149 const std::string& presentation_url, |
161 bool available) { | 150 bool available) { |
162 ++callback_count_; | 151 ++callback_count_; |
163 EXPECT_EQ(expected, available); | 152 EXPECT_EQ(expected, available); |
164 if (!run_loop_quit_closure_.is_null()) | 153 if (!run_loop_quit_closure_.is_null()) |
165 run_loop_quit_closure_.Run(); | 154 run_loop_quit_closure_.Run(); |
166 } | 155 } |
167 | 156 |
168 void ExpectReset() { | 157 void ExpectReset() { |
169 EXPECT_CALL(mock_delegate_, Reset(_, _)).Times(1); | 158 EXPECT_CALL(mock_delegate_, Reset(_, _)) |
| 159 .Times(1); |
170 } | 160 } |
171 | 161 |
172 void ExpectCleanState() { | 162 void ExpectCleanState() { |
173 EXPECT_TRUE(service_impl_->availability_contexts_.empty()); | 163 EXPECT_TRUE(service_impl_->availability_contexts_.empty()); |
174 EXPECT_TRUE(service_impl_->default_presentation_url_.empty()); | 164 EXPECT_TRUE(service_impl_->default_presentation_url_.empty()); |
175 EXPECT_TRUE(service_impl_->default_presentation_id_.empty()); | 165 EXPECT_TRUE(service_impl_->default_presentation_id_.empty()); |
176 EXPECT_TRUE(service_impl_->queued_start_session_requests_.empty()); | 166 EXPECT_TRUE(service_impl_->queued_start_session_requests_.empty()); |
177 EXPECT_FALSE(service_impl_->default_session_start_context_.get()); | |
178 } | 167 } |
179 | 168 |
180 void ExpectNewSessionMojoCallbackSuccess( | 169 void ExpectNewSessionMojoCallbackSuccess( |
181 presentation::PresentationSessionInfoPtr info, | 170 presentation::PresentationSessionInfoPtr info, |
182 presentation::PresentationErrorPtr error) { | 171 presentation::PresentationErrorPtr error) { |
183 EXPECT_FALSE(info.is_null()); | 172 EXPECT_FALSE(info.is_null()); |
184 EXPECT_TRUE(error.is_null()); | 173 EXPECT_TRUE(error.is_null()); |
185 if (!run_loop_quit_closure_.is_null()) | 174 if (!run_loop_quit_closure_.is_null()) |
186 run_loop_quit_closure_.Run(); | 175 run_loop_quit_closure_.Run(); |
187 } | 176 } |
188 | 177 |
189 void ExpectNewSessionMojoCallbackError( | 178 void ExpectNewSessionMojoCallbackError( |
190 presentation::PresentationSessionInfoPtr info, | 179 presentation::PresentationSessionInfoPtr info, |
191 presentation::PresentationErrorPtr error) { | 180 presentation::PresentationErrorPtr error) { |
192 EXPECT_TRUE(info.is_null()); | 181 EXPECT_TRUE(info.is_null()); |
193 EXPECT_FALSE(error.is_null()); | 182 EXPECT_FALSE(error.is_null()); |
194 if (!run_loop_quit_closure_.is_null()) | 183 if (!run_loop_quit_closure_.is_null()) |
195 run_loop_quit_closure_.Run(); | 184 run_loop_quit_closure_.Run(); |
196 } | 185 } |
197 | 186 |
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 | |
217 MockPresentationServiceDelegate mock_delegate_; | 187 MockPresentationServiceDelegate mock_delegate_; |
218 scoped_ptr<PresentationServiceImpl> service_impl_; | 188 scoped_ptr<PresentationServiceImpl> service_impl_; |
219 mojo::InterfacePtr<presentation::PresentationService> service_ptr_; | 189 mojo::InterfacePtr<presentation::PresentationService> service_ptr_; |
220 base::Closure run_loop_quit_closure_; | 190 base::Closure run_loop_quit_closure_; |
221 int callback_count_; | 191 int callback_count_; |
222 int default_session_started_count_; | |
223 }; | 192 }; |
224 | 193 |
225 TEST_F(PresentationServiceImplTest, ListenForScreenAvailability) { | 194 TEST_F(PresentationServiceImplTest, ListenForScreenAvailability) { |
226 std::string presentation_url("http://fooUrl"); | 195 std::string presentation_url("http://fooUrl"); |
227 ListenForScreenAvailabilityAndWait( | 196 ListenForScreenAvailabilityAndWait( |
228 presentation_url, | 197 presentation_url, |
229 base::Bind( | 198 base::Bind( |
230 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback, | 199 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback, |
231 base::Unretained(this), true), | 200 base::Unretained(this), true), |
232 true); | 201 true); |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
318 ListenForScreenAvailabilityAndWait( | 287 ListenForScreenAvailabilityAndWait( |
319 presentation_url, | 288 presentation_url, |
320 base::Bind(&PresentationServiceImplTest::ShouldNotBeCalled, | 289 base::Bind(&PresentationServiceImplTest::ShouldNotBeCalled, |
321 base::Unretained(this)), | 290 base::Unretained(this)), |
322 true); | 291 true); |
323 | 292 |
324 ExpectReset(); | 293 ExpectReset(); |
325 | 294 |
326 // Since the frame matched the service, |service_impl_| will be deleted. | 295 // Since the frame matched the service, |service_impl_| will be deleted. |
327 PresentationServiceImpl* service = service_impl_.release(); | 296 PresentationServiceImpl* service = service_impl_.release(); |
328 EXPECT_CALL(mock_delegate_, RemoveObserver(_, _)).Times(1); | 297 EXPECT_CALL(mock_delegate_, RemoveObserver(Eq(service))).Times(1); |
329 service->RenderFrameDeleted(contents()->GetMainFrame()); | 298 service->RenderFrameDeleted(contents()->GetMainFrame()); |
330 } | 299 } |
331 | 300 |
332 TEST_F(PresentationServiceImplTest, NotThisRenderFrameDeleted) { | 301 TEST_F(PresentationServiceImplTest, NotThisRenderFrameDeleted) { |
333 std::string presentation_url("http://fooUrl"); | 302 std::string presentation_url("http://fooUrl"); |
334 ListenForScreenAvailabilityAndWait( | 303 ListenForScreenAvailabilityAndWait( |
335 presentation_url, | 304 presentation_url, |
336 base::Bind( | 305 base::Bind( |
337 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback, | 306 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback, |
338 base::Unretained(this), | 307 base::Unretained(this), |
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
605 | 574 |
606 // Running the callback means the first request is done. It should now | 575 // Running the callback means the first request is done. It should now |
607 // move on to the queued request. | 576 // move on to the queued request. |
608 EXPECT_CALL(mock_delegate_, StartSession( | 577 EXPECT_CALL(mock_delegate_, StartSession( |
609 _, _, Eq(presentation_url2), Eq(presentation_id2), _, _)) | 578 _, _, Eq(presentation_url2), Eq(presentation_id2), _, _)) |
610 .Times(1); | 579 .Times(1); |
611 success_cb.Run(PresentationSessionInfo(presentation_url1, presentation_id1)); | 580 success_cb.Run(PresentationSessionInfo(presentation_url1, presentation_id1)); |
612 SaveQuitClosureAndRunLoop(); | 581 SaveQuitClosureAndRunLoop(); |
613 } | 582 } |
614 | 583 |
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 | |
670 } // namespace content | 584 } // namespace content |
OLD | NEW |