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 #include "native_client/tests/ppapi_test_lib/test_interface.h" |
| 6 |
| 7 #include <string.h> |
| 8 #include <map> |
| 9 #include <new> |
| 10 |
| 11 #include "native_client/src/include/nacl_macros.h" |
| 12 #include "native_client/src/shared/platform/nacl_check.h" |
| 13 #include "native_client/tests/ppapi_test_lib/get_browser_interface.h" |
| 14 #include "native_client/tests/ppapi_test_lib/internal_utils.h" |
| 15 |
| 16 #include "ppapi/c/pp_instance.h" |
| 17 #include "ppapi/c/pp_module.h" |
| 18 #include "ppapi/c/pp_var.h" |
| 19 #include "ppapi/c/ppb_instance.h" |
| 20 #include "ppapi/c/ppb_messaging.h" |
| 21 #include "ppapi/c/ppb_var.h" |
| 22 |
| 23 void PostTestMessage(nacl::string test_name, nacl::string message) { |
| 24 nacl::string test_message = test_name; |
| 25 test_message += ":"; |
| 26 test_message += message; |
| 27 PP_Var post_var = PPBVar()->VarFromUtf8(pp_instance(), |
| 28 test_message.c_str(), |
| 29 test_message.size()); |
| 30 PPBMessaging()->PostMessage(pp_instance(), post_var); |
| 31 PPBVar()->Release(post_var); |
| 32 } |
| 33 |
| 34 PP_Var PP_MakeString(const char* s) { |
| 35 return PPBVar()->VarFromUtf8(pp_module(), s, strlen(s)); |
| 36 } |
| 37 |
| 38 nacl::string StringifyVar(const PP_Var& var) { |
| 39 uint32_t dummy_size; |
| 40 switch (var.type) { |
| 41 default: |
| 42 return "<UNKNOWN>" + toString(var.type); |
| 43 case PP_VARTYPE_NULL: |
| 44 return "<NULL>"; |
| 45 case PP_VARTYPE_BOOL: |
| 46 return "<BOOL>" + toString(var.value.as_bool); |
| 47 case PP_VARTYPE_INT32: |
| 48 return "<INT32>" + toString(var.value.as_int); |
| 49 case PP_VARTYPE_DOUBLE: |
| 50 return "<DOUBLE>" + toString(var.value.as_double); |
| 51 case PP_VARTYPE_STRING: |
| 52 return "<STRING>" + nacl::string(PPBVar()->VarToUtf8(var, &dummy_size)); |
| 53 } |
| 54 } |
| 55 |
| 56 //////////////////////////////////////////////////////////////////////////////// |
| 57 // Test registration |
| 58 //////////////////////////////////////////////////////////////////////////////// |
| 59 |
| 60 namespace { |
| 61 |
| 62 class TestTable { |
| 63 public: |
| 64 // Return singleton intsance. |
| 65 static TestTable* Get() { |
| 66 static TestTable table; |
| 67 return &table; |
| 68 } |
| 69 |
| 70 void AddTest(nacl::string test_name, TestFunction test_function) { |
| 71 test_map_[test_name] = test_function; |
| 72 } |
| 73 void RunTest(nacl::string test_name); |
| 74 |
| 75 private: |
| 76 NACL_DISALLOW_COPY_AND_ASSIGN(TestTable); |
| 77 |
| 78 TestTable() {} |
| 79 |
| 80 typedef std::map<nacl::string, TestFunction> TestMap; |
| 81 TestMap test_map_; |
| 82 }; |
| 83 |
| 84 void TestTable::RunTest(nacl::string test_name) { |
| 85 TestMap::iterator it = test_map_.find(test_name); |
| 86 if (it == test_map_.end()) { |
| 87 PostTestMessage(test_name, "NOTFOUND"); |
| 88 return; |
| 89 } |
| 90 CHECK(it->second != NULL); |
| 91 TestFunction test_function = it->second; |
| 92 return test_function(); |
| 93 } |
| 94 |
| 95 } // namespace |
| 96 |
| 97 void RegisterTest(nacl::string test_name, TestFunction test_func) { |
| 98 TestTable::Get()->AddTest(test_name, test_func); |
| 99 } |
| 100 |
| 101 void RunTest(nacl::string test_name) { |
| 102 TestTable::Get()->RunTest(test_name); |
| 103 } |
| 104 |
| 105 //////////////////////////////////////////////////////////////////////////////// |
| 106 // Testable callback support |
| 107 //////////////////////////////////////////////////////////////////////////////// |
| 108 |
| 109 namespace { |
| 110 |
| 111 struct CallbackInfo { |
| 112 nacl::string callback_name; |
| 113 PP_CompletionCallback user_callback; |
| 114 }; |
| 115 |
| 116 void ReportCallbackInvocationToJS(const char* callback_name) { |
| 117 PP_Var callback_var = PPBVar()->VarFromUtf8(pp_module(), |
| 118 callback_name, |
| 119 strlen(callback_name)); |
| 120 // Report using postmessage for async tests. |
| 121 PPBMessaging()->PostMessage(pp_instance(), callback_var); |
| 122 PPBVar()->Release(callback_var); |
| 123 } |
| 124 |
| 125 void CallbackWrapper(void* user_data, int32_t result) { |
| 126 CallbackInfo* callback_info = reinterpret_cast<CallbackInfo*>(user_data); |
| 127 PP_RunCompletionCallback(&callback_info->user_callback, result); |
| 128 ReportCallbackInvocationToJS(callback_info->callback_name.c_str()); |
| 129 delete callback_info; |
| 130 } |
| 131 |
| 132 } // namespace |
| 133 |
| 134 PP_CompletionCallback MakeTestableCompletionCallback( |
| 135 const char* callback_name, // Tested for by JS harness. |
| 136 PP_CompletionCallback_Func func, |
| 137 void* user_data) { |
| 138 CHECK(callback_name != NULL && strlen(callback_name) > 0); |
| 139 CHECK(func != NULL); |
| 140 |
| 141 CallbackInfo* callback_info = new(std::nothrow) CallbackInfo; |
| 142 CHECK(callback_info != NULL); |
| 143 callback_info->callback_name = callback_name; |
| 144 callback_info->user_callback = |
| 145 PP_MakeOptionalCompletionCallback(func, user_data); |
| 146 |
| 147 return PP_MakeOptionalCompletionCallback(CallbackWrapper, callback_info); |
| 148 } |
| 149 |
| 150 PP_CompletionCallback MakeTestableCompletionCallback( |
| 151 const char* callback_name, // Tested for by JS harness. |
| 152 PP_CompletionCallback_Func func) { |
| 153 return MakeTestableCompletionCallback(callback_name, func, NULL); |
| 154 } |
OLD | NEW |