| 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 <map> |
| 10 #include <set> | 11 #include <set> |
| 11 #include <string> | 12 #include <string> |
| 12 | 13 |
| 14 #include "ppapi/c/dev/ppb_testing_dev.h" |
| 13 #include "ppapi/c/pp_resource.h" | 15 #include "ppapi/c/pp_resource.h" |
| 14 #include "ppapi/c/dev/ppb_testing_dev.h" | 16 #include "ppapi/c/pp_time.h" |
| 15 #include "ppapi/cpp/dev/scrollbar_dev.h" | 17 #include "ppapi/cpp/dev/scrollbar_dev.h" |
| 16 #include "ppapi/cpp/message_loop.h" | 18 #include "ppapi/cpp/message_loop.h" |
| 17 #include "ppapi/cpp/view.h" | 19 #include "ppapi/cpp/view.h" |
| 18 #include "ppapi/tests/test_utils.h" | 20 #include "ppapi/tests/test_utils.h" |
| 19 #include "ppapi/tests/testing_instance.h" | 21 #include "ppapi/tests/testing_instance.h" |
| 20 | 22 |
| 21 #if (defined __native_client__) | 23 #if (defined __native_client__) |
| 22 #include "ppapi/cpp/var.h" | 24 #include "ppapi/cpp/var.h" |
| 23 #else | 25 #else |
| 24 #include "ppapi/cpp/private/var_private.h" | 26 #include "ppapi/cpp/private/var_private.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 36 class TestCase { | 38 class TestCase { |
| 37 public: | 39 public: |
| 38 explicit TestCase(TestingInstance* instance); | 40 explicit TestCase(TestingInstance* instance); |
| 39 virtual ~TestCase(); | 41 virtual ~TestCase(); |
| 40 | 42 |
| 41 // Optionally override to do testcase specific initialization. | 43 // Optionally override to do testcase specific initialization. |
| 42 // Default implementation just returns true. | 44 // Default implementation just returns true. |
| 43 virtual bool Init(); | 45 virtual bool Init(); |
| 44 | 46 |
| 45 // Override to implement the test case. It will be called after the plugin is | 47 // Override to implement the test case. It will be called after the plugin is |
| 46 // first displayed, passing a string. If the string is empty, the | 48 // first displayed, passing a string. If the string is empty, RunTests should |
| 47 // should run all tests for this test case. Otherwise, it should run the test | 49 // run all tests for this test case. Otherwise, it must be a comma-delimited |
| 48 // whose name matches test_filter exactly (if there is one). This should | 50 // list of test names, possibly prefixed. E.g.: |
| 49 // generally be implemented using the RUN_TEST* macros. | 51 // "Foo_GoodTest,DISABLED_Foo_BadTest,Foo_OtherGoodTest" |
| 52 // All listed tests which are not prefixed will be run. |
| 53 // |
| 54 // This should generally be implemented in a TestCase subclass using the |
| 55 // RUN_TEST* macros. |
| 50 virtual void RunTests(const std::string& test_filter) = 0; | 56 virtual void RunTests(const std::string& test_filter) = 0; |
| 51 | 57 |
| 52 static std::string MakeFailureMessage(const char* file, int line, | 58 static std::string MakeFailureMessage(const char* file, int line, |
| 53 const char* cmd); | 59 const char* cmd); |
| 54 | 60 |
| 55 #if !(defined __native_client__) | 61 #if !(defined __native_client__) |
| 56 // Returns the scriptable test object for the current test, if any. | 62 // Returns the scriptable test object for the current test, if any. |
| 57 // Internally, this uses CreateTestObject which each test overrides. | 63 // Internally, this uses CreateTestObject which each test overrides. |
| 58 pp::VarPrivate GetTestObject(); | 64 pp::VarPrivate GetTestObject(); |
| 59 #endif | 65 #endif |
| (...skipping 15 matching lines...) Expand all Loading... |
| 75 virtual bool HandleInputEvent(const pp::InputEvent& event); | 81 virtual bool HandleInputEvent(const pp::InputEvent& event); |
| 76 | 82 |
| 77 void IgnoreLeakedVar(int64_t id); | 83 void IgnoreLeakedVar(int64_t id); |
| 78 | 84 |
| 79 TestingInstance* instance() { return instance_; } | 85 TestingInstance* instance() { return instance_; } |
| 80 | 86 |
| 81 const PPB_Testing_Dev* testing_interface() { return testing_interface_; } | 87 const PPB_Testing_Dev* testing_interface() { return testing_interface_; } |
| 82 | 88 |
| 83 static void QuitMainMessageLoop(PP_Instance instance); | 89 static void QuitMainMessageLoop(PP_Instance instance); |
| 84 | 90 |
| 91 const std::map<std::string, bool>& remaining_tests() { |
| 92 return remaining_tests_; |
| 93 } |
| 94 const std::set<std::string>& skipped_tests() { |
| 95 return skipped_tests_; |
| 96 } |
| 97 |
| 85 protected: | 98 protected: |
| 86 #if !(defined __native_client__) | 99 #if !(defined __native_client__) |
| 87 // Overridden by each test to supply a ScriptableObject corresponding to the | 100 // Overridden by each test to supply a ScriptableObject corresponding to the |
| 88 // test. There can only be one object created for all tests in a given class, | 101 // test. There can only be one object created for all tests in a given class, |
| 89 // so be sure your object is designed to be re-used. | 102 // so be sure your object is designed to be re-used. |
| 90 // | 103 // |
| 91 // This object should be created on the heap. Ownership will be passed to the | 104 // This object should be created on the heap. Ownership will be passed to the |
| 92 // caller. Return NULL if there is no supported test object (the default). | 105 // caller. Return NULL if there is no supported test object (the default). |
| 93 virtual pp::deprecated::ScriptableObject* CreateTestObject(); | 106 virtual pp::deprecated::ScriptableObject* CreateTestObject(); |
| 94 #endif | 107 #endif |
| 95 | 108 |
| 96 // Checks whether the testing interface is available. Returns true if it is, | 109 // Checks whether the testing interface is available. Returns true if it is, |
| 97 // false otherwise. If it is not available, adds a descriptive error. This is | 110 // false otherwise. If it is not available, adds a descriptive error. This is |
| 98 // for use by tests that require the testing interface. | 111 // for use by tests that require the testing interface. |
| 99 bool CheckTestingInterface(); | 112 bool CheckTestingInterface(); |
| 100 | 113 |
| 101 // Makes sure the test is run over HTTP. | 114 // Makes sure the test is run over HTTP. |
| 102 bool EnsureRunningOverHTTP(); | 115 bool EnsureRunningOverHTTP(); |
| 103 | 116 |
| 117 // Returns true if |filter| only contains a TestCase name, which normally |
| 118 // means "run all tests". Some TestCases require special setup for individual |
| 119 // tests, and can use this function to decide whether to ignore those tests. |
| 120 bool ShouldRunAllTests(const std::string& filter); |
| 121 |
| 104 // Return true if the given test name matches the filter. This is true if | 122 // Return true if the given test name matches the filter. This is true if |
| 105 // (a) filter is empty or (b) test_name and filter match exactly. | 123 // (a) filter is empty or (b) test_name matches a test name listed in filter |
| 106 bool MatchesFilter(const std::string& test_name, const std::string& filter); | 124 // exactly. |
| 125 bool ShouldRunTest(const std::string& test_name, const std::string& filter); |
| 107 | 126 |
| 108 // Check for leaked resources and vars at the end of the test. If any exist, | 127 // Check for leaked resources and vars at the end of the test. If any exist, |
| 109 // return a string with some information about the error. Otherwise, return | 128 // return a string with some information about the error. Otherwise, return |
| 110 // an empty string. | 129 // an empty string. |
| 111 // | 130 // |
| 112 // You should pass the error string from the test so far; if it is non-empty, | 131 // 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. | 132 // CheckResourcesAndVars will do nothing and return the same string. |
| 114 std::string CheckResourcesAndVars(std::string errors); | 133 std::string CheckResourcesAndVars(std::string errors); |
| 115 | 134 |
| 135 PP_TimeTicks NowInTimeTicks(); |
| 136 |
| 116 // Run the given test method on a background thread and return the result. | 137 // Run the given test method on a background thread and return the result. |
| 117 template <class T> | 138 template <class T> |
| 118 std::string RunOnThread(std::string(T::*test_to_run)()) { | 139 std::string RunOnThread(std::string(T::*test_to_run)()) { |
| 119 #ifdef ENABLE_PEPPER_THREADING | 140 #ifdef ENABLE_PEPPER_THREADING |
| 120 if (!testing_interface_) { | 141 if (!testing_interface_) { |
| 121 return "Testing blocking callbacks requires the testing interface. In " | 142 return "Testing blocking callbacks requires the testing interface. In " |
| 122 "Chrome, use the --enable-pepper-testing flag."; | 143 "Chrome, use the --enable-pepper-testing flag."; |
| 123 } | 144 } |
| 124 // These tests are only valid if running out-of-process (threading is not | 145 // These tests are only valid if running out-of-process (threading is not |
| 125 // supported in-process). For in-process, just consider it a pass. | 146 // supported in-process). For in-process, just consider it a pass. |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 | 226 |
| 206 static void DoQuitMainMessageLoop(void* pp_instance, int32_t result); | 227 static void DoQuitMainMessageLoop(void* pp_instance, int32_t result); |
| 207 | 228 |
| 208 // Passed when creating completion callbacks in some tests. This determines | 229 // Passed when creating completion callbacks in some tests. This determines |
| 209 // what kind of callback we use for the test. | 230 // what kind of callback we use for the test. |
| 210 CallbackType callback_type_; | 231 CallbackType callback_type_; |
| 211 | 232 |
| 212 // Var ids that should be ignored when checking for leaks on shutdown. | 233 // Var ids that should be ignored when checking for leaks on shutdown. |
| 213 std::set<int64_t> ignored_leaked_vars_; | 234 std::set<int64_t> ignored_leaked_vars_; |
| 214 | 235 |
| 236 // The tests that were found in test_filter but have not yet been run. The |
| 237 // bool indicates whether the test should be run (i.e., it will be false if |
| 238 // the test name was prefixed in the test_filter string). |
| 239 // |
| 240 // This is initialized lazily the first time that ShouldRunTest is called by |
| 241 // RunTests. When RunTests is finished, this should be empty. Any remaining |
| 242 // tests are tests that were listed in the test_filter but didn't match |
| 243 // any calls to ShouldRunTest, meaning it was probably a typo. TestingInstance |
| 244 // should log this and consider it a failure. |
| 245 std::map<std::string, bool> remaining_tests_; |
| 246 // Flag indicating whether we have populated remaining_tests_ yet. |
| 247 bool have_populated_remaining_tests_; |
| 248 |
| 249 // If ShouldRunTest is called but the given test name doesn't match anything |
| 250 // in the test_filter, the test name will be added here. This allows |
| 251 // TestingInstance to detect when not all tests were listed. |
| 252 std::set<std::string> skipped_tests_; |
| 253 |
| 215 #if !(defined __native_client__) | 254 #if !(defined __native_client__) |
| 216 // Holds the test object, if any was retrieved from CreateTestObject. | 255 // Holds the test object, if any was retrieved from CreateTestObject. |
| 217 pp::VarPrivate test_object_; | 256 pp::VarPrivate test_object_; |
| 218 #endif | 257 #endif |
| 219 }; | 258 }; |
| 220 | 259 |
| 221 // This class is an implementation detail. | 260 // This class is an implementation detail. |
| 222 class TestCaseFactory { | 261 class TestCaseFactory { |
| 223 public: | 262 public: |
| 224 typedef TestCase* (*Method)(TestingInstance* instance); | 263 typedef TestCase* (*Method)(TestingInstance* instance); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 253 return new Test##name(instance); \ | 292 return new Test##name(instance); \ |
| 254 } \ | 293 } \ |
| 255 static TestCaseFactory g_Test##name_factory( \ | 294 static TestCaseFactory g_Test##name_factory( \ |
| 256 #name, &Test##name##_FactoryMethod \ | 295 #name, &Test##name##_FactoryMethod \ |
| 257 ) | 296 ) |
| 258 | 297 |
| 259 // Helper macro for calling functions implementing specific tests in the | 298 // Helper macro for calling functions implementing specific tests in the |
| 260 // RunTest function. This assumes the function name is TestFoo where Foo is the | 299 // RunTest function. This assumes the function name is TestFoo where Foo is the |
| 261 // test |name|. | 300 // test |name|. |
| 262 #define RUN_TEST(name, test_filter) \ | 301 #define RUN_TEST(name, test_filter) \ |
| 263 if (MatchesFilter(#name, test_filter)) { \ | 302 if (ShouldRunTest(#name, test_filter)) { \ |
| 264 set_callback_type(PP_OPTIONAL); \ | 303 set_callback_type(PP_OPTIONAL); \ |
| 265 instance_->LogTest(#name, CheckResourcesAndVars(Test##name())); \ | 304 PP_TimeTicks start_time(NowInTimeTicks()); \ |
| 305 instance_->LogTest(#name, \ |
| 306 CheckResourcesAndVars(Test##name()), \ |
| 307 start_time); \ |
| 266 } | 308 } |
| 267 | 309 |
| 268 // Like RUN_TEST above but forces functions taking callbacks to complete | 310 // Like RUN_TEST above but forces functions taking callbacks to complete |
| 269 // asynchronously on success or error. | 311 // asynchronously on success or error. |
| 270 #define RUN_TEST_FORCEASYNC(name, test_filter) \ | 312 #define RUN_TEST_FORCEASYNC(name, test_filter) \ |
| 271 if (MatchesFilter(#name, test_filter)) { \ | 313 if (ShouldRunTest(#name, test_filter)) { \ |
| 272 set_callback_type(PP_REQUIRED); \ | 314 set_callback_type(PP_REQUIRED); \ |
| 315 PP_TimeTicks start_time(NowInTimeTicks()); \ |
| 273 instance_->LogTest(#name"ForceAsync", \ | 316 instance_->LogTest(#name"ForceAsync", \ |
| 274 CheckResourcesAndVars(Test##name())); \ | 317 CheckResourcesAndVars(Test##name()), \ |
| 318 start_time); \ |
| 275 } | 319 } |
| 276 | 320 |
| 277 #define RUN_TEST_BLOCKING(test_case, name, test_filter) \ | 321 #define RUN_TEST_BLOCKING(test_case, name, test_filter) \ |
| 278 if (MatchesFilter(#name, test_filter)) { \ | 322 if (ShouldRunTest(#name, test_filter)) { \ |
| 279 set_callback_type(PP_BLOCKING); \ | 323 set_callback_type(PP_BLOCKING); \ |
| 280 instance_->LogTest(#name"Blocking", \ | 324 PP_TimeTicks start_time(NowInTimeTicks()); \ |
| 281 CheckResourcesAndVars(RunOnThread(&test_case::Test##name))); \ | 325 instance_->LogTest( \ |
| 326 #name"Blocking", \ |
| 327 CheckResourcesAndVars(RunOnThread(&test_case::Test##name)), \ |
| 328 start_time); \ |
| 282 } | 329 } |
| 283 | 330 |
| 284 #define RUN_TEST_BACKGROUND(test_case, name, test_filter) \ | 331 #define RUN_TEST_BACKGROUND(test_case, name, test_filter) \ |
| 285 if (MatchesFilter(#name, test_filter)) { \ | 332 if (ShouldRunTest(#name, test_filter)) { \ |
| 286 instance_->LogTest(#name"Background", \ | 333 PP_TimeTicks start_time(NowInTimeTicks()); \ |
| 287 CheckResourcesAndVars(RunOnThread(&test_case::Test##name))); \ | 334 instance_->LogTest( \ |
| 335 #name"Background", \ |
| 336 CheckResourcesAndVars(RunOnThread(&test_case::Test##name)), \ |
| 337 start_time); \ |
| 288 } | 338 } |
| 289 | 339 |
| 290 #define RUN_TEST_FORCEASYNC_AND_NOT(name, test_filter) \ | 340 #define RUN_TEST_FORCEASYNC_AND_NOT(name, test_filter) \ |
| 291 do { \ | 341 do { \ |
| 292 RUN_TEST_FORCEASYNC(name, test_filter); \ | 342 RUN_TEST_FORCEASYNC(name, test_filter); \ |
| 293 RUN_TEST(name, test_filter); \ | 343 RUN_TEST(name, test_filter); \ |
| 294 } while (false) | 344 } while (false) |
| 295 | 345 |
| 296 // Run a test with all possible callback types. | 346 // Run a test with all possible callback types. |
| 297 #define RUN_CALLBACK_TEST(test_case, name, test_filter) \ | 347 #define RUN_CALLBACK_TEST(test_case, name, test_filter) \ |
| 298 do { \ | 348 do { \ |
| 299 RUN_TEST_FORCEASYNC(name, test_filter); \ | 349 RUN_TEST_FORCEASYNC(name, test_filter); \ |
| 300 RUN_TEST(name, test_filter); \ | 350 RUN_TEST(name, test_filter); \ |
| 301 RUN_TEST_BLOCKING(test_case, name, test_filter); \ | 351 RUN_TEST_BLOCKING(test_case, name, test_filter); \ |
| 302 RUN_TEST_BACKGROUND(test_case, name, test_filter); \ | 352 RUN_TEST_BACKGROUND(test_case, name, test_filter); \ |
| 303 } while (false) | 353 } while (false) |
| 304 | 354 |
| 305 #define RUN_TEST_WITH_REFERENCE_CHECK(name, test_filter) \ | 355 #define RUN_TEST_WITH_REFERENCE_CHECK(name, test_filter) \ |
| 306 if (MatchesFilter(#name, test_filter)) { \ | 356 if (ShouldRunTest(#name, test_filter)) { \ |
| 307 set_callback_type(PP_OPTIONAL); \ | 357 set_callback_type(PP_OPTIONAL); \ |
| 308 uint32_t objects = testing_interface_->GetLiveObjectsForInstance( \ | 358 uint32_t objects = testing_interface_->GetLiveObjectsForInstance( \ |
| 309 instance_->pp_instance()); \ | 359 instance_->pp_instance()); \ |
| 310 std::string error_message = Test##name(); \ | 360 std::string error_message = Test##name(); \ |
| 311 if (error_message.empty() && \ | 361 if (error_message.empty() && \ |
| 312 testing_interface_->GetLiveObjectsForInstance( \ | 362 testing_interface_->GetLiveObjectsForInstance( \ |
| 313 instance_->pp_instance()) != objects) \ | 363 instance_->pp_instance()) != objects) \ |
| 314 error_message = MakeFailureMessage(__FILE__, __LINE__, \ | 364 error_message = MakeFailureMessage(__FILE__, __LINE__, \ |
| 315 "reference leak check"); \ | 365 "reference leak check"); \ |
| 316 instance_->LogTest(#name, error_message); \ | 366 PP_TimeTicks start_time(NowInTimeTicks()); \ |
| 367 instance_->LogTest(#name, \ |
| 368 error_message, \ |
| 369 start_time); \ |
| 317 } | 370 } |
| 318 // TODO(dmichael): Add CheckResourcesAndVars above when Windows tests pass | 371 // TODO(dmichael): Add CheckResourcesAndVars above when Windows tests pass |
| 319 // cleanly. crbug.com/173503 | 372 // cleanly. crbug.com/173503 |
| 320 | 373 |
| 321 // Helper macros for checking values in tests, and returning a location | 374 // Helper macros for checking values in tests, and returning a location |
| 322 // description of the test fails. | 375 // description of the test fails. |
| 323 #define ASSERT_TRUE(cmd) \ | 376 #define ASSERT_TRUE(cmd) \ |
| 324 if (!(cmd)) { \ | 377 if (!(cmd)) { \ |
| 325 return MakeFailureMessage(__FILE__, __LINE__, #cmd); \ | 378 return MakeFailureMessage(__FILE__, __LINE__, #cmd); \ |
| 326 } | 379 } |
| 327 #define ASSERT_FALSE(cmd) ASSERT_TRUE(!(cmd)) | 380 #define ASSERT_FALSE(cmd) ASSERT_TRUE(!(cmd)) |
| 328 #define ASSERT_EQ(a, b) ASSERT_TRUE((a) == (b)) | 381 #define ASSERT_EQ(a, b) ASSERT_TRUE((a) == (b)) |
| 329 #define ASSERT_NE(a, b) ASSERT_TRUE((a) != (b)) | 382 #define ASSERT_NE(a, b) ASSERT_TRUE((a) != (b)) |
| 330 | 383 |
| 331 #define ASSERT_DOUBLE_EQ(a, b) ASSERT_TRUE( \ | 384 #define ASSERT_DOUBLE_EQ(a, b) ASSERT_TRUE( \ |
| 332 std::fabs((a)-(b)) <= std::numeric_limits<double>::epsilon()) | 385 std::fabs((a)-(b)) <= std::numeric_limits<double>::epsilon()) |
| 333 | 386 |
| 334 // Runs |function| as a subtest and asserts that it has passed. | 387 // Runs |function| as a subtest and asserts that it has passed. |
| 335 #define ASSERT_SUBTEST_SUCCESS(function) \ | 388 #define ASSERT_SUBTEST_SUCCESS(function) \ |
| 336 do { \ | 389 do { \ |
| 337 std::string result = (function); \ | 390 std::string result = (function); \ |
| 338 if (!result.empty()) \ | 391 if (!result.empty()) \ |
| 339 return result; \ | 392 return result; \ |
| 340 } while (false) | 393 } while (false) |
| 341 | 394 |
| 342 #define PASS() return std::string() | 395 #define PASS() return std::string() |
| 343 | 396 |
| 344 #endif // PPAPI_TESTS_TEST_CASE_H_ | 397 #endif // PPAPI_TESTS_TEST_CASE_H_ |
| OLD | NEW |