Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / test / ppapi / ppapi_test.cc
blob11e287d2917bd926050cea4ad32e92a6b9bceaed
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/infobars/infobar_service.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/ui/browser.h"
19 #include "chrome/browser/ui/tabs/tab_strip_model.h"
20 #include "chrome/common/chrome_paths.h"
21 #include "chrome/common/chrome_switches.h"
22 #include "chrome/test/base/test_switches.h"
23 #include "chrome/test/base/ui_test_utils.h"
24 #include "components/content_settings/core/browser/host_content_settings_map.h"
25 #include "components/infobars/core/confirm_infobar_delegate.h"
26 #include "components/infobars/core/infobar.h"
27 #include "components/nacl/common/nacl_switches.h"
28 #include "content/public/browser/dom_operation_notification_details.h"
29 #include "content/public/browser/notification_service.h"
30 #include "content/public/browser/web_contents.h"
31 #include "content/public/common/content_switches.h"
32 #include "content/public/test/ppapi_test_utils.h"
33 #include "media/base/media_switches.h"
34 #include "net/base/filename_util.h"
35 #include "net/base/test_data_directory.h"
36 #include "ppapi/shared_impl/ppapi_switches.h"
37 #include "ui/gl/gl_switches.h"
39 using content::DomOperationNotificationDetails;
40 using content::RenderViewHost;
41 using content::TestMessageHandler;
43 namespace {
45 void AddPrivateSwitches(base::CommandLine* command_line) {
46 // For TestRequestOSFileHandle.
47 command_line->AppendSwitch(switches::kUnlimitedStorage);
48 command_line->AppendSwitchASCII(switches::kAllowNaClFileHandleAPI,
49 "127.0.0.1");
52 } // namespace
54 PPAPITestMessageHandler::PPAPITestMessageHandler() {
57 TestMessageHandler::MessageResponse PPAPITestMessageHandler::HandleMessage(
58 const std::string& json) {
59 std::string trimmed;
60 base::TrimString(json, "\"", &trimmed);
61 if (trimmed == "...")
62 return CONTINUE;
63 message_ = trimmed;
64 return DONE;
67 void PPAPITestMessageHandler::Reset() {
68 TestMessageHandler::Reset();
69 message_.clear();
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(
85 bool should_accept) {
86 ASSERT_FALSE(expecting_infobar_);
87 expecting_infobar_ = true;
88 should_accept_ = should_accept;
91 void PPAPITestBase::InfoBarObserver::Observe(
92 int type,
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::ThreadTaskRunnerHandle::Get()->PostTask(
100 FROM_HERE,
101 base::Bind(&InfoBarObserver::VerifyInfoBarState, 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_)
114 return;
115 expecting_infobar_ = false;
117 infobars::InfoBar* infobar = infobar_service->infobar_at(0);
118 ConfirmInfoBarDelegate* delegate =
119 infobar->delegate()->AsConfirmInfoBarDelegate();
120 ASSERT_TRUE(delegate != NULL);
121 if (should_accept_)
122 delegate->Accept();
123 else
124 delegate->Cancel();
126 infobar_service->RemoveInfoBar(infobar);
129 PPAPITestBase::PPAPITestBase() {
132 void PPAPITestBase::SetUp() {
133 EnablePixelOutput();
134 InProcessBrowserTest::SetUp();
137 void PPAPITestBase::SetUpCommandLine(base::CommandLine* command_line) {
138 // Some stuff is hung off of the testing interface which is not enabled
139 // by default.
140 command_line->AppendSwitch(switches::kEnablePepperTesting);
142 // Smooth scrolling confuses the scrollbar test.
143 command_line->AppendSwitch(switches::kDisableSmoothScrolling);
146 void PPAPITestBase::SetUpOnMainThread() {
147 // Always allow access to the PPAPI broker.
148 browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting(
149 CONTENT_SETTINGS_TYPE_PPAPI_BROKER, CONTENT_SETTING_ALLOW);
152 GURL PPAPITestBase::GetTestFileUrl(const std::string& test_case) {
153 base::FilePath test_path;
154 EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &test_path));
155 test_path = test_path.Append(FILE_PATH_LITERAL("ppapi"));
156 test_path = test_path.Append(FILE_PATH_LITERAL("tests"));
157 test_path = test_path.Append(FILE_PATH_LITERAL("test_case.html"));
159 // Sanity check the file name.
160 EXPECT_TRUE(base::PathExists(test_path));
162 GURL test_url = net::FilePathToFileURL(test_path);
164 GURL::Replacements replacements;
165 std::string query = BuildQuery(std::string(), test_case);
166 replacements.SetQuery(query.c_str(), url::Component(0, query.size()));
167 return test_url.ReplaceComponents(replacements);
170 void PPAPITestBase::RunTest(const std::string& test_case) {
171 GURL url = GetTestFileUrl(test_case);
172 RunTestURL(url);
175 void PPAPITestBase::RunTestViaHTTP(const std::string& test_case) {
176 base::FilePath document_root;
177 ASSERT_TRUE(ui_test_utils::GetRelativeBuildDirectory(&document_root));
178 net::SpawnedTestServer http_server(net::SpawnedTestServer::TYPE_HTTP,
179 net::SpawnedTestServer::kLocalhost,
180 document_root);
181 ASSERT_TRUE(http_server.Start());
182 RunTestURL(GetTestURL(http_server, test_case, std::string()));
185 void PPAPITestBase::RunTestWithSSLServer(const std::string& test_case) {
186 base::FilePath http_document_root;
187 ASSERT_TRUE(ui_test_utils::GetRelativeBuildDirectory(&http_document_root));
188 net::SpawnedTestServer http_server(net::SpawnedTestServer::TYPE_HTTP,
189 net::SpawnedTestServer::kLocalhost,
190 http_document_root);
191 net::SpawnedTestServer ssl_server(net::SpawnedTestServer::TYPE_HTTPS,
192 net::BaseTestServer::SSLOptions(),
193 http_document_root);
194 // Start the servers in parallel.
195 ASSERT_TRUE(http_server.StartInBackground());
196 ASSERT_TRUE(ssl_server.StartInBackground());
197 // Wait until they are both finished before continuing.
198 ASSERT_TRUE(http_server.BlockUntilStarted());
199 ASSERT_TRUE(ssl_server.BlockUntilStarted());
201 uint16_t port = ssl_server.host_port_pair().port();
202 RunTestURL(GetTestURL(http_server,
203 test_case,
204 base::StringPrintf("ssl_server_port=%d", port)));
207 void PPAPITestBase::RunTestWithWebSocketServer(const std::string& test_case) {
208 base::FilePath http_document_root;
209 ASSERT_TRUE(ui_test_utils::GetRelativeBuildDirectory(&http_document_root));
210 net::SpawnedTestServer http_server(net::SpawnedTestServer::TYPE_HTTP,
211 net::SpawnedTestServer::kLocalhost,
212 http_document_root);
213 net::SpawnedTestServer ws_server(net::SpawnedTestServer::TYPE_WS,
214 net::SpawnedTestServer::kLocalhost,
215 net::GetWebSocketTestDataDirectory());
216 // Start the servers in parallel.
217 ASSERT_TRUE(http_server.StartInBackground());
218 ASSERT_TRUE(ws_server.StartInBackground());
219 // Wait until they are both finished before continuing.
220 ASSERT_TRUE(http_server.BlockUntilStarted());
221 ASSERT_TRUE(ws_server.BlockUntilStarted());
223 std::string host = ws_server.host_port_pair().HostForURL();
224 uint16_t port = ws_server.host_port_pair().port();
225 RunTestURL(GetTestURL(http_server,
226 test_case,
227 base::StringPrintf(
228 "websocket_host=%s&websocket_port=%d",
229 host.c_str(),
230 port)));
233 void PPAPITestBase::RunTestIfAudioOutputAvailable(
234 const std::string& test_case) {
235 RunTest(test_case);
238 void PPAPITestBase::RunTestViaHTTPIfAudioOutputAvailable(
239 const std::string& test_case) {
240 RunTestViaHTTP(test_case);
243 void PPAPITestBase::RunTestURL(const GURL& test_url) {
244 #if defined(OS_WIN) && defined(USE_ASH)
245 // PPAPITests are broken in Ash browser tests (http://crbug.com/263548).
246 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
247 switches::kAshBrowserTests)) {
248 LOG(WARNING) << "PPAPITests are disabled for Ash browser tests.";
249 return;
251 #endif
253 // See comment above TestingInstance in ppapi/test/testing_instance.h.
254 // Basically it sends messages using the DOM automation controller. The
255 // value of "..." means it's still working and we should continue to wait,
256 // any other value indicates completion (in this case it will start with
257 // "PASS" or "FAIL"). This keeps us from timing out on waits for long tests.
258 PPAPITestMessageHandler handler;
259 content::JavascriptTestObserver observer(
260 browser()->tab_strip_model()->GetActiveWebContents(),
261 &handler);
263 ui_test_utils::NavigateToURL(browser(), test_url);
265 ASSERT_TRUE(observer.Run()) << handler.error_message();
266 EXPECT_STREQ("PASS", handler.message().c_str());
269 GURL PPAPITestBase::GetTestURL(
270 const net::SpawnedTestServer& http_server,
271 const std::string& test_case,
272 const std::string& extra_params) {
273 std::string query = BuildQuery("files/test_case.html?", test_case);
274 if (!extra_params.empty())
275 query = base::StringPrintf("%s&%s", query.c_str(), extra_params.c_str());
277 return http_server.GetURL(query);
280 PPAPITest::PPAPITest() : in_process_(true) {
283 void PPAPITest::SetUpCommandLine(base::CommandLine* command_line) {
284 PPAPITestBase::SetUpCommandLine(command_line);
286 // library name = "PPAPI Tests"
287 // version = "1.2.3"
288 base::FilePath::StringType parameters;
289 parameters.append(FILE_PATH_LITERAL("#PPAPI Tests"));
290 parameters.append(FILE_PATH_LITERAL("#")); // No description.
291 parameters.append(FILE_PATH_LITERAL("#1.2.3"));
292 ASSERT_TRUE(
293 ppapi::RegisterTestPluginWithExtraParameters(command_line, parameters));
295 command_line->AppendSwitchASCII(switches::kAllowNaClSocketAPI, "127.0.0.1");
296 if (in_process_)
297 command_line->AppendSwitch(switches::kPpapiInProcess);
300 std::string PPAPITest::BuildQuery(const std::string& base,
301 const std::string& test_case){
302 return base::StringPrintf("%stestcase=%s", base.c_str(), test_case.c_str());
305 void PPAPIPrivateTest::SetUpCommandLine(base::CommandLine* command_line) {
306 PPAPITest::SetUpCommandLine(command_line);
307 AddPrivateSwitches(command_line);
310 OutOfProcessPPAPITest::OutOfProcessPPAPITest() {
311 in_process_ = false;
314 void OutOfProcessPPAPITest::SetUpCommandLine(base::CommandLine* command_line) {
315 PPAPITest::SetUpCommandLine(command_line);
316 command_line->AppendSwitch(switches::kUseFakeDeviceForMediaStream);
317 command_line->AppendSwitch(switches::kUseFakeUIForMediaStream);
320 void OutOfProcessPPAPIPrivateTest::SetUpCommandLine(
321 base::CommandLine* command_line) {
322 OutOfProcessPPAPITest::SetUpCommandLine(command_line);
323 AddPrivateSwitches(command_line);
326 void PPAPINaClTest::SetUpCommandLine(base::CommandLine* command_line) {
327 PPAPITestBase::SetUpCommandLine(command_line);
328 #if !defined(DISABLE_NACL)
329 // Enable running (non-portable) NaCl outside of the Chrome web store.
330 command_line->AppendSwitch(switches::kEnableNaCl);
331 command_line->AppendSwitchASCII(switches::kAllowNaClSocketAPI, "127.0.0.1");
332 command_line->AppendSwitch(switches::kUseFakeDeviceForMediaStream);
333 command_line->AppendSwitch(switches::kUseFakeUIForMediaStream);
334 #endif
337 void PPAPINaClTest::SetUpOnMainThread() {
340 void PPAPINaClTest::RunTest(const std::string& test_case) {
341 #if !defined(DISABLE_NACL)
342 PPAPITestBase::RunTest(test_case);
343 #endif
346 void PPAPINaClTest::RunTestViaHTTP(const std::string& test_case) {
347 #if !defined(DISABLE_NACL)
348 PPAPITestBase::RunTestViaHTTP(test_case);
349 #endif
352 void PPAPINaClTest::RunTestWithSSLServer(const std::string& test_case) {
353 #if !defined(DISABLE_NACL)
354 PPAPITestBase::RunTestWithSSLServer(test_case);
355 #endif
358 void PPAPINaClTest::RunTestWithWebSocketServer(const std::string& test_case) {
359 #if !defined(DISABLE_NACL)
360 PPAPITestBase::RunTestWithWebSocketServer(test_case);
361 #endif
364 void PPAPINaClTest::RunTestIfAudioOutputAvailable(
365 const std::string& test_case) {
366 #if !defined(DISABLE_NACL)
367 PPAPITestBase::RunTestIfAudioOutputAvailable(test_case);
368 #endif
371 void PPAPINaClTest::RunTestViaHTTPIfAudioOutputAvailable(
372 const std::string& test_case) {
373 #if !defined(DISABLE_NACL)
374 PPAPITestBase::RunTestViaHTTPIfAudioOutputAvailable(test_case);
375 #endif
378 // Append the correct mode and testcase string
379 std::string PPAPINaClNewlibTest::BuildQuery(const std::string& base,
380 const std::string& test_case) {
381 return base::StringPrintf("%smode=nacl_newlib&testcase=%s", base.c_str(),
382 test_case.c_str());
385 void PPAPIPrivateNaClNewlibTest::SetUpCommandLine(
386 base::CommandLine* command_line) {
387 PPAPINaClNewlibTest::SetUpCommandLine(command_line);
388 AddPrivateSwitches(command_line);
391 // Append the correct mode and testcase string
392 std::string PPAPINaClGLibcTest::BuildQuery(const std::string& base,
393 const std::string& test_case) {
394 return base::StringPrintf("%smode=nacl_glibc&testcase=%s", base.c_str(),
395 test_case.c_str());
398 void PPAPIPrivateNaClGLibcTest::SetUpCommandLine(
399 base::CommandLine* command_line) {
400 PPAPINaClGLibcTest::SetUpCommandLine(command_line);
401 AddPrivateSwitches(command_line);
404 // Append the correct mode and testcase string
405 std::string PPAPINaClPNaClTest::BuildQuery(const std::string& base,
406 const std::string& test_case) {
407 return base::StringPrintf("%smode=nacl_pnacl&testcase=%s", base.c_str(),
408 test_case.c_str());
411 void PPAPIPrivateNaClPNaClTest::SetUpCommandLine(
412 base::CommandLine* command_line) {
413 PPAPINaClPNaClTest::SetUpCommandLine(command_line);
414 AddPrivateSwitches(command_line);
417 void PPAPINaClPNaClNonSfiTest::SetUpCommandLine(
418 base::CommandLine* command_line) {
419 PPAPINaClTest::SetUpCommandLine(command_line);
420 #if !defined(DISABLE_NACL)
421 command_line->AppendSwitch(switches::kEnableNaClNonSfiMode);
422 #endif
425 std::string PPAPINaClPNaClNonSfiTest::BuildQuery(
426 const std::string& base,
427 const std::string& test_case) {
428 return base::StringPrintf("%smode=nacl_pnacl_nonsfi&testcase=%s",
429 base.c_str(), test_case.c_str());
432 void PPAPINaClPNaClTransitionalNonSfiTest::SetUpCommandLine(
433 base::CommandLine* command_line) {
434 PPAPINaClPNaClNonSfiTest::SetUpCommandLine(command_line);
435 #if !defined(DISABLE_NACL)
436 command_line->AppendSwitchASCII(switches::kUseNaClHelperNonSfi, "false");
437 #endif
440 void PPAPIPrivateNaClPNaClNonSfiTest::SetUpCommandLine(
441 base::CommandLine* command_line) {
442 PPAPINaClPNaClNonSfiTest::SetUpCommandLine(command_line);
443 AddPrivateSwitches(command_line);
446 void PPAPIPrivateNaClPNaClTransitionalNonSfiTest::SetUpCommandLine(
447 base::CommandLine* command_line) {
448 PPAPIPrivateNaClPNaClNonSfiTest::SetUpCommandLine(command_line);
449 #if !defined(DISABLE_NACL)
450 command_line->AppendSwitchASCII(switches::kUseNaClHelperNonSfi, "false");
451 #endif
454 void PPAPINaClTestDisallowedSockets::SetUpCommandLine(
455 base::CommandLine* command_line) {
456 PPAPITestBase::SetUpCommandLine(command_line);
458 // Enable running (non-portable) NaCl outside of the Chrome web store.
459 command_line->AppendSwitch(switches::kEnableNaCl);
462 // Append the correct mode and testcase string
463 std::string PPAPINaClTestDisallowedSockets::BuildQuery(
464 const std::string& base,
465 const std::string& test_case) {
466 return base::StringPrintf("%smode=nacl_newlib&testcase=%s", base.c_str(),
467 test_case.c_str());
470 void PPAPIBrokerInfoBarTest::SetUpOnMainThread() {
471 // The default content setting for the PPAPI broker is ASK. We purposefully
472 // don't call PPAPITestBase::SetUpOnMainThread() to keep it that way.