File: mouse_keys_tray_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 (152 lines) | stat: -rw-r--r-- 5,690 bytes parent folder | download | duplicates (6)
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
// 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 "ash/system/accessibility/mouse_keys/mouse_keys_tray.h"

#include "ash/accessibility/accessibility_controller.h"
#include "ash/accessibility/mouse_keys/mouse_keys_controller.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/system/status_area_widget.h"
#include "ash/system/status_area_widget_test_helper.h"
#include "ash/test/ash_test_base.h"
#include "base/test/scoped_feature_list.h"
#include "ui/accessibility/accessibility_features.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/events/base_event_utils.h"
#include "ui/views/accessibility/view_accessibility.h"

namespace ash {

namespace {

MouseKeysTray* GetTray() {
  return StatusAreaWidgetTestHelper::GetStatusAreaWidget()->mouse_keys_tray();
}

// Returns true if the Mouse keys tray is visible.
bool IsVisible() {
  return GetTray()->GetVisible();
}

bool IsActive() {
  return GetTray()->is_active();
}

}  // namespace

class MouseKeysTrayTest : public AshTestBase {
 public:
  MouseKeysTrayTest() = default;

  MouseKeysTrayTest(const MouseKeysTrayTest&) = delete;
  MouseKeysTrayTest& operator=(const MouseKeysTrayTest&) = delete;

  ~MouseKeysTrayTest() override = default;

  void SetUp() override {
    scoped_feature_list_.InitAndEnableFeature(
        ::features::kAccessibilityMouseKeys);
    AshTestBase::SetUp();
    Shell::Get()->accessibility_controller()->mouse_keys().SetEnabled(true);

    EXPECT_TRUE(GetImageView());
    EXPECT_TRUE(IsVisible());
  }

  // Gets the current tray image view.
  views::ImageView* GetImageView() { return GetTray()->GetIcon(); }

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
};

// Tests the icon disappears when mouse keys is disabled and re-appears
// when it is enabled.
TEST_F(MouseKeysTrayTest, ShowsAndHidesWithMouseKeysEnabled) {
  Shell::Get()->accessibility_controller()->mouse_keys().SetEnabled(false);
  EXPECT_FALSE(IsVisible());
  Shell::Get()->accessibility_controller()->mouse_keys().SetEnabled(true);
  EXPECT_TRUE(IsVisible());
}

// Trivial test to increase coverage of mouse_keys_tray.h. The
// MouseKeysTray does not have a bubble, so these are empty functions.
// Without this test, coverage of mouse_keys_tray.h is 0%.
TEST_F(MouseKeysTrayTest, OverriddenFunctionsDoNothing) {
  GetTray()->HideBubbleWithView(nullptr);
  const ui::MouseEvent event(ui::EventType::kMousePressed, gfx::Point(),
                             gfx::Point(), ui::EventTimeForNow(), 0, 0);
  GetTray()->ClickedOutsideBubble(event);
}

// Tests that the accessible name is set correctly in the accessibility cache.
TEST_F(MouseKeysTrayTest, AccessibleName) {
  ui::AXNodeData tray_data;
  GetTray()->GetViewAccessibility().GetAccessibleNodeData(&tray_data);
  EXPECT_EQ(tray_data.GetString16Attribute(ax::mojom::StringAttribute::kName),
            l10n_util::GetStringUTF16(
                IDS_ASH_STATUS_TRAY_ACCESSIBILITY_MOUSE_KEYS_PAUSE));
}

// Tests that the mouse keys icon is activated and deactivated when unpaused
// and paused respectively.
TEST_F(MouseKeysTrayTest, MouseKeysTrayStateChangesOnPause) {
  // When mouse keys is initially enabled, it should also be unpaused.
  Shell::Get()->accessibility_controller()->mouse_keys().SetEnabled(true);
  EXPECT_FALSE(Shell::Get()->mouse_keys_controller()->paused());
  EXPECT_TRUE(IsVisible());
  EXPECT_TRUE(IsActive());

  ui::AXNodeData tray_data;
  GetTray()->GetViewAccessibility().GetAccessibleNodeData(&tray_data);

  Shell::Get()->accessibility_controller()->ToggleMouseKeys();
  EXPECT_TRUE(Shell::Get()->mouse_keys_controller()->paused());
  EXPECT_EQ(GetImageView()->GetTooltipText(),
            l10n_util::GetStringUTF16(
                IDS_ASH_STATUS_TRAY_ACCESSIBILITY_MOUSE_KEYS_RESUME));
  EXPECT_EQ(tray_data.GetString16Attribute(ax::mojom::StringAttribute::kName),
            l10n_util::GetStringUTF16(
                IDS_ASH_STATUS_TRAY_ACCESSIBILITY_MOUSE_KEYS_PAUSE));
  EXPECT_FALSE(IsActive());

  GetTray()->GetViewAccessibility().GetAccessibleNodeData(&tray_data);
  Shell::Get()->accessibility_controller()->ToggleMouseKeys();
  EXPECT_FALSE(Shell::Get()->mouse_keys_controller()->paused());
  EXPECT_TRUE(IsActive());
  EXPECT_EQ(GetImageView()->GetTooltipText(),
            l10n_util::GetStringUTF16(
                IDS_ASH_STATUS_TRAY_ACCESSIBILITY_MOUSE_KEYS_PAUSE));
  EXPECT_EQ(tray_data.GetString16Attribute(ax::mojom::StringAttribute::kName),
            l10n_util::GetStringUTF16(
                IDS_ASH_STATUS_TRAY_ACCESSIBILITY_MOUSE_KEYS_RESUME));
}

// Tests that the mouse keys icon stays active when enabled from a previously
// paused state
TEST_F(MouseKeysTrayTest, MouseKeysActiveOnReenable) {
  // When mouse keys is initially enabled, it should also be unpaused.
  Shell::Get()->accessibility_controller()->mouse_keys().SetEnabled(true);
  EXPECT_FALSE(Shell::Get()->mouse_keys_controller()->paused());
  EXPECT_TRUE(IsVisible());
  EXPECT_TRUE(IsActive());

  // Pause mouse keys
  Shell::Get()->accessibility_controller()->ToggleMouseKeys();
  EXPECT_TRUE(Shell::Get()->mouse_keys_controller()->paused());
  EXPECT_FALSE(IsActive());

  // Disable mouse keys
  Shell::Get()->accessibility_controller()->mouse_keys().SetEnabled(false);
  EXPECT_FALSE(IsVisible());

  // Re-enable mouse keys, though paused before, it should be unpaused now.
  Shell::Get()->accessibility_controller()->mouse_keys().SetEnabled(true);
  EXPECT_FALSE(Shell::Get()->mouse_keys_controller()->paused());
  EXPECT_TRUE(IsVisible());
  EXPECT_TRUE(IsActive());
}

}  // namespace ash