Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / chrome / browser / apps / web_view_browsertest.cc
blobababfa90c3e27bb66bb19b00f3b0fde7c89211cc
1 // Copyright 2013 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 "apps/ui/native_app_window.h"
6 #include "base/path_service.h"
7 #include "base/strings/stringprintf.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/app/chrome_command_ids.h"
10 #include "chrome/browser/apps/app_browsertest_util.h"
11 #include "chrome/browser/chrome_content_browser_client.h"
12 #include "chrome/browser/extensions/extension_test_message_listener.h"
13 #include "chrome/browser/prerender/prerender_link_manager.h"
14 #include "chrome/browser/prerender/prerender_link_manager_factory.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h"
17 #include "chrome/browser/renderer_context_menu/render_view_context_menu_test_util.h"
18 #include "chrome/browser/task_manager/task_manager_browsertest_util.h"
19 #include "chrome/browser/ui/browser.h"
20 #include "chrome/browser/ui/browser_dialogs.h"
21 #include "chrome/browser/ui/tabs/tab_strip_model.h"
22 #include "chrome/test/base/ui_test_utils.h"
23 #include "content/public/browser/gpu_data_manager.h"
24 #include "content/public/browser/interstitial_page.h"
25 #include "content/public/browser/interstitial_page_delegate.h"
26 #include "content/public/browser/notification_service.h"
27 #include "content/public/browser/render_process_host.h"
28 #include "content/public/browser/web_contents_delegate.h"
29 #include "content/public/common/content_switches.h"
30 #include "content/public/test/browser_test_utils.h"
31 #include "content/public/test/fake_speech_recognition_manager.h"
32 #include "extensions/common/extension.h"
33 #include "extensions/common/extensions_client.h"
34 #include "net/test/embedded_test_server/embedded_test_server.h"
35 #include "net/test/embedded_test_server/http_request.h"
36 #include "net/test/embedded_test_server/http_response.h"
37 #include "ui/gl/gl_switches.h"
39 #if defined(OS_CHROMEOS)
40 #include "chrome/browser/chromeos/accessibility/accessibility_manager.h"
41 #include "chrome/browser/chromeos/accessibility/speech_monitor.h"
42 #endif
44 // For fine-grained suppression on flaky tests.
45 #if defined(OS_WIN)
46 #include "base/win/windows_version.h"
47 #endif
49 using extensions::MenuItem;
50 using prerender::PrerenderLinkManager;
51 using prerender::PrerenderLinkManagerFactory;
52 using task_manager::browsertest_util::MatchAboutBlankTab;
53 using task_manager::browsertest_util::MatchAnyApp;
54 using task_manager::browsertest_util::MatchAnyBackground;
55 using task_manager::browsertest_util::MatchAnyTab;
56 using task_manager::browsertest_util::MatchAnyWebView;
57 using task_manager::browsertest_util::MatchApp;
58 using task_manager::browsertest_util::MatchBackground;
59 using task_manager::browsertest_util::MatchWebView;
60 using task_manager::browsertest_util::WaitForTaskManagerRows;
61 using ui::MenuModel;
63 namespace {
64 const char kEmptyResponsePath[] = "/close-socket";
65 const char kRedirectResponsePath[] = "/server-redirect";
66 const char kRedirectResponseFullPath[] =
67 "/extensions/platform_apps/web_view/shim/guest_redirect.html";
69 // Platform-specific filename relative to the chrome executable.
70 #if defined(OS_WIN)
71 const wchar_t library_name[] = L"ppapi_tests.dll";
72 #elif defined(OS_MACOSX)
73 const char library_name[] = "ppapi_tests.plugin";
74 #elif defined(OS_POSIX)
75 const char library_name[] = "libppapi_tests.so";
76 #endif
78 class EmptyHttpResponse : public net::test_server::HttpResponse {
79 public:
80 virtual std::string ToResponseString() const OVERRIDE {
81 return std::string();
85 class TestInterstitialPageDelegate : public content::InterstitialPageDelegate {
86 public:
87 TestInterstitialPageDelegate() {
89 virtual ~TestInterstitialPageDelegate() {}
90 virtual std::string GetHTMLContents() OVERRIDE { return std::string(); }
93 // Used to get notified when a guest is created.
94 class GuestContentBrowserClient : public chrome::ChromeContentBrowserClient {
95 public:
96 GuestContentBrowserClient() : web_contents_(NULL) {}
98 content::WebContents* WaitForGuestCreated() {
99 if (web_contents_)
100 return web_contents_;
102 message_loop_runner_ = new content::MessageLoopRunner;
103 message_loop_runner_->Run();
104 return web_contents_;
107 private:
108 // ChromeContentBrowserClient implementation:
109 virtual void GuestWebContentsAttached(
110 content::WebContents* guest_web_contents,
111 content::WebContents* embedder_web_contents,
112 const base::DictionaryValue& extra_params) OVERRIDE {
113 ChromeContentBrowserClient::GuestWebContentsAttached(
114 guest_web_contents, embedder_web_contents, extra_params);
115 web_contents_ = guest_web_contents;
117 if (message_loop_runner_)
118 message_loop_runner_->Quit();
121 content::WebContents* web_contents_;
122 scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
125 class InterstitialObserver : public content::WebContentsObserver {
126 public:
127 InterstitialObserver(content::WebContents* web_contents,
128 const base::Closure& attach_callback,
129 const base::Closure& detach_callback)
130 : WebContentsObserver(web_contents),
131 attach_callback_(attach_callback),
132 detach_callback_(detach_callback) {
135 virtual void DidAttachInterstitialPage() OVERRIDE {
136 attach_callback_.Run();
139 virtual void DidDetachInterstitialPage() OVERRIDE {
140 detach_callback_.Run();
143 private:
144 base::Closure attach_callback_;
145 base::Closure detach_callback_;
147 DISALLOW_COPY_AND_ASSIGN(InterstitialObserver);
150 } // namespace
152 // This class intercepts media access request from the embedder. The request
153 // should be triggered only if the embedder API (from tests) allows the request
154 // in Javascript.
155 // We do not issue the actual media request; the fact that the request reached
156 // embedder's WebContents is good enough for our tests. This is also to make
157 // the test run successfully on trybots.
158 class MockWebContentsDelegate : public content::WebContentsDelegate {
159 public:
160 MockWebContentsDelegate() : requested_(false) {}
161 virtual ~MockWebContentsDelegate() {}
163 virtual void RequestMediaAccessPermission(
164 content::WebContents* web_contents,
165 const content::MediaStreamRequest& request,
166 const content::MediaResponseCallback& callback) OVERRIDE {
167 requested_ = true;
168 if (message_loop_runner_.get())
169 message_loop_runner_->Quit();
172 void WaitForSetMediaPermission() {
173 if (requested_)
174 return;
175 message_loop_runner_ = new content::MessageLoopRunner;
176 message_loop_runner_->Run();
179 private:
180 bool requested_;
181 scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
183 DISALLOW_COPY_AND_ASSIGN(MockWebContentsDelegate);
186 // This class intercepts download request from the guest.
187 class MockDownloadWebContentsDelegate : public content::WebContentsDelegate {
188 public:
189 explicit MockDownloadWebContentsDelegate(
190 content::WebContentsDelegate* orig_delegate)
191 : orig_delegate_(orig_delegate),
192 waiting_for_decision_(false),
193 expect_allow_(false),
194 decision_made_(false),
195 last_download_allowed_(false) {}
196 virtual ~MockDownloadWebContentsDelegate() {}
198 virtual void CanDownload(
199 content::RenderViewHost* render_view_host,
200 int request_id,
201 const std::string& request_method,
202 const base::Callback<void(bool)>& callback) OVERRIDE {
203 orig_delegate_->CanDownload(
204 render_view_host, request_id, request_method,
205 base::Bind(&MockDownloadWebContentsDelegate::DownloadDecided,
206 base::Unretained(this)));
209 void WaitForCanDownload(bool expect_allow) {
210 EXPECT_FALSE(waiting_for_decision_);
211 waiting_for_decision_ = true;
213 if (decision_made_) {
214 EXPECT_EQ(expect_allow, last_download_allowed_);
215 return;
218 expect_allow_ = expect_allow;
219 message_loop_runner_ = new content::MessageLoopRunner;
220 message_loop_runner_->Run();
223 void DownloadDecided(bool allow) {
224 EXPECT_FALSE(decision_made_);
225 decision_made_ = true;
227 if (waiting_for_decision_) {
228 EXPECT_EQ(expect_allow_, allow);
229 if (message_loop_runner_.get())
230 message_loop_runner_->Quit();
231 return;
233 last_download_allowed_ = allow;
236 void Reset() {
237 waiting_for_decision_ = false;
238 decision_made_ = false;
241 private:
242 content::WebContentsDelegate* orig_delegate_;
243 bool waiting_for_decision_;
244 bool expect_allow_;
245 bool decision_made_;
246 bool last_download_allowed_;
247 scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
249 DISALLOW_COPY_AND_ASSIGN(MockDownloadWebContentsDelegate);
252 class WebViewTest : public extensions::PlatformAppBrowserTest {
253 protected:
254 virtual void SetUp() OVERRIDE {
255 if (UsesFakeSpeech()) {
256 // SpeechRecognition test specific SetUp.
257 fake_speech_recognition_manager_.reset(
258 new content::FakeSpeechRecognitionManager());
259 fake_speech_recognition_manager_->set_should_send_fake_response(true);
260 // Inject the fake manager factory so that the test result is returned to
261 // the web page.
262 content::SpeechRecognitionManager::SetManagerForTesting(
263 fake_speech_recognition_manager_.get());
265 extensions::PlatformAppBrowserTest::SetUp();
268 virtual void TearDown() OVERRIDE {
269 if (UsesFakeSpeech()) {
270 // SpeechRecognition test specific TearDown.
271 content::SpeechRecognitionManager::SetManagerForTesting(NULL);
274 extensions::PlatformAppBrowserTest::TearDown();
277 virtual void SetUpOnMainThread() OVERRIDE {
278 extensions::PlatformAppBrowserTest::SetUpOnMainThread();
279 const testing::TestInfo* const test_info =
280 testing::UnitTest::GetInstance()->current_test_info();
281 // Mock out geolocation for geolocation specific tests.
282 if (!strncmp(test_info->name(), "GeolocationAPI",
283 strlen("GeolocationAPI"))) {
284 ui_test_utils::OverrideGeolocation(10, 20);
288 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
289 command_line->AppendSwitch(switches::kUseFakeDeviceForMediaStream);
290 command_line->AppendSwitchASCII(switches::kJavaScriptFlags, "--expose-gc");
292 extensions::PlatformAppBrowserTest::SetUpCommandLine(command_line);
295 // This method is responsible for initializing a packaged app, which contains
296 // multiple webview tags. The tags have different partition identifiers and
297 // their WebContent objects are returned as output. The method also verifies
298 // the expected process allocation and storage partition assignment.
299 // The |navigate_to_url| parameter is used to navigate the main browser
300 // window.
302 // TODO(ajwong): This function is getting to be too large. Either refactor it
303 // so the test can specify a configuration of WebView tags that we will
304 // dynamically inject JS to generate, or move this test wholesale into
305 // something that RunPlatformAppTest() can execute purely in Javascript. This
306 // won't let us do a white-box examination of the StoragePartition equivalence
307 // directly, but we will be able to view the black box effects which is good
308 // enough. http://crbug.com/160361
309 void NavigateAndOpenAppForIsolation(
310 GURL navigate_to_url,
311 content::WebContents** default_tag_contents1,
312 content::WebContents** default_tag_contents2,
313 content::WebContents** named_partition_contents1,
314 content::WebContents** named_partition_contents2,
315 content::WebContents** persistent_partition_contents1,
316 content::WebContents** persistent_partition_contents2,
317 content::WebContents** persistent_partition_contents3) {
318 GURL::Replacements replace_host;
319 std::string host_str("localhost"); // Must stay in scope with replace_host.
320 replace_host.SetHostStr(host_str);
322 navigate_to_url = navigate_to_url.ReplaceComponents(replace_host);
324 GURL tag_url1 = embedded_test_server()->GetURL(
325 "/extensions/platform_apps/web_view/isolation/cookie.html");
326 tag_url1 = tag_url1.ReplaceComponents(replace_host);
327 GURL tag_url2 = embedded_test_server()->GetURL(
328 "/extensions/platform_apps/web_view/isolation/cookie2.html");
329 tag_url2 = tag_url2.ReplaceComponents(replace_host);
330 GURL tag_url3 = embedded_test_server()->GetURL(
331 "/extensions/platform_apps/web_view/isolation/storage1.html");
332 tag_url3 = tag_url3.ReplaceComponents(replace_host);
333 GURL tag_url4 = embedded_test_server()->GetURL(
334 "/extensions/platform_apps/web_view/isolation/storage2.html");
335 tag_url4 = tag_url4.ReplaceComponents(replace_host);
336 GURL tag_url5 = embedded_test_server()->GetURL(
337 "/extensions/platform_apps/web_view/isolation/storage1.html#p1");
338 tag_url5 = tag_url5.ReplaceComponents(replace_host);
339 GURL tag_url6 = embedded_test_server()->GetURL(
340 "/extensions/platform_apps/web_view/isolation/storage1.html#p2");
341 tag_url6 = tag_url6.ReplaceComponents(replace_host);
342 GURL tag_url7 = embedded_test_server()->GetURL(
343 "/extensions/platform_apps/web_view/isolation/storage1.html#p3");
344 tag_url7 = tag_url7.ReplaceComponents(replace_host);
346 ui_test_utils::NavigateToURLWithDisposition(
347 browser(), navigate_to_url, CURRENT_TAB,
348 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
350 ui_test_utils::UrlLoadObserver observer1(
351 tag_url1, content::NotificationService::AllSources());
352 ui_test_utils::UrlLoadObserver observer2(
353 tag_url2, content::NotificationService::AllSources());
354 ui_test_utils::UrlLoadObserver observer3(
355 tag_url3, content::NotificationService::AllSources());
356 ui_test_utils::UrlLoadObserver observer4(
357 tag_url4, content::NotificationService::AllSources());
358 ui_test_utils::UrlLoadObserver observer5(
359 tag_url5, content::NotificationService::AllSources());
360 ui_test_utils::UrlLoadObserver observer6(
361 tag_url6, content::NotificationService::AllSources());
362 ui_test_utils::UrlLoadObserver observer7(
363 tag_url7, content::NotificationService::AllSources());
364 LoadAndLaunchPlatformApp("web_view/isolation");
365 observer1.Wait();
366 observer2.Wait();
367 observer3.Wait();
368 observer4.Wait();
369 observer5.Wait();
370 observer6.Wait();
371 observer7.Wait();
373 content::Source<content::NavigationController> source1 = observer1.source();
374 EXPECT_TRUE(source1->GetWebContents()->GetRenderProcessHost()->IsGuest());
375 content::Source<content::NavigationController> source2 = observer2.source();
376 EXPECT_TRUE(source2->GetWebContents()->GetRenderProcessHost()->IsGuest());
377 content::Source<content::NavigationController> source3 = observer3.source();
378 EXPECT_TRUE(source3->GetWebContents()->GetRenderProcessHost()->IsGuest());
379 content::Source<content::NavigationController> source4 = observer4.source();
380 EXPECT_TRUE(source4->GetWebContents()->GetRenderProcessHost()->IsGuest());
381 content::Source<content::NavigationController> source5 = observer5.source();
382 EXPECT_TRUE(source5->GetWebContents()->GetRenderProcessHost()->IsGuest());
383 content::Source<content::NavigationController> source6 = observer6.source();
384 EXPECT_TRUE(source6->GetWebContents()->GetRenderProcessHost()->IsGuest());
385 content::Source<content::NavigationController> source7 = observer7.source();
386 EXPECT_TRUE(source7->GetWebContents()->GetRenderProcessHost()->IsGuest());
388 // Check that the first two tags use the same process and it is different
389 // than the process used by the other two.
390 EXPECT_EQ(source1->GetWebContents()->GetRenderProcessHost()->GetID(),
391 source2->GetWebContents()->GetRenderProcessHost()->GetID());
392 EXPECT_EQ(source3->GetWebContents()->GetRenderProcessHost()->GetID(),
393 source4->GetWebContents()->GetRenderProcessHost()->GetID());
394 EXPECT_NE(source1->GetWebContents()->GetRenderProcessHost()->GetID(),
395 source3->GetWebContents()->GetRenderProcessHost()->GetID());
397 // The two sets of tags should also be isolated from the main browser.
398 EXPECT_NE(source1->GetWebContents()->GetRenderProcessHost()->GetID(),
399 browser()->tab_strip_model()->GetWebContentsAt(0)->
400 GetRenderProcessHost()->GetID());
401 EXPECT_NE(source3->GetWebContents()->GetRenderProcessHost()->GetID(),
402 browser()->tab_strip_model()->GetWebContentsAt(0)->
403 GetRenderProcessHost()->GetID());
405 // Check that the storage partitions of the first two tags match and are
406 // different than the other two.
407 EXPECT_EQ(
408 source1->GetWebContents()->GetRenderProcessHost()->
409 GetStoragePartition(),
410 source2->GetWebContents()->GetRenderProcessHost()->
411 GetStoragePartition());
412 EXPECT_EQ(
413 source3->GetWebContents()->GetRenderProcessHost()->
414 GetStoragePartition(),
415 source4->GetWebContents()->GetRenderProcessHost()->
416 GetStoragePartition());
417 EXPECT_NE(
418 source1->GetWebContents()->GetRenderProcessHost()->
419 GetStoragePartition(),
420 source3->GetWebContents()->GetRenderProcessHost()->
421 GetStoragePartition());
423 // Ensure the persistent storage partitions are different.
424 EXPECT_EQ(
425 source5->GetWebContents()->GetRenderProcessHost()->
426 GetStoragePartition(),
427 source6->GetWebContents()->GetRenderProcessHost()->
428 GetStoragePartition());
429 EXPECT_NE(
430 source5->GetWebContents()->GetRenderProcessHost()->
431 GetStoragePartition(),
432 source7->GetWebContents()->GetRenderProcessHost()->
433 GetStoragePartition());
434 EXPECT_NE(
435 source1->GetWebContents()->GetRenderProcessHost()->
436 GetStoragePartition(),
437 source5->GetWebContents()->GetRenderProcessHost()->
438 GetStoragePartition());
439 EXPECT_NE(
440 source1->GetWebContents()->GetRenderProcessHost()->
441 GetStoragePartition(),
442 source7->GetWebContents()->GetRenderProcessHost()->
443 GetStoragePartition());
445 *default_tag_contents1 = source1->GetWebContents();
446 *default_tag_contents2 = source2->GetWebContents();
447 *named_partition_contents1 = source3->GetWebContents();
448 *named_partition_contents2 = source4->GetWebContents();
449 if (persistent_partition_contents1) {
450 *persistent_partition_contents1 = source5->GetWebContents();
452 if (persistent_partition_contents2) {
453 *persistent_partition_contents2 = source6->GetWebContents();
455 if (persistent_partition_contents3) {
456 *persistent_partition_contents3 = source7->GetWebContents();
460 void ExecuteScriptWaitForTitle(content::WebContents* web_contents,
461 const char* script,
462 const char* title) {
463 base::string16 expected_title(base::ASCIIToUTF16(title));
464 base::string16 error_title(base::ASCIIToUTF16("error"));
466 content::TitleWatcher title_watcher(web_contents, expected_title);
467 title_watcher.AlsoWaitForTitle(error_title);
468 EXPECT_TRUE(content::ExecuteScript(web_contents, script));
469 EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
472 // Handles |request| by serving a redirect response.
473 static scoped_ptr<net::test_server::HttpResponse> RedirectResponseHandler(
474 const std::string& path,
475 const GURL& redirect_target,
476 const net::test_server::HttpRequest& request) {
477 if (!StartsWithASCII(path, request.relative_url, true))
478 return scoped_ptr<net::test_server::HttpResponse>();
480 scoped_ptr<net::test_server::BasicHttpResponse> http_response(
481 new net::test_server::BasicHttpResponse);
482 http_response->set_code(net::HTTP_MOVED_PERMANENTLY);
483 http_response->AddCustomHeader("Location", redirect_target.spec());
484 return http_response.PassAs<net::test_server::HttpResponse>();
487 // Handles |request| by serving an empty response.
488 static scoped_ptr<net::test_server::HttpResponse> EmptyResponseHandler(
489 const std::string& path,
490 const net::test_server::HttpRequest& request) {
491 if (StartsWithASCII(path, request.relative_url, true)) {
492 return scoped_ptr<net::test_server::HttpResponse>(
493 new EmptyHttpResponse);
496 return scoped_ptr<net::test_server::HttpResponse>();
499 // Shortcut to return the current MenuManager.
500 extensions::MenuManager* menu_manager() {
501 return extensions::MenuManager::Get(browser()->profile());
504 // This gets all the items that any extension has registered for possible
505 // inclusion in context menus.
506 MenuItem::List GetItems() {
507 MenuItem::List result;
508 std::set<MenuItem::ExtensionKey> extension_ids =
509 menu_manager()->ExtensionIds();
510 std::set<MenuItem::ExtensionKey>::iterator i;
511 for (i = extension_ids.begin(); i != extension_ids.end(); ++i) {
512 const MenuItem::List* list = menu_manager()->MenuItems(*i);
513 result.insert(result.end(), list->begin(), list->end());
515 return result;
518 enum TestServer {
519 NEEDS_TEST_SERVER,
520 NO_TEST_SERVER
523 void TestHelper(const std::string& test_name,
524 const std::string& app_location,
525 TestServer test_server) {
526 // For serving guest pages.
527 if (test_server == NEEDS_TEST_SERVER) {
528 if (!StartEmbeddedTestServer()) {
529 LOG(ERROR) << "FAILED TO START TEST SERVER.";
530 return;
532 embedded_test_server()->RegisterRequestHandler(
533 base::Bind(&WebViewTest::RedirectResponseHandler,
534 kRedirectResponsePath,
535 embedded_test_server()->GetURL(kRedirectResponseFullPath)));
537 embedded_test_server()->RegisterRequestHandler(
538 base::Bind(&WebViewTest::EmptyResponseHandler, kEmptyResponsePath));
541 ExtensionTestMessageListener launched_listener("Launched", false);
542 LoadAndLaunchPlatformApp(app_location.c_str());
543 if (!launched_listener.WaitUntilSatisfied()) {
544 LOG(ERROR) << "TEST DID NOT LAUNCH.";
545 return;
548 // Flush any pending events to make sure we start with a clean slate.
549 content::RunAllPendingInMessageLoop();
551 content::WebContents* embedder_web_contents =
552 GetFirstAppWindowWebContents();
553 if (!embedder_web_contents) {
554 LOG(ERROR) << "UNABLE TO FIND EMBEDDER WEB CONTENTS.";
555 return;
558 ExtensionTestMessageListener done_listener("TEST_PASSED", false);
559 done_listener.set_failure_message("TEST_FAILED");
560 if (!content::ExecuteScript(
561 embedder_web_contents,
562 base::StringPrintf("runTest('%s')", test_name.c_str()))) {
563 LOG(ERROR) << "UNABLE TO START TEST.";
564 return;
566 ASSERT_TRUE(done_listener.WaitUntilSatisfied());
569 content::WebContents* LoadGuest(const std::string& guest_path,
570 const std::string& app_path) {
571 GURL::Replacements replace_host;
572 std::string host_str("localhost"); // Must stay in scope with replace_host.
573 replace_host.SetHostStr(host_str);
575 GURL guest_url = embedded_test_server()->GetURL(guest_path);
576 guest_url = guest_url.ReplaceComponents(replace_host);
578 ui_test_utils::UrlLoadObserver guest_observer(
579 guest_url, content::NotificationService::AllSources());
581 ExtensionTestMessageListener guest_loaded_listener("guest-loaded", false);
582 LoadAndLaunchPlatformApp(app_path.c_str());
583 guest_observer.Wait();
585 content::Source<content::NavigationController> source =
586 guest_observer.source();
587 EXPECT_TRUE(source->GetWebContents()->GetRenderProcessHost()->IsGuest());
589 bool satisfied = guest_loaded_listener.WaitUntilSatisfied();
590 if (!satisfied)
591 return NULL;
593 content::WebContents* guest_web_contents = source->GetWebContents();
594 return guest_web_contents;
597 // Runs media_access/allow tests.
598 void MediaAccessAPIAllowTestHelper(const std::string& test_name);
600 // Runs media_access/deny tests, each of them are run separately otherwise
601 // they timeout (mostly on Windows).
602 void MediaAccessAPIDenyTestHelper(const std::string& test_name) {
603 ASSERT_TRUE(StartEmbeddedTestServer()); // For serving guest pages.
604 ExtensionTestMessageListener loaded_listener("loaded", false);
605 LoadAndLaunchPlatformApp("web_view/media_access/deny");
606 ASSERT_TRUE(loaded_listener.WaitUntilSatisfied());
608 content::WebContents* embedder_web_contents =
609 GetFirstAppWindowWebContents();
610 ASSERT_TRUE(embedder_web_contents);
612 ExtensionTestMessageListener test_run_listener("PASSED", false);
613 test_run_listener.set_failure_message("FAILED");
614 EXPECT_TRUE(
615 content::ExecuteScript(
616 embedder_web_contents,
617 base::StringPrintf("startDenyTest('%s')", test_name.c_str())));
618 ASSERT_TRUE(test_run_listener.WaitUntilSatisfied());
621 void WaitForInterstitial(content::WebContents* web_contents) {
622 scoped_refptr<content::MessageLoopRunner> loop_runner(
623 new content::MessageLoopRunner);
624 InterstitialObserver observer(web_contents,
625 loop_runner->QuitClosure(),
626 base::Closure());
627 if (!content::InterstitialPage::GetInterstitialPage(web_contents))
628 loop_runner->Run();
631 private:
632 bool UsesFakeSpeech() {
633 const testing::TestInfo* const test_info =
634 testing::UnitTest::GetInstance()->current_test_info();
636 // SpeechRecognition test specific SetUp.
637 return !strcmp(test_info->name(), "SpeechRecognition") ||
638 !strcmp(test_info->name(),
639 "SpeechRecognitionAPI_HasPermissionAllow");
642 scoped_ptr<content::FakeSpeechRecognitionManager>
643 fake_speech_recognition_manager_;
646 // This test ensures JavaScript errors ("Cannot redefine property") do not
647 // happen when a <webview> is removed from DOM and added back.
648 IN_PROC_BROWSER_TEST_F(WebViewTest,
649 AddRemoveWebView_AddRemoveWebView) {
650 ASSERT_TRUE(StartEmbeddedTestServer()); // For serving guest pages.
651 ASSERT_TRUE(RunPlatformAppTest("platform_apps/web_view/addremove"))
652 << message_;
655 IN_PROC_BROWSER_TEST_F(WebViewTest, AutoSize) {
656 #if defined(OS_WIN)
657 // Flaky on XP bot http://crbug.com/299507
658 if (base::win::GetVersion() <= base::win::VERSION_XP)
659 return;
660 #endif
662 ASSERT_TRUE(RunPlatformAppTest("platform_apps/web_view/autosize"))
663 << message_;
666 // http://crbug.com/326332
667 IN_PROC_BROWSER_TEST_F(WebViewTest, DISABLED_Shim_TestAutosizeAfterNavigation) {
668 TestHelper("testAutosizeAfterNavigation", "web_view/shim", NO_TEST_SERVER);
671 IN_PROC_BROWSER_TEST_F(WebViewTest, Shim_TestAutosizeBeforeNavigation) {
672 TestHelper("testAutosizeBeforeNavigation", "web_view/shim", NO_TEST_SERVER);
674 IN_PROC_BROWSER_TEST_F(WebViewTest, Shim_TestAutosizeRemoveAttributes) {
675 TestHelper("testAutosizeRemoveAttributes", "web_view/shim", NO_TEST_SERVER);
678 // This test is disabled due to being flaky. http://crbug.com/282116
679 #if defined(OS_WIN)
680 #define MAYBE_Shim_TestAutosizeWithPartialAttributes \
681 DISABLED_Shim_TestAutosizeWithPartialAttributes
682 #else
683 #define MAYBE_Shim_TestAutosizeWithPartialAttributes \
684 Shim_TestAutosizeWithPartialAttributes
685 #endif
686 IN_PROC_BROWSER_TEST_F(WebViewTest,
687 MAYBE_Shim_TestAutosizeWithPartialAttributes) {
688 TestHelper("testAutosizeWithPartialAttributes",
689 "web_view/shim",
690 NO_TEST_SERVER);
693 IN_PROC_BROWSER_TEST_F(WebViewTest, Shim_TestAPIMethodExistence) {
694 TestHelper("testAPIMethodExistence", "web_view/shim", NO_TEST_SERVER);
697 // Tests the existence of WebRequest API event objects on the request
698 // object, on the webview element, and hanging directly off webview.
699 IN_PROC_BROWSER_TEST_F(WebViewTest, Shim_TestWebRequestAPIExistence) {
700 TestHelper("testWebRequestAPIExistence", "web_view/shim", NO_TEST_SERVER);
703 // http://crbug.com/315920
704 #if defined(GOOGLE_CHROME_BUILD) && (defined(OS_WIN) || defined(OS_LINUX))
705 #define MAYBE_Shim_TestChromeExtensionURL DISABLED_Shim_TestChromeExtensionURL
706 #else
707 #define MAYBE_Shim_TestChromeExtensionURL Shim_TestChromeExtensionURL
708 #endif
709 IN_PROC_BROWSER_TEST_F(WebViewTest, MAYBE_Shim_TestChromeExtensionURL) {
710 TestHelper("testChromeExtensionURL", "web_view/shim", NO_TEST_SERVER);
713 // http://crbug.com/315920
714 #if defined(GOOGLE_CHROME_BUILD) && (defined(OS_WIN) || defined(OS_LINUX))
715 #define MAYBE_Shim_TestChromeExtensionRelativePath \
716 DISABLED_Shim_TestChromeExtensionRelativePath
717 #else
718 #define MAYBE_Shim_TestChromeExtensionRelativePath \
719 Shim_TestChromeExtensionRelativePath
720 #endif
721 IN_PROC_BROWSER_TEST_F(WebViewTest,
722 MAYBE_Shim_TestChromeExtensionRelativePath) {
723 TestHelper("testChromeExtensionRelativePath",
724 "web_view/shim",
725 NO_TEST_SERVER);
728 IN_PROC_BROWSER_TEST_F(WebViewTest,
729 Shim_TestInlineScriptFromAccessibleResources) {
730 TestHelper("testInlineScriptFromAccessibleResources",
731 "web_view/shim",
732 NO_TEST_SERVER);
735 IN_PROC_BROWSER_TEST_F(WebViewTest, Shim_TestInvalidChromeExtensionURL) {
736 TestHelper("testInvalidChromeExtensionURL", "web_view/shim", NO_TEST_SERVER);
739 IN_PROC_BROWSER_TEST_F(WebViewTest, Shim_TestEventName) {
740 TestHelper("testEventName", "web_view/shim", NO_TEST_SERVER);
743 // WebViewTest.Shim_TestOnEventProperty is flaky, so disable it.
744 // http://crbug.com/359832.
745 IN_PROC_BROWSER_TEST_F(WebViewTest, DISABLED_Shim_TestOnEventProperty) {
746 TestHelper("testOnEventProperties", "web_view/shim", NO_TEST_SERVER);
749 IN_PROC_BROWSER_TEST_F(WebViewTest, Shim_TestLoadProgressEvent) {
750 TestHelper("testLoadProgressEvent", "web_view/shim", NO_TEST_SERVER);
753 // WebViewTest.Shim_TestDestroyOnEventListener is flaky, so disable it.
754 // http://crbug.com/255106
755 IN_PROC_BROWSER_TEST_F(WebViewTest, DISABLED_Shim_TestDestroyOnEventListener) {
756 TestHelper("testDestroyOnEventListener", "web_view/shim", NO_TEST_SERVER);
759 IN_PROC_BROWSER_TEST_F(WebViewTest, Shim_TestCannotMutateEventName) {
760 TestHelper("testCannotMutateEventName", "web_view/shim", NO_TEST_SERVER);
763 // http://crbug.com/267304
764 #if defined(OS_WIN)
765 #define MAYBE_Shim_TestPartitionRaisesException \
766 DISABLED_Shim_TestPartitionRaisesException
767 #else
768 #define MAYBE_Shim_TestPartitionRaisesException \
769 Shim_TestPartitionRaisesException
770 #endif
772 IN_PROC_BROWSER_TEST_F(WebViewTest, MAYBE_Shim_TestPartitionRaisesException) {
773 TestHelper("testPartitionRaisesException",
774 "web_view/shim",
775 NO_TEST_SERVER);
778 IN_PROC_BROWSER_TEST_F(WebViewTest, Shim_TestExecuteScriptFail) {
779 #if defined(OS_WIN)
780 // Flaky on XP bot http://crbug.com/266185
781 if (base::win::GetVersion() <= base::win::VERSION_XP)
782 return;
783 #endif
785 TestHelper("testExecuteScriptFail", "web_view/shim", NEEDS_TEST_SERVER);
788 IN_PROC_BROWSER_TEST_F(WebViewTest, Shim_TestExecuteScript) {
789 TestHelper("testExecuteScript", "web_view/shim", NO_TEST_SERVER);
792 IN_PROC_BROWSER_TEST_F(
793 WebViewTest,
794 Shim_TestExecuteScriptIsAbortedWhenWebViewSourceIsChanged) {
795 TestHelper("testExecuteScriptIsAbortedWhenWebViewSourceIsChanged",
796 "web_view/shim",
797 NO_TEST_SERVER);
800 IN_PROC_BROWSER_TEST_F(WebViewTest, Shim_TestTerminateAfterExit) {
801 TestHelper("testTerminateAfterExit", "web_view/shim", NO_TEST_SERVER);
804 IN_PROC_BROWSER_TEST_F(WebViewTest, Shim_TestAssignSrcAfterCrash) {
805 TestHelper("testAssignSrcAfterCrash", "web_view/shim", NO_TEST_SERVER);
808 IN_PROC_BROWSER_TEST_F(WebViewTest,
809 Shim_TestNavOnConsecutiveSrcAttributeChanges) {
810 TestHelper("testNavOnConsecutiveSrcAttributeChanges",
811 "web_view/shim",
812 NO_TEST_SERVER);
815 IN_PROC_BROWSER_TEST_F(WebViewTest, Shim_TestNavOnSrcAttributeChange) {
816 TestHelper("testNavOnSrcAttributeChange", "web_view/shim", NO_TEST_SERVER);
819 IN_PROC_BROWSER_TEST_F(WebViewTest, Shim_TestRemoveSrcAttribute) {
820 TestHelper("testRemoveSrcAttribute", "web_view/shim", NO_TEST_SERVER);
823 IN_PROC_BROWSER_TEST_F(WebViewTest, Shim_TestReassignSrcAttribute) {
824 TestHelper("testReassignSrcAttribute", "web_view/shim", NO_TEST_SERVER);
827 IN_PROC_BROWSER_TEST_F(WebViewTest, Shim_TestBrowserPluginNotAllowed) {
828 #if defined(OS_WIN)
829 // Flaky on XP bots. http://crbug.com/267300
830 if (base::win::GetVersion() <= base::win::VERSION_XP)
831 return;
832 #endif
834 TestHelper("testBrowserPluginNotAllowed", "web_view/shim", NO_TEST_SERVER);
837 IN_PROC_BROWSER_TEST_F(WebViewTest, Shim_TestNewWindow) {
838 TestHelper("testNewWindow", "web_view/shim", NEEDS_TEST_SERVER);
841 IN_PROC_BROWSER_TEST_F(WebViewTest, Shim_TestNewWindowTwoListeners) {
842 TestHelper("testNewWindowTwoListeners", "web_view/shim", NEEDS_TEST_SERVER);
845 IN_PROC_BROWSER_TEST_F(WebViewTest, Shim_TestNewWindowNoPreventDefault) {
846 TestHelper("testNewWindowNoPreventDefault",
847 "web_view/shim",
848 NEEDS_TEST_SERVER);
851 IN_PROC_BROWSER_TEST_F(WebViewTest, Shim_TestNewWindowNoReferrerLink) {
852 TestHelper("testNewWindowNoReferrerLink", "web_view/shim", NEEDS_TEST_SERVER);
855 IN_PROC_BROWSER_TEST_F(WebViewTest, Shim_TestContentLoadEvent) {
856 TestHelper("testContentLoadEvent", "web_view/shim", NO_TEST_SERVER);
859 // http://crbug.com/326330
860 IN_PROC_BROWSER_TEST_F(WebViewTest,
861 DISABLED_Shim_TestDeclarativeWebRequestAPI) {
862 TestHelper("testDeclarativeWebRequestAPI",
863 "web_view/shim",
864 NEEDS_TEST_SERVER);
867 IN_PROC_BROWSER_TEST_F(WebViewTest, Shim_TestWebRequestAPI) {
868 TestHelper("testWebRequestAPI", "web_view/shim", NEEDS_TEST_SERVER);
871 IN_PROC_BROWSER_TEST_F(WebViewTest, Shim_TestWebRequestAPIGoogleProperty) {
872 TestHelper("testWebRequestAPIGoogleProperty",
873 "web_view/shim",
874 NO_TEST_SERVER);
877 // This test is disabled due to being flaky. http://crbug.com/309451
878 #if defined(OS_WIN)
879 #define MAYBE_Shim_TestWebRequestListenerSurvivesReparenting \
880 DISABLED_Shim_TestWebRequestListenerSurvivesReparenting
881 #else
882 #define MAYBE_Shim_TestWebRequestListenerSurvivesReparenting \
883 Shim_TestWebRequestListenerSurvivesReparenting
884 #endif
885 IN_PROC_BROWSER_TEST_F(
886 WebViewTest,
887 MAYBE_Shim_TestWebRequestListenerSurvivesReparenting) {
888 TestHelper("testWebRequestListenerSurvivesReparenting",
889 "web_view/shim",
890 NEEDS_TEST_SERVER);
893 IN_PROC_BROWSER_TEST_F(WebViewTest, Shim_TestLoadStartLoadRedirect) {
894 TestHelper("testLoadStartLoadRedirect", "web_view/shim", NEEDS_TEST_SERVER);
897 IN_PROC_BROWSER_TEST_F(WebViewTest,
898 Shim_TestLoadAbortChromeExtensionURLWrongPartition) {
899 TestHelper("testLoadAbortChromeExtensionURLWrongPartition",
900 "web_view/shim",
901 NO_TEST_SERVER);
904 IN_PROC_BROWSER_TEST_F(WebViewTest, Shim_TestLoadAbortEmptyResponse) {
905 TestHelper("testLoadAbortEmptyResponse", "web_view/shim", NEEDS_TEST_SERVER);
908 IN_PROC_BROWSER_TEST_F(WebViewTest, Shim_TestLoadAbortIllegalChromeURL) {
909 TestHelper("testLoadAbortIllegalChromeURL",
910 "web_view/shim",
911 NO_TEST_SERVER);
914 IN_PROC_BROWSER_TEST_F(WebViewTest, Shim_TestLoadAbortIllegalFileURL) {
915 TestHelper("testLoadAbortIllegalFileURL", "web_view/shim", NO_TEST_SERVER);
918 IN_PROC_BROWSER_TEST_F(WebViewTest, Shim_TestLoadAbortIllegalJavaScriptURL) {
919 TestHelper("testLoadAbortIllegalJavaScriptURL",
920 "web_view/shim",
921 NO_TEST_SERVER);
924 IN_PROC_BROWSER_TEST_F(WebViewTest, Shim_TestReload) {
925 TestHelper("testReload", "web_view/shim", NEEDS_TEST_SERVER);
928 IN_PROC_BROWSER_TEST_F(WebViewTest, Shim_TestGetProcessId) {
929 TestHelper("testGetProcessId", "web_view/shim", NEEDS_TEST_SERVER);
932 IN_PROC_BROWSER_TEST_F(WebViewTest, Shim_TestRemoveWebviewOnExit) {
933 ASSERT_TRUE(StartEmbeddedTestServer()); // For serving guest pages.
935 // Launch the app and wait until it's ready to load a test.
936 ExtensionTestMessageListener launched_listener("Launched", false);
937 LoadAndLaunchPlatformApp("web_view/shim");
938 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
940 content::WebContents* embedder_web_contents = GetFirstAppWindowWebContents();
941 ASSERT_TRUE(embedder_web_contents);
943 GURL::Replacements replace_host;
944 std::string host_str("localhost"); // Must stay in scope with replace_host.
945 replace_host.SetHostStr(host_str);
947 std::string guest_path(
948 "/extensions/platform_apps/web_view/shim/empty_guest.html");
949 GURL guest_url = embedded_test_server()->GetURL(guest_path);
950 guest_url = guest_url.ReplaceComponents(replace_host);
952 ui_test_utils::UrlLoadObserver guest_observer(
953 guest_url, content::NotificationService::AllSources());
955 // Run the test and wait until the guest WebContents is available and has
956 // finished loading.
957 ExtensionTestMessageListener guest_loaded_listener("guest-loaded", false);
958 EXPECT_TRUE(content::ExecuteScript(
959 embedder_web_contents,
960 "runTest('testRemoveWebviewOnExit')"));
961 guest_observer.Wait();
963 content::Source<content::NavigationController> source =
964 guest_observer.source();
965 EXPECT_TRUE(source->GetWebContents()->GetRenderProcessHost()->IsGuest());
967 ASSERT_TRUE(guest_loaded_listener.WaitUntilSatisfied());
969 content::WebContentsDestroyedWatcher destroyed_watcher(
970 source->GetWebContents());
972 // Tell the embedder to kill the guest.
973 EXPECT_TRUE(content::ExecuteScript(
974 embedder_web_contents,
975 "removeWebviewOnExitDoCrash();"));
977 // Wait until the guest WebContents is destroyed.
978 destroyed_watcher.Wait();
981 // Remove <webview> immediately after navigating it.
982 // This is a regression test for http://crbug.com/276023.
983 IN_PROC_BROWSER_TEST_F(WebViewTest, Shim_TestRemoveWebviewAfterNavigation) {
984 TestHelper("testRemoveWebviewAfterNavigation",
985 "web_view/shim",
986 NO_TEST_SERVER);
989 IN_PROC_BROWSER_TEST_F(WebViewTest, Shim_TestNavigationToExternalProtocol) {
990 TestHelper("testNavigationToExternalProtocol",
991 "web_view/shim",
992 NO_TEST_SERVER);
995 IN_PROC_BROWSER_TEST_F(WebViewTest, Shim_TestResizeWebviewResizesContent) {
996 TestHelper("testResizeWebviewResizesContent",
997 "web_view/shim",
998 NO_TEST_SERVER);
1001 // This test makes sure we do not crash if app is closed while interstitial
1002 // page is being shown in guest.
1003 // Disabled under LeakSanitizer due to memory leaks. http://crbug.com/321662
1004 #if defined(LEAK_SANITIZER)
1005 #define MAYBE_InterstitialTeardown DISABLED_InterstitialTeardown
1006 #else
1007 #define MAYBE_InterstitialTeardown InterstitialTeardown
1008 #endif
1009 IN_PROC_BROWSER_TEST_F(WebViewTest, MAYBE_InterstitialTeardown) {
1010 #if defined(OS_WIN)
1011 // Flaky on XP bot http://crbug.com/297014
1012 if (base::win::GetVersion() <= base::win::VERSION_XP)
1013 return;
1014 #endif
1016 // Start a HTTPS server so we can load an interstitial page inside guest.
1017 net::SpawnedTestServer::SSLOptions ssl_options;
1018 ssl_options.server_certificate =
1019 net::SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME;
1020 net::SpawnedTestServer https_server(
1021 net::SpawnedTestServer::TYPE_HTTPS, ssl_options,
1022 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
1023 ASSERT_TRUE(https_server.Start());
1025 net::HostPortPair host_and_port = https_server.host_port_pair();
1027 ExtensionTestMessageListener embedder_loaded_listener("EmbedderLoaded",
1028 false);
1029 LoadAndLaunchPlatformApp("web_view/interstitial_teardown");
1030 ASSERT_TRUE(embedder_loaded_listener.WaitUntilSatisfied());
1032 GuestContentBrowserClient new_client;
1033 content::ContentBrowserClient* old_client =
1034 SetBrowserClientForTesting(&new_client);
1036 // Now load the guest.
1037 content::WebContents* embedder_web_contents = GetFirstAppWindowWebContents();
1038 ExtensionTestMessageListener second("GuestAddedToDom", false);
1039 EXPECT_TRUE(content::ExecuteScript(
1040 embedder_web_contents,
1041 base::StringPrintf("loadGuest(%d);\n", host_and_port.port())));
1042 ASSERT_TRUE(second.WaitUntilSatisfied());
1044 // Wait for interstitial page to be shown in guest.
1045 content::WebContents* guest_web_contents = new_client.WaitForGuestCreated();
1046 SetBrowserClientForTesting(old_client);
1047 ASSERT_TRUE(guest_web_contents->GetRenderProcessHost()->IsGuest());
1048 WaitForInterstitial(guest_web_contents);
1050 // Now close the app while interstitial page being shown in guest.
1051 apps::AppWindow* window = GetFirstAppWindow();
1052 window->GetBaseWindow()->Close();
1055 IN_PROC_BROWSER_TEST_F(WebViewTest, ShimSrcAttribute) {
1056 ASSERT_TRUE(RunPlatformAppTest("platform_apps/web_view/src_attribute"))
1057 << message_;
1060 // This test verifies that prerendering has been disabled inside <webview>.
1061 // This test is here rather than in PrerenderBrowserTest for testing convenience
1062 // only. If it breaks then this is a bug in the prerenderer.
1063 IN_PROC_BROWSER_TEST_F(WebViewTest, NoPrerenderer) {
1064 ASSERT_TRUE(StartEmbeddedTestServer());
1065 content::WebContents* guest_web_contents =
1066 LoadGuest(
1067 "/extensions/platform_apps/web_view/noprerenderer/guest.html",
1068 "web_view/noprerenderer");
1069 ASSERT_TRUE(guest_web_contents != NULL);
1071 PrerenderLinkManager* prerender_link_manager =
1072 PrerenderLinkManagerFactory::GetForProfile(
1073 Profile::FromBrowserContext(guest_web_contents->GetBrowserContext()));
1074 ASSERT_TRUE(prerender_link_manager != NULL);
1075 EXPECT_TRUE(prerender_link_manager->IsEmpty());
1078 // Verify that existing <webview>'s are detected when the task manager starts
1079 // up.
1080 IN_PROC_BROWSER_TEST_F(WebViewTest, TaskManagerExistingWebView) {
1081 ASSERT_TRUE(StartEmbeddedTestServer());
1083 LoadGuest("/extensions/platform_apps/web_view/task_manager/guest.html",
1084 "web_view/task_manager");
1086 chrome::ShowTaskManager(browser()); // Show task manager AFTER guest loads.
1088 const char* guest_title = "WebViewed test content";
1089 const char* app_name = "<webview> task manager test";
1090 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchWebView(guest_title)));
1091 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAboutBlankTab()));
1092 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchApp(app_name)));
1093 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchBackground(app_name)));
1095 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyWebView()));
1096 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyTab()));
1097 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyApp()));
1098 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyBackground()));
1101 // Verify that the task manager notices the creation of new <webview>'s.
1102 IN_PROC_BROWSER_TEST_F(WebViewTest, TaskManagerNewWebView) {
1103 ASSERT_TRUE(StartEmbeddedTestServer());
1105 chrome::ShowTaskManager(browser()); // Show task manager BEFORE guest loads.
1107 LoadGuest("/extensions/platform_apps/web_view/task_manager/guest.html",
1108 "web_view/task_manager");
1110 const char* guest_title = "WebViewed test content";
1111 const char* app_name = "<webview> task manager test";
1112 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchWebView(guest_title)));
1113 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAboutBlankTab()));
1114 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchApp(app_name)));
1115 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchBackground(app_name)));
1117 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyWebView()));
1118 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyTab()));
1119 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyApp()));
1120 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyBackground()));
1123 // This tests cookie isolation for packaged apps with webview tags. It navigates
1124 // the main browser window to a page that sets a cookie and loads an app with
1125 // multiple webview tags. Each tag sets a cookie and the test checks the proper
1126 // storage isolation is enforced.
1127 // This test is disabled due to being flaky. http://crbug.com/294196
1128 #if defined(OS_WIN)
1129 #define MAYBE_CookieIsolation DISABLED_CookieIsolation
1130 #else
1131 #define MAYBE_CookieIsolation CookieIsolation
1132 #endif
1133 IN_PROC_BROWSER_TEST_F(WebViewTest, MAYBE_CookieIsolation) {
1134 ASSERT_TRUE(StartEmbeddedTestServer());
1135 const std::string kExpire =
1136 "var expire = new Date(Date.now() + 24 * 60 * 60 * 1000);";
1137 std::string cookie_script1(kExpire);
1138 cookie_script1.append(
1139 "document.cookie = 'guest1=true; path=/; expires=' + expire + ';';");
1140 std::string cookie_script2(kExpire);
1141 cookie_script2.append(
1142 "document.cookie = 'guest2=true; path=/; expires=' + expire + ';';");
1144 GURL::Replacements replace_host;
1145 std::string host_str("localhost"); // Must stay in scope with replace_host.
1146 replace_host.SetHostStr(host_str);
1148 GURL set_cookie_url = embedded_test_server()->GetURL(
1149 "/extensions/platform_apps/isolation/set_cookie.html");
1150 set_cookie_url = set_cookie_url.ReplaceComponents(replace_host);
1152 // The first two partitions will be used to set cookies and ensure they are
1153 // shared. The named partition is used to ensure that cookies are isolated
1154 // between partitions within the same app.
1155 content::WebContents* cookie_contents1;
1156 content::WebContents* cookie_contents2;
1157 content::WebContents* named_partition_contents1;
1158 content::WebContents* named_partition_contents2;
1160 NavigateAndOpenAppForIsolation(set_cookie_url, &cookie_contents1,
1161 &cookie_contents2, &named_partition_contents1,
1162 &named_partition_contents2, NULL, NULL, NULL);
1164 EXPECT_TRUE(content::ExecuteScript(cookie_contents1, cookie_script1));
1165 EXPECT_TRUE(content::ExecuteScript(cookie_contents2, cookie_script2));
1167 int cookie_size;
1168 std::string cookie_value;
1170 // Test the regular browser context to ensure we have only one cookie.
1171 ui_test_utils::GetCookies(GURL("http://localhost"),
1172 browser()->tab_strip_model()->GetWebContentsAt(0),
1173 &cookie_size, &cookie_value);
1174 EXPECT_EQ("testCookie=1", cookie_value);
1176 // The default behavior is to combine webview tags with no explicit partition
1177 // declaration into the same in-memory partition. Test the webview tags to
1178 // ensure we have properly set the cookies and we have both cookies in both
1179 // tags.
1180 ui_test_utils::GetCookies(GURL("http://localhost"),
1181 cookie_contents1,
1182 &cookie_size, &cookie_value);
1183 EXPECT_EQ("guest1=true; guest2=true", cookie_value);
1185 ui_test_utils::GetCookies(GURL("http://localhost"),
1186 cookie_contents2,
1187 &cookie_size, &cookie_value);
1188 EXPECT_EQ("guest1=true; guest2=true", cookie_value);
1190 // The third tag should not have any cookies as it is in a separate partition.
1191 ui_test_utils::GetCookies(GURL("http://localhost"),
1192 named_partition_contents1,
1193 &cookie_size, &cookie_value);
1194 EXPECT_EQ("", cookie_value);
1197 // This tests that in-memory storage partitions are reset on browser restart,
1198 // but persistent ones maintain state for cookies and HTML5 storage.
1199 IN_PROC_BROWSER_TEST_F(WebViewTest, PRE_StoragePersistence) {
1200 ASSERT_TRUE(StartEmbeddedTestServer());
1201 const std::string kExpire =
1202 "var expire = new Date(Date.now() + 24 * 60 * 60 * 1000);";
1203 std::string cookie_script1(kExpire);
1204 cookie_script1.append(
1205 "document.cookie = 'inmemory=true; path=/; expires=' + expire + ';';");
1206 std::string cookie_script2(kExpire);
1207 cookie_script2.append(
1208 "document.cookie = 'persist1=true; path=/; expires=' + expire + ';';");
1209 std::string cookie_script3(kExpire);
1210 cookie_script3.append(
1211 "document.cookie = 'persist2=true; path=/; expires=' + expire + ';';");
1213 // We don't care where the main browser is on this test.
1214 GURL blank_url("about:blank");
1216 // The first two partitions will be used to set cookies and ensure they are
1217 // shared. The named partition is used to ensure that cookies are isolated
1218 // between partitions within the same app.
1219 content::WebContents* cookie_contents1;
1220 content::WebContents* cookie_contents2;
1221 content::WebContents* named_partition_contents1;
1222 content::WebContents* named_partition_contents2;
1223 content::WebContents* persistent_partition_contents1;
1224 content::WebContents* persistent_partition_contents2;
1225 content::WebContents* persistent_partition_contents3;
1226 NavigateAndOpenAppForIsolation(blank_url, &cookie_contents1,
1227 &cookie_contents2, &named_partition_contents1,
1228 &named_partition_contents2,
1229 &persistent_partition_contents1,
1230 &persistent_partition_contents2,
1231 &persistent_partition_contents3);
1233 // Set the inmemory=true cookie for tags with inmemory partitions.
1234 EXPECT_TRUE(content::ExecuteScript(cookie_contents1, cookie_script1));
1235 EXPECT_TRUE(content::ExecuteScript(named_partition_contents1,
1236 cookie_script1));
1238 // For the two different persistent storage partitions, set the
1239 // two different cookies so we can check that they aren't comingled below.
1240 EXPECT_TRUE(content::ExecuteScript(persistent_partition_contents1,
1241 cookie_script2));
1243 EXPECT_TRUE(content::ExecuteScript(persistent_partition_contents3,
1244 cookie_script3));
1246 int cookie_size;
1247 std::string cookie_value;
1249 // Check that all in-memory partitions have a cookie set.
1250 ui_test_utils::GetCookies(GURL("http://localhost"),
1251 cookie_contents1,
1252 &cookie_size, &cookie_value);
1253 EXPECT_EQ("inmemory=true", cookie_value);
1254 ui_test_utils::GetCookies(GURL("http://localhost"),
1255 cookie_contents2,
1256 &cookie_size, &cookie_value);
1257 EXPECT_EQ("inmemory=true", cookie_value);
1258 ui_test_utils::GetCookies(GURL("http://localhost"),
1259 named_partition_contents1,
1260 &cookie_size, &cookie_value);
1261 EXPECT_EQ("inmemory=true", cookie_value);
1262 ui_test_utils::GetCookies(GURL("http://localhost"),
1263 named_partition_contents2,
1264 &cookie_size, &cookie_value);
1265 EXPECT_EQ("inmemory=true", cookie_value);
1267 // Check that all persistent partitions kept their state.
1268 ui_test_utils::GetCookies(GURL("http://localhost"),
1269 persistent_partition_contents1,
1270 &cookie_size, &cookie_value);
1271 EXPECT_EQ("persist1=true", cookie_value);
1272 ui_test_utils::GetCookies(GURL("http://localhost"),
1273 persistent_partition_contents2,
1274 &cookie_size, &cookie_value);
1275 EXPECT_EQ("persist1=true", cookie_value);
1276 ui_test_utils::GetCookies(GURL("http://localhost"),
1277 persistent_partition_contents3,
1278 &cookie_size, &cookie_value);
1279 EXPECT_EQ("persist2=true", cookie_value);
1282 // This is the post-reset portion of the StoragePersistence test. See
1283 // PRE_StoragePersistence for main comment.
1284 IN_PROC_BROWSER_TEST_F(WebViewTest, DISABLED_StoragePersistence) {
1285 ASSERT_TRUE(StartEmbeddedTestServer());
1287 // We don't care where the main browser is on this test.
1288 GURL blank_url("about:blank");
1290 // The first two partitions will be used to set cookies and ensure they are
1291 // shared. The named partition is used to ensure that cookies are isolated
1292 // between partitions within the same app.
1293 content::WebContents* cookie_contents1;
1294 content::WebContents* cookie_contents2;
1295 content::WebContents* named_partition_contents1;
1296 content::WebContents* named_partition_contents2;
1297 content::WebContents* persistent_partition_contents1;
1298 content::WebContents* persistent_partition_contents2;
1299 content::WebContents* persistent_partition_contents3;
1300 NavigateAndOpenAppForIsolation(blank_url, &cookie_contents1,
1301 &cookie_contents2, &named_partition_contents1,
1302 &named_partition_contents2,
1303 &persistent_partition_contents1,
1304 &persistent_partition_contents2,
1305 &persistent_partition_contents3);
1307 int cookie_size;
1308 std::string cookie_value;
1310 // Check that all in-memory partitions lost their state.
1311 ui_test_utils::GetCookies(GURL("http://localhost"),
1312 cookie_contents1,
1313 &cookie_size, &cookie_value);
1314 EXPECT_EQ("", cookie_value);
1315 ui_test_utils::GetCookies(GURL("http://localhost"),
1316 cookie_contents2,
1317 &cookie_size, &cookie_value);
1318 EXPECT_EQ("", cookie_value);
1319 ui_test_utils::GetCookies(GURL("http://localhost"),
1320 named_partition_contents1,
1321 &cookie_size, &cookie_value);
1322 EXPECT_EQ("", cookie_value);
1323 ui_test_utils::GetCookies(GURL("http://localhost"),
1324 named_partition_contents2,
1325 &cookie_size, &cookie_value);
1326 EXPECT_EQ("", cookie_value);
1328 // Check that all persistent partitions kept their state.
1329 ui_test_utils::GetCookies(GURL("http://localhost"),
1330 persistent_partition_contents1,
1331 &cookie_size, &cookie_value);
1332 EXPECT_EQ("persist1=true", cookie_value);
1333 ui_test_utils::GetCookies(GURL("http://localhost"),
1334 persistent_partition_contents2,
1335 &cookie_size, &cookie_value);
1336 EXPECT_EQ("persist1=true", cookie_value);
1337 ui_test_utils::GetCookies(GURL("http://localhost"),
1338 persistent_partition_contents3,
1339 &cookie_size, &cookie_value);
1340 EXPECT_EQ("persist2=true", cookie_value);
1343 #if defined(OS_WIN)
1344 // This test is very flaky on Win Aura, Win XP, Win 7. http://crbug.com/248873
1345 #define MAYBE_DOMStorageIsolation DISABLED_DOMStorageIsolation
1346 #else
1347 #define MAYBE_DOMStorageIsolation DOMStorageIsolation
1348 #endif
1350 // This tests DOM storage isolation for packaged apps with webview tags. It
1351 // loads an app with multiple webview tags and each tag sets DOM storage
1352 // entries, which the test checks to ensure proper storage isolation is
1353 // enforced.
1354 IN_PROC_BROWSER_TEST_F(WebViewTest, MAYBE_DOMStorageIsolation) {
1355 ASSERT_TRUE(StartEmbeddedTestServer());
1356 GURL regular_url = embedded_test_server()->GetURL("/title1.html");
1358 std::string output;
1359 std::string get_local_storage("window.domAutomationController.send("
1360 "window.localStorage.getItem('foo') || 'badval')");
1361 std::string get_session_storage("window.domAutomationController.send("
1362 "window.sessionStorage.getItem('bar') || 'badval')");
1364 content::WebContents* default_tag_contents1;
1365 content::WebContents* default_tag_contents2;
1366 content::WebContents* storage_contents1;
1367 content::WebContents* storage_contents2;
1369 NavigateAndOpenAppForIsolation(regular_url, &default_tag_contents1,
1370 &default_tag_contents2, &storage_contents1,
1371 &storage_contents2, NULL, NULL, NULL);
1373 // Initialize the storage for the first of the two tags that share a storage
1374 // partition.
1375 EXPECT_TRUE(content::ExecuteScript(storage_contents1,
1376 "initDomStorage('page1')"));
1378 // Let's test that the expected values are present in the first tag, as they
1379 // will be overwritten once we call the initDomStorage on the second tag.
1380 EXPECT_TRUE(ExecuteScriptAndExtractString(storage_contents1,
1381 get_local_storage.c_str(),
1382 &output));
1383 EXPECT_STREQ("local-page1", output.c_str());
1384 EXPECT_TRUE(ExecuteScriptAndExtractString(storage_contents1,
1385 get_session_storage.c_str(),
1386 &output));
1387 EXPECT_STREQ("session-page1", output.c_str());
1389 // Now, init the storage in the second tag in the same storage partition,
1390 // which will overwrite the shared localStorage.
1391 EXPECT_TRUE(content::ExecuteScript(storage_contents2,
1392 "initDomStorage('page2')"));
1394 // The localStorage value now should reflect the one written through the
1395 // second tag.
1396 EXPECT_TRUE(ExecuteScriptAndExtractString(storage_contents1,
1397 get_local_storage.c_str(),
1398 &output));
1399 EXPECT_STREQ("local-page2", output.c_str());
1400 EXPECT_TRUE(ExecuteScriptAndExtractString(storage_contents2,
1401 get_local_storage.c_str(),
1402 &output));
1403 EXPECT_STREQ("local-page2", output.c_str());
1405 // Session storage is not shared though, as each webview tag has separate
1406 // instance, even if they are in the same storage partition.
1407 EXPECT_TRUE(ExecuteScriptAndExtractString(storage_contents1,
1408 get_session_storage.c_str(),
1409 &output));
1410 EXPECT_STREQ("session-page1", output.c_str());
1411 EXPECT_TRUE(ExecuteScriptAndExtractString(storage_contents2,
1412 get_session_storage.c_str(),
1413 &output));
1414 EXPECT_STREQ("session-page2", output.c_str());
1416 // Also, let's check that the main browser and another tag that doesn't share
1417 // the same partition don't have those values stored.
1418 EXPECT_TRUE(ExecuteScriptAndExtractString(
1419 browser()->tab_strip_model()->GetWebContentsAt(0),
1420 get_local_storage.c_str(),
1421 &output));
1422 EXPECT_STREQ("badval", output.c_str());
1423 EXPECT_TRUE(ExecuteScriptAndExtractString(
1424 browser()->tab_strip_model()->GetWebContentsAt(0),
1425 get_session_storage.c_str(),
1426 &output));
1427 EXPECT_STREQ("badval", output.c_str());
1428 EXPECT_TRUE(ExecuteScriptAndExtractString(default_tag_contents1,
1429 get_local_storage.c_str(),
1430 &output));
1431 EXPECT_STREQ("badval", output.c_str());
1432 EXPECT_TRUE(ExecuteScriptAndExtractString(default_tag_contents1,
1433 get_session_storage.c_str(),
1434 &output));
1435 EXPECT_STREQ("badval", output.c_str());
1438 // See crbug.com/248500
1439 #if defined(OS_WIN)
1440 #define MAYBE_IndexedDBIsolation DISABLED_IndexedDBIsolation
1441 #else
1442 #define MAYBE_IndexedDBIsolation IndexedDBIsolation
1443 #endif
1445 // This tests IndexedDB isolation for packaged apps with webview tags. It loads
1446 // an app with multiple webview tags and each tag creates an IndexedDB record,
1447 // which the test checks to ensure proper storage isolation is enforced.
1448 IN_PROC_BROWSER_TEST_F(WebViewTest, MAYBE_IndexedDBIsolation) {
1449 ASSERT_TRUE(StartEmbeddedTestServer());
1450 GURL regular_url = embedded_test_server()->GetURL("/title1.html");
1452 content::WebContents* default_tag_contents1;
1453 content::WebContents* default_tag_contents2;
1454 content::WebContents* storage_contents1;
1455 content::WebContents* storage_contents2;
1457 NavigateAndOpenAppForIsolation(regular_url, &default_tag_contents1,
1458 &default_tag_contents2, &storage_contents1,
1459 &storage_contents2, NULL, NULL, NULL);
1461 // Initialize the storage for the first of the two tags that share a storage
1462 // partition.
1463 ExecuteScriptWaitForTitle(storage_contents1, "initIDB()", "idb created");
1464 ExecuteScriptWaitForTitle(storage_contents1, "addItemIDB(7, 'page1')",
1465 "addItemIDB complete");
1466 ExecuteScriptWaitForTitle(storage_contents1, "readItemIDB(7)",
1467 "readItemIDB complete");
1469 std::string output;
1470 std::string get_value(
1471 "window.domAutomationController.send(getValueIDB() || 'badval')");
1473 EXPECT_TRUE(ExecuteScriptAndExtractString(storage_contents1,
1474 get_value.c_str(), &output));
1475 EXPECT_STREQ("page1", output.c_str());
1477 // Initialize the db in the second tag.
1478 ExecuteScriptWaitForTitle(storage_contents2, "initIDB()", "idb open");
1480 // Since we share a partition, reading the value should return the existing
1481 // one.
1482 ExecuteScriptWaitForTitle(storage_contents2, "readItemIDB(7)",
1483 "readItemIDB complete");
1484 EXPECT_TRUE(ExecuteScriptAndExtractString(storage_contents2,
1485 get_value.c_str(), &output));
1486 EXPECT_STREQ("page1", output.c_str());
1488 // Now write through the second tag and read it back.
1489 ExecuteScriptWaitForTitle(storage_contents2, "addItemIDB(7, 'page2')",
1490 "addItemIDB complete");
1491 ExecuteScriptWaitForTitle(storage_contents2, "readItemIDB(7)",
1492 "readItemIDB complete");
1493 EXPECT_TRUE(ExecuteScriptAndExtractString(storage_contents2,
1494 get_value.c_str(), &output));
1495 EXPECT_STREQ("page2", output.c_str());
1497 // Reset the document title, otherwise the next call will not see a change and
1498 // will hang waiting for it.
1499 EXPECT_TRUE(content::ExecuteScript(storage_contents1,
1500 "document.title = 'foo'"));
1502 // Read through the first tag to ensure we have the second value.
1503 ExecuteScriptWaitForTitle(storage_contents1, "readItemIDB(7)",
1504 "readItemIDB complete");
1505 EXPECT_TRUE(ExecuteScriptAndExtractString(storage_contents1,
1506 get_value.c_str(), &output));
1507 EXPECT_STREQ("page2", output.c_str());
1509 // Now, let's confirm there is no database in the main browser and another
1510 // tag that doesn't share the same partition. Due to the IndexedDB API design,
1511 // open will succeed, but the version will be 1, since it creates the database
1512 // if it is not found. The two tags use database version 3, so we avoid
1513 // ambiguity.
1514 const char* script =
1515 "indexedDB.open('isolation').onsuccess = function(e) {"
1516 " if (e.target.result.version == 1)"
1517 " document.title = 'db not found';"
1518 " else "
1519 " document.title = 'error';"
1520 "}";
1521 ExecuteScriptWaitForTitle(browser()->tab_strip_model()->GetWebContentsAt(0),
1522 script, "db not found");
1523 ExecuteScriptWaitForTitle(default_tag_contents1, script, "db not found");
1526 // This test ensures that closing app window on 'loadcommit' does not crash.
1527 // The test launches an app with guest and closes the window on loadcommit. It
1528 // then launches the app window again. The process is repeated 3 times.
1529 // http://crbug.com/291278
1530 #if defined(OS_WIN)
1531 #define MAYBE_CloseOnLoadcommit DISABLED_CloseOnLoadcommit
1532 #else
1533 #define MAYBE_CloseOnLoadcommit CloseOnLoadcommit
1534 #endif
1535 IN_PROC_BROWSER_TEST_F(WebViewTest, MAYBE_CloseOnLoadcommit) {
1536 ExtensionTestMessageListener done_test_listener(
1537 "done-close-on-loadcommit", false);
1538 LoadAndLaunchPlatformApp("web_view/close_on_loadcommit");
1539 ASSERT_TRUE(done_test_listener.WaitUntilSatisfied());
1542 IN_PROC_BROWSER_TEST_F(WebViewTest, MediaAccessAPIDeny_TestDeny) {
1543 MediaAccessAPIDenyTestHelper("testDeny");
1546 IN_PROC_BROWSER_TEST_F(WebViewTest,
1547 MediaAccessAPIDeny_TestDenyThenAllowThrows) {
1548 MediaAccessAPIDenyTestHelper("testDenyThenAllowThrows");
1552 IN_PROC_BROWSER_TEST_F(WebViewTest,
1553 MediaAccessAPIDeny_TestDenyWithPreventDefault) {
1554 MediaAccessAPIDenyTestHelper("testDenyWithPreventDefault");
1557 IN_PROC_BROWSER_TEST_F(WebViewTest,
1558 MediaAccessAPIDeny_TestNoListenersImplyDeny) {
1559 MediaAccessAPIDenyTestHelper("testNoListenersImplyDeny");
1562 IN_PROC_BROWSER_TEST_F(WebViewTest,
1563 MediaAccessAPIDeny_TestNoPreventDefaultImpliesDeny) {
1564 MediaAccessAPIDenyTestHelper("testNoPreventDefaultImpliesDeny");
1567 void WebViewTest::MediaAccessAPIAllowTestHelper(const std::string& test_name) {
1568 ASSERT_TRUE(StartEmbeddedTestServer()); // For serving guest pages.
1569 ExtensionTestMessageListener launched_listener("Launched", false);
1570 LoadAndLaunchPlatformApp("web_view/media_access/allow");
1571 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
1573 content::WebContents* embedder_web_contents = GetFirstAppWindowWebContents();
1574 ASSERT_TRUE(embedder_web_contents);
1575 scoped_ptr<MockWebContentsDelegate> mock(new MockWebContentsDelegate());
1576 embedder_web_contents->SetDelegate(mock.get());
1578 ExtensionTestMessageListener done_listener("TEST_PASSED", false);
1579 done_listener.set_failure_message("TEST_FAILED");
1580 EXPECT_TRUE(
1581 content::ExecuteScript(
1582 embedder_web_contents,
1583 base::StringPrintf("startAllowTest('%s')",
1584 test_name.c_str())));
1585 ASSERT_TRUE(done_listener.WaitUntilSatisfied());
1587 mock->WaitForSetMediaPermission();
1590 IN_PROC_BROWSER_TEST_F(WebViewTest, ContextMenusAPI_Basic) {
1591 GuestContentBrowserClient new_client;
1592 content::ContentBrowserClient* old_client =
1593 SetBrowserClientForTesting(&new_client);
1595 ExtensionTestMessageListener launched_listener("Launched", false);
1596 launched_listener.set_failure_message("TEST_FAILED");
1597 LoadAndLaunchPlatformApp("web_view/context_menus/basic");
1598 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
1600 content::WebContents* guest_web_contents = new_client.WaitForGuestCreated();
1601 ASSERT_TRUE(guest_web_contents);
1602 SetBrowserClientForTesting(old_client);
1604 content::WebContents* embedder = GetFirstAppWindowWebContents();
1605 ASSERT_TRUE(embedder);
1607 // 1. Basic property test.
1608 ExecuteScriptWaitForTitle(embedder, "checkProperties()", "ITEM_CHECKED");
1610 // 2. Create a menu item and wait for created callback to be called.
1611 ExecuteScriptWaitForTitle(embedder, "createMenuItem()", "ITEM_CREATED");
1613 // 3. Click the created item, wait for the click handlers to fire from JS.
1614 ExtensionTestMessageListener click_listener("ITEM_CLICKED", false);
1615 GURL page_url("http://www.google.com");
1616 // Create and build our test context menu.
1617 scoped_ptr<TestRenderViewContextMenu> menu(TestRenderViewContextMenu::Create(
1618 guest_web_contents, page_url, GURL(), GURL()));
1620 // Look for the extension item in the menu, and execute it.
1621 int command_id = IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST;
1622 ASSERT_TRUE(menu->IsCommandIdEnabled(command_id));
1623 menu->ExecuteCommand(command_id, 0);
1625 // Wait for embedder's script to tell us its onclick fired, it does
1626 // chrome.test.sendMessage('ITEM_CLICKED')
1627 ASSERT_TRUE(click_listener.WaitUntilSatisfied());
1629 // 4. Update the item's title and verify.
1630 ExecuteScriptWaitForTitle(embedder, "updateMenuItem()", "ITEM_UPDATED");
1631 MenuItem::List items = GetItems();
1632 ASSERT_EQ(1u, items.size());
1633 MenuItem* item = items.at(0);
1634 EXPECT_EQ("new_title", item->title());
1636 // 5. Remove the item.
1637 ExecuteScriptWaitForTitle(embedder, "removeItem()", "ITEM_REMOVED");
1638 MenuItem::List items_after_removal = GetItems();
1639 ASSERT_EQ(0u, items_after_removal.size());
1641 // 6. Add some more items.
1642 ExecuteScriptWaitForTitle(
1643 embedder, "createThreeMenuItems()", "ITEM_MULTIPLE_CREATED");
1644 MenuItem::List items_after_insertion = GetItems();
1645 ASSERT_EQ(3u, items_after_insertion.size());
1647 // 7. Test removeAll().
1648 ExecuteScriptWaitForTitle(embedder, "removeAllItems()", "ITEM_ALL_REMOVED");
1649 MenuItem::List items_after_all_removal = GetItems();
1650 ASSERT_EQ(0u, items_after_all_removal.size());
1653 IN_PROC_BROWSER_TEST_F(WebViewTest, MediaAccessAPIAllow_TestAllow) {
1654 MediaAccessAPIAllowTestHelper("testAllow");
1657 IN_PROC_BROWSER_TEST_F(WebViewTest, MediaAccessAPIAllow_TestAllowAndThenDeny) {
1658 MediaAccessAPIAllowTestHelper("testAllowAndThenDeny");
1661 IN_PROC_BROWSER_TEST_F(WebViewTest, MediaAccessAPIAllow_TestAllowTwice) {
1662 MediaAccessAPIAllowTestHelper("testAllowTwice");
1665 IN_PROC_BROWSER_TEST_F(WebViewTest, MediaAccessAPIAllow_TestAllowAsync) {
1666 MediaAccessAPIAllowTestHelper("testAllowAsync");
1669 // Checks that window.screenX/screenY/screenLeft/screenTop works correctly for
1670 // guests.
1671 IN_PROC_BROWSER_TEST_F(WebViewTest, ScreenCoordinates) {
1672 ASSERT_TRUE(RunPlatformAppTestWithArg(
1673 "platform_apps/web_view/common", "screen_coordinates"))
1674 << message_;
1677 // crbug/360448
1678 IN_PROC_BROWSER_TEST_F(WebViewTest, DISABLED_SpeechRecognition) {
1679 ASSERT_TRUE(StartEmbeddedTestServer());
1680 content::WebContents* guest_web_contents = LoadGuest(
1681 "/extensions/platform_apps/web_view/speech/guest.html",
1682 "web_view/speech");
1683 ASSERT_TRUE(guest_web_contents);
1685 // Click on the guest (center of the WebContents), the guest is rendered in a
1686 // way that this will trigger clicking on speech recognition input mic.
1687 SimulateMouseClick(guest_web_contents, 0, blink::WebMouseEvent::ButtonLeft);
1689 base::string16 expected_title(base::ASCIIToUTF16("PASSED"));
1690 base::string16 error_title(base::ASCIIToUTF16("FAILED"));
1691 content::TitleWatcher title_watcher(guest_web_contents, expected_title);
1692 title_watcher.AlsoWaitForTitle(error_title);
1693 EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
1696 #if defined(OS_CHROMEOS)
1697 IN_PROC_BROWSER_TEST_F(WebViewTest, ChromeVoxInjection) {
1698 EXPECT_FALSE(
1699 chromeos::AccessibilityManager::Get()->IsSpokenFeedbackEnabled());
1701 ASSERT_TRUE(StartEmbeddedTestServer());
1702 content::WebContents* guest_web_contents = LoadGuest(
1703 "/extensions/platform_apps/web_view/chromevox_injection/guest.html",
1704 "web_view/chromevox_injection");
1705 ASSERT_TRUE(guest_web_contents);
1707 chromeos::SpeechMonitor monitor;
1708 chromeos::AccessibilityManager::Get()->EnableSpokenFeedback(
1709 true, ash::A11Y_NOTIFICATION_NONE);
1710 EXPECT_TRUE(monitor.SkipChromeVoxEnabledMessage());
1712 EXPECT_EQ("chrome vox test title", monitor.GetNextUtterance());
1714 #endif
1716 // Flaky on Windows. http://crbug.com/303966
1717 #if defined(OS_WIN)
1718 #define MAYBE_TearDownTest DISABLED_TearDownTest
1719 #else
1720 #define MAYBE_TearDownTest TearDownTest
1721 #endif
1722 IN_PROC_BROWSER_TEST_F(WebViewTest, MAYBE_TearDownTest) {
1723 ExtensionTestMessageListener first_loaded_listener("guest-loaded", false);
1724 const extensions::Extension* extension =
1725 LoadAndLaunchPlatformApp("web_view/teardown");
1726 ASSERT_TRUE(first_loaded_listener.WaitUntilSatisfied());
1727 apps::AppWindow* window = NULL;
1728 if (!GetAppWindowCount())
1729 window = CreateAppWindow(extension);
1730 else
1731 window = GetFirstAppWindow();
1732 CloseAppWindow(window);
1734 // Load the app again.
1735 ExtensionTestMessageListener second_loaded_listener("guest-loaded", false);
1736 LoadAndLaunchPlatformApp("web_view/teardown");
1737 ASSERT_TRUE(second_loaded_listener.WaitUntilSatisfied());
1740 // In following GeolocationAPIEmbedderHasNoAccess* tests, embedder (i.e. the
1741 // platform app) does not have geolocation permission for this test.
1742 // No matter what the API does, geolocation permission would be denied.
1743 // Note that the test name prefix must be "GeolocationAPI".
1744 IN_PROC_BROWSER_TEST_F(WebViewTest, GeolocationAPIEmbedderHasNoAccessAllow) {
1745 TestHelper("testDenyDenies",
1746 "web_view/geolocation/embedder_has_no_permission",
1747 NEEDS_TEST_SERVER);
1750 IN_PROC_BROWSER_TEST_F(WebViewTest, GeolocationAPIEmbedderHasNoAccessDeny) {
1751 TestHelper("testDenyDenies",
1752 "web_view/geolocation/embedder_has_no_permission",
1753 NEEDS_TEST_SERVER);
1756 // In following GeolocationAPIEmbedderHasAccess* tests, embedder (i.e. the
1757 // platform app) has geolocation permission
1759 // Note that these test names must be "GeolocationAPI" prefixed (b/c we mock out
1760 // geolocation in this case).
1762 // Also note that these are run separately because OverrideGeolocation() doesn't
1763 // mock out geolocation for multiple navigator.geolocation calls properly and
1764 // the tests become flaky.
1765 // GeolocationAPI* test 1 of 3.
1766 IN_PROC_BROWSER_TEST_F(WebViewTest, GeolocationAPIEmbedderHasAccessAllow) {
1767 TestHelper("testAllow",
1768 "web_view/geolocation/embedder_has_permission",
1769 NEEDS_TEST_SERVER);
1772 // GeolocationAPI* test 2 of 3.
1773 IN_PROC_BROWSER_TEST_F(WebViewTest, GeolocationAPIEmbedderHasAccessDeny) {
1774 TestHelper("testDeny",
1775 "web_view/geolocation/embedder_has_permission",
1776 NEEDS_TEST_SERVER);
1779 // GeolocationAPI* test 3 of 3.
1780 IN_PROC_BROWSER_TEST_F(WebViewTest,
1781 GeolocationAPIEmbedderHasAccessMultipleBridgeIdAllow) {
1782 TestHelper("testMultipleBridgeIdAllow",
1783 "web_view/geolocation/embedder_has_permission",
1784 NEEDS_TEST_SERVER);
1787 // Tests that
1788 // BrowserPluginGeolocationPermissionContext::CancelGeolocationPermissionRequest
1789 // is handled correctly (and does not crash).
1790 IN_PROC_BROWSER_TEST_F(WebViewTest, GeolocationAPICancelGeolocation) {
1791 ASSERT_TRUE(StartEmbeddedTestServer()); // For serving guest pages.
1792 ASSERT_TRUE(RunPlatformAppTest(
1793 "platform_apps/web_view/geolocation/cancel_request")) << message_;
1796 IN_PROC_BROWSER_TEST_F(WebViewTest, DISABLED_GeolocationRequestGone) {
1797 ASSERT_TRUE(StartEmbeddedTestServer()); // For serving guest pages.
1798 ASSERT_TRUE(RunPlatformAppTest(
1799 "platform_apps/web_view/geolocation/geolocation_request_gone"))
1800 << message_;
1803 IN_PROC_BROWSER_TEST_F(WebViewTest, ClearData) {
1804 #if defined(OS_WIN)
1805 // Flaky on XP bot http://crbug.com/282674
1806 if (base::win::GetVersion() <= base::win::VERSION_XP)
1807 return;
1808 #endif
1810 ASSERT_TRUE(StartEmbeddedTestServer()); // For serving guest pages.
1811 ASSERT_TRUE(RunPlatformAppTestWithArg(
1812 "platform_apps/web_view/common", "cleardata"))
1813 << message_;
1816 // This test is disabled on Win due to being flaky. http://crbug.com/294592
1817 #if defined(OS_WIN)
1818 #define MAYBE_ConsoleMessage DISABLED_ConsoleMessage
1819 #else
1820 #define MAYBE_ConsoleMessage ConsoleMessage
1821 #endif
1822 IN_PROC_BROWSER_TEST_F(WebViewTest, MAYBE_ConsoleMessage) {
1823 ASSERT_TRUE(RunPlatformAppTestWithArg(
1824 "platform_apps/web_view/common", "console_messages"))
1825 << message_;
1828 IN_PROC_BROWSER_TEST_F(WebViewTest, DownloadPermission) {
1829 ASSERT_TRUE(StartEmbeddedTestServer()); // For serving guest pages.
1830 content::WebContents* guest_web_contents =
1831 LoadGuest("/extensions/platform_apps/web_view/download/guest.html",
1832 "web_view/download");
1833 ASSERT_TRUE(guest_web_contents);
1835 // Replace WebContentsDelegate with mock version so we can intercept download
1836 // requests.
1837 content::WebContentsDelegate* delegate = guest_web_contents->GetDelegate();
1838 scoped_ptr<MockDownloadWebContentsDelegate>
1839 mock_delegate(new MockDownloadWebContentsDelegate(delegate));
1840 guest_web_contents->SetDelegate(mock_delegate.get());
1842 // Start test.
1843 // 1. Guest requests a download that its embedder denies.
1844 EXPECT_TRUE(content::ExecuteScript(guest_web_contents,
1845 "startDownload('download-link-1')"));
1846 mock_delegate->WaitForCanDownload(false); // Expect to not allow.
1847 mock_delegate->Reset();
1849 // 2. Guest requests a download that its embedder allows.
1850 EXPECT_TRUE(content::ExecuteScript(guest_web_contents,
1851 "startDownload('download-link-2')"));
1852 mock_delegate->WaitForCanDownload(true); // Expect to allow.
1853 mock_delegate->Reset();
1855 // 3. Guest requests a download that its embedder ignores, this implies deny.
1856 EXPECT_TRUE(content::ExecuteScript(guest_web_contents,
1857 "startDownload('download-link-3')"));
1858 mock_delegate->WaitForCanDownload(false); // Expect to not allow.
1861 // This test makes sure loading <webview> does not crash when there is an
1862 // extension which has content script whitelisted/forced.
1863 IN_PROC_BROWSER_TEST_F(WebViewTest, WhitelistedContentScript) {
1864 // Whitelist the extension for running content script we are going to load.
1865 extensions::ExtensionsClient::ScriptingWhitelist whitelist;
1866 const std::string extension_id = "imeongpbjoodlnmlakaldhlcmijmhpbb";
1867 whitelist.push_back(extension_id);
1868 extensions::ExtensionsClient::Get()->SetScriptingWhitelist(whitelist);
1870 // Load the extension.
1871 const extensions::Extension* content_script_whitelisted_extension =
1872 LoadExtension(test_data_dir_.AppendASCII(
1873 "platform_apps/web_view/extension_api/content_script"));
1874 ASSERT_TRUE(content_script_whitelisted_extension);
1875 ASSERT_EQ(extension_id, content_script_whitelisted_extension->id());
1877 // Now load an app with <webview>.
1878 ExtensionTestMessageListener done_listener("TEST_PASSED", false);
1879 LoadAndLaunchPlatformApp("web_view/content_script_whitelisted");
1880 ASSERT_TRUE(done_listener.WaitUntilSatisfied());
1883 IN_PROC_BROWSER_TEST_F(WebViewTest, SetPropertyOnDocumentReady) {
1884 ASSERT_TRUE(RunPlatformAppTest("platform_apps/web_view/document_ready"))
1885 << message_;
1888 IN_PROC_BROWSER_TEST_F(WebViewTest, SetPropertyOnDocumentInteractive) {
1889 ASSERT_TRUE(RunPlatformAppTest("platform_apps/web_view/document_interactive"))
1890 << message_;
1893 IN_PROC_BROWSER_TEST_F(WebViewTest, SpeechRecognitionAPI_HasPermissionAllow) {
1894 ASSERT_TRUE(
1895 RunPlatformAppTestWithArg("platform_apps/web_view/speech_recognition_api",
1896 "allowTest"))
1897 << message_;
1900 IN_PROC_BROWSER_TEST_F(WebViewTest, SpeechRecognitionAPI_HasPermissionDeny) {
1901 ASSERT_TRUE(
1902 RunPlatformAppTestWithArg("platform_apps/web_view/speech_recognition_api",
1903 "denyTest"))
1904 << message_;
1907 IN_PROC_BROWSER_TEST_F(WebViewTest, SpeechRecognitionAPI_NoPermission) {
1908 ASSERT_TRUE(
1909 RunPlatformAppTestWithArg("platform_apps/web_view/common",
1910 "speech_recognition_api_no_permission"))
1911 << message_;
1914 // Tests overriding user agent.
1915 IN_PROC_BROWSER_TEST_F(WebViewTest, UserAgent) {
1916 ASSERT_TRUE(RunPlatformAppTestWithArg(
1917 "platform_apps/web_view/common", "useragent")) << message_;
1920 IN_PROC_BROWSER_TEST_F(WebViewTest, UserAgent_NewWindow) {
1921 ASSERT_TRUE(RunPlatformAppTestWithArg(
1922 "platform_apps/web_view/common",
1923 "useragent_newwindow")) << message_;
1926 IN_PROC_BROWSER_TEST_F(WebViewTest, NoPermission) {
1927 ASSERT_TRUE(RunPlatformAppTest("platform_apps/web_view/nopermission"))
1928 << message_;
1931 IN_PROC_BROWSER_TEST_F(WebViewTest, Dialog_TestAlertDialog) {
1932 TestHelper("testAlertDialog", "web_view/dialog", NO_TEST_SERVER);
1935 IN_PROC_BROWSER_TEST_F(WebViewTest, TestConfirmDialog) {
1936 TestHelper("testConfirmDialog", "web_view/dialog", NO_TEST_SERVER);
1939 IN_PROC_BROWSER_TEST_F(WebViewTest, Dialog_TestConfirmDialogCancel) {
1940 TestHelper("testConfirmDialogCancel", "web_view/dialog", NO_TEST_SERVER);
1943 IN_PROC_BROWSER_TEST_F(WebViewTest, Dialog_TestConfirmDialogDefaultCancel) {
1944 TestHelper("testConfirmDialogDefaultCancel",
1945 "web_view/dialog",
1946 NO_TEST_SERVER);
1949 IN_PROC_BROWSER_TEST_F(WebViewTest, Dialog_TestConfirmDialogDefaultGCCancel) {
1950 TestHelper("testConfirmDialogDefaultGCCancel",
1951 "web_view/dialog",
1952 NO_TEST_SERVER);
1955 IN_PROC_BROWSER_TEST_F(WebViewTest, Dialog_TestPromptDialog) {
1956 TestHelper("testPromptDialog", "web_view/dialog", NO_TEST_SERVER);
1959 IN_PROC_BROWSER_TEST_F(WebViewTest, NoContentSettingsAPI) {
1960 // Load the extension.
1961 const extensions::Extension* content_settings_extension =
1962 LoadExtension(
1963 test_data_dir_.AppendASCII(
1964 "platform_apps/web_view/extension_api/content_settings"));
1965 ASSERT_TRUE(content_settings_extension);
1966 TestHelper("testPostMessageCommChannel", "web_view/shim", NO_TEST_SERVER);
1969 #if defined(ENABLE_PLUGINS)
1970 class WebViewPluginTest : public WebViewTest {
1971 protected:
1972 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
1973 WebViewTest::SetUpCommandLine(command_line);
1975 // Append the switch to register the pepper plugin.
1976 // library name = <out dir>/<test_name>.<library_extension>
1977 // MIME type = application/x-ppapi-<test_name>
1978 base::FilePath plugin_dir;
1979 EXPECT_TRUE(PathService::Get(base::DIR_MODULE, &plugin_dir));
1981 base::FilePath plugin_lib = plugin_dir.Append(library_name);
1982 EXPECT_TRUE(base::PathExists(plugin_lib));
1983 base::FilePath::StringType pepper_plugin = plugin_lib.value();
1984 pepper_plugin.append(FILE_PATH_LITERAL(";application/x-ppapi-tests"));
1985 command_line->AppendSwitchNative(switches::kRegisterPepperPlugins,
1986 pepper_plugin);
1990 IN_PROC_BROWSER_TEST_F(WebViewPluginTest, TestLoadPluginEvent) {
1991 TestHelper("testPluginLoadPermission", "web_view/shim", NO_TEST_SERVER);
1993 #endif // defined(ENABLE_PLUGINS)
1995 class WebViewCaptureTest : public WebViewTest {
1996 public:
1997 WebViewCaptureTest() {}
1998 virtual ~WebViewCaptureTest() {}
1999 virtual void SetUp() OVERRIDE {
2000 EnablePixelOutput();
2001 WebViewTest::SetUp();
2005 IN_PROC_BROWSER_TEST_F(WebViewTest, Shim_TestZoomAPI) {
2006 TestHelper("testZoomAPI", "web_view/shim", NO_TEST_SERVER);
2009 IN_PROC_BROWSER_TEST_F(WebViewTest, Shim_TestFindAPI) {
2010 TestHelper("testFindAPI", "web_view/shim", NO_TEST_SERVER);
2013 IN_PROC_BROWSER_TEST_F(WebViewTest, Shim_TestFindAPI_findupdate) {
2014 TestHelper("testFindAPI_findupdate", "web_view/shim", NO_TEST_SERVER);
2017 // <webview> screenshot capture fails with ubercomp.
2018 // See http://crbug.com/327035.
2019 IN_PROC_BROWSER_TEST_F(WebViewCaptureTest,
2020 DISABLED_Shim_ScreenshotCapture) {
2021 TestHelper("testScreenshotCapture", "web_view/shim", NO_TEST_SERVER);