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 "content/public/test/test_utils.h" | 5 #include "content/public/test/test_utils.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 | 60 |
61 DISALLOW_COPY_AND_ASSIGN(ScriptCallback); | 61 DISALLOW_COPY_AND_ASSIGN(ScriptCallback); |
62 }; | 62 }; |
63 | 63 |
64 void ScriptCallback::ResultCallback(const base::Value* result) { | 64 void ScriptCallback::ResultCallback(const base::Value* result) { |
65 if (result) | 65 if (result) |
66 result_.reset(result->DeepCopy()); | 66 result_.reset(result->DeepCopy()); |
67 base::MessageLoop::current()->Quit(); | 67 base::MessageLoop::current()->Quit(); |
68 } | 68 } |
69 | 69 |
| 70 // Adapter that makes a WindowedNotificationObserver::ConditionTestCallback from |
| 71 // a WindowedNotificationObserver::ConditionTestCallbackWithoutSourceAndDetails |
| 72 // by ignoring the notification source and details. |
| 73 bool IgnoreSourceAndDetails( |
| 74 const WindowedNotificationObserver:: |
| 75 ConditionTestCallbackWithoutSourceAndDetails& callback, |
| 76 const NotificationSource& source, |
| 77 const NotificationDetails& details) { |
| 78 return callback.Run(); |
| 79 } |
| 80 |
70 } // namespace | 81 } // namespace |
71 | 82 |
72 void RunMessageLoop() { | 83 void RunMessageLoop() { |
73 base::RunLoop run_loop; | 84 base::RunLoop run_loop; |
74 RunThisRunLoop(&run_loop); | 85 RunThisRunLoop(&run_loop); |
75 } | 86 } |
76 | 87 |
77 void RunThisRunLoop(base::RunLoop* run_loop) { | 88 void RunThisRunLoop(base::RunLoop* run_loop) { |
78 base::MessageLoop::ScopedNestableTaskAllower allow( | 89 base::MessageLoop::ScopedNestableTaskAllower allow( |
79 base::MessageLoop::current()); | 90 base::MessageLoop::current()); |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 WindowedNotificationObserver::WindowedNotificationObserver( | 190 WindowedNotificationObserver::WindowedNotificationObserver( |
180 int notification_type, | 191 int notification_type, |
181 const ConditionTestCallback& callback) | 192 const ConditionTestCallback& callback) |
182 : seen_(false), | 193 : seen_(false), |
183 running_(false), | 194 running_(false), |
184 callback_(callback), | 195 callback_(callback), |
185 source_(NotificationService::AllSources()) { | 196 source_(NotificationService::AllSources()) { |
186 registrar_.Add(this, notification_type, source_); | 197 registrar_.Add(this, notification_type, source_); |
187 } | 198 } |
188 | 199 |
| 200 WindowedNotificationObserver::WindowedNotificationObserver( |
| 201 int notification_type, |
| 202 const ConditionTestCallbackWithoutSourceAndDetails& callback) |
| 203 : seen_(false), |
| 204 running_(false), |
| 205 callback_(base::Bind(&IgnoreSourceAndDetails, callback)), |
| 206 source_(NotificationService::AllSources()) { |
| 207 registrar_.Add(this, notification_type, source_); |
| 208 } |
| 209 |
189 WindowedNotificationObserver::~WindowedNotificationObserver() {} | 210 WindowedNotificationObserver::~WindowedNotificationObserver() {} |
190 | 211 |
191 void WindowedNotificationObserver::Wait() { | 212 void WindowedNotificationObserver::Wait() { |
192 if (seen_) | 213 if (seen_) |
193 return; | 214 return; |
194 | 215 |
195 running_ = true; | 216 running_ = true; |
196 message_loop_runner_ = new MessageLoopRunner; | 217 message_loop_runner_ = new MessageLoopRunner; |
197 message_loop_runner_->Run(); | 218 message_loop_runner_->Run(); |
198 EXPECT_TRUE(seen_); | 219 EXPECT_TRUE(seen_); |
199 } | 220 } |
200 | 221 |
201 void WindowedNotificationObserver::Observe( | 222 void WindowedNotificationObserver::Observe( |
202 int type, | 223 int type, |
203 const NotificationSource& source, | 224 const NotificationSource& source, |
204 const NotificationDetails& details) { | 225 const NotificationDetails& details) { |
205 source_ = source; | 226 source_ = source; |
206 details_ = details; | 227 details_ = details; |
207 if (!callback_.is_null() && !callback_.Run()) | 228 if (!callback_.is_null() && !callback_.Run(source, details)) |
208 return; | 229 return; |
209 | 230 |
210 seen_ = true; | 231 seen_ = true; |
211 if (!running_) | 232 if (!running_) |
212 return; | 233 return; |
213 | 234 |
214 message_loop_runner_->Quit(); | 235 message_loop_runner_->Quit(); |
215 running_ = false; | 236 running_ = false; |
216 } | 237 } |
217 | 238 |
218 } // namespace content | 239 } // namespace content |
OLD | NEW |