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