File: touch_accessibility_enabler_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 (187 lines) | stat: -rw-r--r-- 6,323 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
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
// Copyright 2016 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/accessibility/chromevox/touch_accessibility_enabler.h"

#include <memory>

#include "ash/accessibility/chromevox/mock_touch_exploration_controller_delegate.h"
#include "ash/accessibility/chromevox/touch_exploration_controller.h"
#include "base/test/simple_test_tick_clock.h"
#include "base/time/time.h"
#include "ui/aura/test/aura_test_base.h"
#include "ui/aura/window.h"
#include "ui/events/event.h"
#include "ui/events/event_utils.h"
#include "ui/events/gestures/gesture_provider_aura.h"
#include "ui/events/test/event_generator.h"
#include "ui/events/test/events_test_utils.h"
#include "ui/gfx/geometry/point.h"

namespace ash {

namespace {

class MockTouchAccessibilityEnablerDelegate
    : public TouchAccessibilityEnablerDelegate {
 public:
  MockTouchAccessibilityEnablerDelegate() {}

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

  ~MockTouchAccessibilityEnablerDelegate() override {}

  void ToggleSpokenFeedback() override { toggle_spoken_feedback_ = true; }
  bool toggle_spoken_feedback() const { return toggle_spoken_feedback_; }

 private:
  bool toggle_spoken_feedback_ = false;
};

class TouchAccessibilityEnablerTest : public aura::test::AuraTestBase {
 public:
  TouchAccessibilityEnablerTest() {}

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

  ~TouchAccessibilityEnablerTest() override {}

  void SetUp() override {
    aura::test::AuraTestBase::SetUp();

    generator_ = std::make_unique<ui::test::EventGenerator>(root_window());

    // Tests fail if time is ever 0.
    simulated_clock_.Advance(base::Milliseconds(10));
    ui::SetEventTickClockForTesting(&simulated_clock_);

    enabler_ =
        std::make_unique<TouchAccessibilityEnabler>(root_window(), &delegate_);
  }

  void TearDown() override {
    enabler_.reset(nullptr);
    ui::SetEventTickClockForTesting(nullptr);
    aura::test::AuraTestBase::TearDown();
  }

 protected:
  base::TimeTicks Now() {
    // This is the same as what EventTimeForNow() does, but here we do it
    // with our simulated clock.
    return simulated_clock_.NowTicks();
  }

  std::unique_ptr<ui::test::EventGenerator> generator_;
  base::SimpleTestTickClock simulated_clock_;
  MockTouchAccessibilityEnablerDelegate delegate_;
  std::unique_ptr<TouchAccessibilityEnabler> enabler_;
  ui::GestureDetector::Config gesture_detector_config_;
};

}  // namespace

TEST_F(TouchAccessibilityEnablerTest, InteractsWithTouchExplorationController) {
  // This test ensures that if TouchExplorationController starts and stops,
  // TouchAccessibilityEnabler continues to work correctly. Because
  // TouchExplorationController rewrites most touch events, it can screw up
  // TouchAccessibilityEnabler if they don't explicitly coordinate.

  MockTouchExplorationControllerDelegate delegate;
  std::unique_ptr<TouchExplorationController> controller(
      new TouchExplorationController(root_window(), &delegate,
                                     enabler_->GetWeakPtr()));

  EXPECT_TRUE(enabler_->IsInNoFingersDownForTesting());
  generator_->set_current_screen_location(gfx::Point(11, 12));
  generator_->PressTouchId(1);

  simulated_clock_.Advance(base::Milliseconds(500));

  generator_->set_current_screen_location(gfx::Point(22, 34));
  generator_->PressTouchId(2);

  EXPECT_TRUE(enabler_->IsInTwoFingersDownForTesting());

  controller.reset();

  generator_->ReleaseTouchId(1);
  generator_->ReleaseTouchId(2);
  EXPECT_TRUE(enabler_->IsInNoFingersDownForTesting());
}

TEST_F(TouchAccessibilityEnablerTest, EntersOneFingerDownMode) {
  EXPECT_TRUE(enabler_->IsInNoFingersDownForTesting());
  EXPECT_FALSE(enabler_->IsInOneFingerDownForTesting());
  generator_->set_current_screen_location(gfx::Point(11, 12));
  generator_->PressTouch();

  EXPECT_FALSE(enabler_->IsInNoFingersDownForTesting());
  EXPECT_TRUE(enabler_->IsInOneFingerDownForTesting());
}

TEST_F(TouchAccessibilityEnablerTest, EntersTwoFingersDownMode) {
  EXPECT_TRUE(enabler_->IsInNoFingersDownForTesting());
  generator_->set_current_screen_location(gfx::Point(11, 12));
  generator_->PressTouchId(1);

  generator_->set_current_screen_location(gfx::Point(22, 34));
  generator_->PressTouchId(2);

  EXPECT_TRUE(enabler_->IsInTwoFingersDownForTesting());
}

TEST_F(TouchAccessibilityEnablerTest, TogglesSpokenFeedback) {
  EXPECT_TRUE(enabler_->IsInNoFingersDownForTesting());
  generator_->set_current_screen_location(gfx::Point(11, 12));
  generator_->PressTouchId(1);

  generator_->set_current_screen_location(gfx::Point(22, 34));
  generator_->PressTouchId(2);

  EXPECT_TRUE(enabler_->IsInTwoFingersDownForTesting());
  EXPECT_FALSE(delegate_.toggle_spoken_feedback());

  simulated_clock_.Advance(base::Milliseconds(5000));
  enabler_->TriggerOnTimerForTesting();
  EXPECT_TRUE(delegate_.toggle_spoken_feedback());
}

TEST_F(TouchAccessibilityEnablerTest, ThreeFingersCancelsDetection) {
  EXPECT_TRUE(enabler_->IsInNoFingersDownForTesting());
  generator_->set_current_screen_location(gfx::Point(11, 12));
  generator_->PressTouchId(1);

  generator_->set_current_screen_location(gfx::Point(22, 34));
  generator_->PressTouchId(2);

  EXPECT_TRUE(enabler_->IsInTwoFingersDownForTesting());

  generator_->set_current_screen_location(gfx::Point(33, 56));
  generator_->PressTouchId(3);

  EXPECT_TRUE(enabler_->IsInWaitForNoFingersForTesting());
}

TEST_F(TouchAccessibilityEnablerTest, MovingFingerPastSlopCancelsDetection) {
  EXPECT_TRUE(enabler_->IsInNoFingersDownForTesting());
  generator_->set_current_screen_location(gfx::Point(11, 12));
  generator_->PressTouch();

  int slop = gesture_detector_config_.double_tap_slop;
  int half_slop = slop / 2;

  generator_->MoveTouch(gfx::Point(11 + half_slop, 12));
  EXPECT_TRUE(enabler_->IsInOneFingerDownForTesting());

  generator_->MoveTouch(gfx::Point(11 + slop + 1, 12));
  EXPECT_TRUE(enabler_->IsInWaitForNoFingersForTesting());
}

}  // namespace ash