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