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

Unified Diff: content/renderer/screen_orientation/screen_orientation_dispatcher_unittest.cc

Issue 2391883006: Mojo-ify implementation of screen orientation locking/unlocking. (Closed)
Patch Set: Moved enum mojo definition to content/common due to dependency issue Created 4 years, 2 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/renderer/screen_orientation/screen_orientation_dispatcher_unittest.cc
diff --git a/content/renderer/screen_orientation/screen_orientation_dispatcher_unittest.cc b/content/renderer/screen_orientation/screen_orientation_dispatcher_unittest.cc
index 58ca079dd0faebdf5f977e958f26e569d3e46050..6ff9f01f4462b769701f22ad55ff31bd79047d02 100644
--- a/content/renderer/screen_orientation/screen_orientation_dispatcher_unittest.cc
+++ b/content/renderer/screen_orientation/screen_orientation_dispatcher_unittest.cc
@@ -9,14 +9,16 @@
#include <tuple>
#include "base/logging.h"
-#include "content/common/screen_orientation_messages.h"
#include "content/public/test/test_utils.h"
-#include "ipc/ipc_test_sink.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/WebKit/public/platform/modules/screen_orientation/WebLockOrientationCallback.h"
namespace content {
+using LockOrientationCallback =
+ ScreenOrientationService::LockOrientationCallback;
+using LockResult = mojom::ScreenOrientationLockResult;
+
// MockLockOrientationCallback is an implementation of
// WebLockOrientationCallback and takes a LockOrientationResultHolder* as a
// parameter when being constructed. The |results_| pointer is owned by the
@@ -51,35 +53,53 @@ class MockLockOrientationCallback : public blink::WebLockOrientationCallback {
LockOrientationResultHolder* results_;
};
-class ScreenOrientationDispatcherWithSink : public ScreenOrientationDispatcher {
+// MockScreenOrientationService is an implementation of
+// ScreenOrientationService and provides LockOrientation() and
+// UnlockOrientation() services called by ScreenOrientationDispatcher.
+// ScreenOrientationLockResult (either SUCCESS or some ERROR) will be returned
+// as a callback message to the caller.
+class MockScreenOrientationService : public ScreenOrientationService {
public:
- explicit ScreenOrientationDispatcherWithSink(IPC::TestSink* sink)
- :ScreenOrientationDispatcher(NULL) , sink_(sink) {
- }
+ MockScreenOrientationService() {}
+ ~MockScreenOrientationService() override {}
+
+ // ScreenOrientationService:
+ void LockOrientation(blink::WebScreenOrientationLockType orientation,
+ const LockOrientationCallback& callback) override;
+ void UnlockOrientation() override;
- bool Send(IPC::Message* message) override { return sink_->Send(message); }
+ void RunLockResultCallback(LockResult result);
+ const LockOrientationCallback on_result_callback() {
+ return on_result_callback_;
+ }
- IPC::TestSink* sink_;
+ private:
+ LockOrientationCallback on_result_callback_;
};
-class ScreenOrientationDispatcherTest : public testing::Test {
- protected:
- void SetUp() override {
- dispatcher_.reset(new ScreenOrientationDispatcherWithSink(&sink_));
- }
+void MockScreenOrientationService::LockOrientation(
+ blink::WebScreenOrientationLockType orientation,
+ const LockOrientationCallback& callback) {
+ on_result_callback_ = callback;
+}
- int GetFirstLockRequestIdFromSink() {
- const IPC::Message* msg = sink().GetFirstMessageMatching(
- ScreenOrientationHostMsg_LockRequest::ID);
- EXPECT_TRUE(msg != NULL);
+void MockScreenOrientationService::UnlockOrientation() {
+ // Cancel any pending lock request.
+ RunLockResultCallback(
+ LockResult::SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED);
+}
- std::tuple<blink::WebScreenOrientationLockType, int> params;
- ScreenOrientationHostMsg_LockRequest::Read(msg, &params);
- return std::get<1>(params);
- }
+void MockScreenOrientationService::RunLockResultCallback(LockResult result) {
+ DCHECK(!on_result_callback_.is_null());
+ on_result_callback_.Run(result);
+ on_result_callback_ = LockOrientationCallback();
+}
- IPC::TestSink& sink() {
- return sink_;
+class ScreenOrientationDispatcherTest : public testing::Test {
+ protected:
+ void SetUp() override {
+ dispatcher_.reset(
+ new ScreenOrientationDispatcher(&screen_orientation_service_));
}
void LockOrientation(blink::WebScreenOrientationLockType orientation,
@@ -91,16 +111,7 @@ class ScreenOrientationDispatcherTest : public testing::Test {
dispatcher_->unlockOrientation();
}
- void OnMessageReceived(const IPC::Message& message) {
- dispatcher_->OnMessageReceived(message);
- }
-
- int routing_id() const {
- // We return a fake routing_id() in the context of this test.
- return 0;
- }
-
- IPC::TestSink sink_;
+ MockScreenOrientationService screen_orientation_service_;
std::unique_ptr<ScreenOrientationDispatcher> dispatcher_;
};
@@ -125,6 +136,7 @@ TEST_F(ScreenOrientationDispatcherTest, CancelPending_DoubleLock) {
LockOrientation(blink::WebScreenOrientationLockPortraitPrimary,
new MockLockOrientationCallback(&callback_results));
+
LockOrientation(blink::WebScreenOrientationLockPortraitPrimary,
new MockLockOrientationCallback(&callback_results2));
@@ -136,27 +148,25 @@ TEST_F(ScreenOrientationDispatcherTest, CancelPending_DoubleLock) {
// Test that when a LockError message is received, the request is set as failed
// with the correct values.
TEST_F(ScreenOrientationDispatcherTest, LockRequest_Error) {
- std::list<blink::WebLockOrientationError> errors;
- errors.push_back(blink::WebLockOrientationErrorNotAvailable);
- errors.push_back(
- blink::WebLockOrientationErrorFullscreenRequired);
- errors.push_back(blink::WebLockOrientationErrorCanceled);
-
- for (std::list<blink::WebLockOrientationError>::const_iterator
- it = errors.begin(); it != errors.end(); ++it) {
+ std::map<LockResult, blink::WebLockOrientationError> errors;
+ errors[LockResult::SCREEN_ORIENTATION_LOCK_RESULT_ERROR_NOT_AVAILABLE] =
+ blink::WebLockOrientationErrorNotAvailable;
+ errors[LockResult::SCREEN_ORIENTATION_LOCK_RESULT_ERROR_FULLSCREEN_REQUIRED] =
+ blink::WebLockOrientationErrorFullscreenRequired;
+ errors[LockResult::SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED] =
+ blink::WebLockOrientationErrorCanceled;
+
+ for (std::map<LockResult, blink::WebLockOrientationError>::const_iterator it =
+ errors.begin();
+ it != errors.end(); ++it) {
MockLockOrientationCallback::LockOrientationResultHolder callback_results;
LockOrientation(blink::WebScreenOrientationLockPortraitPrimary,
new MockLockOrientationCallback(&callback_results));
-
- int request_id = GetFirstLockRequestIdFromSink();
- OnMessageReceived(
- ScreenOrientationMsg_LockError(routing_id(), request_id, *it));
+ screen_orientation_service_.RunLockResultCallback(it->first);
EXPECT_FALSE(callback_results.succeeded_);
EXPECT_TRUE(callback_results.failed_);
- EXPECT_EQ(*it, callback_results.error_);
-
- sink().ClearMessages();
+ EXPECT_EQ(it->second, callback_results.error_);
}
}
@@ -167,44 +177,11 @@ TEST_F(ScreenOrientationDispatcherTest, LockRequest_Success) {
LockOrientation(blink::WebScreenOrientationLockPortraitPrimary,
new MockLockOrientationCallback(&callback_results));
- int request_id = GetFirstLockRequestIdFromSink();
- OnMessageReceived(ScreenOrientationMsg_LockSuccess(routing_id(),
- request_id));
+ screen_orientation_service_.RunLockResultCallback(
+ LockResult::SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS);
EXPECT_TRUE(callback_results.succeeded_);
EXPECT_FALSE(callback_results.failed_);
-
- sink().ClearMessages();
-}
-
-// Test an edge case: a LockSuccess is received but it matches no pending
-// callback.
-TEST_F(ScreenOrientationDispatcherTest, SuccessForUnknownRequest) {
- MockLockOrientationCallback::LockOrientationResultHolder callback_results;
- LockOrientation(blink::WebScreenOrientationLockPortraitPrimary,
- new MockLockOrientationCallback(&callback_results));
-
- int request_id = GetFirstLockRequestIdFromSink();
- OnMessageReceived(ScreenOrientationMsg_LockSuccess(routing_id(),
- request_id + 1));
-
- EXPECT_FALSE(callback_results.succeeded_);
- EXPECT_FALSE(callback_results.failed_);
-}
-
-// Test an edge case: a LockError is received but it matches no pending
-// callback.
-TEST_F(ScreenOrientationDispatcherTest, ErrorForUnknownRequest) {
- MockLockOrientationCallback::LockOrientationResultHolder callback_results;
- LockOrientation(blink::WebScreenOrientationLockPortraitPrimary,
- new MockLockOrientationCallback(&callback_results));
-
- int request_id = GetFirstLockRequestIdFromSink();
- OnMessageReceived(ScreenOrientationMsg_LockError(
- routing_id(), request_id + 1, blink::WebLockOrientationErrorCanceled));
-
- EXPECT_FALSE(callback_results.succeeded_);
- EXPECT_FALSE(callback_results.failed_);
}
// Test the following scenario:
@@ -219,15 +196,14 @@ TEST_F(ScreenOrientationDispatcherTest, RaceScenario) {
LockOrientation(blink::WebScreenOrientationLockPortraitPrimary,
new MockLockOrientationCallback(&callback_results1));
- int request_id1 = GetFirstLockRequestIdFromSink();
+ LockOrientationCallback service_callback1 =
+ screen_orientation_service_.on_result_callback();
LockOrientation(blink::WebScreenOrientationLockLandscapePrimary,
new MockLockOrientationCallback(&callback_results2));
// callback_results1 must be rejected, tested in CancelPending_DoubleLock.
-
- OnMessageReceived(ScreenOrientationMsg_LockSuccess(routing_id(),
- request_id1));
+ service_callback1.Run(LockResult::SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS);
// First request is still rejected.
EXPECT_FALSE(callback_results1.succeeded_);

Powered by Google App Engine
This is Rietveld 408576698