Commit 038742bf authored by baxley's avatar baxley Committed by Commit bot

Create EarlGrey wait utility.

EarlGrey typically handles synchronization with the UI, but it is
useful when manipulating non-UI APIs to be able to wait for a certain
state.

BUG=610837

Review-Url: https://codereview.chromium.org/2044433003
Cr-Commit-Position: refs/heads/master@{#398182}
parent 8b06121b
......@@ -9,6 +9,11 @@ config("earl_grey_support_config") {
source_set("earl_grey_support") {
testonly = true
deps = [
"//ios/third_party/earl_grey",
]
sources = [
"wait_util.h",
"wait_util.mm",
......
......@@ -10,6 +10,9 @@
# GN version: //ios/testing/earl_grey:earl_grey_support
'target_name': 'earl_grey_support',
'type': 'static_library',
'dependencies': [
'<(DEPTH)/ios/third_party/earl_grey/earl_grey.gyp:EarlGrey',
],
'sources': [
'wait_util.h',
'wait_util.mm',
......
......@@ -17,6 +17,10 @@ extern const NSTimeInterval kWaitForUIElementTimeout;
// Constant for timeout in seconds while waiting for JavaScript completion.
extern const NSTimeInterval kWaitForJSCompletionTimeout;
}
// Waits until |condition| is true, or induces GREYAssert after |timeout|.
void WaitUntilCondition(NSTimeInterval timeout, bool (^condition)(void));
} // namespace testing
#endif // IOS_TESTING_EARL_GREY_WAIT_UTIL_H_
\ No newline at end of file
......@@ -4,9 +4,24 @@
#include "ios/testing/earl_grey/wait_util.h"
#import <EarlGrey/EarlGrey.h>
#include "base/test/ios/wait_util.h"
namespace testing {
const NSTimeInterval kSpinDelaySeconds = 0.01;
const NSTimeInterval kWaitForUIElementTimeout = 4.0;
const NSTimeInterval kWaitForJSCompletionTimeout = 2.0;
const NSTimeInterval kWaitForUIElementTimeout = 4.0;
void WaitUntilCondition(NSTimeInterval timeout, bool (^condition)(void)) {
NSDate* deadline = [NSDate dateWithTimeIntervalSinceNow:timeout];
while (!condition() &&
[[NSDate date] compare:deadline] != NSOrderedDescending) {
base::test::ios::SpinRunLoopWithMaxDelay(
base::TimeDelta::FromSecondsD(testing::kSpinDelaySeconds));
}
GREYAssert(condition(), @"Timeout waiting for condition.");
}
} // namespace testing
......@@ -27,14 +27,9 @@ void TapWebViewElementWithId(web::WebState* web_state,
did_complete = true;
}));
// TODO(crbug.com/610837): Re-factor wait code into common location.
NSDate* deadline = [NSDate
dateWithTimeIntervalSinceNow:testing::kWaitForJSCompletionTimeout];
while (([[NSDate date] compare:deadline] != NSOrderedDescending) &&
!did_complete) {
base::test::ios::SpinRunLoopWithMaxDelay(
base::TimeDelta::FromSecondsD(testing::kSpinDelaySeconds));
}
testing::WaitUntilCondition(testing::kWaitForJSCompletionTimeout, ^{
return did_complete;
});
}
} // namespace test
......
......@@ -54,16 +54,10 @@ id<GREYMatcher> addressField() {
return NO;
}
UITextField* textField = base::mac::ObjCCastStrict<UITextField>(view);
NSDate* deadline =
[NSDate dateWithTimeIntervalSinceNow:testing::kWaitForUIElementTimeout];
while ([[NSDate date] compare:deadline] != NSOrderedDescending) {
if ([textField.text isEqualToString:base::SysUTF8ToNSString(text)]) {
return YES;
}
base::test::ios::SpinRunLoopWithMaxDelay(
base::TimeDelta::FromSecondsD(testing::kSpinDelaySeconds));
}
return NO;
testing::WaitUntilCondition(testing::kWaitForUIElementTimeout, ^bool() {
return [textField.text isEqualToString:base::SysUTF8ToNSString(text)];
});
return YES;
};
DescribeToBlock describe = ^(id<GREYDescription> description) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment