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 "ppapi/tests/test_mouse_lock.h"
6
7#include "ppapi/cpp/input_event.h"
8#include "ppapi/cpp/view.h"
9#include "ppapi/tests/testing_instance.h"
10
11REGISTER_TEST_CASE(MouseLock);
12
13TestMouseLock::TestMouseLock(TestingInstance* instance)
14    : TestCase(instance),
15      MouseLock(instance),
16      nested_event_(instance->pp_instance()) {
17}
18
19TestMouseLock::~TestMouseLock() {
20}
21
22bool TestMouseLock::Init() {
23  return CheckTestingInterface();
24}
25
26void TestMouseLock::RunTests(const std::string& filter) {
27  RUN_TEST(SucceedWhenAllowed, filter);
28  RUN_TEST(FailWhenBlocked, filter);
29}
30
31void TestMouseLock::DidChangeView(const pp::View& view) {
32  position_ = view.GetRect();
33}
34
35void TestMouseLock::MouseLockLost() {
36  nested_event_.Signal();
37}
38
39std::string TestMouseLock::TestSucceedWhenAllowed() {
40  // Content settings are configured to allow mouse lock for any site.
41  // Please see chrome/test/ppapi/ppapi_interactive_browsertest.cc.
42  TestCompletionCallback callback(instance_->pp_instance(), callback_type());
43  SimulateUserGesture();
44  callback.WaitForResult(LockMouse(callback.GetCallback()));
45  ASSERT_EQ(PP_OK, callback.result());
46
47  UnlockMouse();
48  // Wait for the MouseLockLost() call.
49  nested_event_.Wait();
50
51  PASS();
52}
53
54std::string TestMouseLock::TestFailWhenBlocked() {
55  // Content settings are configured to block mouse lock for any site.
56  // Please see chrome/test/ppapi/ppapi_interactive_browsertest.cc.
57  TestCompletionCallback callback(instance_->pp_instance(), callback_type());
58  SimulateUserGesture();
59  callback.WaitForResult(LockMouse(callback.GetCallback()));
60  ASSERT_NE(PP_OK, callback.result());
61
62  PASS();
63}
64
65void TestMouseLock::SimulateUserGesture() {
66  pp::Point mouse_movement;
67  pp::MouseInputEvent input_event(
68      instance_,
69      PP_INPUTEVENT_TYPE_MOUSEDOWN,
70      0,  // time_stamp
71      0,  // modifiers
72      PP_INPUTEVENT_MOUSEBUTTON_LEFT,
73      position_.CenterPoint(),
74      1,  // click_count
75      mouse_movement);
76
77  testing_interface_->SimulateInputEvent(instance_->pp_instance(),
78                                         input_event.pp_resource());
79}
80