| Index: content/browser/presentation/presentation_service_impl_unittest.cc
|
| diff --git a/content/browser/presentation/presentation_service_impl_unittest.cc b/content/browser/presentation/presentation_service_impl_unittest.cc
|
| index 89a6b3c779aa02419bb6b210c2d8f33c1e779ae4..aaa62b93655cd956ea04a1df80be56aa986c1743 100644
|
| --- a/content/browser/presentation/presentation_service_impl_unittest.cc
|
| +++ b/content/browser/presentation/presentation_service_impl_unittest.cc
|
| @@ -7,6 +7,7 @@
|
| #include "base/run_loop.h"
|
| #include "content/browser/presentation/presentation_service_impl.h"
|
| #include "content/public/browser/presentation_service_delegate.h"
|
| +#include "content/public/browser/presentation_session.h"
|
| #include "content/test/test_render_frame_host.h"
|
| #include "content/test/test_render_view_host.h"
|
| #include "content/test/test_web_contents.h"
|
| @@ -18,6 +19,7 @@ using ::testing::Eq;
|
| using ::testing::InvokeWithoutArgs;
|
| using ::testing::Mock;
|
| using ::testing::Return;
|
| +using ::testing::SaveArg;
|
|
|
| namespace content {
|
|
|
| @@ -41,6 +43,28 @@ class MockPresentationServiceDelegate : public PresentationServiceDelegate {
|
| void(
|
| int render_process_id,
|
| int routing_id));
|
| + MOCK_METHOD4(SetDefaultPresentationUrl,
|
| + void(
|
| + int render_process_id,
|
| + int routing_id,
|
| + const std::string& default_presentation_url,
|
| + const std::string& default_presentation_id));
|
| + MOCK_METHOD6(StartSession,
|
| + void(
|
| + int render_process_id,
|
| + int render_frame_id,
|
| + const std::string& presentation_url,
|
| + const std::string& presentation_id,
|
| + const PresentationSessionSuccessCallback& success_cb,
|
| + const PresentationSessionErrorCallback& error_cb));
|
| + MOCK_METHOD6(JoinSession,
|
| + void(
|
| + int render_process_id,
|
| + int render_frame_id,
|
| + const std::string& presentation_url,
|
| + const std::string& presentation_id,
|
| + const PresentationSessionSuccessCallback& success_cb,
|
| + const PresentationSessionErrorCallback& error_cb));
|
| };
|
|
|
| class PresentationServiceImplTest : public RenderViewHostImplTestHarness {
|
| @@ -69,9 +93,8 @@ class PresentationServiceImplTest : public RenderViewHostImplTestHarness {
|
|
|
| void GetScreenAvailabilityAndWait(
|
| const std::string& presentation_url,
|
| - const base::Callback<void(bool)>& callback,
|
| + const base::Callback<void(const std::string&, bool)>& callback,
|
| bool delegate_success) {
|
| - VLOG(1) << "GetScreenAvailabilityAndWait for " << presentation_url;
|
| base::RunLoop run_loop;
|
| // This will call to |service_impl_| via mojo. Process the message
|
| // using RunLoop.
|
| @@ -93,6 +116,10 @@ class PresentationServiceImplTest : public RenderViewHostImplTestHarness {
|
| EXPECT_TRUE(it == contexts.end());
|
| }
|
|
|
| + void RunLoopUntilIdle() {
|
| + base::RunLoop().RunUntilIdle();
|
| + }
|
| +
|
| void RunLoopFor(base::TimeDelta duration) {
|
| base::RunLoop run_loop;
|
| base::MessageLoop::current()->PostDelayedTask(
|
| @@ -107,8 +134,9 @@ class PresentationServiceImplTest : public RenderViewHostImplTestHarness {
|
| run_loop_quit_closure_.Reset();
|
| }
|
|
|
| - void ShouldNotBeCalled(bool available) {
|
| - FAIL() << "Callback unexpectedly invoked with available = " << available;
|
| + void ShouldNotBeCalled(const std::string& presentation_url, bool available) {
|
| + FAIL() << "Callback unexpectedly invoked with "
|
| + << "url = " << presentation_url << ", available = " << available;
|
| }
|
|
|
| void SimulateScreenAvailabilityChange(
|
| @@ -119,13 +147,49 @@ class PresentationServiceImplTest : public RenderViewHostImplTestHarness {
|
| it->second->OnScreenAvailabilityChanged(available);
|
| }
|
|
|
| - void ScreenAvailabilityChangedCallback(bool expected, bool available) {
|
| + void ScreenAvailabilityChangedCallback(
|
| + bool expected,
|
| + const std::string& presentation_url,
|
| + bool available) {
|
| ++callback_count_;
|
| EXPECT_EQ(expected, available);
|
| if (!run_loop_quit_closure_.is_null())
|
| run_loop_quit_closure_.Run();
|
| }
|
|
|
| + void ExpectReset() {
|
| + EXPECT_CALL(mock_delegate_, RemoveAllScreenAvailabilityListeners(_, _))
|
| + .Times(1);
|
| + EXPECT_CALL(mock_delegate_, SetDefaultPresentationUrl(_, _, Eq(""), Eq("")))
|
| + .Times(1);
|
| + }
|
| +
|
| + void ExpectCleanState() {
|
| + EXPECT_TRUE(service_impl_->availability_contexts_.empty());
|
| + EXPECT_TRUE(service_impl_->default_presentation_url_.empty());
|
| + EXPECT_TRUE(service_impl_->default_presentation_id_.empty());
|
| + EXPECT_FALSE(service_impl_->start_session_in_progress_);
|
| + EXPECT_TRUE(service_impl_->queued_start_session_requests_.empty());
|
| + }
|
| +
|
| + void ExpectNewSessionMojoCallbackSuccess(
|
| + presentation::PresentationSessionInfoPtr info,
|
| + presentation::PresentationErrorPtr error) {
|
| + EXPECT_FALSE(info.is_null());
|
| + EXPECT_TRUE(error.is_null());
|
| + if (!run_loop_quit_closure_.is_null())
|
| + run_loop_quit_closure_.Run();
|
| + }
|
| +
|
| + void ExpectNewSessionMojoCallbackError(
|
| + presentation::PresentationSessionInfoPtr info,
|
| + presentation::PresentationErrorPtr error) {
|
| + EXPECT_TRUE(info.is_null());
|
| + EXPECT_FALSE(error.is_null());
|
| + if (!run_loop_quit_closure_.is_null())
|
| + run_loop_quit_closure_.Run();
|
| + }
|
| +
|
| MockPresentationServiceDelegate mock_delegate_;
|
| scoped_ptr<PresentationServiceImpl> service_impl_;
|
| mojo::InterfacePtr<presentation::PresentationService> service_ptr_;
|
| @@ -170,7 +234,7 @@ TEST_F(PresentationServiceImplTest, GetScreenAvailability) {
|
| EXPECT_EQ(2, callback_count_);
|
| }
|
|
|
| -TEST_F(PresentationServiceImplTest, RemoveAllListeners) {
|
| +TEST_F(PresentationServiceImplTest, Reset) {
|
| std::string presentation_url("http://fooUrl");
|
| GetScreenAvailabilityAndWait(
|
| presentation_url,
|
| @@ -178,9 +242,9 @@ TEST_F(PresentationServiceImplTest, RemoveAllListeners) {
|
| base::Unretained(this)),
|
| true);
|
|
|
| - service_impl_->RemoveAllListeners();
|
| -
|
| - ExpectListenerDoesNotExist(presentation_url);
|
| + ExpectReset();
|
| + service_impl_->Reset();
|
| + ExpectCleanState();
|
|
|
| EXPECT_EQ(0, callback_count_);
|
| }
|
| @@ -193,12 +257,12 @@ TEST_F(PresentationServiceImplTest, DidNavigateThisFrame) {
|
| base::Unretained(this)),
|
| true);
|
|
|
| + ExpectReset();
|
| service_impl_->DidNavigateAnyFrame(
|
| contents()->GetMainFrame(),
|
| content::LoadCommittedDetails(),
|
| content::FrameNavigateParams());
|
| -
|
| - ExpectListenerDoesNotExist(presentation_url);
|
| + ExpectCleanState();
|
| }
|
|
|
| TEST_F(PresentationServiceImplTest, DidNavigateNotThisFrame) {
|
| @@ -232,9 +296,9 @@ TEST_F(PresentationServiceImplTest, ThisRenderFrameDeleted) {
|
| base::Unretained(this)),
|
| true);
|
|
|
| + ExpectReset();
|
| service_impl_->RenderFrameDeleted(contents()->GetMainFrame());
|
| -
|
| - ExpectListenerDoesNotExist(presentation_url);
|
| + ExpectCleanState();
|
| }
|
|
|
| TEST_F(PresentationServiceImplTest, NotThisRenderFrameDeleted) {
|
| @@ -296,4 +360,211 @@ TEST_F(PresentationServiceImplTest, DelegateFails) {
|
| ExpectListenerDoesNotExist(presentation_url);
|
| }
|
|
|
| +TEST_F(PresentationServiceImplTest, SetDefaultPresentationUrl) {
|
| + std::string url1("http://fooUrl");
|
| + std::string dpu_id("dpuId");
|
| + EXPECT_CALL(mock_delegate_,
|
| + SetDefaultPresentationUrl(_, _, Eq(url1), Eq(dpu_id)))
|
| + .Times(1);
|
| + service_impl_->SetDefaultPresentationUrl(url1, dpu_id);
|
| + EXPECT_EQ(url1, service_impl_->default_presentation_url_);
|
| +
|
| + // Now there should be a callback registered with the DPU.
|
| + GetScreenAvailabilityAndWait(
|
| + url1,
|
| + base::Bind(
|
| + &PresentationServiceImplTest::ScreenAvailabilityChangedCallback,
|
| + base::Unretained(this), true),
|
| + true);
|
| + const auto& contexts = service_impl_->availability_contexts_;
|
| + auto it = contexts.find(url1);
|
| + ASSERT_TRUE(it != contexts.end());
|
| + EXPECT_NE(nullptr, it->second->callback());
|
| +
|
| + std::string url2("http://barUrl");
|
| + // Sets different DPU.
|
| + // Adds listener for url2 and removes listener for url1.
|
| + // Also, the callback from url1 is transferred to url2.
|
| + EXPECT_CALL(
|
| + mock_delegate_,
|
| + AddScreenAvailabilityListener(_, _, _))
|
| + .WillOnce(Return(true));
|
| + EXPECT_CALL(
|
| + mock_delegate_,
|
| + RemoveScreenAvailabilityListener(_, _, _))
|
| + .Times(1);
|
| + EXPECT_CALL(mock_delegate_,
|
| + SetDefaultPresentationUrl(_, _, Eq(url2), Eq(dpu_id)))
|
| + .Times(1);
|
| + service_impl_->SetDefaultPresentationUrl(url2, dpu_id);
|
| + EXPECT_EQ(url2, service_impl_->default_presentation_url_);
|
| +
|
| + it = contexts.find(url2);
|
| + ASSERT_TRUE(it != contexts.end());
|
| + EXPECT_NE(nullptr, it->second->callback());
|
| +}
|
| +
|
| +TEST_F(PresentationServiceImplTest, SetSameDefaultPresentationUrl) {
|
| + std::string url("http://fooUrl");
|
| + std::string dpu_id("dpuId");
|
| + EXPECT_CALL(mock_delegate_,
|
| + SetDefaultPresentationUrl(_, _, Eq(url), Eq(dpu_id)))
|
| + .Times(1);
|
| + service_impl_->SetDefaultPresentationUrl(url, dpu_id);
|
| + EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_delegate_));
|
| + EXPECT_EQ(url, service_impl_->default_presentation_url_);
|
| +
|
| + // Same URL as before; no-ops.
|
| + service_impl_->SetDefaultPresentationUrl(url, dpu_id);
|
| + EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_delegate_));
|
| + EXPECT_EQ(url, service_impl_->default_presentation_url_);
|
| +}
|
| +
|
| +TEST_F(PresentationServiceImplTest, ClearDefaultPresentationUrl) {
|
| + std::string url("http://fooUrl");
|
| + std::string dpu_id("dpuId");
|
| + EXPECT_CALL(mock_delegate_,
|
| + SetDefaultPresentationUrl(_, _, Eq(url), Eq(dpu_id)))
|
| + .Times(1);
|
| + service_impl_->SetDefaultPresentationUrl(url, dpu_id);
|
| + EXPECT_EQ(url, service_impl_->default_presentation_url_);
|
| +
|
| + // Now there should be a callback registered with the DPU.
|
| + GetScreenAvailabilityAndWait(
|
| + url,
|
| + base::Bind(
|
| + &PresentationServiceImplTest::ScreenAvailabilityChangedCallback,
|
| + base::Unretained(this), true),
|
| + true);
|
| +
|
| + const auto& contexts = service_impl_->availability_contexts_;
|
| + auto it = contexts.find(url);
|
| + ASSERT_TRUE(it != contexts.end());
|
| + EXPECT_NE(nullptr, it->second->callback());
|
| +
|
| + // Clears the default presentation URL. Transfers the listener from url to
|
| + // "1-UA" mode.
|
| + EXPECT_CALL(
|
| + mock_delegate_,
|
| + AddScreenAvailabilityListener(_, _, _))
|
| + .WillOnce(Return(true));
|
| + EXPECT_CALL(
|
| + mock_delegate_,
|
| + RemoveScreenAvailabilityListener(_, _, _))
|
| + .Times(1);
|
| + EXPECT_CALL(
|
| + mock_delegate_,
|
| + SetDefaultPresentationUrl(_, _, Eq(std::string()), Eq(std::string())))
|
| + .Times(1);
|
| + service_impl_->SetDefaultPresentationUrl(std::string(), std::string());
|
| + EXPECT_TRUE(service_impl_->default_presentation_url_.empty());
|
| +
|
| + it = contexts.find(url);
|
| + ASSERT_TRUE(it == contexts.end());
|
| +}
|
| +
|
| +TEST_F(PresentationServiceImplTest, StartSessionSuccess) {
|
| + std::string presentation_url("http://fooUrl");
|
| + std::string presentation_id("presentationId");
|
| + service_ptr_->StartSession(
|
| + presentation_url,
|
| + presentation_id,
|
| + base::Bind(
|
| + &PresentationServiceImplTest::ExpectNewSessionMojoCallbackSuccess,
|
| + base::Unretained(this)));
|
| + base::Callback<void(const PresentationSessionInfo&)> success_cb;
|
| + EXPECT_CALL(mock_delegate_, StartSession(
|
| + _, _, Eq(presentation_url), Eq(presentation_id), _, _))
|
| + .WillOnce(SaveArg<4>(&success_cb));
|
| + RunLoopUntilIdle();
|
| + success_cb.Run(PresentationSessionInfo(presentation_url, presentation_id));
|
| + SaveQuitClosureAndRunLoop();
|
| +}
|
| +
|
| +TEST_F(PresentationServiceImplTest, StartSessionError) {
|
| + std::string presentation_url("http://fooUrl");
|
| + std::string presentation_id("presentationId");
|
| + service_ptr_->StartSession(
|
| + presentation_url,
|
| + presentation_id,
|
| + base::Bind(
|
| + &PresentationServiceImplTest::ExpectNewSessionMojoCallbackError,
|
| + base::Unretained(this)));
|
| + base::Callback<void(const PresentationError&)> error_cb;
|
| + EXPECT_CALL(mock_delegate_, StartSession(
|
| + _, _, Eq(presentation_url), Eq(presentation_id), _, _))
|
| + .WillOnce(SaveArg<5>(&error_cb));
|
| + RunLoopUntilIdle();
|
| + error_cb.Run(PresentationError(PRESENTATION_ERROR_UNKNOWN, "Error message"));
|
| + SaveQuitClosureAndRunLoop();
|
| +}
|
| +
|
| +TEST_F(PresentationServiceImplTest, JoinSessionSuccess) {
|
| + std::string presentation_url("http://fooUrl");
|
| + std::string presentation_id("presentationId");
|
| + service_ptr_->JoinSession(
|
| + presentation_url,
|
| + presentation_id,
|
| + base::Bind(
|
| + &PresentationServiceImplTest::ExpectNewSessionMojoCallbackSuccess,
|
| + base::Unretained(this)));
|
| + base::Callback<void(const PresentationSessionInfo&)> success_cb;
|
| + EXPECT_CALL(mock_delegate_, JoinSession(
|
| + _, _, Eq(presentation_url), Eq(presentation_id), _, _))
|
| + .WillOnce(SaveArg<4>(&success_cb));
|
| + RunLoopUntilIdle();
|
| + success_cb.Run(PresentationSessionInfo(presentation_url, presentation_id));
|
| + SaveQuitClosureAndRunLoop();
|
| +}
|
| +
|
| +TEST_F(PresentationServiceImplTest, JoinSessionError) {
|
| + std::string presentation_url("http://fooUrl");
|
| + std::string presentation_id("presentationId");
|
| + service_ptr_->JoinSession(
|
| + presentation_url,
|
| + presentation_id,
|
| + base::Bind(
|
| + &PresentationServiceImplTest::ExpectNewSessionMojoCallbackError,
|
| + base::Unretained(this)));
|
| + base::Callback<void(const PresentationError&)> error_cb;
|
| + EXPECT_CALL(mock_delegate_, JoinSession(
|
| + _, _, Eq(presentation_url), Eq(presentation_id), _, _))
|
| + .WillOnce(SaveArg<5>(&error_cb));
|
| + RunLoopUntilIdle();
|
| + error_cb.Run(PresentationError(PRESENTATION_ERROR_UNKNOWN, "Error message"));
|
| + SaveQuitClosureAndRunLoop();
|
| +}
|
| +
|
| +TEST_F(PresentationServiceImplTest, StartSessionInProgress) {
|
| + std::string presentation_url1("http://fooUrl");
|
| + std::string presentation_id1("presentationId1");
|
| + std::string presentation_url2("http://barUrl");
|
| + std::string presentation_id2("presentationId2");
|
| + service_ptr_->StartSession(
|
| + presentation_url1,
|
| + presentation_id1,
|
| + base::Bind(
|
| + &PresentationServiceImplTest::ExpectNewSessionMojoCallbackSuccess,
|
| + base::Unretained(this)));
|
| + service_ptr_->StartSession(
|
| + presentation_url2,
|
| + presentation_id2,
|
| + base::Bind(
|
| + &PresentationServiceImplTest::ExpectNewSessionMojoCallbackSuccess,
|
| + base::Unretained(this)));
|
| + base::Callback<void(const PresentationSessionInfo&)> success_cb;
|
| + EXPECT_CALL(mock_delegate_, StartSession(
|
| + _, _, Eq(presentation_url1), Eq(presentation_id1), _, _))
|
| + .WillOnce(SaveArg<4>(&success_cb));
|
| + RunLoopUntilIdle();
|
| +
|
| + // Running the callback means the first request is done. It should now
|
| + // move on to the queued request.
|
| + EXPECT_CALL(mock_delegate_, StartSession(
|
| + _, _, Eq(presentation_url2), Eq(presentation_id2), _, _))
|
| + .Times(1);
|
| + success_cb.Run(PresentationSessionInfo(presentation_url1, presentation_id1));
|
| + SaveQuitClosureAndRunLoop();
|
| +}
|
| +
|
| } // namespace content
|
|
|