Index: content/renderer/mouse_lock_dispatcher_unittest.cc |
diff --git a/content/renderer/mouse_lock_dispatcher_unittest.cc b/content/renderer/mouse_lock_dispatcher_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..d93a28df8a289ddc7a38d188d390f721a6afceb8 |
--- /dev/null |
+++ b/content/renderer/mouse_lock_dispatcher_unittest.cc |
@@ -0,0 +1,235 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <string> |
+ |
+#include "content/common/view_messages.h" |
+#include "content/renderer/render_view_impl.h" |
+#include "content/test/render_view_test.h" |
+#include "mouse_lock_dispatcher.h" |
yzshen1
2012/01/24 18:56:08
Please add path.
scheib
2012/01/25 00:27:11
Done.
|
+#include "testing/gmock/include/gmock/gmock.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+using ::testing::_; |
+ |
+namespace { |
+ |
+class MockLockTarget : public MouseLockDispatcher::LockTarget { |
+ public: |
+ MOCK_METHOD1(OnLockMouseACK, void(bool)); |
+ MOCK_METHOD0(OnMouseLockLost, void()); |
+ MOCK_METHOD1(HandleMouseLockedInputEvent, |
+ bool(const WebKit::WebMouseEvent&)); |
+}; |
+ |
+// MouseLockDispatcher is a RenderViewObserver, and we test it by creating a |
+// fixture containing a RenderViewImpl view() and interacting to that interface. |
+class MouseLockDispatcherTest |
+ : public content::RenderViewTest { |
+ public: |
+ virtual void SetUp() { |
+ content::RenderViewTest::SetUp(); |
+ route_id_ = view()->GetRoutingId(); |
+ target_.reset(new MockLockTarget()); |
+ alternate_target_.reset(new MockLockTarget()); |
+ } |
+ |
+ virtual void TearDown() { |
+ content::RenderViewTest::TearDown(); |
+ target_.reset(NULL); |
+ alternate_target_.reset(NULL); |
+ } |
+ |
+ RenderViewImpl* view() { return static_cast<RenderViewImpl*>(view_); } |
+ MouseLockDispatcher* dispatcher() { return view()->mouse_lock_dispatcher(); } |
+ int route_id_; |
piman
2012/01/24 02:27:37
Typically we make the members protected, they shou
scheib
2012/01/25 00:27:11
Done.
|
+ scoped_ptr<MockLockTarget> target_; |
+ scoped_ptr<MockLockTarget> alternate_target_; |
+}; |
+ |
+} // namespace |
+ |
+// Test simple use of RenderViewImpl interface to WebKit for pointer lock. |
+TEST_F(MouseLockDispatcherTest, BasicWebWidget) { |
+ // Start unlocked. |
+ EXPECT_FALSE(view()->isPointerLocked()); |
+ |
+ // Lock. |
+ EXPECT_TRUE(view()->requestPointerLock()); |
+ view()->OnMessageReceived(ViewMsg_LockMouse_ACK(route_id_, true)); |
+ EXPECT_TRUE(view()->isPointerLocked()); |
+ |
+ // Unlock. |
+ view()->requestPointerUnlock(); |
+ view()->OnMessageReceived(ViewMsg_MouseLockLost(route_id_)); |
+ EXPECT_FALSE(view()->isPointerLocked()); |
+ |
+ // Attempt a lock, and have it fail. |
+ EXPECT_TRUE(view()->requestPointerLock()); |
+ view()->OnMessageReceived(ViewMsg_LockMouse_ACK(route_id_, false)); |
+ EXPECT_FALSE(view()->isPointerLocked()); |
+} |
+ |
+// Test simple use of MouseLockDispatcher with a mock LockTarget. |
+TEST_F(MouseLockDispatcherTest, BasicMockLockTarget) { |
+ MockLockTarget& target = *(target_.get()); |
+ ::testing::InSequence expect_calls_in_sequence; |
+ |
+ // Start unlocked. |
+ EXPECT_FALSE(dispatcher()->IsMouseLockedTo(NULL)); |
+ EXPECT_FALSE(dispatcher()->IsMouseLockedTo(&target)); |
+ |
+ // Lock. |
+ EXPECT_TRUE(dispatcher()->LockMouse(&target)); |
+ EXPECT_CALL(target, OnLockMouseACK(true)); |
yzshen1
2012/01/24 18:56:08
Important note: gMock requires expectations to be
scheib
2012/01/25 00:27:11
Done.
|
+ view()->OnMessageReceived(ViewMsg_LockMouse_ACK(route_id_, true)); |
+ EXPECT_TRUE(dispatcher()->IsMouseLockedTo(&target)); |
+ |
+ // Receive mouse event. |
+ EXPECT_CALL(target, HandleMouseLockedInputEvent(_)); |
+ dispatcher()->WillHandleMouseEvent(WebKit::WebMouseEvent()); |
+ |
+ // Unlock. |
+ dispatcher()->UnlockMouse(&target); |
+ EXPECT_CALL(target, OnMouseLockLost()); |
+ view()->OnMessageReceived(ViewMsg_MouseLockLost(route_id_)); |
+ EXPECT_FALSE(dispatcher()->IsMouseLockedTo(&target)); |
+ |
+ // Attempt a lock, and have it fail. |
+ EXPECT_TRUE(dispatcher()->LockMouse(&target)); |
+ EXPECT_CALL(target, OnLockMouseACK(false)); |
+ view()->OnMessageReceived(ViewMsg_LockMouse_ACK(route_id_, false)); |
+ EXPECT_FALSE(dispatcher()->IsMouseLockedTo(&target)); |
+} |
+ |
+// Test deleting a target while it is in use by MouseLockDispatcher. |
+TEST_F(MouseLockDispatcherTest, DeleteAndUnlock) { |
+ MockLockTarget& target = *(target_.get()); |
+ ::testing::InSequence expect_calls_in_sequence; |
+ |
+ // Lock. |
+ EXPECT_TRUE(dispatcher()->LockMouse(&target)); |
+ EXPECT_CALL(target, OnLockMouseACK(true)); |
+ view()->OnMessageReceived(ViewMsg_LockMouse_ACK(route_id_, true)); |
+ EXPECT_TRUE(dispatcher()->IsMouseLockedTo(&target)); |
+ |
+ // Unlock, with a deleted target. |
+ // Don't receive mouse events or lock lost. |
+ dispatcher()->UnlockMouseAndClearTarget(&target); |
+ EXPECT_CALL(target, HandleMouseLockedInputEvent(_)).Times(0); |
+ EXPECT_CALL(target, OnMouseLockLost()).Times(0); |
+ target_.reset(NULL); |
+ dispatcher()->WillHandleMouseEvent(WebKit::WebMouseEvent()); |
+ view()->OnMessageReceived(ViewMsg_MouseLockLost(route_id_)); |
+ EXPECT_FALSE(dispatcher()->IsMouseLockedTo(&target)); |
+} |
+ |
+// Test deleting a target that is pending a lock request response. |
+TEST_F(MouseLockDispatcherTest, DeleteWithPendingLockSuccess) { |
+ MockLockTarget& target = *(target_.get()); |
+ ::testing::InSequence expect_calls_in_sequence; |
+ |
+ // Lock request. |
+ EXPECT_TRUE(dispatcher()->LockMouse(&target)); |
+ |
+ // Before receiving response delete the target. |
+ dispatcher()->UnlockMouseAndClearTarget(&target); |
+ EXPECT_CALL(target, OnLockMouseACK(true)).Times(0); |
+ EXPECT_CALL(target, OnMouseLockLost()).Times(0); |
+ target_.reset(NULL); |
+ |
+ // Lock response. |
+ view()->OnMessageReceived(ViewMsg_LockMouse_ACK(route_id_, true)); |
+} |
+ |
+// Test deleting a target that is pending a lock request failure response. |
+TEST_F(MouseLockDispatcherTest, DeleteWithPendingLockFail) { |
+ MockLockTarget& target = *(target_.get()); |
+ ::testing::InSequence expect_calls_in_sequence; |
+ |
+ // Lock request. |
+ EXPECT_TRUE(dispatcher()->LockMouse(&target)); |
+ |
+ // Before receiving response delete the target. |
+ dispatcher()->UnlockMouseAndClearTarget(&target); |
+ EXPECT_CALL(target, OnLockMouseACK(true)).Times(0); |
+ EXPECT_CALL(target, OnMouseLockLost()).Times(0); |
+ target_.reset(NULL); |
+ |
+ // Lock response. |
+ view()->OnMessageReceived(ViewMsg_LockMouse_ACK(route_id_, false)); |
+} |
+ |
+// Test not receiving mouse events when a target is not locked. |
+TEST_F(MouseLockDispatcherTest, MouseEventsNotReceived) { |
+ MockLockTarget& target = *(target_.get()); |
+ ::testing::InSequence expect_calls_in_sequence; |
+ |
+ // (Don't) receive mouse event. |
+ EXPECT_CALL(target, HandleMouseLockedInputEvent(_)).Times(0); |
+ dispatcher()->WillHandleMouseEvent(WebKit::WebMouseEvent()); |
+ |
+ // Lock. |
+ EXPECT_TRUE(dispatcher()->LockMouse(&target)); |
+ EXPECT_CALL(target, OnLockMouseACK(true)); |
+ view()->OnMessageReceived(ViewMsg_LockMouse_ACK(route_id_, true)); |
+ EXPECT_TRUE(dispatcher()->IsMouseLockedTo(&target)); |
+ |
+ // Receive mouse event. |
+ EXPECT_CALL(target, HandleMouseLockedInputEvent(_)); |
+ dispatcher()->WillHandleMouseEvent(WebKit::WebMouseEvent()); |
+ |
+ // Unlock. |
+ dispatcher()->UnlockMouse(&target); |
+ EXPECT_CALL(target, OnMouseLockLost()); |
+ view()->OnMessageReceived(ViewMsg_MouseLockLost(route_id_)); |
+ EXPECT_FALSE(dispatcher()->IsMouseLockedTo(&target)); |
+ |
+ // (Don't) receive mouse event. |
+ EXPECT_CALL(target, HandleMouseLockedInputEvent(_)).Times(0); |
+ dispatcher()->WillHandleMouseEvent(WebKit::WebMouseEvent()); |
+} |
+ |
+// Test multiple targets |
+TEST_F(MouseLockDispatcherTest, MultipleTargets) { |
+ MockLockTarget& target = *(target_.get()); |
yzshen1
2012/01/24 18:56:08
Now that you define a ref explicitly at the top of
scheib
2012/01/25 00:27:11
Done: I've removed the per TEST reference line, an
|
+ MockLockTarget& alternate_target = *(alternate_target_.get()); |
+ ::testing::InSequence expect_calls_in_sequence; |
+ |
+ // Lock request for target. |
+ EXPECT_TRUE(dispatcher()->LockMouse(&target)); |
+ |
+ // Fail attempt to lock alternate. |
+ EXPECT_FALSE(dispatcher()->IsMouseLockedTo(&alternate_target)); |
+ EXPECT_FALSE(dispatcher()->LockMouse(&alternate_target)); |
+ |
+ // Lock completion for target. |
+ EXPECT_CALL(target, OnLockMouseACK(true)); |
+ view()->OnMessageReceived(ViewMsg_LockMouse_ACK(route_id_, true)); |
+ EXPECT_TRUE(dispatcher()->IsMouseLockedTo(&target)); |
+ |
+ // Fail attempt to lock alternate. |
+ EXPECT_FALSE(dispatcher()->IsMouseLockedTo(&alternate_target)); |
+ EXPECT_FALSE(dispatcher()->LockMouse(&alternate_target)); |
+ |
+ // Receive mouse event to only one target. |
+ EXPECT_CALL(target, HandleMouseLockedInputEvent(_)); |
+ EXPECT_CALL(alternate_target, HandleMouseLockedInputEvent(_)).Times(0); |
+ dispatcher()->WillHandleMouseEvent(WebKit::WebMouseEvent()); |
+ |
+ // Unlock alternate target has no effect. |
+ EXPECT_CALL(target, OnMouseLockLost()).Times(0); |
+ EXPECT_CALL(alternate_target, OnMouseLockLost()).Times(0); |
+ dispatcher()->UnlockMouse(&alternate_target); |
+ EXPECT_TRUE(dispatcher()->IsMouseLockedTo(&target)); |
+ EXPECT_FALSE(dispatcher()->IsMouseLockedTo(&alternate_target)); |
+ |
+ // Though the call to UnlockMouse should not unlock any target, we will |
+ // cause an unlock (as if e.g. user escaped mouse lock) and verify the |
+ // correct target is unlocked. |
+ EXPECT_CALL(target, OnMouseLockLost()); |
+ view()->OnMessageReceived(ViewMsg_MouseLockLost(route_id_)); |
+ EXPECT_FALSE(dispatcher()->IsMouseLockedTo(&target)); |
+} |
+ |