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.
4
5#ifndef PPAPI_TESTS_TEST_CASE_H_
6#define PPAPI_TESTS_TEST_CASE_H_
7
8#include <cmath>
9#include <limits>
10#include <map>
11#include <set>
12#include <sstream>
13#include <string>
14
15#include "ppapi/c/pp_resource.h"
16#include "ppapi/c/pp_time.h"
17#include "ppapi/c/private/ppb_testing_private.h"
18#include "ppapi/cpp/dev/scrollbar_dev.h"
19#include "ppapi/cpp/message_loop.h"
20#include "ppapi/cpp/view.h"
21#include "ppapi/tests/test_utils.h"
22#include "ppapi/tests/testing_instance.h"
23
24#if (defined __native_client__)
25#include "ppapi/cpp/var.h"
26#else
27#include "ppapi/cpp/private/var_private.h"
28#endif
29
30class TestingInstance;
31
32namespace pp {
33namespace deprecated {
34class ScriptableObject;
35}
36}
37
38// Individual classes of tests derive from this generic test case.
39class TestCase {
40 public:
41  explicit TestCase(TestingInstance* instance);
42  virtual ~TestCase();
43
44  // Optionally override to do testcase specific initialization.
45  // Default implementation just returns true.
46  virtual bool Init();
47
48  // Override to implement the test case. It will be called after the plugin is
49  // first displayed, passing a string. If the string is empty, RunTests should
50  // run all tests for this test case. Otherwise, it must be a comma-delimited
51  // list of test names, possibly prefixed. E.g.:
52  //   "Foo_GoodTest,DISABLED_Foo_BadTest,Foo_OtherGoodTest"
53  // All listed tests which are not prefixed will be run.
54  //
55  // This should generally be implemented in a TestCase subclass using the
56  // RUN_TEST* macros.
57  virtual void RunTests(const std::string& test_filter) = 0;
58
59  static std::string MakeFailureMessage(const char* file, int line,
60                                        const char* cmd);
61
62#if !(defined __native_client__)
63  // Returns the scriptable test object for the current test, if any.
64  // Internally, this uses CreateTestObject which each test overrides.
65  pp::VarPrivate GetTestObject();
66  void ResetTestObject() { test_object_ = pp::VarPrivate(); }
67#endif
68
69  // A function that is invoked whenever HandleMessage is called on the
70  // associated TestingInstance. Default implementation does nothing.  TestCases
71  // that want to handle incoming postMessage events should override this
72  // method.
73  virtual void HandleMessage(const pp::Var& message_data);
74
75  // A function that is invoked whenever DidChangeView is called on the
76  // associated TestingInstance. Default implementation does nothing. TestCases
77  // that want to handle view changes should override this method.
78  virtual void DidChangeView(const pp::View& view);
79
80  // A function that is invoked whenever HandleInputEvent is called on the
81  // associated TestingInstance. Default implementation returns false. TestCases
82  // that want to handle view changes should override this method.
83  virtual bool HandleInputEvent(const pp::InputEvent& event);
84
85  void IgnoreLeakedVar(int64_t id);
86
87  TestingInstance* instance() { return instance_; }
88
89  const PPB_Testing_Private* testing_interface() { return testing_interface_; }
90
91  static void QuitMainMessageLoop(PP_Instance instance);
92
93  const std::map<std::string, bool>& remaining_tests() {
94    return remaining_tests_;
95  }
96  const std::set<std::string>& skipped_tests() {
97    return skipped_tests_;
98  }
99
100 protected:
101#if !(defined __native_client__)
102  // Overridden by each test to supply a ScriptableObject corresponding to the
103  // test. There can only be one object created for all tests in a given class,
104  // so be sure your object is designed to be re-used.
105  //
106  // This object should be created on the heap. Ownership will be passed to the
107  // caller. Return NULL if there is no supported test object (the default).
108  virtual pp::deprecated::ScriptableObject* CreateTestObject();
109#endif
110
111  // Checks whether the testing interface is available. Returns true if it is,
112  // false otherwise. If it is not available, adds a descriptive error. This is
113  // for use by tests that require the testing interface.
114  bool CheckTestingInterface();
115
116  // Makes sure the test is run over HTTP.
117  bool EnsureRunningOverHTTP();
118
119  // Returns true if |filter| only contains a TestCase name, which normally
120  // means "run all tests". Some TestCases require special setup for individual
121  // tests, and can use this function to decide whether to ignore those tests.
122  bool ShouldRunAllTests(const std::string& filter);
123
124  // Return true if the given test name matches the filter. This is true if
125  // (a) filter is empty or (b) test_name matches a test name listed in filter
126  // exactly.
127  bool ShouldRunTest(const std::string& test_name, const std::string& filter);
128
129  // Check for leaked resources and vars at the end of the test. If any exist,
130  // return a string with some information about the error. Otherwise, return
131  // an empty string.
132  //
133  // You should pass the error string from the test so far; if it is non-empty,
134  // CheckResourcesAndVars will do nothing and return the same string.
135  std::string CheckResourcesAndVars(std::string errors);
136
137  PP_TimeTicks NowInTimeTicks();
138
139  // Run the given test method on a background thread and return the result.
140  template <class T>
141  std::string RunOnThread(std::string(T::*test_to_run)()) {
142    if (!testing_interface_) {
143      return "Testing blocking callbacks requires the testing interface. In "
144             "Chrome, use the --enable-pepper-testing flag.";
145    }
146    // These tests are only valid if running out-of-process (threading is not
147    // supported in-process). For in-process, just consider it a pass.
148    if (!testing_interface_->IsOutOfProcess())
149      return std::string();
150    pp::MessageLoop background_loop(instance_);
151    ThreadedTestRunner<T> runner(instance_->pp_instance(),
152        static_cast<T*>(this), test_to_run, background_loop);
153    RunOnThreadInternal(&ThreadedTestRunner<T>::ThreadFunction, &runner,
154                        testing_interface_);
155    return runner.result();
156  }
157
158  // Pointer to the instance that owns us.
159  TestingInstance* instance_;
160
161  // NULL unless InitTestingInterface is called.
162  const PPB_Testing_Private* testing_interface_;
163
164  void set_callback_type(CallbackType callback_type) {
165    callback_type_ = callback_type;
166  }
167  CallbackType callback_type() const {
168    return callback_type_;
169  }
170
171 private:
172  template <class T>
173  class ThreadedTestRunner {
174   public:
175    typedef std::string(T::*TestMethodType)();
176    ThreadedTestRunner(PP_Instance instance,
177                       T* test_case,
178                       TestMethodType test_to_run,
179                       pp::MessageLoop loop)
180        : instance_(instance),
181          test_case_(test_case),
182          test_to_run_(test_to_run),
183          loop_(loop) {
184    }
185    const std::string& result() { return result_; }
186    static void ThreadFunction(void* runner) {
187      static_cast<ThreadedTestRunner<T>*>(runner)->Run();
188    }
189
190   private:
191    void Run() {
192      int32_t result = loop_.AttachToCurrentThread();
193      static_cast<void>(result); // result is not used in the RELEASE build.
194      PP_DCHECK(PP_OK == result);
195      result_ = (test_case_->*test_to_run_)();
196      // Now give the loop a chance to clean up.
197      loop_.PostQuit(true /* should_destroy */);
198      loop_.Run();
199      // Tell the main thread to quit its nested message loop, now that the test
200      // is complete.
201      TestCase::QuitMainMessageLoop(instance_);
202    }
203
204    std::string result_;
205    PP_Instance instance_;
206    T* test_case_;
207    TestMethodType test_to_run_;
208    pp::MessageLoop loop_;
209  };
210
211  // The internals for RunOnThread. This allows us to avoid including
212  // pp_thread.h in this header file, since it includes system headers like
213  // windows.h.
214  // RunOnThreadInternal launches a new thread to run |thread_func|, waits
215  // for it to complete using RunMessageLoop(), then joins.
216  void RunOnThreadInternal(void (*thread_func)(void*),
217                           void* thread_param,
218                           const PPB_Testing_Private* testing_interface);
219
220  static void DoQuitMainMessageLoop(void* pp_instance, int32_t result);
221
222  // Passed when creating completion callbacks in some tests. This determines
223  // what kind of callback we use for the test.
224  CallbackType callback_type_;
225
226  // Var ids that should be ignored when checking for leaks on shutdown.
227  std::set<int64_t> ignored_leaked_vars_;
228
229  // The tests that were found in test_filter. The bool indicates whether the
230  // test should be run (i.e., it will be false if the test name was prefixed in
231  // the test_filter string).
232  //
233  // This is initialized lazily the first time that ShouldRunTest is called.
234  std::map<std::string, bool> filter_tests_;
235  // Flag indicating whether we have populated filter_tests_ yet.
236  bool have_populated_filter_tests_;
237  // This is initialized with the contents of filter_tests_. As each test is
238  // run, it is removed from remaining_tests_. When RunTests is finished,
239  // remaining_tests_ should be empty. Any remaining tests are tests that were
240  // listed in the test_filter but didn't match any calls to ShouldRunTest,
241  // meaning it was probably a typo. TestingInstance should log this and
242  // consider it a failure.
243  std::map<std::string, bool> remaining_tests_;
244
245  // If ShouldRunTest 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
250#if !(defined __native_client__)
251  // Holds the test object, if any was retrieved from CreateTestObject.
252  pp::VarPrivate test_object_;
253#endif
254};
255
256// This class is an implementation detail.
257class TestCaseFactory {
258 public:
259  typedef TestCase* (*Method)(TestingInstance* instance);
260
261  TestCaseFactory(const char* name, Method method)
262      : next_(head_),
263        name_(name),
264        method_(method) {
265    head_ = this;
266  }
267
268 private:
269  friend class TestingInstance;
270
271  TestCaseFactory* next_;
272  const char* name_;
273  Method method_;
274
275  static TestCaseFactory* head_;
276};
277
278namespace internal {
279
280// The internal namespace contains implementation details that are used by
281// the ASSERT macros.
282
283// This base class provides a ToString that works for classes that can be
284// converted to a string using std::stringstream. Later, we'll do
285// specializations for types that we know will work with this approach.
286template <class T>
287struct StringinatorBase {
288  static std::string ToString(const T& value) {
289    std::stringstream stream;
290    stream << value;
291    return stream.str();
292  }
293 protected:
294  // Not implemented, do not use.
295  // Note, these are protected because Windows complains if I make these private
296  // and then inherit StringinatorBase (even though they're never used).
297  StringinatorBase();
298  ~StringinatorBase();
299};
300
301// This default class template is for types that we don't recognize as
302// something we can convert into a string using stringstream. Types that we
303// know *can* be turned to a string should have specializations below.
304template <class T>
305struct Stringinator {
306  static std::string ToString(const T& value) {
307    return std::string();
308  }
309 private:
310  // Not implemented, do not use.
311  Stringinator();
312  ~Stringinator();
313};
314
315// Define some full specializations for types that can just use stringstream.
316#define DEFINE_STRINGINATOR_FOR_TYPE(type) \
317template <> \
318struct Stringinator<type> : public StringinatorBase<type> {};
319DEFINE_STRINGINATOR_FOR_TYPE(int32_t);
320DEFINE_STRINGINATOR_FOR_TYPE(uint32_t);
321DEFINE_STRINGINATOR_FOR_TYPE(int64_t);
322DEFINE_STRINGINATOR_FOR_TYPE(uint64_t);
323DEFINE_STRINGINATOR_FOR_TYPE(float);
324DEFINE_STRINGINATOR_FOR_TYPE(double);
325DEFINE_STRINGINATOR_FOR_TYPE(bool);
326DEFINE_STRINGINATOR_FOR_TYPE(std::string);
327#undef DEFINE_STRINGINATOR_FOR_TYPE
328
329template <class T>
330std::string ToString(const T& param) {
331  return Stringinator<T>::ToString(param);
332}
333
334// This overload is necessary to allow enum values (such as those from
335// pp_errors.h, including PP_OK) to work. They won't automatically convert to
336// an integral type to instantiate the above function template.
337inline std::string ToString(int32_t param) {
338  return Stringinator<int32_t>::ToString(param);
339}
340
341inline std::string ToString(const char* c_string) {
342  return std::string(c_string);
343}
344
345// This overload deals with pointers.
346template <class T>
347std::string ToString(const T* ptr) {
348  uintptr_t ptr_val = reinterpret_cast<uintptr_t>(ptr);
349  std::stringstream stream;
350  stream << ptr_val;
351  return stream.str();
352}
353
354// ComparisonHelper classes wrap the left-hand parameter of a binary comparison
355// ASSERT. The correct class gets chosen based on whether or not it's a NULL or
356// 0 literal. If it is a NULL/0 literal, we use NullLiteralComparisonHelper.
357// For all other parameters, we use ComparisonHelper. There's also a
358// specialization of ComparisonHelper for int below (see below for why
359// that is.)
360//
361// ComparisonHelper does two things for the left param:
362//  1) Provides all the appropriate CompareXX functions (CompareEQ, etc).
363//  2) Provides ToString.
364template <class T>
365struct ComparisonHelper {
366  explicit ComparisonHelper(const T& param) : value(param) {}
367  template <class U>
368  bool CompareEQ(const U& right) const {
369    return value == right;
370  }
371  template <class U>
372  bool CompareNE(const U& right) const {
373    return value != right;
374  }
375  template <class U>
376  bool CompareLT(const U& right) const {
377    return value < right;
378  }
379  template <class U>
380  bool CompareGT(const U& right) const {
381    return value > right;
382  }
383  template <class U>
384  bool CompareLE(const U& right) const {
385    return value <= right;
386  }
387  template <class U>
388  bool CompareGE(const U& right) const {
389    return value >= right;
390  }
391  std::string ToString() const {
392    return internal::ToString(value);
393  }
394  const T& value;
395};
396
397// Used for NULL or 0.
398struct NullLiteralComparisonHelper {
399  NullLiteralComparisonHelper() : value(0) {}
400  template <class U>
401  bool CompareEQ(const U& right) const {
402    return 0 == right;
403  }
404  template <class U>
405  bool CompareNE(const U& right) const {
406    return 0 != right;
407  }
408  template <class U>
409  bool CompareLT(const U& right) const {
410    return 0 < right;
411  }
412  template <class U>
413  bool CompareGT(const U& right) const {
414    return 0 > right;
415  }
416  template <class U>
417  bool CompareLE(const U& right) const {
418    return 0 <= right;
419  }
420  template <class U>
421  bool CompareGE(const U& right) const {
422    return 0 >= right;
423  }
424  std::string ToString() const {
425    return std::string("0");
426  }
427  const int value;
428};
429
430// This class makes it safe to use an integer literal (like 5, or 123) when
431// comparing with an unsigned. For example:
432// ASSERT_EQ(1, some_vector.size());
433// We do a lot of those comparisons, so this makes it easy to get it right
434// (rather than forcing assertions to use unsigned literals like 5u or 123u).
435//
436// This is slightly risky; we're static_casting an int to whatever's on the
437// right. If the left value is negative and the right hand side is a large
438// unsigned value, it's possible that the comparison will succeed when maybe
439// it shouldn't have.
440// TODO(dmichael): It should be possible to fix this and upgrade int32_t and
441//                 uint32_t to int64_t for the comparison, and make any unsafe
442//                 comparisons into compile errors.
443template <>
444struct ComparisonHelper<int> {
445  explicit ComparisonHelper(int param) : value(param) {}
446  template <class U>
447  bool CompareEQ(const U& right) const {
448    return static_cast<U>(value) == right;
449  }
450  template <class U>
451  bool CompareNE(const U& right) const {
452    return static_cast<U>(value) != right;
453  }
454  template <class U>
455  bool CompareLT(const U& right) const {
456    return static_cast<U>(value) < right;
457  }
458  template <class U>
459  bool CompareGT(const U& right) const {
460    return static_cast<U>(value) > right;
461  }
462  template <class U>
463  bool CompareLE(const U& right) const {
464    return static_cast<U>(value) <= right;
465  }
466  template <class U>
467  bool CompareGE(const U& right) const {
468    return static_cast<U>(value) >= right;
469  }
470  std::string ToString() const {
471    return internal::ToString(value);
472  }
473  const int value;
474 private:
475};
476
477// The default is for the case there the parameter is *not* a NULL or 0 literal.
478template <bool is_null_literal>
479struct ParameterWrapper {
480  template <class T>
481  static ComparisonHelper<T> WrapValue(const T& value) {
482    return ComparisonHelper<T>(value);
483  }
484  // This overload is so that we can deal with values from anonymous enums,
485  // like the one in pp_errors.h. The function template above won't be
486  // considered a match by the compiler.
487  static ComparisonHelper<int> WrapValue(int value) {
488    return ComparisonHelper<int>(value);
489  }
490};
491
492// The parameter to WrapValue *is* a NULL or 0 literal.
493template <>
494struct ParameterWrapper<true> {
495  // We just use "..." and ignore the parameter. This sidesteps some problems we
496  // would run in to (not all compilers have the same set of constraints).
497  // - We can't use a pointer type, because int and enums won't convert.
498  // - We can't use an integral type, because pointers won't convert.
499  // - We can't overload, because it will sometimes be ambiguous.
500  // - We can't templatize and deduce the parameter. Some compilers will deduce
501  //   int for NULL, and then refuse to convert NULL to an int.
502  //
503  // We know in this case that the value is 0, so there's no need to capture the
504  // value. We also know it's a fundamental type, so it's safe to pass to "...".
505  // (It's illegal to pass non-POD types to ...).
506  static NullLiteralComparisonHelper WrapValue(...) {
507    return NullLiteralComparisonHelper();
508  }
509};
510
511// IS_NULL_LITERAL(type) is a little template metaprogramming for determining
512// if a type is a null or zero literal (NULL or 0 or a constant that evaluates
513// to one of those).
514// The idea is that for NULL or 0, any pointer type is always a better match
515// than "...". But no other pointer types or literals should convert
516// automatically to InternalDummyClass.
517struct InternalDummyClass {};
518char TestNullLiteral(const InternalDummyClass*);
519struct BiggerThanChar { char dummy[2]; };
520BiggerThanChar TestNullLiteral(...);
521// If the compiler chooses the overload of TestNullLiteral which returns char,
522// then we know the value converts automatically to InternalDummyClass*, which
523// should only be true of NULL and 0 constants.
524#define IS_NULL_LITERAL(a) sizeof(internal::TestNullLiteral(a)) == sizeof(char)
525
526template <class T, class U>
527static std::string MakeBinaryComparisonFailureMessage(
528    const char* comparator,
529    const T& left,
530    const U& right,
531    const char* left_precompiler_string,
532    const char* right_precompiler_string,
533    const char* file_name,
534    int line_number) {
535  std::string error_msg =
536      std::string("Failed ASSERT_") + comparator + "(" +
537      left_precompiler_string + ", " + right_precompiler_string + ")";
538  std::string left_string(left.ToString());
539  std::string right_string(ToString(right));
540  if (!left_string.empty())
541    error_msg += " Left: (" + left_string + ")";
542
543  if (!right_string.empty())
544    error_msg += " Right: (" + right_string + ")";
545
546  return TestCase::MakeFailureMessage(file_name, line_number,
547                                      error_msg.c_str());
548}
549
550// The Comparison function templates allow us to pass the parameter for
551// ASSERT macros below and have them be evaluated only once. This is important
552// for cases where the parameter might be an expression with side-effects, like
553// a function call.
554#define DEFINE_COMPARE_FUNCTION(comparator_name) \
555template <class T, class U> \
556std::string Compare ## comparator_name ( \
557    const T& left, \
558    const U& right, \
559    const char* left_precompiler_string, \
560    const char* right_precompiler_string, \
561    const char* file_name, \
562    int line_num) { \
563  if (!(left.Compare##comparator_name(right))) { \
564    return MakeBinaryComparisonFailureMessage(#comparator_name, \
565                                              left, \
566                                              right, \
567                                              left_precompiler_string, \
568                                              right_precompiler_string, \
569                                              file_name, \
570                                              line_num); \
571  } \
572  return std::string(); \
573}
574DEFINE_COMPARE_FUNCTION(EQ)
575DEFINE_COMPARE_FUNCTION(NE)
576DEFINE_COMPARE_FUNCTION(LT)
577DEFINE_COMPARE_FUNCTION(LE)
578DEFINE_COMPARE_FUNCTION(GT)
579DEFINE_COMPARE_FUNCTION(GE)
580#undef DEFINE_COMPARE_FUNCTION
581inline std::string CompareDoubleEq(ComparisonHelper<double> left,
582                                   double right,
583                                   const char* left_precompiler_string,
584                                   const char* right_precompiler_string,
585                                   const char* file_name,
586                                   int linu_num) {
587  if (!(std::fabs(left.value - right) <=
588        std::numeric_limits<double>::epsilon())) {
589    return MakeBinaryComparisonFailureMessage(
590        "~=", left, right, left_precompiler_string, right_precompiler_string,
591        __FILE__, __LINE__);
592  }
593  return std::string();
594}
595
596}  // namespace internal
597
598// Use the REGISTER_TEST_CASE macro in your TestCase implementation file to
599// register your TestCase.  If your test is named TestFoo, then add the
600// following to test_foo.cc:
601//
602//   REGISTER_TEST_CASE(Foo);
603//
604// This will cause your test to be included in the set of known tests.
605//
606#define REGISTER_TEST_CASE(name)                                            \
607  static TestCase* Test##name##_FactoryMethod(TestingInstance* instance) {  \
608    return new Test##name(instance);                                        \
609  }                                                                         \
610  static TestCaseFactory g_Test##name_factory(                              \
611    #name, &Test##name##_FactoryMethod                                      \
612  )
613
614// Helper macro for calling functions implementing specific tests in the
615// RunTest function. This assumes the function name is TestFoo where Foo is the
616// test |name|.
617#define RUN_TEST(name, test_filter) \
618  if (ShouldRunTest(#name, test_filter)) { \
619    set_callback_type(PP_OPTIONAL); \
620    PP_TimeTicks start_time(NowInTimeTicks()); \
621    instance_->LogTest(#name, \
622                       CheckResourcesAndVars(Test##name()), \
623                       start_time); \
624  }
625
626// Like RUN_TEST above but forces functions taking callbacks to complete
627// asynchronously on success or error.
628#define RUN_TEST_FORCEASYNC(name, test_filter) \
629  if (ShouldRunTest(#name, test_filter)) { \
630    set_callback_type(PP_REQUIRED); \
631    PP_TimeTicks start_time(NowInTimeTicks()); \
632    instance_->LogTest(#name"ForceAsync", \
633                       CheckResourcesAndVars(Test##name()), \
634                       start_time); \
635  }
636
637#define RUN_TEST_BLOCKING(test_case, name, test_filter) \
638  if (ShouldRunTest(#name, test_filter)) { \
639    set_callback_type(PP_BLOCKING); \
640    PP_TimeTicks start_time(NowInTimeTicks()); \
641    instance_->LogTest( \
642        #name"Blocking", \
643        CheckResourcesAndVars(RunOnThread(&test_case::Test##name)), \
644        start_time); \
645  }
646
647#define RUN_TEST_BACKGROUND(test_case, name, test_filter) \
648  if (ShouldRunTest(#name, test_filter)) { \
649    PP_TimeTicks start_time(NowInTimeTicks()); \
650    instance_->LogTest( \
651        #name"Background", \
652        CheckResourcesAndVars(RunOnThread(&test_case::Test##name)), \
653        start_time); \
654  }
655
656#define RUN_TEST_FORCEASYNC_AND_NOT(name, test_filter) \
657  do { \
658    RUN_TEST_FORCEASYNC(name, test_filter); \
659    RUN_TEST(name, test_filter); \
660  } while (false)
661
662// Run a test with all possible callback types.
663#define RUN_CALLBACK_TEST(test_case, name, test_filter) \
664  do { \
665    RUN_TEST_FORCEASYNC(name, test_filter); \
666    RUN_TEST(name, test_filter); \
667    RUN_TEST_BLOCKING(test_case, name, test_filter); \
668    RUN_TEST_BACKGROUND(test_case, name, test_filter); \
669  } while (false)
670
671#define RUN_TEST_WITH_REFERENCE_CHECK(name, test_filter) \
672  if (ShouldRunTest(#name, test_filter)) { \
673    set_callback_type(PP_OPTIONAL); \
674    uint32_t objects = testing_interface_->GetLiveObjectsForInstance( \
675        instance_->pp_instance()); \
676    std::string error_message = Test##name(); \
677    if (error_message.empty() && \
678        testing_interface_->GetLiveObjectsForInstance( \
679            instance_->pp_instance()) != objects) \
680      error_message = MakeFailureMessage(__FILE__, __LINE__, \
681          "reference leak check"); \
682    PP_TimeTicks start_time(NowInTimeTicks()); \
683    instance_->LogTest(#name, \
684                       CheckResourcesAndVars(error_message), \
685                       start_time); \
686  }
687// TODO(dmichael): Add CheckResourcesAndVars above when Windows tests pass
688//                 cleanly. crbug.com/173503
689
690// Helper macros for checking values in tests, and returning a location
691// description of the test fails.
692#define ASSERT_TRUE(cmd) \
693  do { \
694    if (!(cmd)) \
695      return MakeFailureMessage(__FILE__, __LINE__, #cmd); \
696  } while (false)
697#define ASSERT_FALSE(cmd) ASSERT_TRUE(!(cmd))
698#define COMPARE_BINARY_INTERNAL(comparison_type, a, b) \
699    internal::Compare##comparison_type( \
700        internal::ParameterWrapper<IS_NULL_LITERAL(a)>::WrapValue(a), \
701        (b), \
702        #a, \
703        #b, \
704        __FILE__, \
705        __LINE__)
706#define ASSERT_BINARY_INTERNAL(comparison_type, a, b) \
707do { \
708  std::string internal_assert_result_string = \
709      COMPARE_BINARY_INTERNAL(comparison_type, a, b); \
710  if (!internal_assert_result_string.empty()) { \
711    return internal_assert_result_string; \
712  } \
713} while(false)
714#define ASSERT_EQ(a, b) ASSERT_BINARY_INTERNAL(EQ, a, b)
715#define ASSERT_NE(a, b) ASSERT_BINARY_INTERNAL(NE, a, b)
716#define ASSERT_LT(a, b) ASSERT_BINARY_INTERNAL(LT, a, b)
717#define ASSERT_LE(a, b) ASSERT_BINARY_INTERNAL(LE, a, b)
718#define ASSERT_GT(a, b) ASSERT_BINARY_INTERNAL(GT, a, b)
719#define ASSERT_GE(a, b) ASSERT_BINARY_INTERNAL(GE, a, b)
720#define ASSERT_DOUBLE_EQ(a, b) \
721do { \
722  std::string internal_assert_result_string = \
723      internal::CompareDoubleEq( \
724          internal::ParameterWrapper<IS_NULL_LITERAL(a)>::WrapValue(a), \
725          (b), \
726          #a, \
727          #b, \
728          __FILE__, \
729          __LINE__); \
730  if (!internal_assert_result_string.empty()) { \
731    return internal_assert_result_string; \
732  } \
733} while(false)
734// Runs |function| as a subtest and asserts that it has passed.
735#define ASSERT_SUBTEST_SUCCESS(function) \
736  do { \
737    std::string result = (function); \
738    if (!result.empty()) \
739      return TestCase::MakeFailureMessage(__FILE__, __LINE__, result.c_str()); \
740  } while (false)
741
742#define PASS() return std::string()
743
744#endif  // PPAPI_TESTS_TEST_CASE_H_
745