1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/glic/widget/glic_window_hotkey_delegate.h"
#include <memory>
#include <vector>
#include "base/containers/contains.h"
#include "base/memory/weak_ptr.h"
#include "base/test/metrics/user_action_tester.h"
#include "build/build_config.h"
#include "chrome/browser/glic/test_support/mock_glic_window_controller.h"
#include "chrome/browser/glic/widget/glic_view.h"
#include "chrome/browser/glic/widget/glic_window_controller.h"
#include "chrome/browser/glic/widget/local_hotkey_manager.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/test/base/scoped_testing_local_state.h"
#include "chrome/test/base/test_browser_window.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/accelerators/accelerator.h"
#include "ui/events/keycodes/keyboard_codes.h"
#include "ui/views/view.h"
namespace glic {
namespace {
class MockGlicView : public views::View {
public:
MockGlicView() = default;
~MockGlicView() override = default;
base::WeakPtr<MockGlicView> GetWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
private:
base::WeakPtrFactory<MockGlicView> weak_ptr_factory_{this};
};
class MockBrowserWindow : public TestBrowserWindow {
public:
MOCK_METHOD(void, Activate, ());
};
class GlicWindowHotkeyDelegateTest : public testing::Test {
public:
GlicWindowHotkeyDelegateTest()
: local_state_(TestingBrowserProcess::GetGlobal()) {}
void SetUp() override {
testing::Test::SetUp();
// Profile needed for Browser::Create
profile_ = std::make_unique<TestingProfile>();
// Create the mock window first
mock_browser_window_ = std::make_unique<MockBrowserWindow>();
// Create the fake Browser, providing the mock window.
// This Browser instance will be added to the BrowserList automatically.
Browser::CreateParams params(profile_.get(), true);
params.window = mock_browser_window_.get(); // Assign raw pointer
fake_browser_ = Browser::DeprecatedCreateOwnedForTesting(params);
// Create other mocks
mock_controller_ = std::make_unique<MockGlicWindowController>();
mock_glic_view_ = std::make_unique<MockGlicView>();
ON_CALL(*mock_controller_, GetGlicViewAsView())
.WillByDefault(testing::Return(mock_glic_view_->GetWeakPtr()));
delegate_ = std::make_unique<GlicWindowHotkeyDelegate>(
mock_controller_->GetWeakPtr());
}
void TearDown() override {
delegate_.reset();
mock_glic_view_.reset();
mock_controller_.reset();
// Destroy the browser before the window it points to.
// The Browser destructor should remove it from the BrowserList.
fake_browser_.reset();
mock_browser_window_.reset();
profile_.reset();
testing::Test::TearDown();
}
protected:
content::BrowserTaskEnvironment task_environment_;
std::unique_ptr<MockGlicWindowController> mock_controller_;
std::unique_ptr<MockGlicView> mock_glic_view_;
std::unique_ptr<GlicWindowHotkeyDelegate> delegate_;
base::UserActionTester user_action_tester_;
ScopedTestingLocalState local_state_;
std::unique_ptr<TestingProfile> profile_;
std::unique_ptr<MockBrowserWindow> mock_browser_window_;
std::unique_ptr<Browser>
fake_browser_; // Use real Browser, created minimally
};
} // namespace
TEST_F(GlicWindowHotkeyDelegateTest, GetSupportedHotkeys) {
auto supported = delegate_->GetSupportedHotkeys();
const std::vector<LocalHotkeyManager::Hotkey> kExpectedHotkeys = {
LocalHotkeyManager::Hotkey::kClose,
LocalHotkeyManager::Hotkey::kFocusToggle,
#if BUILDFLAG(IS_WIN)
LocalHotkeyManager::Hotkey::kTitleBarContextMenu,
#endif
};
EXPECT_THAT(supported, testing::ElementsAreArray(kExpectedHotkeys));
}
TEST_F(GlicWindowHotkeyDelegateTest, AcceleratorPressedClose) {
EXPECT_CALL(*mock_controller_, Close()).Times(1);
EXPECT_TRUE(
delegate_->AcceleratorPressed(LocalHotkeyManager::Hotkey::kClose));
}
TEST_F(GlicWindowHotkeyDelegateTest, AcceleratorPressedFocusToggleAttached) {
EXPECT_CALL(*mock_controller_, IsAttached()).WillOnce(testing::Return(true));
// Provide the fake browser to the mock controller
ON_CALL(*mock_controller_, attached_browser())
.WillByDefault(testing::Return(fake_browser_.get()));
EXPECT_CALL(*mock_browser_window_, Activate()).Times(1);
EXPECT_TRUE(
delegate_->AcceleratorPressed(LocalHotkeyManager::Hotkey::kFocusToggle));
EXPECT_EQ(0, user_action_tester_.GetActionCount("Glic.FocusHotKey"));
}
TEST_F(GlicWindowHotkeyDelegateTest,
AcceleratorPressedFocusToggleDetachedWithLastActive) {
// Set the fake browser as the last active one for this scenario.
// It's already in the BrowserList thanks to Browser::Create in SetUp.
BrowserList::SetLastActive(fake_browser_.get());
EXPECT_CALL(*mock_controller_, IsAttached()).WillOnce(testing::Return(false));
EXPECT_CALL(*mock_browser_window_, Activate()).Times(1);
EXPECT_TRUE(
delegate_->AcceleratorPressed(LocalHotkeyManager::Hotkey::kFocusToggle));
EXPECT_EQ(1, user_action_tester_.GetActionCount("Glic.FocusHotKey"));
}
TEST_F(GlicWindowHotkeyDelegateTest,
AcceleratorPressedFocusToggleDetachedNoLastActive) {
fake_browser_.reset();
EXPECT_CALL(*mock_controller_, IsAttached()).WillOnce(testing::Return(false));
// Ensure BrowserList returns nullptr
ASSERT_EQ(BrowserList::GetInstance()->GetLastActive(), nullptr);
EXPECT_FALSE(
delegate_->AcceleratorPressed(LocalHotkeyManager::Hotkey::kFocusToggle));
EXPECT_EQ(0, user_action_tester_.GetActionCount("Glic.FocusHotKey"));
}
#if BUILDFLAG(IS_WIN)
TEST_F(GlicWindowHotkeyDelegateTest, AcceleratorPressedTitleBarContextMenu) {
EXPECT_CALL(*mock_controller_, ShowTitleBarContextMenuAt(gfx::Point()))
.Times(1);
EXPECT_TRUE(delegate_->AcceleratorPressed(
LocalHotkeyManager::Hotkey::kTitleBarContextMenu));
}
#endif
TEST_F(GlicWindowHotkeyDelegateTest, CreateScopedHotkeyRegistration) {
ui::Accelerator test_accel(ui::VKEY_A, ui::EF_NONE);
auto registration =
delegate_->CreateScopedHotkeyRegistration(test_accel, nullptr);
ASSERT_TRUE(registration);
EXPECT_THAT(mock_glic_view_->GetAccelerators(),
testing::ElementsAre(test_accel));
registration.reset();
EXPECT_EQ(mock_glic_view_->GetAccelerators().size(), 0u);
}
TEST_F(GlicWindowHotkeyDelegateTest, MakeGlicWindowHotkeyManager) {
auto manager = MakeGlicWindowHotkeyManager(mock_controller_->GetWeakPtr());
EXPECT_TRUE(manager);
}
} // namespace glic
|