[content shell] implement testRunner.overridePreference
[chromium-blink-merge.git] / ash / wm / user_activity_detector_unittest.cc
blobe0108d44c720524274b672c5f2911c3a393594fc
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
3 // found in the LICENSE file.
5 #include "ash/wm/user_activity_detector.h"
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"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/time.h"
13 #include "ui/aura/test/test_windows.h"
14 #include "ui/aura/window.h"
15 #include "ui/base/events/event.h"
16 #include "ui/base/events/event_constants.h"
17 #include "ui/base/keycodes/keyboard_codes.h"
18 #include "ui/gfx/point.h"
20 namespace {
22 void SetEventTarget(ui::EventTarget* target, ui::Event* event) {
23 ui::Event::DispatcherApi dispatch_helper(event);
24 dispatch_helper.set_target(target);
29 namespace ash {
30 namespace test {
32 // Implementation that just counts the number of times we've been told that the
33 // user is active.
34 class TestUserActivityObserver : public UserActivityObserver {
35 public:
36 TestUserActivityObserver() : num_invocations_(0) {}
38 int num_invocations() const { return num_invocations_; }
39 void reset_stats() { num_invocations_ = 0; }
41 // UserActivityObserver implementation.
42 virtual void OnUserActivity() OVERRIDE { num_invocations_++; }
44 private:
45 // Number of times that OnUserActivity() has been called.
46 int num_invocations_;
48 DISALLOW_COPY_AND_ASSIGN(TestUserActivityObserver);
51 class UserActivityDetectorTest : public AshTestBase {
52 public:
53 UserActivityDetectorTest() {}
54 virtual ~UserActivityDetectorTest() {}
56 virtual void SetUp() OVERRIDE {
57 AshTestBase::SetUp();
58 observer_.reset(new TestUserActivityObserver);
59 detector_ = Shell::GetInstance()->user_activity_detector();
60 detector_->AddObserver(observer_.get());
62 now_ = base::TimeTicks::Now();
63 detector_->set_now_for_test(now_);
66 virtual void TearDown() OVERRIDE {
67 detector_->RemoveObserver(observer_.get());
68 AshTestBase::TearDown();
71 protected:
72 // Move |detector_|'s idea of the current time forward by |delta|.
73 void AdvanceTime(base::TimeDelta delta) {
74 now_ += delta;
75 detector_->set_now_for_test(now_);
78 UserActivityDetector* detector_; // not owned
80 scoped_ptr<TestUserActivityObserver> observer_;
82 base::TimeTicks now_;
84 private:
85 DISALLOW_COPY_AND_ASSIGN(UserActivityDetectorTest);
88 // Checks that the observer is notified in response to different types of input
89 // events.
90 TEST_F(UserActivityDetectorTest, Basic) {
91 scoped_ptr<aura::Window> window(CreateTestWindowInShellWithId(12345));
93 ui::KeyEvent key_event(ui::ET_KEY_PRESSED, ui::VKEY_A, ui::EF_NONE, false);
94 SetEventTarget(window.get(), &key_event);
95 detector_->OnKeyEvent(&key_event);
96 EXPECT_FALSE(key_event.handled());
97 EXPECT_EQ(1, observer_->num_invocations());
98 observer_->reset_stats();
100 base::TimeDelta advance_delta =
101 base::TimeDelta::FromSeconds(UserActivityDetector::kNotifyIntervalMs);
102 AdvanceTime(advance_delta);
103 ui::MouseEvent mouse_event(
104 ui::ET_MOUSE_MOVED, gfx::Point(), gfx::Point(), ui::EF_NONE);
105 SetEventTarget(window.get(), &mouse_event);
106 detector_->OnMouseEvent(&mouse_event);
107 EXPECT_FALSE(mouse_event.handled());
108 EXPECT_EQ(1, observer_->num_invocations());
109 observer_->reset_stats();
111 // Ignore one mouse event when all displays are turned off.
112 detector_->OnAllOutputsTurnedOff();
113 AdvanceTime(advance_delta);
114 detector_->OnMouseEvent(&mouse_event);
115 EXPECT_FALSE(mouse_event.handled());
116 EXPECT_EQ(0, observer_->num_invocations());
117 observer_->reset_stats();
119 AdvanceTime(advance_delta);
120 ui::TouchEvent touch_event(
121 ui::ET_TOUCH_PRESSED, gfx::Point(), 0, base::TimeDelta());
122 SetEventTarget(window.get(), &touch_event);
123 detector_->OnTouchEvent(&touch_event);
124 EXPECT_FALSE(touch_event.handled());
125 EXPECT_EQ(1, observer_->num_invocations());
126 observer_->reset_stats();
128 AdvanceTime(advance_delta);
129 ui::GestureEvent gesture_event(
130 ui::ET_GESTURE_TAP, 0, 0, ui::EF_NONE,
131 base::TimeDelta::FromMilliseconds(base::Time::Now().ToDoubleT() * 1000),
132 ui::GestureEventDetails(ui::ET_GESTURE_TAP, 0, 0), 0U);
133 SetEventTarget(window.get(), &gesture_event);
134 detector_->OnGestureEvent(&gesture_event);
135 EXPECT_FALSE(gesture_event.handled());
136 EXPECT_EQ(1, observer_->num_invocations());
137 observer_->reset_stats();
140 // Checks that observers aren't notified too frequently.
141 TEST_F(UserActivityDetectorTest, RateLimitNotifications) {
142 scoped_ptr<aura::Window> window(CreateTestWindowInShellWithId(12345));
144 // The observer should be notified about a key event.
145 ui::KeyEvent event(ui::ET_KEY_PRESSED, ui::VKEY_A, ui::EF_NONE, false);
146 SetEventTarget(window.get(), &event);
147 detector_->OnKeyEvent(&event);
148 EXPECT_FALSE(event.handled());
149 EXPECT_EQ(1, observer_->num_invocations());
150 observer_->reset_stats();
152 // It shouldn't be notified if a second event occurs
153 // in the same instant in time.
154 detector_->OnKeyEvent(&event);
155 EXPECT_FALSE(event.handled());
156 EXPECT_EQ(0, observer_->num_invocations());
157 observer_->reset_stats();
159 // Advance the time, but not quite enough for another notification to be sent.
160 AdvanceTime(
161 base::TimeDelta::FromMilliseconds(
162 UserActivityDetector::kNotifyIntervalMs - 100));
163 detector_->OnKeyEvent(&event);
164 EXPECT_FALSE(event.handled());
165 EXPECT_EQ(0, observer_->num_invocations());
166 observer_->reset_stats();
168 // Advance time by the notification interval, definitely moving out of the
169 // rate limit. This should let us trigger another notification.
170 AdvanceTime(base::TimeDelta::FromMilliseconds(
171 UserActivityDetector::kNotifyIntervalMs));
173 detector_->OnKeyEvent(&event);
174 EXPECT_FALSE(event.handled());
175 EXPECT_EQ(1, observer_->num_invocations());
178 // Checks that the detector ignores synthetic mouse events.
179 TEST_F(UserActivityDetectorTest, IgnoreSyntheticMouseEvents) {
180 scoped_ptr<aura::Window> window(CreateTestWindowInShellWithId(12345));
181 ui::MouseEvent mouse_event(
182 ui::ET_MOUSE_MOVED, gfx::Point(), gfx::Point(), ui::EF_IS_SYNTHESIZED);
183 SetEventTarget(window.get(), &mouse_event);
184 detector_->OnMouseEvent(&mouse_event);
185 EXPECT_FALSE(mouse_event.handled());
186 EXPECT_EQ(0, observer_->num_invocations());
189 } // namespace test
190 } // namespace ash