1 // Copyright (c) 2011 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.
5 // Functions and constants for test registration and setup.
7 // NOTE: These must be implemented by the tester:
9 // - SetupPluginInterfaces()
13 // void MyCallback(void* user_data, int32_t result) { ... }
15 // void TestPPBFoo() {
17 // PP_Resource my_resource = PPBFoo()->Create(kInvalidInstance);
18 // EXPECT(my_resource == kInvalidResource);
21 // PP_CompletionCallback testable_callback =
22 // MakeTestableCompletionCallback("MyCallback", MyCallback, NULL);
23 // int32_t pp_error = PPBFoo()->AsyncFunction(testable_callback);
24 // EXPECT(pp_error == PP_OK_COMPLETIONPENDING);
29 // void SetupTests() {
30 // RegisterTest("TestPPBFoo", TestPPBFoo);
33 // const PPP_Bar ppp_bar_interface = { ... };
35 // void SetupPluginInterface() {
36 // RegisterPluginInterface(PPP_BAR_INTERFACE, &ppp_bar_interface);
40 #ifndef NATIVE_CLIENT_TESTS_PPAPI_TEST_PPB_TEMPLATE_TEST_INTERFACE_H
41 #define NATIVE_CLIENT_TESTS_PPAPI_TEST_PPB_TEMPLATE_TEST_INTERFACE_H
48 #include "native_client/src/include/nacl_string.h"
50 #include "ppapi/c/pp_completion_callback.h"
51 #include "ppapi/c/pp_instance.h"
52 #include "ppapi/c/pp_module.h"
53 #include "ppapi/c/pp_point.h"
54 #include "ppapi/c/pp_resource.h"
55 #include "ppapi/c/pp_var.h"
56 #include "ppapi/c/ppb_image_data.h"
61 ////////////////////////////////////////////////////////////////////////////////
62 // These must be implemented by the tester
63 ////////////////////////////////////////////////////////////////////////////////
65 // Use RegisterTest() to register each TestFunction.
67 // Use RegisterPluginInterface() to register custom PPP_ interfaces other than
68 // PPP_Instance that is required and provided by default.
69 void SetupPluginInterfaces();
71 ////////////////////////////////////////////////////////////////////////////////
73 ////////////////////////////////////////////////////////////////////////////////
75 // Registers test_function, so it is callable from JS using
76 // plugin.postMessage(test_name);
77 typedef void (*TestFunction
)();
78 void RegisterTest(nacl::string test_name
, TestFunction test_function
);
80 // Registers ppp_interface, so it is returned by PPP_GetInterface().
81 void RegisterPluginInterface(const char* interface_name
,
82 const void* ppp_interface
);
84 // Helper for creating user callbacks whose invocation will be reported to JS.
85 // Callback setting allows for synchronous completion to make it easier to
86 // test error conditions.
87 // WARNING: Do not reuse this callback if the operation that took it as an arg
88 // returned PP_OK_COMPLETIONPENDING. The wrapper allocates data on creation
89 // and then deallocates it when the callback is invoked.
90 PP_CompletionCallback
MakeTestableCompletionCallback(
91 const char* callback_name
, // will be postmessage'ed to JS
92 PP_CompletionCallback_Func func
,
94 PP_CompletionCallback
MakeTestableCompletionCallback(
95 const char* callback_name
, // will be postmessage'ed to JS
96 PP_CompletionCallback_Func func
);
98 // Uses PPB_Messaging interface to post "test_name:message".
99 void PostTestMessage(nacl::string test_name
, nacl::string message
);
101 // Make a STRING var.
102 PP_Var
PP_MakeString(const char* s
);
104 // Convert var into printable string (for debuggin)
105 nacl::string
StringifyVar(const PP_Var
& var
);
107 // Use to verify the result of a test and report failures.
108 #define EXPECT(expr) do { \
111 snprintf(error, sizeof(error), \
112 "ERROR at %s:%d: %s\n", __FILE__, __LINE__, #expr); \
113 fprintf(stderr, "%s", error); \
114 PostTestMessage(__FUNCTION__, error); \
118 // Check expected value of INT32 var.
119 #define EXPECT_VAR_INT(var, val) \
120 EXPECT(var.type == PP_VARTYPE_INT32 && var.value.as_int == val)
122 // Check expected value of STRING var (val is 'char*')
123 #define EXPECT_VAR_STRING(var, val) \
125 EXPECT(var.type == PP_VARTYPE_STRING); \
126 uint32_t dummy_size; \
127 const char* expected = PPBVar()->VarToUtf8(var, &dummy_size); \
128 EXPECT(0 == strcmp(expected, val)); \
131 // Check expected value of BOOL var.
132 #define EXPECT_VAR_BOOL(var, val) \
133 EXPECT(var.type == PP_VARTYPE_BOOL && var.value.as_bool == val)
135 // Use to report success.
136 #define TEST_PASSED PostTestMessage(__FUNCTION__, "PASSED");
138 #define TEST_FAILED EXPECT(false)
140 // Handy for use with LOG_TO_BROWSER() convert arbitrary objects into strings.
141 template<typename T
> nacl::string
toString(T v
) {
147 // Log message for debugging or progress reporting purposes.
148 // If you use this with nacltest.js::expectMessageSequence
149 // it will not interfere with output used for correctness checking.
150 #define LOG_TO_BROWSER(message) PostTestMessage("@", message)
152 // Cause a crash in a way that is guaranteed not to get optimized out by LLVM.
153 #define CRASH *(volatile int *) 0 = 0;
155 // Use this constant for stress testing
156 // (i.e. creating and using a large number of resources).
157 const int kManyResources
= 1000;
159 ////////////////////////////////////////////////////////////////////////////////
161 ////////////////////////////////////////////////////////////////////////////////
163 const PP_Instance kInvalidInstance
= 0;
164 const PP_Module kInvalidModule
= 0;
165 const PP_Resource kInvalidResource
= 0;
167 // These should not exist.
168 // Chrome uses the bottom 2 bits to differentiate between different id types.
169 // 00 - module, 01 - instance, 10 - resource, 11 - var.
170 const PP_Instance kNotAnInstance
= 0xFFFFF0;
171 const PP_Resource kNotAResource
= 0xAAAAA0;
173 const PP_Point kOrigin
= PP_MakePoint(0, 0);
175 // Interface pointers and ids corresponding to this plugin;
176 // set at initialization/creation.
177 PP_Instance
pp_instance();
178 PP_Module
pp_module();
180 // If you are providing your own version of PPP_Instance::DidCreate
181 // call this function to ensure proper test set-up.
182 PP_Bool
DidCreateDefault(PP_Instance instance
,
183 uint32_t argc
, const char* argn
[], const char* argv
[]);
184 // Other default implementations of the required PPP_Instance functions.
185 void DidDestroyDefault(PP_Instance instance
);
186 void DidChangeViewDefault(PP_Instance instance
, PP_Resource view
);
187 void DidChangeFocusDefault(PP_Instance instance
, PP_Bool has_focus
);
188 PP_Bool
HandleDocumentLoadDefault(PP_Instance instance
, PP_Resource url_loader
);
191 bool IsSizeInRange(PP_Size size
, PP_Size min_size
, PP_Size max_size
);
192 bool IsSizeEqual(PP_Size size
, PP_Size expected
);
193 bool IsRectEqual(PP_Rect position
, PP_Rect expected
);
195 // TODO(polina, nfullagar): allow specification of non-premultipled colors
196 // and provide alpha premultiplcation in FormatColor(). This will be required
197 // when future PPAPI pixel formats are extended to include non-premultipled
200 struct ColorPremul
{ uint32_t A
, R
, G
, B
; }; // Use premultipled Alpha.
201 const ColorPremul kSheerRed
= { 0x88, 0x88, 0x00, 0x00 };
202 const ColorPremul kSheerBlue
= { 0x88, 0x00, 0x00, 0x88 };
203 const ColorPremul kSheerGray
= { 0x77, 0x55, 0x55, 0x55 };
204 const ColorPremul kOpaqueGreen
= { 0xFF, 0x00, 0xFF, 0x00 };
205 const ColorPremul kOpaqueBlack
= { 0xFF, 0x00, 0x00, 0x00 };
206 const ColorPremul kOpaqueWhite
= { 0xFF, 0xFF, 0xFF, 0xFF };
207 const ColorPremul kOpaqueYellow
= { 0xFF, 0xFF, 0xFF, 0x00 };
208 const int kBytesPerPixel
= sizeof(uint32_t); // 4 bytes for BGRA or RGBA.
210 // Assumes premultipled Alpha.
211 uint32_t FormatColor(PP_ImageDataFormat format
, ColorPremul color
);
213 // Creates image data resource and bitmap for a rectangular region of |size|
214 // and |pixel_color|.
215 PP_Resource
CreateImageData(PP_Size size
, ColorPremul pixel_color
, void** bmp
);
218 // Checks if the image rect of |color| and |size| is on the screen at |origin|.
219 bool IsImageRectOnScreen(PP_Resource graphics2d
,
224 #endif // NATIVE_CLIENT_TESTS_PPAPI_TEST_PPB_TEMPLATE_TEST_INTERFACE_H