1244384d1a8a518c0b1ceaa5809333a91db1104b6Kostya Serebryany// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2244384d1a8a518c0b1ceaa5809333a91db1104b6Kostya Serebryany// Use of this source code is governed by a BSD-style license that can be
3244384d1a8a518c0b1ceaa5809333a91db1104b6Kostya Serebryany// found in the LICENSE file.
4244384d1a8a518c0b1ceaa5809333a91db1104b6Kostya Serebryany
5244384d1a8a518c0b1ceaa5809333a91db1104b6Kostya Serebryany#include "chrome/test/ppapi/ppapi_test.h"
6244384d1a8a518c0b1ceaa5809333a91db1104b6Kostya Serebryany
7244384d1a8a518c0b1ceaa5809333a91db1104b6Kostya Serebryany#include "base/command_line.h"
8244384d1a8a518c0b1ceaa5809333a91db1104b6Kostya Serebryany#include "base/files/file_util.h"
9244384d1a8a518c0b1ceaa5809333a91db1104b6Kostya Serebryany#include "base/path_service.h"
10244384d1a8a518c0b1ceaa5809333a91db1104b6Kostya Serebryany#include "base/strings/string_util.h"
11244384d1a8a518c0b1ceaa5809333a91db1104b6Kostya Serebryany#include "base/strings/stringprintf.h"
12ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany#include "chrome/browser/chrome_notification_types.h"
13244384d1a8a518c0b1ceaa5809333a91db1104b6Kostya Serebryany#include "chrome/browser/content_settings/host_content_settings_map.h"
14244384d1a8a518c0b1ceaa5809333a91db1104b6Kostya Serebryany#include "chrome/browser/infobars/infobar_service.h"
15244384d1a8a518c0b1ceaa5809333a91db1104b6Kostya Serebryany#include "chrome/browser/profiles/profile.h"
16244384d1a8a518c0b1ceaa5809333a91db1104b6Kostya Serebryany#include "chrome/browser/ui/browser.h"
17244384d1a8a518c0b1ceaa5809333a91db1104b6Kostya Serebryany#include "chrome/browser/ui/tabs/tab_strip_model.h"
18ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany#include "chrome/common/chrome_paths.h"
19ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany#include "chrome/common/chrome_switches.h"
20244384d1a8a518c0b1ceaa5809333a91db1104b6Kostya Serebryany#include "chrome/test/base/test_switches.h"
21244384d1a8a518c0b1ceaa5809333a91db1104b6Kostya Serebryany#include "chrome/test/base/ui_test_utils.h"
22244384d1a8a518c0b1ceaa5809333a91db1104b6Kostya Serebryany#include "components/infobars/core/confirm_infobar_delegate.h"
23244384d1a8a518c0b1ceaa5809333a91db1104b6Kostya Serebryany#include "components/infobars/core/infobar.h"
24244384d1a8a518c0b1ceaa5809333a91db1104b6Kostya Serebryany#include "components/nacl/common/nacl_switches.h"
25244384d1a8a518c0b1ceaa5809333a91db1104b6Kostya Serebryany#include "content/public/browser/dom_operation_notification_details.h"
26ce0f7d13a6d2f1c141bc247aa75770cb88b38cb6Kostya Serebryany#include "content/public/browser/notification_service.h"
270f2283742e1d37ebf0c5ac034dab704a7d9af099Kostya Serebryany#include "content/public/browser/web_contents.h"
28244384d1a8a518c0b1ceaa5809333a91db1104b6Kostya Serebryany#include "content/public/common/content_switches.h"
29244384d1a8a518c0b1ceaa5809333a91db1104b6Kostya Serebryany#include "media/base/media_switches.h"
30244384d1a8a518c0b1ceaa5809333a91db1104b6Kostya Serebryany#include "net/base/filename_util.h"
31244384d1a8a518c0b1ceaa5809333a91db1104b6Kostya Serebryany#include "net/base/test_data_directory.h"
32244384d1a8a518c0b1ceaa5809333a91db1104b6Kostya Serebryany#include "ppapi/shared_impl/ppapi_switches.h"
33ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany#include "ppapi/shared_impl/test_harness_utils.h"
34244384d1a8a518c0b1ceaa5809333a91db1104b6Kostya Serebryany#include "ui/gl/gl_switches.h"
35244384d1a8a518c0b1ceaa5809333a91db1104b6Kostya Serebryany
36ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryanyusing content::DomOperationNotificationDetails;
37ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryanyusing content::RenderViewHost;
38ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryanyusing content::TestMessageHandler;
39ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany
40ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryanynamespace {
41ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany
42ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryanyvoid AddPrivateSwitches(base::CommandLine* command_line) {
43ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany  // For TestRequestOSFileHandle.
44ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany  command_line->AppendSwitch(switches::kUnlimitedStorage);
45ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany  command_line->AppendSwitchASCII(switches::kAllowNaClFileHandleAPI,
46ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany                                  "127.0.0.1");
47ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany}
48ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany
49ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany}  // namespace
50ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany
51ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya SerebryanyPPAPITestMessageHandler::PPAPITestMessageHandler() {
52ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany}
53ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany
54ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya SerebryanyTestMessageHandler::MessageResponse PPAPITestMessageHandler::HandleMessage(
55ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany    const std::string& json) {
56ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany  std::string trimmed;
57244384d1a8a518c0b1ceaa5809333a91db1104b6Kostya Serebryany  base::TrimString(json, "\"", &trimmed);
58ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany  if (trimmed == "...")
59ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany    return CONTINUE;
60ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany  message_ = trimmed;
61244384d1a8a518c0b1ceaa5809333a91db1104b6Kostya Serebryany  return DONE;
62ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany}
63ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany
64ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryanyvoid PPAPITestMessageHandler::Reset() {
65ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany  TestMessageHandler::Reset();
66e1c68c319a0113be832da17a777892353a8b5f23Kostya Serebryany  message_.clear();
67244384d1a8a518c0b1ceaa5809333a91db1104b6Kostya Serebryany}
682d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines
6934e3ed1db94c5ce9784d7ffb8d66a54cf523e09cKostya SerebryanyPPAPITestBase::InfoBarObserver::InfoBarObserver(PPAPITestBase* test_base)
70ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany    : test_base_(test_base),
71ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany      expecting_infobar_(false),
72ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany      should_accept_(false) {
7334e3ed1db94c5ce9784d7ffb8d66a54cf523e09cKostya Serebryany  registrar_.Add(this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED,
7434e3ed1db94c5ce9784d7ffb8d66a54cf523e09cKostya Serebryany                 content::NotificationService::AllSources());
75ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany}
76ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany
77ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya SerebryanyPPAPITestBase::InfoBarObserver::~InfoBarObserver() {
78ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany  EXPECT_FALSE(expecting_infobar_) << "Missing an expected infobar";
79244384d1a8a518c0b1ceaa5809333a91db1104b6Kostya Serebryany}
80ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany
81ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryanyvoid PPAPITestBase::InfoBarObserver::ExpectInfoBarAndAccept(
82ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany    bool should_accept) {
83ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany  ASSERT_FALSE(expecting_infobar_);
84ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany  expecting_infobar_ = true;
85244384d1a8a518c0b1ceaa5809333a91db1104b6Kostya Serebryany  should_accept_ = should_accept;
86ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany}
87ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany
88ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryanyvoid PPAPITestBase::InfoBarObserver::Observe(
89ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany    int type,
90ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany    const content::NotificationSource& source,
91ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany    const content::NotificationDetails& details) {
92ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany  ASSERT_EQ(chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED, type);
93ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany  // It's not safe to remove the infobar here, since other observers (e.g. the
94ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany  // InfoBarContainer) may still need to access it.  Instead, post a task to
95ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany  // do all necessary infobar manipulation as soon as this call stack returns.
96ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany  base::MessageLoop::current()->PostTask(
97244384d1a8a518c0b1ceaa5809333a91db1104b6Kostya Serebryany      FROM_HERE, base::Bind(&InfoBarObserver::VerifyInfoBarState,
98ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany                            base::Unretained(this)));
99ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany}
100ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany
101244384d1a8a518c0b1ceaa5809333a91db1104b6Kostya Serebryanyvoid PPAPITestBase::InfoBarObserver::VerifyInfoBarState() {
102ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany  content::WebContents* web_contents =
103ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany      test_base_->browser()->tab_strip_model()->GetActiveWebContents();
104ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany  ASSERT_TRUE(web_contents != NULL);
105ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany  InfoBarService* infobar_service =
106244384d1a8a518c0b1ceaa5809333a91db1104b6Kostya Serebryany      InfoBarService::FromWebContents(web_contents);
107ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany  ASSERT_TRUE(infobar_service != NULL);
108ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany
109ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany  EXPECT_EQ(expecting_infobar_ ? 1U : 0U, infobar_service->infobar_count());
110ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany  if (!expecting_infobar_)
111ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany    return;
112244384d1a8a518c0b1ceaa5809333a91db1104b6Kostya Serebryany  expecting_infobar_ = false;
113ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany
114ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany  infobars::InfoBar* infobar = infobar_service->infobar_at(0);
115ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany  ConfirmInfoBarDelegate* delegate =
116ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany      infobar->delegate()->AsConfirmInfoBarDelegate();
117ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany  ASSERT_TRUE(delegate != NULL);
118ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany  if (should_accept_)
119ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany    delegate->Accept();
120ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany  else
121ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany    delegate->Cancel();
122ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany
123b388987bf988435ee87dc4848ea8e62ebfa942ebKostya Serebryany  infobar_service->RemoveInfoBar(infobar);
124b388987bf988435ee87dc4848ea8e62ebfa942ebKostya Serebryany}
125b388987bf988435ee87dc4848ea8e62ebfa942ebKostya Serebryany
126b388987bf988435ee87dc4848ea8e62ebfa942ebKostya SerebryanyPPAPITestBase::PPAPITestBase() {
127244384d1a8a518c0b1ceaa5809333a91db1104b6Kostya Serebryany}
128ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany
129ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryanyvoid PPAPITestBase::SetUp() {
130ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany  EnablePixelOutput();
131ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany  InProcessBrowserTest::SetUp();
1322d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines}
1332d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines
1342d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hinesvoid PPAPITestBase::SetUpCommandLine(base::CommandLine* command_line) {
1352d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines  // The test sends us the result via a cookie.
1362d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines  command_line->AppendSwitch(switches::kEnableFileCookies);
137ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany
138ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany  // Some stuff is hung off of the testing interface which is not enabled
139ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany  // by default.
140ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany  command_line->AppendSwitch(switches::kEnablePepperTesting);
141ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany
142ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany  // Smooth scrolling confuses the scrollbar test.
143b388987bf988435ee87dc4848ea8e62ebfa942ebKostya Serebryany  command_line->AppendSwitch(switches::kDisableSmoothScrolling);
144b388987bf988435ee87dc4848ea8e62ebfa942ebKostya Serebryany}
145b388987bf988435ee87dc4848ea8e62ebfa942ebKostya Serebryany
146b388987bf988435ee87dc4848ea8e62ebfa942ebKostya Serebryanyvoid PPAPITestBase::SetUpOnMainThread() {
147b388987bf988435ee87dc4848ea8e62ebfa942ebKostya Serebryany  // Always allow access to the PPAPI broker.
148b388987bf988435ee87dc4848ea8e62ebfa942ebKostya Serebryany  browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting(
149b388987bf988435ee87dc4848ea8e62ebfa942ebKostya Serebryany      CONTENT_SETTINGS_TYPE_PPAPI_BROKER, CONTENT_SETTING_ALLOW);
150ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany}
151ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany
15289de457bd3ec40d38bc7860f88f1d4da473eacc4Kostya SerebryanyGURL PPAPITestBase::GetTestFileUrl(const std::string& test_case) {
15389de457bd3ec40d38bc7860f88f1d4da473eacc4Kostya Serebryany  base::FilePath test_path;
15489de457bd3ec40d38bc7860f88f1d4da473eacc4Kostya Serebryany  EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &test_path));
155c519335c2d6d32acaac32c0595f08a05081567e7Sergey Matveev  test_path = test_path.Append(FILE_PATH_LITERAL("ppapi"));
156c519335c2d6d32acaac32c0595f08a05081567e7Sergey Matveev  test_path = test_path.Append(FILE_PATH_LITERAL("tests"));
157ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany  test_path = test_path.Append(FILE_PATH_LITERAL("test_case.html"));
158ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany
15989de457bd3ec40d38bc7860f88f1d4da473eacc4Kostya Serebryany  // Sanity check the file name.
160ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany  EXPECT_TRUE(base::PathExists(test_path));
161ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany
162ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany  GURL test_url = net::FilePathToFileURL(test_path);
163ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany
164ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany  GURL::Replacements replacements;
165ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany  std::string query = BuildQuery(std::string(), test_case);
16689de457bd3ec40d38bc7860f88f1d4da473eacc4Kostya Serebryany  replacements.SetQuery(query.c_str(), url::Component(0, query.size()));
16789de457bd3ec40d38bc7860f88f1d4da473eacc4Kostya Serebryany  return test_url.ReplaceComponents(replacements);
168ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1Kostya Serebryany}
1697a0bba457ee05ced3adf37a0c0790d0ed23a5446Kostya Serebryany
1709433af375c7813486be91d2ac76f5072ee41818dKostya Serebryanyvoid PPAPITestBase::RunTest(const std::string& test_case) {
1719433af375c7813486be91d2ac76f5072ee41818dKostya Serebryany  GURL url = GetTestFileUrl(test_case);
1729433af375c7813486be91d2ac76f5072ee41818dKostya Serebryany  RunTestURL(url);
173244384d1a8a518c0b1ceaa5809333a91db1104b6Kostya Serebryany}
174244384d1a8a518c0b1ceaa5809333a91db1104b6Kostya Serebryany
175244384d1a8a518c0b1ceaa5809333a91db1104b6Kostya Serebryanyvoid PPAPITestBase::RunTestViaHTTP(const std::string& test_case) {
176  base::FilePath document_root;
177  ASSERT_TRUE(ui_test_utils::GetRelativeBuildDirectory(&document_root));
178  base::FilePath http_document_root;
179  ASSERT_TRUE(ui_test_utils::GetRelativeBuildDirectory(&http_document_root));
180  net::SpawnedTestServer http_server(net::SpawnedTestServer::TYPE_HTTP,
181                                     net::SpawnedTestServer::kLocalhost,
182                                     document_root);
183  ASSERT_TRUE(http_server.Start());
184  RunTestURL(GetTestURL(http_server, test_case, std::string()));
185}
186
187void PPAPITestBase::RunTestWithSSLServer(const std::string& test_case) {
188  base::FilePath http_document_root;
189  ASSERT_TRUE(ui_test_utils::GetRelativeBuildDirectory(&http_document_root));
190  net::SpawnedTestServer http_server(net::SpawnedTestServer::TYPE_HTTP,
191                                     net::SpawnedTestServer::kLocalhost,
192                                     http_document_root);
193  net::SpawnedTestServer ssl_server(net::SpawnedTestServer::TYPE_HTTPS,
194                                    net::BaseTestServer::SSLOptions(),
195                                    http_document_root);
196  // Start the servers in parallel.
197  ASSERT_TRUE(http_server.StartInBackground());
198  ASSERT_TRUE(ssl_server.StartInBackground());
199  // Wait until they are both finished before continuing.
200  ASSERT_TRUE(http_server.BlockUntilStarted());
201  ASSERT_TRUE(ssl_server.BlockUntilStarted());
202
203  uint16_t port = ssl_server.host_port_pair().port();
204  RunTestURL(GetTestURL(http_server,
205                        test_case,
206                        base::StringPrintf("ssl_server_port=%d", port)));
207}
208
209void PPAPITestBase::RunTestWithWebSocketServer(const std::string& test_case) {
210  base::FilePath http_document_root;
211  ASSERT_TRUE(ui_test_utils::GetRelativeBuildDirectory(&http_document_root));
212  net::SpawnedTestServer http_server(net::SpawnedTestServer::TYPE_HTTP,
213                                     net::SpawnedTestServer::kLocalhost,
214                                     http_document_root);
215  net::SpawnedTestServer ws_server(net::SpawnedTestServer::TYPE_WS,
216                                   net::SpawnedTestServer::kLocalhost,
217                                   net::GetWebSocketTestDataDirectory());
218  // Start the servers in parallel.
219  ASSERT_TRUE(http_server.StartInBackground());
220  ASSERT_TRUE(ws_server.StartInBackground());
221  // Wait until they are both finished before continuing.
222  ASSERT_TRUE(http_server.BlockUntilStarted());
223  ASSERT_TRUE(ws_server.BlockUntilStarted());
224
225  std::string host = ws_server.host_port_pair().HostForURL();
226  uint16_t port = ws_server.host_port_pair().port();
227  RunTestURL(GetTestURL(http_server,
228                        test_case,
229                        base::StringPrintf(
230                            "websocket_host=%s&websocket_port=%d",
231                            host.c_str(),
232                            port)));
233}
234
235void PPAPITestBase::RunTestIfAudioOutputAvailable(
236    const std::string& test_case) {
237  RunTest(test_case);
238}
239
240void PPAPITestBase::RunTestViaHTTPIfAudioOutputAvailable(
241    const std::string& test_case) {
242  RunTestViaHTTP(test_case);
243}
244
245void PPAPITestBase::RunTestURL(const GURL& test_url) {
246#if defined(OS_WIN) && defined(USE_ASH)
247  // PPAPITests are broken in Ash browser tests (http://crbug.com/263548).
248  if (base::CommandLine::ForCurrentProcess()->HasSwitch(
249          switches::kAshBrowserTests)) {
250    LOG(WARNING) << "PPAPITests are disabled for Ash browser tests.";
251    return;
252  }
253#endif
254
255  // See comment above TestingInstance in ppapi/test/testing_instance.h.
256  // Basically it sends messages using the DOM automation controller. The
257  // value of "..." means it's still working and we should continue to wait,
258  // any other value indicates completion (in this case it will start with
259  // "PASS" or "FAIL"). This keeps us from timing out on waits for long tests.
260  PPAPITestMessageHandler handler;
261  content::JavascriptTestObserver observer(
262      browser()->tab_strip_model()->GetActiveWebContents(),
263      &handler);
264
265  ui_test_utils::NavigateToURL(browser(), test_url);
266
267  ASSERT_TRUE(observer.Run()) << handler.error_message();
268  EXPECT_STREQ("PASS", handler.message().c_str());
269}
270
271GURL PPAPITestBase::GetTestURL(
272    const net::SpawnedTestServer& http_server,
273    const std::string& test_case,
274    const std::string& extra_params) {
275  std::string query = BuildQuery("files/test_case.html?", test_case);
276  if (!extra_params.empty())
277    query = base::StringPrintf("%s&%s", query.c_str(), extra_params.c_str());
278
279  return http_server.GetURL(query);
280}
281
282PPAPITest::PPAPITest() : in_process_(true) {
283}
284
285void PPAPITest::SetUpCommandLine(base::CommandLine* command_line) {
286  PPAPITestBase::SetUpCommandLine(command_line);
287
288  base::FilePath plugin_dir;
289  EXPECT_TRUE(PathService::Get(base::DIR_MODULE, &plugin_dir));
290
291  base::FilePath plugin_lib = plugin_dir.Append(ppapi::GetTestLibraryName());
292  EXPECT_TRUE(base::PathExists(plugin_lib));
293
294  // Append the switch to register the pepper plugin.
295  // library path = <out dir>/<test_name>.<library_extension>
296  // library name = "PPAPI Tests"
297  // version = "1.2.3"
298  // MIME type = application/x-ppapi-<test_name>
299  base::FilePath::StringType pepper_plugin = plugin_lib.value();
300  pepper_plugin.append(FILE_PATH_LITERAL("#PPAPI Tests"));
301  pepper_plugin.append(FILE_PATH_LITERAL("#"));  // No description.
302  pepper_plugin.append(FILE_PATH_LITERAL("#1.2.3"));
303  pepper_plugin.append(FILE_PATH_LITERAL(";application/x-ppapi-tests"));
304  command_line->AppendSwitchNative(switches::kRegisterPepperPlugins,
305                                   pepper_plugin);
306
307  command_line->AppendSwitchASCII(switches::kAllowNaClSocketAPI, "127.0.0.1");
308  if (in_process_)
309    command_line->AppendSwitch(switches::kPpapiInProcess);
310}
311
312std::string PPAPITest::BuildQuery(const std::string& base,
313                                  const std::string& test_case){
314  return base::StringPrintf("%stestcase=%s", base.c_str(), test_case.c_str());
315}
316
317void PPAPIPrivateTest::SetUpCommandLine(base::CommandLine* command_line) {
318  PPAPITest::SetUpCommandLine(command_line);
319  AddPrivateSwitches(command_line);
320}
321
322OutOfProcessPPAPITest::OutOfProcessPPAPITest() {
323  in_process_ = false;
324}
325
326void OutOfProcessPPAPITest::SetUpCommandLine(base::CommandLine* command_line) {
327  PPAPITest::SetUpCommandLine(command_line);
328  command_line->AppendSwitch(switches::kUseFakeDeviceForMediaStream);
329  command_line->AppendSwitch(switches::kUseFakeUIForMediaStream);
330}
331
332void OutOfProcessPPAPIPrivateTest::SetUpCommandLine(
333    base::CommandLine* command_line) {
334  OutOfProcessPPAPITest::SetUpCommandLine(command_line);
335  AddPrivateSwitches(command_line);
336}
337
338void PPAPINaClTest::SetUpCommandLine(base::CommandLine* command_line) {
339#if !defined(DISABLE_NACL)
340  PPAPITestBase::SetUpCommandLine(command_line);
341
342  // Enable running (non-portable) NaCl outside of the Chrome web store.
343  command_line->AppendSwitch(switches::kEnableNaCl);
344  command_line->AppendSwitchASCII(switches::kAllowNaClSocketAPI, "127.0.0.1");
345  command_line->AppendSwitch(switches::kUseFakeDeviceForMediaStream);
346  command_line->AppendSwitch(switches::kUseFakeUIForMediaStream);
347#endif
348}
349
350void PPAPINaClTest::SetUpOnMainThread() {
351}
352
353void PPAPINaClTest::RunTest(const std::string& test_case) {
354#if !defined(DISABLE_NACL)
355  PPAPITestBase::RunTest(test_case);
356#endif
357}
358
359void PPAPINaClTest::RunTestViaHTTP(const std::string& test_case) {
360#if !defined(DISABLE_NACL)
361  PPAPITestBase::RunTestViaHTTP(test_case);
362#endif
363}
364
365void PPAPINaClTest::RunTestWithSSLServer(const std::string& test_case) {
366#if !defined(DISABLE_NACL)
367  PPAPITestBase::RunTestWithSSLServer(test_case);
368#endif
369}
370
371void PPAPINaClTest::RunTestWithWebSocketServer(const std::string& test_case) {
372#if !defined(DISABLE_NACL)
373  PPAPITestBase::RunTestWithWebSocketServer(test_case);
374#endif
375}
376
377void PPAPINaClTest::RunTestIfAudioOutputAvailable(
378    const std::string& test_case) {
379#if !defined(DISABLE_NACL)
380  PPAPITestBase::RunTestIfAudioOutputAvailable(test_case);
381#endif
382}
383
384void PPAPINaClTest::RunTestViaHTTPIfAudioOutputAvailable(
385    const std::string& test_case) {
386#if !defined(DISABLE_NACL)
387  PPAPITestBase::RunTestViaHTTPIfAudioOutputAvailable(test_case);
388#endif
389}
390
391// Append the correct mode and testcase string
392std::string PPAPINaClNewlibTest::BuildQuery(const std::string& base,
393                                            const std::string& test_case) {
394  return base::StringPrintf("%smode=nacl_newlib&testcase=%s", base.c_str(),
395                            test_case.c_str());
396}
397
398void PPAPIPrivateNaClNewlibTest::SetUpCommandLine(
399    base::CommandLine* command_line) {
400  PPAPINaClNewlibTest::SetUpCommandLine(command_line);
401  AddPrivateSwitches(command_line);
402}
403
404// Append the correct mode and testcase string
405std::string PPAPINaClGLibcTest::BuildQuery(const std::string& base,
406                                           const std::string& test_case) {
407  return base::StringPrintf("%smode=nacl_glibc&testcase=%s", base.c_str(),
408                            test_case.c_str());
409}
410
411void PPAPIPrivateNaClGLibcTest::SetUpCommandLine(
412    base::CommandLine* command_line) {
413  PPAPINaClGLibcTest::SetUpCommandLine(command_line);
414  AddPrivateSwitches(command_line);
415}
416
417// Append the correct mode and testcase string
418std::string PPAPINaClPNaClTest::BuildQuery(const std::string& base,
419                                           const std::string& test_case) {
420  return base::StringPrintf("%smode=nacl_pnacl&testcase=%s", base.c_str(),
421                            test_case.c_str());
422}
423
424void PPAPIPrivateNaClPNaClTest::SetUpCommandLine(
425    base::CommandLine* command_line) {
426  PPAPINaClPNaClTest::SetUpCommandLine(command_line);
427  AddPrivateSwitches(command_line);
428}
429
430void PPAPINaClPNaClNonSfiTest::SetUpCommandLine(
431    base::CommandLine* command_line) {
432#if !defined(DISABLE_NACL)
433  PPAPINaClTest::SetUpCommandLine(command_line);
434  command_line->AppendSwitch(switches::kEnableNaClNonSfiMode);
435#endif
436}
437
438std::string PPAPINaClPNaClNonSfiTest::BuildQuery(
439    const std::string& base,
440    const std::string& test_case) {
441  return base::StringPrintf("%smode=nacl_pnacl_nonsfi&testcase=%s",
442                            base.c_str(), test_case.c_str());
443}
444
445void PPAPIPrivateNaClPNaClNonSfiTest::SetUpCommandLine(
446    base::CommandLine* command_line) {
447  PPAPINaClPNaClNonSfiTest::SetUpCommandLine(command_line);
448  AddPrivateSwitches(command_line);
449}
450
451void PPAPINaClTestDisallowedSockets::SetUpCommandLine(
452    base::CommandLine* command_line) {
453  PPAPITestBase::SetUpCommandLine(command_line);
454
455  // Enable running (non-portable) NaCl outside of the Chrome web store.
456  command_line->AppendSwitch(switches::kEnableNaCl);
457}
458
459// Append the correct mode and testcase string
460std::string PPAPINaClTestDisallowedSockets::BuildQuery(
461    const std::string& base,
462    const std::string& test_case) {
463  return base::StringPrintf("%smode=nacl_newlib&testcase=%s", base.c_str(),
464                            test_case.c_str());
465}
466
467void PPAPIBrokerInfoBarTest::SetUpOnMainThread() {
468  // The default content setting for the PPAPI broker is ASK. We purposefully
469  // don't call PPAPITestBase::SetUpOnMainThread() to keep it that way.
470}
471