File: glic_launcher_configuration_unittest.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 (88 lines) | stat: -rw-r--r-- 3,200 bytes parent folder | download | duplicates (4)
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
// 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/background/glic/glic_launcher_configuration.h"

#include "base/test/task_environment.h"
#include "chrome/browser/glic/glic_pref_names.h"
#include "chrome/common/chrome_features.h"
#include "chrome/test/base/scoped_testing_local_state.h"
#include "chrome/test/base/testing_browser_process.h"
#include "components/prefs/testing_pref_service.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/base/accelerators/command.h"
#include "ui/events/event_constants.h"
#include "ui/events/keycodes/keyboard_codes.h"

namespace glic {

namespace {
class MockObserver : public GlicLauncherConfiguration::Observer {
 public:
  // void OnEnabledChanged(bool enabled) override {}
  MOCK_METHOD1(OnEnabledChanged, void(bool));
  MOCK_METHOD1(OnGlobalHotkeyChanged, void(ui::Accelerator));
};
}  // namespace

class GlicLauncherConfigurationTest : public testing::Test {
 public:
  GlicLauncherConfigurationTest() = default;
  ~GlicLauncherConfigurationTest() override = default;

  PrefService* local_state() { return scoped_testing_local_state_.Get(); }

 private:
  content::BrowserTaskEnvironment task_environment_{
      base::test::TaskEnvironment::MainThreadType::UI};
  ScopedTestingLocalState scoped_testing_local_state_{
      TestingBrowserProcess::GetGlobal()};
};

TEST_F(GlicLauncherConfigurationTest, IsEnabled) {
  EXPECT_FALSE(GlicLauncherConfiguration::IsEnabled());

  local_state()->SetBoolean(prefs::kGlicLauncherEnabled, true);

  EXPECT_TRUE(GlicLauncherConfiguration::IsEnabled());
}

TEST_F(GlicLauncherConfigurationTest, GetGlobalHotkey_Default) {
  const ui::Accelerator accelerator =
      GlicLauncherConfiguration::GetGlobalHotkey();
  EXPECT_EQ(accelerator.key_code(), ui::VKEY_G);
#if BUILDFLAG(IS_MAC)
  EXPECT_TRUE(accelerator.IsCtrlDown());
  EXPECT_FALSE(accelerator.IsAltDown());
  EXPECT_FALSE(accelerator.IsCmdDown());
#else
  EXPECT_FALSE(accelerator.IsCtrlDown());
  EXPECT_TRUE(accelerator.IsAltDown());
  EXPECT_FALSE(accelerator.IsCmdDown());
#endif
}

TEST_F(GlicLauncherConfigurationTest, GetGlobalHotkey_Invalid) {
  const ui::Accelerator invalid_hotkey(ui::VKEY_G, ui::EF_NONE);
  local_state()->SetString(prefs::kGlicLauncherHotkey,
                           ui::Command::AcceleratorToString(invalid_hotkey));
  EXPECT_EQ(GlicLauncherConfiguration::GetGlobalHotkey(), ui::Accelerator());
}

TEST_F(GlicLauncherConfigurationTest, Observer) {
  MockObserver observer;
  GlicLauncherConfiguration glic_launcher_configuration{&observer};
  EXPECT_CALL(observer, OnEnabledChanged(true)).Times(1);
  local_state()->SetBoolean(prefs::kGlicLauncherEnabled, true);

  EXPECT_CALL(observer, OnGlobalHotkeyChanged(testing::_)).Times(1);
  const ui::Accelerator hotkey(ui::VKEY_K, ui::EF_ALT_DOWN);
  local_state()->SetString(prefs::kGlicLauncherHotkey,
                           ui::Command::AcceleratorToString(hotkey));
}

}  // namespace glic