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
3 // found in the LICENSE file.
5 #ifndef PPAPI_TESTS_TEST_CASE_H_
6 #define PPAPI_TESTS_TEST_CASE_H_
14 #include "ppapi/c/dev/ppb_testing_dev.h"
15 #include "ppapi/c/pp_resource.h"
16 #include "ppapi/c/pp_time.h"
17 #include "ppapi/cpp/dev/scrollbar_dev.h"
18 #include "ppapi/cpp/message_loop.h"
19 #include "ppapi/cpp/view.h"
20 #include "ppapi/tests/test_utils.h"
21 #include "ppapi/tests/testing_instance.h"
23 #if (defined __native_client__)
24 #include "ppapi/cpp/var.h"
26 #include "ppapi/cpp/private/var_private.h"
29 class TestingInstance
;
32 namespace deprecated
{
33 class ScriptableObject
;
37 // Individual classes of tests derive from this generic test case.
40 explicit TestCase(TestingInstance
* instance
);
43 // Optionally override to do testcase specific initialization.
44 // Default implementation just returns true.
47 // Override to implement the test case. It will be called after the plugin is
48 // first displayed, passing a string. If the string is empty, RunTests should
49 // run all tests for this test case. Otherwise, it must be a comma-delimited
50 // list of test names, possibly prefixed. E.g.:
51 // "Foo_GoodTest,DISABLED_Foo_BadTest,Foo_OtherGoodTest"
52 // All listed tests which are not prefixed will be run.
54 // This should generally be implemented in a TestCase subclass using the
56 virtual void RunTests(const std::string
& test_filter
) = 0;
58 static std::string
MakeFailureMessage(const char* file
, int line
,
61 #if !(defined __native_client__)
62 // Returns the scriptable test object for the current test, if any.
63 // Internally, this uses CreateTestObject which each test overrides.
64 pp::VarPrivate
GetTestObject();
67 // A function that is invoked whenever HandleMessage is called on the
68 // associated TestingInstance. Default implementation does nothing. TestCases
69 // that want to handle incoming postMessage events should override this
71 virtual void HandleMessage(const pp::Var
& message_data
);
73 // A function that is invoked whenever DidChangeView is called on the
74 // associated TestingInstance. Default implementation does nothing. TestCases
75 // that want to handle view changes should override this method.
76 virtual void DidChangeView(const pp::View
& view
);
78 // A function that is invoked whenever HandleInputEvent is called on the
79 // associated TestingInstance. Default implementation returns false. TestCases
80 // that want to handle view changes should override this method.
81 virtual bool HandleInputEvent(const pp::InputEvent
& event
);
83 void IgnoreLeakedVar(int64_t id
);
85 TestingInstance
* instance() { return instance_
; }
87 const PPB_Testing_Dev
* testing_interface() { return testing_interface_
; }
89 static void QuitMainMessageLoop(PP_Instance instance
);
91 const std::map
<std::string
, bool>& remaining_tests() {
92 return remaining_tests_
;
94 const std::set
<std::string
>& skipped_tests() {
95 return skipped_tests_
;
99 #if !(defined __native_client__)
100 // Overridden by each test to supply a ScriptableObject corresponding to the
101 // test. There can only be one object created for all tests in a given class,
102 // so be sure your object is designed to be re-used.
104 // This object should be created on the heap. Ownership will be passed to the
105 // caller. Return NULL if there is no supported test object (the default).
106 virtual pp::deprecated::ScriptableObject
* CreateTestObject();
109 // Checks whether the testing interface is available. Returns true if it is,
110 // false otherwise. If it is not available, adds a descriptive error. This is
111 // for use by tests that require the testing interface.
112 bool CheckTestingInterface();
114 // Makes sure the test is run over HTTP.
115 bool EnsureRunningOverHTTP();
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
);
122 // Return true if the given test name matches the filter. This is true if
123 // (a) filter is empty or (b) test_name matches a test name listed in filter
125 bool ShouldRunTest(const std::string
& test_name
, const std::string
& filter
);
127 // Check for leaked resources and vars at the end of the test. If any exist,
128 // return a string with some information about the error. Otherwise, return
131 // You should pass the error string from the test so far; if it is non-empty,
132 // CheckResourcesAndVars will do nothing and return the same string.
133 std::string
CheckResourcesAndVars(std::string errors
);
135 PP_TimeTicks
NowInTimeTicks();
137 // Run the given test method on a background thread and return the result.
139 std::string
RunOnThread(std::string(T::*test_to_run
)()) {
140 if (!testing_interface_
) {
141 return "Testing blocking callbacks requires the testing interface. In "
142 "Chrome, use the --enable-pepper-testing flag.";
144 // These tests are only valid if running out-of-process (threading is not
145 // supported in-process). For in-process, just consider it a pass.
146 if (!testing_interface_
->IsOutOfProcess())
147 return std::string();
148 pp::MessageLoop
background_loop(instance_
);
149 ThreadedTestRunner
<T
> runner(instance_
->pp_instance(),
150 static_cast<T
*>(this), test_to_run
, background_loop
);
151 RunOnThreadInternal(&ThreadedTestRunner
<T
>::ThreadFunction
, &runner
,
153 return runner
.result();
156 // Pointer to the instance that owns us.
157 TestingInstance
* instance_
;
159 // NULL unless InitTestingInterface is called.
160 const PPB_Testing_Dev
* testing_interface_
;
162 void set_callback_type(CallbackType callback_type
) {
163 callback_type_
= callback_type
;
165 CallbackType
callback_type() const {
166 return callback_type_
;
171 class ThreadedTestRunner
{
173 typedef std::string(T::*TestMethodType
)();
174 ThreadedTestRunner(PP_Instance instance
,
176 TestMethodType test_to_run
,
177 pp::MessageLoop loop
)
178 : instance_(instance
),
179 test_case_(test_case
),
180 test_to_run_(test_to_run
),
183 const std::string
& result() { return result_
; }
184 static void ThreadFunction(void* runner
) {
185 static_cast<ThreadedTestRunner
<T
>*>(runner
)->Run();
190 PP_DCHECK(PP_OK
== loop_
.AttachToCurrentThread());
191 result_
= (test_case_
->*test_to_run_
)();
192 // Now give the loop a chance to clean up.
193 loop_
.PostQuit(true /* should_destroy */);
195 // Tell the main thread to quit its nested message loop, now that the test
197 TestCase::QuitMainMessageLoop(instance_
);
201 PP_Instance instance_
;
203 TestMethodType test_to_run_
;
204 pp::MessageLoop loop_
;
207 // The internals for RunOnThread. This allows us to avoid including
208 // pp_thread.h in this header file, since it includes system headers like
210 // RunOnThreadInternal launches a new thread to run |thread_func|, waits
211 // for it to complete using RunMessageLoop(), then joins.
212 void RunOnThreadInternal(void (*thread_func
)(void*),
214 const PPB_Testing_Dev
* testing_interface
);
216 static void DoQuitMainMessageLoop(void* pp_instance
, int32_t result
);
218 // Passed when creating completion callbacks in some tests. This determines
219 // what kind of callback we use for the test.
220 CallbackType callback_type_
;
222 // Var ids that should be ignored when checking for leaks on shutdown.
223 std::set
<int64_t> ignored_leaked_vars_
;
225 // The tests that were found in test_filter but have not yet been run. The
226 // bool indicates whether the test should be run (i.e., it will be false if
227 // the test name was prefixed in the test_filter string).
229 // This is initialized lazily the first time that ShouldRunTest is called by
230 // RunTests. When RunTests is finished, this should be empty. Any remaining
231 // tests are tests that were listed in the test_filter but didn't match
232 // any calls to ShouldRunTest, meaning it was probably a typo. TestingInstance
233 // should log this and consider it a failure.
234 std::map
<std::string
, bool> remaining_tests_
;
235 // Flag indicating whether we have populated remaining_tests_ yet.
236 bool have_populated_remaining_tests_
;
238 // If ShouldRunTest is called but the given test name doesn't match anything
239 // in the test_filter, the test name will be added here. This allows
240 // TestingInstance to detect when not all tests were listed.
241 std::set
<std::string
> skipped_tests_
;
243 #if !(defined __native_client__)
244 // Holds the test object, if any was retrieved from CreateTestObject.
245 pp::VarPrivate test_object_
;
249 // This class is an implementation detail.
250 class TestCaseFactory
{
252 typedef TestCase
* (*Method
)(TestingInstance
* instance
);
254 TestCaseFactory(const char* name
, Method method
)
262 friend class TestingInstance
;
264 TestCaseFactory
* next_
;
268 static TestCaseFactory
* head_
;
271 // Use the REGISTER_TEST_CASE macro in your TestCase implementation file to
272 // register your TestCase. If your test is named TestFoo, then add the
273 // following to test_foo.cc:
275 // REGISTER_TEST_CASE(Foo);
277 // This will cause your test to be included in the set of known tests.
279 #define REGISTER_TEST_CASE(name) \
280 static TestCase* Test##name##_FactoryMethod(TestingInstance* instance) { \
281 return new Test##name(instance); \
283 static TestCaseFactory g_Test##name_factory( \
284 #name, &Test##name##_FactoryMethod \
287 // Helper macro for calling functions implementing specific tests in the
288 // RunTest function. This assumes the function name is TestFoo where Foo is the
290 #define RUN_TEST(name, test_filter) \
291 if (ShouldRunTest(#name, test_filter)) { \
292 set_callback_type(PP_OPTIONAL); \
293 PP_TimeTicks start_time(NowInTimeTicks()); \
294 instance_->LogTest(#name, \
295 CheckResourcesAndVars(Test##name()), \
299 // Like RUN_TEST above but forces functions taking callbacks to complete
300 // asynchronously on success or error.
301 #define RUN_TEST_FORCEASYNC(name, test_filter) \
302 if (ShouldRunTest(#name, test_filter)) { \
303 set_callback_type(PP_REQUIRED); \
304 PP_TimeTicks start_time(NowInTimeTicks()); \
305 instance_->LogTest(#name"ForceAsync", \
306 CheckResourcesAndVars(Test##name()), \
310 #define RUN_TEST_BLOCKING(test_case, name, test_filter) \
311 if (ShouldRunTest(#name, test_filter)) { \
312 set_callback_type(PP_BLOCKING); \
313 PP_TimeTicks start_time(NowInTimeTicks()); \
314 instance_->LogTest( \
316 CheckResourcesAndVars(RunOnThread(&test_case::Test##name)), \
320 #define RUN_TEST_BACKGROUND(test_case, name, test_filter) \
321 if (ShouldRunTest(#name, test_filter)) { \
322 PP_TimeTicks start_time(NowInTimeTicks()); \
323 instance_->LogTest( \
325 CheckResourcesAndVars(RunOnThread(&test_case::Test##name)), \
329 #define RUN_TEST_FORCEASYNC_AND_NOT(name, test_filter) \
331 RUN_TEST_FORCEASYNC(name, test_filter); \
332 RUN_TEST(name, test_filter); \
335 // Run a test with all possible callback types.
336 #define RUN_CALLBACK_TEST(test_case, name, test_filter) \
338 RUN_TEST_FORCEASYNC(name, test_filter); \
339 RUN_TEST(name, test_filter); \
340 RUN_TEST_BLOCKING(test_case, name, test_filter); \
341 RUN_TEST_BACKGROUND(test_case, name, test_filter); \
344 #define RUN_TEST_WITH_REFERENCE_CHECK(name, test_filter) \
345 if (ShouldRunTest(#name, test_filter)) { \
346 set_callback_type(PP_OPTIONAL); \
347 uint32_t objects = testing_interface_->GetLiveObjectsForInstance( \
348 instance_->pp_instance()); \
349 std::string error_message = Test##name(); \
350 if (error_message.empty() && \
351 testing_interface_->GetLiveObjectsForInstance( \
352 instance_->pp_instance()) != objects) \
353 error_message = MakeFailureMessage(__FILE__, __LINE__, \
354 "reference leak check"); \
355 PP_TimeTicks start_time(NowInTimeTicks()); \
356 instance_->LogTest(#name, \
357 CheckResourcesAndVars(error_message), \
360 // TODO(dmichael): Add CheckResourcesAndVars above when Windows tests pass
361 // cleanly. crbug.com/173503
363 // Helper macros for checking values in tests, and returning a location
364 // description of the test fails.
365 #define ASSERT_TRUE(cmd) \
368 return MakeFailureMessage(__FILE__, __LINE__, #cmd); \
370 #define ASSERT_FALSE(cmd) ASSERT_TRUE(!(cmd))
371 #define ASSERT_EQ(a, b) ASSERT_TRUE((a) == (b))
372 #define ASSERT_NE(a, b) ASSERT_TRUE((a) != (b))
373 #define ASSERT_LT(a, b) ASSERT_TRUE((a) < (b))
374 #define ASSERT_LE(a, b) ASSERT_TRUE((a) <= (b))
375 #define ASSERT_GT(a, b) ASSERT_TRUE((a) > (b))
376 #define ASSERT_GE(a, b) ASSERT_TRUE((a) >= (b))
378 #define ASSERT_DOUBLE_EQ(a, b) ASSERT_TRUE( \
379 std::fabs((a)-(b)) <= std::numeric_limits<double>::epsilon())
381 // Runs |function| as a subtest and asserts that it has passed.
382 #define ASSERT_SUBTEST_SUCCESS(function) \
384 std::string result = (function); \
385 if (!result.empty()) \
389 #define PASS() return std::string()
391 #endif // PPAPI_TESTS_TEST_CASE_H_