Revert "Clean up GestureEventDetails constructors and fix unit tests."
[chromium-blink-merge.git] / ui / wm / core / user_activity_detector_unittest.cc
blobdf669c7ace21270ff855be783831d9a3093a9077
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/point.h"
15 #include "ui/wm/core/user_activity_observer.h"
17 namespace wm {
19 // Implementation that just counts the number of times we've been told that the
20 // user is active.
21 class TestUserActivityObserver : public UserActivityObserver {
22 public:
23 TestUserActivityObserver() : num_invocations_(0) {}
25 int num_invocations() const { return num_invocations_; }
26 void reset_stats() { num_invocations_ = 0; }
28 // UserActivityObserver implementation.
29 virtual void OnUserActivity(const ui::Event* event) OVERRIDE {
30 num_invocations_++;
33 private:
34 // Number of times that OnUserActivity() has been called.
35 int num_invocations_;
37 DISALLOW_COPY_AND_ASSIGN(TestUserActivityObserver);
40 class UserActivityDetectorTest : public aura::test::AuraTestBase {
41 public:
42 UserActivityDetectorTest() {}
43 virtual ~UserActivityDetectorTest() {}
45 virtual void SetUp() OVERRIDE {
46 AuraTestBase::SetUp();
47 observer_.reset(new TestUserActivityObserver);
48 detector_.reset(new UserActivityDetector);
49 detector_->AddObserver(observer_.get());
51 now_ = base::TimeTicks::Now();
52 detector_->set_now_for_test(now_);
55 virtual void TearDown() OVERRIDE {
56 detector_->RemoveObserver(observer_.get());
57 AuraTestBase::TearDown();
60 protected:
61 // Move |detector_|'s idea of the current time forward by |delta|.
62 void AdvanceTime(base::TimeDelta delta) {
63 now_ += delta;
64 detector_->set_now_for_test(now_);
67 scoped_ptr<UserActivityDetector> detector_;
68 scoped_ptr<TestUserActivityObserver> observer_;
70 base::TimeTicks now_;
72 private:
73 DISALLOW_COPY_AND_ASSIGN(UserActivityDetectorTest);
76 // Checks that the observer is notified in response to different types of input
77 // events.
78 TEST_F(UserActivityDetectorTest, Basic) {
79 ui::KeyEvent key_event(ui::ET_KEY_PRESSED, ui::VKEY_A, ui::EF_NONE);
80 detector_->OnKeyEvent(&key_event);
81 EXPECT_FALSE(key_event.handled());
82 EXPECT_EQ(now_.ToInternalValue(),
83 detector_->last_activity_time().ToInternalValue());
84 EXPECT_EQ(1, observer_->num_invocations());
85 observer_->reset_stats();
87 base::TimeDelta advance_delta = base::TimeDelta::FromMilliseconds(
88 UserActivityDetector::kNotifyIntervalMs);
89 AdvanceTime(advance_delta);
90 ui::MouseEvent mouse_event(
91 ui::ET_MOUSE_MOVED, gfx::Point(), gfx::Point(), ui::EF_NONE, ui::EF_NONE);
92 detector_->OnMouseEvent(&mouse_event);
93 EXPECT_FALSE(mouse_event.handled());
94 EXPECT_EQ(now_.ToInternalValue(),
95 detector_->last_activity_time().ToInternalValue());
96 EXPECT_EQ(1, observer_->num_invocations());
97 observer_->reset_stats();
99 base::TimeTicks time_before_ignore = now_;
101 // Temporarily ignore mouse events when displays are turned on or off.
102 detector_->OnDisplayPowerChanging();
103 detector_->OnMouseEvent(&mouse_event);
104 EXPECT_FALSE(mouse_event.handled());
105 EXPECT_EQ(time_before_ignore.ToInternalValue(),
106 detector_->last_activity_time().ToInternalValue());
107 EXPECT_EQ(0, observer_->num_invocations());
108 observer_->reset_stats();
110 const base::TimeDelta kIgnoreMouseTime =
111 base::TimeDelta::FromMilliseconds(
112 UserActivityDetector::kDisplayPowerChangeIgnoreMouseMs);
113 AdvanceTime(kIgnoreMouseTime / 2);
114 detector_->OnMouseEvent(&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 // After enough time has passed, mouse events should be reported again.
122 AdvanceTime(std::max(kIgnoreMouseTime, advance_delta));
123 detector_->OnMouseEvent(&mouse_event);
124 EXPECT_FALSE(mouse_event.handled());
125 EXPECT_EQ(now_.ToInternalValue(),
126 detector_->last_activity_time().ToInternalValue());
127 EXPECT_EQ(1, observer_->num_invocations());
128 observer_->reset_stats();
130 AdvanceTime(advance_delta);
131 ui::TouchEvent touch_event(
132 ui::ET_TOUCH_PRESSED, gfx::Point(), 0, base::TimeDelta());
133 detector_->OnTouchEvent(&touch_event);
134 EXPECT_FALSE(touch_event.handled());
135 EXPECT_EQ(now_.ToInternalValue(),
136 detector_->last_activity_time().ToInternalValue());
137 EXPECT_EQ(1, observer_->num_invocations());
138 observer_->reset_stats();
140 AdvanceTime(advance_delta);
141 ui::GestureEvent gesture_event(
142 0, 0, ui::EF_NONE,
143 base::TimeDelta::FromMilliseconds(base::Time::Now().ToDoubleT() * 1000),
144 ui::GestureEventDetails(ui::ET_GESTURE_TAP, 0, 0));
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
163 // time.
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.
170 AdvanceTime(
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,
192 ui::EF_NONE);
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());
200 } // namespace wm