This source file includes following definitions.
- StripPrefix
- StripTestCase
- ParseTestFilter
- have_populated_filter_tests_
- Init
- MakeFailureMessage
- GetTestObject
- CheckTestingInterface
- HandleMessage
- DidChangeView
- HandleInputEvent
- IgnoreLeakedVar
- CreateTestObject
- EnsureRunningOverHTTP
- ShouldRunAllTests
- ShouldRunTest
- NowInTimeTicks
- CheckResourcesAndVars
- QuitMainMessageLoop
- DoQuitMainMessageLoop
- RunOnThreadInternal
#include "ppapi/tests/test_case.h"
#include <string.h>
#include <algorithm>
#include <sstream>
#include "ppapi/cpp/core.h"
#include "ppapi/cpp/module.h"
#include "ppapi/tests/pp_thread.h"
#include "ppapi/tests/test_utils.h"
#include "ppapi/tests/testing_instance.h"
namespace {
std::string StripPrefix(const std::string& test_name) {
const char* const prefixes[] = {
"FAILS_", "FLAKY_", "DISABLED_" };
for (size_t i = 0; i < sizeof(prefixes)/sizeof(prefixes[0]); ++i)
if (test_name.find(prefixes[i]) == 0)
return test_name.substr(strlen(prefixes[i]));
return test_name;
}
std::string StripTestCase(const std::string& full_test_name) {
size_t delim = full_test_name.find_first_of('_');
if (delim != std::string::npos)
return full_test_name.substr(delim+1);
return std::string();
}
void ParseTestFilter(const std::string& test_filter,
std::map<std::string, bool>* remaining_tests) {
std::istringstream filter_stream(test_filter);
std::string current_test;
while (std::getline(filter_stream, current_test, ',')) {
std::string stripped_test_name(StripPrefix(current_test));
std::string test_name_without_case(StripTestCase(stripped_test_name));
bool should_run_test = (current_test == stripped_test_name);
PP_DCHECK(remaining_tests->count(test_name_without_case) == 0);
remaining_tests->insert(
std::make_pair(test_name_without_case, should_run_test));
}
remaining_tests->erase(std::string());
}
}
TestCase::TestCase(TestingInstance* instance)
: instance_(instance),
testing_interface_(NULL),
callback_type_(PP_REQUIRED),
have_populated_filter_tests_(false) {
testing_interface_ = GetTestingInterface();
}
TestCase::~TestCase() {
}
bool TestCase::Init() {
return true;
}
std::string TestCase::MakeFailureMessage(const char* file,
int line,
const char* cmd) {
std::ostringstream output;
output << "Failure in " << file << "(" << line << "): " << cmd;
return output.str();
}
#if !(defined __native_client__)
pp::VarPrivate TestCase::GetTestObject() {
if (test_object_.is_undefined()) {
pp::deprecated::ScriptableObject* so = CreateTestObject();
if (so) {
test_object_ = pp::VarPrivate(instance_, so);
IgnoreLeakedVar(test_object_.pp_var().value.as_id);
}
}
return test_object_;
}
#endif
bool TestCase::CheckTestingInterface() {
testing_interface_ = GetTestingInterface();
if (!testing_interface_) {
instance_->AppendError("This test needs the testing interface, which is "
"not currently available. In Chrome, use "
"--enable-pepper-testing when launching.");
return false;
}
return true;
}
void TestCase::HandleMessage(const pp::Var& message_data) {
}
void TestCase::DidChangeView(const pp::View& view) {
}
bool TestCase::HandleInputEvent(const pp::InputEvent& event) {
return false;
}
void TestCase::IgnoreLeakedVar(int64_t id) {
ignored_leaked_vars_.insert(id);
}
#if !(defined __native_client__)
pp::deprecated::ScriptableObject* TestCase::CreateTestObject() {
return NULL;
}
#endif
bool TestCase::EnsureRunningOverHTTP() {
if (instance_->protocol() != "http:") {
instance_->AppendError("This test needs to be run over HTTP.");
return false;
}
return true;
}
bool TestCase::ShouldRunAllTests(const std::string& filter) {
return (StripTestCase(filter) == std::string());
}
bool TestCase::ShouldRunTest(const std::string& test_name,
const std::string& filter) {
if (ShouldRunAllTests(filter))
return true;
if (!have_populated_filter_tests_) {
ParseTestFilter(filter, &filter_tests_);
remaining_tests_ = filter_tests_;
have_populated_filter_tests_ = true;
}
std::map<std::string, bool>::iterator iter = filter_tests_.find(test_name);
if (iter == filter_tests_.end()) {
skipped_tests_.insert(test_name);
return false;
}
remaining_tests_.erase(test_name);
return iter->second;
}
PP_TimeTicks TestCase::NowInTimeTicks() {
return pp::Module::Get()->core()->GetTimeTicks();
}
std::string TestCase::CheckResourcesAndVars(std::string errors) {
if (!errors.empty())
return errors;
if (testing_interface_) {
const int kVarsToPrint = 100;
PP_Var vars[kVarsToPrint];
int found_vars = testing_interface_->GetLiveVars(vars, kVarsToPrint);
int leaked_vars =
found_vars - static_cast<int>(ignored_leaked_vars_.size());
if (leaked_vars > 0) {
std::ostringstream output;
output << "Test leaked " << leaked_vars << " vars (printing at most "
<< kVarsToPrint <<"):<p>";
errors += output.str();
}
for (int i = 0; i < std::min(found_vars, kVarsToPrint); ++i) {
pp::Var leaked_var(pp::PASS_REF, vars[i]);
if (ignored_leaked_vars_.count(leaked_var.pp_var().value.as_id) == 0)
errors += leaked_var.DebugString() + "<p>";
}
}
return errors;
}
void TestCase::QuitMainMessageLoop(PP_Instance instance) {
PP_Instance* heap_instance = new PP_Instance(instance);
pp::CompletionCallback callback(&DoQuitMainMessageLoop, heap_instance);
pp::Module::Get()->core()->CallOnMainThread(0, callback);
}
void TestCase::DoQuitMainMessageLoop(void* pp_instance, int32_t result) {
PP_Instance* instance = static_cast<PP_Instance*>(pp_instance);
GetTestingInterface()->QuitMessageLoop(*instance);
delete instance;
}
void TestCase::RunOnThreadInternal(
void (*thread_func)(void*),
void* thread_param,
const PPB_Testing_Private* testing_interface) {
PP_ThreadType thread;
PP_CreateThread(&thread, thread_func, thread_param);
testing_interface->RunMessageLoop(instance_->pp_instance());
PP_JoinThread(thread);
}