File: local_hotkey_manager_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (238 lines) | stat: -rw-r--r-- 8,945 bytes parent folder | download | duplicates (5)
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// 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/local_hotkey_manager.h"

#include <memory>
#include <vector>

#include "base/containers/flat_set.h"
#include "base/memory/weak_ptr.h"
#include "base/test/task_environment.h"
#include "chrome/browser/glic/glic_pref_names.h"
#include "chrome/browser/glic/test_support/mock_glic_window_controller.h"
#include "chrome/browser/glic/widget/glic_window_controller.h"
#include "chrome/test/base/scoped_testing_local_state.h"
#include "chrome/test/base/testing_browser_process.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/testing_pref_service.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/accelerators/accelerator.h"
#include "ui/base/accelerators/command.h"
#include "ui/events/event_constants.h"
#include "ui/events/keycodes/keyboard_codes.h"

namespace glic {

namespace {
class FakeScopedHotkeyRegistration
    : public LocalHotkeyManager::ScopedHotkeyRegistration {
 public:
  FakeScopedHotkeyRegistration(ui::Accelerator accelerator,
                               base::OnceClosure destruction_callback)
      : accelerator_(accelerator),
        destruction_callback_(std::move(destruction_callback)) {}

  ~FakeScopedHotkeyRegistration() override {
    if (destruction_callback_) {
      std::move(destruction_callback_).Run();
    }
  }

  ui::Accelerator accelerator() const { return accelerator_; }

 private:
  ui::Accelerator accelerator_;
  base::OnceClosure destruction_callback_;
};

class FakeLocalHotkeyDelegate : public LocalHotkeyManager::Delegate {
 public:
  FakeLocalHotkeyDelegate() = default;
  ~FakeLocalHotkeyDelegate() override = default;

  // LocalHotkeyManager::Delegate:
  const base::span<const LocalHotkeyManager::Hotkey> GetSupportedHotkeys()
      const override {
    return supported_hotkeys_;
  }

  std::unique_ptr<FakeScopedHotkeyRegistration::ScopedHotkeyRegistration>
  CreateScopedHotkeyRegistration(
      ui::Accelerator accelerator,
      base::WeakPtr<ui::AcceleratorTarget> target) override {
    last_registered_accelerator_ = accelerator;
    registration_count_++;
    auto registration = std::make_unique<FakeScopedHotkeyRegistration>(
        accelerator,
        base::BindOnce(&FakeLocalHotkeyDelegate::OnRegistrationDestroyed,
                       base::Unretained(this), accelerator));
    active_registrations_.insert(accelerator);
    return registration;
  }

  bool AcceleratorPressed(LocalHotkeyManager::Hotkey hotkey) override {
    last_pressed_hotkey_ = hotkey;
    return true;  // Assume handled
  }

  // Test helpers
  void SetSupportedHotkeys(std::vector<LocalHotkeyManager::Hotkey> hotkeys) {
    supported_hotkeys_ = std::move(hotkeys);
  }

  std::optional<LocalHotkeyManager::Hotkey> last_pressed_hotkey() const {
    return last_pressed_hotkey_;
  }

  std::optional<ui::Accelerator> last_registered_accelerator() const {
    return last_registered_accelerator_;
  }

  int registration_count() const { return registration_count_; }
  int destruction_count() const { return destruction_count_; }
  bool IsRegistered(ui::Accelerator acc) const {
    return active_registrations_.contains(acc);
  }

 private:
  void OnRegistrationDestroyed(ui::Accelerator accelerator) {
    destruction_count_++;
    active_registrations_.erase(accelerator);
  }

  std::vector<LocalHotkeyManager::Hotkey> supported_hotkeys_ = {
      LocalHotkeyManager::Hotkey::kClose,
      LocalHotkeyManager::Hotkey::kFocusToggle};
  std::optional<LocalHotkeyManager::Hotkey> last_pressed_hotkey_;
  std::optional<ui::Accelerator> last_registered_accelerator_;
  int registration_count_ = 0;
  int destruction_count_ = 0;
  base::flat_set<ui::Accelerator> active_registrations_;
};

class LocalHotkeyManagerTest : public testing::Test {
 public:
  LocalHotkeyManagerTest() : local_state_(TestingBrowserProcess::GetGlobal()) {}

  void SetUp() override {
    mock_controller_ = std::make_unique<MockGlicWindowController>();
    auto fake_delegate = std::make_unique<FakeLocalHotkeyDelegate>();
    fake_delegate_ = fake_delegate.get();  // Keep a raw pointer for access

    manager_ = std::make_unique<LocalHotkeyManager>(
        mock_controller_->GetWeakPtr(), std::move(fake_delegate));
  }

