| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #import "ios/web/public/test/earl_grey/web_view_matchers.h" | 5 #import "ios/web/public/test/earl_grey/web_view_matchers.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #import <WebKit/WebKit.h> | 9 #import <WebKit/WebKit.h> |
| 10 | 10 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 // ../web_view_matchers.mm:ll:cc: error: call to implicitly-deleted copy | 49 // ../web_view_matchers.mm:ll:cc: error: call to implicitly-deleted copy |
| 50 // constructor of 'std::unique_ptr<base::Value>' | 50 // constructor of 'std::unique_ptr<base::Value>' |
| 51 std::unique_ptr<base::Value> stack_result = std::move(result); | 51 std::unique_ptr<base::Value> stack_result = std::move(result); |
| 52 return stack_result; | 52 return stack_result; |
| 53 } | 53 } |
| 54 | 54 |
| 55 } // namespace | 55 } // namespace |
| 56 | 56 |
| 57 namespace web { | 57 namespace web { |
| 58 | 58 |
| 59 id<GREYMatcher> webViewInWebState(web::WebState* webState) { | 59 id<GREYMatcher> webViewInWebState(WebState* web_state) { |
| 60 return [GREYMatchers matcherForWebViewInWebState:webState]; | |
| 61 } | |
| 62 | |
| 63 id<GREYMatcher> webViewContainingText(const std::string& text, | |
| 64 web::WebState* webState) { | |
| 65 return | |
| 66 [GREYMatchers matcherForWebViewContainingText:text inWebState:webState]; | |
| 67 } | |
| 68 | |
| 69 id<GREYMatcher> webViewCssSelector(const std::string& selector, | |
| 70 web::WebState* webState) { | |
| 71 return | |
| 72 [GREYMatchers matcherForWebWithCSSSelector:selector inWebState:webState]; | |
| 73 } | |
| 74 | |
| 75 id<GREYMatcher> webViewScrollView(web::WebState* webState) { | |
| 76 return [GREYMatchers matcherForWebViewScrollViewInWebState:webState]; | |
| 77 } | |
| 78 | |
| 79 } // namespace web | |
| 80 | |
| 81 @implementation GREYMatchers (WebViewAdditions) | |
| 82 | |
| 83 + (id<GREYMatcher>)matcherForWebViewInWebState:(web::WebState*)webState { | |
| 84 MatchesBlock matches = ^BOOL(UIView* view) { | 60 MatchesBlock matches = ^BOOL(UIView* view) { |
| 85 return [view isKindOfClass:[WKWebView class]] && | 61 return [view isKindOfClass:[WKWebView class]] && |
| 86 [view isDescendantOfView:webState->GetView()]; | 62 [view isDescendantOfView:web_state->GetView()]; |
| 87 }; | 63 }; |
| 88 | 64 |
| 89 DescribeToBlock describe = ^(id<GREYDescription> description) { | 65 DescribeToBlock describe = ^(id<GREYDescription> description) { |
| 90 [description appendText:@"web view in web state"]; | 66 [description appendText:@"web view in web state"]; |
| 91 }; | 67 }; |
| 92 | 68 |
| 93 return [[[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches | 69 return [[[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches |
| 94 descriptionBlock:describe] | 70 descriptionBlock:describe] |
| 95 autorelease]; | 71 autorelease]; |
| 96 } | 72 } |
| 97 | 73 |
| 98 + (id<GREYMatcher>)matcherForWebViewContainingText:(const std::string&)text | 74 id<GREYMatcher> webViewContainingText(std::string text, WebState* web_state) { |
| 99 inWebState:(web::WebState*)webState { | |
| 100 std::string textCopyForBlock = text; | |
| 101 MatchesBlock matches = ^BOOL(WKWebView*) { | 75 MatchesBlock matches = ^BOOL(WKWebView*) { |
| 102 __block BOOL didSucceed = NO; | 76 __block BOOL did_succeed = NO; |
| 103 NSDate* deadline = | 77 NSDate* deadline = |
| 104 [NSDate dateWithTimeIntervalSinceNow:testing::kWaitForUIElementTimeout]; | 78 [NSDate dateWithTimeIntervalSinceNow:testing::kWaitForUIElementTimeout]; |
| 105 while (([[NSDate date] compare:deadline] != NSOrderedDescending) && | 79 while (([[NSDate date] compare:deadline] != NSOrderedDescending) && |
| 106 !didSucceed) { | 80 !did_succeed) { |
| 107 std::unique_ptr<base::Value> value = | 81 std::unique_ptr<base::Value> value = |
| 108 ExecuteScript(webState, kGetDocumentBodyJavaScript); | 82 ExecuteScript(web_state, kGetDocumentBodyJavaScript); |
| 109 std::string body; | 83 std::string body; |
| 110 if (value && value->GetAsString(&body)) { | 84 if (value && value->GetAsString(&body)) { |
| 111 didSucceed = body.find(textCopyForBlock) != std::string::npos; | 85 did_succeed = body.find(text) != std::string::npos; |
| 112 } | 86 } |
| 113 base::test::ios::SpinRunLoopWithMaxDelay( | 87 base::test::ios::SpinRunLoopWithMaxDelay( |
| 114 base::TimeDelta::FromSecondsD(testing::kSpinDelaySeconds)); | 88 base::TimeDelta::FromSecondsD(testing::kSpinDelaySeconds)); |
| 115 } | 89 } |
| 116 return didSucceed; | 90 return did_succeed; |
| 117 }; | 91 }; |
| 118 | 92 |
| 119 DescribeToBlock describe = ^(id<GREYDescription> description) { | 93 DescribeToBlock describe = ^(id<GREYDescription> description) { |
| 120 [description appendText:@"web view containing "]; | 94 [description appendText:@"web view containing "]; |
| 121 [description appendText:base::SysUTF8ToNSString(textCopyForBlock)]; | 95 [description appendText:base::SysUTF8ToNSString(text)]; |
| 122 }; | 96 }; |
| 123 | 97 |
| 124 return grey_allOf(webViewInWebState(webState), | 98 return grey_allOf(webViewInWebState(web_state), |
| 125 [[[GREYElementMatcherBlock alloc] | 99 [[[GREYElementMatcherBlock alloc] |
| 126 initWithMatchesBlock:matches | 100 initWithMatchesBlock:matches |
| 127 descriptionBlock:describe] autorelease], | 101 descriptionBlock:describe] autorelease], |
| 128 nil); | 102 nil); |
| 129 } | 103 } |
| 130 | 104 |
| 131 + (id<GREYMatcher>)matcherForWebWithCSSSelector:(const std::string&)selector | 105 id<GREYMatcher> webViewCssSelector(std::string selector, WebState* web_state) { |
| 132 inWebState:(web::WebState*)webState { | |
| 133 std::string selectorCopy = selector; | |
| 134 MatchesBlock matches = ^BOOL(WKWebView*) { | 106 MatchesBlock matches = ^BOOL(WKWebView*) { |
| 135 std::string script = base::StringPrintf(kTestCssSelectorJavaScriptTemplate, | 107 std::string script = base::StringPrintf(kTestCssSelectorJavaScriptTemplate, |
| 136 selectorCopy.c_str()); | 108 selector.c_str()); |
| 137 __block bool didSucceed = false; | 109 __block bool did_succeed = false; |
| 138 NSDate* deadline = | 110 NSDate* deadline = |
| 139 [NSDate dateWithTimeIntervalSinceNow:testing::kWaitForUIElementTimeout]; | 111 [NSDate dateWithTimeIntervalSinceNow:testing::kWaitForUIElementTimeout]; |
| 140 while (([[NSDate date] compare:deadline] != NSOrderedDescending) && | 112 while (([[NSDate date] compare:deadline] != NSOrderedDescending) && |
| 141 !didSucceed) { | 113 !did_succeed) { |
| 142 std::unique_ptr<base::Value> value = ExecuteScript(webState, script); | 114 std::unique_ptr<base::Value> value = ExecuteScript(web_state, script); |
| 143 if (value) | 115 if (value) |
| 144 value->GetAsBoolean(&didSucceed); | 116 value->GetAsBoolean(&did_succeed); |
| 145 base::test::ios::SpinRunLoopWithMaxDelay( | 117 base::test::ios::SpinRunLoopWithMaxDelay( |
| 146 base::TimeDelta::FromSecondsD(testing::kSpinDelaySeconds)); | 118 base::TimeDelta::FromSecondsD(testing::kSpinDelaySeconds)); |
| 147 } | 119 } |
| 148 return didSucceed; | 120 return did_succeed; |
| 149 }; | 121 }; |
| 150 | 122 |
| 151 DescribeToBlock describe = ^(id<GREYDescription> description) { | 123 DescribeToBlock describe = ^(id<GREYDescription> description) { |
| 152 [description appendText:@"web view selector "]; | 124 [description appendText:@"web view selector "]; |
| 153 [description appendText:base::SysUTF8ToNSString(selectorCopy)]; | 125 [description appendText:base::SysUTF8ToNSString(selector)]; |
| 154 }; | 126 }; |
| 155 | 127 |
| 156 return grey_allOf(webViewInWebState(webState), | 128 return grey_allOf(webViewInWebState(web_state), |
| 157 [[[GREYElementMatcherBlock alloc] | 129 [[[GREYElementMatcherBlock alloc] |
| 158 initWithMatchesBlock:matches | 130 initWithMatchesBlock:matches |
| 159 descriptionBlock:describe] autorelease], | 131 descriptionBlock:describe] autorelease], |
| 160 nil); | 132 nil); |
| 161 } | 133 } |
| 162 | 134 |
| 163 + (id<GREYMatcher>)matcherForWebViewScrollViewInWebState: | 135 id<GREYMatcher> webViewScrollView(WebState* web_state) { |
| 164 (web::WebState*)webState { | |
| 165 MatchesBlock matches = ^BOOL(UIView* view) { | 136 MatchesBlock matches = ^BOOL(UIView* view) { |
| 166 return [view isKindOfClass:[UIScrollView class]] && | 137 return [view isKindOfClass:[UIScrollView class]] && |
| 167 [view.superview isKindOfClass:[WKWebView class]] && | 138 [view.superview isKindOfClass:[WKWebView class]] && |
| 168 [view isDescendantOfView:webState->GetView()]; | 139 [view isDescendantOfView:web_state->GetView()]; |
| 169 }; | 140 }; |
| 170 | 141 |
| 171 DescribeToBlock describe = ^(id<GREYDescription> description) { | 142 DescribeToBlock describe = ^(id<GREYDescription> description) { |
| 172 [description appendText:@"web view scroll view"]; | 143 [description appendText:@"web view scroll view"]; |
| 173 }; | 144 }; |
| 174 | 145 |
| 175 return [[[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches | 146 return [[[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches |
| 176 descriptionBlock:describe] | 147 descriptionBlock:describe] |
| 177 autorelease]; | 148 autorelease]; |
| 178 } | 149 } |
| 179 | 150 |
| 180 @end | 151 } // namespace web |
| OLD | NEW |