Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(266)

Unified Diff: base/test/ios/wait_util.mm

Issue 1124763003: Update from https://crrev.com/327068 (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: update nacl, buildtools, fix display_change_notifier_unittest Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/test/ios/wait_util.mm
diff --git a/base/test/ios/wait_util.mm b/base/test/ios/wait_util.mm
new file mode 100644
index 0000000000000000000000000000000000000000..e8b3a0f220cfbe16846e651dacfc56b719a1c213
--- /dev/null
+++ b/base/test/ios/wait_util.mm
@@ -0,0 +1,62 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#import "base/test/ios/wait_util.h"
+
+#import <Foundation/Foundation.h>
+
+#include "base/logging.h"
+#include "base/mac/scoped_nsobject.h"
+#include "base/message_loop/message_loop.h"
+#include "base/test/test_timeouts.h"
+#include "base/timer/elapsed_timer.h"
+
+namespace base {
+namespace test {
+namespace ios {
+
+TimeDelta TimeUntilCondition(ProceduralBlock action,
+ ConditionBlock condition,
+ MessageLoop* message_loop,
+ TimeDelta timeout) {
+ ElapsedTimer timer;
+ if (action)
+ action();
+ if (timeout == TimeDelta())
+ timeout = TestTimeouts::action_timeout();
+ const TimeDelta spin_delay(TimeDelta::FromMilliseconds(10));
+ while (timer.Elapsed() < timeout && (!condition || !condition())) {
+ SpinRunLoopWithMaxDelay(spin_delay);
+ if (message_loop) {
+ message_loop->RunUntilIdle();
+ }
+ }
+ TimeDelta elapsed = timer.Elapsed();
+ // If DCHECK is ever hit, check if |action| is doing something that is
+ // taking an unreasonably long time, or if |condition| does not come
+ // true quickly enough. Increase |timeout| only if necessary.
+ DCHECK(!condition || condition());
+ return elapsed;
+}
+
+void WaitUntilCondition(ConditionBlock condition,
+ MessageLoop* message_loop,
+ TimeDelta timeout) {
+ TimeUntilCondition(nil, condition, message_loop, timeout);
+}
+
+void WaitUntilCondition(ConditionBlock condition) {
+ WaitUntilCondition(condition, nullptr, TimeDelta());
+}
+
+void SpinRunLoopWithMaxDelay(TimeDelta max_delay) {
+ scoped_nsobject<NSDate> beforeDate(
+ [[NSDate alloc] initWithTimeIntervalSinceNow:max_delay.InSecondsF()]);
+ [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
+ beforeDate:beforeDate];
+}
+
+} // namespace ios
+} // namespace test
+} // namespace base

Powered by Google App Engine
This is Rietveld 408576698