 protected:
  base::test::TaskEnvironment task_environment_;
  ScopedTestingLocalState local_state_;
  std::unique_ptr<MockGlicWindowController> mock_controller_;
  std::unique_ptr<LocalHotkeyManager> manager_;
  raw_ptr<FakeLocalHotkeyDelegate> fake_delegate_;
};

}  // namespace

TEST_F(LocalHotkeyManagerTest, InitializationRegistersSupportedHotkeys) {
  EXPECT_EQ(fake_delegate_->registration_count(), 0);

  manager_->InitializeAccelerators();

  // Default FakeLocalHotkeyDelegate supports kClose and kFocusToggle.
  // kClose has 2 static accelerators (Escape, Ctrl/Cmd+W).
  // kFocusToggle has 1 configurable accelerator.
  // Total = 3.
  EXPECT_EQ(fake_delegate_->registration_count(), 3);

  for (const auto& acc : LocalHotkeyManager::GetStaticAccelerators(
           LocalHotkeyManager::Hotkey::kClose)) {
    EXPECT_TRUE(fake_delegate_->IsRegistered(acc));
  }
  EXPECT_TRUE(
      fake_delegate_->IsRegistered(LocalHotkeyManager::GetDefaultAccelerator(
          LocalHotkeyManager::Hotkey::kFocusToggle)));
}

TEST_F(LocalHotkeyManagerTest, AcceleratorPressedCallsDelegate) {
  manager_->InitializeAccelerators();
  // Use the first static accelerator for kClose (Escape)
  ui::Accelerator close_acc = LocalHotkeyManager::GetStaticAccelerators(
      LocalHotkeyManager::Hotkey::kClose)[0];

  EXPECT_FALSE(fake_delegate_->last_pressed_hotkey().has_value());
  EXPECT_TRUE(manager_->AcceleratorPressed(close_acc));
  ASSERT_TRUE(fake_delegate_->last_pressed_hotkey().has_value());
  EXPECT_EQ(fake_delegate_->last_pressed_hotkey().value(),
            LocalHotkeyManager::Hotkey::kClose);
}

TEST_F(LocalHotkeyManagerTest, CanHandleAcceleratorsDependsOnController) {
  EXPECT_CALL(*mock_controller_, IsShowing()).WillOnce(testing::Return(false));
  EXPECT_FALSE(manager_->CanHandleAccelerators());

  EXPECT_CALL(*mock_controller_, IsShowing()).WillOnce(testing::Return(true));
  EXPECT_TRUE(manager_->CanHandleAccelerators());
}

TEST_F(LocalHotkeyManagerTest, PrefChangeUpdatesRegistration) {
  manager_->InitializeAccelerators();
  // Initial: kClose (2 accelerators) + kFocusToggle (1 accelerator) = 3
  EXPECT_EQ(fake_delegate_->registration_count(), 3);
  EXPECT_EQ(fake_delegate_->destruction_count(), 0);

  ui::Accelerator default_focus_acc = LocalHotkeyManager::GetDefaultAccelerator(
      LocalHotkeyManager::Hotkey::kFocusToggle);
  EXPECT_TRUE(fake_delegate_->IsRegistered(default_focus_acc));

  // Change the pref to a new valid accelerator.
  ui::Accelerator new_focus_acc(ui::VKEY_X, ui::EF_CONTROL_DOWN);
  local_state_.Get()->SetString(
      prefs::kGlicFocusToggleHotkey,
      ui::Command::AcceleratorToString(new_focus_acc));

  // Should destroy the old registration and create a new one.
  EXPECT_EQ(fake_delegate_->registration_count(), 4);
  EXPECT_EQ(fake_delegate_->destruction_count(), 1);
  EXPECT_FALSE(fake_delegate_->IsRegistered(default_focus_acc));
  EXPECT_TRUE(fake_delegate_->IsRegistered(new_focus_acc));

  // Change the pref to an empty string (clears the shortcut).
  local_state_.Get()->SetString(prefs::kGlicFocusToggleHotkey, "");

  // Should destroy the custom registration, no new one created.
  // Registration count remains the same as the previous step because one was
  // destroyed but no new one was added.
  EXPECT_EQ(fake_delegate_->registration_count(), 4);
  EXPECT_EQ(fake_delegate_->destruction_count(), 2);
  EXPECT_FALSE(fake_delegate_->IsRegistered(new_focus_acc));
}

TEST_F(LocalHotkeyManagerTest, GetAcceleratorRespectsPrefs) {
  ui::Accelerator default_focus_acc = LocalHotkeyManager::GetDefaultAccelerator(
      LocalHotkeyManager::Hotkey::kFocusToggle);

  // FocusToggle is backed by a pref. Default initially.
  EXPECT_EQ(LocalHotkeyManager::GetConfigurableAccelerator(
                LocalHotkeyManager::Hotkey::kFocusToggle),
            default_focus_acc);

  // Set a valid pref.
  ui::Accelerator new_focus_acc(ui::VKEY_X, ui::EF_CONTROL_DOWN);
  local_state_.Get()->SetString(
      prefs::kGlicFocusToggleHotkey,
      ui::Command::AcceleratorToString(new_focus_acc));
  EXPECT_EQ(LocalHotkeyManager::GetConfigurableAccelerator(
                LocalHotkeyManager::Hotkey::kFocusToggle),
            new_focus_acc);

  // Set an invalid pref string (e.g., just a modifier).
  local_state_.Get()->SetString(prefs::kGlicFocusToggleHotkey, "Ctrl");
  EXPECT_TRUE(LocalHotkeyManager::GetConfigurableAccelerator(
                  LocalHotkeyManager::Hotkey::kFocusToggle)
                  .IsEmpty());
}

}  // namespace glic