Home | History | Annotate | Download | only in ppapi_test_lib
      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.
      4 //
      5 // Functions and constants for test registration and setup.
      6 //
      7 // NOTE: These must be implemented by the tester:
      8 // - SetupTests()
      9 // - SetupPluginInterfaces()
     10 //
     11 // Sample Usage:
     12 //
     13 //   void MyCallback(void* user_data, int32_t result) { ... }
     14 //
     15 //   void TestPPBFoo() {
     16 //     // sync test case
     17 //     PP_Resource my_resource = PPBFoo()->Create(kInvalidInstance);
     18 //     EXPECT(my_resource == kInvalidResource);
     19 //
     20 //     // async test case
     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);
     25 //
     26 //     TEST_PASSED;
     27 //   }
     28 //
     29 //   void SetupTests() {
     30 //     RegisterTest("TestPPBFoo", TestPPBFoo);
     31 //   }
     32 //
     33 //   const PPP_Bar ppp_bar_interface = { ... };
     34 //
     35 //   void SetupPluginInterface() {
     36 //     RegisterPluginInterface(PPP_BAR_INTERFACE, &ppp_bar_interface);
     37 //   }
     38 //
     39 
     40 #ifndef NATIVE_CLIENT_TESTS_PPAPI_TEST_PPB_TEMPLATE_TEST_INTERFACE_H
     41 #define NATIVE_CLIENT_TESTS_PPAPI_TEST_PPB_TEMPLATE_TEST_INTERFACE_H
     42 
     43 #include <stdio.h>
     44 #include <limits>
     45 
     46 #include <sstream>
     47 
     48 #include "native_client/src/include/nacl_string.h"
     49 
     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"
     57 
     58 struct PP_Rect;
     59 struct PP_Size;
     60 
     61 ////////////////////////////////////////////////////////////////////////////////
     62 // These must be implemented by the tester
     63 ////////////////////////////////////////////////////////////////////////////////
     64 
     65 // Use RegisterTest() to register each TestFunction.
     66 void SetupTests();
     67 // Use RegisterPluginInterface() to register custom PPP_ interfaces other than
     68 // PPP_Instance that is required and provided by default.
     69 void SetupPluginInterfaces();
     70 
     71 ////////////////////////////////////////////////////////////////////////////////
     72 // Test helpers
     73 ////////////////////////////////////////////////////////////////////////////////
     74 
     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);
     79 
     80 // Registers ppp_interface, so it is returned by PPP_GetInterface().
     81 void RegisterPluginInterface(const char* interface_name,
     82                              const void* ppp_interface);
     83 
     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,
     93     void* user_data);
     94 PP_CompletionCallback MakeTestableCompletionCallback(
     95     const char* callback_name,  // will be postmessage'ed to JS
     96     PP_CompletionCallback_Func func);
     97 
     98 // Uses PPB_Messaging interface to post "test_name:message".
     99 void PostTestMessage(nacl::string test_name, nacl::string message);
    100 
    101 // Make a STRING var.
    102 PP_Var PP_MakeString(const char* s);
    103 
    104 // Convert var into printable string (for debuggin)
    105 nacl::string StringifyVar(const PP_Var& var);
    106 
    107 // Use to verify the result of a test and report failures.
    108 #define EXPECT(expr) do { \
    109   if (!(expr)) { \
    110     char error[1024]; \
    111     snprintf(error, sizeof(error), \
    112              "ERROR at %s:%d: %s\n", __FILE__, __LINE__, #expr); \
    113     fprintf(stderr, "%s", error); \
    114     PostTestMessage(__FUNCTION__, error); \
    115   } \
    116 } while (0)
    117 
    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)
    121 
    122 // Check expected value of STRING var (val is 'char*')
    123 #define EXPECT_VAR_STRING(var, val) \
    124   do { \
    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)); \
    129   } while (0)
    130 
    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)
    134 
    135 // Use to report success.
    136 #define TEST_PASSED PostTestMessage(__FUNCTION__, "PASSED");
    137 // Or failure.
    138 #define TEST_FAILED EXPECT(false)
    139 
    140 // Handy for use with LOG_TO_BROWSER() convert arbitrary objects into strings.
    141 template<typename T> nacl::string toString(T v) {
    142   std::stringstream s;
    143   s << v;
    144   return s.str();
    145 }
    146 
    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)
    151 
    152 // Cause a crash in a way that is guaranteed not to get optimized out by LLVM.
    153 #define CRASH *(volatile int *) 0 = 0;
    154 
    155 // Use this constant for stress testing
    156 // (i.e. creating and using a large number of resources).
    157 const int kManyResources = 1000;
    158 
    159 ////////////////////////////////////////////////////////////////////////////////
    160 // PPAPI Helpers
    161 ////////////////////////////////////////////////////////////////////////////////
    162 
    163 const PP_Instance kInvalidInstance = 0;
    164 const PP_Module kInvalidModule = 0;
    165 const PP_Resource kInvalidResource = 0;
    166 
    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;
    172 
    173 const PP_Point kOrigin = PP_MakePoint(0, 0);
    174 
    175 // Interface pointers and ids corresponding to this plugin;
    176 // set at initialization/creation.
    177 PP_Instance pp_instance();
    178 PP_Module pp_module();
    179 
    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);
    189 
    190 
    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);
    194 
    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
    198 // or ignored alpha.
    199 
    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.
    209 
    210 // Assumes premultipled Alpha.
    211 uint32_t FormatColor(PP_ImageDataFormat format, ColorPremul color);
    212 
    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);
    216 
    217 
    218 // Checks if the image rect of |color| and |size| is on the screen at |origin|.
    219 bool IsImageRectOnScreen(PP_Resource graphics2d,
    220                          PP_Point origin,
    221                          PP_Size size,
    222                          ColorPremul color);
    223 
    224 #endif  // NATIVE_CLIENT_TESTS_PPAPI_TEST_PPB_TEMPLATE_TEST_INTERFACE_H
    225