1 // Copyright (c) 2012 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 "chrome/test/ppapi/ppapi_test.h"
7 #include "base/command_line.h"
8 #include "base/files/file_util.h"
9 #include "base/location.h"
10 #include "base/path_service.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/thread_task_runner_handle.h"
15 #include "chrome/browser/chrome_notification_types.h"
16 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
17 #include "chrome/browser/infobars/infobar_service.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/ui/browser.h"
20 #include "chrome/browser/ui/tabs/tab_strip_model.h"
21 #include "chrome/common/chrome_paths.h"
22 #include "chrome/common/chrome_switches.h"
23 #include "chrome/test/base/test_switches.h"
24 #include "chrome/test/base/ui_test_utils.h"
25 #include "components/content_settings/core/browser/host_content_settings_map.h"
26 #include "components/infobars/core/confirm_infobar_delegate.h"
27 #include "components/infobars/core/infobar.h"
28 #include "components/nacl/common/nacl_switches.h"
29 #include "content/public/browser/dom_operation_notification_details.h"
30 #include "content/public/browser/notification_service.h"
31 #include "content/public/browser/web_contents.h"
32 #include "content/public/common/content_switches.h"
33 #include "content/public/test/ppapi_test_utils.h"
34 #include "media/base/media_switches.h"
35 #include "net/base/filename_util.h"
36 #include "net/base/test_data_directory.h"
37 #include "ppapi/shared_impl/ppapi_switches.h"
38 #include "ui/gl/gl_switches.h"
40 using content::DomOperationNotificationDetails
;
41 using content::RenderViewHost
;
42 using content::TestMessageHandler
;
46 void AddPrivateSwitches(base::CommandLine
* command_line
) {
47 // For TestRequestOSFileHandle.
48 command_line
->AppendSwitch(switches::kUnlimitedStorage
);
49 command_line
->AppendSwitchASCII(switches::kAllowNaClFileHandleAPI
,
55 PPAPITestMessageHandler::PPAPITestMessageHandler() {
58 TestMessageHandler::MessageResponse
PPAPITestMessageHandler::HandleMessage(
59 const std::string
& json
) {
61 base::TrimString(json
, "\"", &trimmed
);
68 void PPAPITestMessageHandler::Reset() {
69 TestMessageHandler::Reset();
73 PPAPITestBase::InfoBarObserver::InfoBarObserver(PPAPITestBase
* test_base
)
74 : test_base_(test_base
),
75 expecting_infobar_(false),
76 should_accept_(false) {
77 registrar_
.Add(this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED
,
78 content::NotificationService::AllSources());
81 PPAPITestBase::InfoBarObserver::~InfoBarObserver() {
82 EXPECT_FALSE(expecting_infobar_
) << "Missing an expected infobar";
85 void PPAPITestBase::InfoBarObserver::ExpectInfoBarAndAccept(
87 ASSERT_FALSE(expecting_infobar_
);
88 expecting_infobar_
= true;
89 should_accept_
= should_accept
;
92 void PPAPITestBase::InfoBarObserver::Observe(
94 const content::NotificationSource
& source
,
95 const content::NotificationDetails
& details
) {
96 ASSERT_EQ(chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED
, type
);
97 // It's not safe to remove the infobar here, since other observers (e.g. the
98 // InfoBarContainer) may still need to access it. Instead, post a task to
99 // do all necessary infobar manipulation as soon as this call stack returns.
100 base::ThreadTaskRunnerHandle::Get()->PostTask(
102 base::Bind(&InfoBarObserver::VerifyInfoBarState
, base::Unretained(this)));
105 void PPAPITestBase::InfoBarObserver::VerifyInfoBarState() {
106 content::WebContents
* web_contents
=
107 test_base_
->browser()->tab_strip_model()->GetActiveWebContents();
108 ASSERT_TRUE(web_contents
!= NULL
);
109 InfoBarService
* infobar_service
=
110 InfoBarService::FromWebContents(web_contents
);
111 ASSERT_TRUE(infobar_service
!= NULL
);
113 EXPECT_EQ(expecting_infobar_
? 1U : 0U, infobar_service
->infobar_count());
114 if (!expecting_infobar_
)
116 expecting_infobar_
= false;
118 infobars::InfoBar
* infobar
= infobar_service
->infobar_at(0);
119 ConfirmInfoBarDelegate
* delegate
=
120 infobar
->delegate()->AsConfirmInfoBarDelegate();
121 ASSERT_TRUE(delegate
!= NULL
);
127 infobar_service
->RemoveInfoBar(infobar
);
130 PPAPITestBase::PPAPITestBase() {
133 void PPAPITestBase::SetUp() {
135 InProcessBrowserTest::SetUp();
138 void PPAPITestBase::SetUpCommandLine(base::CommandLine
* command_line
) {
139 // Some stuff is hung off of the testing interface which is not enabled
141 command_line
->AppendSwitch(switches::kEnablePepperTesting
);
143 // Smooth scrolling confuses the scrollbar test.
144 command_line
->AppendSwitch(switches::kDisableSmoothScrolling
);
147 void PPAPITestBase::SetUpOnMainThread() {
148 // Always allow access to the PPAPI broker.
149 HostContentSettingsMapFactory::GetForProfile(browser()->profile())
150 ->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_PPAPI_BROKER
,
151 CONTENT_SETTING_ALLOW
);
154 GURL
PPAPITestBase::GetTestFileUrl(const std::string
& test_case
) {
155 base::FilePath test_path
;
156 EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT
, &test_path
));
157 test_path
= test_path
.Append(FILE_PATH_LITERAL("ppapi"));
158 test_path
= test_path
.Append(FILE_PATH_LITERAL("tests"));
159 test_path
= test_path
.Append(FILE_PATH_LITERAL("test_case.html"));
161 // Sanity check the file name.
162 EXPECT_TRUE(base::PathExists(test_path
));
164 GURL test_url
= net::FilePathToFileURL(test_path
);
166 GURL::Replacements replacements
;
167 std::string query
= BuildQuery(std::string(), test_case
);
168 replacements
.SetQuery(query
.c_str(), url::Component(0, query
.size()));
169 return test_url
.ReplaceComponents(replacements
);
172 void PPAPITestBase::RunTest(const std::string
& test_case
) {
173 GURL url
= GetTestFileUrl(test_case
);
177 void PPAPITestBase::RunTestViaHTTP(const std::string
& test_case
) {
178 base::FilePath document_root
;
179 ASSERT_TRUE(ui_test_utils::GetRelativeBuildDirectory(&document_root
));
180 net::SpawnedTestServer
http_server(net::SpawnedTestServer::TYPE_HTTP
,
181 net::SpawnedTestServer::kLocalhost
,
183 ASSERT_TRUE(http_server
.Start());
184 RunTestURL(GetTestURL(http_server
, test_case
, std::string()));
187 void 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
,
193 net::SpawnedTestServer
ssl_server(net::SpawnedTestServer::TYPE_HTTPS
,
194 net::BaseTestServer::SSLOptions(),
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());
203 uint16_t port
= ssl_server
.host_port_pair().port();
204 RunTestURL(GetTestURL(http_server
,
206 base::StringPrintf("ssl_server_port=%d", port
)));
209 void 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
,
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());
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
,
230 "websocket_host=%s&websocket_port=%d",
235 void PPAPITestBase::RunTestIfAudioOutputAvailable(
236 const std::string
& test_case
) {
240 void PPAPITestBase::RunTestViaHTTPIfAudioOutputAvailable(
241 const std::string
& test_case
) {
242 RunTestViaHTTP(test_case
);
245 void 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.";
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(),
265 ui_test_utils::NavigateToURL(browser(), test_url
);
267 ASSERT_TRUE(observer
.Run()) << handler
.error_message();
268 EXPECT_STREQ("PASS", handler
.message().c_str());
271 GURL
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());
279 return http_server
.GetURL(query
);
282 PPAPITest::PPAPITest() : in_process_(true) {
285 void PPAPITest::SetUpCommandLine(base::CommandLine
* command_line
) {
286 PPAPITestBase::SetUpCommandLine(command_line
);
288 // library name = "PPAPI Tests"
290 base::FilePath::StringType parameters
;
291 parameters
.append(FILE_PATH_LITERAL("#PPAPI Tests"));
292 parameters
.append(FILE_PATH_LITERAL("#")); // No description.
293 parameters
.append(FILE_PATH_LITERAL("#1.2.3"));
295 ppapi::RegisterTestPluginWithExtraParameters(command_line
, parameters
));
297 command_line
->AppendSwitchASCII(switches::kAllowNaClSocketAPI
, "127.0.0.1");
299 command_line
->AppendSwitch(switches::kPpapiInProcess
);
302 std::string
PPAPITest::BuildQuery(const std::string
& base
,
303 const std::string
& test_case
){
304 return base::StringPrintf("%stestcase=%s", base
.c_str(), test_case
.c_str());
307 void PPAPIPrivateTest::SetUpCommandLine(base::CommandLine
* command_line
) {
308 PPAPITest::SetUpCommandLine(command_line
);
309 AddPrivateSwitches(command_line
);
312 OutOfProcessPPAPITest::OutOfProcessPPAPITest() {
316 void OutOfProcessPPAPITest::SetUpCommandLine(base::CommandLine
* command_line
) {
317 PPAPITest::SetUpCommandLine(command_line
);
318 command_line
->AppendSwitch(switches::kUseFakeDeviceForMediaStream
);
319 command_line
->AppendSwitch(switches::kUseFakeUIForMediaStream
);
322 void OutOfProcessPPAPIPrivateTest::SetUpCommandLine(
323 base::CommandLine
* command_line
) {
324 OutOfProcessPPAPITest::SetUpCommandLine(command_line
);
325 AddPrivateSwitches(command_line
);
328 void PPAPINaClTest::SetUpCommandLine(base::CommandLine
* command_line
) {
329 PPAPITestBase::SetUpCommandLine(command_line
);
330 #if !defined(DISABLE_NACL)
331 // Enable running (non-portable) NaCl outside of the Chrome web store.
332 command_line
->AppendSwitch(switches::kEnableNaCl
);
333 command_line
->AppendSwitchASCII(switches::kAllowNaClSocketAPI
, "127.0.0.1");
334 command_line
->AppendSwitch(switches::kUseFakeDeviceForMediaStream
);
335 command_line
->AppendSwitch(switches::kUseFakeUIForMediaStream
);
339 void PPAPINaClTest::SetUpOnMainThread() {
342 void PPAPINaClTest::RunTest(const std::string
& test_case
) {
343 #if !defined(DISABLE_NACL)
344 PPAPITestBase::RunTest(test_case
);
348 void PPAPINaClTest::RunTestViaHTTP(const std::string
& test_case
) {
349 #if !defined(DISABLE_NACL)
350 PPAPITestBase::RunTestViaHTTP(test_case
);
354 void PPAPINaClTest::RunTestWithSSLServer(const std::string
& test_case
) {
355 #if !defined(DISABLE_NACL)
356 PPAPITestBase::RunTestWithSSLServer(test_case
);
360 void PPAPINaClTest::RunTestWithWebSocketServer(const std::string
& test_case
) {
361 #if !defined(DISABLE_NACL)
362 PPAPITestBase::RunTestWithWebSocketServer(test_case
);
366 void PPAPINaClTest::RunTestIfAudioOutputAvailable(
367 const std::string
& test_case
) {
368 #if !defined(DISABLE_NACL)
369 PPAPITestBase::RunTestIfAudioOutputAvailable(test_case
);
373 void PPAPINaClTest::RunTestViaHTTPIfAudioOutputAvailable(
374 const std::string
& test_case
) {
375 #if !defined(DISABLE_NACL)
376 PPAPITestBase::RunTestViaHTTPIfAudioOutputAvailable(test_case
);
380 // Append the correct mode and testcase string
381 std::string
PPAPINaClNewlibTest::BuildQuery(const std::string
& base
,
382 const std::string
& test_case
) {
383 return base::StringPrintf("%smode=nacl_newlib&testcase=%s", base
.c_str(),
387 void PPAPIPrivateNaClNewlibTest::SetUpCommandLine(
388 base::CommandLine
* command_line
) {
389 PPAPINaClNewlibTest::SetUpCommandLine(command_line
);
390 AddPrivateSwitches(command_line
);
393 // Append the correct mode and testcase string
394 std::string
PPAPINaClGLibcTest::BuildQuery(const std::string
& base
,
395 const std::string
& test_case
) {
396 return base::StringPrintf("%smode=nacl_glibc&testcase=%s", base
.c_str(),
400 void PPAPIPrivateNaClGLibcTest::SetUpCommandLine(
401 base::CommandLine
* command_line
) {
402 PPAPINaClGLibcTest::SetUpCommandLine(command_line
);
403 AddPrivateSwitches(command_line
);
406 // Append the correct mode and testcase string
407 std::string
PPAPINaClPNaClTest::BuildQuery(const std::string
& base
,
408 const std::string
& test_case
) {
409 return base::StringPrintf("%smode=nacl_pnacl&testcase=%s", base
.c_str(),
413 void PPAPIPrivateNaClPNaClTest::SetUpCommandLine(
414 base::CommandLine
* command_line
) {
415 PPAPINaClPNaClTest::SetUpCommandLine(command_line
);
416 AddPrivateSwitches(command_line
);
419 void PPAPINaClPNaClNonSfiTest::SetUpCommandLine(
420 base::CommandLine
* command_line
) {
421 PPAPINaClTest::SetUpCommandLine(command_line
);
422 #if !defined(DISABLE_NACL)
423 command_line
->AppendSwitch(switches::kEnableNaClNonSfiMode
);
427 std::string
PPAPINaClPNaClNonSfiTest::BuildQuery(
428 const std::string
& base
,
429 const std::string
& test_case
) {
430 return base::StringPrintf("%smode=nacl_pnacl_nonsfi&testcase=%s",
431 base
.c_str(), test_case
.c_str());
434 void PPAPIPrivateNaClPNaClNonSfiTest::SetUpCommandLine(
435 base::CommandLine
* command_line
) {
436 PPAPINaClPNaClNonSfiTest::SetUpCommandLine(command_line
);
437 AddPrivateSwitches(command_line
);
440 void PPAPINaClTestDisallowedSockets::SetUpCommandLine(
441 base::CommandLine
* command_line
) {
442 PPAPITestBase::SetUpCommandLine(command_line
);
444 // Enable running (non-portable) NaCl outside of the Chrome web store.
445 command_line
->AppendSwitch(switches::kEnableNaCl
);
448 // Append the correct mode and testcase string
449 std::string
PPAPINaClTestDisallowedSockets::BuildQuery(
450 const std::string
& base
,
451 const std::string
& test_case
) {
452 return base::StringPrintf("%smode=nacl_newlib&testcase=%s", base
.c_str(),
456 void PPAPIBrokerInfoBarTest::SetUpOnMainThread() {
457 // The default content setting for the PPAPI broker is ASK. We purposefully
458 // don't call PPAPITestBase::SetUpOnMainThread() to keep it that way.