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"
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"
20 std::string
StripPrefix(const std::string
& test_name
) {
21 const char* const prefixes
[] = {
22 "FAILS_", "FLAKY_", "DISABLED_" };
23 for (size_t i
= 0; i
< sizeof(prefixes
)/sizeof(prefixes
[0]); ++i
)
24 if (test_name
.find(prefixes
[i
]) == 0)
25 return test_name
.substr(strlen(prefixes
[i
]));
29 // Strip the TestCase name off and return the remainder (i.e., everything after
30 // '_'). If there is no '_', assume only the TestCase was provided, and return
33 // StripTestCase("TestCase_TestName");
37 // StripTestCase("TestCase);
40 std::string
StripTestCase(const std::string
& full_test_name
) {
41 size_t delim
= full_test_name
.find_first_of('_');
42 if (delim
!= std::string::npos
)
43 return full_test_name
.substr(delim
+1);
44 // In this case, our "filter" is the empty string; the full test name is the
45 // same as the TestCase name with which we were constructed.
46 // TODO(dmichael): It might be nice to be able to PP_DCHECK against the
47 // TestCase class name, but we'd have to plumb that name to TestCase somehow.
51 // Parse |test_filter|, which is a comma-delimited list of (possibly prefixed)
52 // test names and insert the un-prefixed names into |remaining_tests|, with
53 // the bool indicating whether the test should be run.
54 void ParseTestFilter(const std::string
& test_filter
,
55 std::map
<std::string
, bool>* remaining_tests
) {
56 // We can't use base/strings/string_util.h::Tokenize in ppapi, so we have to
58 std::istringstream
filter_stream(test_filter
);
59 std::string current_test
;
60 while (std::getline(filter_stream
, current_test
, ',')) {
61 // |current_test| might include a prefix, like DISABLED_Foo_TestBar, so we
62 // we strip it off if there is one.
63 std::string
stripped_test_name(StripPrefix(current_test
));
64 // Strip off the test case and use the test name as a key, because the test
65 // name ShouldRunTest wants to use to look up the test doesn't have the
67 std::string
test_name_without_case(StripTestCase(stripped_test_name
));
69 // If the test wasn't prefixed, it should be run.
70 bool should_run_test
= (current_test
== stripped_test_name
);
71 PP_DCHECK(remaining_tests
->count(test_name_without_case
) == 0);
72 remaining_tests
->insert(
73 std::make_pair(test_name_without_case
, should_run_test
));
75 // There may be a trailing comma; ignore empty strings.
76 remaining_tests
->erase(std::string());
81 TestCase::TestCase(TestingInstance
* instance
)
82 : instance_(instance
),
83 testing_interface_(NULL
),
84 callback_type_(PP_REQUIRED
),
85 have_populated_filter_tests_(false) {
86 // Get the testing_interface_ if it is available, so that we can do Resource
87 // and Var checks on shutdown (see CheckResourcesAndVars). If it is not
88 // available, testing_interface_ will be NULL. Some tests do not require it.
89 testing_interface_
= GetTestingInterface();
92 TestCase::~TestCase() {
95 bool TestCase::Init() {
100 std::string
TestCase::MakeFailureMessage(const char* file
,
103 // The mere presence of this local variable works around a gcc-4.2.4
104 // compiler bug in official Chrome Linux builds. If you remove it,
105 // confirm this compile command still works:
106 // GYP_DEFINES='branding=Chrome buildtype=Official target_arch=x64'
108 // make -k -j4 BUILDTYPE=Release ppapi_tests
110 std::ostringstream output
;
111 output
<< "Failure in " << file
<< "(" << line
<< "): " << cmd
;
115 #if !(defined __native_client__)
116 pp::VarPrivate
TestCase::GetTestObject() {
117 if (test_object_
.is_undefined()) {
118 pp::deprecated::ScriptableObject
* so
= CreateTestObject();
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
124 IgnoreLeakedVar(test_object_
.pp_var().value
.as_id
);
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.");
145 void TestCase::HandleMessage(const pp::Var
& message_data
) {
148 void TestCase::DidChangeView(const pp::View
& view
) {
151 bool TestCase::HandleInputEvent(const pp::InputEvent
& event
) {
155 void TestCase::IgnoreLeakedVar(int64_t id
) {
156 ignored_leaked_vars_
.insert(id
);
159 #if !(defined __native_client__)
160 pp::deprecated::ScriptableObject
* TestCase::CreateTestObject() {
165 bool TestCase::EnsureRunningOverHTTP() {
166 if (instance_
->protocol() != "http:") {
167 instance_
->AppendError("This test needs to be run over HTTP.");
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());
179 bool TestCase::ShouldRunTest(const std::string
& test_name
,
180 const std::string
& filter
) {
181 if (ShouldRunAllTests(filter
))
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;
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
);
197 remaining_tests_
.erase(test_name
);
201 PP_TimeTicks
TestCase::NowInTimeTicks() {
202 return pp::Module::Get()->core()->GetTimeTicks();
205 std::string
TestCase::CheckResourcesAndVars(std::string errors
) {
209 if (testing_interface_
) {
210 // TODO(dmichael): Fix tests that leak resources and enable the following:
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();
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.
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();
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>";
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
);
251 void TestCase::DoQuitMainMessageLoop(void* pp_instance
, int32_t result
) {
252 PP_Instance
* instance
= static_cast<PP_Instance
*>(pp_instance
);
253 GetTestingInterface()->QuitMessageLoop(*instance
);
257 void TestCase::RunOnThreadInternal(
258 void (*thread_func
)(void*),
260 const PPB_Testing_Private
* testing_interface
) {
261 PP_ThreadType thread
;
262 PP_CreateThread(&thread
, thread_func
, thread_param
);
263 // Run a message loop so pepper calls can be dispatched. The background
264 // thread will set result_ and make us Quit when it's done.
265 testing_interface
->RunMessageLoop(instance_
->pp_instance());
266 PP_JoinThread(thread
);