OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Native Client 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_resource.h" |
| 54 #include "ppapi/c/pp_var.h" |
| 55 |
| 56 //////////////////////////////////////////////////////////////////////////////// |
| 57 // These must be implemented by the tester |
| 58 //////////////////////////////////////////////////////////////////////////////// |
| 59 |
| 60 // Use RegisterTest() to register each TestFunction. |
| 61 void SetupTests(); |
| 62 // Use RegisterPluginInterface() to register custom PPP_ interfaces other than |
| 63 // PPP_Instance that is required and provided by default. |
| 64 void SetupPluginInterfaces(); |
| 65 |
| 66 //////////////////////////////////////////////////////////////////////////////// |
| 67 // Test helpers |
| 68 //////////////////////////////////////////////////////////////////////////////// |
| 69 |
| 70 // Registers test_function, so it is callable from JS using |
| 71 // plugin.postMessage(test_name); |
| 72 typedef void (*TestFunction)(); |
| 73 void RegisterTest(nacl::string test_name, TestFunction test_function); |
| 74 |
| 75 // Registers ppp_interface, so it is returned by PPP_GetInterface(). |
| 76 void RegisterPluginInterface(const char* interface_name, |
| 77 const void* ppp_interface); |
| 78 |
| 79 // Helper for creating user callbacks whose invocation will be reported to JS. |
| 80 // Callback setting allows for synchronous completion to make it easier to |
| 81 // test error conditions. |
| 82 // WARNING: Do not reuse this callback if the operation that took it as an arg |
| 83 // returned PP_OK_COMPLETIONPENDING. The wrapper allocates data on creation |
| 84 // and then deallocates it when the callback is invoked. |
| 85 PP_CompletionCallback MakeTestableCompletionCallback( |
| 86 const char* callback_name, // will be postmessage'ed to JS |
| 87 PP_CompletionCallback_Func func, |
| 88 void* user_data); |
| 89 PP_CompletionCallback MakeTestableCompletionCallback( |
| 90 const char* callback_name, // will be postmessage'ed to JS |
| 91 PP_CompletionCallback_Func func); |
| 92 |
| 93 // Uses PPB_Messaging interface to post "test_name:message". |
| 94 void PostTestMessage(nacl::string test_name, nacl::string message); |
| 95 |
| 96 // Make a STRING var. |
| 97 PP_Var PP_MakeString(const char* s); |
| 98 |
| 99 // Convert var into printable string (for debuggin) |
| 100 nacl::string StringifyVar(const PP_Var& var); |
| 101 |
| 102 // Use to verify the result of a test and report failures. |
| 103 #define EXPECT(expr) do { \ |
| 104 if (!(expr)) { \ |
| 105 char error[1024]; \ |
| 106 snprintf(error, sizeof(error), \ |
| 107 "ERROR at %s:%d: %s\n", __FILE__, __LINE__, #expr); \ |
| 108 fprintf(stderr, "%s", error); \ |
| 109 PostTestMessage(__FUNCTION__, error); \ |
| 110 } \ |
| 111 } while (0) |
| 112 |
| 113 // Check expected value of INT32 var. |
| 114 #define EXPECT_VAR_INT(var, val) \ |
| 115 EXPECT(var.type == PP_VARTYPE_INT32 && var.value.as_int == val) |
| 116 |
| 117 // Check expected value of STRING var (val is 'char*') |
| 118 #define EXPECT_VAR_STRING(var, val) \ |
| 119 do { \ |
| 120 EXPECT(var.type == PP_VARTYPE_STRING); \ |
| 121 uint32_t dummy_size; \ |
| 122 const char* expected = PPBVar()->VarToUtf8(var, &dummy_size); \ |
| 123 EXPECT(0 == strcmp(expected, val)); \ |
| 124 } while (0) |
| 125 |
| 126 // Check expected value of BOOL var. |
| 127 #define EXPECT_VAR_BOOL(var, val) \ |
| 128 EXPECT(var.type == PP_VARTYPE_BOOL && var.value.as_bool == val) |
| 129 |
| 130 // Use to report success. |
| 131 #define TEST_PASSED PostTestMessage(__FUNCTION__, "PASSED"); |
| 132 // Or failure. |
| 133 #define TEST_FAILED EXPECT(false) |
| 134 |
| 135 // Handy for use with LOG_TO_BROWSER() convert arbitrary objects into strings. |
| 136 template<typename T> nacl::string toString(T v) { |
| 137 std::stringstream s; |
| 138 s << v; |
| 139 return s.str(); |
| 140 } |
| 141 |
| 142 // Log message for debugging or progress reporting purposes. |
| 143 // If you use this with nacltest.js::expectMessageSequence |
| 144 // it will not interfere with output used for correctness checking. |
| 145 #define LOG_TO_BROWSER(message) PostTestMessage("@", message) |
| 146 |
| 147 // Cause a crash in a way that is guaranteed not to get optimized out by LLVM. |
| 148 #define CRASH *(volatile int *) 0 = 0; |
| 149 |
| 150 |
| 151 // Use this constant for stress testing |
| 152 // (i.e. creating and using a large number of resources). |
| 153 const int kManyResources = 1000; |
| 154 |
| 155 const PP_Instance kInvalidInstance = 0; |
| 156 const PP_Module kInvalidModule = 0; |
| 157 const PP_Resource kInvalidResource = 0; |
| 158 |
| 159 // These should not exist. |
| 160 // Chrome uses the bottom 2 bits to differentiate between different id types. |
| 161 // 00 - module, 01 - instance, 10 - resource, 11 - var. |
| 162 const PP_Instance kNotAnInstance = 0xFFFFF0; |
| 163 const PP_Resource kNotAResource = 0xAAAAA0; |
| 164 |
| 165 // Interface pointers and ids corresponding to this plugin; |
| 166 // set at initialization/creation. |
| 167 PP_Instance pp_instance(); |
| 168 PP_Module pp_module(); |
| 169 |
| 170 // If you are providing your own version of PPP_Instance::DidCreate |
| 171 // call this function to ensure proper test set-up. |
| 172 PP_Bool DidCreateDefault(PP_Instance instance, |
| 173 uint32_t argc, const char* argn[], const char* argv[]); |
| 174 |
| 175 #endif // NATIVE_CLIENT_TESTS_PPAPI_TEST_PPB_TEMPLATE_TEST_INTERFACE_H |
OLD | NEW |