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