[email protected] | 2022903 | 2012-09-17 18:55:25 | [diff] [blame] | 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. |
| 4 | |
[email protected] | 10994d13 | 2013-06-11 07:16:18 | [diff] [blame] | 5 | #include "base/strings/string_util.h" |
[email protected] | 2022903 | 2012-09-17 18:55:25 | [diff] [blame] | 6 | #include "content/public/browser/web_contents.h" |
| 7 | #include "content/public/test/browser_test_utils.h" |
[email protected] | 6e9def1 | 2014-03-27 20:23:28 | [diff] [blame] | 8 | #include "content/public/test/content_browser_test.h" |
| 9 | #include "content/public/test/content_browser_test_utils.h" |
[email protected] | 2022903 | 2012-09-17 18:55:25 | [diff] [blame] | 10 | #include "content/public/test/test_utils.h" |
[email protected] | de7d61ff | 2013-08-20 11:30:41 | [diff] [blame] | 11 | #include "content/shell/browser/shell.h" |
[email protected] | 2022903 | 2012-09-17 18:55:25 | [diff] [blame] | 12 | #include "testing/gtest/include/gtest/gtest.h" |
| 13 | |
| 14 | namespace content { |
| 15 | |
| 16 | class BookmarkletTest : public ContentBrowserTest { |
meacer | e91f49f | 2017-04-05 00:34:34 | [diff] [blame] | 17 | protected: |
| 18 | void SetUpOnMainThread() override { |
| 19 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 20 | } |
| 21 | |
[email protected] | 2022903 | 2012-09-17 18:55:25 | [diff] [blame] | 22 | 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] | b6987e0 | 2013-01-04 18:30:43 | [diff] [blame] | 29 | EXPECT_TRUE(ExecuteScriptAndExtractString( |
nick | adef4a5 | 2016-06-09 18:45:54 | [diff] [blame] | 30 | shell(), |
[email protected] | 06bc5d9 | 2013-01-02 22:44:13 | [diff] [blame] | 31 | "window.domAutomationController.send(document.body.innerText);", |
[email protected] | 2022903 | 2012-09-17 18:55:25 | [diff] [blame] | 32 | &body_text)); |
| 33 | return body_text; |
| 34 | } |
| 35 | }; |
| 36 | |
| 37 | IN_PROC_BROWSER_TEST_F(BookmarkletTest, Redirect) { |
| 38 | NavigateToStartPage(); |
| 39 | |
meacer | e91f49f | 2017-04-05 00:34:34 | [diff] [blame] | 40 | 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] | 2022903 | 2012-09-17 18:55:25 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | IN_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. |
meacer | e91f49f | 2017-04-05 00:34:34 | [diff] [blame] | 54 | 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] | 2022903 | 2012-09-17 18:55:25 | [diff] [blame] | 59 | } |
| 60 | |
[email protected] | e91cd2e | 2013-02-24 14:54:21 | [diff] [blame] | 61 | // http://crbug.com/177957 |
[email protected] | bc3176fa | 2013-02-25 19:30:32 | [diff] [blame] | 62 | IN_PROC_BROWSER_TEST_F(BookmarkletTest, NonEmptyResult) { |
[email protected] | 2022903 | 2012-09-17 18:55:25 | [diff] [blame] | 63 | 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 | |
| 70 | IN_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] | 2022903 | 2012-09-17 18:55:25 | [diff] [blame] | 81 | } // namespace content |