File: accelerator_tracker_unittest.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (83 lines) | stat: -rw-r--r-- 3,603 bytes parent folder | download
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
// Copyright 2022 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/accelerators/accelerator_tracker.h"

#include "ash/test/ash_test_base.h"
#include "base/strings/string_piece_forward.h"
#include "base/test/metrics/user_action_tester.h"

namespace ash {

using AcceleratorTrackerTest = AshTestBase;

constexpr base::StringPiece kUserActionPrefix = "AccelTracker_";

// Tests user action string starts with "AccelTracker_". The user action
// string locates in the kAcceleratorTrackerList table in accelerator_tracker.h
// file. Please make sure the user action strings in the table have this prefix.
TEST_F(AcceleratorTrackerTest, UserActionPrefix) {
  for (const auto& [_, user_action_name] : kAcceleratorTrackerList) {
    EXPECT_TRUE(base::StartsWith(user_action_name, kUserActionPrefix));
  }
}

// Tests AcceleratorTracker will only record key events that are in the
// initialized accelerator tracker map.
TEST_F(AcceleratorTrackerTest, TrackKeyEvent) {
  constexpr ui::KeyboardCode intended_key_code = ui::VKEY_A;
  constexpr ui::EventFlags intended_modifier =
      ui::EF_CONTROL_DOWN | ui::EF_SHIFT_DOWN;
  constexpr base::StringPiece intended_user_action =
      "AccelTracker_Ctrl_Shift_A";

  constexpr TrackerDataActionPair kAcceleratorTrackerListForTesting[] = {
      {{KeyState::PRESSED, intended_key_code, intended_modifier},
       intended_user_action},
  };

  AcceleratorTracker accelerator_tracker(kAcceleratorTrackerListForTesting);
  base::UserActionTester user_action_tester;

  // The metric is not recorded before the event is fired.
  EXPECT_EQ(0, user_action_tester.GetActionCount(intended_user_action));

  // The metric is recorded after the event is fired.
  ui::KeyEvent key_event(ui::ET_KEY_PRESSED, intended_key_code,
                         intended_modifier);
  accelerator_tracker.OnKeyEvent(&key_event);
  EXPECT_EQ(1, user_action_tester.GetActionCount(intended_user_action));

  // Fire a similar key event won't trigger this metric.
  // An event with same key code and modifier, but different key states.
  ui::KeyEvent key_event1(ui::ET_KEY_RELEASED, intended_key_code,
                          intended_modifier);
  accelerator_tracker.OnKeyEvent(&key_event1);
  EXPECT_EQ(1, user_action_tester.GetActionCount(intended_user_action));

  // An event with same key code and key state, but superset of modifiers.
  ui::KeyEvent key_event2(ui::ET_KEY_PRESSED, intended_key_code,
                          intended_modifier | ui::EF_ALT_DOWN);
  accelerator_tracker.OnKeyEvent(&key_event2);
  EXPECT_EQ(1, user_action_tester.GetActionCount(intended_user_action));

  // An event with same key code and key state, but subset of modifiers.
  ui::KeyEvent key_event3(ui::ET_KEY_PRESSED, intended_key_code,
                          ui::EF_CONTROL_DOWN);
  accelerator_tracker.OnKeyEvent(&key_event3);
  EXPECT_EQ(1, user_action_tester.GetActionCount(intended_user_action));

  // An event with same key code and key state, but different modifiers.
  ui::KeyEvent key_event4(ui::ET_KEY_PRESSED, intended_key_code,
                          ui::EF_ALT_DOWN);
  accelerator_tracker.OnKeyEvent(&key_event4);
  EXPECT_EQ(1, user_action_tester.GetActionCount(intended_user_action));

  // An event with same key state and modifiers, but different key codes.
  ui::KeyEvent key_event5(ui::ET_KEY_PRESSED, ui::VKEY_B, intended_modifier);
  accelerator_tracker.OnKeyEvent(&key_event5);
  EXPECT_EQ(1, user_action_tester.GetActionCount(intended_user_action));
}

}  // namespace ash