File: glic_background_mode_manager_interactive_uitest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (276 lines) | stat: -rw-r--r-- 12,407 bytes parent folder | download | duplicates (2)
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <memory>

#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/values.h"
#include "chrome/browser/background/glic/glic_background_mode_manager.h"
#include "chrome/browser/background/glic/glic_launcher_configuration.h"
#include "chrome/browser/background/startup_launch_manager.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/glic/fre/glic_fre_controller.h"
#include "chrome/browser/glic/glic_keyed_service.h"
#include "chrome/browser/glic/glic_keyed_service_factory.h"
#include "chrome/browser/glic/glic_pref_names.h"
#include "chrome/browser/glic/test_support/glic_test_util.h"
#include "chrome/browser/glic/test_support/interactive_glic_test.h"
#include "chrome/browser/global_features.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profile_test_util.h"
#include "chrome/browser/status_icons/status_tray.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/common/chrome_features.h"
#include "chrome/test/base/ui_test_utils.h"
#include "chrome/test/interaction/interactive_browser_test.h"
#include "components/keep_alive_registry/keep_alive_registry.h"
#include "components/keep_alive_registry/keep_alive_types.h"
#include "components/prefs/pref_service.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_base.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/global_accelerator_listener/global_accelerator_listener.h"
#include "ui/events/event_constants.h"
#include "ui/events/keycodes/keyboard_codes.h"

namespace {
class TestStartupLaunchManager : public StartupLaunchManager {
 public:
  TestStartupLaunchManager() = default;
  MOCK_METHOD1(UpdateLaunchOnStartup, void(bool should_launch_on_startup));
};
}  // namespace

namespace glic {

class GlicBackgroundModeManagerUiTest : public test::InteractiveGlicTest {
 public:
  void TearDownOnMainThread() override {
    // Disable glic so that the glic_background_mode_manager won't prevent the
    // browser process from closing which causes the test to hang.
    g_browser_process->local_state()->SetBoolean(prefs::kGlicLauncherEnabled,
                                                 false);
    test::InteractiveGlicTest::TearDownOnMainThread();
  }

  bool IsHotkeySupported() {
    auto* const global_shortcut_listener =
        ui::GlobalAcceleratorListener::GetInstance();
    return global_shortcut_listener != nullptr &&
           !global_shortcut_listener->IsRegistrationHandledExternally();
  }

  void RegisterHotkey(ui::Accelerator updated_hotkey) {
    g_browser_process->local_state()->SetString(
        prefs::kGlicLauncherHotkey,
        ui::Command::AcceleratorToString(updated_hotkey));
  }

