Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / content / browser / bookmarklet_browsertest.cc
blob5ec10d505a581333d6bbcaf7a7480c41a1f2739b
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 "base/strings/string_util.h"
6 #include "content/public/browser/web_contents.h"
7 #include "content/public/test/browser_test_utils.h"
8 #include "content/public/test/content_browser_test.h"
9 #include "content/public/test/content_browser_test_utils.h"
10 #include "content/public/test/test_utils.h"
11 #include "content/shell/browser/shell.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace content {
16 class BookmarkletTest : public ContentBrowserTest {
17 public:
18 void NavigateToStartPage() {
19 NavigateToURL(shell(), GURL("data:text/html,start page"));
20 EXPECT_EQ("start page", GetBodyText());
23 std::string GetBodyText() {
24 std::string body_text;
25 EXPECT_TRUE(ExecuteScriptAndExtractString(
26 shell()->web_contents(),
27 "window.domAutomationController.send(document.body.innerText);",
28 &body_text));
29 return body_text;
33 IN_PROC_BROWSER_TEST_F(BookmarkletTest, Redirect) {
34 NavigateToStartPage();
36 NavigateToURL(shell(), GURL(
37 "javascript:location.href='data:text/plain,SUCCESS'"));
38 EXPECT_EQ("SUCCESS", GetBodyText());
41 IN_PROC_BROWSER_TEST_F(BookmarkletTest, RedirectVoided) {
42 NavigateToStartPage();
44 // This test should be redundant with the Redirect test above. The point
45 // here is to emphasize that in either case the assignment to location during
46 // the evaluation of the script should suppress loading the script result.
47 // Here, because of the void() wrapping there is no script result.
48 NavigateToURL(shell(), GURL(
49 "javascript:void(location.href='data:text/plain,SUCCESS')"));
50 EXPECT_EQ("SUCCESS", GetBodyText());
53 // http://crbug.com/177957
54 IN_PROC_BROWSER_TEST_F(BookmarkletTest, NonEmptyResult) {
55 NavigateToStartPage();
56 // If there's no navigation, javascript: URLs are run synchronously.
57 shell()->LoadURL(GURL("javascript:'hello world'"));
59 EXPECT_EQ("hello world", GetBodyText());
62 IN_PROC_BROWSER_TEST_F(BookmarkletTest, DocumentWrite) {
63 NavigateToStartPage();
65 // If there's no navigation, javascript: URLs are run synchronously.
66 shell()->LoadURL(GURL(
67 "javascript:document.open();"
68 "document.write('hello world');"
69 "document.close();"));
70 EXPECT_EQ("hello world", GetBodyText());
73 } // namespace content