This source file includes following definitions.
- GetCenter
- RunTests
- received_finish_message_
- Init
- CreateMouseEvent
- CreateWheelEvent
- CreateKeyEvent
- CreateCharEvent
- CreateTouchEvent
- PostMessageBarrier
- SimulateInputEvent
- AreEquivalentEvents
- HandleInputEvent
- HandleMessage
- DidChangeView
- TestEvents
- TestAcceptTouchEvent_1
- TestAcceptTouchEvent_2
- TestAcceptTouchEvent_3
- TestAcceptTouchEvent_4
#include "ppapi/tests/test_input_event.h"
#include "ppapi/c/pp_errors.h"
#include "ppapi/c/ppb_input_event.h"
#include "ppapi/cpp/input_event.h"
#include "ppapi/cpp/module.h"
#include "ppapi/tests/test_utils.h"
#include "ppapi/tests/testing_instance.h"
REGISTER_TEST_CASE(InputEvent);
namespace {
const uint32_t kSpaceChar = 0x20;
const char* kSpaceString = " ";
const char* kSpaceCode = "Space";
#define FINISHED_WAITING_MESSAGE "TEST_INPUT_EVENT_FINISHED_WAITING"
pp::Point GetCenter(const pp::Rect& rect) {
  return pp::Point(
      rect.x() + rect.width() / 2,
      rect.y() + rect.height() / 2);
}
}  
void TestInputEvent::RunTests(const std::string& filter) {
  RUN_TEST(Events, filter);
  
  
  
  
  if (!ShouldRunAllTests(filter)) {
    RUN_TEST(AcceptTouchEvent_1, filter);
    RUN_TEST(AcceptTouchEvent_2, filter);
    RUN_TEST(AcceptTouchEvent_3, filter);
    RUN_TEST(AcceptTouchEvent_4, filter);
  }
}
TestInputEvent::TestInputEvent(TestingInstance* instance)
    : TestCase(instance),
      input_event_interface_(NULL),
      mouse_input_event_interface_(NULL),
      wheel_input_event_interface_(NULL),
      keyboard_input_event_interface_(NULL),
      touch_input_event_interface_(NULL),
      nested_event_(instance->pp_instance()),
      view_rect_(),
      expected_input_event_(0),
      received_expected_event_(false),
      received_finish_message_(false) {
}
TestInputEvent::~TestInputEvent() {
  
  
  std::string js_code;
  js_code += "var plugin = document.getElementById('plugin');"
             "plugin.removeEventListener('message',"
             "                           plugin.wait_for_messages_handler);"
             "delete plugin.wait_for_messages_handler;";
  instance_->EvalScript(js_code);
}
bool TestInputEvent::Init() {
  input_event_interface_ = static_cast<const PPB_InputEvent*>(
      pp::Module::Get()->GetBrowserInterface(PPB_INPUT_EVENT_INTERFACE));
  mouse_input_event_interface_ = static_cast<const PPB_MouseInputEvent*>(
      pp::Module::Get()->GetBrowserInterface(
          PPB_MOUSE_INPUT_EVENT_INTERFACE));
  wheel_input_event_interface_ = static_cast<const PPB_WheelInputEvent*>(
      pp::Module::Get()->GetBrowserInterface(
          PPB_WHEEL_INPUT_EVENT_INTERFACE));
  keyboard_input_event_interface_ = static_cast<const PPB_KeyboardInputEvent*>(
      pp::Module::Get()->GetBrowserInterface(
          PPB_KEYBOARD_INPUT_EVENT_INTERFACE));
  touch_input_event_interface_ = static_cast<const PPB_TouchInputEvent*>(
      pp::Module::Get()->GetBrowserInterface(
          PPB_TOUCH_INPUT_EVENT_INTERFACE));
  bool success =
      input_event_interface_ &&
      mouse_input_event_interface_ &&
      wheel_input_event_interface_ &&
      keyboard_input_event_interface_ &&
      touch_input_event_interface_ &&
      CheckTestingInterface();
  
  
  std::string js_code;
  
  
  
  
  
  js_code += "var plugin = document.getElementById('plugin');"
             "var wait_for_messages_handler = function(message_event) {"
             "  if (!IsTestingMessage(message_event.data) &&"
             "      message_event.data === '" FINISHED_WAITING_MESSAGE "') {"
             "    plugin.postMessage('" FINISHED_WAITING_MESSAGE "');"
             "  }"
             "};"
             "plugin.addEventListener('message', wait_for_messages_handler);"
             
             "plugin.wait_for_messages_handler = wait_for_messages_handler;";
  instance_->EvalScript(js_code);
  return success;
}
pp::InputEvent TestInputEvent::CreateMouseEvent(
    PP_InputEvent_Type type,
    PP_InputEvent_MouseButton buttons) {
  return pp::MouseInputEvent(
      instance_,
      type,
      100,  
      0,  
      buttons,
      GetCenter(view_rect_),
      1,  
      pp::Point());  
}
pp::InputEvent TestInputEvent::CreateWheelEvent() {
  return pp::WheelInputEvent(
      instance_,
      100,  
      0,  
      pp::FloatPoint(1, 2),
      pp::FloatPoint(3, 4),
      PP_TRUE);  
}
pp::InputEvent TestInputEvent::CreateKeyEvent(PP_InputEvent_Type type,
                                              uint32_t key_code,
                                              const std::string& code) {
  return pp::KeyboardInputEvent(
      instance_,
      type,
      100,  
      0,  
      key_code,
      pp::Var(),
      pp::Var(code));
}
pp::InputEvent TestInputEvent::CreateCharEvent(const std::string& text) {
  return pp::KeyboardInputEvent(
      instance_,
      PP_INPUTEVENT_TYPE_CHAR,
      100,  
      0,  
      0,  
      pp::Var(text),
      pp::Var());
}
pp::InputEvent TestInputEvent::CreateTouchEvent(PP_InputEvent_Type type,
                                                const pp::FloatPoint& point) {
  PP_TouchPoint touch_point = PP_MakeTouchPoint();
  touch_point.position = point;
  pp::TouchInputEvent touch_event(instance_, type, 100, 0);
  touch_event.AddTouchPoint(PP_TOUCHLIST_TYPE_TOUCHES, touch_point);
  touch_event.AddTouchPoint(PP_TOUCHLIST_TYPE_CHANGEDTOUCHES, touch_point);
  touch_event.AddTouchPoint(PP_TOUCHLIST_TYPE_TARGETTOUCHES, touch_point);
  return touch_event;
}
void TestInputEvent::PostMessageBarrier() {
  received_finish_message_ = false;
  instance_->PostMessage(pp::Var(FINISHED_WAITING_MESSAGE));
  testing_interface_->RunMessageLoop(instance_->pp_instance());
  nested_event_.Wait();
}
bool TestInputEvent::SimulateInputEvent(
    const pp::InputEvent& input_event) {
  expected_input_event_ = pp::InputEvent(input_event.pp_resource());
  received_expected_event_ = false;
  testing_interface_->SimulateInputEvent(instance_->pp_instance(),
                                         input_event.pp_resource());
  PostMessageBarrier();
  return received_expected_event_;
}
bool TestInputEvent::AreEquivalentEvents(PP_Resource received,
                                         PP_Resource expected) {
  if (!input_event_interface_->IsInputEvent(received) ||
      !input_event_interface_->IsInputEvent(expected)) {
    return false;
  }
  
  
  int32_t received_type = input_event_interface_->GetType(received);
  int32_t expected_type = input_event_interface_->GetType(expected);
  if (received_type != expected_type) {
    
    if (expected_type != PP_INPUTEVENT_TYPE_KEYDOWN &&
        received_type != PP_INPUTEVENT_TYPE_RAWKEYDOWN) {
      return false;
    }
  }
  
  switch (input_event_interface_->GetType(received)) {
    case PP_INPUTEVENT_TYPE_MOUSEDOWN:
    case PP_INPUTEVENT_TYPE_MOUSEUP:
    case PP_INPUTEVENT_TYPE_MOUSEMOVE:
    case PP_INPUTEVENT_TYPE_MOUSEENTER:
    case PP_INPUTEVENT_TYPE_MOUSELEAVE:
      
      
      return
          mouse_input_event_interface_->GetButton(received) ==
          mouse_input_event_interface_->GetButton(expected) &&
          mouse_input_event_interface_->GetClickCount(received) ==
          mouse_input_event_interface_->GetClickCount(expected);
    case PP_INPUTEVENT_TYPE_WHEEL:
      return
          pp::FloatPoint(wheel_input_event_interface_->GetDelta(received)) ==
          pp::FloatPoint(wheel_input_event_interface_->GetDelta(expected)) &&
          pp::FloatPoint(wheel_input_event_interface_->GetTicks(received)) ==
          pp::FloatPoint(wheel_input_event_interface_->GetTicks(expected)) &&
          wheel_input_event_interface_->GetScrollByPage(received) ==
          wheel_input_event_interface_->GetScrollByPage(expected);
    case PP_INPUTEVENT_TYPE_RAWKEYDOWN:
    case PP_INPUTEVENT_TYPE_KEYDOWN:
    case PP_INPUTEVENT_TYPE_KEYUP:
      return
          keyboard_input_event_interface_->GetKeyCode(received) ==
          keyboard_input_event_interface_->GetKeyCode(expected);
    case PP_INPUTEVENT_TYPE_CHAR:
      return
          keyboard_input_event_interface_->GetKeyCode(received) ==
          keyboard_input_event_interface_->GetKeyCode(expected) &&
          pp::Var(pp::PASS_REF,
              keyboard_input_event_interface_->GetCharacterText(received)) ==
          pp::Var(pp::PASS_REF,
              keyboard_input_event_interface_->GetCharacterText(expected));
    case PP_INPUTEVENT_TYPE_TOUCHSTART:
    case PP_INPUTEVENT_TYPE_TOUCHMOVE:
    case PP_INPUTEVENT_TYPE_TOUCHEND:
    case PP_INPUTEVENT_TYPE_TOUCHCANCEL: {
      if (!touch_input_event_interface_->IsTouchInputEvent(received) ||
          !touch_input_event_interface_->IsTouchInputEvent(expected))
        return false;
      uint32_t touch_count = touch_input_event_interface_->GetTouchCount(
          received, PP_TOUCHLIST_TYPE_TOUCHES);
      if (touch_count <= 0 ||
          touch_count != touch_input_event_interface_->GetTouchCount(expected,
              PP_TOUCHLIST_TYPE_TOUCHES))
        return false;
      for (uint32_t i = 0; i < touch_count; ++i) {
        PP_TouchPoint expected_point = touch_input_event_interface_->
            GetTouchByIndex(expected, PP_TOUCHLIST_TYPE_TOUCHES, i);
        PP_TouchPoint received_point = touch_input_event_interface_->
            GetTouchByIndex(received, PP_TOUCHLIST_TYPE_TOUCHES, i);
        if (expected_point.id != received_point.id ||
            expected_point.radius != received_point.radius ||
            expected_point.rotation_angle != received_point.rotation_angle ||
            expected_point.pressure != received_point.pressure)
          return false;
        if (expected_point.position.x != received_point.position.x ||
            expected_point.position.y != received_point.position.y)
          return false;
      }
      return true;
    }
    default:
      break;
  }
  return false;
}
bool TestInputEvent::HandleInputEvent(const pp::InputEvent& input_event) {
  
  
  if (!received_expected_event_) {
    received_expected_event_ = AreEquivalentEvents(
        input_event.pp_resource(),
        expected_input_event_.pp_resource());
  }
  
  return true;
}
void TestInputEvent::HandleMessage(const pp::Var& message_data) {
  if (message_data.is_string() &&
      (message_data.AsString() == FINISHED_WAITING_MESSAGE)) {
    testing_interface_->QuitMessageLoop(instance_->pp_instance());
    received_finish_message_ = true;
    nested_event_.Signal();
  }
}
void TestInputEvent::DidChangeView(const pp::View& view) {
  view_rect_ = view.GetRect();
}
std::string TestInputEvent::TestEvents() {
  
  input_event_interface_->RequestInputEvents(instance_->pp_instance(),
                                             PP_INPUTEVENT_CLASS_MOUSE |
                                             PP_INPUTEVENT_CLASS_WHEEL |
                                             PP_INPUTEVENT_CLASS_KEYBOARD |
                                             PP_INPUTEVENT_CLASS_TOUCH);
  PostMessageBarrier();
  
  ASSERT_TRUE(
      SimulateInputEvent(CreateMouseEvent(PP_INPUTEVENT_TYPE_MOUSEDOWN,
                                          PP_INPUTEVENT_MOUSEBUTTON_LEFT)));
  ASSERT_TRUE(
      SimulateInputEvent(CreateWheelEvent()));
  ASSERT_TRUE(
      SimulateInputEvent(CreateKeyEvent(PP_INPUTEVENT_TYPE_KEYDOWN,
                                        kSpaceChar, kSpaceCode)));
  ASSERT_TRUE(
      SimulateInputEvent(CreateCharEvent(kSpaceString)));
  ASSERT_TRUE(SimulateInputEvent(CreateTouchEvent(PP_INPUTEVENT_TYPE_TOUCHSTART,
                                                  pp::FloatPoint(12, 23))));
  
  input_event_interface_->ClearInputEventRequest(instance_->pp_instance(),
                                                 PP_INPUTEVENT_CLASS_WHEEL |
                                                 PP_INPUTEVENT_CLASS_KEYBOARD);
  PostMessageBarrier();
  
  ASSERT_TRUE(
      SimulateInputEvent(CreateMouseEvent(PP_INPUTEVENT_TYPE_MOUSEDOWN,
                                          PP_INPUTEVENT_MOUSEBUTTON_LEFT)));
  ASSERT_FALSE(
      SimulateInputEvent(CreateWheelEvent()));
  ASSERT_FALSE(
      SimulateInputEvent(CreateKeyEvent(PP_INPUTEVENT_TYPE_KEYDOWN,
                                        kSpaceChar, kSpaceCode)));
  ASSERT_FALSE(
      SimulateInputEvent(CreateCharEvent(kSpaceString)));
  PASS();
}
std::string TestInputEvent::TestAcceptTouchEvent_1() {
  
  
  
  
  
  
  
  
  
  
  
  instance_->set_remove_plugin(false);
  input_event_interface_->RequestInputEvents(instance_->pp_instance(),
                                             PP_INPUTEVENT_CLASS_MOUSE |
                                             PP_INPUTEVENT_CLASS_WHEEL |
                                             PP_INPUTEVENT_CLASS_KEYBOARD |
                                             PP_INPUTEVENT_CLASS_TOUCH);
  PASS();
}
std::string TestInputEvent::TestAcceptTouchEvent_2() {
  
  instance_->set_remove_plugin(false);
  input_event_interface_->RequestInputEvents(instance_->pp_instance(),
                                             PP_INPUTEVENT_CLASS_MOUSE |
                                             PP_INPUTEVENT_CLASS_WHEEL |
                                             PP_INPUTEVENT_CLASS_KEYBOARD |
                                             PP_INPUTEVENT_CLASS_TOUCH);
  input_event_interface_->ClearInputEventRequest(instance_->pp_instance(),
                                                 PP_INPUTEVENT_CLASS_TOUCH);
  PASS();
}
std::string TestInputEvent::TestAcceptTouchEvent_3() {
  
  instance_->set_remove_plugin(false);
  input_event_interface_->RequestInputEvents(instance_->pp_instance(),
                                             PP_INPUTEVENT_CLASS_MOUSE |
                                             PP_INPUTEVENT_CLASS_WHEEL |
                                             PP_INPUTEVENT_CLASS_KEYBOARD);
  input_event_interface_->RequestFilteringInputEvents(instance_->pp_instance(),
      PP_INPUTEVENT_CLASS_TOUCH);
  PASS();
}
std::string TestInputEvent::TestAcceptTouchEvent_4() {
  
  instance_->set_remove_plugin(false);
  input_event_interface_->RequestInputEvents(instance_->pp_instance(),
                                             PP_INPUTEVENT_CLASS_MOUSE |
                                             PP_INPUTEVENT_CLASS_WHEEL |
                                             PP_INPUTEVENT_CLASS_KEYBOARD);
  input_event_interface_->RequestInputEvents(instance_->pp_instance(),
                                             PP_INPUTEVENT_CLASS_TOUCH);
  PASS();
}