forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_monitor_unittest.cc
184 lines (143 loc) · 5.66 KB
/
event_monitor_unittest.cc
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
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/views/event_monitor.h"
#include <utility>
#include "base/memory/raw_ptr.h"
#include "ui/events/event_observer.h"
#include "ui/events/test/event_generator.h"
#include "ui/views/test/widget_test.h"
#include "ui/views/widget/widget_utils.h"
#if defined(USE_AURA)
#include "ui/aura/window.h"
#endif
namespace views {
namespace test {
// A simple event observer that records the number of events.
class TestEventObserver : public ui::EventObserver {
public:
TestEventObserver() = default;
TestEventObserver(const TestEventObserver&) = delete;
TestEventObserver& operator=(const TestEventObserver&) = delete;
~TestEventObserver() override = default;
// ui::EventObserver:
void OnEvent(const ui::Event& event) override { ++observed_event_count_; }
size_t observed_event_count() const { return observed_event_count_; }
private:
size_t observed_event_count_ = 0;
};
class EventMonitorTest : public WidgetTest {
public:
EventMonitorTest() = default;
EventMonitorTest(const EventMonitorTest&) = delete;
EventMonitorTest& operator=(const EventMonitorTest&) = delete;
// testing::Test:
void SetUp() override {
WidgetTest::SetUp();
widget_ = CreateTopLevelNativeWidget();
widget_->SetSize(gfx::Size(100, 100));
widget_->Show();
generator_ = std::make_unique<ui::test::EventGenerator>(
GetContext(), widget_->GetNativeWindow());
generator_->set_target(ui::test::EventGenerator::Target::APPLICATION);
}
void TearDown() override {
widget_->CloseNow();
WidgetTest::TearDown();
}
protected:
raw_ptr<Widget> widget_ = nullptr;
std::unique_ptr<ui::test::EventGenerator> generator_;
TestEventObserver observer_;
};
TEST_F(EventMonitorTest, ShouldReceiveAppEventsWhileInstalled) {
std::unique_ptr<EventMonitor> monitor(EventMonitor::CreateApplicationMonitor(
&observer_, widget_->GetNativeWindow(),
{ui::ET_MOUSE_PRESSED, ui::ET_MOUSE_RELEASED}));
generator_->ClickLeftButton();
EXPECT_EQ(2u, observer_.observed_event_count());
monitor.reset();
generator_->ClickLeftButton();
EXPECT_EQ(2u, observer_.observed_event_count());
}
TEST_F(EventMonitorTest, ShouldReceiveWindowEventsWhileInstalled) {
std::unique_ptr<EventMonitor> monitor(EventMonitor::CreateWindowMonitor(
&observer_, widget_->GetNativeWindow(),
{ui::ET_MOUSE_PRESSED, ui::ET_MOUSE_RELEASED}));
generator_->ClickLeftButton();
EXPECT_EQ(2u, observer_.observed_event_count());
monitor.reset();
generator_->ClickLeftButton();
EXPECT_EQ(2u, observer_.observed_event_count());
}
TEST_F(EventMonitorTest, ShouldNotReceiveEventsFromOtherWindow) {
Widget* widget2 = CreateTopLevelNativeWidget();
std::unique_ptr<EventMonitor> monitor(EventMonitor::CreateWindowMonitor(
&observer_, widget2->GetNativeWindow(),
{ui::ET_MOUSE_PRESSED, ui::ET_MOUSE_RELEASED}));
generator_->ClickLeftButton();
EXPECT_EQ(0u, observer_.observed_event_count());
monitor.reset();
widget2->CloseNow();
}
TEST_F(EventMonitorTest, ShouldOnlyReceiveRequestedEventTypes) {
// This event monitor only listens to mouse press, not release.
std::unique_ptr<EventMonitor> monitor(EventMonitor::CreateWindowMonitor(
&observer_, widget_->GetNativeWindow(), {ui::ET_MOUSE_PRESSED}));
generator_->ClickLeftButton();
EXPECT_EQ(1u, observer_.observed_event_count());
monitor.reset();
}
TEST_F(EventMonitorTest, WindowMonitorTornDownOnWindowClose) {
Widget* widget2 = CreateTopLevelNativeWidget();
widget2->Show();
std::unique_ptr<EventMonitor> monitor(EventMonitor::CreateWindowMonitor(
&observer_, widget2->GetNativeWindow(), {ui::ET_MOUSE_PRESSED}));
// Closing the widget before destroying the monitor should not crash.
widget2->CloseNow();
monitor.reset();
}
namespace {
class DeleteOtherOnEventObserver : public ui::EventObserver {
public:
explicit DeleteOtherOnEventObserver(gfx::NativeWindow context) {
monitor_ = EventMonitor::CreateApplicationMonitor(
this, context, {ui::ET_MOUSE_PRESSED, ui::ET_MOUSE_RELEASED});
}
DeleteOtherOnEventObserver(const DeleteOtherOnEventObserver&) = delete;
DeleteOtherOnEventObserver& operator=(const DeleteOtherOnEventObserver&) =
delete;
bool DidDelete() const { return !observer_to_delete_; }
void set_monitor_to_delete(
std::unique_ptr<DeleteOtherOnEventObserver> observer_to_delete) {
observer_to_delete_ = std::move(observer_to_delete);
}
// ui::EventObserver:
void OnEvent(const ui::Event& event) override {
observer_to_delete_ = nullptr;
}
private:
std::unique_ptr<EventMonitor> monitor_;
std::unique_ptr<DeleteOtherOnEventObserver> observer_to_delete_;
};
} // namespace
// Ensure correct behavior when an event monitor is removed while iterating
// over the OS-controlled observer list.
TEST_F(EventMonitorTest, TwoMonitors) {
gfx::NativeWindow window = widget_->GetNativeWindow();
auto deleter = std::make_unique<DeleteOtherOnEventObserver>(window);
auto deletee = std::make_unique<DeleteOtherOnEventObserver>(window);
deleter->set_monitor_to_delete(std::move(deletee));
EXPECT_FALSE(deleter->DidDelete());
generator_->PressLeftButton();
EXPECT_TRUE(deleter->DidDelete());
// Now try setting up observers in the alternate order.
deletee = std::make_unique<DeleteOtherOnEventObserver>(window);
deleter = std::make_unique<DeleteOtherOnEventObserver>(window);
deleter->set_monitor_to_delete(std::move(deletee));
EXPECT_FALSE(deleter->DidDelete());
generator_->ReleaseLeftButton();
EXPECT_TRUE(deleter->DidDelete());
}
} // namespace test
} // namespace views