Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
|
Paweł Hajdan Jr.
2015/01/30 12:24:29
nit: 2015
| |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #import "base/test/ios/wait_util.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/mac/scoped_nsobject.h" | |
| 9 #include "base/message_loop/message_loop.h" | |
| 10 | |
| 11 namespace { | |
| 12 // Constants for UI wait loop and timing calculation. | |
| 13 const NSTimeInterval kSpinDelaySeconds = 0.01; | |
| 14 const NSTimeInterval kDefaultDelaySeconds = 10.0; | |
| 15 } // namespace | |
| 16 | |
| 17 namespace base { | |
| 18 | |
| 19 TimeDelta TimeUntilCondition(ProceduralBlock action, | |
| 20 ConditionBlock condition, | |
| 21 MessageLoop* message_loop, | |
| 22 TimeDelta timeout) { | |
| 23 NSDate* startTime = [NSDate date]; | |
| 24 if (action) | |
| 25 action(); | |
| 26 const NSTimeInterval timeout_in_seconds = timeout.InSecondsF(); | |
| 27 while ((-[startTime timeIntervalSinceNow]) < timeout_in_seconds && | |
|
Paweł Hajdan Jr.
2015/01/30 12:24:29
I strongly second Nico's comment about waiting pro
| |
| 28 (!condition || !condition())) { | |
| 29 SpinRunLoopWithMaxDelay(TimeDelta::FromSecondsD(kSpinDelaySeconds)); | |
| 30 if (message_loop) { | |
| 31 message_loop->RunUntilIdle(); | |
| 32 } | |
| 33 } | |
| 34 NSTimeInterval elapsed = -[startTime timeIntervalSinceNow]; | |
| 35 // If DCHECK is ever hit, check if |action| is doing something that is | |
| 36 // taking an unreasonably long time, or if |condition| does not come | |
| 37 // true quickly enough. Increase |timeout| only if necessary. | |
| 38 DCHECK(!condition || condition()); | |
| 39 return TimeDelta::FromSecondsD(elapsed); | |
| 40 } | |
| 41 | |
| 42 TimeDelta TimeUntilCondition(ProceduralBlock action, ConditionBlock condition) { | |
| 43 return TimeUntilCondition(action, condition, nullptr, | |
| 44 TimeDelta::FromSecondsD(kDefaultDelaySeconds)); | |
| 45 } | |
| 46 | |
| 47 void WaitUntilCondition(ConditionBlock condition, | |
| 48 MessageLoop* message_loop, | |
| 49 TimeDelta timeout) { | |
| 50 TimeUntilCondition(nil, condition, message_loop, timeout); | |
| 51 } | |
| 52 | |
| 53 void WaitUntilCondition(ConditionBlock condition, MessageLoop* message_loop) { | |
| 54 WaitUntilCondition(condition, message_loop, | |
| 55 TimeDelta::FromSecondsD(kDefaultDelaySeconds)); | |
| 56 } | |
| 57 | |
| 58 void WaitUntilCondition(ConditionBlock condition) { | |
| 59 WaitUntilCondition(condition, nullptr); | |
| 60 } | |
| 61 | |
| 62 void Wait(TimeDelta timeout) { | |
| 63 return WaitUntilCondition(nil, nullptr, timeout); | |
| 64 } | |
| 65 | |
| 66 void SpinRunLoopWithMaxDelay(TimeDelta max_delay) { | |
| 67 scoped_nsobject<NSDate> beforeDate( | |
| 68 [[NSDate alloc] initWithTimeIntervalSinceNow:max_delay.InSecondsF()]); | |
| 69 [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode | |
| 70 beforeDate:beforeDate]; | |
| 71 } | |
| 72 | |
| 73 } // namespace base | |
| OLD | NEW |