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

Unified 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: rm presentation_session.mojom 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 side-by-side diff with in-line comments
Download patch
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..7068f561c7871cdae3972aec1bd2c1b432992f2e 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 {
@@ -37,10 +39,32 @@ class MockPresentationServiceDelegate : public PresentationServiceDelegate {
int render_process_id,
int routing_id,
PresentationScreenAvailabilityListener* listener));
- MOCK_METHOD2(RemoveAllScreenAvailabilityListeners,
+ MOCK_METHOD2(Reset,
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.
@@ -107,8 +130,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 +143,46 @@ 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_, Reset(_, _))
+ .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_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 +227,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 +235,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 +250,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 +289,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 +353,226 @@ 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->GetCallback());
+
+ 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->GetCallback());
+}
+
+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->GetCallback());
+
+ // 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::RunLoop run_loop;
+ base::Callback<void(const PresentationSessionInfo&)> success_cb;
+ EXPECT_CALL(mock_delegate_, StartSession(
+ _, _, Eq(presentation_url), Eq(presentation_id), _, _))
+ .WillOnce(DoAll(
+ InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit),
+ SaveArg<4>(&success_cb)));
+ run_loop.Run();
+ 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::RunLoop run_loop;
+ base::Callback<void(const PresentationError&)> error_cb;
+ EXPECT_CALL(mock_delegate_, StartSession(
+ _, _, Eq(presentation_url), Eq(presentation_id), _, _))
+ .WillOnce(DoAll(
+ InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit),
+ SaveArg<5>(&error_cb)));
+ run_loop.Run();
+ 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::RunLoop run_loop;
+ base::Callback<void(const PresentationSessionInfo&)> success_cb;
+ EXPECT_CALL(mock_delegate_, JoinSession(
+ _, _, Eq(presentation_url), Eq(presentation_id), _, _))
+ .WillOnce(DoAll(
+ InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit),
+ SaveArg<4>(&success_cb)));
+ run_loop.Run();
+ 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::RunLoop run_loop;
+ base::Callback<void(const PresentationError&)> error_cb;
+ EXPECT_CALL(mock_delegate_, JoinSession(
+ _, _, Eq(presentation_url), Eq(presentation_id), _, _))
+ .WillOnce(DoAll(
+ InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit),
+ SaveArg<5>(&error_cb)));
+ run_loop.Run();
+ 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::RunLoop run_loop;
+ base::Callback<void(const PresentationSessionInfo&)> success_cb;
+ EXPECT_CALL(mock_delegate_, StartSession(
+ _, _, Eq(presentation_url1), Eq(presentation_id1), _, _))
+ .WillOnce(DoAll(
+ InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit),
+ SaveArg<4>(&success_cb)));
+ run_loop.Run();
+
+ // 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
« no previous file with comments | « content/browser/presentation/presentation_service_impl.cc ('k') | content/browser/presentation/presentation_type_converters.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698