Add tests for button and webui

This commit is contained in:
Brian R. Bondy
2021-05-04 11:57:48 -04:00
parent 4e11e481d2
commit 0d295db8a5
5 changed files with 175 additions and 1 deletions
+4
View File
@@ -39,6 +39,10 @@ class WalletButton : public ToolbarButton, public views::WidgetObserver {
void UpdateImageAndText();
void UpdateVisibility();
WebUIBubbleManager* webui_bubble_manager_for_testing() {
return &webui_bubble_manager_;
}
private:
void OnPreferenceChanged();
void OnWalletPressed(const ui::Event& event);
@@ -0,0 +1,98 @@
// Copyright (c) 2021 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// you can obtain one at http://mozilla.org/MPL/2.0/.
#include "brave/browser/ui/views/toolbar/wallet_button.h"
#include "base/feature_list.h"
#include "base/run_loop.h"
#include "base/test/scoped_feature_list.h"
#include "brave/browser/ui/views/frame/brave_browser_view.h"
#include "brave/components/brave_wallet/common/features.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/test/test_browser_dialog.h"
#include "chrome/browser/ui/views/bubble/webui_bubble_manager.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "content/public/test/browser_test.h"
#include "ui/views/test/button_test_api.h"
namespace {
ui::MouseEvent GetDummyEvent() {
return ui::MouseEvent(ui::ET_MOUSE_PRESSED, gfx::PointF(), gfx::PointF(),
base::TimeTicks::Now(), 0, 0);
}
} // namespace
namespace brave_wallet {
class WalletButtonButtonBrowserTest : public InProcessBrowserTest {
public:
// InProcessBrowserTest:
void SetUp() override {
scoped_feature_list_.InitAndEnableFeature(
features::kNativeBraveWalletFeature);
InProcessBrowserTest::SetUp();
}
BrowserView* browser_view() {
return BrowserView::GetBrowserViewForBrowser(browser());
}
WalletButton* wallet_button() {
return static_cast<BraveBrowserView*>(browser_view())->GetWalletButton();
}
WebUIBubbleManager* bubble_manager() {
return wallet_button()->webui_bubble_manager_for_testing();
}
void RunUntilBubbleWidgetDestroyed() {
ASSERT_NE(nullptr, bubble_manager()->GetBubbleWidget());
base::RunLoop run_loop;
base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
run_loop.QuitClosure());
run_loop.Run();
ASSERT_EQ(nullptr, bubble_manager()->GetBubbleWidget());
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
IN_PROC_BROWSER_TEST_F(WalletButtonButtonBrowserTest,
ButtonClickCreatesBubble) {
ASSERT_EQ(nullptr, bubble_manager()->GetBubbleWidget());
views::test::ButtonTestApi(wallet_button()).NotifyClick(GetDummyEvent());
ASSERT_NE(nullptr, bubble_manager()->GetBubbleWidget());
wallet_button()->CloseWalletBubble();
ASSERT_TRUE(bubble_manager()->GetBubbleWidget()->IsClosed());
RunUntilBubbleWidgetDestroyed();
}
class WalletButtonBrowserUITest : public DialogBrowserTest {
public:
// DialogBrowserTest:
void SetUp() override {
feature_list_.InitAndEnableFeature(features::kNativeBraveWalletFeature);
DialogBrowserTest::SetUp();
}
void ShowUi(const std::string& name) override {
auto* wallet_button = static_cast<BraveBrowserView*>(
BrowserView::GetBrowserViewForBrowser(browser()))
->GetWalletButton();
views::test::ButtonTestApi(wallet_button).NotifyClick(GetDummyEvent());
}
private:
base::test::ScopedFeatureList feature_list_;
};
// Invokes a wallet panel bubble.
IN_PROC_BROWSER_TEST_F(WalletButtonBrowserUITest, InvokeUi_default) {
ShowAndVerifyUi();
}
} // namespace brave_wallet
@@ -9,7 +9,6 @@
#include "brave/common/webui_url_constants.h"
#include "brave/components/brave_wallet_ui/grit/brave_wallet_panel_generated_map.h"
#include "brave/grit/wallet_panel_resources.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/webui/favicon_source.h"
#include "chrome/browser/ui/webui/webui_util.h"
@@ -0,0 +1,71 @@
// Copyright (c) 2021 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// you can obtain one at http://mozilla.org/MPL/2.0/.
#include <string>
#include "base/test/scoped_feature_list.h"
#include "brave/browser/ui/webui/wallet_panel/wallet_panel_ui.h"
#include "brave/common/webui_url_constants.h"
#include "brave/components/brave_wallet/common/features.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/common/chrome_isolated_world_ids.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/test_utils.h"
class WalletPanelUIBrowserTest : public InProcessBrowserTest {
public:
void SetUp() override {
feature_list_.InitAndEnableFeature(
brave_wallet::features::kNativeBraveWalletFeature);
InProcessBrowserTest::SetUp();
}
void SetUpOnMainThread() override {
webui_contents_ = content::WebContents::Create(
content::WebContents::CreateParams(browser()->profile()));
webui_contents_->GetController().LoadURLWithParams(
content::NavigationController::LoadURLParams(
GURL(kBraveUIWalletPanelURL)));
// Finish loading after initializing.
ASSERT_TRUE(content::WaitForLoadStop(webui_contents_.get()));
}
void TearDownOnMainThread() override { webui_contents_.reset(); }
void AppendTab(std::string url) {
chrome::AddTabAt(browser(), GURL(url), -1, true);
}
content::WebContents* GetActiveTab() {
return browser()->tab_strip_model()->GetActiveWebContents();
}
WalletPanelUI* GetWebUIController() {
return webui_contents_->GetWebUI()
->GetController()
->template GetAs<WalletPanelUI>();
}
protected:
std::unique_ptr<content::WebContents> webui_contents_;
private:
base::test::ScopedFeatureList feature_list_;
};
IN_PROC_BROWSER_TEST_F(WalletPanelUIBrowserTest, InitialUIRendered) {
const std::string wallet_panel_js =
"!!document.querySelector('wallet-panel-app')";
bool exists = content::EvalJs(webui_contents_.get(), wallet_panel_js,
content::EXECUTE_SCRIPT_DEFAULT_OPTIONS,
ISOLATED_WORLD_ID_CHROME_INTERNAL)
.ExtractBool();
ASSERT_TRUE(exists);
}
+2
View File
@@ -673,6 +673,8 @@ if (!is_android) {
"//brave/browser/brave_wallet/eth_json_rpc_controller_browsertest.cc",
"//brave/browser/extensions/api/brave_wallet_api_browsertest.cc",
"//brave/browser/extensions/brave_wallet_apitest.cc",
"//brave/browser/ui/views/toolbar/wallet_button_browsertest.cc",
"//brave/browser/ui/webui/wallet_panel/wallet_panel_ui_browsertest.cc",
]
deps += [