OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/base/user_activity/user_activity_detector.h" | 5 #include "ui/base/user_activity/user_activity_detector.h" |
6 | 6 |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/time/time.h" | 9 #include "base/time/time.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 #include "ui/base/user_activity/user_activity_observer.h" | 11 #include "ui/base/user_activity/user_activity_observer.h" |
12 #include "ui/events/event.h" | 12 #include "ui/events/event.h" |
13 #include "ui/events/event_constants.h" | 13 #include "ui/events/event_constants.h" |
14 #include "ui/events/event_utils.h" | 14 #include "ui/events/event_utils.h" |
15 #include "ui/events/keycodes/keyboard_codes.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" | 16 #include "ui/gfx/geometry/point.h" |
18 | 17 |
19 namespace ui { | 18 namespace ui { |
20 | 19 |
21 // Implementation that just counts the number of times we've been told that the | 20 // Implementation that just counts the number of times we've been told that the |
22 // user is active. | 21 // user is active. |
23 class TestUserActivityObserver : public UserActivityObserver { | 22 class TestUserActivityObserver : public UserActivityObserver { |
24 public: | 23 public: |
25 TestUserActivityObserver() : num_invocations_(0) {} | 24 TestUserActivityObserver() : num_invocations_(0) {} |
26 | 25 |
27 int num_invocations() const { return num_invocations_; } | 26 int num_invocations() const { return num_invocations_; } |
28 void reset_stats() { num_invocations_ = 0; } | 27 void reset_stats() { num_invocations_ = 0; } |
29 | 28 |
30 // UserActivityObserver implementation. | 29 // UserActivityObserver implementation. |
31 void OnUserActivity(const ui::Event* event) override { num_invocations_++; } | 30 void OnUserActivity(const ui::Event* event) override { num_invocations_++; } |
32 | 31 |
33 private: | 32 private: |
34 // Number of times that OnUserActivity() has been called. | 33 // Number of times that OnUserActivity() has been called. |
35 int num_invocations_; | 34 int num_invocations_; |
36 | 35 |
37 DISALLOW_COPY_AND_ASSIGN(TestUserActivityObserver); | 36 DISALLOW_COPY_AND_ASSIGN(TestUserActivityObserver); |
38 }; | 37 }; |
39 | 38 |
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 {} | |
46 | |
47 private: | |
48 DISALLOW_COPY_AND_ASSIGN(TestPlatformEventSource); | |
49 }; | |
50 | |
51 class UserActivityDetectorTest : public testing::Test { | 39 class UserActivityDetectorTest : public testing::Test { |
52 public: | 40 public: |
53 UserActivityDetectorTest() | 41 UserActivityDetectorTest() |
54 : platform_event_source_(new TestPlatformEventSource), | 42 : detector_(new UserActivityDetector), |
55 detector_(new UserActivityDetector), | |
56 observer_(new TestUserActivityObserver) { | 43 observer_(new TestUserActivityObserver) { |
57 detector_->AddObserver(observer_.get()); | 44 detector_->AddObserver(observer_.get()); |
58 now_ = base::TimeTicks::Now(); | 45 now_ = base::TimeTicks::Now(); |
59 detector_->set_now_for_test(now_); | 46 detector_->set_now_for_test(now_); |
60 } | 47 } |
61 | 48 |
62 ~UserActivityDetectorTest() override { | 49 ~UserActivityDetectorTest() override { |
63 detector_->RemoveObserver(observer_.get()); | 50 detector_->RemoveObserver(observer_.get()); |
64 } | 51 } |
65 | 52 |
66 protected: | 53 protected: |
67 // Move |detector_|'s idea of the current time forward by |delta|. | 54 // Move |detector_|'s idea of the current time forward by |delta|. |
68 void AdvanceTime(base::TimeDelta delta) { | 55 void AdvanceTime(base::TimeDelta delta) { |
69 now_ += delta; | 56 now_ += delta; |
70 detector_->set_now_for_test(now_); | 57 detector_->set_now_for_test(now_); |
71 } | 58 } |
72 | 59 |
73 void OnEvent(const ui::Event* event) { | |
74 detector_->ProcessReceivedEvent(event); | |
75 } | |
76 | |
77 scoped_ptr<TestPlatformEventSource> platform_event_source_; | |
78 scoped_ptr<UserActivityDetector> detector_; | 60 scoped_ptr<UserActivityDetector> detector_; |
79 scoped_ptr<TestUserActivityObserver> observer_; | 61 scoped_ptr<TestUserActivityObserver> observer_; |
80 | 62 |
81 base::TimeTicks now_; | 63 base::TimeTicks now_; |
82 | 64 |
83 private: | 65 private: |
84 DISALLOW_COPY_AND_ASSIGN(UserActivityDetectorTest); | 66 DISALLOW_COPY_AND_ASSIGN(UserActivityDetectorTest); |
85 }; | 67 }; |
86 | 68 |
87 // Checks that the observer is notified in response to different types of input | 69 // Checks that the observer is notified in response to different types of input |
88 // events. | 70 // events. |
89 TEST_F(UserActivityDetectorTest, Basic) { | 71 TEST_F(UserActivityDetectorTest, Basic) { |
90 ui::KeyEvent key_event(ui::ET_KEY_PRESSED, ui::VKEY_A, ui::EF_NONE); | 72 ui::KeyEvent key_event(ui::ET_KEY_PRESSED, ui::VKEY_A, ui::EF_NONE); |
91 OnEvent(&key_event); | 73 detector_->OnKeyEvent(&key_event); |
92 EXPECT_FALSE(key_event.handled()); | 74 EXPECT_FALSE(key_event.handled()); |
93 EXPECT_EQ(now_.ToInternalValue(), | 75 EXPECT_EQ(now_.ToInternalValue(), |
94 detector_->last_activity_time().ToInternalValue()); | 76 detector_->last_activity_time().ToInternalValue()); |
95 EXPECT_EQ(1, observer_->num_invocations()); | 77 EXPECT_EQ(1, observer_->num_invocations()); |
96 observer_->reset_stats(); | 78 observer_->reset_stats(); |
97 | 79 |
98 base::TimeDelta advance_delta = base::TimeDelta::FromMilliseconds( | 80 base::TimeDelta advance_delta = base::TimeDelta::FromMilliseconds( |
99 UserActivityDetector::kNotifyIntervalMs); | 81 UserActivityDetector::kNotifyIntervalMs); |
100 AdvanceTime(advance_delta); | 82 AdvanceTime(advance_delta); |
101 ui::MouseEvent mouse_event(ui::ET_MOUSE_MOVED, gfx::Point(), gfx::Point(), | 83 ui::MouseEvent mouse_event(ui::ET_MOUSE_MOVED, gfx::Point(), gfx::Point(), |
102 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE); | 84 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE); |
103 OnEvent(&mouse_event); | 85 detector_->OnMouseEvent(&mouse_event); |
104 EXPECT_FALSE(mouse_event.handled()); | 86 EXPECT_FALSE(mouse_event.handled()); |
105 EXPECT_EQ(now_.ToInternalValue(), | 87 EXPECT_EQ(now_.ToInternalValue(), |
106 detector_->last_activity_time().ToInternalValue()); | 88 detector_->last_activity_time().ToInternalValue()); |
107 EXPECT_EQ(1, observer_->num_invocations()); | 89 EXPECT_EQ(1, observer_->num_invocations()); |
108 observer_->reset_stats(); | 90 observer_->reset_stats(); |
109 | 91 |
110 base::TimeTicks time_before_ignore = now_; | 92 base::TimeTicks time_before_ignore = now_; |
111 | 93 |
112 // Temporarily ignore mouse events when displays are turned on or off. | 94 // Temporarily ignore mouse events when displays are turned on or off. |
113 detector_->OnDisplayPowerChanging(); | 95 detector_->OnDisplayPowerChanging(); |
114 OnEvent(&mouse_event); | 96 detector_->OnMouseEvent(&mouse_event); |
115 EXPECT_FALSE(mouse_event.handled()); | 97 EXPECT_FALSE(mouse_event.handled()); |
116 EXPECT_EQ(time_before_ignore.ToInternalValue(), | 98 EXPECT_EQ(time_before_ignore.ToInternalValue(), |
117 detector_->last_activity_time().ToInternalValue()); | 99 detector_->last_activity_time().ToInternalValue()); |
118 EXPECT_EQ(0, observer_->num_invocations()); | 100 EXPECT_EQ(0, observer_->num_invocations()); |
119 observer_->reset_stats(); | 101 observer_->reset_stats(); |
120 | 102 |
121 const base::TimeDelta kIgnoreMouseTime = | 103 const base::TimeDelta kIgnoreMouseTime = |
122 base::TimeDelta::FromMilliseconds( | 104 base::TimeDelta::FromMilliseconds( |
123 UserActivityDetector::kDisplayPowerChangeIgnoreMouseMs); | 105 UserActivityDetector::kDisplayPowerChangeIgnoreMouseMs); |
124 AdvanceTime(kIgnoreMouseTime / 2); | 106 AdvanceTime(kIgnoreMouseTime / 2); |
125 OnEvent(&mouse_event); | 107 detector_->OnMouseEvent(&mouse_event); |
126 EXPECT_FALSE(mouse_event.handled()); | 108 EXPECT_FALSE(mouse_event.handled()); |
127 EXPECT_EQ(time_before_ignore.ToInternalValue(), | 109 EXPECT_EQ(time_before_ignore.ToInternalValue(), |
128 detector_->last_activity_time().ToInternalValue()); | 110 detector_->last_activity_time().ToInternalValue()); |
129 EXPECT_EQ(0, observer_->num_invocations()); | 111 EXPECT_EQ(0, observer_->num_invocations()); |
130 observer_->reset_stats(); | 112 observer_->reset_stats(); |
131 | 113 |
132 // After enough time has passed, mouse events should be reported again. | 114 // After enough time has passed, mouse events should be reported again. |
133 AdvanceTime(std::max(kIgnoreMouseTime, advance_delta)); | 115 AdvanceTime(std::max(kIgnoreMouseTime, advance_delta)); |
134 OnEvent(&mouse_event); | 116 detector_->OnMouseEvent(&mouse_event); |
135 EXPECT_FALSE(mouse_event.handled()); | 117 EXPECT_FALSE(mouse_event.handled()); |
136 EXPECT_EQ(now_.ToInternalValue(), | 118 EXPECT_EQ(now_.ToInternalValue(), |
137 detector_->last_activity_time().ToInternalValue()); | 119 detector_->last_activity_time().ToInternalValue()); |
138 EXPECT_EQ(1, observer_->num_invocations()); | 120 EXPECT_EQ(1, observer_->num_invocations()); |
139 observer_->reset_stats(); | 121 observer_->reset_stats(); |
140 | 122 |
141 AdvanceTime(advance_delta); | 123 AdvanceTime(advance_delta); |
142 ui::TouchEvent touch_event( | 124 ui::TouchEvent touch_event( |
143 ui::ET_TOUCH_PRESSED, gfx::Point(), 0, base::TimeDelta()); | 125 ui::ET_TOUCH_PRESSED, gfx::Point(), 0, base::TimeDelta()); |
144 OnEvent(&touch_event); | 126 detector_->OnTouchEvent(&touch_event); |
145 EXPECT_FALSE(touch_event.handled()); | 127 EXPECT_FALSE(touch_event.handled()); |
146 EXPECT_EQ(now_.ToInternalValue(), | 128 EXPECT_EQ(now_.ToInternalValue(), |
147 detector_->last_activity_time().ToInternalValue()); | 129 detector_->last_activity_time().ToInternalValue()); |
148 EXPECT_EQ(1, observer_->num_invocations()); | 130 EXPECT_EQ(1, observer_->num_invocations()); |
149 observer_->reset_stats(); | 131 observer_->reset_stats(); |
150 | 132 |
151 AdvanceTime(advance_delta); | 133 AdvanceTime(advance_delta); |
152 ui::GestureEvent gesture_event( | 134 ui::GestureEvent gesture_event( |
153 0, | 135 0, |
154 0, | 136 0, |
155 ui::EF_NONE, | 137 ui::EF_NONE, |
156 base::TimeDelta::FromMilliseconds(base::Time::Now().ToDoubleT() * 1000), | 138 base::TimeDelta::FromMilliseconds(base::Time::Now().ToDoubleT() * 1000), |
157 ui::GestureEventDetails(ui::ET_GESTURE_TAP)); | 139 ui::GestureEventDetails(ui::ET_GESTURE_TAP)); |
158 OnEvent(&gesture_event); | 140 detector_->OnGestureEvent(&gesture_event); |
159 EXPECT_FALSE(gesture_event.handled()); | 141 EXPECT_FALSE(gesture_event.handled()); |
160 EXPECT_EQ(now_.ToInternalValue(), | 142 EXPECT_EQ(now_.ToInternalValue(), |
161 detector_->last_activity_time().ToInternalValue()); | 143 detector_->last_activity_time().ToInternalValue()); |
162 EXPECT_EQ(1, observer_->num_invocations()); | 144 EXPECT_EQ(1, observer_->num_invocations()); |
163 observer_->reset_stats(); | 145 observer_->reset_stats(); |
164 } | 146 } |
165 | 147 |
166 // Checks that observers aren't notified too frequently. | 148 // Checks that observers aren't notified too frequently. |
167 TEST_F(UserActivityDetectorTest, RateLimitNotifications) { | 149 TEST_F(UserActivityDetectorTest, RateLimitNotifications) { |
168 // The observer should be notified about a key event. | 150 // The observer should be notified about a key event. |
169 ui::KeyEvent event(ui::ET_KEY_PRESSED, ui::VKEY_A, ui::EF_NONE); | 151 ui::KeyEvent event(ui::ET_KEY_PRESSED, ui::VKEY_A, ui::EF_NONE); |
170 OnEvent(&event); | 152 detector_->OnKeyEvent(&event); |
171 EXPECT_FALSE(event.handled()); | 153 EXPECT_FALSE(event.handled()); |
172 EXPECT_EQ(1, observer_->num_invocations()); | 154 EXPECT_EQ(1, observer_->num_invocations()); |
173 observer_->reset_stats(); | 155 observer_->reset_stats(); |
174 | 156 |
175 // It shouldn't be notified if a second event occurs in the same instant in | 157 // It shouldn't be notified if a second event occurs in the same instant in |
176 // time. | 158 // time. |
177 OnEvent(&event); | 159 detector_->OnKeyEvent(&event); |
178 EXPECT_FALSE(event.handled()); | 160 EXPECT_FALSE(event.handled()); |
179 EXPECT_EQ(0, observer_->num_invocations()); | 161 EXPECT_EQ(0, observer_->num_invocations()); |
180 observer_->reset_stats(); | 162 observer_->reset_stats(); |
181 | 163 |
182 // Advance the time, but not quite enough for another notification to be sent. | 164 // Advance the time, but not quite enough for another notification to be sent. |
183 AdvanceTime( | 165 AdvanceTime( |
184 base::TimeDelta::FromMilliseconds( | 166 base::TimeDelta::FromMilliseconds( |
185 UserActivityDetector::kNotifyIntervalMs - 100)); | 167 UserActivityDetector::kNotifyIntervalMs - 100)); |
186 OnEvent(&event); | 168 detector_->OnKeyEvent(&event); |
187 EXPECT_FALSE(event.handled()); | 169 EXPECT_FALSE(event.handled()); |
188 EXPECT_EQ(0, observer_->num_invocations()); | 170 EXPECT_EQ(0, observer_->num_invocations()); |
189 observer_->reset_stats(); | 171 observer_->reset_stats(); |
190 | 172 |
191 // Advance time by the notification interval, definitely moving out of the | 173 // Advance time by the notification interval, definitely moving out of the |
192 // rate limit. This should let us trigger another notification. | 174 // rate limit. This should let us trigger another notification. |
193 AdvanceTime(base::TimeDelta::FromMilliseconds( | 175 AdvanceTime(base::TimeDelta::FromMilliseconds( |
194 UserActivityDetector::kNotifyIntervalMs)); | 176 UserActivityDetector::kNotifyIntervalMs)); |
195 | 177 |
196 OnEvent(&event); | 178 detector_->OnKeyEvent(&event); |
197 EXPECT_FALSE(event.handled()); | 179 EXPECT_FALSE(event.handled()); |
198 EXPECT_EQ(1, observer_->num_invocations()); | 180 EXPECT_EQ(1, observer_->num_invocations()); |
199 } | 181 } |
200 | 182 |
201 // Checks that the detector ignores synthetic mouse events. | 183 // Checks that the detector ignores synthetic mouse events. |
202 TEST_F(UserActivityDetectorTest, IgnoreSyntheticMouseEvents) { | 184 TEST_F(UserActivityDetectorTest, IgnoreSyntheticMouseEvents) { |
203 ui::MouseEvent mouse_event(ui::ET_MOUSE_MOVED, gfx::Point(), gfx::Point(), | 185 ui::MouseEvent mouse_event(ui::ET_MOUSE_MOVED, gfx::Point(), gfx::Point(), |
204 ui::EventTimeForNow(), ui::EF_IS_SYNTHESIZED, | 186 ui::EventTimeForNow(), ui::EF_IS_SYNTHESIZED, |
205 ui::EF_NONE); | 187 ui::EF_NONE); |
206 OnEvent(&mouse_event); | 188 detector_->OnMouseEvent(&mouse_event); |
207 EXPECT_FALSE(mouse_event.handled()); | 189 EXPECT_FALSE(mouse_event.handled()); |
208 EXPECT_EQ(base::TimeTicks().ToInternalValue(), | 190 EXPECT_EQ(base::TimeTicks().ToInternalValue(), |
209 detector_->last_activity_time().ToInternalValue()); | 191 detector_->last_activity_time().ToInternalValue()); |
210 EXPECT_EQ(0, observer_->num_invocations()); | 192 EXPECT_EQ(0, observer_->num_invocations()); |
211 } | 193 } |
212 | 194 |
213 } // namespace ui | 195 } // namespace ui |
OLD | NEW |