OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include <string> | |
6 | |
7 #include "content/common/view_messages.h" | |
8 #include "content/renderer/render_view_impl.h" | |
9 #include "content/test/render_view_test.h" | |
10 #include "mouse_lock_dispatcher.h" | |
yzshen1
2012/01/24 18:56:08
Please add path.
scheib
2012/01/25 00:27:11
Done.
| |
11 #include "testing/gmock/include/gmock/gmock.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | |
13 | |
14 using ::testing::_; | |
15 | |
16 namespace { | |
17 | |
18 class MockLockTarget : public MouseLockDispatcher::LockTarget { | |
19 public: | |
20 MOCK_METHOD1(OnLockMouseACK, void(bool)); | |
21 MOCK_METHOD0(OnMouseLockLost, void()); | |
22 MOCK_METHOD1(HandleMouseLockedInputEvent, | |
23 bool(const WebKit::WebMouseEvent&)); | |
24 }; | |
25 | |
26 // MouseLockDispatcher is a RenderViewObserver, and we test it by creating a | |
27 // fixture containing a RenderViewImpl view() and interacting to that interface. | |
28 class MouseLockDispatcherTest | |
29 : public content::RenderViewTest { | |
30 public: | |
31 virtual void SetUp() { | |
32 content::RenderViewTest::SetUp(); | |
33 route_id_ = view()->GetRoutingId(); | |
34 target_.reset(new MockLockTarget()); | |
35 alternate_target_.reset(new MockLockTarget()); | |
36 } | |
37 | |
38 virtual void TearDown() { | |
39 content::RenderViewTest::TearDown(); | |
40 target_.reset(NULL); | |
41 alternate_target_.reset(NULL); | |
42 } | |
43 | |
44 RenderViewImpl* view() { return static_cast<RenderViewImpl*>(view_); } | |
45 MouseLockDispatcher* dispatcher() { return view()->mouse_lock_dispatcher(); } | |
46 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.
| |
47 scoped_ptr<MockLockTarget> target_; | |
48 scoped_ptr<MockLockTarget> alternate_target_; | |
49 }; | |
50 | |
51 } // namespace | |
52 | |
53 // Test simple use of RenderViewImpl interface to WebKit for pointer lock. | |
54 TEST_F(MouseLockDispatcherTest, BasicWebWidget) { | |
55 // Start unlocked. | |
56 EXPECT_FALSE(view()->isPointerLocked()); | |
57 | |
58 // Lock. | |
59 EXPECT_TRUE(view()->requestPointerLock()); | |
60 view()->OnMessageReceived(ViewMsg_LockMouse_ACK(route_id_, true)); | |
61 EXPECT_TRUE(view()->isPointerLocked()); | |
62 | |
63 // Unlock. | |
64 view()->requestPointerUnlock(); | |
65 view()->OnMessageReceived(ViewMsg_MouseLockLost(route_id_)); | |
66 EXPECT_FALSE(view()->isPointerLocked()); | |
67 | |
68 // Attempt a lock, and have it fail. | |
69 EXPECT_TRUE(view()->requestPointerLock()); | |
70 view()->OnMessageReceived(ViewMsg_LockMouse_ACK(route_id_, false)); | |
71 EXPECT_FALSE(view()->isPointerLocked()); | |
72 } | |
73 | |
74 // Test simple use of MouseLockDispatcher with a mock LockTarget. | |
75 TEST_F(MouseLockDispatcherTest, BasicMockLockTarget) { | |
76 MockLockTarget& target = *(target_.get()); | |
77 ::testing::InSequence expect_calls_in_sequence; | |
78 | |
79 // Start unlocked. | |
80 EXPECT_FALSE(dispatcher()->IsMouseLockedTo(NULL)); | |
81 EXPECT_FALSE(dispatcher()->IsMouseLockedTo(&target)); | |
82 | |
83 // Lock. | |
84 EXPECT_TRUE(dispatcher()->LockMouse(&target)); | |
85 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.
| |
86 view()->OnMessageReceived(ViewMsg_LockMouse_ACK(route_id_, true)); | |
87 EXPECT_TRUE(dispatcher()->IsMouseLockedTo(&target)); | |
88 | |
89 // Receive mouse event. | |
90 EXPECT_CALL(target, HandleMouseLockedInputEvent(_)); | |
91 dispatcher()->WillHandleMouseEvent(WebKit::WebMouseEvent()); | |
92 | |
93 // Unlock. | |
94 dispatcher()->UnlockMouse(&target); | |
95 EXPECT_CALL(target, OnMouseLockLost()); | |
96 view()->OnMessageReceived(ViewMsg_MouseLockLost(route_id_)); | |
97 EXPECT_FALSE(dispatcher()->IsMouseLockedTo(&target)); | |
98 | |
99 // Attempt a lock, and have it fail. | |
100 EXPECT_TRUE(dispatcher()->LockMouse(&target)); | |
101 EXPECT_CALL(target, OnLockMouseACK(false)); | |
102 view()->OnMessageReceived(ViewMsg_LockMouse_ACK(route_id_, false)); | |
103 EXPECT_FALSE(dispatcher()->IsMouseLockedTo(&target)); | |
104 } | |
105 | |
106 // Test deleting a target while it is in use by MouseLockDispatcher. | |
107 TEST_F(MouseLockDispatcherTest, DeleteAndUnlock) { | |
108 MockLockTarget& target = *(target_.get()); | |
109 ::testing::InSequence expect_calls_in_sequence; | |
110 | |
111 // Lock. | |
112 EXPECT_TRUE(dispatcher()->LockMouse(&target)); | |
113 EXPECT_CALL(target, OnLockMouseACK(true)); | |
114 view()->OnMessageReceived(ViewMsg_LockMouse_ACK(route_id_, true)); | |
115 EXPECT_TRUE(dispatcher()->IsMouseLockedTo(&target)); | |
116 | |
117 // Unlock, with a deleted target. | |
118 // Don't receive mouse events or lock lost. | |
119 dispatcher()->UnlockMouseAndClearTarget(&target); | |
120 EXPECT_CALL(target, HandleMouseLockedInputEvent(_)).Times(0); | |
121 EXPECT_CALL(target, OnMouseLockLost()).Times(0); | |
122 target_.reset(NULL); | |
123 dispatcher()->WillHandleMouseEvent(WebKit::WebMouseEvent()); | |
124 view()->OnMessageReceived(ViewMsg_MouseLockLost(route_id_)); | |
125 EXPECT_FALSE(dispatcher()->IsMouseLockedTo(&target)); | |
126 } | |
127 | |
128 // Test deleting a target that is pending a lock request response. | |
129 TEST_F(MouseLockDispatcherTest, DeleteWithPendingLockSuccess) { | |
130 MockLockTarget& target = *(target_.get()); | |
131 ::testing::InSequence expect_calls_in_sequence; | |
132 | |
133 // Lock request. | |
134 EXPECT_TRUE(dispatcher()->LockMouse(&target)); | |
135 | |
136 // Before receiving response delete the target. | |
137 dispatcher()->UnlockMouseAndClearTarget(&target); | |
138 EXPECT_CALL(target, OnLockMouseACK(true)).Times(0); | |
139 EXPECT_CALL(target, OnMouseLockLost()).Times(0); | |
140 target_.reset(NULL); | |
141 | |
142 // Lock response. | |
143 view()->OnMessageReceived(ViewMsg_LockMouse_ACK(route_id_, true)); | |
144 } | |
145 | |
146 // Test deleting a target that is pending a lock request failure response. | |
147 TEST_F(MouseLockDispatcherTest, DeleteWithPendingLockFail) { | |
148 MockLockTarget& target = *(target_.get()); | |
149 ::testing::InSequence expect_calls_in_sequence; | |
150 | |
151 // Lock request. | |
152 EXPECT_TRUE(dispatcher()->LockMouse(&target)); | |
153 | |
154 // Before receiving response delete the target. | |
155 dispatcher()->UnlockMouseAndClearTarget(&target); | |
156 EXPECT_CALL(target, OnLockMouseACK(true)).Times(0); | |
157 EXPECT_CALL(target, OnMouseLockLost()).Times(0); | |
158 target_.reset(NULL); | |
159 | |
160 // Lock response. | |
161 view()->OnMessageReceived(ViewMsg_LockMouse_ACK(route_id_, false)); | |
162 } | |
163 | |
164 // Test not receiving mouse events when a target is not locked. | |
165 TEST_F(MouseLockDispatcherTest, MouseEventsNotReceived) { | |
166 MockLockTarget& target = *(target_.get()); | |
167 ::testing::InSequence expect_calls_in_sequence; | |
168 | |
169 // (Don't) receive mouse event. | |
170 EXPECT_CALL(target, HandleMouseLockedInputEvent(_)).Times(0); | |
171 dispatcher()->WillHandleMouseEvent(WebKit::WebMouseEvent()); | |
172 | |
173 // Lock. | |
174 EXPECT_TRUE(dispatcher()->LockMouse(&target)); | |
175 EXPECT_CALL(target, OnLockMouseACK(true)); | |
176 view()->OnMessageReceived(ViewMsg_LockMouse_ACK(route_id_, true)); | |
177 EXPECT_TRUE(dispatcher()->IsMouseLockedTo(&target)); | |
178 | |
179 // Receive mouse event. | |
180 EXPECT_CALL(target, HandleMouseLockedInputEvent(_)); | |
181 dispatcher()->WillHandleMouseEvent(WebKit::WebMouseEvent()); | |
182 | |
183 // Unlock. | |
184 dispatcher()->UnlockMouse(&target); | |
185 EXPECT_CALL(target, OnMouseLockLost()); | |
186 view()->OnMessageReceived(ViewMsg_MouseLockLost(route_id_)); | |
187 EXPECT_FALSE(dispatcher()->IsMouseLockedTo(&target)); | |
188 | |
189 // (Don't) receive mouse event. | |
190 EXPECT_CALL(target, HandleMouseLockedInputEvent(_)).Times(0); | |
191 dispatcher()->WillHandleMouseEvent(WebKit::WebMouseEvent()); | |
192 } | |
193 | |
194 // Test multiple targets | |
195 TEST_F(MouseLockDispatcherTest, MultipleTargets) { | |
196 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
| |
197 MockLockTarget& alternate_target = *(alternate_target_.get()); | |
198 ::testing::InSequence expect_calls_in_sequence; | |
199 | |
200 // Lock request for target. | |
201 EXPECT_TRUE(dispatcher()->LockMouse(&target)); | |
202 | |
203 // Fail attempt to lock alternate. | |
204 EXPECT_FALSE(dispatcher()->IsMouseLockedTo(&alternate_target)); | |
205 EXPECT_FALSE(dispatcher()->LockMouse(&alternate_target)); | |
206 | |
207 // Lock completion for target. | |
208 EXPECT_CALL(target, OnLockMouseACK(true)); | |
209 view()->OnMessageReceived(ViewMsg_LockMouse_ACK(route_id_, true)); | |
210 EXPECT_TRUE(dispatcher()->IsMouseLockedTo(&target)); | |
211 | |
212 // Fail attempt to lock alternate. | |
213 EXPECT_FALSE(dispatcher()->IsMouseLockedTo(&alternate_target)); | |
214 EXPECT_FALSE(dispatcher()->LockMouse(&alternate_target)); | |
215 | |
216 // Receive mouse event to only one target. | |
217 EXPECT_CALL(target, HandleMouseLockedInputEvent(_)); | |
218 EXPECT_CALL(alternate_target, HandleMouseLockedInputEvent(_)).Times(0); | |
219 dispatcher()->WillHandleMouseEvent(WebKit::WebMouseEvent()); | |
220 | |
221 // Unlock alternate target has no effect. | |
222 EXPECT_CALL(target, OnMouseLockLost()).Times(0); | |
223 EXPECT_CALL(alternate_target, OnMouseLockLost()).Times(0); | |
224 dispatcher()->UnlockMouse(&alternate_target); | |
225 EXPECT_TRUE(dispatcher()->IsMouseLockedTo(&target)); | |
226 EXPECT_FALSE(dispatcher()->IsMouseLockedTo(&alternate_target)); | |
227 | |
228 // Though the call to UnlockMouse should not unlock any target, we will | |
229 // cause an unlock (as if e.g. user escaped mouse lock) and verify the | |
230 // correct target is unlocked. | |
231 EXPECT_CALL(target, OnMouseLockLost()); | |
232 view()->OnMessageReceived(ViewMsg_MouseLockLost(route_id_)); | |
233 EXPECT_FALSE(dispatcher()->IsMouseLockedTo(&target)); | |
234 } | |
235 | |
OLD | NEW |