 private:
  base::test::ScopedFeatureList feature_list_;
};

// Checks that modifying the pref propagates to KeepAliveRegistry.
IN_PROC_BROWSER_TEST_F(GlicBackgroundModeManagerUiTest, KeepAlive) {
  auto* keep_alive_registry = KeepAliveRegistry::GetInstance();
  ASSERT_FALSE(
      keep_alive_registry->IsOriginRegistered(KeepAliveOrigin::GLIC_LAUNCHER));

  g_browser_process->local_state()->SetBoolean(prefs::kGlicLauncherEnabled,
                                               true);
  EXPECT_TRUE(
      keep_alive_registry->IsOriginRegistered(KeepAliveOrigin::GLIC_LAUNCHER));

  g_browser_process->local_state()->SetBoolean(prefs::kGlicLauncherEnabled,
                                               false);
  EXPECT_FALSE(
      keep_alive_registry->IsOriginRegistered(KeepAliveOrigin::GLIC_LAUNCHER));
}

// Checks that the status icon exists when the pref is enabled.
IN_PROC_BROWSER_TEST_F(GlicBackgroundModeManagerUiTest, StatusIcon) {
  ASSERT_FALSE(g_browser_process->status_tray()->HasStatusIconOfTypeForTesting(
      StatusTray::StatusIconType::GLIC_ICON));

  g_browser_process->local_state()->SetBoolean(prefs::kGlicLauncherEnabled,
                                               true);
  EXPECT_TRUE(g_browser_process->status_tray()->HasStatusIconOfTypeForTesting(
      StatusTray::StatusIconType::GLIC_ICON));

  g_browser_process->local_state()->SetBoolean(prefs::kGlicLauncherEnabled,
                                               false);
  EXPECT_FALSE(g_browser_process->status_tray()->HasStatusIconOfTypeForTesting(
      StatusTray::StatusIconType::GLIC_ICON));
}

IN_PROC_BROWSER_TEST_F(GlicBackgroundModeManagerUiTest,
                       UpdateHotkeyWhileEnabled) {
  if (!IsHotkeySupported()) {
    GTEST_SKIP() << "Test does not apply to this platform.";
  }
  g_browser_process->local_state()->SetBoolean(prefs::kGlicLauncherEnabled,
                                               true);
  GlicBackgroundModeManager* const manager =
      g_browser_process->GetFeatures()->glic_background_mode_manager();
  EXPECT_EQ(ui::Accelerator(ui::VKEY_G,
#if BUILDFLAG(IS_MAC)
                            ui::EF_CONTROL_DOWN
#else
                            ui::EF_ALT_DOWN
#endif
                            ),
            manager->RegisteredHotkeyForTesting());

  ui::Accelerator updated_hotkey(ui::VKEY_A, ui::EF_CONTROL_DOWN);
  RegisterHotkey(updated_hotkey);
  EXPECT_EQ(updated_hotkey, manager->RegisteredHotkeyForTesting());
}

IN_PROC_BROWSER_TEST_F(GlicBackgroundModeManagerUiTest,
                       UpdateHotkeyWhileDisabled) {
  if (!IsHotkeySupported()) {
    GTEST_SKIP() << "Test does not apply to this platform.";
  }
  PrefService* const pref_service = g_browser_process->local_state();
  ASSERT_FALSE(pref_service->GetBoolean(prefs::kGlicLauncherEnabled));
  GlicBackgroundModeManager* const manager =
      g_browser_process->GetFeatures()->glic_background_mode_manager();
  EXPECT_TRUE(manager->RegisteredHotkeyForTesting().IsEmpty());

  // If the hotkey pref were to somehow change even while glic was disabled,
  // the manager should not register the hotkey.
  ui::Accelerator updated_hotkey(ui::VKEY_A, ui::EF_CONTROL_DOWN);
  RegisterHotkey(updated_hotkey);
  EXPECT_TRUE(manager->RegisteredHotkeyForTesting().IsEmpty());

  // Re-enabling glic should register the updated hotkey pref.
  pref_service->SetBoolean(prefs::kGlicLauncherEnabled, true);
  EXPECT_EQ(updated_hotkey, manager->RegisteredHotkeyForTesting());
}

IN_PROC_BROWSER_TEST_F(GlicBackgroundModeManagerUiTest,
                       RegisterInvalidAccelerator) {
  if (!IsHotkeySupported()) {
    GTEST_SKIP() << "Test does not apply to this platform.";
  }
  g_browser_process->local_state()->SetBoolean(prefs::kGlicLauncherEnabled,
                                               true);
  GlicBackgroundModeManager* const manager =
      g_browser_process->GetFeatures()->glic_background_mode_manager();

  // Registering an invalid hotkey should fail.
  ui::Accelerator updated_hotkey(ui::VKEY_A, ui::EF_NONE);
  RegisterHotkey(updated_hotkey);
  EXPECT_NE(updated_hotkey, manager->RegisteredHotkeyForTesting());
}

IN_PROC_BROWSER_TEST_F(GlicBackgroundModeManagerUiTest,
                       SuspendShortcutAndRegisterAccelerator) {
  if (!IsHotkeySupported()) {
    GTEST_SKIP() << "Test does not apply to this platform.";
  }
  g_browser_process->local_state()->SetBoolean(prefs::kGlicLauncherEnabled,
                                               true);
  GlicBackgroundModeManager* const manager =
      g_browser_process->GetFeatures()->glic_background_mode_manager();

  auto* const global_accelerator_listener =
      ui::GlobalAcceleratorListener::GetInstance();
  global_accelerator_listener->SetShortcutHandlingSuspended(true);
  ui::Accelerator updated_hotkey(ui::VKEY_A,
                                 ui::EF_CONTROL_DOWN | ui::EF_SHIFT_DOWN);
  RegisterHotkey(updated_hotkey);

  EXPECT_EQ(updated_hotkey, manager->RegisteredHotkeyForTesting());
  EXPECT_TRUE(global_accelerator_listener->IsShortcutHandlingSuspended());
}

#if BUILDFLAG(IS_WIN)
IN_PROC_BROWSER_TEST_F(GlicBackgroundModeManagerUiTest, LaunchOnStartup) {
  auto launch_manager = std::make_unique<TestStartupLaunchManager>();
  StartupLaunchManager::SetInstanceForTesting(launch_manager.get());

  EXPECT_CALL(*launch_manager, UpdateLaunchOnStartup(true))
      .Times(testing::Exactly(1));
  g_browser_process->local_state()->SetBoolean(prefs::kGlicLauncherEnabled,
                                               true);
  testing::Mock::VerifyAndClearExpectations(launch_manager.get());
  EXPECT_CALL(*launch_manager, UpdateLaunchOnStartup(false))
      .Times(testing::Exactly(1));
  g_browser_process->local_state()->SetBoolean(prefs::kGlicLauncherEnabled,
                                               false);
  testing::Mock::VerifyAndClearExpectations(launch_manager.get());
}
#endif

// Test that hotkey is logged when pressed.
IN_PROC_BROWSER_TEST_F(GlicBackgroundModeManagerUiTest, HotkeyPressed) {
  if (!IsHotkeySupported()) {
    GTEST_SKIP() << "Test does not apply to this platform.";
  }
  g_browser_process->local_state()->SetBoolean(prefs::kGlicLauncherEnabled,
                                               true);
  base::HistogramTester histogram_tester;
  GlicBackgroundModeManager* const manager =
      g_browser_process->GetFeatures()->glic_background_mode_manager();

  ui::Accelerator default_hotkey =
      GlicLauncherConfiguration::GetDefaultHotkey();
  EXPECT_EQ(default_hotkey, manager->RegisteredHotkeyForTesting());
  manager->OnKeyPressed(default_hotkey);
  histogram_tester.ExpectBucketCount("Glic.Usage.Hotkey", HotkeyUsage::kDefault,
                                     1);
  histogram_tester.ExpectBucketCount("Glic.Usage.Hotkey", HotkeyUsage::kCustom,
                                     0);

  ui::Accelerator custom_hotkey(ui::VKEY_A, ui::EF_CONTROL_DOWN);
  RegisterHotkey(custom_hotkey);
  EXPECT_EQ(custom_hotkey, manager->RegisteredHotkeyForTesting());
  manager->OnKeyPressed(custom_hotkey);
  histogram_tester.ExpectBucketCount("Glic.Usage.Hotkey", HotkeyUsage::kDefault,
                                     1);
  histogram_tester.ExpectBucketCount("Glic.Usage.Hotkey", HotkeyUsage::kCustom,
                                     1);
}

IN_PROC_BROWSER_TEST_F(GlicBackgroundModeManagerUiTest, DeleteEligibleProfile) {
  GlicBackgroundModeManager* const background_mode_manager =
      g_browser_process->GetFeatures()->glic_background_mode_manager();
  g_browser_process->local_state()->SetBoolean(prefs::kGlicLauncherEnabled,
                                               true);
  EXPECT_TRUE(background_mode_manager->IsInBackgroundModeForTesting());

  // Create a second browser with a different profile that didn't complete Glic
  // fre yet.
  ProfileManager* const profile_manager = g_browser_process->profile_manager();
  Profile& second_profile = profiles::testing::CreateProfileSync(
      profile_manager, profile_manager->GenerateNextProfileDirectoryPath());
  Browser* const second_browser = CreateBrowser(&second_profile);
  SigninWithPrimaryAccount(&second_profile);
  SetModelExecutionCapability(&second_profile, true);

  // Delete the first profile and the glic launcher should not be in the
  // background since there are no profiles that are eligible to use glic.
  profile_manager->GetDeleteProfileHelper().MaybeScheduleProfileForDeletion(
      browser()->profile()->GetPath(), base::DoNothing(),
      ProfileMetrics::DELETE_PROFILE_USER_MANAGER);
  ui_test_utils::WaitForBrowserToClose(browser());
  EXPECT_FALSE(background_mode_manager->IsInBackgroundModeForTesting());
  EXPECT_TRUE(g_browser_process->local_state()->GetBoolean(
      prefs::kGlicLauncherEnabled));

  // The GlicBackgroundModeManager should go into background mode after
  // completing the fre in the second profile since the glic launcher local pref
  // has already been set to enabled.
  GlicKeyedService* const second_keyed_service =
      GlicKeyedServiceFactory::GetGlicKeyedService(second_browser->profile());
  EXPECT_FALSE(second_keyed_service->enabling().HasConsented());
  second_keyed_service->window_controller().fre_controller()->AcceptFre();
  EXPECT_TRUE(second_keyed_service->enabling().HasConsented());
  EXPECT_TRUE(background_mode_manager->IsInBackgroundModeForTesting());
}
}  // namespace glic