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