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/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/content_settings/host_content_settings_map.h"
14 #include "chrome/browser/infobars/confirm_infobar_delegate.h"
15 #include "chrome/browser/infobars/infobar.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/ui/browser.h"
18 #include "chrome/browser/ui/tabs/tab_strip_model.h"
19 #include "chrome/common/chrome_paths.h"
20 #include "chrome/common/chrome_switches.h"
21 #include "chrome/test/base/test_switches.h"
22 #include "chrome/test/base/ui_test_utils.h"
23 #include "content/public/browser/dom_operation_notification_details.h"
24 #include "content/public/browser/notification_service.h"
25 #include "content/public/browser/web_contents.h"
26 #include "net/base/net_util.h"
27 #include "net/base/test_data_directory.h"
28 #include "ppapi/shared_impl/ppapi_switches.h"
29 #include "ui/gl/gl_switches.h"
31 using content::DomOperationNotificationDetails
;
32 using content::RenderViewHost
;
36 // Platform-specific filename relative to the chrome executable.
38 const wchar_t library_name
[] = L
"ppapi_tests.dll";
39 #elif defined(OS_MACOSX)
40 const char library_name
[] = "ppapi_tests.plugin";
41 #elif defined(OS_POSIX)
42 const char library_name
[] = "libppapi_tests.so";
45 void AddPrivateSwitches(CommandLine
* command_line
) {
46 // For TestRequestOSFileHandle.
47 command_line
->AppendSwitch(switches::kUnlimitedStorage
);
48 command_line
->AppendSwitchASCII(switches::kAllowNaClFileHandleAPI
,
54 PPAPITestMessageHandler::PPAPITestMessageHandler() {
57 TestMessageHandler::MessageResponse
PPAPITestMessageHandler::HandleMessage(
58 const std::string
& json
) {
60 base::TrimString(json
, "\"", &trimmed
);
67 void PPAPITestMessageHandler::Reset() {
68 TestMessageHandler::Reset();
72 PPAPITestBase::InfoBarObserver::InfoBarObserver(PPAPITestBase
* test_base
)
73 : test_base_(test_base
),
74 expecting_infobar_(false),
75 should_accept_(false) {
76 registrar_
.Add(this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED
,
77 content::NotificationService::AllSources());
80 PPAPITestBase::InfoBarObserver::~InfoBarObserver() {
81 EXPECT_FALSE(expecting_infobar_
) << "Missing an expected infobar";
84 void PPAPITestBase::InfoBarObserver::ExpectInfoBarAndAccept(
86 ASSERT_FALSE(expecting_infobar_
);
87 expecting_infobar_
= true;
88 should_accept_
= should_accept
;
91 void PPAPITestBase::InfoBarObserver::Observe(
93 const content::NotificationSource
& source
,
94 const content::NotificationDetails
& details
) {
95 ASSERT_EQ(chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED
, type
);
96 // It's not safe to remove the infobar here, since other observers (e.g. the
97 // InfoBarContainer) may still need to access it. Instead, post a task to
98 // do all necessary infobar manipulation as soon as this call stack returns.
99 base::MessageLoop::current()->PostTask(
100 FROM_HERE
, base::Bind(&InfoBarObserver::VerifyInfoBarState
,
101 base::Unretained(this)));
104 void PPAPITestBase::InfoBarObserver::VerifyInfoBarState() {
105 content::WebContents
* web_contents
=
106 test_base_
->browser()->tab_strip_model()->GetActiveWebContents();
107 ASSERT_TRUE(web_contents
!= NULL
);
108 InfoBarService
* infobar_service
=
109 InfoBarService::FromWebContents(web_contents
);
110 ASSERT_TRUE(infobar_service
!= NULL
);
112 EXPECT_EQ(expecting_infobar_
? 1U : 0U, infobar_service
->infobar_count());
113 if (!expecting_infobar_
)
115 expecting_infobar_
= false;
117 InfoBar
* infobar
= infobar_service
->infobar_at(0);
118 ConfirmInfoBarDelegate
* delegate
=
119 infobar
->delegate()->AsConfirmInfoBarDelegate();
120 ASSERT_TRUE(delegate
!= NULL
);
126 infobar_service
->RemoveInfoBar(infobar
);
129 PPAPITestBase::PPAPITestBase() {
132 void PPAPITestBase::SetUpCommandLine(CommandLine
* command_line
) {
133 // The test sends us the result via a cookie.
134 command_line
->AppendSwitch(switches::kEnableFileCookies
);
136 // Some stuff is hung off of the testing interface which is not enabled
138 command_line
->AppendSwitch(switches::kEnablePepperTesting
);
140 // Smooth scrolling confuses the scrollbar test.
141 command_line
->AppendSwitch(switches::kDisableSmoothScrolling
);
144 void PPAPITestBase::SetUpOnMainThread() {
145 // Always allow access to the PPAPI broker.
146 browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting(
147 CONTENT_SETTINGS_TYPE_PPAPI_BROKER
, CONTENT_SETTING_ALLOW
);
150 GURL
PPAPITestBase::GetTestFileUrl(const std::string
& test_case
) {
151 base::FilePath test_path
;
152 EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT
, &test_path
));
153 test_path
= test_path
.Append(FILE_PATH_LITERAL("ppapi"));
154 test_path
= test_path
.Append(FILE_PATH_LITERAL("tests"));
155 test_path
= test_path
.Append(FILE_PATH_LITERAL("test_case.html"));
157 // Sanity check the file name.
158 EXPECT_TRUE(base::PathExists(test_path
));
160 GURL test_url
= net::FilePathToFileURL(test_path
);
162 GURL::Replacements replacements
;
163 std::string query
= BuildQuery(std::string(), test_case
);
164 replacements
.SetQuery(query
.c_str(), url_parse::Component(0, query
.size()));
165 return test_url
.ReplaceComponents(replacements
);
168 void PPAPITestBase::RunTest(const std::string
& test_case
) {
169 GURL url
= GetTestFileUrl(test_case
);
173 void PPAPITestBase::RunTestAndReload(const std::string
& test_case
) {
174 GURL url
= GetTestFileUrl(test_case
);
176 // If that passed, we simply run the test again, which navigates again.
180 void PPAPITestBase::RunTestViaHTTP(const std::string
& test_case
) {
181 base::FilePath document_root
;
182 ASSERT_TRUE(ui_test_utils::GetRelativeBuildDirectory(&document_root
));
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 ASSERT_TRUE(http_server
.Start());
189 RunTestURL(GetTestURL(http_server
, test_case
, std::string()));
192 void PPAPITestBase::RunTestWithSSLServer(const std::string
& test_case
) {
193 base::FilePath http_document_root
;
194 ASSERT_TRUE(ui_test_utils::GetRelativeBuildDirectory(&http_document_root
));
195 net::SpawnedTestServer
http_server(net::SpawnedTestServer::TYPE_HTTP
,
196 net::SpawnedTestServer::kLocalhost
,
198 net::SpawnedTestServer
ssl_server(net::SpawnedTestServer::TYPE_HTTPS
,
199 net::BaseTestServer::SSLOptions(),
201 // Start the servers in parallel.
202 ASSERT_TRUE(http_server
.StartInBackground());
203 ASSERT_TRUE(ssl_server
.StartInBackground());
204 // Wait until they are both finished before continuing.
205 ASSERT_TRUE(http_server
.BlockUntilStarted());
206 ASSERT_TRUE(ssl_server
.BlockUntilStarted());
208 uint16_t port
= ssl_server
.host_port_pair().port();
209 RunTestURL(GetTestURL(http_server
,
211 base::StringPrintf("ssl_server_port=%d", port
)));
214 void PPAPITestBase::RunTestWithWebSocketServer(const std::string
& test_case
) {
215 base::FilePath http_document_root
;
216 ASSERT_TRUE(ui_test_utils::GetRelativeBuildDirectory(&http_document_root
));
217 net::SpawnedTestServer
http_server(net::SpawnedTestServer::TYPE_HTTP
,
218 net::SpawnedTestServer::kLocalhost
,
220 net::SpawnedTestServer
ws_server(net::SpawnedTestServer::TYPE_WS
,
221 net::SpawnedTestServer::kLocalhost
,
222 net::GetWebSocketTestDataDirectory());
223 // Start the servers in parallel.
224 ASSERT_TRUE(http_server
.StartInBackground());
225 ASSERT_TRUE(ws_server
.StartInBackground());
226 // Wait until they are both finished before continuing.
227 ASSERT_TRUE(http_server
.BlockUntilStarted());
228 ASSERT_TRUE(ws_server
.BlockUntilStarted());
230 std::string host
= ws_server
.host_port_pair().HostForURL();
231 uint16_t port
= ws_server
.host_port_pair().port();
232 RunTestURL(GetTestURL(http_server
,
235 "websocket_host=%s&websocket_port=%d",
240 void PPAPITestBase::RunTestIfAudioOutputAvailable(
241 const std::string
& test_case
) {
245 void PPAPITestBase::RunTestViaHTTPIfAudioOutputAvailable(
246 const std::string
& test_case
) {
247 RunTestViaHTTP(test_case
);
250 std::string
PPAPITestBase::StripPrefixes(const std::string
& test_name
) {
251 const char* const prefixes
[] = {
252 "FAILS_", "FLAKY_", "DISABLED_", "SLOW_" };
253 for (size_t i
= 0; i
< sizeof(prefixes
)/sizeof(prefixes
[0]); ++i
)
254 if (test_name
.find(prefixes
[i
]) == 0)
255 return test_name
.substr(strlen(prefixes
[i
]));
259 void PPAPITestBase::RunTestURL(const GURL
& test_url
) {
260 #if defined(OS_WIN) && defined(USE_ASH)
261 // PPAPITests are broken in Ash browser tests (http://crbug.com/263548).
262 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
)) {
263 LOG(WARNING
) << "PPAPITests are disabled for Ash browser tests.";
268 // See comment above TestingInstance in ppapi/test/testing_instance.h.
269 // Basically it sends messages using the DOM automation controller. The
270 // value of "..." means it's still working and we should continue to wait,
271 // any other value indicates completion (in this case it will start with
272 // "PASS" or "FAIL"). This keeps us from timing out on waits for long tests.
273 PPAPITestMessageHandler handler
;
274 JavascriptTestObserver
observer(
275 browser()->tab_strip_model()->GetActiveWebContents()->GetRenderViewHost(),
278 ui_test_utils::NavigateToURL(browser(), test_url
);
280 ASSERT_TRUE(observer
.Run()) << handler
.error_message();
281 EXPECT_STREQ("PASS", handler
.message().c_str());
284 GURL
PPAPITestBase::GetTestURL(
285 const net::SpawnedTestServer
& http_server
,
286 const std::string
& test_case
,
287 const std::string
& extra_params
) {
288 std::string query
= BuildQuery("files/test_case.html?", test_case
);
289 if (!extra_params
.empty())
290 query
= base::StringPrintf("%s&%s", query
.c_str(), extra_params
.c_str());
292 return http_server
.GetURL(query
);
295 PPAPITest::PPAPITest() : in_process_(true) {
298 void PPAPITest::SetUpCommandLine(CommandLine
* command_line
) {
299 PPAPITestBase::SetUpCommandLine(command_line
);
301 // Append the switch to register the pepper plugin.
302 // library name = <out dir>/<test_name>.<library_extension>
303 // MIME type = application/x-ppapi-<test_name>
304 base::FilePath plugin_dir
;
305 EXPECT_TRUE(PathService::Get(base::DIR_MODULE
, &plugin_dir
));
307 base::FilePath plugin_lib
= plugin_dir
.Append(library_name
);
308 EXPECT_TRUE(base::PathExists(plugin_lib
));
309 base::FilePath::StringType pepper_plugin
= plugin_lib
.value();
310 pepper_plugin
.append(FILE_PATH_LITERAL(";application/x-ppapi-tests"));
311 command_line
->AppendSwitchNative(switches::kRegisterPepperPlugins
,
313 command_line
->AppendSwitchASCII(switches::kAllowNaClSocketAPI
, "127.0.0.1");
316 command_line
->AppendSwitch(switches::kPpapiInProcess
);
319 std::string
PPAPITest::BuildQuery(const std::string
& base
,
320 const std::string
& test_case
){
321 return base::StringPrintf("%stestcase=%s", base
.c_str(), test_case
.c_str());
324 void PPAPIPrivateTest::SetUpCommandLine(CommandLine
* command_line
) {
325 PPAPITest::SetUpCommandLine(command_line
);
326 AddPrivateSwitches(command_line
);
329 OutOfProcessPPAPITest::OutOfProcessPPAPITest() {
333 void OutOfProcessPPAPITest::SetUpCommandLine(CommandLine
* command_line
) {
334 PPAPITest::SetUpCommandLine(command_line
);
335 command_line
->AppendSwitch(switches::kUseFakeDeviceForMediaStream
);
336 command_line
->AppendSwitch(switches::kUseFakeUIForMediaStream
);
339 void OutOfProcessPPAPIPrivateTest::SetUpCommandLine(CommandLine
* command_line
) {
340 OutOfProcessPPAPITest::SetUpCommandLine(command_line
);
341 AddPrivateSwitches(command_line
);
344 void PPAPINaClTest::SetUpCommandLine(CommandLine
* command_line
) {
345 PPAPITestBase::SetUpCommandLine(command_line
);
347 base::FilePath plugin_lib
;
348 EXPECT_TRUE(PathService::Get(chrome::FILE_NACL_PLUGIN
, &plugin_lib
));
349 EXPECT_TRUE(base::PathExists(plugin_lib
));
351 // Enable running (non-portable) NaCl outside of the Chrome web store.
352 command_line
->AppendSwitch(switches::kEnableNaCl
);
353 command_line
->AppendSwitchASCII(switches::kAllowNaClSocketAPI
, "127.0.0.1");
354 command_line
->AppendSwitch(switches::kUseFakeDeviceForMediaStream
);
355 command_line
->AppendSwitch(switches::kUseFakeUIForMediaStream
);
358 // Append the correct mode and testcase string
359 std::string
PPAPINaClNewlibTest::BuildQuery(const std::string
& base
,
360 const std::string
& test_case
) {
361 return base::StringPrintf("%smode=nacl_newlib&testcase=%s", base
.c_str(),
365 void PPAPIPrivateNaClNewlibTest::SetUpCommandLine(CommandLine
* command_line
) {
366 PPAPINaClNewlibTest::SetUpCommandLine(command_line
);
367 AddPrivateSwitches(command_line
);
370 // Append the correct mode and testcase string
371 std::string
PPAPINaClGLibcTest::BuildQuery(const std::string
& base
,
372 const std::string
& test_case
) {
373 return base::StringPrintf("%smode=nacl_glibc&testcase=%s", base
.c_str(),
377 void PPAPIPrivateNaClGLibcTest::SetUpCommandLine(CommandLine
* command_line
) {
378 PPAPINaClGLibcTest::SetUpCommandLine(command_line
);
379 AddPrivateSwitches(command_line
);
382 // Append the correct mode and testcase string
383 std::string
PPAPINaClPNaClTest::BuildQuery(const std::string
& base
,
384 const std::string
& test_case
) {
385 return base::StringPrintf("%smode=nacl_pnacl&testcase=%s", base
.c_str(),
389 void PPAPIPrivateNaClPNaClTest::SetUpCommandLine(CommandLine
* command_line
) {
390 PPAPINaClPNaClTest::SetUpCommandLine(command_line
);
391 AddPrivateSwitches(command_line
);
394 void PPAPINaClTestDisallowedSockets::SetUpCommandLine(
395 CommandLine
* command_line
) {
396 PPAPITestBase::SetUpCommandLine(command_line
);
398 base::FilePath plugin_lib
;
399 EXPECT_TRUE(PathService::Get(chrome::FILE_NACL_PLUGIN
, &plugin_lib
));
400 EXPECT_TRUE(base::PathExists(plugin_lib
));
402 // Enable running (non-portable) NaCl outside of the Chrome web store.
403 command_line
->AppendSwitch(switches::kEnableNaCl
);
406 // Append the correct mode and testcase string
407 std::string
PPAPINaClTestDisallowedSockets::BuildQuery(
408 const std::string
& base
,
409 const std::string
& test_case
) {
410 return base::StringPrintf("%smode=nacl_newlib&testcase=%s", base
.c_str(),
414 void PPAPIBrokerInfoBarTest::SetUpOnMainThread() {
415 // The default content setting for the PPAPI broker is ASK. We purposefully
416 // don't call PPAPITestBase::SetUpOnMainThread() to keep it that way.