File: touch_ui_controller_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; 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,806; 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 (193 lines) | stat: -rw-r--r-- 8,088 bytes parent folder | download | duplicates (8)
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
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <string>

#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/pointer/mock_touch_ui_controller.h"

namespace ui {

namespace {

class TestObserver {
 public:
  explicit TestObserver(TouchUiController* controller)
      : subscription_(controller->RegisterCallback(
            base::BindLambdaForTesting([this]() { ++touch_ui_changes_; }))) {}
  ~TestObserver() = default;

  int touch_ui_changes() const { return touch_ui_changes_; }

 private:
  int touch_ui_changes_ = 0;
  base::CallbackListSubscription subscription_;
};

class TouchUiControllerTest : public testing::Test {
 public:
  using TouchUiState = ::ui::TouchUiController::TouchUiState;

  void RunUntilIdle() { task_environment_.RunUntilIdle(); }

 private:
  base::test::TaskEnvironment task_environment_{
      base::test::TaskEnvironment::MainThreadType::UI};
};

}  // namespace

// Verifies that non-touch is the default.
TEST_F(TouchUiControllerTest, DefaultIsNonTouch) {
  MockTouchUiController controller;
  EXPECT_FALSE(controller.touch_ui());
}

// Verifies that kDisabled maps to non-touch.
TEST_F(TouchUiControllerTest, DisabledIsNonTouch) {
  MockTouchUiController controller(TouchUiState::kDisabled);
  EXPECT_FALSE(controller.touch_ui());
}

// Verifies that kAuto maps to non-touch (the default).
TEST_F(TouchUiControllerTest, AutoIsNonTouch) {
  MockTouchUiController controller(TouchUiState::kAuto);
  EXPECT_FALSE(controller.touch_ui());
}

// Verifies that kEnabled maps to touch.
TEST_F(TouchUiControllerTest, EnabledIsNonTouch) {
  MockTouchUiController controller(TouchUiState::kEnabled);
  EXPECT_TRUE(controller.touch_ui());
}

// Verifies that when the mode is set to non-touch and the tablet mode toggles,
// the touch UI state does not change.
TEST_F(TouchUiControllerTest, TabletToggledOnTouchUiDisabled) {
  MockTouchUiController controller(TouchUiState::kDisabled);
  TestObserver observer(&controller);

  controller.OnTabletModeToggled(true);
  EXPECT_FALSE(controller.touch_ui());
  EXPECT_EQ(0, observer.touch_ui_changes());

  controller.OnTabletModeToggled(false);
  EXPECT_FALSE(controller.touch_ui());
  EXPECT_EQ(0, observer.touch_ui_changes());
}

// Verifies that when the mode is set to auto and the tablet mode toggles, the
// touch UI state changes and the observer gets called back.
TEST_F(TouchUiControllerTest, TabletToggledOnTouchUiAuto) {
  MockTouchUiController controller(TouchUiState::kAuto);
  TestObserver observer(&controller);

  controller.OnTabletModeToggled(true);
  EXPECT_TRUE(controller.touch_ui());
  EXPECT_EQ(1, observer.touch_ui_changes());

  controller.OnTabletModeToggled(false);
  EXPECT_FALSE(controller.touch_ui());
  EXPECT_EQ(2, observer.touch_ui_changes());
}

#if BUILDFLAG(USE_BLINK)
TEST_F(TouchUiControllerTest, DetectPointerDevices) {
  constexpr const char kOnStartupHistogram[] = "Input.Digitizer.OnStartup";
  constexpr const char kOnConnectedHistogram[] = "Input.Digitizer.OnConnected";
  constexpr const char kOnDisconnectedHistogram[] =
      "Input.Digitizer.OnDisconnected";
  constexpr const char kMaxTouchPointsDirectPenHistogram[] =
      "Input.Digitizer.MaxTouchPoints.DirectPen";
  constexpr const char kMaxTouchPointsIndirectPenHistogram[] =
      "Input.Digitizer.MaxTouchPoints.IndirectPen";
  constexpr const char kMaxTouchPointsTouchHistogram[] =
      "Input.Digitizer.MaxTouchPoints.Touch";
  constexpr const char kMaxTouchPointsTouchPadHistogram[] =
      "Input.Digitizer.MaxTouchPoints.TouchPad";
  constexpr const char kMaxTouchPointsSupportedBySystemAtStartupHistogram[] =
      "Input.Digitizer.MaxTouchPointsSupportedBySystemAtStartup";

  static const PointerDevice kDirectPenDevice = {
      .key = PointerDevice::Key(0),
      .digitizer = PointerDigitizerType::kDirectPen,
      .max_active_contacts = 11};
  static const PointerDevice kIndirectPenDevice = {
      .key = PointerDevice::Key(1),
      .digitizer = PointerDigitizerType::kIndirectPen,
      .max_active_contacts = 20};
  static const PointerDevice kTouchKeyDevice = {
      .key = PointerDevice::Key(2),
      .digitizer = PointerDigitizerType::kTouch,
      .max_active_contacts = 1};
  static const PointerDevice kTouchPadDevice = {
      .key = PointerDevice::Key(3),
      .digitizer = PointerDigitizerType::kTouchPad,
      .max_active_contacts = 5};
  base::HistogramTester histogram_tester;

  // Expected to fire histograms for digitizer(s) found at startup,
  // per-digitizer type max touch points, and aggregate max touch points.
  MockTouchUiController controller(TouchUiState::kAuto);
  controller.SetMockConnectedPointerDevices((std::vector<PointerDevice>{
      kTouchPadDevice, kDirectPenDevice, kTouchKeyDevice}));
  RunUntilIdle();
  histogram_tester.ExpectTotalCount(kOnStartupHistogram, 3);
  histogram_tester.ExpectBucketCount(kOnStartupHistogram,
                                     PointerDigitizerType::kDirectPen, 1);
  histogram_tester.ExpectBucketCount(kOnStartupHistogram,
                                     PointerDigitizerType::kTouch, 1);
  histogram_tester.ExpectBucketCount(kOnStartupHistogram,
                                     PointerDigitizerType::kTouchPad, 1);
  histogram_tester.ExpectTotalCount(kMaxTouchPointsDirectPenHistogram, 1);
  histogram_tester.ExpectBucketCount(kMaxTouchPointsDirectPenHistogram, 11, 1);
  histogram_tester.ExpectTotalCount(kMaxTouchPointsTouchHistogram, 1);
  histogram_tester.ExpectBucketCount(kMaxTouchPointsTouchHistogram, 1, 1);
  histogram_tester.ExpectTotalCount(kMaxTouchPointsTouchPadHistogram, 1);
  histogram_tester.ExpectBucketCount(kMaxTouchPointsTouchPadHistogram, 5, 1);
  histogram_tester.ExpectTotalCount(
      kMaxTouchPointsSupportedBySystemAtStartupHistogram, 1);
  histogram_tester.ExpectBucketCount(
      kMaxTouchPointsSupportedBySystemAtStartupHistogram, 11, 1);

  // Simulate receiving platform specific notifications that pointer devices
  // have been connected or disconnected.
  // Expected to fire histograms for digitizer(s) connected or disconnected,
  // and per-digitizer type max touch points for newly connected devices.
  controller.SetMockConnectedPointerDevices(
      {kTouchPadDevice, kIndirectPenDevice});
  controller.OnPointerDeviceDisconnected(kDirectPenDevice.key);
  controller.OnPointerDeviceDisconnected(kTouchKeyDevice.key);
  controller.OnPointerDeviceConnected(kIndirectPenDevice.key);
  EXPECT_EQ(controller.GetLastKnownPointerDevicesForTesting(),
            (std::vector<PointerDevice>{kTouchPadDevice, kIndirectPenDevice}));

  histogram_tester.ExpectTotalCount(kOnConnectedHistogram, 1);
  histogram_tester.ExpectBucketCount(kOnConnectedHistogram,
                                     PointerDigitizerType::kIndirectPen, 1);
  histogram_tester.ExpectTotalCount(kOnDisconnectedHistogram, 2);
  histogram_tester.ExpectBucketCount(kOnDisconnectedHistogram,
                                     PointerDigitizerType::kTouch, 1);
  histogram_tester.ExpectBucketCount(kOnDisconnectedHistogram,
                                     PointerDigitizerType::kDirectPen, 1);
  histogram_tester.ExpectTotalCount(kMaxTouchPointsIndirectPenHistogram, 1);
  histogram_tester.ExpectBucketCount(kMaxTouchPointsIndirectPenHistogram, 20,
                                     1);

  // The following aren't affected by the events above.
  histogram_tester.ExpectTotalCount(kMaxTouchPointsDirectPenHistogram, 1);
  histogram_tester.ExpectTotalCount(kMaxTouchPointsTouchHistogram, 1);
  histogram_tester.ExpectTotalCount(kMaxTouchPointsTouchPadHistogram, 1);

  // The following should never change after their initially logged at startup.
  histogram_tester.ExpectTotalCount(kOnStartupHistogram, 3);
  histogram_tester.ExpectTotalCount(
      kMaxTouchPointsSupportedBySystemAtStartupHistogram, 1);
}
#endif  // BUILDFLAG(USE_BLINK)

}  // namespace ui