1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/wm/core/user_activity_detector.h"
7 #include "base/compiler_specific.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/time/time.h"
10 #include "ui/aura/test/aura_test_base.h"
11 #include "ui/events/event.h"
12 #include "ui/events/event_constants.h"
13 #include "ui/events/keycodes/keyboard_codes.h"
14 #include "ui/gfx/geometry/point.h"
15 #include "ui/wm/core/user_activity_observer.h"
19 // Implementation that just counts the number of times we've been told that the
21 class TestUserActivityObserver
: public UserActivityObserver
{
23 TestUserActivityObserver() : num_invocations_(0) {}
25 int num_invocations() const { return num_invocations_
; }
26 void reset_stats() { num_invocations_
= 0; }
28 // UserActivityObserver implementation.
29 void OnUserActivity(const ui::Event
* event
) override
{ num_invocations_
++; }
32 // Number of times that OnUserActivity() has been called.
35 DISALLOW_COPY_AND_ASSIGN(TestUserActivityObserver
);
38 class UserActivityDetectorTest
: public aura::test::AuraTestBase
{
40 UserActivityDetectorTest() {}
41 ~UserActivityDetectorTest() override
{}
43 void SetUp() override
{
44 AuraTestBase::SetUp();
45 observer_
.reset(new TestUserActivityObserver
);
46 detector_
.reset(new UserActivityDetector
);
47 detector_
->AddObserver(observer_
.get());
49 now_
= base::TimeTicks::Now();
50 detector_
->set_now_for_test(now_
);
53 void TearDown() override
{
54 detector_
->RemoveObserver(observer_
.get());
55 AuraTestBase::TearDown();
59 // Move |detector_|'s idea of the current time forward by |delta|.
60 void AdvanceTime(base::TimeDelta delta
) {
62 detector_
->set_now_for_test(now_
);
65 scoped_ptr
<UserActivityDetector
> detector_
;
66 scoped_ptr
<TestUserActivityObserver
> observer_
;
71 DISALLOW_COPY_AND_ASSIGN(UserActivityDetectorTest
);
74 // Checks that the observer is notified in response to different types of input
76 TEST_F(UserActivityDetectorTest
, Basic
) {
77 ui::KeyEvent
key_event(ui::ET_KEY_PRESSED
, ui::VKEY_A
, ui::EF_NONE
);
78 detector_
->OnKeyEvent(&key_event
);
79 EXPECT_FALSE(key_event
.handled());
80 EXPECT_EQ(now_
.ToInternalValue(),
81 detector_
->last_activity_time().ToInternalValue());
82 EXPECT_EQ(1, observer_
->num_invocations());
83 observer_
->reset_stats();
85 base::TimeDelta advance_delta
= base::TimeDelta::FromMilliseconds(
86 UserActivityDetector::kNotifyIntervalMs
);
87 AdvanceTime(advance_delta
);
88 ui::MouseEvent
mouse_event(
89 ui::ET_MOUSE_MOVED
, gfx::Point(), gfx::Point(), ui::EF_NONE
, ui::EF_NONE
);
90 detector_
->OnMouseEvent(&mouse_event
);
91 EXPECT_FALSE(mouse_event
.handled());
92 EXPECT_EQ(now_
.ToInternalValue(),
93 detector_
->last_activity_time().ToInternalValue());
94 EXPECT_EQ(1, observer_
->num_invocations());
95 observer_
->reset_stats();
97 base::TimeTicks time_before_ignore
= now_
;
99 // Temporarily ignore mouse events when displays are turned on or off.
100 detector_
->OnDisplayPowerChanging();
101 detector_
->OnMouseEvent(&mouse_event
);
102 EXPECT_FALSE(mouse_event
.handled());
103 EXPECT_EQ(time_before_ignore
.ToInternalValue(),
104 detector_
->last_activity_time().ToInternalValue());
105 EXPECT_EQ(0, observer_
->num_invocations());
106 observer_
->reset_stats();
108 const base::TimeDelta kIgnoreMouseTime
=
109 base::TimeDelta::FromMilliseconds(
110 UserActivityDetector::kDisplayPowerChangeIgnoreMouseMs
);
111 AdvanceTime(kIgnoreMouseTime
/ 2);
112 detector_
->OnMouseEvent(&mouse_event
);
113 EXPECT_FALSE(mouse_event
.handled());
114 EXPECT_EQ(time_before_ignore
.ToInternalValue(),
115 detector_
->last_activity_time().ToInternalValue());
116 EXPECT_EQ(0, observer_
->num_invocations());
117 observer_
->reset_stats();
119 // After enough time has passed, mouse events should be reported again.
120 AdvanceTime(std::max(kIgnoreMouseTime
, advance_delta
));
121 detector_
->OnMouseEvent(&mouse_event
);
122 EXPECT_FALSE(mouse_event
.handled());
123 EXPECT_EQ(now_
.ToInternalValue(),
124 detector_
->last_activity_time().ToInternalValue());
125 EXPECT_EQ(1, observer_
->num_invocations());
126 observer_
->reset_stats();
128 AdvanceTime(advance_delta
);
129 ui::TouchEvent
touch_event(
130 ui::ET_TOUCH_PRESSED
, gfx::Point(), 0, base::TimeDelta());
131 detector_
->OnTouchEvent(&touch_event
);
132 EXPECT_FALSE(touch_event
.handled());
133 EXPECT_EQ(now_
.ToInternalValue(),
134 detector_
->last_activity_time().ToInternalValue());
135 EXPECT_EQ(1, observer_
->num_invocations());
136 observer_
->reset_stats();
138 AdvanceTime(advance_delta
);
139 ui::GestureEvent
gesture_event(
143 base::TimeDelta::FromMilliseconds(base::Time::Now().ToDoubleT() * 1000),
144 ui::GestureEventDetails(ui::ET_GESTURE_TAP
));
145 detector_
->OnGestureEvent(&gesture_event
);
146 EXPECT_FALSE(gesture_event
.handled());
147 EXPECT_EQ(now_
.ToInternalValue(),
148 detector_
->last_activity_time().ToInternalValue());
149 EXPECT_EQ(1, observer_
->num_invocations());
150 observer_
->reset_stats();
153 // Checks that observers aren't notified too frequently.
154 TEST_F(UserActivityDetectorTest
, RateLimitNotifications
) {
155 // The observer should be notified about a key event.
156 ui::KeyEvent
event(ui::ET_KEY_PRESSED
, ui::VKEY_A
, ui::EF_NONE
);
157 detector_
->OnKeyEvent(&event
);
158 EXPECT_FALSE(event
.handled());
159 EXPECT_EQ(1, observer_
->num_invocations());
160 observer_
->reset_stats();
162 // It shouldn't be notified if a second event occurs in the same instant in
164 detector_
->OnKeyEvent(&event
);
165 EXPECT_FALSE(event
.handled());
166 EXPECT_EQ(0, observer_
->num_invocations());
167 observer_
->reset_stats();
169 // Advance the time, but not quite enough for another notification to be sent.
171 base::TimeDelta::FromMilliseconds(
172 UserActivityDetector::kNotifyIntervalMs
- 100));
173 detector_
->OnKeyEvent(&event
);
174 EXPECT_FALSE(event
.handled());
175 EXPECT_EQ(0, observer_
->num_invocations());
176 observer_
->reset_stats();
178 // Advance time by the notification interval, definitely moving out of the
179 // rate limit. This should let us trigger another notification.
180 AdvanceTime(base::TimeDelta::FromMilliseconds(
181 UserActivityDetector::kNotifyIntervalMs
));
183 detector_
->OnKeyEvent(&event
);
184 EXPECT_FALSE(event
.handled());
185 EXPECT_EQ(1, observer_
->num_invocations());
188 // Checks that the detector ignores synthetic mouse events.
189 TEST_F(UserActivityDetectorTest
, IgnoreSyntheticMouseEvents
) {
190 ui::MouseEvent
mouse_event(
191 ui::ET_MOUSE_MOVED
, gfx::Point(), gfx::Point(), ui::EF_IS_SYNTHESIZED
,
193 detector_
->OnMouseEvent(&mouse_event
);
194 EXPECT_FALSE(mouse_event
.handled());
195 EXPECT_EQ(base::TimeTicks().ToInternalValue(),
196 detector_
->last_activity_time().ToInternalValue());
197 EXPECT_EQ(0, observer_
->num_invocations());