| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/strings/string_util.h" | 5 #include "base/strings/string_util.h" |
| 6 #include "content/public/browser/web_contents.h" | 6 #include "content/public/browser/web_contents.h" |
| 7 #include "content/public/test/browser_test_utils.h" | 7 #include "content/public/test/browser_test_utils.h" |
| 8 #include "content/public/test/content_browser_test.h" | 8 #include "content/public/test/content_browser_test.h" |
| 9 #include "content/public/test/content_browser_test_utils.h" | 9 #include "content/public/test/content_browser_test_utils.h" |
| 10 #include "content/public/test/test_utils.h" | 10 #include "content/public/test/test_utils.h" |
| 11 #include "content/shell/browser/shell.h" | 11 #include "content/shell/browser/shell.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 13 |
| 14 namespace content { | 14 namespace content { |
| 15 | 15 |
| 16 class BookmarkletTest : public ContentBrowserTest { | 16 class BookmarkletTest : public ContentBrowserTest { |
| 17 public: | 17 protected: |
| 18 void SetUpOnMainThread() override { |
| 19 ASSERT_TRUE(embedded_test_server()->Start()); |
| 20 } |
| 21 |
| 18 void NavigateToStartPage() { | 22 void NavigateToStartPage() { |
| 19 NavigateToURL(shell(), GURL("data:text/html,start page")); | 23 NavigateToURL(shell(), GURL("data:text/html,start page")); |
| 20 EXPECT_EQ("start page", GetBodyText()); | 24 EXPECT_EQ("start page", GetBodyText()); |
| 21 } | 25 } |
| 22 | 26 |
| 23 std::string GetBodyText() { | 27 std::string GetBodyText() { |
| 24 std::string body_text; | 28 std::string body_text; |
| 25 EXPECT_TRUE(ExecuteScriptAndExtractString( | 29 EXPECT_TRUE(ExecuteScriptAndExtractString( |
| 26 shell(), | 30 shell(), |
| 27 "window.domAutomationController.send(document.body.innerText);", | 31 "window.domAutomationController.send(document.body.innerText);", |
| 28 &body_text)); | 32 &body_text)); |
| 29 return body_text; | 33 return body_text; |
| 30 } | 34 } |
| 31 }; | 35 }; |
| 32 | 36 |
| 33 IN_PROC_BROWSER_TEST_F(BookmarkletTest, Redirect) { | 37 IN_PROC_BROWSER_TEST_F(BookmarkletTest, Redirect) { |
| 34 NavigateToStartPage(); | 38 NavigateToStartPage(); |
| 35 | 39 |
| 36 NavigateToURL(shell(), GURL( | 40 const GURL url(base::StringPrintf( |
| 37 "javascript:location.href='data:text/plain,SUCCESS'")); | 41 "javascript:location.href='%s'", |
| 38 EXPECT_EQ("SUCCESS", GetBodyText()); | 42 embedded_test_server()->GetURL("/simple_page.html").spec().c_str())); |
| 43 NavigateToURL(shell(), url); |
| 44 EXPECT_EQ("Basic html test.", GetBodyText()); |
| 39 } | 45 } |
| 40 | 46 |
| 41 IN_PROC_BROWSER_TEST_F(BookmarkletTest, RedirectVoided) { | 47 IN_PROC_BROWSER_TEST_F(BookmarkletTest, RedirectVoided) { |
| 42 NavigateToStartPage(); | 48 NavigateToStartPage(); |
| 43 | 49 |
| 44 // This test should be redundant with the Redirect test above. The point | 50 // 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 | 51 // 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. | 52 // the evaluation of the script should suppress loading the script result. |
| 47 // Here, because of the void() wrapping there is no script result. | 53 // Here, because of the void() wrapping there is no script result. |
| 48 NavigateToURL(shell(), GURL( | 54 const GURL url(base::StringPrintf( |
| 49 "javascript:void(location.href='data:text/plain,SUCCESS')")); | 55 "javascript:void(location.href='%s')", |
| 50 EXPECT_EQ("SUCCESS", GetBodyText()); | 56 embedded_test_server()->GetURL("/simple_page.html").spec().c_str())); |
| 57 NavigateToURL(shell(), url); |
| 58 EXPECT_EQ("Basic html test.", GetBodyText()); |
| 51 } | 59 } |
| 52 | 60 |
| 53 // http://crbug.com/177957 | 61 // http://crbug.com/177957 |
| 54 IN_PROC_BROWSER_TEST_F(BookmarkletTest, NonEmptyResult) { | 62 IN_PROC_BROWSER_TEST_F(BookmarkletTest, NonEmptyResult) { |
| 55 NavigateToStartPage(); | 63 NavigateToStartPage(); |
| 56 // If there's no navigation, javascript: URLs are run synchronously. | 64 // If there's no navigation, javascript: URLs are run synchronously. |
| 57 shell()->LoadURL(GURL("javascript:'hello world'")); | 65 shell()->LoadURL(GURL("javascript:'hello world'")); |
| 58 | 66 |
| 59 EXPECT_EQ("hello world", GetBodyText()); | 67 EXPECT_EQ("hello world", GetBodyText()); |
| 60 } | 68 } |
| 61 | 69 |
| 62 IN_PROC_BROWSER_TEST_F(BookmarkletTest, DocumentWrite) { | 70 IN_PROC_BROWSER_TEST_F(BookmarkletTest, DocumentWrite) { |
| 63 NavigateToStartPage(); | 71 NavigateToStartPage(); |
| 64 | 72 |
| 65 // If there's no navigation, javascript: URLs are run synchronously. | 73 // If there's no navigation, javascript: URLs are run synchronously. |
| 66 shell()->LoadURL(GURL( | 74 shell()->LoadURL(GURL( |
| 67 "javascript:document.open();" | 75 "javascript:document.open();" |
| 68 "document.write('hello world');" | 76 "document.write('hello world');" |
| 69 "document.close();")); | 77 "document.close();")); |
| 70 EXPECT_EQ("hello world", GetBodyText()); | 78 EXPECT_EQ("hello world", GetBodyText()); |
| 71 } | 79 } |
| 72 | 80 |
| 73 } // namespace content | 81 } // namespace content |
| OLD | NEW |