Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(147)

Side by Side Diff: ppapi/tests/test_case.h

Issue 12193015: PPAPI/NaCl: Make related tests run in 1 fixture (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: pre-review cleanup Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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
98
bbudge 2013/02/06 14:51:26 extra line
85 protected: 99 protected:
86 #if !(defined __native_client__) 100 #if !(defined __native_client__)
87 // Overridden by each test to supply a ScriptableObject corresponding to the 101 // 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, 102 // 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. 103 // so be sure your object is designed to be re-used.
90 // 104 //
91 // This object should be created on the heap. Ownership will be passed to the 105 // 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). 106 // caller. Return NULL if there is no supported test object (the default).
93 virtual pp::deprecated::ScriptableObject* CreateTestObject(); 107 virtual pp::deprecated::ScriptableObject* CreateTestObject();
94 #endif 108 #endif
95 109
96 // Checks whether the testing interface is available. Returns true if it is, 110 // 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 111 // false otherwise. If it is not available, adds a descriptive error. This is
98 // for use by tests that require the testing interface. 112 // for use by tests that require the testing interface.
99 bool CheckTestingInterface(); 113 bool CheckTestingInterface();
100 114
101 // Makes sure the test is run over HTTP. 115 // Makes sure the test is run over HTTP.
102 bool EnsureRunningOverHTTP(); 116 bool EnsureRunningOverHTTP();
103 117
104 // Return true if the given test name matches the filter. This is true if 118 // 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. 119 // (a) filter is empty or (b) test_name matches a test name listed in filter
120 // exactly.
106 bool MatchesFilter(const std::string& test_name, const std::string& filter); 121 bool MatchesFilter(const std::string& test_name, const std::string& filter);
107 122
108 // Check for leaked resources and vars at the end of the test. If any exist, 123 // 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 124 // return a string with some information about the error. Otherwise, return
110 // an empty string. 125 // an empty string.
111 // 126 //
112 // You should pass the error string from the test so far; if it is non-empty, 127 // 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. 128 // CheckResourcesAndVars will do nothing and return the same string.
114 std::string CheckResourcesAndVars(std::string errors); 129 std::string CheckResourcesAndVars(std::string errors);
115 130
131 PP_TimeTicks NowInTimeTicks();
132
116 // Run the given test method on a background thread and return the result. 133 // Run the given test method on a background thread and return the result.
117 template <class T> 134 template <class T>
118 std::string RunOnThread(std::string(T::*test_to_run)()) { 135 std::string RunOnThread(std::string(T::*test_to_run)()) {
119 #ifdef ENABLE_PEPPER_THREADING 136 #ifdef ENABLE_PEPPER_THREADING
120 if (!testing_interface_) { 137 if (!testing_interface_) {
121 return "Testing blocking callbacks requires the testing interface. In " 138 return "Testing blocking callbacks requires the testing interface. In "
122 "Chrome, use the --enable-pepper-testing flag."; 139 "Chrome, use the --enable-pepper-testing flag.";
123 } 140 }
124 // These tests are only valid if running out-of-process (threading is not 141 // 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. 142 // supported in-process). For in-process, just consider it a pass.
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 222
206 static void DoQuitMainMessageLoop(void* pp_instance, int32_t result); 223 static void DoQuitMainMessageLoop(void* pp_instance, int32_t result);
207 224
208 // Passed when creating completion callbacks in some tests. This determines 225 // Passed when creating completion callbacks in some tests. This determines
209 // what kind of callback we use for the test. 226 // what kind of callback we use for the test.
210 CallbackType callback_type_; 227 CallbackType callback_type_;
211 228
212 // Var ids that should be ignored when checking for leaks on shutdown. 229 // Var ids that should be ignored when checking for leaks on shutdown.
213 std::set<int64_t> ignored_leaked_vars_; 230 std::set<int64_t> ignored_leaked_vars_;
214 231
232 // The tests that were found in test_filter but have not yet been run. The
233 // bool indicates whether the test should be run (i.e., it will be false if
234 // the test name was prefixed in the test_filter string).
235 //
236 // This is initialized lazily the first time that MatchesFilter is called by
237 // RunTests. When RunTests is finished, this should be empty. Any remaining
238 // tests are tests that were listed in the test_filter but didn't match
239 // any calls to MatchesFilter, meaning it was probably a typo. TestingInstance
240 // should log this and consider it a failure.
241 std::map<std::string, bool> remaining_tests_;
242 // Flag indicating whether we have populated remaining_tests_ yet.
243 bool have_populated_remaining_tests_;
244
245 // If MatchesFilter is called but the given test name doesn't match anything
246 // in the test_filter, the test name will be added here. This allows
247 // TestingInstance to detect when not all tests were listed.
248 std::set<std::string> skipped_tests_;
249
215 #if !(defined __native_client__) 250 #if !(defined __native_client__)
216 // Holds the test object, if any was retrieved from CreateTestObject. 251 // Holds the test object, if any was retrieved from CreateTestObject.
217 pp::VarPrivate test_object_; 252 pp::VarPrivate test_object_;
218 #endif 253 #endif
219 }; 254 };
220 255
221 // This class is an implementation detail. 256 // This class is an implementation detail.
222 class TestCaseFactory { 257 class TestCaseFactory {
223 public: 258 public:
224 typedef TestCase* (*Method)(TestingInstance* instance); 259 typedef TestCase* (*Method)(TestingInstance* instance);
(...skipping 30 matching lines...) Expand all
255 static TestCaseFactory g_Test##name_factory( \ 290 static TestCaseFactory g_Test##name_factory( \
256 #name, &Test##name##_FactoryMethod \ 291 #name, &Test##name##_FactoryMethod \
257 ) 292 )
258 293
259 // Helper macro for calling functions implementing specific tests in the 294 // Helper macro for calling functions implementing specific tests in the
260 // RunTest function. This assumes the function name is TestFoo where Foo is the 295 // RunTest function. This assumes the function name is TestFoo where Foo is the
261 // test |name|. 296 // test |name|.
262 #define RUN_TEST(name, test_filter) \ 297 #define RUN_TEST(name, test_filter) \
263 if (MatchesFilter(#name, test_filter)) { \ 298 if (MatchesFilter(#name, test_filter)) { \
264 set_callback_type(PP_OPTIONAL); \ 299 set_callback_type(PP_OPTIONAL); \
265 instance_->LogTest(#name, CheckResourcesAndVars(Test##name())); \ 300 PP_TimeTicks start_time(NowInTimeTicks()); \
301 instance_->LogTest(#name, \
302 CheckResourcesAndVars(Test##name()), \
303 start_time); \
266 } 304 }
267 305
268 // Like RUN_TEST above but forces functions taking callbacks to complete 306 // Like RUN_TEST above but forces functions taking callbacks to complete
269 // asynchronously on success or error. 307 // asynchronously on success or error.
270 #define RUN_TEST_FORCEASYNC(name, test_filter) \ 308 #define RUN_TEST_FORCEASYNC(name, test_filter) \
271 if (MatchesFilter(#name, test_filter)) { \ 309 if (MatchesFilter(#name, test_filter)) { \
272 set_callback_type(PP_REQUIRED); \ 310 set_callback_type(PP_REQUIRED); \
311 PP_TimeTicks start_time(NowInTimeTicks()); \
273 instance_->LogTest(#name"ForceAsync", \ 312 instance_->LogTest(#name"ForceAsync", \
274 CheckResourcesAndVars(Test##name())); \ 313 CheckResourcesAndVars(Test##name()), \
314 start_time); \
275 } 315 }
276 316
277 #define RUN_TEST_BLOCKING(test_case, name, test_filter) \ 317 #define RUN_TEST_BLOCKING(test_case, name, test_filter) \
278 if (MatchesFilter(#name, test_filter)) { \ 318 if (MatchesFilter(#name, test_filter)) { \
279 set_callback_type(PP_BLOCKING); \ 319 set_callback_type(PP_BLOCKING); \
280 instance_->LogTest(#name"Blocking", \ 320 PP_TimeTicks start_time(NowInTimeTicks()); \
281 CheckResourcesAndVars(RunOnThread(&test_case::Test##name))); \ 321 instance_->LogTest( \
322 #name"Blocking", \
323 CheckResourcesAndVars(RunOnThread(&test_case::Test##name)), \
324 start_time); \
282 } 325 }
283 326
284 #define RUN_TEST_BACKGROUND(test_case, name, test_filter) \ 327 #define RUN_TEST_BACKGROUND(test_case, name, test_filter) \
285 if (MatchesFilter(#name, test_filter)) { \ 328 if (MatchesFilter(#name, test_filter)) { \
286 instance_->LogTest(#name"Background", \ 329 PP_TimeTicks start_time(NowInTimeTicks()); \
287 CheckResourcesAndVars(RunOnThread(&test_case::Test##name))); \ 330 instance_->LogTest( \
331 #name"Background", \
332 CheckResourcesAndVars(RunOnThread(&test_case::Test##name)), \
333 start_time); \
288 } 334 }
289 335
290 #define RUN_TEST_FORCEASYNC_AND_NOT(name, test_filter) \ 336 #define RUN_TEST_FORCEASYNC_AND_NOT(name, test_filter) \
291 do { \ 337 do { \
292 RUN_TEST_FORCEASYNC(name, test_filter); \ 338 RUN_TEST_FORCEASYNC(name, test_filter); \
293 RUN_TEST(name, test_filter); \ 339 RUN_TEST(name, test_filter); \
294 } while (false) 340 } while (false)
295 341
296 // Run a test with all possible callback types. 342 // Run a test with all possible callback types.
297 #define RUN_CALLBACK_TEST(test_case, name, test_filter) \ 343 #define RUN_CALLBACK_TEST(test_case, name, test_filter) \
298 do { \ 344 do { \
299 RUN_TEST_FORCEASYNC(name, test_filter); \ 345 RUN_TEST_FORCEASYNC(name, test_filter); \
300 RUN_TEST(name, test_filter); \ 346 RUN_TEST(name, test_filter); \
301 RUN_TEST_BLOCKING(test_case, name, test_filter); \ 347 RUN_TEST_BLOCKING(test_case, name, test_filter); \
302 RUN_TEST_BACKGROUND(test_case, name, test_filter); \ 348 RUN_TEST_BACKGROUND(test_case, name, test_filter); \
303 } while (false) 349 } while (false)
304 350
305 #define RUN_TEST_WITH_REFERENCE_CHECK(name, test_filter) \ 351 #define RUN_TEST_WITH_REFERENCE_CHECK(name, test_filter) \
306 if (MatchesFilter(#name, test_filter)) { \ 352 if (MatchesFilter(#name, test_filter)) { \
307 set_callback_type(PP_OPTIONAL); \ 353 set_callback_type(PP_OPTIONAL); \
308 uint32_t objects = testing_interface_->GetLiveObjectsForInstance( \ 354 uint32_t objects = testing_interface_->GetLiveObjectsForInstance( \
309 instance_->pp_instance()); \ 355 instance_->pp_instance()); \
310 std::string error_message = Test##name(); \ 356 std::string error_message = Test##name(); \
311 if (error_message.empty() && \ 357 if (error_message.empty() && \
312 testing_interface_->GetLiveObjectsForInstance( \ 358 testing_interface_->GetLiveObjectsForInstance( \
313 instance_->pp_instance()) != objects) \ 359 instance_->pp_instance()) != objects) \
314 error_message = MakeFailureMessage(__FILE__, __LINE__, \ 360 error_message = MakeFailureMessage(__FILE__, __LINE__, \
315 "reference leak check"); \ 361 "reference leak check"); \
316 instance_->LogTest(#name, error_message); \ 362 PP_TimeTicks start_time(NowInTimeTicks()); \
363 instance_->LogTest(#name, \
364 error_message, \
365 start_time); \
317 } 366 }
318 // TODO(dmichael): Add CheckResourcesAndVars above when Windows tests pass 367 // TODO(dmichael): Add CheckResourcesAndVars above when Windows tests pass
319 // cleanly. crbug.com/173503 368 // cleanly. crbug.com/173503
320 369
321 // Helper macros for checking values in tests, and returning a location 370 // Helper macros for checking values in tests, and returning a location
322 // description of the test fails. 371 // description of the test fails.
323 #define ASSERT_TRUE(cmd) \ 372 #define ASSERT_TRUE(cmd) \
324 if (!(cmd)) { \ 373 if (!(cmd)) { \
325 return MakeFailureMessage(__FILE__, __LINE__, #cmd); \ 374 return MakeFailureMessage(__FILE__, __LINE__, #cmd); \
326 } 375 }
327 #define ASSERT_FALSE(cmd) ASSERT_TRUE(!(cmd)) 376 #define ASSERT_FALSE(cmd) ASSERT_TRUE(!(cmd))
328 #define ASSERT_EQ(a, b) ASSERT_TRUE((a) == (b)) 377 #define ASSERT_EQ(a, b) ASSERT_TRUE((a) == (b))
329 #define ASSERT_NE(a, b) ASSERT_TRUE((a) != (b)) 378 #define ASSERT_NE(a, b) ASSERT_TRUE((a) != (b))
330 379
331 #define ASSERT_DOUBLE_EQ(a, b) ASSERT_TRUE( \ 380 #define ASSERT_DOUBLE_EQ(a, b) ASSERT_TRUE( \
332 std::fabs((a)-(b)) <= std::numeric_limits<double>::epsilon()) 381 std::fabs((a)-(b)) <= std::numeric_limits<double>::epsilon())
333 382
334 // Runs |function| as a subtest and asserts that it has passed. 383 // Runs |function| as a subtest and asserts that it has passed.
335 #define ASSERT_SUBTEST_SUCCESS(function) \ 384 #define ASSERT_SUBTEST_SUCCESS(function) \
336 do { \ 385 do { \
337 std::string result = (function); \ 386 std::string result = (function); \
338 if (!result.empty()) \ 387 if (!result.empty()) \
339 return result; \ 388 return result; \
340 } while (false) 389 } while (false)
341 390
342 #define PASS() return std::string() 391 #define PASS() return std::string()
343 392
344 #endif // PPAPI_TESTS_TEST_CASE_H_ 393 #endif // PPAPI_TESTS_TEST_CASE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698