test_case.cc 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. #include "ppapi/tests/test_case.h"
  5. #include <stddef.h>
  6. #include <string.h>
  7. #include <algorithm>
  8. #include <sstream>
  9. #include "ppapi/cpp/core.h"
  10. #include "ppapi/cpp/module.h"
  11. #include "ppapi/tests/pp_thread.h"
  12. #include "ppapi/tests/test_utils.h"
  13. #include "ppapi/tests/testing_instance.h"
  14. namespace {
  15. std::string StripPrefix(const std::string& test_name) {
  16. if (test_name.find("DISABLED_") == 0)
  17. return test_name.substr(strlen("DISABLED_"));
  18. return test_name;
  19. }
  20. // Strip the TestCase name off and return the remainder (i.e., everything after
  21. // '_'). If there is no '_', assume only the TestCase was provided, and return
  22. // an empty string.
  23. // For example:
  24. // StripTestCase("TestCase_TestName");
  25. // returns
  26. // "TestName"
  27. // while
  28. // StripTestCase("TestCase);
  29. // returns
  30. // ""
  31. std::string StripTestCase(const std::string& full_test_name) {
  32. size_t delim = full_test_name.find_first_of('_');
  33. if (delim != std::string::npos)
  34. return full_test_name.substr(delim+1);
  35. // In this case, our "filter" is the empty string; the full test name is the
  36. // same as the TestCase name with which we were constructed.
  37. // TODO(dmichael): It might be nice to be able to PP_DCHECK against the
  38. // TestCase class name, but we'd have to plumb that name to TestCase somehow.
  39. return std::string();
  40. }
  41. // Parse |test_filter|, which is a comma-delimited list of (possibly prefixed)
  42. // test names and insert the un-prefixed names into |remaining_tests|, with
  43. // the bool indicating whether the test should be run.
  44. void ParseTestFilter(const std::string& test_filter,
  45. std::map<std::string, bool>* remaining_tests) {
  46. // We can't use base/strings/string_util.h::Tokenize in ppapi, so we have to
  47. // do it ourselves.
  48. std::istringstream filter_stream(test_filter);
  49. std::string current_test;
  50. while (std::getline(filter_stream, current_test, ',')) {
  51. // |current_test| might include a prefix, like DISABLED_Foo_TestBar, so we
  52. // we strip it off if there is one.
  53. std::string stripped_test_name(StripPrefix(current_test));
  54. // Strip off the test case and use the test name as a key, because the test
  55. // name ShouldRunTest wants to use to look up the test doesn't have the
  56. // TestCase name.
  57. std::string test_name_without_case(StripTestCase(stripped_test_name));
  58. // If the test wasn't prefixed, it should be run.
  59. bool should_run_test = (current_test == stripped_test_name);
  60. PP_DCHECK(remaining_tests->count(test_name_without_case) == 0);
  61. remaining_tests->insert(
  62. std::make_pair(test_name_without_case, should_run_test));
  63. }
  64. // There may be a trailing comma; ignore empty strings.
  65. remaining_tests->erase(std::string());
  66. }
  67. } // namespace
  68. TestCase::TestCase(TestingInstance* instance)
  69. : instance_(instance),
  70. testing_interface_(NULL),
  71. callback_type_(PP_REQUIRED),
  72. have_populated_filter_tests_(false) {
  73. // Get the testing_interface_ if it is available, so that we can do Resource
  74. // and Var checks on shutdown (see CheckResourcesAndVars). If it is not
  75. // available, testing_interface_ will be NULL. Some tests do not require it.
  76. testing_interface_ = GetTestingInterface();
  77. }
  78. TestCase::~TestCase() {
  79. }
  80. bool TestCase::Init() {
  81. return true;
  82. }
  83. // static
  84. std::string TestCase::MakeFailureMessage(const char* file,
  85. int line,
  86. const char* cmd) {
  87. std::ostringstream output;
  88. output << "Failure in " << file << "(" << line << "): " << cmd;
  89. return output.str();
  90. }
  91. #if !(defined __native_client__)
  92. pp::VarPrivate TestCase::GetTestObject() {
  93. if (test_object_.is_undefined()) {
  94. pp::deprecated::ScriptableObject* so = CreateTestObject();
  95. if (so) {
  96. test_object_ = pp::VarPrivate(instance_, so); // Takes ownership.
  97. // CheckResourcesAndVars runs and looks for leaks before we've actually
  98. // completely shut down. Ignore the instance object, since it's not a real
  99. // leak.
  100. IgnoreLeakedVar(test_object_.pp_var().value.as_id);
  101. }
  102. }
  103. return test_object_;
  104. }
  105. #endif
  106. bool TestCase::CheckTestingInterface() {
  107. testing_interface_ = GetTestingInterface();
  108. if (!testing_interface_) {
  109. // Give a more helpful error message for the testing interface being gone
  110. // since that needs special enabling in Chrome.
  111. instance_->AppendError("This test needs the testing interface, which is "
  112. "not currently available. In Chrome, use "
  113. "--enable-pepper-testing when launching.");
  114. return false;
  115. }
  116. return true;
  117. }
  118. void TestCase::HandleMessage(const pp::Var& message_data) {
  119. }
  120. void TestCase::DidChangeView(const pp::View& view) {
  121. }
  122. bool TestCase::HandleInputEvent(const pp::InputEvent& event) {
  123. return false;
  124. }
  125. void TestCase::IgnoreLeakedVar(int64_t id) {
  126. ignored_leaked_vars_.insert(id);
  127. }
  128. #if !(defined __native_client__)
  129. pp::deprecated::ScriptableObject* TestCase::CreateTestObject() {
  130. return NULL;
  131. }
  132. #endif
  133. bool TestCase::EnsureRunningOverHTTP() {
  134. if (instance_->protocol() != "http:") {
  135. instance_->AppendError("This test needs to be run over HTTP.");
  136. return false;
  137. }
  138. return true;
  139. }
  140. bool TestCase::ShouldRunAllTests(const std::string& filter) {
  141. // If only the TestCase is listed, we're running all the tests in RunTests.
  142. return (StripTestCase(filter) == std::string());
  143. }
  144. bool TestCase::ShouldRunTest(const std::string& test_name,
  145. const std::string& filter) {
  146. if (ShouldRunAllTests(filter))
  147. return true;
  148. // Lazily initialize our "filter_tests_" map.
  149. if (!have_populated_filter_tests_) {
  150. ParseTestFilter(filter, &filter_tests_);
  151. remaining_tests_ = filter_tests_;
  152. have_populated_filter_tests_ = true;
  153. }
  154. std::map<std::string, bool>::iterator iter = filter_tests_.find(test_name);
  155. if (iter == filter_tests_.end()) {
  156. // The test name wasn't listed in the filter. Don't run it, but store it
  157. // so TestingInstance::ExecuteTests can report an error later.
  158. skipped_tests_.insert(test_name);
  159. return false;
  160. }
  161. remaining_tests_.erase(test_name);
  162. return iter->second;
  163. }
  164. PP_TimeTicks TestCase::NowInTimeTicks() {
  165. return pp::Module::Get()->core()->GetTimeTicks();
  166. }
  167. std::string TestCase::CheckResourcesAndVars(std::string errors) {
  168. if (!errors.empty())
  169. return errors;
  170. if (testing_interface_) {
  171. // TODO(dmichael): Fix tests that leak resources and enable the following:
  172. /*
  173. uint32_t leaked_resources =
  174. testing_interface_->GetLiveObjectsForInstance(instance_->pp_instance());
  175. if (leaked_resources) {
  176. std::ostringstream output;
  177. output << "FAILED: Test leaked " << leaked_resources << " resources.\n";
  178. errors += output.str();
  179. }
  180. */
  181. const int kVarsToPrint = 100;
  182. PP_Var vars[kVarsToPrint];
  183. int found_vars = testing_interface_->GetLiveVars(vars, kVarsToPrint);
  184. // This will undercount if we are told to ignore a Var which is then *not*
  185. // leaked. Worst case, we just won't print the little "Test leaked" message,
  186. // but we'll still print any non-ignored leaked vars we found.
  187. int leaked_vars =
  188. found_vars - static_cast<int>(ignored_leaked_vars_.size());
  189. if (leaked_vars > 0) {
  190. std::ostringstream output;
  191. output << "Test leaked " << leaked_vars << " vars (printing at most "
  192. << kVarsToPrint <<"):<p>";
  193. errors += output.str();
  194. }
  195. for (int i = 0; i < std::min(found_vars, kVarsToPrint); ++i) {
  196. pp::Var leaked_var(pp::PASS_REF, vars[i]);
  197. if (ignored_leaked_vars_.count(leaked_var.pp_var().value.as_id) == 0)
  198. errors += leaked_var.DebugString() + "<p>";
  199. }
  200. }
  201. return errors;
  202. }
  203. // static
  204. void TestCase::QuitMainMessageLoop(PP_Instance instance) {
  205. PP_Instance* heap_instance = new PP_Instance(instance);
  206. pp::CompletionCallback callback(&DoQuitMainMessageLoop, heap_instance);
  207. pp::Module::Get()->core()->CallOnMainThread(0, callback);
  208. }
  209. // static
  210. void TestCase::DoQuitMainMessageLoop(void* pp_instance, int32_t result) {
  211. PP_Instance* instance = static_cast<PP_Instance*>(pp_instance);
  212. GetTestingInterface()->QuitMessageLoop(*instance);
  213. delete instance;
  214. }
  215. void TestCase::RunOnThreadInternal(
  216. void (*thread_func)(void*),
  217. void* thread_param,
  218. const PPB_Testing_Private* testing_interface) {
  219. PP_Thread thread;
  220. PP_CreateThread(&thread, thread_func, thread_param);
  221. // Run a message loop so pepper calls can be dispatched. The background
  222. // thread will set result_ and make us Quit when it's done.
  223. testing_interface->RunMessageLoop(instance_->pp_instance());
  224. PP_JoinThread(thread);
  225. }