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