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