OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 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 | |
5 #include "ash/system/logout_button/logout_button_tray.h" | |
6 | |
7 #include "ash/system/logout_button/logout_button_observer.h" | |
8 #include "ash/system/logout_button/logout_confirmation_dialog_view.h" | |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "base/single_thread_task_runner.h" | |
11 #include "base/thread_task_runner_handle.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | |
13 | |
14 namespace ash { | |
15 namespace internal { | |
16 | |
17 // A SingleThreadTaskRunner that mocks the current time and allows it to be | |
18 // fast-forwarded. | |
19 class MockTimeSingleThreadTaskRunner : public base::SingleThreadTaskRunner { | |
bartfab (slow)
2013/12/03 19:46:04
This entire class is a copy & paste from another p
binjin
2013/12/04 10:47:03
Can you suggest a proper place to move the shared
bartfab (slow)
2013/12/04 12:51:56
It should probably move to a file ending in test_u
binjin
2013/12/05 10:08:05
I'm not sure if it's okay to use chrome/* classes
bartfab (slow)
2013/12/06 13:50:04
As discussed offline, I agree that having this cod
| |
20 public: | |
21 MockTimeSingleThreadTaskRunner(); | |
22 | |
23 // base::SingleThreadTaskRunner: | |
24 virtual bool RunsTasksOnCurrentThread() const OVERRIDE; | |
25 virtual bool PostDelayedTask(const tracked_objects::Location& from_here, | |
26 const base::Closure& task, | |
27 base::TimeDelta delay) OVERRIDE; | |
28 virtual bool PostNonNestableDelayedTask( | |
29 const tracked_objects::Location& from_here, | |
30 const base::Closure& task, | |
31 base::TimeDelta delay) OVERRIDE; | |
32 | |
33 const base::TimeTicks& GetCurrentTime() const; | |
34 | |
35 void FastForwardBy(int64 milliseconds); | |
36 void FastForwardUntilNoTasksRemain(); | |
37 | |
38 private: | |
39 // Strict weak temporal ordering of tasks. | |
40 class TemporalOrder { | |
41 public: | |
42 bool operator()( | |
43 const std::pair<base::TimeTicks, base::Closure>& first_task, | |
44 const std::pair<base::TimeTicks, base::Closure>& second_task) const; | |
45 }; | |
46 | |
47 virtual ~MockTimeSingleThreadTaskRunner(); | |
48 | |
49 base::TimeTicks now_; | |
50 std::priority_queue<std::pair<base::TimeTicks, base::Closure>, | |
51 std::vector<std::pair<base::TimeTicks, base::Closure> >, | |
52 TemporalOrder> tasks_; | |
53 }; | |
54 | |
55 class MockLogoutConfirmationSettingsProvider | |
56 : public LogoutConfirmationSettingsProvider { | |
bartfab (slow)
2013/12/03 19:46:04
Nit: s/SettingsProvider/Delegate/. This name is mo
binjin
2013/12/05 10:08:05
Done.
| |
57 public: | |
58 MockLogoutConfirmationSettingsProvider( | |
bartfab (slow)
2013/12/03 19:46:04
Single-argument constructors should be marked expl
binjin
2013/12/04 10:47:03
Done.
| |
59 MockTimeSingleThreadTaskRunner* runner); | |
60 | |
61 // LogoutConfirmationSettingsProvider | |
bartfab (slow)
2013/12/03 19:46:04
Nit: Add a colon at the end of the line.
binjin
2013/12/04 10:47:03
Done.
| |
62 virtual void LogoutCurrentUser(LogoutConfirmationDialogView*) OVERRIDE; | |
bartfab (slow)
2013/12/03 19:46:04
Nit: Add argument name.
binjin
2013/12/04 10:47:03
Done.
| |
63 virtual base::TimeTicks GetCurrentTime() OVERRIDE; | |
64 | |
65 void SetLogoutCalled(bool called); | |
bartfab (slow)
2013/12/03 19:46:04
Nit: This is never used.
binjin
2013/12/04 10:47:03
Done.
| |
66 bool IsLogoutCalled(); | |
bartfab (slow)
2013/12/03 19:46:04
Nit 1: WasLogoutCalled() would be more correct.
Ni
binjin
2013/12/04 10:47:03
Done.
| |
67 | |
68 private: | |
69 bool logout_called_; | |
70 | |
71 MockTimeSingleThreadTaskRunner* runner_; | |
72 }; | |
bartfab (slow)
2013/12/03 19:46:04
Nit: Add DISALLOW_COPY_AND_ASSIGN(MockLogoutConfir
binjin
2013/12/04 10:47:03
Done.
| |
73 | |
74 MockTimeSingleThreadTaskRunner::MockTimeSingleThreadTaskRunner() { | |
75 } | |
76 | |
77 bool MockTimeSingleThreadTaskRunner::RunsTasksOnCurrentThread() const { | |
78 return true; | |
79 } | |
80 | |
81 bool MockTimeSingleThreadTaskRunner::PostDelayedTask( | |
82 const tracked_objects::Location& from_here, | |
83 const base::Closure& task, | |
84 base::TimeDelta delay) { | |
85 tasks_.push(std::pair<base::TimeTicks, base::Closure>(now_ + delay, task)); | |
86 return true; | |
87 } | |
88 | |
89 bool MockTimeSingleThreadTaskRunner::PostNonNestableDelayedTask( | |
90 const tracked_objects::Location& from_here, | |
91 const base::Closure& task, | |
92 base::TimeDelta delay) { | |
93 NOTREACHED(); | |
94 return false; | |
95 } | |
96 | |
97 const base::TimeTicks& MockTimeSingleThreadTaskRunner::GetCurrentTime() const { | |
98 return now_; | |
99 } | |
100 | |
101 void MockTimeSingleThreadTaskRunner::FastForwardBy(int64 delta) { | |
102 const base::TimeTicks latest = | |
103 now_ + base::TimeDelta::FromMilliseconds(delta); | |
104 while (!tasks_.empty() && tasks_.top().first <= latest) { | |
105 now_ = tasks_.top().first; | |
106 base::Closure task = tasks_.top().second; | |
107 tasks_.pop(); | |
108 task.Run(); | |
109 } | |
110 now_ = latest; | |
111 } | |
112 | |
113 void MockTimeSingleThreadTaskRunner::FastForwardUntilNoTasksRemain() { | |
114 while (!tasks_.empty()) { | |
115 now_ = tasks_.top().first; | |
116 base::Closure task = tasks_.top().second; | |
117 tasks_.pop(); | |
118 task.Run(); | |
119 } | |
120 } | |
121 | |
122 bool MockTimeSingleThreadTaskRunner::TemporalOrder::operator()( | |
123 const std::pair<base::TimeTicks, base::Closure>& first_task, | |
124 const std::pair<base::TimeTicks, base::Closure>& second_task) const { | |
125 return first_task.first >= second_task.first; | |
126 } | |
127 | |
128 MockTimeSingleThreadTaskRunner::~MockTimeSingleThreadTaskRunner() { | |
129 } | |
130 | |
131 MockLogoutConfirmationSettingsProvider::MockLogoutConfirmationSettingsProvider( | |
132 MockTimeSingleThreadTaskRunner* runner) { | |
133 runner_ = runner; | |
134 logout_called_ = false; | |
135 } | |
136 | |
137 void MockLogoutConfirmationSettingsProvider::LogoutCurrentUser( | |
138 LogoutConfirmationDialogView* dialog) { | |
139 logout_called_ = true; | |
140 if (dialog) | |
141 dialog->DeleteDelegate(); | |
142 } | |
143 | |
144 base::TimeTicks MockLogoutConfirmationSettingsProvider::GetCurrentTime() { | |
145 return runner_->GetCurrentTime(); | |
146 } | |
147 | |
148 void MockLogoutConfirmationSettingsProvider::SetLogoutCalled(bool called) { | |
149 logout_called_ = called; | |
150 } | |
151 | |
152 bool MockLogoutConfirmationSettingsProvider::IsLogoutCalled() { | |
153 return logout_called_; | |
154 } | |
155 | |
156 class LogoutConfirmationDialogTest : public testing::Test { | |
157 public: | |
158 LogoutConfirmationDialogTest() {} | |
bartfab (slow)
2013/12/03 19:46:04
Nit: You should be consistent with your inlining s
binjin
2013/12/04 10:47:03
Done.
| |
159 virtual ~LogoutConfirmationDialogTest() {} | |
160 | |
161 // testing::Test | |
bartfab (slow)
2013/12/03 19:46:04
Nit: Add a colon at the end of the line.
binjin
2013/12/04 10:47:03
Done.
| |
162 virtual void SetUp() OVERRIDE; | |
163 virtual void TearDown() OVERRIDE; | |
164 | |
165 void ChangeDialogDuration(int duration_ms); | |
bartfab (slow)
2013/12/03 19:46:04
Nit: Make the argument a base::TimeDelta.
binjin
2013/12/05 10:08:05
Done.
| |
166 | |
167 protected: | |
168 scoped_ptr<LogoutButtonTray> tray_; | |
bartfab (slow)
2013/12/03 19:46:04
Nit: s/tray_/button_/ or s/tray_/logout_button_/ w
binjin
2013/12/04 10:47:03
Done.
| |
169 scoped_refptr<MockTimeSingleThreadTaskRunner> runner_; | |
170 scoped_ptr<MockLogoutConfirmationSettingsProvider> provider_; | |
bartfab (slow)
2013/12/03 19:46:04
Nit: In line with renaming the class, this member
binjin
2013/12/05 10:08:05
Done.
| |
171 }; | |
172 | |
173 void LogoutConfirmationDialogTest::SetUp() { | |
174 runner_ = new MockTimeSingleThreadTaskRunner; | |
175 tray_.reset(new LogoutButtonTray(NULL)); | |
176 ChangeDialogDuration(20 * 1000); // default value, 20 seconds. | |
177 provider_.reset(new MockLogoutConfirmationSettingsProvider(runner_.get())); | |
178 LogoutConfirmationDialogView::SetProvider(provider_.get()); | |
179 } | |
180 | |
181 void LogoutConfirmationDialogTest::TearDown() { | |
182 } | |
183 | |
184 void LogoutConfirmationDialogTest::ChangeDialogDuration(int duration_ms) { | |
185 tray_->OnLogoutDialogDurationChanged( | |
186 base::TimeDelta::FromMilliseconds(duration_ms)); | |
187 } | |
188 | |
189 TEST_F(LogoutConfirmationDialogTest, NoClickWithDefaultValue) { | |
190 base::ThreadTaskRunnerHandle runner_handle(runner_); | |
bartfab (slow)
2013/12/03 19:46:04
Nit: Could this be handled by the LogoutConfirmati
binjin
2013/12/05 10:08:05
Done.
| |
191 | |
192 EXPECT_FALSE(tray_->IsConfirmationDialogShowing()); | |
193 | |
194 tray_->OnUserLogoutEvent(); | |
195 | |
196 runner_->FastForwardBy(1 * 1000); // 1 second after start | |
bartfab (slow)
2013/12/03 19:46:04
Nit 1: Here and elsewhere: Terminate comments with
binjin
2013/12/05 10:08:05
Done.
| |
197 | |
198 EXPECT_TRUE(tray_->IsConfirmationDialogShowing()); | |
199 EXPECT_FALSE(provider_->IsLogoutCalled()); | |
200 | |
201 runner_->FastForwardBy(18 * 1000); // 19 seconds after start | |
bartfab (slow)
2013/12/03 19:46:04
Nit: Fast-forwarding by 1s and then 18s is rather
binjin
2013/12/05 10:08:05
Done.
| |
202 | |
203 EXPECT_TRUE(tray_->IsConfirmationDialogShowing()); | |
204 EXPECT_FALSE(provider_->IsLogoutCalled()); | |
205 | |
206 runner_->FastForwardBy(2 * 1000); // 21 seconds after start | |
207 | |
208 EXPECT_FALSE(tray_->IsConfirmationDialogShowing()); | |
209 EXPECT_TRUE(provider_->IsLogoutCalled()); | |
210 } | |
211 | |
212 TEST_F(LogoutConfirmationDialogTest, ZeroPreferenceValue) { | |
213 base::ThreadTaskRunnerHandle runner_handle(runner_); | |
214 | |
215 ChangeDialogDuration(0); // set preference to 0 | |
bartfab (slow)
2013/12/03 19:46:04
Nit: The comment does not really add much.
binjin
2013/12/05 10:08:05
Done.
| |
216 | |
217 EXPECT_FALSE(tray_->IsConfirmationDialogShowing()); | |
218 | |
219 tray_->OnUserLogoutEvent(); | |
220 | |
221 EXPECT_FALSE(tray_->IsConfirmationDialogShowing()); | |
222 | |
223 runner_->FastForwardBy(1 * 1000); // 1 second | |
bartfab (slow)
2013/12/03 19:46:04
Nit: Same as above: Why not runner_->FastForwardBy
binjin
2013/12/05 10:08:05
Done.
| |
224 | |
225 EXPECT_FALSE(tray_->IsConfirmationDialogShowing()); | |
226 EXPECT_TRUE(provider_->IsLogoutCalled()); | |
227 } | |
bartfab (slow)
2013/12/03 19:46:04
Nit: Add runner_->FastForwardUntilNoTasksRemain()
binjin
2013/12/05 10:08:05
I didn't follow you on this.
On 2013/12/03 19:46:
bartfab (slow)
2013/12/06 13:50:04
At the end of the test, you verify that the confir
binjin
2013/12/09 18:50:02
Done.
| |
228 | |
229 TEST_F(LogoutConfirmationDialogTest, OnTheFlyDialogDurationChange) { | |
230 base::ThreadTaskRunnerHandle runner_handle(runner_); | |
231 | |
232 ChangeDialogDuration(5 * 1000); // set dialog duration to 5 seconds | |
bartfab (slow)
2013/12/03 19:46:04
Nit: Here and elsewhere: If you change the argumen
binjin
2013/12/05 10:08:05
Done.
| |
233 | |
234 EXPECT_FALSE(tray_->IsConfirmationDialogShowing()); | |
235 | |
236 tray_->OnUserLogoutEvent(); | |
237 | |
238 runner_->FastForwardBy(3 * 1000); // 3 seconds after start | |
bartfab (slow)
2013/12/03 19:46:04
Nit: Same as above: Why not runner_->FastForwardBy
binjin
2013/12/05 10:08:05
Done.
| |
239 | |
240 EXPECT_TRUE(tray_->IsConfirmationDialogShowing()); | |
241 EXPECT_FALSE(provider_->IsLogoutCalled()); | |
242 | |
243 ChangeDialogDuration(10 * 1000); // change dialog duration to 10 seconds | |
244 | |
245 runner_->FastForwardBy(6 * 1000); // 9 seconds after start | |
246 | |
247 EXPECT_TRUE(tray_->IsConfirmationDialogShowing()); | |
248 EXPECT_FALSE(provider_->IsLogoutCalled()); | |
249 | |
250 runner_->FastForwardBy(2 * 1000); // 11 seconds after start | |
251 | |
252 EXPECT_FALSE(tray_->IsConfirmationDialogShowing()); | |
253 EXPECT_TRUE(provider_->IsLogoutCalled()); | |
254 } | |
255 | |
256 TEST_F(LogoutConfirmationDialogTest, UserClickedButton) { | |
257 base::ThreadTaskRunnerHandle runner_handle(runner_); | |
258 | |
259 tray_->OnUserLogoutEvent(); | |
260 | |
261 runner_->FastForwardBy(3 * 1000); // 3 seconds after start | |
bartfab (slow)
2013/12/03 19:46:04
Nit: Same as above: Why not runner_->FastForwardBy
binjin
2013/12/05 10:08:05
Done.
| |
262 | |
263 EXPECT_FALSE(provider_->IsLogoutCalled()); | |
264 | |
265 tray_->GetConfirmationDialog()->Accept(); | |
266 | |
267 runner_->FastForwardBy(1 * 1000); // 4 seconds after start | |
bartfab (slow)
2013/12/03 19:46:04
Nit: Same as above: Why not runner_->FastForwardBy
binjin
2013/12/05 10:08:05
Done.
| |
268 | |
269 EXPECT_TRUE(provider_->IsLogoutCalled()); | |
270 } | |
271 | |
272 } // namespace internal | |
273 } // namespace ash | |
OLD | NEW |