Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / base / user_activity / user_activity_detector_unittest.cc
blob68d9056c851b901e7a914753d83e567beba826bf
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/base/user_activity/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 "testing/gtest/include/gtest/gtest.h"
11 #include "ui/base/user_activity/user_activity_observer.h"
12 #include "ui/events/event.h"
13 #include "ui/events/event_constants.h"
14 #include "ui/events/event_utils.h"
15 #include "ui/events/keycodes/keyboard_codes.h"
16 #include "ui/events/platform/platform_event_source.h"
17 #include "ui/gfx/geometry/point.h"
19 namespace ui {
21 // Implementation that just counts the number of times we've been told that the
22 // user is active.
23 class TestUserActivityObserver : public UserActivityObserver {
24 public:
25 TestUserActivityObserver() : num_invocations_(0) {}
27 int num_invocations() const { return num_invocations_; }
28 void reset_stats() { num_invocations_ = 0; }
30 // UserActivityObserver implementation.
31 void OnUserActivity(const ui::Event* event) override { num_invocations_++; }
33 private:
34 // Number of times that OnUserActivity() has been called.
35 int num_invocations_;
37 DISALLOW_COPY_AND_ASSIGN(TestUserActivityObserver);
40 // A test implementation of PlatformEventSource that we can instantiate to make
41 // sure that the PlatformEventSource has an instance while in unit tests.
42 class TestPlatformEventSource : public ui::PlatformEventSource {
43 public:
44 TestPlatformEventSource() {}
45 ~TestPlatformEventSource() override {}
47 private:
48 DISALLOW_COPY_AND_ASSIGN(TestPlatformEventSource);
51 class UserActivityDetectorTest : public testing::Test {
52 public:
53 UserActivityDetectorTest()
54 : platform_event_source_(new TestPlatformEventSource),
55 detector_(new UserActivityDetector),
56 observer_(new TestUserActivityObserver) {
57 detector_->AddObserver(observer_.get());
58 now_ = base::TimeTicks::Now();
59 detector_->set_now_for_test(now_);
62 ~UserActivityDetectorTest() override {
63 detector_->RemoveObserver(observer_.get());
66 protected:
67 // Move |detector_|'s idea of the current time forward by |delta|.
68 void AdvanceTime(base::TimeDelta delta) {
69 now_ += delta;
70 detector_->set_now_for_test(now_);
73 void OnEvent(const ui::Event* event) {
74 detector_->ProcessReceivedEvent(event);
77 scoped_ptr<TestPlatformEventSource> platform_event_source_;
78 scoped_ptr<UserActivityDetector> detector_;
79 scoped_ptr<TestUserActivityObserver> observer_;
81 base::TimeTicks now_;
83 private:
84 DISALLOW_COPY_AND_ASSIGN(UserActivityDetectorTest);
87 // Checks that the observer is notified in response to different types of input
88 // events.
89 TEST_F(UserActivityDetectorTest, Basic) {
90 ui::KeyEvent key_event(ui::ET_KEY_PRESSED, ui::VKEY_A, ui::EF_NONE);
91 OnEvent(&key_event);
92 EXPECT_FALSE(key_event.handled());
93 EXPECT_EQ(now_.ToInternalValue(),
94 detector_->last_activity_time().ToInternalValue());
95 EXPECT_EQ(1, observer_->num_invocations());
96 observer_->reset_stats();
98 base::TimeDelta advance_delta = base::TimeDelta::FromMilliseconds(
99 UserActivityDetector::kNotifyIntervalMs);
100 AdvanceTime(advance_delta);
101 ui::MouseEvent mouse_event(ui::ET_MOUSE_MOVED, gfx::Point(), gfx::Point(),
102 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE);
103 OnEvent(&mouse_event);
104 EXPECT_FALSE(mouse_event.handled());
105 EXPECT_EQ(now_.ToInternalValue(),
106 detector_->last_activity_time().ToInternalValue());
107 EXPECT_EQ(1, observer_->num_invocations());
108 observer_->reset_stats();
110 base::TimeTicks time_before_ignore = now_;
112 // Temporarily ignore mouse events when displays are turned on or off.
113 detector_->OnDisplayPowerChanging();
114 OnEvent(&mouse_event);
115 EXPECT_FALSE(mouse_event.handled());
116 EXPECT_EQ(time_before_ignore.ToInternalValue(),
117 detector_->last_activity_time().ToInternalValue());
118 EXPECT_EQ(0, observer_->num_invocations());
119 observer_->reset_stats();
121 const base::TimeDelta kIgnoreMouseTime =
122 base::TimeDelta::FromMilliseconds(
123 UserActivityDetector::kDisplayPowerChangeIgnoreMouseMs);
124 AdvanceTime(kIgnoreMouseTime / 2);
125 OnEvent(&mouse_event);
126 EXPECT_FALSE(mouse_event.handled());
127 EXPECT_EQ(time_before_ignore.ToInternalValue(),
128 detector_->last_activity_time().ToInternalValue());
129 EXPECT_EQ(0, observer_->num_invocations());
130 observer_->reset_stats();
132 // After enough time has passed, mouse events should be reported again.
133 AdvanceTime(std::max(kIgnoreMouseTime, advance_delta));
134 OnEvent(&mouse_event);
135 EXPECT_FALSE(mouse_event.handled());
136 EXPECT_EQ(now_.ToInternalValue(),
137 detector_->last_activity_time().ToInternalValue());
138 EXPECT_EQ(1, observer_->num_invocations());
139 observer_->reset_stats();
141 AdvanceTime(advance_delta);
142 ui::TouchEvent touch_event(
143 ui::ET_TOUCH_PRESSED, gfx::Point(), 0, base::TimeDelta());
144 OnEvent(&touch_event);
145 EXPECT_FALSE(touch_event.handled());
146 EXPECT_EQ(now_.ToInternalValue(),
147 detector_->last_activity_time().ToInternalValue());
148 EXPECT_EQ(1, observer_->num_invocations());
149 observer_->reset_stats();
151 AdvanceTime(advance_delta);
152 ui::GestureEvent gesture_event(
155 ui::EF_NONE,
156 base::TimeDelta::FromMilliseconds(base::Time::Now().ToDoubleT() * 1000),
157 ui::GestureEventDetails(ui::ET_GESTURE_TAP));
158 OnEvent(&gesture_event);
159 EXPECT_FALSE(gesture_event.handled());
160 EXPECT_EQ(now_.ToInternalValue(),
161 detector_->last_activity_time().ToInternalValue());
162 EXPECT_EQ(1, observer_->num_invocations());
163 observer_->reset_stats();
166 // Checks that observers aren't notified too frequently.
167 TEST_F(UserActivityDetectorTest, RateLimitNotifications) {
168 // The observer should be notified about a key event.
169 ui::KeyEvent event(ui::ET_KEY_PRESSED, ui::VKEY_A, ui::EF_NONE);
170 OnEvent(&event);
171 EXPECT_FALSE(event.handled());
172 EXPECT_EQ(1, observer_->num_invocations());
173 observer_->reset_stats();
175 // It shouldn't be notified if a second event occurs in the same instant in
176 // time.
177 OnEvent(&event);
178 EXPECT_FALSE(event.handled());
179 EXPECT_EQ(0, observer_->num_invocations());
180 observer_->reset_stats();
182 // Advance the time, but not quite enough for another notification to be sent.
183 AdvanceTime(
184 base::TimeDelta::FromMilliseconds(
185 UserActivityDetector::kNotifyIntervalMs - 100));
186 OnEvent(&event);
187 EXPECT_FALSE(event.handled());
188 EXPECT_EQ(0, observer_->num_invocations());
189 observer_->reset_stats();
191 // Advance time by the notification interval, definitely moving out of the
192 // rate limit. This should let us trigger another notification.
193 AdvanceTime(base::TimeDelta::FromMilliseconds(
194 UserActivityDetector::kNotifyIntervalMs));
196 OnEvent(&event);
197 EXPECT_FALSE(event.handled());
198 EXPECT_EQ(1, observer_->num_invocations());
201 // Checks that the detector ignores synthetic mouse events.
202 TEST_F(UserActivityDetectorTest, IgnoreSyntheticMouseEvents) {
203 ui::MouseEvent mouse_event(ui::ET_MOUSE_MOVED, gfx::Point(), gfx::Point(),
204 ui::EventTimeForNow(), ui::EF_IS_SYNTHESIZED,
205 ui::EF_NONE);
206 OnEvent(&mouse_event);
207 EXPECT_FALSE(mouse_event.handled());
208 EXPECT_EQ(base::TimeTicks().ToInternalValue(),
209 detector_->last_activity_time().ToInternalValue());
210 EXPECT_EQ(0, observer_->num_invocations());
213 } // namespace ui