This source file includes following definitions.
- SetUp
- TearDown
- view
- dispatcher
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include <string>
#include "content/common/view_messages.h"
#include "content/public/test/render_view_test.h"
#include "content/renderer/mouse_lock_dispatcher.h"
#include "content/renderer/render_view_impl.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::_;
namespace content {
namespace {
class MockLockTarget : public MouseLockDispatcher::LockTarget {
public:
MOCK_METHOD1(OnLockMouseACK, void(bool));
MOCK_METHOD0(OnMouseLockLost, void());
MOCK_METHOD1(HandleMouseLockedInputEvent,
bool(const blink::WebMouseEvent&));
};
class MouseLockDispatcherTest : public RenderViewTest {
public:
virtual void SetUp() {
RenderViewTest::SetUp();
route_id_ = view()->GetRoutingID();
target_ = new MockLockTarget();
alternate_target_ = new MockLockTarget();
}
virtual void TearDown() {
RenderViewTest::TearDown();
delete target_;
delete alternate_target_;
}
protected:
RenderViewImpl* view() { return static_cast<RenderViewImpl*>(view_); }
MouseLockDispatcher* dispatcher() { return view()->mouse_lock_dispatcher(); }
int route_id_;
MockLockTarget* target_;
MockLockTarget* alternate_target_;
};
}
TEST_F(MouseLockDispatcherTest, BasicWebWidget) {
EXPECT_FALSE(view()->isPointerLocked());
EXPECT_TRUE(view()->requestPointerLock());
view()->OnMessageReceived(ViewMsg_LockMouse_ACK(route_id_, true));
EXPECT_TRUE(view()->isPointerLocked());
view()->requestPointerUnlock();
view()->OnMessageReceived(ViewMsg_MouseLockLost(route_id_));
EXPECT_FALSE(view()->isPointerLocked());
EXPECT_TRUE(view()->requestPointerLock());
view()->OnMessageReceived(ViewMsg_LockMouse_ACK(route_id_, false));
EXPECT_FALSE(view()->isPointerLocked());
}
TEST_F(MouseLockDispatcherTest, BasicMockLockTarget) {
::testing::InSequence expect_calls_in_sequence;
EXPECT_CALL(*target_, OnLockMouseACK(true));
EXPECT_CALL(*target_, HandleMouseLockedInputEvent(_));
EXPECT_CALL(*target_, OnMouseLockLost());
EXPECT_CALL(*target_, OnLockMouseACK(false));
EXPECT_FALSE(dispatcher()->IsMouseLockedTo(NULL));
EXPECT_FALSE(dispatcher()->IsMouseLockedTo(target_));
EXPECT_TRUE(dispatcher()->LockMouse(target_));
view()->OnMessageReceived(ViewMsg_LockMouse_ACK(route_id_, true));
EXPECT_TRUE(dispatcher()->IsMouseLockedTo(target_));
dispatcher()->WillHandleMouseEvent(blink::WebMouseEvent());
dispatcher()->UnlockMouse(target_);
view()->OnMessageReceived(ViewMsg_MouseLockLost(route_id_));
EXPECT_FALSE(dispatcher()->IsMouseLockedTo(target_));
EXPECT_TRUE(dispatcher()->LockMouse(target_));
view()->OnMessageReceived(ViewMsg_LockMouse_ACK(route_id_, false));
EXPECT_FALSE(dispatcher()->IsMouseLockedTo(target_));
}
TEST_F(MouseLockDispatcherTest, DeleteAndUnlock) {
::testing::InSequence expect_calls_in_sequence;
EXPECT_CALL(*target_, OnLockMouseACK(true));
EXPECT_CALL(*target_, HandleMouseLockedInputEvent(_)).Times(0);
EXPECT_CALL(*target_, OnMouseLockLost()).Times(0);
EXPECT_TRUE(dispatcher()->LockMouse(target_));
view()->OnMessageReceived(ViewMsg_LockMouse_ACK(route_id_, true));
EXPECT_TRUE(dispatcher()->IsMouseLockedTo(target_));
dispatcher()->OnLockTargetDestroyed(target_);
delete target_;
target_ = NULL;
dispatcher()->WillHandleMouseEvent(blink::WebMouseEvent());
view()->OnMessageReceived(ViewMsg_MouseLockLost(route_id_));
EXPECT_FALSE(dispatcher()->IsMouseLockedTo(target_));
}
TEST_F(MouseLockDispatcherTest, DeleteWithPendingLockSuccess) {
::testing::InSequence expect_calls_in_sequence;
EXPECT_CALL(*target_, OnLockMouseACK(true)).Times(0);
EXPECT_CALL(*target_, OnMouseLockLost()).Times(0);
EXPECT_TRUE(dispatcher()->LockMouse(target_));
dispatcher()->OnLockTargetDestroyed(target_);
delete target_;
target_ = NULL;
view()->OnMessageReceived(ViewMsg_LockMouse_ACK(route_id_, true));
}
TEST_F(MouseLockDispatcherTest, DeleteWithPendingLockFail) {
::testing::InSequence expect_calls_in_sequence;
EXPECT_CALL(*target_, OnLockMouseACK(true)).Times(0);
EXPECT_CALL(*target_, OnMouseLockLost()).Times(0);
EXPECT_TRUE(dispatcher()->LockMouse(target_));
dispatcher()->OnLockTargetDestroyed(target_);
delete target_;
target_ = NULL;
view()->OnMessageReceived(ViewMsg_LockMouse_ACK(route_id_, false));
}
TEST_F(MouseLockDispatcherTest, MouseEventsNotReceived) {
::testing::InSequence expect_calls_in_sequence;
EXPECT_CALL(*target_, HandleMouseLockedInputEvent(_)).Times(0);
EXPECT_CALL(*target_, OnLockMouseACK(true));
EXPECT_CALL(*target_, HandleMouseLockedInputEvent(_));
EXPECT_CALL(*target_, OnMouseLockLost());
EXPECT_CALL(*target_, HandleMouseLockedInputEvent(_)).Times(0);
dispatcher()->WillHandleMouseEvent(blink::WebMouseEvent());
EXPECT_TRUE(dispatcher()->LockMouse(target_));
view()->OnMessageReceived(ViewMsg_LockMouse_ACK(route_id_, true));
EXPECT_TRUE(dispatcher()->IsMouseLockedTo(target_));
dispatcher()->WillHandleMouseEvent(blink::WebMouseEvent());
dispatcher()->UnlockMouse(target_);
view()->OnMessageReceived(ViewMsg_MouseLockLost(route_id_));
EXPECT_FALSE(dispatcher()->IsMouseLockedTo(target_));
dispatcher()->WillHandleMouseEvent(blink::WebMouseEvent());
}
TEST_F(MouseLockDispatcherTest, MultipleTargets) {
::testing::InSequence expect_calls_in_sequence;
EXPECT_CALL(*target_, OnLockMouseACK(true));
EXPECT_CALL(*target_, HandleMouseLockedInputEvent(_));
EXPECT_CALL(*alternate_target_, HandleMouseLockedInputEvent(_)).Times(0);
EXPECT_CALL(*target_, OnMouseLockLost()).Times(0);
EXPECT_CALL(*alternate_target_, OnMouseLockLost()).Times(0);
EXPECT_CALL(*target_, OnMouseLockLost());
EXPECT_TRUE(dispatcher()->LockMouse(target_));
EXPECT_FALSE(dispatcher()->IsMouseLockedTo(alternate_target_));
EXPECT_FALSE(dispatcher()->LockMouse(alternate_target_));
view()->OnMessageReceived(ViewMsg_LockMouse_ACK(route_id_, true));
EXPECT_TRUE(dispatcher()->IsMouseLockedTo(target_));
EXPECT_FALSE(dispatcher()->IsMouseLockedTo(alternate_target_));
EXPECT_FALSE(dispatcher()->LockMouse(alternate_target_));
dispatcher()->WillHandleMouseEvent(blink::WebMouseEvent());
dispatcher()->UnlockMouse(alternate_target_);
EXPECT_TRUE(dispatcher()->IsMouseLockedTo(target_));
EXPECT_FALSE(dispatcher()->IsMouseLockedTo(alternate_target_));
view()->OnMessageReceived(ViewMsg_MouseLockLost(route_id_));
EXPECT_FALSE(dispatcher()->IsMouseLockedTo(target_));
}
}