1 // Copyright 2014 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 "content/test/ppapi/ppapi_test.h" 6 7 #include "base/command_line.h" 8 #include "base/files/file_path.h" 9 #include "base/files/file_util.h" 10 #include "base/path_service.h" 11 #include "base/strings/string_util.h" 12 #include "base/strings/stringprintf.h" 13 #include "content/public/browser/web_contents.h" 14 #include "content/public/common/content_switches.h" 15 #include "content/shell/browser/shell.h" 16 #include "net/base/filename_util.h" 17 #include "ppapi/shared_impl/ppapi_switches.h" 18 #include "ppapi/shared_impl/test_harness_utils.h" 19 20 #if defined(OS_CHROMEOS) 21 #include "chromeos/audio/cras_audio_handler.h" 22 #endif 23 24 namespace content { 25 26 PPAPITestMessageHandler::PPAPITestMessageHandler() { 27 } 28 29 TestMessageHandler::MessageResponse PPAPITestMessageHandler::HandleMessage( 30 const std::string& json) { 31 std::string trimmed; 32 base::TrimString(json, "\"", &trimmed); 33 if (trimmed == "...") 34 return CONTINUE; 35 message_ = trimmed; 36 return DONE; 37 } 38 39 void PPAPITestMessageHandler::Reset() { 40 TestMessageHandler::Reset(); 41 message_.clear(); 42 } 43 44 PPAPITestBase::PPAPITestBase() { } 45 46 void PPAPITestBase::SetUpCommandLine(base::CommandLine* command_line) { 47 // The test sends us the result via a cookie. 48 command_line->AppendSwitch(switches::kEnableFileCookies); 49 50 // Some stuff is hung off of the testing interface which is not enabled 51 // by default. 52 command_line->AppendSwitch(switches::kEnablePepperTesting); 53 54 // Smooth scrolling confuses the scrollbar test. 55 command_line->AppendSwitch(switches::kDisableSmoothScrolling); 56 57 // Allow manual garbage collection. 58 command_line->AppendSwitchASCII(switches::kJavaScriptFlags, "--expose_gc"); 59 } 60 61 GURL PPAPITestBase::GetTestFileUrl(const std::string& test_case) { 62 base::FilePath test_path; 63 EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &test_path)); 64 test_path = test_path.Append(FILE_PATH_LITERAL("ppapi")); 65 test_path = test_path.Append(FILE_PATH_LITERAL("tests")); 66 test_path = test_path.Append(FILE_PATH_LITERAL("test_case.html")); 67 68 // Sanity check the file name. 69 EXPECT_TRUE(base::PathExists(test_path)); 70 GURL test_url = net::FilePathToFileURL(test_path); 71 72 GURL::Replacements replacements; 73 std::string query = BuildQuery(std::string(), test_case); 74 replacements.SetQuery(query.c_str(), url::Component(0, query.size())); 75 return test_url.ReplaceComponents(replacements); 76 } 77 78 void PPAPITestBase::RunTest(const std::string& test_case) { 79 GURL url = GetTestFileUrl(test_case); 80 RunTestURL(url); 81 } 82 83 void PPAPITestBase::RunTestAndReload(const std::string& test_case) { 84 GURL url = GetTestFileUrl(test_case); 85 RunTestURL(url); 86 // If that passed, we simply run the test again, which navigates again. 87 RunTestURL(url); 88 } 89 90 void PPAPITestBase::RunTestURL(const GURL& test_url) { 91 // See comment above TestingInstance in ppapi/test/testing_instance.h. 92 // Basically it sends messages using the DOM automation controller. The 93 // value of "..." means it's still working and we should continue to wait, 94 // any other value indicates completion (in this case it will start with 95 // "PASS" or "FAIL"). This keeps us from timing out on waits for long tests. 96 PPAPITestMessageHandler handler; 97 JavascriptTestObserver observer(shell()->web_contents(), &handler); 98 shell()->LoadURL(test_url); 99 100 ASSERT_TRUE(observer.Run()) << handler.error_message(); 101 EXPECT_STREQ("PASS", handler.message().c_str()); 102 } 103 104 PPAPITest::PPAPITest() : in_process_(true) { 105 } 106 107 void PPAPITest::SetUpCommandLine(base::CommandLine* command_line) { 108 PPAPITestBase::SetUpCommandLine(command_line); 109 110 // Append the switch to register the pepper plugin. 111 // library name = <out dir>/<test_name>.<library_extension> 112 // MIME type = application/x-ppapi-<test_name> 113 base::FilePath plugin_dir; 114 EXPECT_TRUE(PathService::Get(base::DIR_MODULE, &plugin_dir)); 115 116 base::FilePath plugin_lib = plugin_dir.Append(ppapi::GetTestLibraryName()); 117 EXPECT_TRUE(base::PathExists(plugin_lib)); 118 base::FilePath::StringType pepper_plugin = plugin_lib.value(); 119 pepper_plugin.append(FILE_PATH_LITERAL(";application/x-ppapi-tests")); 120 command_line->AppendSwitchNative(switches::kRegisterPepperPlugins, 121 pepper_plugin); 122 123 if (in_process_) 124 command_line->AppendSwitch(switches::kPpapiInProcess); 125 } 126 127 std::string PPAPITest::BuildQuery(const std::string& base, 128 const std::string& test_case){ 129 return base::StringPrintf("%stestcase=%s", base.c_str(), test_case.c_str()); 130 } 131 132 OutOfProcessPPAPITest::OutOfProcessPPAPITest() { 133 in_process_ = false; 134 } 135 136 void OutOfProcessPPAPITest::SetUp() { 137 #if defined(OS_CHROMEOS) 138 chromeos::CrasAudioHandler::InitializeForTesting(); 139 #endif 140 ContentBrowserTest::SetUp(); 141 } 142 143 void OutOfProcessPPAPITest::TearDown() { 144 ContentBrowserTest::TearDown(); 145 #if defined(OS_CHROMEOS) 146 chromeos::CrasAudioHandler::Shutdown(); 147 #endif 148 } 149 150 } // namespace content 151