| 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/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_METHOD4(SetDefaultPresentationUrl, |
| 47 void( |
| 48 int render_process_id, |
| 49 int routing_id, |
| 50 const std::string& default_presentation_url, |
| 51 const std::string& default_presentation_id)); |
| 52 MOCK_METHOD6(StartSession, |
| 53 void( |
| 54 int render_process_id, |
| 55 int render_frame_id, |
| 56 const std::string& presentation_url, |
| 57 const std::string& presentation_id, |
| 58 const PresentationSessionSuccessCallback& success_cb, |
| 59 const PresentationSessionErrorCallback& error_cb)); |
| 60 MOCK_METHOD6(JoinSession, |
| 61 void( |
| 62 int render_process_id, |
| 63 int render_frame_id, |
| 64 const std::string& presentation_url, |
| 65 const std::string& presentation_id, |
| 66 const PresentationSessionSuccessCallback& success_cb, |
| 67 const PresentationSessionErrorCallback& error_cb)); |
| 44 }; | 68 }; |
| 45 | 69 |
| 46 class PresentationServiceImplTest : public RenderViewHostImplTestHarness { | 70 class PresentationServiceImplTest : public RenderViewHostImplTestHarness { |
| 47 public: | 71 public: |
| 48 PresentationServiceImplTest() : callback_count_(0) {} | 72 PresentationServiceImplTest() : callback_count_(0) {} |
| 49 | 73 |
| 50 void SetUp() override { | 74 void SetUp() override { |
| 51 RenderViewHostImplTestHarness::SetUp(); | 75 RenderViewHostImplTestHarness::SetUp(); |
| 52 | 76 |
| 53 EXPECT_CALL(mock_delegate_, AddObserver(_)).Times(1); | 77 EXPECT_CALL(mock_delegate_, AddObserver(_)).Times(1); |
| 54 service_impl_.reset(mojo::WeakBindToProxy( | 78 service_impl_.reset(mojo::WeakBindToProxy( |
| 55 new PresentationServiceImpl( | 79 new PresentationServiceImpl( |
| 56 contents()->GetMainFrame(), contents(), &mock_delegate_), | 80 contents()->GetMainFrame(), contents(), &mock_delegate_), |
| 57 &service_ptr_)); | 81 &service_ptr_)); |
| 58 } | 82 } |
| 59 | 83 |
| 60 void TearDown() override { | 84 void TearDown() override { |
| 61 service_ptr_.reset(); | 85 service_ptr_.reset(); |
| 62 | 86 |
| 63 EXPECT_CALL(mock_delegate_, RemoveObserver(Eq(service_impl_.get()))) | 87 EXPECT_CALL(mock_delegate_, RemoveObserver(Eq(service_impl_.get()))) |
| 64 .Times(1); | 88 .Times(1); |
| 65 service_impl_.reset(); | 89 service_impl_.reset(); |
| 66 | 90 |
| 67 RenderViewHostImplTestHarness::TearDown(); | 91 RenderViewHostImplTestHarness::TearDown(); |
| 68 } | 92 } |
| 69 | 93 |
| 70 void GetScreenAvailabilityAndWait( | 94 void GetScreenAvailabilityAndWait( |
| 71 const std::string& presentation_url, | 95 const std::string& presentation_url, |
| 72 const base::Callback<void(bool)>& callback, | 96 const base::Callback<void(const std::string&, bool)>& callback, |
| 73 bool delegate_success) { | 97 bool delegate_success) { |
| 74 VLOG(1) << "GetScreenAvailabilityAndWait for " << presentation_url; | |
| 75 base::RunLoop run_loop; | 98 base::RunLoop run_loop; |
| 76 // This will call to |service_impl_| via mojo. Process the message | 99 // This will call to |service_impl_| via mojo. Process the message |
| 77 // using RunLoop. | 100 // using RunLoop. |
| 78 // The callback shouldn't be invoked since there is no availability | 101 // The callback shouldn't be invoked since there is no availability |
| 79 // result yet. | 102 // result yet. |
| 80 EXPECT_CALL(mock_delegate_, AddScreenAvailabilityListener(_, _, _)) | 103 EXPECT_CALL(mock_delegate_, AddScreenAvailabilityListener(_, _, _)) |
| 81 .WillOnce(DoAll( | 104 .WillOnce(DoAll( |
| 82 InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit), | 105 InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit), |
| 83 Return(delegate_success))); | 106 Return(delegate_success))); |
| 84 service_ptr_->GetScreenAvailability(presentation_url, callback); | 107 service_ptr_->GetScreenAvailability(presentation_url, callback); |
| 85 run_loop.Run(); | 108 run_loop.Run(); |
| 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(); |
| 106 run_loop.Run(); | 133 run_loop.Run(); |
| 107 run_loop_quit_closure_.Reset(); | 134 run_loop_quit_closure_.Reset(); |
| 108 } | 135 } |
| 109 | 136 |
| 110 void ShouldNotBeCalled(bool available) { | 137 void ShouldNotBeCalled(const std::string& presentation_url, bool available) { |
| 111 FAIL() << "Callback unexpectedly invoked with available = " << available; | 138 FAIL() << "Callback unexpectedly invoked with " |
| 139 << "url = " << presentation_url << ", available = " << available; |
| 112 } | 140 } |
| 113 | 141 |
| 114 void SimulateScreenAvailabilityChange( | 142 void SimulateScreenAvailabilityChange( |
| 115 const std::string& presentation_url, bool available) { | 143 const std::string& presentation_url, bool available) { |
| 116 const auto& contexts = service_impl_->availability_contexts_; | 144 const auto& contexts = service_impl_->availability_contexts_; |
| 117 auto it = contexts.find(presentation_url); | 145 auto it = contexts.find(presentation_url); |
| 118 ASSERT_TRUE(it != contexts.end()); | 146 ASSERT_TRUE(it != contexts.end()); |
| 119 it->second->OnScreenAvailabilityChanged(available); | 147 it->second->OnScreenAvailabilityChanged(available); |
| 120 } | 148 } |
| 121 | 149 |
| 122 void ScreenAvailabilityChangedCallback(bool expected, bool available) { | 150 void ScreenAvailabilityChangedCallback( |
| 151 bool expected, |
| 152 const std::string& presentation_url, |
| 153 bool available) { |
| 123 ++callback_count_; | 154 ++callback_count_; |
| 124 EXPECT_EQ(expected, available); | 155 EXPECT_EQ(expected, available); |
| 125 if (!run_loop_quit_closure_.is_null()) | 156 if (!run_loop_quit_closure_.is_null()) |
| 126 run_loop_quit_closure_.Run(); | 157 run_loop_quit_closure_.Run(); |
| 127 } | 158 } |
| 128 | 159 |
| 160 void ExpectReset() { |
| 161 EXPECT_CALL(mock_delegate_, RemoveAllScreenAvailabilityListeners(_, _)) |
| 162 .Times(1); |
| 163 EXPECT_CALL(mock_delegate_, SetDefaultPresentationUrl(_, _, Eq(""), Eq(""))) |
| 164 .Times(1); |
| 165 } |
| 166 |
| 167 void ExpectCleanState() { |
| 168 EXPECT_TRUE(service_impl_->availability_contexts_.empty()); |
| 169 EXPECT_TRUE(service_impl_->default_presentation_url_.empty()); |
| 170 EXPECT_TRUE(service_impl_->default_presentation_id_.empty()); |
| 171 EXPECT_FALSE(service_impl_->start_session_in_progress_); |
| 172 EXPECT_TRUE(service_impl_->queued_start_session_requests_.empty()); |
| 173 } |
| 174 |
| 175 void ExpectNewSessionMojoCallbackSuccess( |
| 176 presentation::PresentationSessionInfoPtr info, |
| 177 presentation::PresentationErrorPtr error) { |
| 178 EXPECT_FALSE(info.is_null()); |
| 179 EXPECT_TRUE(error.is_null()); |
| 180 if (!run_loop_quit_closure_.is_null()) |
| 181 run_loop_quit_closure_.Run(); |
| 182 } |
| 183 |
| 184 void ExpectNewSessionMojoCallbackError( |
| 185 presentation::PresentationSessionInfoPtr info, |
| 186 presentation::PresentationErrorPtr error) { |
| 187 EXPECT_TRUE(info.is_null()); |
| 188 EXPECT_FALSE(error.is_null()); |
| 189 if (!run_loop_quit_closure_.is_null()) |
| 190 run_loop_quit_closure_.Run(); |
| 191 } |
| 192 |
| 129 MockPresentationServiceDelegate mock_delegate_; | 193 MockPresentationServiceDelegate mock_delegate_; |
| 130 scoped_ptr<PresentationServiceImpl> service_impl_; | 194 scoped_ptr<PresentationServiceImpl> service_impl_; |
| 131 mojo::InterfacePtr<presentation::PresentationService> service_ptr_; | 195 mojo::InterfacePtr<presentation::PresentationService> service_ptr_; |
| 132 base::Closure run_loop_quit_closure_; | 196 base::Closure run_loop_quit_closure_; |
| 133 int callback_count_; | 197 int callback_count_; |
| 134 }; | 198 }; |
| 135 | 199 |
| 136 TEST_F(PresentationServiceImplTest, GetScreenAvailability) { | 200 TEST_F(PresentationServiceImplTest, GetScreenAvailability) { |
| 137 std::string presentation_url("http://fooUrl"); | 201 std::string presentation_url("http://fooUrl"); |
| 138 GetScreenAvailabilityAndWait( | 202 GetScreenAvailabilityAndWait( |
| (...skipping 24 matching lines...) Expand all Loading... |
| 163 service_ptr_->GetScreenAvailability( | 227 service_ptr_->GetScreenAvailability( |
| 164 presentation_url, | 228 presentation_url, |
| 165 base::Bind( | 229 base::Bind( |
| 166 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback, | 230 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback, |
| 167 base::Unretained(this), | 231 base::Unretained(this), |
| 168 false)); | 232 false)); |
| 169 SaveQuitClosureAndRunLoop(); | 233 SaveQuitClosureAndRunLoop(); |
| 170 EXPECT_EQ(2, callback_count_); | 234 EXPECT_EQ(2, callback_count_); |
| 171 } | 235 } |
| 172 | 236 |
| 173 TEST_F(PresentationServiceImplTest, RemoveAllListeners) { | 237 TEST_F(PresentationServiceImplTest, Reset) { |
| 174 std::string presentation_url("http://fooUrl"); | 238 std::string presentation_url("http://fooUrl"); |
| 175 GetScreenAvailabilityAndWait( | 239 GetScreenAvailabilityAndWait( |
| 176 presentation_url, | 240 presentation_url, |
| 177 base::Bind(&PresentationServiceImplTest::ShouldNotBeCalled, | 241 base::Bind(&PresentationServiceImplTest::ShouldNotBeCalled, |
| 178 base::Unretained(this)), | 242 base::Unretained(this)), |
| 179 true); | 243 true); |
| 180 | 244 |
| 181 service_impl_->RemoveAllListeners(); | 245 ExpectReset(); |
| 182 | 246 service_impl_->Reset(); |
| 183 ExpectListenerDoesNotExist(presentation_url); | 247 ExpectCleanState(); |
| 184 | 248 |
| 185 EXPECT_EQ(0, callback_count_); | 249 EXPECT_EQ(0, callback_count_); |
| 186 } | 250 } |
| 187 | 251 |
| 188 TEST_F(PresentationServiceImplTest, DidNavigateThisFrame) { | 252 TEST_F(PresentationServiceImplTest, DidNavigateThisFrame) { |
| 189 std::string presentation_url("http://fooUrl"); | 253 std::string presentation_url("http://fooUrl"); |
| 190 GetScreenAvailabilityAndWait( | 254 GetScreenAvailabilityAndWait( |
| 191 presentation_url, | 255 presentation_url, |
| 192 base::Bind(&PresentationServiceImplTest::ShouldNotBeCalled, | 256 base::Bind(&PresentationServiceImplTest::ShouldNotBeCalled, |
| 193 base::Unretained(this)), | 257 base::Unretained(this)), |
| 194 true); | 258 true); |
| 195 | 259 |
| 260 ExpectReset(); |
| 196 service_impl_->DidNavigateAnyFrame( | 261 service_impl_->DidNavigateAnyFrame( |
| 197 contents()->GetMainFrame(), | 262 contents()->GetMainFrame(), |
| 198 content::LoadCommittedDetails(), | 263 content::LoadCommittedDetails(), |
| 199 content::FrameNavigateParams()); | 264 content::FrameNavigateParams()); |
| 200 | 265 ExpectCleanState(); |
| 201 ExpectListenerDoesNotExist(presentation_url); | |
| 202 } | 266 } |
| 203 | 267 |
| 204 TEST_F(PresentationServiceImplTest, DidNavigateNotThisFrame) { | 268 TEST_F(PresentationServiceImplTest, DidNavigateNotThisFrame) { |
| 205 std::string presentation_url("http://fooUrl"); | 269 std::string presentation_url("http://fooUrl"); |
| 206 GetScreenAvailabilityAndWait( | 270 GetScreenAvailabilityAndWait( |
| 207 presentation_url, | 271 presentation_url, |
| 208 base::Bind( | 272 base::Bind( |
| 209 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback, | 273 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback, |
| 210 base::Unretained(this), | 274 base::Unretained(this), |
| 211 true), | 275 true), |
| (...skipping 13 matching lines...) Expand all Loading... |
| 225 } | 289 } |
| 226 | 290 |
| 227 TEST_F(PresentationServiceImplTest, ThisRenderFrameDeleted) { | 291 TEST_F(PresentationServiceImplTest, ThisRenderFrameDeleted) { |
| 228 std::string presentation_url("http://fooUrl"); | 292 std::string presentation_url("http://fooUrl"); |
| 229 GetScreenAvailabilityAndWait( | 293 GetScreenAvailabilityAndWait( |
| 230 presentation_url, | 294 presentation_url, |
| 231 base::Bind(&PresentationServiceImplTest::ShouldNotBeCalled, | 295 base::Bind(&PresentationServiceImplTest::ShouldNotBeCalled, |
| 232 base::Unretained(this)), | 296 base::Unretained(this)), |
| 233 true); | 297 true); |
| 234 | 298 |
| 299 ExpectReset(); |
| 235 service_impl_->RenderFrameDeleted(contents()->GetMainFrame()); | 300 service_impl_->RenderFrameDeleted(contents()->GetMainFrame()); |
| 236 | 301 ExpectCleanState(); |
| 237 ExpectListenerDoesNotExist(presentation_url); | |
| 238 } | 302 } |
| 239 | 303 |
| 240 TEST_F(PresentationServiceImplTest, NotThisRenderFrameDeleted) { | 304 TEST_F(PresentationServiceImplTest, NotThisRenderFrameDeleted) { |
| 241 std::string presentation_url("http://fooUrl"); | 305 std::string presentation_url("http://fooUrl"); |
| 242 GetScreenAvailabilityAndWait( | 306 GetScreenAvailabilityAndWait( |
| 243 presentation_url, | 307 presentation_url, |
| 244 base::Bind( | 308 base::Bind( |
| 245 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback, | 309 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback, |
| 246 base::Unretained(this), | 310 base::Unretained(this), |
| 247 true), | 311 true), |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 289 std::string presentation_url("http://fooUrl"); | 353 std::string presentation_url("http://fooUrl"); |
| 290 GetScreenAvailabilityAndWait( | 354 GetScreenAvailabilityAndWait( |
| 291 presentation_url, | 355 presentation_url, |
| 292 base::Bind(&PresentationServiceImplTest::ShouldNotBeCalled, | 356 base::Bind(&PresentationServiceImplTest::ShouldNotBeCalled, |
| 293 base::Unretained(this)), | 357 base::Unretained(this)), |
| 294 false); | 358 false); |
| 295 | 359 |
| 296 ExpectListenerDoesNotExist(presentation_url); | 360 ExpectListenerDoesNotExist(presentation_url); |
| 297 } | 361 } |
| 298 | 362 |
| 363 TEST_F(PresentationServiceImplTest, SetDefaultPresentationUrl) { |
| 364 std::string url1("http://fooUrl"); |
| 365 std::string dpu_id("dpuId"); |
| 366 EXPECT_CALL(mock_delegate_, |
| 367 SetDefaultPresentationUrl(_, _, Eq(url1), Eq(dpu_id))) |
| 368 .Times(1); |
| 369 service_impl_->SetDefaultPresentationUrl(url1, dpu_id); |
| 370 EXPECT_EQ(url1, service_impl_->default_presentation_url_); |
| 371 |
| 372 // Now there should be a callback registered with the DPU. |
| 373 GetScreenAvailabilityAndWait( |
| 374 url1, |
| 375 base::Bind( |
| 376 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback, |
| 377 base::Unretained(this), true), |
| 378 true); |
| 379 const auto& contexts = service_impl_->availability_contexts_; |
| 380 auto it = contexts.find(url1); |
| 381 ASSERT_TRUE(it != contexts.end()); |
| 382 EXPECT_NE(nullptr, it->second->callback()); |
| 383 |
| 384 std::string url2("http://barUrl"); |
| 385 // Sets different DPU. |
| 386 // Adds listener for url2 and removes listener for url1. |
| 387 // Also, the callback from url1 is transferred to url2. |
| 388 EXPECT_CALL( |
| 389 mock_delegate_, |
| 390 AddScreenAvailabilityListener(_, _, _)) |
| 391 .WillOnce(Return(true)); |
| 392 EXPECT_CALL( |
| 393 mock_delegate_, |
| 394 RemoveScreenAvailabilityListener(_, _, _)) |
| 395 .Times(1); |
| 396 EXPECT_CALL(mock_delegate_, |
| 397 SetDefaultPresentationUrl(_, _, Eq(url2), Eq(dpu_id))) |
| 398 .Times(1); |
| 399 service_impl_->SetDefaultPresentationUrl(url2, dpu_id); |
| 400 EXPECT_EQ(url2, service_impl_->default_presentation_url_); |
| 401 |
| 402 it = contexts.find(url2); |
| 403 ASSERT_TRUE(it != contexts.end()); |
| 404 EXPECT_NE(nullptr, it->second->callback()); |
| 405 } |
| 406 |
| 407 TEST_F(PresentationServiceImplTest, SetSameDefaultPresentationUrl) { |
| 408 std::string url("http://fooUrl"); |
| 409 std::string dpu_id("dpuId"); |
| 410 EXPECT_CALL(mock_delegate_, |
| 411 SetDefaultPresentationUrl(_, _, Eq(url), Eq(dpu_id))) |
| 412 .Times(1); |
| 413 service_impl_->SetDefaultPresentationUrl(url, dpu_id); |
| 414 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_delegate_)); |
| 415 EXPECT_EQ(url, service_impl_->default_presentation_url_); |
| 416 |
| 417 // Same URL as before; no-ops. |
| 418 service_impl_->SetDefaultPresentationUrl(url, dpu_id); |
| 419 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_delegate_)); |
| 420 EXPECT_EQ(url, service_impl_->default_presentation_url_); |
| 421 } |
| 422 |
| 423 TEST_F(PresentationServiceImplTest, ClearDefaultPresentationUrl) { |
| 424 std::string url("http://fooUrl"); |
| 425 std::string dpu_id("dpuId"); |
| 426 EXPECT_CALL(mock_delegate_, |
| 427 SetDefaultPresentationUrl(_, _, Eq(url), Eq(dpu_id))) |
| 428 .Times(1); |
| 429 service_impl_->SetDefaultPresentationUrl(url, dpu_id); |
| 430 EXPECT_EQ(url, service_impl_->default_presentation_url_); |
| 431 |
| 432 // Now there should be a callback registered with the DPU. |
| 433 GetScreenAvailabilityAndWait( |
| 434 url, |
| 435 base::Bind( |
| 436 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback, |
| 437 base::Unretained(this), true), |
| 438 true); |
| 439 |
| 440 const auto& contexts = service_impl_->availability_contexts_; |
| 441 auto it = contexts.find(url); |
| 442 ASSERT_TRUE(it != contexts.end()); |
| 443 EXPECT_NE(nullptr, it->second->callback()); |
| 444 |
| 445 // Clears the default presentation URL. Transfers the listener from url to |
| 446 // "1-UA" mode. |
| 447 EXPECT_CALL( |
| 448 mock_delegate_, |
| 449 AddScreenAvailabilityListener(_, _, _)) |
| 450 .WillOnce(Return(true)); |
| 451 EXPECT_CALL( |
| 452 mock_delegate_, |
| 453 RemoveScreenAvailabilityListener(_, _, _)) |
| 454 .Times(1); |
| 455 EXPECT_CALL( |
| 456 mock_delegate_, |
| 457 SetDefaultPresentationUrl(_, _, Eq(std::string()), Eq(std::string()))) |
| 458 .Times(1); |
| 459 service_impl_->SetDefaultPresentationUrl(std::string(), std::string()); |
| 460 EXPECT_TRUE(service_impl_->default_presentation_url_.empty()); |
| 461 |
| 462 it = contexts.find(url); |
| 463 ASSERT_TRUE(it == contexts.end()); |
| 464 } |
| 465 |
| 466 TEST_F(PresentationServiceImplTest, StartSessionSuccess) { |
| 467 std::string presentation_url("http://fooUrl"); |
| 468 std::string presentation_id("presentationId"); |
| 469 service_ptr_->StartSession( |
| 470 presentation_url, |
| 471 presentation_id, |
| 472 base::Bind( |
| 473 &PresentationServiceImplTest::ExpectNewSessionMojoCallbackSuccess, |
| 474 base::Unretained(this))); |
| 475 base::Callback<void(const PresentationSessionInfo&)> success_cb; |
| 476 EXPECT_CALL(mock_delegate_, StartSession( |
| 477 _, _, Eq(presentation_url), Eq(presentation_id), _, _)) |
| 478 .WillOnce(SaveArg<4>(&success_cb)); |
| 479 RunLoopUntilIdle(); |
| 480 success_cb.Run(PresentationSessionInfo(presentation_url, presentation_id)); |
| 481 SaveQuitClosureAndRunLoop(); |
| 482 } |
| 483 |
| 484 TEST_F(PresentationServiceImplTest, StartSessionError) { |
| 485 std::string presentation_url("http://fooUrl"); |
| 486 std::string presentation_id("presentationId"); |
| 487 service_ptr_->StartSession( |
| 488 presentation_url, |
| 489 presentation_id, |
| 490 base::Bind( |
| 491 &PresentationServiceImplTest::ExpectNewSessionMojoCallbackError, |
| 492 base::Unretained(this))); |
| 493 base::Callback<void(const PresentationError&)> error_cb; |
| 494 EXPECT_CALL(mock_delegate_, StartSession( |
| 495 _, _, Eq(presentation_url), Eq(presentation_id), _, _)) |
| 496 .WillOnce(SaveArg<5>(&error_cb)); |
| 497 RunLoopUntilIdle(); |
| 498 error_cb.Run(PresentationError(PRESENTATION_ERROR_UNKNOWN, "Error message")); |
| 499 SaveQuitClosureAndRunLoop(); |
| 500 } |
| 501 |
| 502 TEST_F(PresentationServiceImplTest, JoinSessionSuccess) { |
| 503 std::string presentation_url("http://fooUrl"); |
| 504 std::string presentation_id("presentationId"); |
| 505 service_ptr_->JoinSession( |
| 506 presentation_url, |
| 507 presentation_id, |
| 508 base::Bind( |
| 509 &PresentationServiceImplTest::ExpectNewSessionMojoCallbackSuccess, |
| 510 base::Unretained(this))); |
| 511 base::Callback<void(const PresentationSessionInfo&)> success_cb; |
| 512 EXPECT_CALL(mock_delegate_, JoinSession( |
| 513 _, _, Eq(presentation_url), Eq(presentation_id), _, _)) |
| 514 .WillOnce(SaveArg<4>(&success_cb)); |
| 515 RunLoopUntilIdle(); |
| 516 success_cb.Run(PresentationSessionInfo(presentation_url, presentation_id)); |
| 517 SaveQuitClosureAndRunLoop(); |
| 518 } |
| 519 |
| 520 TEST_F(PresentationServiceImplTest, JoinSessionError) { |
| 521 std::string presentation_url("http://fooUrl"); |
| 522 std::string presentation_id("presentationId"); |
| 523 service_ptr_->JoinSession( |
| 524 presentation_url, |
| 525 presentation_id, |
| 526 base::Bind( |
| 527 &PresentationServiceImplTest::ExpectNewSessionMojoCallbackError, |
| 528 base::Unretained(this))); |
| 529 base::Callback<void(const PresentationError&)> error_cb; |
| 530 EXPECT_CALL(mock_delegate_, JoinSession( |
| 531 _, _, Eq(presentation_url), Eq(presentation_id), _, _)) |
| 532 .WillOnce(SaveArg<5>(&error_cb)); |
| 533 RunLoopUntilIdle(); |
| 534 error_cb.Run(PresentationError(PRESENTATION_ERROR_UNKNOWN, "Error message")); |
| 535 SaveQuitClosureAndRunLoop(); |
| 536 } |
| 537 |
| 538 TEST_F(PresentationServiceImplTest, StartSessionInProgress) { |
| 539 std::string presentation_url1("http://fooUrl"); |
| 540 std::string presentation_id1("presentationId1"); |
| 541 std::string presentation_url2("http://barUrl"); |
| 542 std::string presentation_id2("presentationId2"); |
| 543 service_ptr_->StartSession( |
| 544 presentation_url1, |
| 545 presentation_id1, |
| 546 base::Bind( |
| 547 &PresentationServiceImplTest::ExpectNewSessionMojoCallbackSuccess, |
| 548 base::Unretained(this))); |
| 549 service_ptr_->StartSession( |
| 550 presentation_url2, |
| 551 presentation_id2, |
| 552 base::Bind( |
| 553 &PresentationServiceImplTest::ExpectNewSessionMojoCallbackSuccess, |
| 554 base::Unretained(this))); |
| 555 base::Callback<void(const PresentationSessionInfo&)> success_cb; |
| 556 EXPECT_CALL(mock_delegate_, StartSession( |
| 557 _, _, Eq(presentation_url1), Eq(presentation_id1), _, _)) |
| 558 .WillOnce(SaveArg<4>(&success_cb)); |
| 559 RunLoopUntilIdle(); |
| 560 |
| 561 // Running the callback means the first request is done. It should now |
| 562 // move on to the queued request. |
| 563 EXPECT_CALL(mock_delegate_, StartSession( |
| 564 _, _, Eq(presentation_url2), Eq(presentation_id2), _, _)) |
| 565 .Times(1); |
| 566 success_cb.Run(PresentationSessionInfo(presentation_url1, presentation_id1)); |
| 567 SaveQuitClosureAndRunLoop(); |
| 568 } |
| 569 |
| 299 } // namespace content | 570 } // namespace content |
| OLD | NEW |