blob: 761c642260fe299617763563d461b0d662aa2631 [file] [log] [blame]
[email protected]20229032012-09-17 18:55:251// 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.
4
5#include "base/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/test_utils.h"
9#include "content/shell/shell.h"
10#include "content/test/content_browser_test.h"
11#include "content/test/content_browser_test_utils.h"
12#include "testing/gtest/include/gtest/gtest.h"
13
14namespace content {
15
16class BookmarkletTest : public ContentBrowserTest {
17 public:
18 void NavigateToStartPage() {
19 NavigateToURL(shell(), GURL("data:text/html,start page"));
20 EXPECT_EQ("start page", GetBodyText());
21 }
22
23 std::string GetBodyText() {
24 std::string body_text;
25 EXPECT_TRUE(ExecuteJavaScriptAndExtractString(
26 shell()->web_contents()->GetRenderViewHost(),
27 L"",
28 L"window.domAutomationController.send(document.body.innerText);",
29 &body_text));
30 return body_text;
31 }
32};
33
34IN_PROC_BROWSER_TEST_F(BookmarkletTest, Redirect) {
35 NavigateToStartPage();
36
37 NavigateToURL(shell(), GURL(
38 "javascript:location.href='data:text/plain,SUCCESS'"));
39 EXPECT_EQ("SUCCESS", GetBodyText());
40}
41
42IN_PROC_BROWSER_TEST_F(BookmarkletTest, RedirectVoided) {
43 NavigateToStartPage();
44
45 // This test should be redundant with the Redirect test above. The point
46 // here is to emphasize that in either case the assignment to location during
47 // the evaluation of the script should suppress loading the script result.
48 // Here, because of the void() wrapping there is no script result.
49 NavigateToURL(shell(), GURL(
50 "javascript:void(location.href='data:text/plain,SUCCESS')"));
51 EXPECT_EQ("SUCCESS", GetBodyText());
52}
53
54IN_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'"));
58
59 EXPECT_EQ("hello world", GetBodyText());
60}
61
62IN_PROC_BROWSER_TEST_F(BookmarkletTest, DocumentWrite) {
63 NavigateToStartPage();
64
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());
71}
72
73
74} // namespace content
75