Drive: Add BatchableRequest subclass.
[chromium-blink-merge.git] / ui / base / user_activity / user_activity_detector_unittest.cc
blobdc4cbae20ce7579ead297d40a46b9c6c1ba455cf
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/gfx/geometry/point.h"
18 namespace ui {
20 // Implementation that just counts the number of times we've been told that the
21 // user is active.
22 class TestUserActivityObserver : public UserActivityObserver {
23 public:
24 TestUserActivityObserver() : num_invocations_(0) {}
26 int num_invocations() const { return num_invocations_; }
27 void reset_stats() { num_invocations_ = 0; }
29 // UserActivityObserver implementation.
30 void OnUserActivity(const ui::Event* event) override { num_invocations_++; }
32 private:
33 // Number of times that OnUserActivity() has been called.
34 int num_invocations_;
36 DISALLOW_COPY_AND_ASSIGN(TestUserActivityObserver);
39 class UserActivityDetectorTest : public testing::Test {
40 public:
41 UserActivityDetectorTest()
42 : detector_(new UserActivityDetector),
43 observer_(new TestUserActivityObserver) {
44 detector_->AddObserver(observer_.get());
45 now_ = base::TimeTicks::Now();
46 detector_->set_now_for_test(now_);
49 ~UserActivityDetectorTest() override {
50 detector_->RemoveObserver(observer_.get());
53 protected:
54 // Move |detector_|'s idea of the current time forward by |delta|.
55 void AdvanceTime(base::TimeDelta delta) {
56 now_ += delta;
57 detector_->set_now_for_test(now_);
60 scoped_ptr<UserActivityDetector> detector_;
61 scoped_ptr<TestUserActivityObserver> observer_;
63 base::TimeTicks now_;
65 private:
66 DISALLOW_COPY_AND_ASSIGN(UserActivityDetectorTest);
69 // Checks that the observer is notified in response to different types of input
70 // events.
71 TEST_F(UserActivityDetectorTest, Basic) {
72 ui::KeyEvent key_event(ui::ET_KEY_PRESSED, ui::VKEY_A, ui::EF_NONE);
73 detector_->OnKeyEvent(&key_event);
74 EXPECT_FALSE(key_event.handled());
75 EXPECT_EQ(now_.ToInternalValue(),
76 detector_->last_activity_time().ToInternalValue());
77 EXPECT_EQ(1, observer_->num_invocations());
78 observer_->reset_stats();
80 base::TimeDelta advance_delta = base::TimeDelta::FromMilliseconds(
81 UserActivityDetector::kNotifyIntervalMs);
82 AdvanceTime(advance_delta);
83 ui::MouseEvent mouse_event(ui::ET_MOUSE_MOVED, gfx::Point(), gfx::Point(),
84 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE);
85 detector_->OnMouseEvent(&mouse_event);
86 EXPECT_FALSE(mouse_event.handled());
87 EXPECT_EQ(now_.ToInternalValue(),
88 detector_->last_activity_time().ToInternalValue());
89 EXPECT_EQ(1, observer_->num_invocations());
90 observer_->reset_stats();
92 base::TimeTicks time_before_ignore = now_;
94 // Temporarily ignore mouse events when displays are turned on or off.
95 detector_->OnDisplayPowerChanging();
96 detector_->OnMouseEvent(&mouse_event);
97 EXPECT_FALSE(mouse_event.handled());
98 EXPECT_EQ(time_before_ignore.ToInternalValue(),
99 detector_->last_activity_time().ToInternalValue());
100 EXPECT_EQ(0, observer_->num_invocations());
101 observer_->reset_stats();
103 const base::TimeDelta kIgnoreMouseTime =
104 base::TimeDelta::FromMilliseconds(
105 UserActivityDetector::kDisplayPowerChangeIgnoreMouseMs);
106 AdvanceTime(kIgnoreMouseTime / 2);
107 detector_->OnMouseEvent(&mouse_event);
108 EXPECT_FALSE(mouse_event.handled());
109 EXPECT_EQ(time_before_ignore.ToInternalValue(),
110 detector_->last_activity_time().ToInternalValue());
111 EXPECT_EQ(0, observer_->num_invocations());
112 observer_->reset_stats();
114 // After enough time has passed, mouse events should be reported again.
115 AdvanceTime(std::max(kIgnoreMouseTime, advance_delta));
116 detector_->OnMouseEvent(&mouse_event);
117 EXPECT_FALSE(mouse_event.handled());
118 EXPECT_EQ(now_.ToInternalValue(),
119 detector_->last_activity_time().ToInternalValue());
120 EXPECT_EQ(1, observer_->num_invocations());
121 observer_->reset_stats();
123 AdvanceTime(advance_delta);
124 ui::TouchEvent touch_event(
125 ui::ET_TOUCH_PRESSED, gfx::Point(), 0, base::TimeDelta());
126 detector_->OnTouchEvent(&touch_event);
127 EXPECT_FALSE(touch_event.handled());
128 EXPECT_EQ(now_.ToInternalValue(),
129 detector_->last_activity_time().ToInternalValue());
130 EXPECT_EQ(1, observer_->num_invocations());
131 observer_->reset_stats();
133 AdvanceTime(advance_delta);
134 ui::GestureEvent gesture_event(
137 ui::EF_NONE,
138 base::TimeDelta::FromMilliseconds(base::Time::Now().ToDoubleT() * 1000),
139 ui::GestureEventDetails(ui::ET_GESTURE_TAP));
140 detector_->OnGestureEvent(&gesture_event);
141 EXPECT_FALSE(gesture_event.handled());
142 EXPECT_EQ(now_.ToInternalValue(),
143 detector_->last_activity_time().ToInternalValue());
144 EXPECT_EQ(1, observer_->num_invocations());
145 observer_->reset_stats();
148 // Checks that observers aren't notified too frequently.
149 TEST_F(UserActivityDetectorTest, RateLimitNotifications) {
150 // The observer should be notified about a key event.
151 ui::KeyEvent event(ui::ET_KEY_PRESSED, ui::VKEY_A, ui::EF_NONE);
152 detector_->OnKeyEvent(&event);
153 EXPECT_FALSE(event.handled());
154 EXPECT_EQ(1, observer_->num_invocations());
155 observer_->reset_stats();
157 // It shouldn't be notified if a second event occurs in the same instant in
158 // time.
159 detector_->OnKeyEvent(&event);
160 EXPECT_FALSE(event.handled());
161 EXPECT_EQ(0, observer_->num_invocations());
162 observer_->reset_stats();
164 // Advance the time, but not quite enough for another notification to be sent.
165 AdvanceTime(
166 base::TimeDelta::FromMilliseconds(
167 UserActivityDetector::kNotifyIntervalMs - 100));
168 detector_->OnKeyEvent(&event);
169 EXPECT_FALSE(event.handled());
170 EXPECT_EQ(0, observer_->num_invocations());
171 observer_->reset_stats();
173 // Advance time by the notification interval, definitely moving out of the
174 // rate limit. This should let us trigger another notification.
175 AdvanceTime(base::TimeDelta::FromMilliseconds(
176 UserActivityDetector::kNotifyIntervalMs));
178 detector_->OnKeyEvent(&event);
179 EXPECT_FALSE(event.handled());
180 EXPECT_EQ(1, observer_->num_invocations());
183 // Checks that the detector ignores synthetic mouse events.
184 TEST_F(UserActivityDetectorTest, IgnoreSyntheticMouseEvents) {
185 ui::MouseEvent mouse_event(ui::ET_MOUSE_MOVED, gfx::Point(), gfx::Point(),
186 ui::EventTimeForNow(), ui::EF_IS_SYNTHESIZED,
187 ui::EF_NONE);
188 detector_->OnMouseEvent(&mouse_event);
189 EXPECT_FALSE(mouse_event.handled());
190 EXPECT_EQ(base::TimeTicks().ToInternalValue(),
191 detector_->last_activity_time().ToInternalValue());
192 EXPECT_EQ(0, observer_->num_invocations());
195 } // namespace ui