|
| 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/command_line.h" |
| 7 | +#include "base/memory/raw_ptr.h" |
| 8 | +#include "base/path_service.h" |
| 9 | +#include "base/test/bind.h" |
| 10 | +#include "brave/browser/brave_wallet/keyring_service_factory.h" |
| 11 | +#include "brave/components/brave_wallet/browser/keyring_service.h" |
| 12 | +#include "brave/components/constants/brave_paths.h" |
| 13 | +#include "chrome/browser/profiles/profile.h" |
| 14 | +#include "chrome/browser/ui/browser.h" |
| 15 | +#include "chrome/test/base/in_process_browser_test.h" |
| 16 | +#include "chrome/test/base/ui_test_utils.h" |
| 17 | +#include "components/network_session_configurator/common/network_switches.h" |
| 18 | +#include "content/public/browser/web_contents.h" |
| 19 | +#include "content/public/test/browser_test.h" |
| 20 | +#include "content/public/test/browser_test_utils.h" |
| 21 | +#include "content/public/test/test_utils.h" |
| 22 | +#include "net/dns/mock_host_resolver.h" |
| 23 | +#include "net/test/embedded_test_server/embedded_test_server.h" |
| 24 | +#include "url/gurl.h" |
| 25 | + |
| 26 | +namespace { |
| 27 | + |
| 28 | +std::string CheckForEventScript(const std::string& event_var) { |
| 29 | + return base::StringPrintf(R"(function waitForEvent() { |
| 30 | + if (%s) { |
| 31 | + window.domAutomationController.send(true); |
| 32 | + } |
| 33 | + } |
| 34 | + setInterval(waitForEvent, 100);)", |
| 35 | + event_var.c_str()); |
| 36 | +} |
| 37 | + |
| 38 | +} // namespace |
| 39 | + |
| 40 | +namespace brave_wallet { |
| 41 | + |
| 42 | +class EthereumProviderBrowserTest : public InProcessBrowserTest { |
| 43 | + public: |
| 44 | + EthereumProviderBrowserTest() |
| 45 | + : https_server_(net::EmbeddedTestServer::TYPE_HTTPS) {} |
| 46 | + |
| 47 | + ~EthereumProviderBrowserTest() override = default; |
| 48 | + |
| 49 | + void SetUpCommandLine(base::CommandLine* command_line) override { |
| 50 | + InProcessBrowserTest::SetUpCommandLine(command_line); |
| 51 | + command_line->AppendSwitch(switches::kIgnoreCertificateErrors); |
| 52 | + } |
| 53 | + |
| 54 | + void SetUpOnMainThread() override { |
| 55 | + host_resolver()->AddRule("*", "127.0.0.1"); |
| 56 | + |
| 57 | + brave::RegisterPathProvider(); |
| 58 | + base::FilePath test_data_dir; |
| 59 | + base::PathService::Get(brave::DIR_TEST_DATA, &test_data_dir); |
| 60 | + test_data_dir = test_data_dir.AppendASCII("brave-wallet"); |
| 61 | + https_server_.ServeFilesFromDirectory(test_data_dir); |
| 62 | + https_server_.SetSSLConfig(net::EmbeddedTestServer::CERT_TEST_NAMES); |
| 63 | + ASSERT_TRUE(https_server()->Start()); |
| 64 | + |
| 65 | + keyring_service_ = |
| 66 | + KeyringServiceFactory::GetServiceForContext(browser()->profile()); |
| 67 | + } |
| 68 | + |
| 69 | + content::WebContents* web_contents() { |
| 70 | + return browser()->tab_strip_model()->GetActiveWebContents(); |
| 71 | + } |
| 72 | + |
| 73 | + net::EmbeddedTestServer* https_server() { return &https_server_; } |
| 74 | + |
| 75 | + void RestoreWallet() { |
| 76 | + const char mnemonic[] = |
| 77 | + "drip caution abandon festival order clown oven regular absorb " |
| 78 | + "evidence crew where"; |
| 79 | + base::RunLoop run_loop; |
| 80 | + keyring_service_->RestoreWallet( |
| 81 | + mnemonic, "brave123", false, |
| 82 | + base::BindLambdaForTesting([&](bool success) { |
| 83 | + ASSERT_TRUE(success); |
| 84 | + run_loop.Quit(); |
| 85 | + })); |
| 86 | + run_loop.Run(); |
| 87 | + } |
| 88 | + |
| 89 | + private: |
| 90 | + net::test_server::EmbeddedTestServer https_server_; |
| 91 | + raw_ptr<KeyringService> keyring_service_ = nullptr; |
| 92 | +}; |
| 93 | + |
| 94 | +IN_PROC_BROWSER_TEST_F(EthereumProviderBrowserTest, InactiveTabRequest) { |
| 95 | + RestoreWallet(); |
| 96 | + GURL url = https_server()->GetURL("a.com", "/ethereum_provider.html"); |
| 97 | + |
| 98 | + ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url)); |
| 99 | + content::WebContents* first_tab_web_contents = web_contents(); |
| 100 | + |
| 101 | + // Add a new tab and switch to it |
| 102 | + ASSERT_TRUE(AddTabAtIndex(1, url, ui::PAGE_TRANSITION_TYPED)); |
| 103 | + ASSERT_EQ(browser()->tab_strip_model()->active_index(), 1); |
| 104 | + |
| 105 | + // Add an asset using the first page web contents |
| 106 | + ASSERT_TRUE( |
| 107 | + ExecJs(first_tab_web_contents, |
| 108 | + "wallet_watchAsset('ERC20', " |
| 109 | + "'0x6B175474E89094C44Da98b954EedeAC495271d0F', 'USDC', 6)")); |
| 110 | + base::RunLoop().RunUntilIdle(); |
| 111 | + |
| 112 | + auto result_first = |
| 113 | + EvalJs(first_tab_web_contents, CheckForEventScript("inactiveTabError"), |
| 114 | + content::EXECUTE_SCRIPT_USE_MANUAL_REPLY); |
| 115 | + EXPECT_EQ(base::Value(true), result_first.value); |
| 116 | +} |
| 117 | + |
| 118 | +IN_PROC_BROWSER_TEST_F(EthereumProviderBrowserTest, ActiveTabRequest) { |
| 119 | + RestoreWallet(); |
| 120 | + GURL url = https_server()->GetURL("a.com", "/ethereum_provider.html"); |
| 121 | + |
| 122 | + ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url)); |
| 123 | + |
| 124 | + // Add a new tab and switch to it |
| 125 | + ASSERT_TRUE(AddTabAtIndex(1, url, ui::PAGE_TRANSITION_TYPED)); |
| 126 | + ASSERT_EQ(browser()->tab_strip_model()->active_index(), 1); |
| 127 | + |
| 128 | + // Switch back to first tab |
| 129 | + browser()->tab_strip_model()->ActivateTabAt(0); |
| 130 | + ASSERT_EQ(browser()->tab_strip_model()->active_index(), 0); |
| 131 | + |
| 132 | + // Add an asset using the first page web contents |
| 133 | + ASSERT_TRUE( |
| 134 | + ExecJs(web_contents(), |
| 135 | + "wallet_watchAsset('ERC20', " |
| 136 | + "'0x6B175474E89094C44Da98b954EedeAC495271d0F', 'USDC', 6)")); |
| 137 | + base::RunLoop().RunUntilIdle(); |
| 138 | + |
| 139 | + auto result_first = |
| 140 | + EvalJs(web_contents(), CheckForEventScript("!inactiveTabError"), |
| 141 | + content::EXECUTE_SCRIPT_USE_MANUAL_REPLY); |
| 142 | + EXPECT_EQ(base::Value(true), result_first.value); |
| 143 | +} |
| 144 | + |
| 145 | +} // namespace brave_wallet |
0 commit comments