summaryrefslogtreecommitdiffstats
path: root/ppapi/tests/test_resource_array.cc
blob: 333505ef65f05448b94bba37c5b9c07ce016352a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ppapi/tests/test_resource_array.h"

#include "ppapi/cpp/dev/resource_array_dev.h"
#include "ppapi/cpp/image_data.h"
#include "ppapi/cpp/input_event.h"
#include "ppapi/tests/testing_instance.h"

REGISTER_TEST_CASE(ResourceArray);

namespace {

pp::InputEvent CreateMouseEvent(pp::Instance* instance,
                                PP_InputEvent_Type type,
                                PP_InputEvent_MouseButton buttons) {
  return pp::MouseInputEvent(
      instance,
      type,
      100,  // time_stamp
      0,  // modifiers
      buttons,
      pp::Point(),  // position
      1,  // click count
      pp::Point());  // movement
}

pp::ImageData CreateImageData(pp::Instance* instance) {
  return pp::ImageData(
      instance,
      PP_IMAGEDATAFORMAT_RGBA_PREMUL,
      pp::Size(1, 1),
      true);
}

}  // namespace

TestResourceArray::TestResourceArray(TestingInstance* instance)
    : TestCase(instance) {
}

TestResourceArray::~TestResourceArray() {
}

void TestResourceArray::RunTests(const std::string& filter) {
  RUN_TEST(Basics, filter);
  RUN_TEST(OutOfRangeAccess, filter);
  RUN_TEST(EmptyArray, filter);
  RUN_TEST(InvalidElement, filter);
}

std::string TestResourceArray::TestBasics() {
  pp::InputEvent mouse_event_1 = CreateMouseEvent(
      instance_, PP_INPUTEVENT_TYPE_MOUSEDOWN, PP_INPUTEVENT_MOUSEBUTTON_LEFT);
  pp::InputEvent mouse_event_2 = CreateMouseEvent(
      instance_, PP_INPUTEVENT_TYPE_MOUSEUP, PP_INPUTEVENT_MOUSEBUTTON_RIGHT);
  pp::ImageData image_data = CreateImageData(instance_);

  PP_Resource elements[] = {
    mouse_event_1.pp_resource(),
    mouse_event_2.pp_resource(),
    image_data.pp_resource()
  };
  size_t size = sizeof(elements) / sizeof(elements[0]);

  pp::ResourceArray_Dev resource_array(instance_, elements, size);

  ASSERT_EQ(size, resource_array.size());
  for (uint32_t index = 0; index < size; ++index)
    ASSERT_EQ(elements[index], resource_array[index]);

  PASS();
}

std::string TestResourceArray::TestOutOfRangeAccess() {
  pp::InputEvent mouse_event_1 = CreateMouseEvent(
      instance_, PP_INPUTEVENT_TYPE_MOUSEDOWN, PP_INPUTEVENT_MOUSEBUTTON_LEFT);
  pp::InputEvent mouse_event_2 = CreateMouseEvent(
      instance_, PP_INPUTEVENT_TYPE_MOUSEUP, PP_INPUTEVENT_MOUSEBUTTON_RIGHT);
  pp::ImageData image_data = CreateImageData(instance_);

  PP_Resource elements[] = {
    mouse_event_1.pp_resource(),
    mouse_event_2.pp_resource(),
    image_data.pp_resource()
  };
  size_t size = sizeof(elements) / sizeof(elements[0]);

  pp::ResourceArray_Dev resource_array(instance_, elements, size);
  ASSERT_EQ(0, resource_array[size]);
  ASSERT_EQ(0, resource_array[size + 1]);

  PASS();
}

std::string TestResourceArray::TestEmptyArray() {
  pp::ResourceArray_Dev resource_array(instance_, NULL, 0);
  ASSERT_EQ(0, resource_array.size());
  PASS();
}

std::string TestResourceArray::TestInvalidElement() {
  pp::InputEvent mouse_event = CreateMouseEvent(
      instance_, PP_INPUTEVENT_TYPE_MOUSEDOWN, PP_INPUTEVENT_MOUSEBUTTON_LEFT);
  pp::ImageData image_data = CreateImageData(instance_);

  PP_Resource elements[] = {
    mouse_event.pp_resource(),
    0,
    image_data.pp_resource()
  };
  size_t size = sizeof(elements) / sizeof(elements[0]);

  pp::ResourceArray_Dev resource_array(instance_, elements, size);

  ASSERT_EQ(size, resource_array.size());
  for (uint32_t index = 0; index < size; ++index)
    ASSERT_EQ(elements[index], resource_array[index]);

  PASS();
}