| OLD | NEW |
| (Empty) | |
| 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 "chrome/test/ppapi/ppapi_test.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "base/file_util.h" |
| 9 #include "base/logging.h" |
| 10 #include "base/path_service.h" |
| 11 #include "base/stringprintf.h" |
| 12 #include "base/string_util.h" |
| 13 #include "base/test/test_timeouts.h" |
| 14 #include "build/build_config.h" |
| 15 #include "chrome/browser/content_settings/host_content_settings_map.h" |
| 16 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/browser/ui/browser.h" |
| 18 #include "chrome/common/chrome_paths.h" |
| 19 #include "chrome/common/chrome_switches.h" |
| 20 #include "chrome/test/base/ui_test_utils.h" |
| 21 #include "content/public/browser/dom_operation_notification_details.h" |
| 22 #include "content/public/browser/notification_types.h" |
| 23 #include "content/public/browser/web_contents.h" |
| 24 #include "content/public/common/content_paths.h" |
| 25 #include "content/public/common/content_switches.h" |
| 26 #include "media/audio/audio_manager.h" |
| 27 #include "net/base/net_util.h" |
| 28 #include "net/test/test_server.h" |
| 29 #include "webkit/plugins/plugin_switches.h" |
| 30 |
| 31 using content::DomOperationNotificationDetails; |
| 32 using content::RenderViewHost; |
| 33 |
| 34 namespace { |
| 35 |
| 36 // Platform-specific filename relative to the chrome executable. |
| 37 #if defined(OS_WIN) |
| 38 const wchar_t library_name[] = L"ppapi_tests.dll"; |
| 39 #elif defined(OS_MACOSX) |
| 40 const char library_name[] = "ppapi_tests.plugin"; |
| 41 #elif defined(OS_POSIX) |
| 42 const char library_name[] = "libppapi_tests.so"; |
| 43 #endif |
| 44 |
| 45 // The large timeout was causing the cycle time for the whole test suite |
| 46 // to be too long when a tiny bug caused all tests to timeout. |
| 47 // http://crbug.com/108264 |
| 48 static int kTimeoutMs = 90000; |
| 49 //static int kTimeoutMs = TestTimeouts::large_test_timeout_ms()); |
| 50 |
| 51 bool IsAudioOutputAvailable() { |
| 52 scoped_ptr<media::AudioManager> audio_manager(media::AudioManager::Create()); |
| 53 return audio_manager->HasAudioOutputDevices(); |
| 54 } |
| 55 |
| 56 } // namespace |
| 57 |
| 58 PPAPITestBase::TestFinishObserver::TestFinishObserver( |
| 59 RenderViewHost* render_view_host, |
| 60 int timeout_s) |
| 61 : finished_(false), |
| 62 waiting_(false), |
| 63 timeout_s_(timeout_s) { |
| 64 registrar_.Add(this, content::NOTIFICATION_DOM_OPERATION_RESPONSE, |
| 65 content::Source<RenderViewHost>(render_view_host)); |
| 66 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(timeout_s), |
| 67 this, &TestFinishObserver::OnTimeout); |
| 68 } |
| 69 |
| 70 bool PPAPITestBase::TestFinishObserver::WaitForFinish() { |
| 71 if (!finished_) { |
| 72 waiting_ = true; |
| 73 ui_test_utils::RunMessageLoop(); |
| 74 waiting_ = false; |
| 75 } |
| 76 return finished_; |
| 77 } |
| 78 |
| 79 void PPAPITestBase::TestFinishObserver::Observe( |
| 80 int type, |
| 81 const content::NotificationSource& source, |
| 82 const content::NotificationDetails& details) { |
| 83 DCHECK(type == content::NOTIFICATION_DOM_OPERATION_RESPONSE); |
| 84 content::Details<DomOperationNotificationDetails> dom_op_details(details); |
| 85 // We might receive responses for other script execution, but we only |
| 86 // care about the test finished message. |
| 87 std::string response; |
| 88 TrimString(dom_op_details->json, "\"", &response); |
| 89 if (response == "...") { |
| 90 timer_.Stop(); |
| 91 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(timeout_s_), |
| 92 this, &TestFinishObserver::OnTimeout); |
| 93 } else { |
| 94 result_ = response; |
| 95 finished_ = true; |
| 96 if (waiting_) |
| 97 MessageLoopForUI::current()->Quit(); |
| 98 } |
| 99 } |
| 100 |
| 101 void PPAPITestBase::TestFinishObserver::Reset() { |
| 102 finished_ = false; |
| 103 waiting_ = false; |
| 104 result_.clear(); |
| 105 } |
| 106 |
| 107 void PPAPITestBase::TestFinishObserver::OnTimeout() { |
| 108 MessageLoopForUI::current()->Quit(); |
| 109 } |
| 110 |
| 111 PPAPITestBase::PPAPITestBase() { |
| 112 EnableDOMAutomation(); |
| 113 } |
| 114 |
| 115 void PPAPITestBase::SetUpCommandLine(CommandLine* command_line) { |
| 116 // The test sends us the result via a cookie. |
| 117 command_line->AppendSwitch(switches::kEnableFileCookies); |
| 118 |
| 119 // Some stuff is hung off of the testing interface which is not enabled |
| 120 // by default. |
| 121 command_line->AppendSwitch(switches::kEnablePepperTesting); |
| 122 |
| 123 // Smooth scrolling confuses the scrollbar test. |
| 124 command_line->AppendSwitch(switches::kDisableSmoothScrolling); |
| 125 } |
| 126 |
| 127 GURL PPAPITestBase::GetTestFileUrl(const std::string& test_case) { |
| 128 FilePath test_path; |
| 129 EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &test_path)); |
| 130 test_path = test_path.Append(FILE_PATH_LITERAL("ppapi")); |
| 131 test_path = test_path.Append(FILE_PATH_LITERAL("tests")); |
| 132 test_path = test_path.Append(FILE_PATH_LITERAL("test_case.html")); |
| 133 |
| 134 // Sanity check the file name. |
| 135 EXPECT_TRUE(file_util::PathExists(test_path)); |
| 136 |
| 137 GURL test_url = net::FilePathToFileURL(test_path); |
| 138 |
| 139 GURL::Replacements replacements; |
| 140 std::string query = BuildQuery("", test_case); |
| 141 replacements.SetQuery(query.c_str(), url_parse::Component(0, query.size())); |
| 142 return test_url.ReplaceComponents(replacements); |
| 143 } |
| 144 |
| 145 void PPAPITestBase::RunTest(const std::string& test_case) { |
| 146 GURL url = GetTestFileUrl(test_case); |
| 147 RunTestURL(url); |
| 148 } |
| 149 |
| 150 void PPAPITestBase::RunTestAndReload(const std::string& test_case) { |
| 151 GURL url = GetTestFileUrl(test_case); |
| 152 RunTestURL(url); |
| 153 // If that passed, we simply run the test again, which navigates again. |
| 154 RunTestURL(url); |
| 155 } |
| 156 |
| 157 void PPAPITestBase::RunTestViaHTTP(const std::string& test_case) { |
| 158 FilePath document_root; |
| 159 ASSERT_TRUE(GetHTTPDocumentRoot(&document_root)); |
| 160 RunHTTPTestServer(document_root, test_case, ""); |
| 161 } |
| 162 |
| 163 void PPAPITestBase::RunTestWithSSLServer(const std::string& test_case) { |
| 164 FilePath document_root; |
| 165 ASSERT_TRUE(GetHTTPDocumentRoot(&document_root)); |
| 166 net::TestServer test_server(net::BaseTestServer::HTTPSOptions(), |
| 167 document_root); |
| 168 ASSERT_TRUE(test_server.Start()); |
| 169 uint16_t port = test_server.host_port_pair().port(); |
| 170 RunHTTPTestServer(document_root, test_case, |
| 171 StringPrintf("ssl_server_port=%d", port)); |
| 172 } |
| 173 |
| 174 void PPAPITestBase::RunTestWithWebSocketServer(const std::string& test_case) { |
| 175 FilePath websocket_root_dir; |
| 176 ASSERT_TRUE( |
| 177 PathService::Get(content::DIR_LAYOUT_TESTS, &websocket_root_dir)); |
| 178 ui_test_utils::TestWebSocketServer server; |
| 179 int port = server.UseRandomPort(); |
| 180 ASSERT_TRUE(server.Start(websocket_root_dir)); |
| 181 FilePath http_document_root; |
| 182 ASSERT_TRUE(GetHTTPDocumentRoot(&http_document_root)); |
| 183 RunHTTPTestServer(http_document_root, test_case, |
| 184 StringPrintf("websocket_port=%d", port)); |
| 185 } |
| 186 |
| 187 void PPAPITestBase::RunTestIfAudioOutputAvailable( |
| 188 const std::string& test_case) { |
| 189 if (IsAudioOutputAvailable()) { |
| 190 RunTest(test_case); |
| 191 } else { |
| 192 LOG(WARNING) << "PPAPITest: " << test_case << |
| 193 " was not executed because there are no audio devices available."; |
| 194 } |
| 195 } |
| 196 |
| 197 void PPAPITestBase::RunTestViaHTTPIfAudioOutputAvailable( |
| 198 const std::string& test_case) { |
| 199 if (IsAudioOutputAvailable()) { |
| 200 RunTestViaHTTP(test_case); |
| 201 } else { |
| 202 LOG(WARNING) << "PPAPITest: " << test_case << |
| 203 " was not executed because there are no audio devices available."; |
| 204 } |
| 205 } |
| 206 |
| 207 std::string PPAPITestBase::StripPrefixes(const std::string& test_name) { |
| 208 const char* const prefixes[] = { |
| 209 "FAILS_", "FLAKY_", "DISABLED_", "SLOW_" }; |
| 210 for (size_t i = 0; i < sizeof(prefixes)/sizeof(prefixes[0]); ++i) |
| 211 if (test_name.find(prefixes[i]) == 0) |
| 212 return test_name.substr(strlen(prefixes[i])); |
| 213 return test_name; |
| 214 } |
| 215 |
| 216 void PPAPITestBase::RunTestURL(const GURL& test_url) { |
| 217 // See comment above TestingInstance in ppapi/test/testing_instance.h. |
| 218 // Basically it sends messages using the DOM automation controller. The |
| 219 // value of "..." means it's still working and we should continue to wait, |
| 220 // any other value indicates completion (in this case it will start with |
| 221 // "PASS" or "FAIL"). This keeps us from timing out on waits for long tests. |
| 222 TestFinishObserver observer( |
| 223 browser()->GetActiveWebContents()->GetRenderViewHost(), kTimeoutMs); |
| 224 |
| 225 ui_test_utils::NavigateToURL(browser(), test_url); |
| 226 |
| 227 ASSERT_TRUE(observer.WaitForFinish()) << "Test timed out."; |
| 228 |
| 229 EXPECT_STREQ("PASS", observer.result().c_str()); |
| 230 } |
| 231 |
| 232 void PPAPITestBase::RunHTTPTestServer( |
| 233 const FilePath& document_root, |
| 234 const std::string& test_case, |
| 235 const std::string& extra_params) { |
| 236 net::TestServer test_server(net::TestServer::TYPE_HTTP, |
| 237 net::TestServer::kLocalhost, |
| 238 document_root); |
| 239 ASSERT_TRUE(test_server.Start()); |
| 240 std::string query = BuildQuery("files/test_case.html?", test_case); |
| 241 if (!extra_params.empty()) |
| 242 query = StringPrintf("%s&%s", query.c_str(), extra_params.c_str()); |
| 243 |
| 244 GURL url = test_server.GetURL(query); |
| 245 RunTestURL(url); |
| 246 } |
| 247 |
| 248 bool PPAPITestBase::GetHTTPDocumentRoot(FilePath* document_root) { |
| 249 // For HTTP tests, we use the output DIR to grab the generated files such |
| 250 // as the NEXEs. |
| 251 FilePath exe_dir = CommandLine::ForCurrentProcess()->GetProgram().DirName(); |
| 252 FilePath src_dir; |
| 253 if (!PathService::Get(base::DIR_SOURCE_ROOT, &src_dir)) |
| 254 return false; |
| 255 |
| 256 // TestServer expects a path relative to source. So we must first |
| 257 // generate absolute paths to SRC and EXE and from there generate |
| 258 // a relative path. |
| 259 if (!exe_dir.IsAbsolute()) file_util::AbsolutePath(&exe_dir); |
| 260 if (!src_dir.IsAbsolute()) file_util::AbsolutePath(&src_dir); |
| 261 if (!exe_dir.IsAbsolute()) |
| 262 return false; |
| 263 if (!src_dir.IsAbsolute()) |
| 264 return false; |
| 265 |
| 266 size_t match, exe_size, src_size; |
| 267 std::vector<FilePath::StringType> src_parts, exe_parts; |
| 268 |
| 269 // Determine point at which src and exe diverge, and create a relative path. |
| 270 exe_dir.GetComponents(&exe_parts); |
| 271 src_dir.GetComponents(&src_parts); |
| 272 exe_size = exe_parts.size(); |
| 273 src_size = src_parts.size(); |
| 274 for (match = 0; match < exe_size && match < src_size; ++match) { |
| 275 if (exe_parts[match] != src_parts[match]) |
| 276 break; |
| 277 } |
| 278 for (size_t tmp_itr = match; tmp_itr < src_size; ++tmp_itr) { |
| 279 *document_root = document_root->Append(FILE_PATH_LITERAL("..")); |
| 280 } |
| 281 for (; match < exe_size; ++match) { |
| 282 *document_root = document_root->Append(exe_parts[match]); |
| 283 } |
| 284 return true; |
| 285 } |
| 286 |
| 287 PPAPITest::PPAPITest() { |
| 288 } |
| 289 |
| 290 void PPAPITest::SetUpCommandLine(CommandLine* command_line) { |
| 291 PPAPITestBase::SetUpCommandLine(command_line); |
| 292 |
| 293 // Append the switch to register the pepper plugin. |
| 294 // library name = <out dir>/<test_name>.<library_extension> |
| 295 // MIME type = application/x-ppapi-<test_name> |
| 296 FilePath plugin_dir; |
| 297 EXPECT_TRUE(PathService::Get(base::DIR_MODULE, &plugin_dir)); |
| 298 |
| 299 FilePath plugin_lib = plugin_dir.Append(library_name); |
| 300 EXPECT_TRUE(file_util::PathExists(plugin_lib)); |
| 301 FilePath::StringType pepper_plugin = plugin_lib.value(); |
| 302 pepper_plugin.append(FILE_PATH_LITERAL(";application/x-ppapi-tests")); |
| 303 command_line->AppendSwitchNative(switches::kRegisterPepperPlugins, |
| 304 pepper_plugin); |
| 305 command_line->AppendSwitchASCII(switches::kAllowNaClSocketAPI, "127.0.0.1"); |
| 306 } |
| 307 |
| 308 std::string PPAPITest::BuildQuery(const std::string& base, |
| 309 const std::string& test_case){ |
| 310 return StringPrintf("%stestcase=%s", base.c_str(), test_case.c_str()); |
| 311 } |
| 312 |
| 313 OutOfProcessPPAPITest::OutOfProcessPPAPITest() { |
| 314 } |
| 315 |
| 316 void OutOfProcessPPAPITest::SetUpCommandLine(CommandLine* command_line) { |
| 317 PPAPITest::SetUpCommandLine(command_line); |
| 318 |
| 319 // Run PPAPI out-of-process to exercise proxy implementations. |
| 320 command_line->AppendSwitch(switches::kPpapiOutOfProcess); |
| 321 } |
| 322 |
| 323 void PPAPINaClTest::SetUpCommandLine(CommandLine* command_line) { |
| 324 PPAPITestBase::SetUpCommandLine(command_line); |
| 325 |
| 326 FilePath plugin_lib; |
| 327 EXPECT_TRUE(PathService::Get(chrome::FILE_NACL_PLUGIN, &plugin_lib)); |
| 328 EXPECT_TRUE(file_util::PathExists(plugin_lib)); |
| 329 |
| 330 // Enable running NaCl outside of the store. |
| 331 command_line->AppendSwitch(switches::kEnableNaCl); |
| 332 command_line->AppendSwitchASCII(switches::kAllowNaClSocketAPI, "127.0.0.1"); |
| 333 } |
| 334 |
| 335 // Append the correct mode and testcase string |
| 336 std::string PPAPINaClNewlibTest::BuildQuery(const std::string& base, |
| 337 const std::string& test_case) { |
| 338 return StringPrintf("%smode=nacl_newlib&testcase=%s", base.c_str(), |
| 339 test_case.c_str()); |
| 340 } |
| 341 |
| 342 // Append the correct mode and testcase string |
| 343 std::string PPAPINaClGLibcTest::BuildQuery(const std::string& base, |
| 344 const std::string& test_case) { |
| 345 return StringPrintf("%smode=nacl_glibc&testcase=%s", base.c_str(), |
| 346 test_case.c_str()); |
| 347 } |
| 348 |
| 349 void PPAPINaClTestDisallowedSockets::SetUpCommandLine( |
| 350 CommandLine* command_line) { |
| 351 PPAPITestBase::SetUpCommandLine(command_line); |
| 352 |
| 353 FilePath plugin_lib; |
| 354 EXPECT_TRUE(PathService::Get(chrome::FILE_NACL_PLUGIN, &plugin_lib)); |
| 355 EXPECT_TRUE(file_util::PathExists(plugin_lib)); |
| 356 |
| 357 // Enable running NaCl outside of the store. |
| 358 command_line->AppendSwitch(switches::kEnableNaCl); |
| 359 } |
| 360 |
| 361 // Append the correct mode and testcase string |
| 362 std::string PPAPINaClTestDisallowedSockets::BuildQuery( |
| 363 const std::string& base, |
| 364 const std::string& test_case) { |
| 365 return StringPrintf("%smode=nacl_newlib&testcase=%s", base.c_str(), |
| 366 test_case.c_str()); |
| 367 } |
| 368 |
| OLD | NEW |