| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 <string> | 10 #include <string> |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 // Individual classes of tests derive from this generic test case. | 30 // Individual classes of tests derive from this generic test case. |
| 31 class TestCase { | 31 class TestCase { |
| 32 public: | 32 public: |
| 33 explicit TestCase(TestingInstance* instance); | 33 explicit TestCase(TestingInstance* instance); |
| 34 virtual ~TestCase(); | 34 virtual ~TestCase(); |
| 35 | 35 |
| 36 // Optionally override to do testcase specific initialization. | 36 // Optionally override to do testcase specific initialization. |
| 37 // Default implementation just returns true. | 37 // Default implementation just returns true. |
| 38 virtual bool Init(); | 38 virtual bool Init(); |
| 39 | 39 |
| 40 // Override to implement the test. It will be called after the plugin is | 40 // Override to implement the test case. It will be called after the plugin is |
| 41 // first displayed. | 41 // first displayed, passing a string. If the string is empty, the |
| 42 virtual void RunTest() = 0; | 42 // should run all tests for this test case. Otherwise, it should run the test |
| 43 // whose name matches test_filter exactly (if there is one). This should |
| 44 // generally be implemented using the RUN_TEST* macros. |
| 45 virtual void RunTests(const std::string& test_filter) = 0; |
| 43 | 46 |
| 44 static std::string MakeFailureMessage(const char* file, int line, | 47 static std::string MakeFailureMessage(const char* file, int line, |
| 45 const char* cmd); | 48 const char* cmd); |
| 46 | 49 |
| 47 #if !(defined __native_client__) | 50 #if !(defined __native_client__) |
| 48 // Returns the scriptable test object for the current test, if any. | 51 // Returns the scriptable test object for the current test, if any. |
| 49 // Internally, this uses CreateTestObject which each test overrides. | 52 // Internally, this uses CreateTestObject which each test overrides. |
| 50 pp::VarPrivate GetTestObject(); | 53 pp::VarPrivate GetTestObject(); |
| 51 #endif | 54 #endif |
| 52 | 55 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 76 // caller. Return NULL if there is no supported test object (the default). | 79 // caller. Return NULL if there is no supported test object (the default). |
| 77 virtual pp::deprecated::ScriptableObject* CreateTestObject(); | 80 virtual pp::deprecated::ScriptableObject* CreateTestObject(); |
| 78 #endif | 81 #endif |
| 79 | 82 |
| 80 // Initializes the testing interface. | 83 // Initializes the testing interface. |
| 81 bool InitTestingInterface(); | 84 bool InitTestingInterface(); |
| 82 | 85 |
| 83 // Makes sure the test is run over HTTP. | 86 // Makes sure the test is run over HTTP. |
| 84 bool EnsureRunningOverHTTP(); | 87 bool EnsureRunningOverHTTP(); |
| 85 | 88 |
| 89 // Return true if the given test name matches the filter. This is true if |
| 90 // (a) filter is empty or (b) test_name and filter match exactly. |
| 91 bool MatchesFilter(const std::string& test_name, const std::string& filter); |
| 92 |
| 86 // Pointer to the instance that owns us. | 93 // Pointer to the instance that owns us. |
| 87 TestingInstance* instance_; | 94 TestingInstance* instance_; |
| 88 | 95 |
| 89 // NULL unless InitTestingInterface is called. | 96 // NULL unless InitTestingInterface is called. |
| 90 const PPB_Testing_Dev* testing_interface_; | 97 const PPB_Testing_Dev* testing_interface_; |
| 91 | 98 |
| 92 // Force asynchronous completion of any operation taking a callback. | 99 // Force asynchronous completion of any operation taking a callback. |
| 93 bool force_async_; | 100 bool force_async_; |
| 94 | 101 |
| 95 private: | 102 private: |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 static TestCase* Test##name##_FactoryMethod(TestingInstance* instance) { \ | 140 static TestCase* Test##name##_FactoryMethod(TestingInstance* instance) { \ |
| 134 return new Test##name(instance); \ | 141 return new Test##name(instance); \ |
| 135 } \ | 142 } \ |
| 136 static TestCaseFactory g_Test##name_factory( \ | 143 static TestCaseFactory g_Test##name_factory( \ |
| 137 #name, &Test##name##_FactoryMethod \ | 144 #name, &Test##name##_FactoryMethod \ |
| 138 ) | 145 ) |
| 139 | 146 |
| 140 // Helper macro for calling functions implementing specific tests in the | 147 // Helper macro for calling functions implementing specific tests in the |
| 141 // RunTest function. This assumes the function name is TestFoo where Foo is the | 148 // RunTest function. This assumes the function name is TestFoo where Foo is the |
| 142 // test |name|. | 149 // test |name|. |
| 143 #define RUN_TEST(name) \ | 150 #define RUN_TEST(name, test_filter) \ |
| 144 do { \ | 151 if (MatchesFilter(#name, test_filter)) { \ |
| 145 force_async_ = false; \ | 152 force_async_ = false; \ |
| 146 instance_->LogTest(#name, Test##name()); \ | 153 instance_->LogTest(#name, Test##name()); \ |
| 147 } while (false) | 154 } |
| 148 | 155 |
| 149 // Like RUN_TEST above but forces functions taking callbacks to complete | 156 // Like RUN_TEST above but forces functions taking callbacks to complete |
| 150 // asynchronously on success or error. | 157 // asynchronously on success or error. |
| 151 #define RUN_TEST_FORCEASYNC(name) \ | 158 #define RUN_TEST_FORCEASYNC(name, test_filter) \ |
| 152 do { \ | 159 if (MatchesFilter(#name"ForceAsync", test_filter)) { \ |
| 153 force_async_ = true; \ | 160 force_async_ = true; \ |
| 154 instance_->LogTest(#name"ForceAsync", Test##name()); \ | 161 instance_->LogTest(#name"ForceAsync", Test##name()); \ |
| 155 } while (false) | 162 } |
| 156 | 163 |
| 157 #define RUN_TEST_FORCEASYNC_AND_NOT(name) \ | 164 #define RUN_TEST_FORCEASYNC_AND_NOT(name, test_filter) \ |
| 158 do { \ | 165 do { \ |
| 159 RUN_TEST_FORCEASYNC(name); \ | 166 RUN_TEST_FORCEASYNC(name, test_filter); \ |
| 160 RUN_TEST(name); \ | 167 RUN_TEST(name, test_filter); \ |
| 161 } while (false) | 168 } while (false) |
| 162 | 169 |
| 163 | 170 |
| 164 // Helper macros for checking values in tests, and returning a location | 171 // Helper macros for checking values in tests, and returning a location |
| 165 // description of the test fails. | 172 // description of the test fails. |
| 166 #define ASSERT_TRUE(cmd) \ | 173 #define ASSERT_TRUE(cmd) \ |
| 167 if (!(cmd)) { \ | 174 if (!(cmd)) { \ |
| 168 return MakeFailureMessage(__FILE__, __LINE__, #cmd); \ | 175 return MakeFailureMessage(__FILE__, __LINE__, #cmd); \ |
| 169 } | 176 } |
| 170 #define ASSERT_FALSE(cmd) ASSERT_TRUE(!(cmd)) | 177 #define ASSERT_FALSE(cmd) ASSERT_TRUE(!(cmd)) |
| 171 #define ASSERT_EQ(a, b) ASSERT_TRUE((a) == (b)) | 178 #define ASSERT_EQ(a, b) ASSERT_TRUE((a) == (b)) |
| 172 #define ASSERT_NE(a, b) ASSERT_TRUE((a) != (b)) | 179 #define ASSERT_NE(a, b) ASSERT_TRUE((a) != (b)) |
| 173 | 180 |
| 174 #define ASSERT_DOUBLE_EQ(a, b) ASSERT_TRUE( \ | 181 #define ASSERT_DOUBLE_EQ(a, b) ASSERT_TRUE( \ |
| 175 std::fabs((a)-(b)) <= std::numeric_limits<double>::epsilon()) | 182 std::fabs((a)-(b)) <= std::numeric_limits<double>::epsilon()) |
| 176 | 183 |
| 177 #define PASS() return std::string() | 184 #define PASS() return std::string() |
| 178 | 185 |
| 179 #endif // PPAPI_TESTS_TEST_CASE_H_ | 186 #endif // PPAPI_TESTS_TEST_CASE_H_ |
| OLD | NEW |