signin: Fix 'ready' event fired after auth complete regression
[chromium-blink-merge.git] / ppapi / tests / test_case.cc
bloba2575560ca3cf916450fd8acb23aed4642b84c33
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 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]));
26 return test_name;
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
31 // an empty string.
32 // For example:
33 // StripTestCase("TestCase_TestName");
34 // returns
35 // "TestName"
36 // while
37 // StripTestCase("TestCase);
38 // returns
39 // ""
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.
48 return std::string();
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
57 // do it ourselves.
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
66 // TestCase name.
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());
79 } // namespace
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() {
96 return true;
99 // static
100 std::string TestCase::MakeFailureMessage(const char* file,
101 int line,
102 const char* cmd) {
103 std::ostringstream output;
104 output << "Failure in " << file << "(" << line << "): " << cmd;
105 return output.str();
108 #if !(defined __native_client__)
109 pp::VarPrivate TestCase::GetTestObject() {
110 if (test_object_.is_undefined()) {
111 pp::deprecated::ScriptableObject* so = CreateTestObject();
112 if (so) {
113 test_object_ = pp::VarPrivate(instance_, so); // Takes ownership.
114 // CheckResourcesAndVars runs and looks for leaks before we've actually
115 // completely shut down. Ignore the instance object, since it's not a real
116 // leak.
117 IgnoreLeakedVar(test_object_.pp_var().value.as_id);
120 return test_object_;
122 #endif
124 bool TestCase::CheckTestingInterface() {
125 testing_interface_ = GetTestingInterface();
126 if (!testing_interface_) {
127 // Give a more helpful error message for the testing interface being gone
128 // since that needs special enabling in Chrome.
129 instance_->AppendError("This test needs the testing interface, which is "
130 "not currently available. In Chrome, use "
131 "--enable-pepper-testing when launching.");
132 return false;
135 return true;
138 void TestCase::HandleMessage(const pp::Var& message_data) {
141 void TestCase::DidChangeView(const pp::View& view) {
144 bool TestCase::HandleInputEvent(const pp::InputEvent& event) {
145 return false;
148 void TestCase::IgnoreLeakedVar(int64_t id) {
149 ignored_leaked_vars_.insert(id);
152 #if !(defined __native_client__)
153 pp::deprecated::ScriptableObject* TestCase::CreateTestObject() {
154 return NULL;
156 #endif
158 bool TestCase::EnsureRunningOverHTTP() {
159 if (instance_->protocol() != "http:") {
160 instance_->AppendError("This test needs to be run over HTTP.");
161 return false;
164 return true;
167 bool TestCase::ShouldRunAllTests(const std::string& filter) {
168 // If only the TestCase is listed, we're running all the tests in RunTests.
169 return (StripTestCase(filter) == std::string());
172 bool TestCase::ShouldRunTest(const std::string& test_name,
173 const std::string& filter) {
174 if (ShouldRunAllTests(filter))
175 return true;
177 // Lazily initialize our "filter_tests_" map.
178 if (!have_populated_filter_tests_) {
179 ParseTestFilter(filter, &filter_tests_);
180 remaining_tests_ = filter_tests_;
181 have_populated_filter_tests_ = true;
183 std::map<std::string, bool>::iterator iter = filter_tests_.find(test_name);
184 if (iter == filter_tests_.end()) {
185 // The test name wasn't listed in the filter. Don't run it, but store it
186 // so TestingInstance::ExecuteTests can report an error later.
187 skipped_tests_.insert(test_name);
188 return false;
190 remaining_tests_.erase(test_name);
191 return iter->second;
194 PP_TimeTicks TestCase::NowInTimeTicks() {
195 return pp::Module::Get()->core()->GetTimeTicks();
198 std::string TestCase::CheckResourcesAndVars(std::string errors) {
199 if (!errors.empty())
200 return errors;
202 if (testing_interface_) {
203 // TODO(dmichael): Fix tests that leak resources and enable the following:
205 uint32_t leaked_resources =
206 testing_interface_->GetLiveObjectsForInstance(instance_->pp_instance());
207 if (leaked_resources) {
208 std::ostringstream output;
209 output << "FAILED: Test leaked " << leaked_resources << " resources.\n";
210 errors += output.str();
213 const int kVarsToPrint = 100;
214 PP_Var vars[kVarsToPrint];
215 int found_vars = testing_interface_->GetLiveVars(vars, kVarsToPrint);
216 // This will undercount if we are told to ignore a Var which is then *not*
217 // leaked. Worst case, we just won't print the little "Test leaked" message,
218 // but we'll still print any non-ignored leaked vars we found.
219 int leaked_vars =
220 found_vars - static_cast<int>(ignored_leaked_vars_.size());
221 if (leaked_vars > 0) {
222 std::ostringstream output;
223 output << "Test leaked " << leaked_vars << " vars (printing at most "
224 << kVarsToPrint <<"):<p>";
225 errors += output.str();
227 for (int i = 0; i < std::min(found_vars, kVarsToPrint); ++i) {
228 pp::Var leaked_var(pp::PASS_REF, vars[i]);
229 if (ignored_leaked_vars_.count(leaked_var.pp_var().value.as_id) == 0)
230 errors += leaked_var.DebugString() + "<p>";
233 return errors;
236 // static
237 void TestCase::QuitMainMessageLoop(PP_Instance instance) {
238 PP_Instance* heap_instance = new PP_Instance(instance);
239 pp::CompletionCallback callback(&DoQuitMainMessageLoop, heap_instance);
240 pp::Module::Get()->core()->CallOnMainThread(0, callback);
243 // static
244 void TestCase::DoQuitMainMessageLoop(void* pp_instance, int32_t result) {
245 PP_Instance* instance = static_cast<PP_Instance*>(pp_instance);
246 GetTestingInterface()->QuitMessageLoop(*instance);
247 delete instance;
250 void TestCase::RunOnThreadInternal(
251 void (*thread_func)(void*),
252 void* thread_param,
253 const PPB_Testing_Private* testing_interface) {
254 PP_Thread thread;
255 PP_CreateThread(&thread, thread_func, thread_param);
256 // Run a message loop so pepper calls can be dispatched. The background
257 // thread will set result_ and make us Quit when it's done.
258 testing_interface->RunMessageLoop(instance_->pp_instance());
259 PP_JoinThread(thread);