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.
5 #include "content/test/ppapi/ppapi_test.h"
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/public/test/ppapi_test_utils.h"
16 #include "content/shell/browser/shell.h"
17 #include "net/base/filename_util.h"
18 #include "ppapi/shared_impl/ppapi_switches.h"
20 #if defined(OS_CHROMEOS)
21 #include "chromeos/audio/cras_audio_handler.h"
26 PPAPITestMessageHandler::PPAPITestMessageHandler() {
29 TestMessageHandler::MessageResponse
PPAPITestMessageHandler::HandleMessage(
30 const std::string
& json
) {
32 base::TrimString(json
, "\"", &trimmed
);
39 void PPAPITestMessageHandler::Reset() {
40 TestMessageHandler::Reset();
44 PPAPITestBase::PPAPITestBase() { }
46 void PPAPITestBase::SetUpCommandLine(base::CommandLine
* command_line
) {
47 // Some stuff is hung off of the testing interface which is not enabled
49 command_line
->AppendSwitch(switches::kEnablePepperTesting
);
51 // Smooth scrolling confuses the scrollbar test.
52 command_line
->AppendSwitch(switches::kDisableSmoothScrolling
);
54 // Allow manual garbage collection.
55 command_line
->AppendSwitchASCII(switches::kJavaScriptFlags
, "--expose_gc");
58 GURL
PPAPITestBase::GetTestFileUrl(const std::string
& test_case
) {
59 base::FilePath test_path
;
60 EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT
, &test_path
));
61 test_path
= test_path
.Append(FILE_PATH_LITERAL("ppapi"));
62 test_path
= test_path
.Append(FILE_PATH_LITERAL("tests"));
63 test_path
= test_path
.Append(FILE_PATH_LITERAL("test_case.html"));
65 // Sanity check the file name.
66 EXPECT_TRUE(base::PathExists(test_path
));
67 GURL test_url
= net::FilePathToFileURL(test_path
);
69 GURL::Replacements replacements
;
70 std::string query
= BuildQuery(std::string(), test_case
);
71 replacements
.SetQuery(query
.c_str(), url::Component(0, query
.size()));
72 return test_url
.ReplaceComponents(replacements
);
75 void PPAPITestBase::RunTest(const std::string
& test_case
) {
76 GURL url
= GetTestFileUrl(test_case
);
80 void PPAPITestBase::RunTestAndReload(const std::string
& test_case
) {
81 GURL url
= GetTestFileUrl(test_case
);
83 // If that passed, we simply run the test again, which navigates again.
87 void PPAPITestBase::RunTestURL(const GURL
& test_url
) {
88 // See comment above TestingInstance in ppapi/test/testing_instance.h.
89 // Basically it sends messages using the DOM automation controller. The
90 // value of "..." means it's still working and we should continue to wait,
91 // any other value indicates completion (in this case it will start with
92 // "PASS" or "FAIL"). This keeps us from timing out on waits for long tests.
93 PPAPITestMessageHandler handler
;
94 JavascriptTestObserver
observer(shell()->web_contents(), &handler
);
95 shell()->LoadURL(test_url
);
97 ASSERT_TRUE(observer
.Run()) << handler
.error_message();
98 EXPECT_STREQ("PASS", handler
.message().c_str());
101 PPAPITest::PPAPITest() : in_process_(true) {
104 void PPAPITest::SetUpCommandLine(base::CommandLine
* command_line
) {
105 PPAPITestBase::SetUpCommandLine(command_line
);
106 ASSERT_TRUE(ppapi::RegisterTestPlugin(command_line
));
109 command_line
->AppendSwitch(switches::kPpapiInProcess
);
112 std::string
PPAPITest::BuildQuery(const std::string
& base
,
113 const std::string
& test_case
){
114 return base::StringPrintf("%stestcase=%s", base
.c_str(), test_case
.c_str());
117 OutOfProcessPPAPITest::OutOfProcessPPAPITest() {
121 void OutOfProcessPPAPITest::SetUp() {
122 #if defined(OS_CHROMEOS)
123 chromeos::CrasAudioHandler::InitializeForTesting();
125 ContentBrowserTest::SetUp();
128 void OutOfProcessPPAPITest::TearDown() {
129 ContentBrowserTest::TearDown();
130 #if defined(OS_CHROMEOS)
131 chromeos::CrasAudioHandler::Shutdown();
135 } // namespace content