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

Side by Side Diff: content/browser/presentation/presentation_service_impl_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698