user_activity_detector_unittest.cc 8.0 KB

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