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"
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"
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
]));
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
32 // StripTestCase("TestCase_TestName");
36 // StripTestCase("TestCase);
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.
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
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
66 std::string
test_name_without_case(StripTestCase(stripped_test_name
));
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
));
74 // There may be a trailing comma; ignore empty strings.
75 remaining_tests
->erase(std::string());
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();
91 TestCase::~TestCase() {
94 bool TestCase::Init() {
99 std::string
TestCase::MakeFailureMessage(const char* file
,
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'
107 // make -k -j4 BUILDTYPE=Release ppapi_tests
109 std::ostringstream output
;
110 output
<< "Failure in " << file
<< "(" << line
<< "): " << cmd
;
114 #if !(defined __native_client__)
115 pp::VarPrivate
TestCase::GetTestObject() {
116 if (test_object_
.is_undefined()) {
117 pp::deprecated::ScriptableObject
* so
= CreateTestObject();
119 test_object_
= pp::VarPrivate(instance_
, so
); // Takes ownership.
120 // CheckResourcesAndVars runs and looks for leaks before we've actually
121 // completely shut down. Ignore the instance object, since it's not a real
123 IgnoreLeakedVar(test_object_
.pp_var().value
.as_id
);
130 bool TestCase::CheckTestingInterface() {
131 testing_interface_
= GetTestingInterface();
132 if (!testing_interface_
) {
133 // Give a more helpful error message for the testing interface being gone
134 // since that needs special enabling in Chrome.
135 instance_
->AppendError("This test needs the testing interface, which is "
136 "not currently available. In Chrome, use "
137 "--enable-pepper-testing when launching.");
144 void TestCase::HandleMessage(const pp::Var
& message_data
) {
147 void TestCase::DidChangeView(const pp::View
& view
) {
150 bool TestCase::HandleInputEvent(const pp::InputEvent
& event
) {
154 void TestCase::IgnoreLeakedVar(int64_t id
) {
155 ignored_leaked_vars_
.insert(id
);
158 #if !(defined __native_client__)
159 pp::deprecated::ScriptableObject
* TestCase::CreateTestObject() {
164 bool TestCase::EnsureRunningOverHTTP() {
165 if (instance_
->protocol() != "http:") {
166 instance_
->AppendError("This test needs to be run over HTTP.");
173 bool TestCase::ShouldRunAllTests(const std::string
& filter
) {
174 // If only the TestCase is listed, we're running all the tests in RunTests.
175 return (StripTestCase(filter
) == std::string());
178 bool TestCase::ShouldRunTest(const std::string
& test_name
,
179 const std::string
& filter
) {
180 if (ShouldRunAllTests(filter
))
183 // Lazily initialize our "filter_tests_" map.
184 if (!have_populated_filter_tests_
) {
185 ParseTestFilter(filter
, &filter_tests_
);
186 remaining_tests_
= filter_tests_
;
187 have_populated_filter_tests_
= true;
189 std::map
<std::string
, bool>::iterator iter
= filter_tests_
.find(test_name
);
190 if (iter
== filter_tests_
.end()) {
191 // The test name wasn't listed in the filter. Don't run it, but store it
192 // so TestingInstance::ExecuteTests can report an error later.
193 skipped_tests_
.insert(test_name
);
196 remaining_tests_
.erase(test_name
);
200 PP_TimeTicks
TestCase::NowInTimeTicks() {
201 return pp::Module::Get()->core()->GetTimeTicks();
204 std::string
TestCase::CheckResourcesAndVars(std::string errors
) {
208 if (testing_interface_
) {
209 // TODO(dmichael): Fix tests that leak resources and enable the following:
211 uint32_t leaked_resources =
212 testing_interface_->GetLiveObjectsForInstance(instance_->pp_instance());
213 if (leaked_resources) {
214 std::ostringstream output;
215 output << "FAILED: Test leaked " << leaked_resources << " resources.\n";
216 errors += output.str();
219 const int kVarsToPrint
= 100;
220 PP_Var vars
[kVarsToPrint
];
221 int found_vars
= testing_interface_
->GetLiveVars(vars
, kVarsToPrint
);
222 // This will undercount if we are told to ignore a Var which is then *not*
223 // leaked. Worst case, we just won't print the little "Test leaked" message,
224 // but we'll still print any non-ignored leaked vars we found.
226 found_vars
- static_cast<int>(ignored_leaked_vars_
.size());
227 if (leaked_vars
> 0) {
228 std::ostringstream output
;
229 output
<< "Test leaked " << leaked_vars
<< " vars (printing at most "
230 << kVarsToPrint
<<"):<p>";
231 errors
+= output
.str();
233 for (int i
= 0; i
< std::min(found_vars
, kVarsToPrint
); ++i
) {
234 pp::Var
leaked_var(pp::PASS_REF
, vars
[i
]);
235 if (ignored_leaked_vars_
.count(leaked_var
.pp_var().value
.as_id
) == 0)
236 errors
+= leaked_var
.DebugString() + "<p>";
243 void TestCase::QuitMainMessageLoop(PP_Instance instance
) {
244 PP_Instance
* heap_instance
= new PP_Instance(instance
);
245 pp::CompletionCallback
callback(&DoQuitMainMessageLoop
, heap_instance
);
246 pp::Module::Get()->core()->CallOnMainThread(0, callback
);
250 void TestCase::DoQuitMainMessageLoop(void* pp_instance
, int32_t result
) {
251 PP_Instance
* instance
= static_cast<PP_Instance
*>(pp_instance
);
252 GetTestingInterface()->QuitMessageLoop(*instance
);
256 void TestCase::RunOnThreadInternal(void (*thread_func
)(void*),
258 const PPB_Testing_Dev
* testing_interface
) {
259 PP_ThreadType thread
;
260 PP_CreateThread(&thread
, thread_func
, thread_param
);
261 // Run a message loop so pepper calls can be dispatched. The background
262 // thread will set result_ and make us Quit when it's done.
263 testing_interface
->RunMessageLoop(instance_
->pp_instance());
264 PP_JoinThread(thread
);