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
ash / events / prerewritten_event_forwarder_unittest.cc [blame]
// Copyright 2023 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/events/prerewritten_event_forwarder.h"
#include <memory>
#include "ash/test/ash_test_base.h"
#include "ui/base/ui_base_features.h"
#include "ui/events/event.h"
#include "ui/events/test/test_event_rewriter_continuation.h"
namespace ash {
namespace {
class TestEventRewriterContinuation
: public ui::test::TestEventRewriterContinuation {
public:
TestEventRewriterContinuation() = default;
TestEventRewriterContinuation(const TestEventRewriterContinuation&) = delete;
TestEventRewriterContinuation& operator=(
const TestEventRewriterContinuation&) = delete;
~TestEventRewriterContinuation() override = default;
ui::EventDispatchDetails SendEvent(const ui::Event* event) override {
passthrough_event_ = event->Clone();
return ui::EventDispatchDetails();
}
ui::EventDispatchDetails SendEventFinally(const ui::Event* event) override {
rewritten_event_ = event->Clone();
return ui::EventDispatchDetails();
}
ui::EventDispatchDetails DiscardEvent() override {
return ui::EventDispatchDetails();
}
void Reset() {
passthrough_event_.reset();
rewritten_event_.reset();
}
bool discarded() { return !(passthrough_event_ || rewritten_event_); }
std::unique_ptr<ui::Event> passthrough_event_;
std::unique_ptr<ui::Event> rewritten_event_;
base::WeakPtrFactory<TestEventRewriterContinuation> weak_ptr_factory_{this};
};
class TestKeyObserver : public PrerewrittenEventForwarder::Observer {
public:
TestKeyObserver()
: key_event_(ui::EventType::kUnknown, ui::VKEY_UNKNOWN, ui::EF_NONE) {}
void OnPrerewriteKeyInputEvent(const ui::KeyEvent& key_event) override {
key_event_ = key_event;
}
ui::KeyEvent last_observed_key_event() { return key_event_; }
private:
ui::KeyEvent key_event_;
};
bool AreKeyEventsEqual(const ui::KeyEvent& lhs, const ui::KeyEvent& rhs) {
return lhs.key_code() == rhs.key_code() && lhs.flags() == rhs.flags() &&
lhs.type() == rhs.type();
}
} // namespace
class PrerewrittenEventForwarderTest : public AshTestBase {
public:
PrerewrittenEventForwarderTest() = default;
PrerewrittenEventForwarderTest(const PrerewrittenEventForwarderTest&) =
delete;
PrerewrittenEventForwarderTest& operator=(
const PrerewrittenEventForwarderTest&) = delete;
~PrerewrittenEventForwarderTest() override = default;
// testing::Test:
void SetUp() override {
AshTestBase::SetUp();
observer_ = std::make_unique<TestKeyObserver>();
rewriter_ = std::make_unique<PrerewrittenEventForwarder>();
rewriter_->AddObserver(observer_.get());
}
void TearDown() override {
rewriter_->RemoveObserver(observer_.get());
rewriter_.reset();
observer_.reset();
AshTestBase::TearDown();
}
protected:
std::unique_ptr<TestKeyObserver> observer_;
std::unique_ptr<PrerewrittenEventForwarder> rewriter_;
};
TEST_F(PrerewrittenEventForwarderTest, InitializationTest) {
EXPECT_NE(rewriter_.get(), nullptr);
}
TEST_F(PrerewrittenEventForwarderTest, RawKeyEventObserverCalled) {
TestEventRewriterContinuation continuation;
const ui::KeyEvent expected_key_event(ui::EventType::kKeyPressed, ui::VKEY_A,
/*flags=*/0);
rewriter_->RewriteEvent(expected_key_event,
continuation.weak_ptr_factory_.GetWeakPtr());
EXPECT_FALSE(continuation.discarded());
EXPECT_TRUE(AreKeyEventsEqual(expected_key_event,
observer_->last_observed_key_event()));
continuation.Reset();
const ui::KeyEvent expected_key_event_2(ui::EventType::kKeyReleased,
ui::VKEY_B, ui::EF_COMMAND_DOWN);
rewriter_->RewriteEvent(expected_key_event_2,
continuation.weak_ptr_factory_.GetWeakPtr());
EXPECT_FALSE(continuation.discarded());
EXPECT_TRUE(AreKeyEventsEqual(expected_key_event_2,
observer_->last_observed_key_event()));
}
TEST_F(PrerewrittenEventForwarderTest, IgnoreRepeatKeyEvents) {
TestEventRewriterContinuation continuation;
const ui::KeyEvent expected_key_event(ui::EventType::kKeyPressed, ui::VKEY_A,
ui::EF_IS_REPEAT);
rewriter_->RewriteEvent(expected_key_event,
continuation.weak_ptr_factory_.GetWeakPtr());
EXPECT_FALSE(continuation.discarded());
// Expect the event to not have been fired and therefore the last observed
// key event is empty.
const ui::KeyEvent empty_key_event(ui::EventType::kUnknown, ui::VKEY_UNKNOWN,
ui::EF_NONE);
EXPECT_TRUE(
AreKeyEventsEqual(empty_key_event, observer_->last_observed_key_event()));
}
} // namespace ash