• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "ppapi/tests/test_case.h"
6 
7 #include <string.h>
8 
9 #include <algorithm>
10 #include <sstream>
11 
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"
17 
18 namespace {
19 
StripPrefix(const std::string & test_name)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;
27 }
28 
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 //   ""
StripTestCase(const std::string & full_test_name)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();
49 }
50 
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.
ParseTestFilter(const std::string & test_filter,std::map<std::string,bool> * remaining_tests)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));
68 
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));
74   }
75   // There may be a trailing comma; ignore empty strings.
76   remaining_tests->erase(std::string());
77 }
78 
79 }  // namespace
80 
TestCase(TestingInstance * instance)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();
90 }
91 
~TestCase()92 TestCase::~TestCase() {
93 }
94 
Init()95 bool TestCase::Init() {
96   return true;
97 }
98 
99 // static
MakeFailureMessage(const char * file,int line,const char * cmd)100 std::string TestCase::MakeFailureMessage(const char* file,
101                                          int line,
102                                          const char* cmd) {
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'
107   //     gclient runhooks
108   // make -k -j4 BUILDTYPE=Release ppapi_tests
109 
110   std::ostringstream output;
111   output << "Failure in " << file << "(" << line << "): " << cmd;
112   return output.str();
113 }
114 
115 #if !(defined __native_client__)
GetTestObject()116 pp::VarPrivate TestCase::GetTestObject() {
117   if (test_object_.is_undefined()) {
118     pp::deprecated::ScriptableObject* so = CreateTestObject();
119     if (so) {
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
123       // leak.
124       IgnoreLeakedVar(test_object_.pp_var().value.as_id);
125     }
126   }
127   return test_object_;
128 }
129 #endif
130 
CheckTestingInterface()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.");
139     return false;
140   }
141 
142   return true;
143 }
144 
HandleMessage(const pp::Var & message_data)145 void TestCase::HandleMessage(const pp::Var& message_data) {
146 }
147 
DidChangeView(const pp::View & view)148 void TestCase::DidChangeView(const pp::View& view) {
149 }
150 
HandleInputEvent(const pp::InputEvent & event)151 bool TestCase::HandleInputEvent(const pp::InputEvent& event) {
152   return false;
153 }
154 
IgnoreLeakedVar(int64_t id)155 void TestCase::IgnoreLeakedVar(int64_t id) {
156   ignored_leaked_vars_.insert(id);
157 }
158 
159 #if !(defined __native_client__)
CreateTestObject()160 pp::deprecated::ScriptableObject* TestCase::CreateTestObject() {
161   return NULL;
162 }
163 #endif
164 
EnsureRunningOverHTTP()165 bool TestCase::EnsureRunningOverHTTP() {
166   if (instance_->protocol() != "http:") {
167     instance_->AppendError("This test needs to be run over HTTP.");
168     return false;
169   }
170 
171   return true;
172 }
173 
ShouldRunAllTests(const std::string & filter)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());
177 }
178 
ShouldRunTest(const std::string & test_name,const std::string & filter)179 bool TestCase::ShouldRunTest(const std::string& test_name,
180                              const std::string& filter) {
181   if (ShouldRunAllTests(filter))
182     return true;
183 
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;
189   }
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);
195     return false;
196   }
197   remaining_tests_.erase(test_name);
198   return iter->second;
199 }
200 
NowInTimeTicks()201 PP_TimeTicks TestCase::NowInTimeTicks() {
202   return pp::Module::Get()->core()->GetTimeTicks();
203 }
204 
CheckResourcesAndVars(std::string errors)205 std::string TestCase::CheckResourcesAndVars(std::string errors) {
206   if (!errors.empty())
207     return errors;
208 
209   if (testing_interface_) {
210     // TODO(dmichael): Fix tests that leak resources and enable the following:
211     /*
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();
218     }
219     */
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.
226     int leaked_vars =
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();
233     }
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>";
238     }
239   }
240   return errors;
241 }
242 
243 // static
QuitMainMessageLoop(PP_Instance instance)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);
248 }
249 
250 // static
DoQuitMainMessageLoop(void * pp_instance,int32_t result)251 void TestCase::DoQuitMainMessageLoop(void* pp_instance, int32_t result) {
252   PP_Instance* instance = static_cast<PP_Instance*>(pp_instance);
253   GetTestingInterface()->QuitMessageLoop(*instance);
254   delete instance;
255 }
256 
RunOnThreadInternal(void (* thread_func)(void *),void * thread_param,const PPB_Testing_Private * testing_interface)257 void TestCase::RunOnThreadInternal(
258     void (*thread_func)(void*),
259     void* thread_param,
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);
267 }
268