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/path_service.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/stringprintf.h"
12 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/infobars/infobar_service.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/tabs/tab_strip_model.h"
17 #include "chrome/common/chrome_paths.h"
18 #include "chrome/common/chrome_switches.h"
19 #include "chrome/test/base/test_switches.h"
20 #include "chrome/test/base/ui_test_utils.h"
21 #include "components/content_settings/core/browser/host_content_settings_map.h"
22 #include "components/infobars/core/confirm_infobar_delegate.h"
23 #include "components/infobars/core/infobar.h"
24 #include "components/nacl/common/nacl_switches.h"
25 #include "content/public/browser/dom_operation_notification_details.h"
26 #include "content/public/browser/notification_service.h"
27 #include "content/public/browser/web_contents.h"
28 #include "content/public/common/content_switches.h"
29 #include "content/public/test/ppapi_test_utils.h"
30 #include "media/base/media_switches.h"
31 #include "net/base/filename_util.h"
32 #include "net/base/test_data_directory.h"
33 #include "ppapi/shared_impl/ppapi_switches.h"
34 #include "ui/gl/gl_switches.h"
36 using content::DomOperationNotificationDetails
;
37 using content::RenderViewHost
;
38 using content::TestMessageHandler
;
42 void AddPrivateSwitches(base::CommandLine
* command_line
) {
43 // For TestRequestOSFileHandle.
44 command_line
->AppendSwitch(switches::kUnlimitedStorage
);
45 command_line
->AppendSwitchASCII(switches::kAllowNaClFileHandleAPI
,
51 PPAPITestMessageHandler::PPAPITestMessageHandler() {
54 TestMessageHandler::MessageResponse
PPAPITestMessageHandler::HandleMessage(
55 const std::string
& json
) {
57 base::TrimString(json
, "\"", &trimmed
);
64 void PPAPITestMessageHandler::Reset() {
65 TestMessageHandler::Reset();
69 PPAPITestBase::InfoBarObserver::InfoBarObserver(PPAPITestBase
* test_base
)
70 : test_base_(test_base
),
71 expecting_infobar_(false),
72 should_accept_(false) {
73 registrar_
.Add(this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED
,
74 content::NotificationService::AllSources());
77 PPAPITestBase::InfoBarObserver::~InfoBarObserver() {
78 EXPECT_FALSE(expecting_infobar_
) << "Missing an expected infobar";
81 void PPAPITestBase::InfoBarObserver::ExpectInfoBarAndAccept(
83 ASSERT_FALSE(expecting_infobar_
);
84 expecting_infobar_
= true;
85 should_accept_
= should_accept
;
88 void PPAPITestBase::InfoBarObserver::Observe(
90 const content::NotificationSource
& source
,
91 const content::NotificationDetails
& details
) {
92 ASSERT_EQ(chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED
, type
);
93 // It's not safe to remove the infobar here, since other observers (e.g. the
94 // InfoBarContainer) may still need to access it. Instead, post a task to
95 // do all necessary infobar manipulation as soon as this call stack returns.
96 base::MessageLoop::current()->PostTask(
97 FROM_HERE
, base::Bind(&InfoBarObserver::VerifyInfoBarState
,
98 base::Unretained(this)));
101 void PPAPITestBase::InfoBarObserver::VerifyInfoBarState() {
102 content::WebContents
* web_contents
=
103 test_base_
->browser()->tab_strip_model()->GetActiveWebContents();
104 ASSERT_TRUE(web_contents
!= NULL
);
105 InfoBarService
* infobar_service
=
106 InfoBarService::FromWebContents(web_contents
);
107 ASSERT_TRUE(infobar_service
!= NULL
);
109 EXPECT_EQ(expecting_infobar_
? 1U : 0U, infobar_service
->infobar_count());
110 if (!expecting_infobar_
)
112 expecting_infobar_
= false;
114 infobars::InfoBar
* infobar
= infobar_service
->infobar_at(0);
115 ConfirmInfoBarDelegate
* delegate
=
116 infobar
->delegate()->AsConfirmInfoBarDelegate();
117 ASSERT_TRUE(delegate
!= NULL
);
123 infobar_service
->RemoveInfoBar(infobar
);
126 PPAPITestBase::PPAPITestBase() {
129 void PPAPITestBase::SetUp() {
131 InProcessBrowserTest::SetUp();
134 void PPAPITestBase::SetUpCommandLine(base::CommandLine
* command_line
) {
135 // Some stuff is hung off of the testing interface which is not enabled
137 command_line
->AppendSwitch(switches::kEnablePepperTesting
);
139 // Smooth scrolling confuses the scrollbar test.
140 command_line
->AppendSwitch(switches::kDisableSmoothScrolling
);
143 void PPAPITestBase::SetUpOnMainThread() {
144 // Always allow access to the PPAPI broker.
145 browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting(
146 CONTENT_SETTINGS_TYPE_PPAPI_BROKER
, CONTENT_SETTING_ALLOW
);
149 GURL
PPAPITestBase::GetTestFileUrl(const std::string
& test_case
) {
150 base::FilePath test_path
;
151 EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT
, &test_path
));
152 test_path
= test_path
.Append(FILE_PATH_LITERAL("ppapi"));
153 test_path
= test_path
.Append(FILE_PATH_LITERAL("tests"));
154 test_path
= test_path
.Append(FILE_PATH_LITERAL("test_case.html"));
156 // Sanity check the file name.
157 EXPECT_TRUE(base::PathExists(test_path
));
159 GURL test_url
= net::FilePathToFileURL(test_path
);
161 GURL::Replacements replacements
;
162 std::string query
= BuildQuery(std::string(), test_case
);
163 replacements
.SetQuery(query
.c_str(), url::Component(0, query
.size()));
164 return test_url
.ReplaceComponents(replacements
);
167 void PPAPITestBase::RunTest(const std::string
& test_case
) {
168 GURL url
= GetTestFileUrl(test_case
);
172 void PPAPITestBase::RunTestViaHTTP(const std::string
& test_case
) {
173 base::FilePath document_root
;
174 ASSERT_TRUE(ui_test_utils::GetRelativeBuildDirectory(&document_root
));
175 net::SpawnedTestServer
http_server(net::SpawnedTestServer::TYPE_HTTP
,
176 net::SpawnedTestServer::kLocalhost
,
178 ASSERT_TRUE(http_server
.Start());
179 RunTestURL(GetTestURL(http_server
, test_case
, std::string()));
182 void PPAPITestBase::RunTestWithSSLServer(const std::string
& test_case
) {
183 base::FilePath http_document_root
;
184 ASSERT_TRUE(ui_test_utils::GetRelativeBuildDirectory(&http_document_root
));
185 net::SpawnedTestServer
http_server(net::SpawnedTestServer::TYPE_HTTP
,
186 net::SpawnedTestServer::kLocalhost
,
188 net::SpawnedTestServer
ssl_server(net::SpawnedTestServer::TYPE_HTTPS
,
189 net::BaseTestServer::SSLOptions(),
191 // Start the servers in parallel.
192 ASSERT_TRUE(http_server
.StartInBackground());
193 ASSERT_TRUE(ssl_server
.StartInBackground());
194 // Wait until they are both finished before continuing.
195 ASSERT_TRUE(http_server
.BlockUntilStarted());
196 ASSERT_TRUE(ssl_server
.BlockUntilStarted());
198 uint16_t port
= ssl_server
.host_port_pair().port();
199 RunTestURL(GetTestURL(http_server
,
201 base::StringPrintf("ssl_server_port=%d", port
)));
204 void PPAPITestBase::RunTestWithWebSocketServer(const std::string
& test_case
) {
205 base::FilePath http_document_root
;
206 ASSERT_TRUE(ui_test_utils::GetRelativeBuildDirectory(&http_document_root
));
207 net::SpawnedTestServer
http_server(net::SpawnedTestServer::TYPE_HTTP
,
208 net::SpawnedTestServer::kLocalhost
,
210 net::SpawnedTestServer
ws_server(net::SpawnedTestServer::TYPE_WS
,
211 net::SpawnedTestServer::kLocalhost
,
212 net::GetWebSocketTestDataDirectory());
213 // Start the servers in parallel.
214 ASSERT_TRUE(http_server
.StartInBackground());
215 ASSERT_TRUE(ws_server
.StartInBackground());
216 // Wait until they are both finished before continuing.
217 ASSERT_TRUE(http_server
.BlockUntilStarted());
218 ASSERT_TRUE(ws_server
.BlockUntilStarted());
220 std::string host
= ws_server
.host_port_pair().HostForURL();
221 uint16_t port
= ws_server
.host_port_pair().port();
222 RunTestURL(GetTestURL(http_server
,
225 "websocket_host=%s&websocket_port=%d",
230 void PPAPITestBase::RunTestIfAudioOutputAvailable(
231 const std::string
& test_case
) {
235 void PPAPITestBase::RunTestViaHTTPIfAudioOutputAvailable(
236 const std::string
& test_case
) {
237 RunTestViaHTTP(test_case
);
240 void PPAPITestBase::RunTestURL(const GURL
& test_url
) {
241 #if defined(OS_WIN) && defined(USE_ASH)
242 // PPAPITests are broken in Ash browser tests (http://crbug.com/263548).
243 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
244 switches::kAshBrowserTests
)) {
245 LOG(WARNING
) << "PPAPITests are disabled for Ash browser tests.";
250 // See comment above TestingInstance in ppapi/test/testing_instance.h.
251 // Basically it sends messages using the DOM automation controller. The
252 // value of "..." means it's still working and we should continue to wait,
253 // any other value indicates completion (in this case it will start with
254 // "PASS" or "FAIL"). This keeps us from timing out on waits for long tests.
255 PPAPITestMessageHandler handler
;
256 content::JavascriptTestObserver
observer(
257 browser()->tab_strip_model()->GetActiveWebContents(),
260 ui_test_utils::NavigateToURL(browser(), test_url
);
262 ASSERT_TRUE(observer
.Run()) << handler
.error_message();
263 EXPECT_STREQ("PASS", handler
.message().c_str());
266 GURL
PPAPITestBase::GetTestURL(
267 const net::SpawnedTestServer
& http_server
,
268 const std::string
& test_case
,
269 const std::string
& extra_params
) {
270 std::string query
= BuildQuery("files/test_case.html?", test_case
);
271 if (!extra_params
.empty())
272 query
= base::StringPrintf("%s&%s", query
.c_str(), extra_params
.c_str());
274 return http_server
.GetURL(query
);
277 PPAPITest::PPAPITest() : in_process_(true) {
280 void PPAPITest::SetUpCommandLine(base::CommandLine
* command_line
) {
281 PPAPITestBase::SetUpCommandLine(command_line
);
283 // library name = "PPAPI Tests"
285 base::FilePath::StringType parameters
;
286 parameters
.append(FILE_PATH_LITERAL("#PPAPI Tests"));
287 parameters
.append(FILE_PATH_LITERAL("#")); // No description.
288 parameters
.append(FILE_PATH_LITERAL("#1.2.3"));
290 ppapi::RegisterTestPluginWithExtraParameters(command_line
, parameters
));
292 command_line
->AppendSwitchASCII(switches::kAllowNaClSocketAPI
, "127.0.0.1");
294 command_line
->AppendSwitch(switches::kPpapiInProcess
);
297 std::string
PPAPITest::BuildQuery(const std::string
& base
,
298 const std::string
& test_case
){
299 return base::StringPrintf("%stestcase=%s", base
.c_str(), test_case
.c_str());
302 void PPAPIPrivateTest::SetUpCommandLine(base::CommandLine
* command_line
) {
303 PPAPITest::SetUpCommandLine(command_line
);
304 AddPrivateSwitches(command_line
);
307 OutOfProcessPPAPITest::OutOfProcessPPAPITest() {
311 void OutOfProcessPPAPITest::SetUpCommandLine(base::CommandLine
* command_line
) {
312 PPAPITest::SetUpCommandLine(command_line
);
313 command_line
->AppendSwitch(switches::kUseFakeDeviceForMediaStream
);
314 command_line
->AppendSwitch(switches::kUseFakeUIForMediaStream
);
317 void OutOfProcessPPAPIPrivateTest::SetUpCommandLine(
318 base::CommandLine
* command_line
) {
319 OutOfProcessPPAPITest::SetUpCommandLine(command_line
);
320 AddPrivateSwitches(command_line
);
323 void PPAPINaClTest::SetUpCommandLine(base::CommandLine
* command_line
) {
324 PPAPITestBase::SetUpCommandLine(command_line
);
325 #if !defined(DISABLE_NACL)
326 // Enable running (non-portable) NaCl outside of the Chrome web store.
327 command_line
->AppendSwitch(switches::kEnableNaCl
);
328 command_line
->AppendSwitchASCII(switches::kAllowNaClSocketAPI
, "127.0.0.1");
329 command_line
->AppendSwitch(switches::kUseFakeDeviceForMediaStream
);
330 command_line
->AppendSwitch(switches::kUseFakeUIForMediaStream
);
334 void PPAPINaClTest::SetUpOnMainThread() {
337 void PPAPINaClTest::RunTest(const std::string
& test_case
) {
338 #if !defined(DISABLE_NACL)
339 PPAPITestBase::RunTest(test_case
);
343 void PPAPINaClTest::RunTestViaHTTP(const std::string
& test_case
) {
344 #if !defined(DISABLE_NACL)
345 PPAPITestBase::RunTestViaHTTP(test_case
);
349 void PPAPINaClTest::RunTestWithSSLServer(const std::string
& test_case
) {
350 #if !defined(DISABLE_NACL)
351 PPAPITestBase::RunTestWithSSLServer(test_case
);
355 void PPAPINaClTest::RunTestWithWebSocketServer(const std::string
& test_case
) {
356 #if !defined(DISABLE_NACL)
357 PPAPITestBase::RunTestWithWebSocketServer(test_case
);
361 void PPAPINaClTest::RunTestIfAudioOutputAvailable(
362 const std::string
& test_case
) {
363 #if !defined(DISABLE_NACL)
364 PPAPITestBase::RunTestIfAudioOutputAvailable(test_case
);
368 void PPAPINaClTest::RunTestViaHTTPIfAudioOutputAvailable(
369 const std::string
& test_case
) {
370 #if !defined(DISABLE_NACL)
371 PPAPITestBase::RunTestViaHTTPIfAudioOutputAvailable(test_case
);
375 // Append the correct mode and testcase string
376 std::string
PPAPINaClNewlibTest::BuildQuery(const std::string
& base
,
377 const std::string
& test_case
) {
378 return base::StringPrintf("%smode=nacl_newlib&testcase=%s", base
.c_str(),
382 void PPAPIPrivateNaClNewlibTest::SetUpCommandLine(
383 base::CommandLine
* command_line
) {
384 PPAPINaClNewlibTest::SetUpCommandLine(command_line
);
385 AddPrivateSwitches(command_line
);
388 // Append the correct mode and testcase string
389 std::string
PPAPINaClGLibcTest::BuildQuery(const std::string
& base
,
390 const std::string
& test_case
) {
391 return base::StringPrintf("%smode=nacl_glibc&testcase=%s", base
.c_str(),
395 void PPAPIPrivateNaClGLibcTest::SetUpCommandLine(
396 base::CommandLine
* command_line
) {
397 PPAPINaClGLibcTest::SetUpCommandLine(command_line
);
398 AddPrivateSwitches(command_line
);
401 // Append the correct mode and testcase string
402 std::string
PPAPINaClPNaClTest::BuildQuery(const std::string
& base
,
403 const std::string
& test_case
) {
404 return base::StringPrintf("%smode=nacl_pnacl&testcase=%s", base
.c_str(),
408 void PPAPIPrivateNaClPNaClTest::SetUpCommandLine(
409 base::CommandLine
* command_line
) {
410 PPAPINaClPNaClTest::SetUpCommandLine(command_line
);
411 AddPrivateSwitches(command_line
);
414 void PPAPINaClPNaClNonSfiTest::SetUpCommandLine(
415 base::CommandLine
* command_line
) {
416 PPAPINaClTest::SetUpCommandLine(command_line
);
417 #if !defined(DISABLE_NACL)
418 command_line
->AppendSwitch(switches::kEnableNaClNonSfiMode
);
422 std::string
PPAPINaClPNaClNonSfiTest::BuildQuery(
423 const std::string
& base
,
424 const std::string
& test_case
) {
425 return base::StringPrintf("%smode=nacl_pnacl_nonsfi&testcase=%s",
426 base
.c_str(), test_case
.c_str());
429 void PPAPINaClPNaClTransitionalNonSfiTest::SetUpCommandLine(
430 base::CommandLine
* command_line
) {
431 PPAPINaClPNaClNonSfiTest::SetUpCommandLine(command_line
);
432 #if !defined(DISABLE_NACL)
433 command_line
->AppendSwitch(switches::kUseNaClHelperNonSfi
);
437 void PPAPIPrivateNaClPNaClNonSfiTest::SetUpCommandLine(
438 base::CommandLine
* command_line
) {
439 PPAPINaClPNaClNonSfiTest::SetUpCommandLine(command_line
);
440 AddPrivateSwitches(command_line
);
443 void PPAPIPrivateNaClPNaClTransitionalNonSfiTest::SetUpCommandLine(
444 base::CommandLine
* command_line
) {
445 PPAPIPrivateNaClPNaClNonSfiTest::SetUpCommandLine(command_line
);
446 #if !defined(DISABLE_NACL)
447 command_line
->AppendSwitch(switches::kUseNaClHelperNonSfi
);
451 void PPAPINaClTestDisallowedSockets::SetUpCommandLine(
452 base::CommandLine
* command_line
) {
453 PPAPITestBase::SetUpCommandLine(command_line
);
455 // Enable running (non-portable) NaCl outside of the Chrome web store.
456 command_line
->AppendSwitch(switches::kEnableNaCl
);
459 // Append the correct mode and testcase string
460 std::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(),
467 void 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.