|
| 1 | +/* Copyright (c) 2022 The Brave Authors. All rights reserved. |
| 2 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | + * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
| 4 | + * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 5 | + |
| 6 | +#include "base/path_service.h" |
| 7 | +#include "brave/browser/brave_content_browser_client.h" |
| 8 | +#include "brave/components/constants/brave_paths.h" |
| 9 | +#include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 10 | +#include "chrome/browser/profiles/profile.h" |
| 11 | +#include "chrome/browser/ui/browser.h" |
| 12 | +#include "chrome/common/chrome_content_client.h" |
| 13 | +#include "chrome/test/base/in_process_browser_test.h" |
| 14 | +#include "chrome/test/base/ui_test_utils.h" |
| 15 | +#include "content/public/test/browser_test.h" |
| 16 | +#include "content/public/test/browser_test_utils.h" |
| 17 | +#include "net/dns/mock_host_resolver.h" |
| 18 | +#include "url/gurl.h" |
| 19 | + |
| 20 | +namespace { |
| 21 | + |
| 22 | +const char kEmbeddedTestServerDirectory[] = "window_name"; |
| 23 | +const char kWindowNameScript[] = "window.name"; |
| 24 | + |
| 25 | +} // namespace |
| 26 | + |
| 27 | +class BraveWindowNameBrowserTest : public InProcessBrowserTest { |
| 28 | + public: |
| 29 | + BraveWindowNameBrowserTest() |
| 30 | + : https_server_(net::EmbeddedTestServer::TYPE_HTTPS) { |
| 31 | + https_server_.SetSSLConfig(net::EmbeddedTestServer::CERT_TEST_NAMES); |
| 32 | + brave::RegisterPathProvider(); |
| 33 | + base::FilePath test_data_dir; |
| 34 | + base::PathService::Get(brave::DIR_TEST_DATA, &test_data_dir); |
| 35 | + test_data_dir = test_data_dir.AppendASCII(kEmbeddedTestServerDirectory); |
| 36 | + https_server_.ServeFilesFromDirectory(test_data_dir); |
| 37 | + EXPECT_TRUE(https_server_.Start()); |
| 38 | + } |
| 39 | + |
| 40 | + BraveWindowNameBrowserTest(const BraveWindowNameBrowserTest&) = delete; |
| 41 | + BraveWindowNameBrowserTest& operator=(const BraveWindowNameBrowserTest&) = |
| 42 | + delete; |
| 43 | + |
| 44 | + ~BraveWindowNameBrowserTest() override {} |
| 45 | + |
| 46 | + void SetUpOnMainThread() override { |
| 47 | + InProcessBrowserTest::SetUpOnMainThread(); |
| 48 | + content_client_.reset(new ChromeContentClient); |
| 49 | + content::SetContentClient(content_client_.get()); |
| 50 | + browser_content_client_.reset(new BraveContentBrowserClient()); |
| 51 | + content::SetBrowserClientForTesting(browser_content_client_.get()); |
| 52 | + host_resolver()->AddRule("*", "127.0.0.1"); |
| 53 | + } |
| 54 | + |
| 55 | + void TearDown() override { |
| 56 | + browser_content_client_.reset(); |
| 57 | + content_client_.reset(); |
| 58 | + } |
| 59 | + |
| 60 | + protected: |
| 61 | + net::EmbeddedTestServer https_server_; |
| 62 | + |
| 63 | + content::WebContents* web_contents() { |
| 64 | + return browser()->tab_strip_model()->GetActiveWebContents(); |
| 65 | + } |
| 66 | + |
| 67 | + private: |
| 68 | + std::unique_ptr<ChromeContentClient> content_client_; |
| 69 | + std::unique_ptr<BraveContentBrowserClient> browser_content_client_; |
| 70 | +}; |
| 71 | + |
| 72 | +IN_PROC_BROWSER_TEST_F(BraveWindowNameBrowserTest, SameOrigin) { |
| 73 | + GURL url1 = https_server_.GetURL("a.test", "/set_window_name.html"); |
| 74 | + GURL url2 = https_server_.GetURL("a.test", "/get_window_name.html"); |
| 75 | + |
| 76 | + EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), url1)); |
| 77 | + EXPECT_EQ("foo", EvalJs(web_contents(), kWindowNameScript)); |
| 78 | + EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), url2)); |
| 79 | + // Since these URLs are in the same origin, window.name should persist across |
| 80 | + // navigation. |
| 81 | + EXPECT_EQ("foo", EvalJs(web_contents(), kWindowNameScript)); |
| 82 | +} |
| 83 | + |
| 84 | +IN_PROC_BROWSER_TEST_F(BraveWindowNameBrowserTest, CrossOrigin) { |
| 85 | + GURL url1 = https_server_.GetURL("a.test", "/set_window_name.html"); |
| 86 | + GURL url2 = https_server_.GetURL("b.test", "/get_window_name.html"); |
| 87 | + |
| 88 | + EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), url1)); |
| 89 | + EXPECT_EQ("foo", EvalJs(web_contents(), kWindowNameScript)); |
| 90 | + EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), url2)); |
| 91 | + // Since these URLs are in different origins, window.name should be cleared |
| 92 | + // during navigation. |
| 93 | + EXPECT_EQ("", EvalJs(web_contents(), kWindowNameScript)); |
| 94 | +} |
0 commit comments