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_resource_array.h" 6 7 #include "ppapi/cpp/dev/resource_array_dev.h" 8 #include "ppapi/cpp/image_data.h" 9 #include "ppapi/cpp/input_event.h" 10 #include "ppapi/tests/testing_instance.h" 11 12 REGISTER_TEST_CASE(ResourceArray); 13 14 namespace { 15 16 pp::InputEvent CreateMouseEvent(pp::Instance* instance, 17 PP_InputEvent_Type type, 18 PP_InputEvent_MouseButton buttons) { 19 return pp::MouseInputEvent( 20 instance, 21 type, 22 100, // time_stamp 23 0, // modifiers 24 buttons, 25 pp::Point(), // position 26 1, // click count 27 pp::Point()); // movement 28 } 29 30 pp::ImageData CreateImageData(pp::Instance* instance) { 31 return pp::ImageData( 32 instance, 33 PP_IMAGEDATAFORMAT_RGBA_PREMUL, 34 pp::Size(1, 1), 35 true); 36 } 37 38 } // namespace 39 40 TestResourceArray::TestResourceArray(TestingInstance* instance) 41 : TestCase(instance) { 42 } 43 44 TestResourceArray::~TestResourceArray() { 45 } 46 47 void TestResourceArray::RunTests(const std::string& filter) { 48 RUN_TEST(Basics, filter); 49 RUN_TEST(OutOfRangeAccess, filter); 50 RUN_TEST(EmptyArray, filter); 51 RUN_TEST(InvalidElement, filter); 52 } 53 54 std::string TestResourceArray::TestBasics() { 55 pp::InputEvent mouse_event_1 = CreateMouseEvent( 56 instance_, PP_INPUTEVENT_TYPE_MOUSEDOWN, PP_INPUTEVENT_MOUSEBUTTON_LEFT); 57 pp::InputEvent mouse_event_2 = CreateMouseEvent( 58 instance_, PP_INPUTEVENT_TYPE_MOUSEUP, PP_INPUTEVENT_MOUSEBUTTON_RIGHT); 59 pp::ImageData image_data = CreateImageData(instance_); 60 61 PP_Resource elements[] = { 62 mouse_event_1.pp_resource(), 63 mouse_event_2.pp_resource(), 64 image_data.pp_resource() 65 }; 66 size_t size = sizeof(elements) / sizeof(elements[0]); 67 68 pp::ResourceArray_Dev resource_array(instance_, elements, size); 69 70 ASSERT_EQ(size, resource_array.size()); 71 for (uint32_t index = 0; index < size; ++index) 72 ASSERT_EQ(elements[index], resource_array[index]); 73 74 PASS(); 75 } 76 77 std::string TestResourceArray::TestOutOfRangeAccess() { 78 pp::InputEvent mouse_event_1 = CreateMouseEvent( 79 instance_, PP_INPUTEVENT_TYPE_MOUSEDOWN, PP_INPUTEVENT_MOUSEBUTTON_LEFT); 80 pp::InputEvent mouse_event_2 = CreateMouseEvent( 81 instance_, PP_INPUTEVENT_TYPE_MOUSEUP, PP_INPUTEVENT_MOUSEBUTTON_RIGHT); 82 pp::ImageData image_data = CreateImageData(instance_); 83 84 PP_Resource elements[] = { 85 mouse_event_1.pp_resource(), 86 mouse_event_2.pp_resource(), 87 image_data.pp_resource() 88 }; 89 size_t size = sizeof(elements) / sizeof(elements[0]); 90 91 pp::ResourceArray_Dev resource_array(instance_, elements, size); 92 ASSERT_EQ(0, resource_array[size]); 93 ASSERT_EQ(0, resource_array[size + 1]); 94 95 PASS(); 96 } 97 98 std::string TestResourceArray::TestEmptyArray() { 99 pp::ResourceArray_Dev resource_array(instance_, NULL, 0); 100 ASSERT_EQ(0, resource_array.size()); 101 PASS(); 102 } 103 104 std::string TestResourceArray::TestInvalidElement() { 105 pp::InputEvent mouse_event = CreateMouseEvent( 106 instance_, PP_INPUTEVENT_TYPE_MOUSEDOWN, PP_INPUTEVENT_MOUSEBUTTON_LEFT); 107 pp::ImageData image_data = CreateImageData(instance_); 108 109 PP_Resource elements[] = { 110 mouse_event.pp_resource(), 111 0, 112 image_data.pp_resource() 113 }; 114 size_t size = sizeof(elements) / sizeof(elements[0]); 115 116 pp::ResourceArray_Dev resource_array(instance_, elements, size); 117 118 ASSERT_EQ(size, resource_array.size()); 119 for (uint32_t index = 0; index < size; ++index) 120 ASSERT_EQ(elements[index], resource_array[index]); 121 122 PASS(); 123 } 124