blob: 14d135cda15afe1a1456dcc5e7cecc140f8bb745 [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
[email protected]10994d132013-06-11 07:16:185#include "base/strings/string_util.h"
[email protected]20229032012-09-17 18:55:256#include "content/public/browser/web_contents.h"
7#include "content/public/test/browser_test_utils.h"
[email protected]6e9def12014-03-27 20:23:288#include "content/public/test/content_browser_test.h"
9#include "content/public/test/content_browser_test_utils.h"
[email protected]20229032012-09-17 18:55:2510#include "content/public/test/test_utils.h"
[email protected]de7d61ff2013-08-20 11:30:4111#include "content/shell/browser/shell.h"
[email protected]20229032012-09-17 18:55:2512#include "testing/gtest/include/gtest/gtest.h"
13
14namespace content {
15
16class BookmarkletTest : public ContentBrowserTest {
meacere91f49f2017-04-05 00:34:3417 protected:
18 void SetUpOnMainThread() override {
19 ASSERT_TRUE(embedded_test_server()->Start());
20 }
21
[email protected]20229032012-09-17 18:55:2522 void NavigateToStartPage() {
23 NavigateToURL(shell(), GURL("data:text/html,start page"));
24 EXPECT_EQ("start page", GetBodyText());
25 }
26
27 std::string GetBodyText() {
28 std::string body_text;
[email protected]b6987e02013-01-04 18:30:4329 EXPECT_TRUE(ExecuteScriptAndExtractString(
nickadef4a52016-06-09 18:45:5430 shell(),
[email protected]06bc5d92013-01-02 22:44:1331 "window.domAutomationController.send(document.body.innerText);",
[email protected]20229032012-09-17 18:55:2532 &body_text));
33 return body_text;
34 }
35};
36
37IN_PROC_BROWSER_TEST_F(BookmarkletTest, Redirect) {
38 NavigateToStartPage();
39
meacere91f49f2017-04-05 00:34:3440 const GURL url(base::StringPrintf(
41 "javascript:location.href='%s'",
42 embedded_test_server()->GetURL("/simple_page.html").spec().c_str()));
43 NavigateToURL(shell(), url);
44 EXPECT_EQ("Basic html test.", GetBodyText());
[email protected]20229032012-09-17 18:55:2545}
46
47IN_PROC_BROWSER_TEST_F(BookmarkletTest, RedirectVoided) {
48 NavigateToStartPage();
49
50 // This test should be redundant with the Redirect test above. The point
51 // here is to emphasize that in either case the assignment to location during
52 // the evaluation of the script should suppress loading the script result.
53 // Here, because of the void() wrapping there is no script result.
meacere91f49f2017-04-05 00:34:3454 const GURL url(base::StringPrintf(
55 "javascript:void(location.href='%s')",
56 embedded_test_server()->GetURL("/simple_page.html").spec().c_str()));
57 NavigateToURL(shell(), url);
58 EXPECT_EQ("Basic html test.", GetBodyText());
[email protected]20229032012-09-17 18:55:2559}
60
[email protected]e91cd2e2013-02-24 14:54:2161// http://crbug.com/177957
[email protected]bc3176fa2013-02-25 19:30:3262IN_PROC_BROWSER_TEST_F(BookmarkletTest, NonEmptyResult) {
[email protected]20229032012-09-17 18:55:2563 NavigateToStartPage();
64 // If there's no navigation, javascript: URLs are run synchronously.
65 shell()->LoadURL(GURL("javascript:'hello world'"));
66
67 EXPECT_EQ("hello world", GetBodyText());
68}
69
70IN_PROC_BROWSER_TEST_F(BookmarkletTest, DocumentWrite) {
71 NavigateToStartPage();
72
73 // If there's no navigation, javascript: URLs are run synchronously.
74 shell()->LoadURL(GURL(
75 "javascript:document.open();"
76 "document.write('hello world');"
77 "document.close();"));
78 EXPECT_EQ("hello world", GetBodyText());
79}
80
[email protected]20229032012-09-17 18:55:2581} // namespace content