Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / content / test / ppapi / ppapi_test.cc
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
22 PPAPITestMessageHandler::PPAPITestMessageHandler() {
23 }
24
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
35 void PPAPITestMessageHandler::Reset() {
36   TestMessageHandler::Reset();
37   message_.clear();
38 }
39
40 PPAPITestBase::PPAPITestBase() { }
41
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   // Allow manual garbage collection.
54   command_line->AppendSwitchASCII(switches::kJavaScriptFlags, "--expose_gc");
55 }
56
57 GURL PPAPITestBase::GetTestFileUrl(const std::string& test_case) {
58   base::FilePath test_path;
59   EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &test_path));
60   test_path = test_path.Append(FILE_PATH_LITERAL("ppapi"));
61   test_path = test_path.Append(FILE_PATH_LITERAL("tests"));
62   test_path = test_path.Append(FILE_PATH_LITERAL("test_case.html"));
63
64   // Sanity check the file name.
65   EXPECT_TRUE(base::PathExists(test_path));
66   GURL test_url = net::FilePathToFileURL(test_path);
67
68   GURL::Replacements replacements;
69   std::string query = BuildQuery(std::string(), test_case);
70   replacements.SetQuery(query.c_str(), url::Component(0, query.size()));
71   return test_url.ReplaceComponents(replacements);
72 }
73
74 void PPAPITestBase::RunTest(const std::string& test_case) {
75   GURL url = GetTestFileUrl(test_case);
76   RunTestURL(url);
77 }
78
79 void PPAPITestBase::RunTestAndReload(const std::string& test_case) {
80   GURL url = GetTestFileUrl(test_case);
81   RunTestURL(url);
82   // If that passed, we simply run the test again, which navigates again.
83   RunTestURL(url);
84 }
85
86 void PPAPITestBase::RunTestURL(const GURL& test_url) {
87   // See comment above TestingInstance in ppapi/test/testing_instance.h.
88   // Basically it sends messages using the DOM automation controller. The
89   // value of "..." means it's still working and we should continue to wait,
90   // any other value indicates completion (in this case it will start with
91   // "PASS" or "FAIL"). This keeps us from timing out on waits for long tests.
92   PPAPITestMessageHandler handler;
93   JavascriptTestObserver observer(shell()->web_contents(), &handler);
94   shell()->LoadURL(test_url);
95
96   ASSERT_TRUE(observer.Run()) << handler.error_message();
97   EXPECT_STREQ("PASS", handler.message().c_str());
98 }
99
100 PPAPITest::PPAPITest() : in_process_(true) {
101 }
102
103 void PPAPITest::SetUpCommandLine(base::CommandLine* command_line) {
104   PPAPITestBase::SetUpCommandLine(command_line);
105
106   // Append the switch to register the pepper plugin.
107   // library name = <out dir>/<test_name>.<library_extension>
108   // MIME type = application/x-ppapi-<test_name>
109   base::FilePath plugin_dir;
110   EXPECT_TRUE(PathService::Get(base::DIR_MODULE, &plugin_dir));
111
112   base::FilePath plugin_lib = plugin_dir.Append(ppapi::GetTestLibraryName());
113   EXPECT_TRUE(base::PathExists(plugin_lib));
114   base::FilePath::StringType pepper_plugin = plugin_lib.value();
115   pepper_plugin.append(FILE_PATH_LITERAL(";application/x-ppapi-tests"));
116   command_line->AppendSwitchNative(switches::kRegisterPepperPlugins,
117                                    pepper_plugin);
118
119   if (in_process_)
120     command_line->AppendSwitch(switches::kPpapiInProcess);
121 }
122
123 std::string PPAPITest::BuildQuery(const std::string& base,
124                                   const std::string& test_case){
125   return base::StringPrintf("%stestcase=%s", base.c_str(), test_case.c_str());
126 }
127
128 OutOfProcessPPAPITest::OutOfProcessPPAPITest() {
129   in_process_ = false;
130 }
131
132 }  // namespace content