OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef PPAPI_TESTS_TEST_CASE_H_ | 5 #ifndef PPAPI_TESTS_TEST_CASE_H_ |
6 #define PPAPI_TESTS_TEST_CASE_H_ | 6 #define PPAPI_TESTS_TEST_CASE_H_ |
7 | 7 |
8 #include <cmath> | 8 #include <cmath> |
9 #include <limits> | 9 #include <limits> |
10 #include <set> | 10 #include <set> |
11 #include <string> | 11 #include <string> |
12 | 12 |
13 #include "ppapi/c/pp_resource.h" | 13 #include "ppapi/c/pp_resource.h" |
14 #include "ppapi/c/dev/ppb_testing_dev.h" | 14 #include "ppapi/c/dev/ppb_testing_dev.h" |
| 15 #include "ppapi/cpp/dev/message_loop_dev.h" |
15 #include "ppapi/cpp/dev/scrollbar_dev.h" | 16 #include "ppapi/cpp/dev/scrollbar_dev.h" |
16 #include "ppapi/cpp/view.h" | 17 #include "ppapi/cpp/view.h" |
17 #include "ppapi/tests/test_utils.h" | 18 #include "ppapi/tests/test_utils.h" |
| 19 #include "ppapi/tests/testing_instance.h" |
18 | 20 |
19 #if (defined __native_client__) | 21 #if (defined __native_client__) |
20 #include "ppapi/cpp/var.h" | 22 #include "ppapi/cpp/var.h" |
21 #else | 23 #else |
22 #include "ppapi/cpp/private/var_private.h" | 24 #include "ppapi/cpp/private/var_private.h" |
23 #endif | 25 #endif |
24 | 26 |
25 class TestingInstance; | 27 class TestingInstance; |
26 | 28 |
27 namespace pp { | 29 namespace pp { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 // associated TestingInstance. Default implementation returns false. TestCases | 73 // associated TestingInstance. Default implementation returns false. TestCases |
72 // that want to handle view changes should override this method. | 74 // that want to handle view changes should override this method. |
73 virtual bool HandleInputEvent(const pp::InputEvent& event); | 75 virtual bool HandleInputEvent(const pp::InputEvent& event); |
74 | 76 |
75 void IgnoreLeakedVar(int64_t id); | 77 void IgnoreLeakedVar(int64_t id); |
76 | 78 |
77 TestingInstance* instance() { return instance_; } | 79 TestingInstance* instance() { return instance_; } |
78 | 80 |
79 const PPB_Testing_Dev* testing_interface() { return testing_interface_; } | 81 const PPB_Testing_Dev* testing_interface() { return testing_interface_; } |
80 | 82 |
| 83 static void QuitMainMessageLoop(PP_Instance instance); |
| 84 |
81 protected: | 85 protected: |
82 #if !(defined __native_client__) | 86 #if !(defined __native_client__) |
83 // Overridden by each test to supply a ScriptableObject corresponding to the | 87 // Overridden by each test to supply a ScriptableObject corresponding to the |
84 // test. There can only be one object created for all tests in a given class, | 88 // test. There can only be one object created for all tests in a given class, |
85 // so be sure your object is designed to be re-used. | 89 // so be sure your object is designed to be re-used. |
86 // | 90 // |
87 // This object should be created on the heap. Ownership will be passed to the | 91 // This object should be created on the heap. Ownership will be passed to the |
88 // caller. Return NULL if there is no supported test object (the default). | 92 // caller. Return NULL if there is no supported test object (the default). |
89 virtual pp::deprecated::ScriptableObject* CreateTestObject(); | 93 virtual pp::deprecated::ScriptableObject* CreateTestObject(); |
90 #endif | 94 #endif |
91 | 95 |
92 // Checks whether the testing interface is available. Returns true if it is, | 96 // Checks whether the testing interface is available. Returns true if it is, |
93 // false otherwise. If it is not available, adds a descriptive error. This is | 97 // false otherwise. If it is not available, adds a descriptive error. This is |
94 // for use by tests that require the testing interface. | 98 // for use by tests that require the testing interface. |
95 bool CheckTestingInterface(); | 99 bool CheckTestingInterface(); |
96 | 100 |
97 // Makes sure the test is run over HTTP. | 101 // Makes sure the test is run over HTTP. |
98 bool EnsureRunningOverHTTP(); | 102 bool EnsureRunningOverHTTP(); |
99 | 103 |
100 // Return true if the given test name matches the filter. This is true if | 104 // Return true if the given test name matches the filter. This is true if |
101 // (a) filter is empty or (b) test_name and filter match exactly. | 105 // (a) filter is empty or (b) test_name and filter match exactly. |
102 bool MatchesFilter(const std::string& test_name, const std::string& filter); | 106 bool MatchesFilter(const std::string& test_name, const std::string& filter); |
103 | 107 |
104 // Check for leaked resources and vars at the end of the test. If any exist, | 108 // Check for leaked resources and vars at the end of the test. If any exist, |
105 // return a string with some information about the error. Otherwise, return | 109 // return a string with some information about the error. Otherwise, return |
106 // an empty string. | 110 // an empty string. |
107 std::string CheckResourcesAndVars(); | 111 // |
| 112 // You should pass the error string from the test so far; if it is non-empty, |
| 113 // CheckResourcesAndVars will do nothing and return the same string. |
| 114 std::string CheckResourcesAndVars(std::string errors); |
| 115 |
| 116 // Run the given test method on a background thread and return the result. |
| 117 template <class T> |
| 118 std::string RunOnThread(std::string(T::*test_to_run)()) { |
| 119 #ifdef ENABLE_PEPPER_THREADING |
| 120 if (!testing_interface_) { |
| 121 return "Testing blocking callbacks requires the testing interface. In " |
| 122 "Chrome, use the --enable-pepper-testing flag."; |
| 123 } |
| 124 // These tests are only valid if running out-of-process (threading is not |
| 125 // supported in-process). Just consider it a pass. |
| 126 if (!testing_interface_->IsOutOfProcess()) |
| 127 return std::string(); |
| 128 ThreadedTestRunner<T> runner(instance_->pp_instance(), |
| 129 static_cast<T*>(this), test_to_run); |
| 130 RunOnThreadInternal(&ThreadedTestRunner<T>::ThreadFunction, &runner, |
| 131 testing_interface_); |
| 132 return runner.result(); |
| 133 #else |
| 134 // If threading's not enabled, just treat it as success. |
| 135 return std::string(); |
| 136 #endif |
| 137 } |
108 | 138 |
109 // Pointer to the instance that owns us. | 139 // Pointer to the instance that owns us. |
110 TestingInstance* instance_; | 140 TestingInstance* instance_; |
111 | 141 |
112 protected: | |
113 // NULL unless InitTestingInterface is called. | 142 // NULL unless InitTestingInterface is called. |
114 const PPB_Testing_Dev* testing_interface_; | 143 const PPB_Testing_Dev* testing_interface_; |
115 | 144 |
116 // TODO(dmichael): Remove this, it's for temporary backwards compatibility so | 145 // TODO(dmichael): Remove this, it's for temporary backwards compatibility so |
117 // I don't have to change all the tests at once. | 146 // I don't have to change all the tests at once. |
118 bool force_async_; | 147 bool force_async_; |
119 | 148 |
120 void set_callback_type(CallbackType callback_type) { | 149 void set_callback_type(CallbackType callback_type) { |
121 callback_type_ = callback_type; | 150 callback_type_ = callback_type; |
122 // TODO(dmichael): Remove this; see comment on force_async_. | 151 // TODO(dmichael): Remove this; see comment on force_async_. |
123 force_async_ = (callback_type_ == PP_REQUIRED); | 152 force_async_ = (callback_type_ == PP_REQUIRED); |
124 } | 153 } |
125 CallbackType callback_type() const { | 154 CallbackType callback_type() const { |
126 return callback_type_; | 155 return callback_type_; |
127 } | 156 } |
128 | 157 |
129 private: | 158 private: |
| 159 template <class T> |
| 160 class ThreadedTestRunner { |
| 161 public: |
| 162 typedef std::string(T::*TestMethodType)(); |
| 163 ThreadedTestRunner(PP_Instance instance, |
| 164 T* test_case, |
| 165 TestMethodType test_to_run) |
| 166 : instance_(instance), |
| 167 test_case_(test_case), |
| 168 test_to_run_(test_to_run) { |
| 169 } |
| 170 const std::string& result() { return result_; } |
| 171 static void ThreadFunction(void* runner) { |
| 172 static_cast<ThreadedTestRunner<T>*>(runner)->Run(); |
| 173 } |
| 174 |
| 175 private: |
| 176 void Run() { |
| 177 // TODO(dmichael): Create and attach a pp::MessageLoop for this thread so |
| 178 // nested loops work. |
| 179 result_ = (test_case_->*test_to_run_)(); |
| 180 // Tell the main thread to quit its nested message loop, now that the test |
| 181 // is complete. |
| 182 TestCase::QuitMainMessageLoop(instance_); |
| 183 } |
| 184 |
| 185 std::string result_; |
| 186 PP_Instance instance_; |
| 187 T* test_case_; |
| 188 TestMethodType test_to_run_; |
| 189 }; |
| 190 |
| 191 // The internals for RunOnThread. This allows us to avoid including |
| 192 // pp_thread.h in this header file, since it includes system headers like |
| 193 // windows.h. |
| 194 // RunOnThreadInternal launches a new thread to run |thread_func|, waits |
| 195 // for it to complete using RunMessageLoop(), then joins. |
| 196 void RunOnThreadInternal(void (*thread_func)(void*), |
| 197 void* thread_param, |
| 198 const PPB_Testing_Dev* testing_interface); |
| 199 |
| 200 static void DoQuitMainMessageLoop(void* pp_instance, int32_t result); |
| 201 |
130 // Passed when creating completion callbacks in some tests. This determines | 202 // Passed when creating completion callbacks in some tests. This determines |
131 // what kind of callback we use for the test. | 203 // what kind of callback we use for the test. |
132 CallbackType callback_type_; | 204 CallbackType callback_type_; |
133 | 205 |
134 // Var ids that should be ignored when checking for leaks on shutdown. | 206 // Var ids that should be ignored when checking for leaks on shutdown. |
135 std::set<int64_t> ignored_leaked_vars_; | 207 std::set<int64_t> ignored_leaked_vars_; |
136 | 208 |
137 #if !(defined __native_client__) | 209 #if !(defined __native_client__) |
138 // Holds the test object, if any was retrieved from CreateTestObject. | 210 // Holds the test object, if any was retrieved from CreateTestObject. |
139 pp::VarPrivate test_object_; | 211 pp::VarPrivate test_object_; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 static TestCaseFactory g_Test##name_factory( \ | 249 static TestCaseFactory g_Test##name_factory( \ |
178 #name, &Test##name##_FactoryMethod \ | 250 #name, &Test##name##_FactoryMethod \ |
179 ) | 251 ) |
180 | 252 |
181 // Helper macro for calling functions implementing specific tests in the | 253 // Helper macro for calling functions implementing specific tests in the |
182 // RunTest function. This assumes the function name is TestFoo where Foo is the | 254 // RunTest function. This assumes the function name is TestFoo where Foo is the |
183 // test |name|. | 255 // test |name|. |
184 #define RUN_TEST(name, test_filter) \ | 256 #define RUN_TEST(name, test_filter) \ |
185 if (MatchesFilter(#name, test_filter)) { \ | 257 if (MatchesFilter(#name, test_filter)) { \ |
186 set_callback_type(PP_OPTIONAL); \ | 258 set_callback_type(PP_OPTIONAL); \ |
187 std::string error_message = Test##name(); \ | 259 instance_->LogTest(#name, CheckResourcesAndVars(Test##name())); \ |
188 if (error_message.empty()) \ | |
189 error_message = CheckResourcesAndVars(); \ | |
190 instance_->LogTest(#name, error_message); \ | |
191 } | 260 } |
192 | 261 |
| 262 // Like RUN_TEST above but forces functions taking callbacks to complete |
| 263 // asynchronously on success or error. |
| 264 #define RUN_TEST_FORCEASYNC(name, test_filter) \ |
| 265 if (MatchesFilter(#name, test_filter)) { \ |
| 266 set_callback_type(PP_REQUIRED); \ |
| 267 instance_->LogTest(#name"ForceAsync", \ |
| 268 CheckResourcesAndVars(Test##name())); \ |
| 269 } |
| 270 |
| 271 #define RUN_TEST_BLOCKING(test_case, name, test_filter) \ |
| 272 if (MatchesFilter(#name, test_filter)) { \ |
| 273 set_callback_type(PP_BLOCKING); \ |
| 274 instance_->LogTest(#name"Blocking", \ |
| 275 CheckResourcesAndVars(RunOnThread(&test_case::Test##name))); \ |
| 276 } |
| 277 |
| 278 #define RUN_TEST_FORCEASYNC_AND_NOT(name, test_filter) \ |
| 279 do { \ |
| 280 RUN_TEST_FORCEASYNC(name, test_filter); \ |
| 281 RUN_TEST(name, test_filter); \ |
| 282 } while (false) |
| 283 |
| 284 // Run a test with all possible callback types. |
| 285 #define RUN_CALLBACK_TEST(test_case, name, test_filter) \ |
| 286 do { \ |
| 287 RUN_TEST_FORCEASYNC(name, test_filter); \ |
| 288 RUN_TEST(name, test_filter); \ |
| 289 RUN_TEST_BLOCKING(test_case, name, test_filter); \ |
| 290 } while (false) |
| 291 |
193 #define RUN_TEST_WITH_REFERENCE_CHECK(name, test_filter) \ | 292 #define RUN_TEST_WITH_REFERENCE_CHECK(name, test_filter) \ |
194 if (MatchesFilter(#name, test_filter)) { \ | 293 if (MatchesFilter(#name, test_filter)) { \ |
195 set_callback_type(PP_OPTIONAL); \ | 294 set_callback_type(PP_OPTIONAL); \ |
196 uint32_t objects = testing_interface_->GetLiveObjectsForInstance( \ | 295 uint32_t objects = testing_interface_->GetLiveObjectsForInstance( \ |
197 instance_->pp_instance()); \ | 296 instance_->pp_instance()); \ |
198 std::string error_message = Test##name(); \ | 297 std::string error_message = Test##name(); \ |
199 if (error_message.empty() && \ | 298 if (error_message.empty() && \ |
200 testing_interface_->GetLiveObjectsForInstance( \ | 299 testing_interface_->GetLiveObjectsForInstance( \ |
201 instance_->pp_instance()) != objects) \ | 300 instance_->pp_instance()) != objects) \ |
202 error_message = MakeFailureMessage(__FILE__, __LINE__, \ | 301 error_message = MakeFailureMessage(__FILE__, __LINE__, \ |
203 "reference leak check"); \ | 302 "reference leak check"); \ |
204 instance_->LogTest(#name, error_message); \ | 303 instance_->LogTest(#name, error_message); \ |
205 } | 304 } |
206 | 305 |
207 // Like RUN_TEST above but forces functions taking callbacks to complete | |
208 // asynchronously on success or error. | |
209 #define RUN_TEST_FORCEASYNC(name, test_filter) \ | |
210 if (MatchesFilter(#name"ForceAsync", test_filter)) { \ | |
211 set_callback_type(PP_REQUIRED); \ | |
212 instance_->LogTest(#name"ForceAsync", Test##name()); \ | |
213 } | |
214 | |
215 #define RUN_TEST_FORCEASYNC_AND_NOT(name, test_filter) \ | |
216 do { \ | |
217 RUN_TEST_FORCEASYNC(name, test_filter); \ | |
218 RUN_TEST(name, test_filter); \ | |
219 } while (false) | |
220 | |
221 | |
222 // Helper macros for checking values in tests, and returning a location | 306 // Helper macros for checking values in tests, and returning a location |
223 // description of the test fails. | 307 // description of the test fails. |
224 #define ASSERT_TRUE(cmd) \ | 308 #define ASSERT_TRUE(cmd) \ |
225 if (!(cmd)) { \ | 309 if (!(cmd)) { \ |
226 return MakeFailureMessage(__FILE__, __LINE__, #cmd); \ | 310 return MakeFailureMessage(__FILE__, __LINE__, #cmd); \ |
227 } | 311 } |
228 #define ASSERT_FALSE(cmd) ASSERT_TRUE(!(cmd)) | 312 #define ASSERT_FALSE(cmd) ASSERT_TRUE(!(cmd)) |
229 #define ASSERT_EQ(a, b) ASSERT_TRUE((a) == (b)) | 313 #define ASSERT_EQ(a, b) ASSERT_TRUE((a) == (b)) |
230 #define ASSERT_NE(a, b) ASSERT_TRUE((a) != (b)) | 314 #define ASSERT_NE(a, b) ASSERT_TRUE((a) != (b)) |
231 | 315 |
232 #define ASSERT_DOUBLE_EQ(a, b) ASSERT_TRUE( \ | 316 #define ASSERT_DOUBLE_EQ(a, b) ASSERT_TRUE( \ |
233 std::fabs((a)-(b)) <= std::numeric_limits<double>::epsilon()) | 317 std::fabs((a)-(b)) <= std::numeric_limits<double>::epsilon()) |
234 | 318 |
235 // Runs |function| as a subtest and asserts that it has passed. | 319 // Runs |function| as a subtest and asserts that it has passed. |
236 #define ASSERT_SUBTEST_SUCCESS(function) \ | 320 #define ASSERT_SUBTEST_SUCCESS(function) \ |
237 do { \ | 321 do { \ |
238 std::string result = (function); \ | 322 std::string result = (function); \ |
239 if (!result.empty()) \ | 323 if (!result.empty()) \ |
240 return result; \ | 324 return result; \ |
241 } while (false) | 325 } while (false) |
242 | 326 |
243 #define PASS() return std::string() | 327 #define PASS() return std::string() |
244 | 328 |
245 #endif // PPAPI_TESTS_TEST_CASE_H_ | 329 #endif // PPAPI_TESTS_TEST_CASE_H_ |
OLD | NEW |