File: accelerator_tracker_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 (85 lines) | stat: -rw-r--r-- 3,709 bytes parent folder | download | duplicates (3)
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
// 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 <string_view>

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

namespace ash {

using AcceleratorTrackerTest = AshTestBase;

constexpr std::string_view 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& [_, metadata] : kAcceleratorTrackerList) {
    EXPECT_TRUE(base::StartsWith(metadata.action_string, 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 std::string_view intended_user_action = "AccelTracker_Ctrl_Shift_A";

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

  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::EventType::kKeyPressed, 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::EventType::kKeyReleased, 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::EventType::kKeyPressed, 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::EventType::kKeyPressed, 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::EventType::kKeyPressed, 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::EventType::kKeyPressed, ui::VKEY_B,
                          intended_modifier);
  accelerator_tracker.OnKeyEvent(&key_event5);
  EXPECT_EQ(1, user_action_tester.GetActionCount(intended_user_action));
}

}  // namespace ash