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