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

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

Issue 979413002: [Presentation API] Additional plumbing for PresentationServiceImpl. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@REAL-NEW-MASTER
Patch Set: Rebase Created 5 years, 9 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 "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/test/test_render_frame_host.h" 11 #include "content/test/test_render_frame_host.h"
11 #include "content/test/test_render_view_host.h" 12 #include "content/test/test_render_view_host.h"
12 #include "content/test/test_web_contents.h" 13 #include "content/test/test_web_contents.h"
13 #include "mojo/public/cpp/bindings/interface_ptr.h" 14 #include "mojo/public/cpp/bindings/interface_ptr.h"
14 #include "testing/gmock/include/gmock/gmock.h" 15 #include "testing/gmock/include/gmock/gmock.h"
15 16
16 using ::testing::_; 17 using ::testing::_;
17 using ::testing::Eq; 18 using ::testing::Eq;
18 using ::testing::InvokeWithoutArgs; 19 using ::testing::InvokeWithoutArgs;
19 using ::testing::Mock; 20 using ::testing::Mock;
20 using ::testing::Return; 21 using ::testing::Return;
22 using ::testing::SaveArg;
21 23
22 namespace content { 24 namespace content {
23 25
24 class MockPresentationServiceDelegate : public PresentationServiceDelegate { 26 class MockPresentationServiceDelegate : public PresentationServiceDelegate {
25 public: 27 public:
26 MOCK_METHOD1(AddObserver, 28 MOCK_METHOD1(AddObserver,
27 void(PresentationServiceDelegate::Observer* observer)); 29 void(PresentationServiceDelegate::Observer* observer));
28 MOCK_METHOD1(RemoveObserver, 30 MOCK_METHOD1(RemoveObserver,
29 void(PresentationServiceDelegate::Observer* observer)); 31 void(PresentationServiceDelegate::Observer* observer));
30 MOCK_METHOD3(AddScreenAvailabilityListener, 32 MOCK_METHOD3(AddScreenAvailabilityListener,
31 bool( 33 bool(
32 int render_process_id, 34 int render_process_id,
33 int routing_id, 35 int routing_id,
34 PresentationScreenAvailabilityListener* listener)); 36 PresentationScreenAvailabilityListener* listener));
35 MOCK_METHOD3(RemoveScreenAvailabilityListener, 37 MOCK_METHOD3(RemoveScreenAvailabilityListener,
36 void( 38 void(
37 int render_process_id, 39 int render_process_id,
38 int routing_id, 40 int routing_id,
39 PresentationScreenAvailabilityListener* listener)); 41 PresentationScreenAvailabilityListener* listener));
40 MOCK_METHOD2(RemoveAllScreenAvailabilityListeners, 42 MOCK_METHOD2(RemoveAllScreenAvailabilityListeners,
41 void( 43 void(
42 int render_process_id, 44 int render_process_id,
43 int routing_id)); 45 int routing_id));
46 MOCK_METHOD3(SetDefaultPresentationUrl,
47 void(
48 int render_process_id,
49 int routing_id,
50 const std::string& default_presentation_url));
51 MOCK_METHOD6(StartSession,
52 void(
53 int render_process_id,
54 int render_frame_id,
55 const std::string& presentation_url,
56 const std::string& presentation_id,
57 const PresentationSessionSuccessCallback& success_cb,
58 const PresentationSessionErrorCallback& error_cb));
59 MOCK_METHOD6(JoinSession,
60 void(
61 int render_process_id,
62 int render_frame_id,
63 const std::string& presentation_url,
64 const std::string& presentation_id,
65 const PresentationSessionSuccessCallback& success_cb,
66 const PresentationSessionErrorCallback& error_cb));
44 }; 67 };
45 68
46 class PresentationServiceImplTest : public RenderViewHostImplTestHarness { 69 class PresentationServiceImplTest : public RenderViewHostImplTestHarness {
47 public: 70 public:
48 PresentationServiceImplTest() : callback_count_(0) {} 71 PresentationServiceImplTest() : callback_count_(0) {}
49 72
50 void SetUp() override { 73 void SetUp() override {
51 RenderViewHostImplTestHarness::SetUp(); 74 RenderViewHostImplTestHarness::SetUp();
52 75
53 EXPECT_CALL(mock_delegate_, AddObserver(_)).Times(1); 76 EXPECT_CALL(mock_delegate_, AddObserver(_)).Times(1);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 109
87 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_delegate_)); 110 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_delegate_));
88 } 111 }
89 112
90 void ExpectListenerDoesNotExist(const std::string& presentation_url) { 113 void ExpectListenerDoesNotExist(const std::string& presentation_url) {
91 const auto& contexts = service_impl_->availability_contexts_; 114 const auto& contexts = service_impl_->availability_contexts_;
92 auto it = contexts.find(presentation_url); 115 auto it = contexts.find(presentation_url);
93 EXPECT_TRUE(it == contexts.end()); 116 EXPECT_TRUE(it == contexts.end());
94 } 117 }
95 118
119 void RunLoopUntilIdle() {
120 base::RunLoop().RunUntilIdle();
121 }
122
96 void RunLoopFor(base::TimeDelta duration) { 123 void RunLoopFor(base::TimeDelta duration) {
97 base::RunLoop run_loop; 124 base::RunLoop run_loop;
98 base::MessageLoop::current()->PostDelayedTask( 125 base::MessageLoop::current()->PostDelayedTask(
99 FROM_HERE, run_loop.QuitClosure(), duration); 126 FROM_HERE, run_loop.QuitClosure(), duration);
100 run_loop.Run(); 127 run_loop.Run();
101 } 128 }
102 129
103 void SaveQuitClosureAndRunLoop() { 130 void SaveQuitClosureAndRunLoop() {
104 base::RunLoop run_loop; 131 base::RunLoop run_loop;
105 run_loop_quit_closure_ = run_loop.QuitClosure(); 132 run_loop_quit_closure_ = run_loop.QuitClosure();
(...skipping 13 matching lines...) Expand all
119 it->second->OnScreenAvailabilityChanged(available); 146 it->second->OnScreenAvailabilityChanged(available);
120 } 147 }
121 148
122 void ScreenAvailabilityChangedCallback(bool expected, bool available) { 149 void ScreenAvailabilityChangedCallback(bool expected, bool available) {
123 ++callback_count_; 150 ++callback_count_;
124 EXPECT_EQ(expected, available); 151 EXPECT_EQ(expected, available);
125 if (!run_loop_quit_closure_.is_null()) 152 if (!run_loop_quit_closure_.is_null())
126 run_loop_quit_closure_.Run(); 153 run_loop_quit_closure_.Run();
127 } 154 }
128 155
156 void ExpectReset() {
157 EXPECT_CALL(mock_delegate_, RemoveAllScreenAvailabilityListeners(_, _))
158 .Times(1);
159 EXPECT_CALL(mock_delegate_, SetDefaultPresentationUrl(_, _, Eq("")))
160 .Times(1);
161 }
162
163 void ExpectNewSessionMojoCallbackSuccess(
164 presentation::PresentationSessionInfoPtr info,
165 presentation::PresentationErrorPtr error) {
166 EXPECT_FALSE(info.is_null());
167 EXPECT_TRUE(error.is_null());
168 if (!run_loop_quit_closure_.is_null())
169 run_loop_quit_closure_.Run();
170 }
171
172 void ExpectNewSessionMojoCallbackError(
173 presentation::PresentationSessionInfoPtr info,
174 presentation::PresentationErrorPtr error) {
175 EXPECT_TRUE(info.is_null());
176 EXPECT_FALSE(error.is_null());
177 if (!run_loop_quit_closure_.is_null())
178 run_loop_quit_closure_.Run();
179 }
180
129 MockPresentationServiceDelegate mock_delegate_; 181 MockPresentationServiceDelegate mock_delegate_;
130 scoped_ptr<PresentationServiceImpl> service_impl_; 182 scoped_ptr<PresentationServiceImpl> service_impl_;
131 mojo::InterfacePtr<presentation::PresentationService> service_ptr_; 183 mojo::InterfacePtr<presentation::PresentationService> service_ptr_;
132 base::Closure run_loop_quit_closure_; 184 base::Closure run_loop_quit_closure_;
133 int callback_count_; 185 int callback_count_;
134 }; 186 };
135 187
136 TEST_F(PresentationServiceImplTest, GetScreenAvailability) { 188 TEST_F(PresentationServiceImplTest, GetScreenAvailability) {
137 std::string presentation_url("http://fooUrl"); 189 std::string presentation_url("http://fooUrl");
138 GetScreenAvailabilityAndWait( 190 GetScreenAvailabilityAndWait(
(...skipping 24 matching lines...) Expand all
163 service_ptr_->GetScreenAvailability( 215 service_ptr_->GetScreenAvailability(
164 presentation_url, 216 presentation_url,
165 base::Bind( 217 base::Bind(
166 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback, 218 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback,
167 base::Unretained(this), 219 base::Unretained(this),
168 false)); 220 false));
169 SaveQuitClosureAndRunLoop(); 221 SaveQuitClosureAndRunLoop();
170 EXPECT_EQ(2, callback_count_); 222 EXPECT_EQ(2, callback_count_);
171 } 223 }
172 224
173 TEST_F(PresentationServiceImplTest, RemoveAllListeners) { 225 TEST_F(PresentationServiceImplTest, Reset) {
174 std::string presentation_url("http://fooUrl"); 226 std::string presentation_url("http://fooUrl");
175 GetScreenAvailabilityAndWait( 227 GetScreenAvailabilityAndWait(
176 presentation_url, 228 presentation_url,
177 base::Bind(&PresentationServiceImplTest::ShouldNotBeCalled, 229 base::Bind(&PresentationServiceImplTest::ShouldNotBeCalled,
178 base::Unretained(this)), 230 base::Unretained(this)),
179 true); 231 true);
180 232
181 service_impl_->RemoveAllListeners(); 233 ExpectReset();
234 service_impl_->Reset();
182 235
183 ExpectListenerDoesNotExist(presentation_url); 236 ExpectListenerDoesNotExist(presentation_url);
184 237
185 EXPECT_EQ(0, callback_count_); 238 EXPECT_EQ(0, callback_count_);
186 } 239 }
187 240
188 TEST_F(PresentationServiceImplTest, DidNavigateThisFrame) { 241 TEST_F(PresentationServiceImplTest, DidNavigateThisFrame) {
189 std::string presentation_url("http://fooUrl"); 242 std::string presentation_url("http://fooUrl");
190 GetScreenAvailabilityAndWait( 243 GetScreenAvailabilityAndWait(
191 presentation_url, 244 presentation_url,
192 base::Bind(&PresentationServiceImplTest::ShouldNotBeCalled, 245 base::Bind(&PresentationServiceImplTest::ShouldNotBeCalled,
193 base::Unretained(this)), 246 base::Unretained(this)),
194 true); 247 true);
195 248
249 ExpectReset();
196 service_impl_->DidNavigateAnyFrame( 250 service_impl_->DidNavigateAnyFrame(
197 contents()->GetMainFrame(), 251 contents()->GetMainFrame(),
198 content::LoadCommittedDetails(), 252 content::LoadCommittedDetails(),
199 content::FrameNavigateParams()); 253 content::FrameNavigateParams());
200 254
201 ExpectListenerDoesNotExist(presentation_url); 255 ExpectListenerDoesNotExist(presentation_url);
202 } 256 }
203 257
204 TEST_F(PresentationServiceImplTest, DidNavigateNotThisFrame) { 258 TEST_F(PresentationServiceImplTest, DidNavigateNotThisFrame) {
205 std::string presentation_url("http://fooUrl"); 259 std::string presentation_url("http://fooUrl");
(...skipping 19 matching lines...) Expand all
225 } 279 }
226 280
227 TEST_F(PresentationServiceImplTest, ThisRenderFrameDeleted) { 281 TEST_F(PresentationServiceImplTest, ThisRenderFrameDeleted) {
228 std::string presentation_url("http://fooUrl"); 282 std::string presentation_url("http://fooUrl");
229 GetScreenAvailabilityAndWait( 283 GetScreenAvailabilityAndWait(
230 presentation_url, 284 presentation_url,
231 base::Bind(&PresentationServiceImplTest::ShouldNotBeCalled, 285 base::Bind(&PresentationServiceImplTest::ShouldNotBeCalled,
232 base::Unretained(this)), 286 base::Unretained(this)),
233 true); 287 true);
234 288
289 ExpectReset();
235 service_impl_->RenderFrameDeleted(contents()->GetMainFrame()); 290 service_impl_->RenderFrameDeleted(contents()->GetMainFrame());
236 291
237 ExpectListenerDoesNotExist(presentation_url); 292 ExpectListenerDoesNotExist(presentation_url);
238 } 293 }
239 294
240 TEST_F(PresentationServiceImplTest, NotThisRenderFrameDeleted) { 295 TEST_F(PresentationServiceImplTest, NotThisRenderFrameDeleted) {
241 std::string presentation_url("http://fooUrl"); 296 std::string presentation_url("http://fooUrl");
242 GetScreenAvailabilityAndWait( 297 GetScreenAvailabilityAndWait(
243 presentation_url, 298 presentation_url,
244 base::Bind( 299 base::Bind(
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
289 std::string presentation_url("http://fooUrl"); 344 std::string presentation_url("http://fooUrl");
290 GetScreenAvailabilityAndWait( 345 GetScreenAvailabilityAndWait(
291 presentation_url, 346 presentation_url,
292 base::Bind(&PresentationServiceImplTest::ShouldNotBeCalled, 347 base::Bind(&PresentationServiceImplTest::ShouldNotBeCalled,
293 base::Unretained(this)), 348 base::Unretained(this)),
294 false); 349 false);
295 350
296 ExpectListenerDoesNotExist(presentation_url); 351 ExpectListenerDoesNotExist(presentation_url);
297 } 352 }
298 353
354 TEST_F(PresentationServiceImplTest, SetDefaultPresentationUrl) {
355 std::string url1("http://fooUrl");
356 EXPECT_CALL(mock_delegate_, SetDefaultPresentationUrl(_, _, Eq(url1)))
357 .Times(1);
358 service_impl_->SetDefaultPresentationUrl(url1);
359
360 std::string url2("http://barUrl");
361 EXPECT_CALL(mock_delegate_, SetDefaultPresentationUrl(_, _, Eq(url2)))
362 .Times(1);
363 service_impl_->SetDefaultPresentationUrl(url2);
364 }
365
366 TEST_F(PresentationServiceImplTest, SetSameDefaultPresentationUrl) {
367 std::string url("http://fooUrl");
368 EXPECT_CALL(mock_delegate_, SetDefaultPresentationUrl(_, _, Eq(url)))
369 .Times(1);
370 service_impl_->SetDefaultPresentationUrl(url);
371 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_delegate_));
372
373 // Same URL as before; no-ops.
374 service_impl_->SetDefaultPresentationUrl(url);
375 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_delegate_));
376 }
377
378 TEST_F(PresentationServiceImplTest, ClearDefaultPresentationUrl) {
379 std::string url("http://fooUrl");
380 EXPECT_CALL(mock_delegate_, SetDefaultPresentationUrl(_, _, Eq(url)))
381 .Times(1);
382 service_impl_->SetDefaultPresentationUrl(url);
383
384 // Clears the default presentation URL.
385 EXPECT_CALL(
386 mock_delegate_,
387 SetDefaultPresentationUrl(_, _, Eq(std::string())))
388 .Times(1);
389 service_impl_->SetDefaultPresentationUrl(std::string());
390 EXPECT_TRUE(service_impl_->default_presentation_url_.empty());
391 }
392
393 TEST_F(PresentationServiceImplTest, StartSessionSuccess) {
394 std::string presentation_url("http://fooUrl");
395 std::string presentation_id("presentationId");
396 service_ptr_->StartSession(
397 presentation_url,
398 presentation_id,
399 base::Bind(
400 &PresentationServiceImplTest::ExpectNewSessionMojoCallbackSuccess,
401 base::Unretained(this)));
402 base::Callback<void(const PresentationSessionInfo&)> success_cb;
403 EXPECT_CALL(mock_delegate_, StartSession(
404 _, _, Eq(presentation_url), Eq(presentation_id), _, _))
405 .WillOnce(SaveArg<4>(&success_cb));
406 RunLoopUntilIdle();
407 success_cb.Run(PresentationSessionInfo(presentation_url, presentation_id));
408 SaveQuitClosureAndRunLoop();
409 }
410
411 TEST_F(PresentationServiceImplTest, StartSessionError) {
412 std::string presentation_url("http://fooUrl");
413 std::string presentation_id("presentationId");
414 service_ptr_->StartSession(
415 presentation_url,
416 presentation_id,
417 base::Bind(
418 &PresentationServiceImplTest::ExpectNewSessionMojoCallbackError,
419 base::Unretained(this)));
420 base::Callback<void(const PresentationError&)> error_cb;
421 EXPECT_CALL(mock_delegate_, StartSession(
422 _, _, Eq(presentation_url), Eq(presentation_id), _, _))
423 .WillOnce(SaveArg<5>(&error_cb));
424 RunLoopUntilIdle();
425 error_cb.Run(PresentationError(PRESENTATION_ERROR_UNKNOWN, "Error message"));
426 SaveQuitClosureAndRunLoop();
427 }
428
429 TEST_F(PresentationServiceImplTest, JoinSessionSuccess) {
430 std::string presentation_url("http://fooUrl");
431 std::string presentation_id("presentationId");
432 service_ptr_->JoinSession(
433 presentation_url,
434 presentation_id,
435 base::Bind(
436 &PresentationServiceImplTest::ExpectNewSessionMojoCallbackSuccess,
437 base::Unretained(this)));
438 base::Callback<void(const PresentationSessionInfo&)> success_cb;
439 EXPECT_CALL(mock_delegate_, JoinSession(
440 _, _, Eq(presentation_url), Eq(presentation_id), _, _))
441 .WillOnce(SaveArg<4>(&success_cb));
442 RunLoopUntilIdle();
443 success_cb.Run(PresentationSessionInfo(presentation_url, presentation_id));
444 SaveQuitClosureAndRunLoop();
445 }
446
447 TEST_F(PresentationServiceImplTest, JoinSessionError) {
448 std::string presentation_url("http://fooUrl");
449 std::string presentation_id("presentationId");
450 service_ptr_->JoinSession(
451 presentation_url,
452 presentation_id,
453 base::Bind(
454 &PresentationServiceImplTest::ExpectNewSessionMojoCallbackError,
455 base::Unretained(this)));
456 base::Callback<void(const PresentationError&)> error_cb;
457 EXPECT_CALL(mock_delegate_, JoinSession(
458 _, _, Eq(presentation_url), Eq(presentation_id), _, _))
459 .WillOnce(SaveArg<5>(&error_cb));
460 RunLoopUntilIdle();
461 error_cb.Run(PresentationError(PRESENTATION_ERROR_UNKNOWN, "Error message"));
462 SaveQuitClosureAndRunLoop();
463 }
464
465 TEST_F(PresentationServiceImplTest, StartSessionInProgress) {
466 std::string presentation_url("http://fooUrl");
467 std::string presentation_id("presentationId");
468 service_ptr_->StartSession(
469 presentation_url,
470 presentation_id,
471 base::Bind(
472 &PresentationServiceImplTest::ExpectNewSessionMojoCallbackSuccess,
473 base::Unretained(this)));
474 service_ptr_->StartSession(
475 presentation_url,
476 presentation_id,
477 base::Bind(
478 &PresentationServiceImplTest::ExpectNewSessionMojoCallbackSuccess,
479 base::Unretained(this)));
480 EXPECT_CALL(mock_delegate_, StartSession(
481 _, _, Eq(presentation_url), Eq(presentation_id), _, _))
482 .Times(1);
483 RunLoopUntilIdle();
484 }
485
299 } // namespace content 486 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698