OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/mus/compositor_mus_connection.h" | 5 #include "content/renderer/mus/compositor_mus_connection.h" |
6 | 6 |
| 7 #include <memory> |
| 8 |
7 #include "base/macros.h" | 9 #include "base/macros.h" |
8 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "base/test/test_simple_task_runner.h" | 11 #include "base/test/test_simple_task_runner.h" |
11 #include "base/time/time.h" | 12 #include "base/time/time.h" |
12 #include "components/mus/public/cpp/tests/test_window.h" | 13 #include "components/mus/public/cpp/tests/test_window.h" |
13 #include "components/mus/public/interfaces/input_event_constants.mojom.h" | 14 #include "components/mus/public/interfaces/input_event_constants.mojom.h" |
14 #include "components/mus/public/interfaces/input_events.mojom.h" | 15 #include "components/mus/public/interfaces/input_events.mojom.h" |
15 #include "components/mus/public/interfaces/input_key_codes.mojom.h" | 16 #include "components/mus/public/interfaces/input_key_codes.mojom.h" |
16 #include "content/common/input/did_overscroll_params.h" | 17 #include "content/common/input/did_overscroll_params.h" |
17 #include "content/common/input/input_event_ack.h" | 18 #include "content/common/input/input_event_ack.h" |
18 #include "content/common/input/input_event_ack_state.h" | 19 #include "content/common/input/input_event_ack_state.h" |
19 #include "content/public/test/mock_render_thread.h" | 20 #include "content/public/test/mock_render_thread.h" |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 content::RenderWidget* render_widget) | 184 content::RenderWidget* render_widget) |
184 : content::RenderWidgetInputHandler(render_widget, render_widget), | 185 : content::RenderWidgetInputHandler(render_widget, render_widget), |
185 delegate_(nullptr), | 186 delegate_(nullptr), |
186 state_(content::InputEventAckState::INPUT_EVENT_ACK_STATE_UNKNOWN) {} | 187 state_(content::InputEventAckState::INPUT_EVENT_ACK_STATE_UNKNOWN) {} |
187 | 188 |
188 void TestRenderWidgetInputHandler::HandleInputEvent( | 189 void TestRenderWidgetInputHandler::HandleInputEvent( |
189 const blink::WebInputEvent& input_event, | 190 const blink::WebInputEvent& input_event, |
190 const ui::LatencyInfo& latency_info, | 191 const ui::LatencyInfo& latency_info, |
191 content::InputEventDispatchType dispatch_type) { | 192 content::InputEventDispatchType dispatch_type) { |
192 if (delegate_) { | 193 if (delegate_) { |
193 scoped_ptr<content::InputEventAck> ack( | 194 std::unique_ptr<content::InputEventAck> ack( |
194 new content::InputEventAck(input_event.type, state_)); | 195 new content::InputEventAck(input_event.type, state_)); |
195 delegate_->OnInputEventAck(std::move(ack)); | 196 delegate_->OnInputEventAck(std::move(ack)); |
196 } | 197 } |
197 } | 198 } |
198 | 199 |
199 } // namespace | 200 } // namespace |
200 | 201 |
201 namespace content { | 202 namespace content { |
202 | 203 |
203 // Test suite for CompositorMusConnection, this does not setup a full renderer | 204 // Test suite for CompositorMusConnection, this does not setup a full renderer |
204 // environment. This does not establish a connection to a mus server, nor does | 205 // environment. This does not establish a connection to a mus server, nor does |
205 // it initialize one. | 206 // it initialize one. |
206 class CompositorMusConnectionTest : public testing::Test { | 207 class CompositorMusConnectionTest : public testing::Test { |
207 public: | 208 public: |
208 CompositorMusConnectionTest() {} | 209 CompositorMusConnectionTest() {} |
209 ~CompositorMusConnectionTest() override {} | 210 ~CompositorMusConnectionTest() override {} |
210 | 211 |
211 // Returns a valid key event, so that it can be converted to a web event by | 212 // Returns a valid key event, so that it can be converted to a web event by |
212 // CompositorMusConnection. | 213 // CompositorMusConnection. |
213 scoped_ptr<ui::Event> GenerateKeyEvent(); | 214 std::unique_ptr<ui::Event> GenerateKeyEvent(); |
214 | 215 |
215 // Calls CompositorMusConnection::OnWindowInputEvent. | 216 // Calls CompositorMusConnection::OnWindowInputEvent. |
216 void OnWindowInputEvent(mus::Window* window, | 217 void OnWindowInputEvent( |
217 const ui::Event& event, | 218 mus::Window* window, |
218 scoped_ptr<base::Callback<void(bool)>>* ack_callback); | 219 const ui::Event& event, |
| 220 std::unique_ptr<base::Callback<void(bool)>>* ack_callback); |
219 | 221 |
220 // Confirms the state of pending tasks enqueued on each task runner, and runs | 222 // Confirms the state of pending tasks enqueued on each task runner, and runs |
221 // until idle. | 223 // until idle. |
222 void VerifyAndRunQueues(bool main_task_runner_enqueued, | 224 void VerifyAndRunQueues(bool main_task_runner_enqueued, |
223 bool compositor_task_runner_enqueued); | 225 bool compositor_task_runner_enqueued); |
224 | 226 |
225 CompositorMusConnection* compositor_connection() { | 227 CompositorMusConnection* compositor_connection() { |
226 return compositor_connection_.get(); | 228 return compositor_connection_.get(); |
227 } | 229 } |
228 RenderWidgetMusConnection* connection() { return connection_; } | 230 RenderWidgetMusConnection* connection() { return connection_; } |
(...skipping 22 matching lines...) Expand all Loading... |
251 RenderWidgetMusConnection* connection_; | 253 RenderWidgetMusConnection* connection_; |
252 | 254 |
253 // Test versions of task runners, see VerifyAndRunQueues to use in testing. | 255 // Test versions of task runners, see VerifyAndRunQueues to use in testing. |
254 scoped_refptr<base::TestSimpleTaskRunner> main_task_runner_; | 256 scoped_refptr<base::TestSimpleTaskRunner> main_task_runner_; |
255 scoped_refptr<base::TestSimpleTaskRunner> compositor_task_runner_; | 257 scoped_refptr<base::TestSimpleTaskRunner> compositor_task_runner_; |
256 | 258 |
257 // Actual CompositorMusConnection for testing. | 259 // Actual CompositorMusConnection for testing. |
258 scoped_refptr<CompositorMusConnection> compositor_connection_; | 260 scoped_refptr<CompositorMusConnection> compositor_connection_; |
259 | 261 |
260 // Test implementations, to control input given to |compositor_connection_|. | 262 // Test implementations, to control input given to |compositor_connection_|. |
261 scoped_ptr<TestInputHandlerManager> input_handler_manager_; | 263 std::unique_ptr<TestInputHandlerManager> input_handler_manager_; |
262 scoped_ptr<TestRenderWidgetInputHandler> render_widget_input_handler_; | 264 std::unique_ptr<TestRenderWidgetInputHandler> render_widget_input_handler_; |
263 | 265 |
264 DISALLOW_COPY_AND_ASSIGN(CompositorMusConnectionTest); | 266 DISALLOW_COPY_AND_ASSIGN(CompositorMusConnectionTest); |
265 }; | 267 }; |
266 | 268 |
267 scoped_ptr<ui::Event> CompositorMusConnectionTest::GenerateKeyEvent() { | 269 std::unique_ptr<ui::Event> CompositorMusConnectionTest::GenerateKeyEvent() { |
268 return scoped_ptr<ui::Event>(new ui::KeyEvent( | 270 return std::unique_ptr<ui::Event>(new ui::KeyEvent( |
269 ui::ET_KEY_PRESSED, ui::KeyboardCode::VKEY_A, ui::EF_NONE)); | 271 ui::ET_KEY_PRESSED, ui::KeyboardCode::VKEY_A, ui::EF_NONE)); |
270 } | 272 } |
271 | 273 |
272 void CompositorMusConnectionTest::OnWindowInputEvent( | 274 void CompositorMusConnectionTest::OnWindowInputEvent( |
273 mus::Window* window, | 275 mus::Window* window, |
274 const ui::Event& event, | 276 const ui::Event& event, |
275 scoped_ptr<base::Callback<void(bool)>>* ack_callback) { | 277 std::unique_ptr<base::Callback<void(bool)>>* ack_callback) { |
276 compositor_connection_->OnWindowInputEvent(window, event, ack_callback); | 278 compositor_connection_->OnWindowInputEvent(window, event, ack_callback); |
277 } | 279 } |
278 | 280 |
279 void CompositorMusConnectionTest::VerifyAndRunQueues( | 281 void CompositorMusConnectionTest::VerifyAndRunQueues( |
280 bool main_task_runner_enqueued, | 282 bool main_task_runner_enqueued, |
281 bool compositor_task_runner_enqueued) { | 283 bool compositor_task_runner_enqueued) { |
282 // Run through the enqueued actions. | 284 // Run through the enqueued actions. |
283 EXPECT_EQ(main_task_runner_enqueued, main_task_runner_->HasPendingTask()); | 285 EXPECT_EQ(main_task_runner_enqueued, main_task_runner_->HasPendingTask()); |
284 main_task_runner_->RunUntilIdle(); | 286 main_task_runner_->RunUntilIdle(); |
285 | 287 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 // Tests that for events which the renderer will ack, yet not consume, that | 325 // Tests that for events which the renderer will ack, yet not consume, that |
324 // CompositorMusConnection consumes the ack during OnWindowInputEvent, and calls | 326 // CompositorMusConnection consumes the ack during OnWindowInputEvent, and calls |
325 // it with the correct state once processed. | 327 // it with the correct state once processed. |
326 TEST_F(CompositorMusConnectionTest, NotConsumed) { | 328 TEST_F(CompositorMusConnectionTest, NotConsumed) { |
327 TestRenderWidgetInputHandler* input_handler = render_widget_input_handler(); | 329 TestRenderWidgetInputHandler* input_handler = render_widget_input_handler(); |
328 input_handler->set_delegate(connection()); | 330 input_handler->set_delegate(connection()); |
329 input_handler->set_state( | 331 input_handler->set_state( |
330 InputEventAckState::INPUT_EVENT_ACK_STATE_NOT_CONSUMED); | 332 InputEventAckState::INPUT_EVENT_ACK_STATE_NOT_CONSUMED); |
331 | 333 |
332 mus::TestWindow test_window; | 334 mus::TestWindow test_window; |
333 scoped_ptr<ui::Event> event(GenerateKeyEvent()); | 335 std::unique_ptr<ui::Event> event(GenerateKeyEvent()); |
334 scoped_refptr<TestCallback> test_callback(new TestCallback); | 336 scoped_refptr<TestCallback> test_callback(new TestCallback); |
335 scoped_ptr<base::Callback<void(bool)>> ack_callback( | 337 std::unique_ptr<base::Callback<void(bool)>> ack_callback( |
336 new base::Callback<void(bool)>( | 338 new base::Callback<void(bool)>( |
337 base::Bind(&::TestCallback::BoolCallback, test_callback))); | 339 base::Bind(&::TestCallback::BoolCallback, test_callback))); |
338 | 340 |
339 OnWindowInputEvent(&test_window, *event.get(), &ack_callback); | 341 OnWindowInputEvent(&test_window, *event.get(), &ack_callback); |
340 // OnWindowInputEvent is expected to clear the callback if it plans on | 342 // OnWindowInputEvent is expected to clear the callback if it plans on |
341 // handling the ack. | 343 // handling the ack. |
342 EXPECT_FALSE(ack_callback.get()); | 344 EXPECT_FALSE(ack_callback.get()); |
343 | 345 |
344 VerifyAndRunQueues(true, true); | 346 VerifyAndRunQueues(true, true); |
345 | 347 |
346 // The ack callback should have been called | 348 // The ack callback should have been called |
347 EXPECT_TRUE(test_callback->called()); | 349 EXPECT_TRUE(test_callback->called()); |
348 EXPECT_FALSE(test_callback->result()); | 350 EXPECT_FALSE(test_callback->result()); |
349 } | 351 } |
350 | 352 |
351 // Tests that for events which the renderer will ack, and consume, that | 353 // Tests that for events which the renderer will ack, and consume, that |
352 // CompositorMusConnection consumes the ack during OnWindowInputEvent, and calls | 354 // CompositorMusConnection consumes the ack during OnWindowInputEvent, and calls |
353 // it with the correct state once processed. | 355 // it with the correct state once processed. |
354 TEST_F(CompositorMusConnectionTest, Consumed) { | 356 TEST_F(CompositorMusConnectionTest, Consumed) { |
355 TestRenderWidgetInputHandler* input_handler = render_widget_input_handler(); | 357 TestRenderWidgetInputHandler* input_handler = render_widget_input_handler(); |
356 input_handler->set_delegate(connection()); | 358 input_handler->set_delegate(connection()); |
357 input_handler->set_state(InputEventAckState::INPUT_EVENT_ACK_STATE_CONSUMED); | 359 input_handler->set_state(InputEventAckState::INPUT_EVENT_ACK_STATE_CONSUMED); |
358 | 360 |
359 mus::TestWindow test_window; | 361 mus::TestWindow test_window; |
360 scoped_ptr<ui::Event> event(GenerateKeyEvent()); | 362 std::unique_ptr<ui::Event> event(GenerateKeyEvent()); |
361 scoped_refptr<TestCallback> test_callback(new TestCallback); | 363 scoped_refptr<TestCallback> test_callback(new TestCallback); |
362 scoped_ptr<base::Callback<void(bool)>> ack_callback( | 364 std::unique_ptr<base::Callback<void(bool)>> ack_callback( |
363 new base::Callback<void(bool)>( | 365 new base::Callback<void(bool)>( |
364 base::Bind(&::TestCallback::BoolCallback, test_callback))); | 366 base::Bind(&::TestCallback::BoolCallback, test_callback))); |
365 | 367 |
366 OnWindowInputEvent(&test_window, *event.get(), &ack_callback); | 368 OnWindowInputEvent(&test_window, *event.get(), &ack_callback); |
367 // OnWindowInputEvent is expected to clear the callback if it plans on | 369 // OnWindowInputEvent is expected to clear the callback if it plans on |
368 // handling the ack. | 370 // handling the ack. |
369 EXPECT_FALSE(ack_callback.get()); | 371 EXPECT_FALSE(ack_callback.get()); |
370 | 372 |
371 VerifyAndRunQueues(true, true); | 373 VerifyAndRunQueues(true, true); |
372 | 374 |
373 // The ack callback should have been called | 375 // The ack callback should have been called |
374 EXPECT_TRUE(test_callback->called()); | 376 EXPECT_TRUE(test_callback->called()); |
375 EXPECT_TRUE(test_callback->result()); | 377 EXPECT_TRUE(test_callback->result()); |
376 } | 378 } |
377 | 379 |
378 // Tests that when the RenderWidgetInputHandler does not ack before a new event | 380 // Tests that when the RenderWidgetInputHandler does not ack before a new event |
379 // arrives, that only the most recent ack is fired. | 381 // arrives, that only the most recent ack is fired. |
380 TEST_F(CompositorMusConnectionTest, LostAck) { | 382 TEST_F(CompositorMusConnectionTest, LostAck) { |
381 mus::TestWindow test_window; | 383 mus::TestWindow test_window; |
382 scoped_ptr<ui::Event> event1(GenerateKeyEvent()); | 384 std::unique_ptr<ui::Event> event1(GenerateKeyEvent()); |
383 scoped_refptr<TestCallback> test_callback1(new TestCallback); | 385 scoped_refptr<TestCallback> test_callback1(new TestCallback); |
384 scoped_ptr<base::Callback<void(bool)>> ack_callback1( | 386 std::unique_ptr<base::Callback<void(bool)>> ack_callback1( |
385 new base::Callback<void(bool)>( | 387 new base::Callback<void(bool)>( |
386 base::Bind(&::TestCallback::BoolCallback, test_callback1))); | 388 base::Bind(&::TestCallback::BoolCallback, test_callback1))); |
387 | 389 |
388 OnWindowInputEvent(&test_window, *event1.get(), &ack_callback1); | 390 OnWindowInputEvent(&test_window, *event1.get(), &ack_callback1); |
389 EXPECT_FALSE(ack_callback1.get()); | 391 EXPECT_FALSE(ack_callback1.get()); |
390 // When simulating the timeout the ack is never enqueued | 392 // When simulating the timeout the ack is never enqueued |
391 VerifyAndRunQueues(true, false); | 393 VerifyAndRunQueues(true, false); |
392 | 394 |
393 // Setting a delegate will lead to the next event being acked. Having a | 395 // Setting a delegate will lead to the next event being acked. Having a |
394 // cleared queue simulates the input handler timing out on an event. | 396 // cleared queue simulates the input handler timing out on an event. |
395 TestRenderWidgetInputHandler* input_handler = render_widget_input_handler(); | 397 TestRenderWidgetInputHandler* input_handler = render_widget_input_handler(); |
396 input_handler->set_delegate(connection()); | 398 input_handler->set_delegate(connection()); |
397 input_handler->set_state(InputEventAckState::INPUT_EVENT_ACK_STATE_CONSUMED); | 399 input_handler->set_state(InputEventAckState::INPUT_EVENT_ACK_STATE_CONSUMED); |
398 | 400 |
399 scoped_ptr<ui::Event> event2(GenerateKeyEvent()); | 401 std::unique_ptr<ui::Event> event2(GenerateKeyEvent()); |
400 scoped_refptr<TestCallback> test_callback2(new TestCallback); | 402 scoped_refptr<TestCallback> test_callback2(new TestCallback); |
401 scoped_ptr<base::Callback<void(bool)>> ack_callback2( | 403 std::unique_ptr<base::Callback<void(bool)>> ack_callback2( |
402 new base::Callback<void(bool)>( | 404 new base::Callback<void(bool)>( |
403 base::Bind(&::TestCallback::BoolCallback, test_callback2))); | 405 base::Bind(&::TestCallback::BoolCallback, test_callback2))); |
404 OnWindowInputEvent(&test_window, *event2.get(), &ack_callback2); | 406 OnWindowInputEvent(&test_window, *event2.get(), &ack_callback2); |
405 EXPECT_FALSE(ack_callback2.get()); | 407 EXPECT_FALSE(ack_callback2.get()); |
406 | 408 |
407 VerifyAndRunQueues(true, true); | 409 VerifyAndRunQueues(true, true); |
408 | 410 |
409 // Only the most recent ack was called. | 411 // Only the most recent ack was called. |
410 EXPECT_FALSE(test_callback1->called()); | 412 EXPECT_FALSE(test_callback1->called()); |
411 EXPECT_TRUE(test_callback2->called()); | 413 EXPECT_TRUE(test_callback2->called()); |
412 EXPECT_TRUE(test_callback2->result()); | 414 EXPECT_TRUE(test_callback2->result()); |
413 } | 415 } |
414 | 416 |
415 // Tests that when an input handler consumes the event, that | 417 // Tests that when an input handler consumes the event, that |
416 // CompositorMusConnection does not consume the ack, nor calls it. | 418 // CompositorMusConnection does not consume the ack, nor calls it. |
417 TEST_F(CompositorMusConnectionTest, InputHandlerConsumes) { | 419 TEST_F(CompositorMusConnectionTest, InputHandlerConsumes) { |
418 input_handler_manager()->SetHandleInputEventResult( | 420 input_handler_manager()->SetHandleInputEventResult( |
419 InputEventAckState::INPUT_EVENT_ACK_STATE_CONSUMED); | 421 InputEventAckState::INPUT_EVENT_ACK_STATE_CONSUMED); |
420 mus::TestWindow test_window; | 422 mus::TestWindow test_window; |
421 scoped_ptr<ui::Event> event(GenerateKeyEvent()); | 423 std::unique_ptr<ui::Event> event(GenerateKeyEvent()); |
422 scoped_refptr<TestCallback> test_callback(new TestCallback); | 424 scoped_refptr<TestCallback> test_callback(new TestCallback); |
423 scoped_ptr<base::Callback<void(bool)>> ack_callback( | 425 std::unique_ptr<base::Callback<void(bool)>> ack_callback( |
424 new base::Callback<void(bool)>( | 426 new base::Callback<void(bool)>( |
425 base::Bind(&::TestCallback::BoolCallback, test_callback))); | 427 base::Bind(&::TestCallback::BoolCallback, test_callback))); |
426 | 428 |
427 OnWindowInputEvent(&test_window, *event.get(), &ack_callback); | 429 OnWindowInputEvent(&test_window, *event.get(), &ack_callback); |
428 | 430 |
429 EXPECT_TRUE(ack_callback.get()); | 431 EXPECT_TRUE(ack_callback.get()); |
430 VerifyAndRunQueues(false, false); | 432 VerifyAndRunQueues(false, false); |
431 EXPECT_FALSE(test_callback->called()); | 433 EXPECT_FALSE(test_callback->called()); |
432 } | 434 } |
433 | 435 |
434 // Tests that when the renderer will not ack an event, that | 436 // Tests that when the renderer will not ack an event, that |
435 // CompositorMusConnection does not consume the ack, nor calls it. | 437 // CompositorMusConnection does not consume the ack, nor calls it. |
436 TEST_F(CompositorMusConnectionTest, RendererWillNotSendAck) { | 438 TEST_F(CompositorMusConnectionTest, RendererWillNotSendAck) { |
437 mus::TestWindow test_window; | 439 mus::TestWindow test_window; |
438 ui::PointerEvent event(ui::ET_POINTER_DOWN, | 440 ui::PointerEvent event(ui::ET_POINTER_DOWN, |
439 ui::EventPointerType::POINTER_TYPE_MOUSE, gfx::Point(), | 441 ui::EventPointerType::POINTER_TYPE_MOUSE, gfx::Point(), |
440 gfx::Point(), ui::EF_NONE, 0, ui::EventTimeForNow()); | 442 gfx::Point(), ui::EF_NONE, 0, ui::EventTimeForNow()); |
441 | 443 |
442 scoped_refptr<TestCallback> test_callback(new TestCallback); | 444 scoped_refptr<TestCallback> test_callback(new TestCallback); |
443 scoped_ptr<base::Callback<void(bool)>> ack_callback( | 445 std::unique_ptr<base::Callback<void(bool)>> ack_callback( |
444 new base::Callback<void(bool)>( | 446 new base::Callback<void(bool)>( |
445 base::Bind(&::TestCallback::BoolCallback, test_callback))); | 447 base::Bind(&::TestCallback::BoolCallback, test_callback))); |
446 | 448 |
447 OnWindowInputEvent(&test_window, event, &ack_callback); | 449 OnWindowInputEvent(&test_window, event, &ack_callback); |
448 EXPECT_TRUE(ack_callback.get()); | 450 EXPECT_TRUE(ack_callback.get()); |
449 | 451 |
450 VerifyAndRunQueues(true, false); | 452 VerifyAndRunQueues(true, false); |
451 EXPECT_FALSE(test_callback->called()); | 453 EXPECT_FALSE(test_callback->called()); |
452 } | 454 } |
453 | 455 |
454 } // namespace content | 456 } // namespace content |
OLD | NEW |