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

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

Issue 549603003: Create Mojo service for locking/unlocking screen orientation. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Nits Created 6 years, 3 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 9eb03c699042dc3f2c7e054bb50f2aae0335fa0c..5f14b2df42a9c65420977af833491f58f49b9d56 100644
--- a/content/renderer/screen_orientation/screen_orientation_dispatcher_unittest.cc
+++ b/content/renderer/screen_orientation/screen_orientation_dispatcher_unittest.cc
@@ -8,9 +8,7 @@
#include "base/logging.h"
#include "base/memory/scoped_ptr.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/WebLockOrientationCallback.h"
@@ -53,38 +51,58 @@ class MockLockOrientationCallback :
LockOrientationResultHolder* results_;
};
-class ScreenOrientationDispatcherWithSink : public ScreenOrientationDispatcher {
+class MockScreenOrientationService : public ScreenOrientationService {
public:
- explicit ScreenOrientationDispatcherWithSink(IPC::TestSink* sink)
- :ScreenOrientationDispatcher(NULL) , sink_(sink) {
+ MockScreenOrientationService();
+ virtual ~MockScreenOrientationService();
+
+ // ScreenOrientationService:
+ virtual void LockOrientation(
+ ScreenOrientationLockType lock_type,
+ const mojo::Callback<void(ScreenOrientationLockResult)>& callback)
+ OVERRIDE;
+ virtual void UnlockOrientation() OVERRIDE;
+
+ void RunLockResultCallback(ScreenOrientationLockResult result_type);
+ const mojo::Callback<void(ScreenOrientationLockResult)>&
+ on_result_callback() {
+ return on_result_callback_;
}
- virtual bool Send(IPC::Message* message) OVERRIDE {
- return sink_->Send(message);
- }
-
- IPC::TestSink* sink_;
+ private:
+ mojo::Callback<void(ScreenOrientationLockResult)> on_result_callback_;
};
+MockScreenOrientationService::MockScreenOrientationService() {
+}
+
+MockScreenOrientationService::~MockScreenOrientationService() {
+}
+
+void MockScreenOrientationService::LockOrientation(
+ ScreenOrientationLockType lock_type,
+ const mojo::Callback<void(ScreenOrientationLockResult)>& callback) {
+ on_result_callback_ = callback;
+}
+
+void MockScreenOrientationService::UnlockOrientation() {
+}
+
+void MockScreenOrientationService::RunLockResultCallback(
+ ScreenOrientationLockResult result_type) {
+ DCHECK(!on_result_callback_.is_null());
+ on_result_callback_.Run(result_type);
+ on_result_callback_ = mojo::Callback<void(ScreenOrientationLockResult)>();
+}
+
class ScreenOrientationDispatcherTest : public testing::Test {
protected:
virtual void SetUp() OVERRIDE {
- dispatcher_.reset(new ScreenOrientationDispatcherWithSink(&sink_));
- }
-
- int GetFirstLockRequestIdFromSink() {
- const IPC::Message* msg = sink().GetFirstMessageMatching(
- ScreenOrientationHostMsg_LockRequest::ID);
- EXPECT_TRUE(msg != NULL);
-
- Tuple2<blink::WebScreenOrientationLockType,int> params;
- ScreenOrientationHostMsg_LockRequest::Read(msg, &params);
- return params.b;
+ dispatcher_.reset(
+ new ScreenOrientationDispatcher(&screen_orientation_service_));
}
- IPC::TestSink& sink() {
- return sink_;
- }
+ virtual void TearDown() OVERRIDE {}
void LockOrientation(blink::WebScreenOrientationLockType orientation,
blink::WebLockOrientationCallback* callback) {
@@ -95,16 +113,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_;
scoped_ptr<ScreenOrientationDispatcher> dispatcher_;
};
@@ -137,30 +146,30 @@ TEST_F(ScreenOrientationDispatcherTest, CancelPending_DoubleLock) {
EXPECT_EQ(blink::WebLockOrientationErrorCanceled, callback_results.error_);
}
-// Test that when a LockError message is received, the request is set as failed
-// with the correct values.
+// 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<ScreenOrientationLockResult, blink::WebLockOrientationError> errors;
+ errors[SCREEN_ORIENTATION_LOCK_RESULT_ERROR_NOT_AVAILABLE] =
+ blink::WebLockOrientationErrorNotAvailable;
+ errors[SCREEN_ORIENTATION_LOCK_RESULT_ERROR_FULLSCREEN_REQUIRED] =
+ blink::WebLockOrientationErrorFullScreenRequired;
+ errors[SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED] =
+ blink::WebLockOrientationErrorCanceled;
+
+ for (std::map<ScreenOrientationLockResult,
+ 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_);
}
}
@@ -171,44 +180,10 @@ 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(
+ 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:
@@ -223,15 +198,15 @@ TEST_F(ScreenOrientationDispatcherTest, RaceScenario) {
LockOrientation(blink::WebScreenOrientationLockPortraitPrimary,
new MockLockOrientationCallback(&callback_results1));
- int request_id1 = GetFirstLockRequestIdFromSink();
+ mojo::Callback<void(ScreenOrientationLockResult)> 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(SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS);
// First request is still rejected.
EXPECT_FALSE(callback_results1.succeeded_);

Powered by Google App Engine
This is Rietveld 408576698