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