| 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/chrome/test/earl_grey/chrome_matchers.h" | 5 #import "ios/chrome/test/earl_grey/chrome_matchers.h" |
| 6 | 6 |
| 7 #import <OCHamcrest/OCHamcrest.h> | 7 #import <OCHamcrest/OCHamcrest.h> |
| 8 | 8 |
| 9 #import <WebKit/WebKit.h> | 9 #import <WebKit/WebKit.h> |
| 10 | 10 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 #import "ios/web/public/test/earl_grey/web_view_matchers.h" | 24 #import "ios/web/public/test/earl_grey/web_view_matchers.h" |
| 25 #include "ui/base/l10n/l10n_util.h" | 25 #include "ui/base/l10n/l10n_util.h" |
| 26 #include "ui/base/test/ios/ui_image_test_utils.h" | 26 #include "ui/base/test/ios/ui_image_test_utils.h" |
| 27 | 27 |
| 28 #if !defined(__has_feature) || !__has_feature(objc_arc) | 28 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 29 #error "This file requires ARC support." | 29 #error "This file requires ARC support." |
| 30 #endif | 30 #endif |
| 31 | 31 |
| 32 namespace { | 32 namespace { |
| 33 | 33 |
| 34 // Script that returns document.body as a string. | |
| 35 NSString* const kGetDocumentBodyJavaScript = | |
| 36 @"document.body ? document.body.textContent : null"; | |
| 37 | |
| 38 // Synchronously returns the result of executed JavaScript. | |
| 39 id ExecuteScriptInStaticController( | |
| 40 StaticHtmlViewController* html_view_controller, | |
| 41 NSString* script) { | |
| 42 __block id result = nil; | |
| 43 __block bool did_finish = false; | |
| 44 web::JavaScriptResultBlock completion_handler = | |
| 45 ^(id script_result, NSError* error) { | |
| 46 result = [script_result copy]; | |
| 47 did_finish = true; | |
| 48 }; | |
| 49 [html_view_controller executeJavaScript:script | |
| 50 completionHandler:completion_handler]; | |
| 51 | |
| 52 GREYAssert( | |
| 53 testing::WaitUntilConditionOrTimeout(testing::kWaitForJSCompletionTimeout, | |
| 54 ^{ | |
| 55 return did_finish; | |
| 56 }), | |
| 57 @"JavaScript did not complete"); | |
| 58 | |
| 59 return result; | |
| 60 } | |
| 61 | |
| 62 // TODO(crbug.com/684142): This matcher uses too many implementation details, | |
| 63 // it would be good to replace it. | |
| 64 id<GREYMatcher> WebViewWithNavDelegateOfClass(Class cls) { | |
| 65 MatchesBlock matches = ^BOOL(UIView* view) { | |
| 66 return [view isKindOfClass:[WKWebView class]] && | |
| 67 [base::mac::ObjCCast<WKWebView>(view).navigationDelegate | |
| 68 isKindOfClass:cls]; | |
| 69 }; | |
| 70 | |
| 71 DescribeToBlock describe = ^(id<GREYDescription> description) { | |
| 72 [description appendText:@"web view with "]; | |
| 73 [description appendText:NSStringFromClass(cls)]; | |
| 74 [description appendText:@"navigation delegate"]; | |
| 75 }; | |
| 76 | |
| 77 return [[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches | |
| 78 descriptionBlock:describe]; | |
| 79 } | |
| 80 | |
| 81 id<GREYMatcher> CollectionViewSwitchIsOn(BOOL is_on) { | 34 id<GREYMatcher> CollectionViewSwitchIsOn(BOOL is_on) { |
| 82 MatchesBlock matches = ^BOOL(id element) { | 35 MatchesBlock matches = ^BOOL(id element) { |
| 83 CollectionViewSwitchCell* switch_cell = | 36 CollectionViewSwitchCell* switch_cell = |
| 84 base::mac::ObjCCastStrict<CollectionViewSwitchCell>(element); | 37 base::mac::ObjCCastStrict<CollectionViewSwitchCell>(element); |
| 85 UISwitch* switch_view = switch_cell.switchView; | 38 UISwitch* switch_view = switch_cell.switchView; |
| 86 return (switch_view.on && is_on) || (!switch_view.on && !is_on); | 39 return (switch_view.on && is_on) || (!switch_view.on && !is_on); |
| 87 }; | 40 }; |
| 88 DescribeToBlock describe = ^void(id<GREYDescription> description) { | 41 DescribeToBlock describe = ^void(id<GREYDescription> description) { |
| 89 NSString* name = | 42 NSString* name = |
| 90 [NSString stringWithFormat:@"collectionViewSwitchInState(%@)", | 43 [NSString stringWithFormat:@"collectionViewSwitchInState(%@)", |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 } | 92 } |
| 140 | 93 |
| 141 id<GREYMatcher> WebViewContainingText(std::string text) { | 94 id<GREYMatcher> WebViewContainingText(std::string text) { |
| 142 return web::WebViewContainingText(std::move(text), GetCurrentWebState()); | 95 return web::WebViewContainingText(std::move(text), GetCurrentWebState()); |
| 143 } | 96 } |
| 144 | 97 |
| 145 id<GREYMatcher> WebViewNotContainingText(std::string text) { | 98 id<GREYMatcher> WebViewNotContainingText(std::string text) { |
| 146 return web::WebViewNotContainingText(std::move(text), GetCurrentWebState()); | 99 return web::WebViewNotContainingText(std::move(text), GetCurrentWebState()); |
| 147 } | 100 } |
| 148 | 101 |
| 149 id<GREYMatcher> StaticHtmlViewContainingText(NSString* text) { | |
| 150 // The WKWebView in a static HTML view isn't part of a webState, but it | |
| 151 // does have the StaticHtmlViewController as its navigation delegate. | |
| 152 MatchesBlock matches = ^BOOL(WKWebView* webView) { | |
| 153 StaticHtmlViewController* html_view_controller = | |
| 154 base::mac::ObjCCast<StaticHtmlViewController>( | |
| 155 webView.navigationDelegate); | |
| 156 | |
| 157 __block BOOL did_succeed = NO; | |
| 158 NSDate* deadline = | |
| 159 [NSDate dateWithTimeIntervalSinceNow:testing::kWaitForUIElementTimeout]; | |
| 160 while (([[NSDate date] compare:deadline] != NSOrderedDescending) && | |
| 161 !did_succeed) { | |
| 162 id result = ExecuteScriptInStaticController(html_view_controller, | |
| 163 kGetDocumentBodyJavaScript); | |
| 164 if ([result isKindOfClass:[NSString class]]) { | |
| 165 NSString* body = base::mac::ObjCCast<NSString>(result); | |
| 166 did_succeed = [body containsString:text]; | |
| 167 } | |
| 168 base::test::ios::SpinRunLoopWithMaxDelay( | |
| 169 base::TimeDelta::FromSecondsD(testing::kSpinDelaySeconds)); | |
| 170 } | |
| 171 return did_succeed; | |
| 172 }; | |
| 173 | |
| 174 DescribeToBlock describe = ^(id<GREYDescription> description) { | |
| 175 [description appendText:@"static HTML web view containing "]; | |
| 176 [description appendText:text]; | |
| 177 }; | |
| 178 | |
| 179 return grey_allOf( | |
| 180 WebViewWithNavDelegateOfClass([StaticHtmlViewController class]), | |
| 181 [[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches | |
| 182 descriptionBlock:describe], | |
| 183 nil); | |
| 184 } | |
| 185 | |
| 186 id<GREYMatcher> WebViewContainingBlockedImage(std::string image_id) { | 102 id<GREYMatcher> WebViewContainingBlockedImage(std::string image_id) { |
| 187 return web::WebViewContainingBlockedImage( | 103 return web::WebViewContainingBlockedImage( |
| 188 std::move(image_id), chrome_test_util::GetCurrentWebState()); | 104 std::move(image_id), chrome_test_util::GetCurrentWebState()); |
| 189 } | 105 } |
| 190 | 106 |
| 191 id<GREYMatcher> WebViewContainingLoadedImage(std::string image_id) { | 107 id<GREYMatcher> WebViewContainingLoadedImage(std::string image_id) { |
| 192 return web::WebViewContainingLoadedImage( | 108 return web::WebViewContainingLoadedImage( |
| 193 std::move(image_id), chrome_test_util::GetCurrentWebState()); | 109 std::move(image_id), chrome_test_util::GetCurrentWebState()); |
| 194 } | 110 } |
| 195 | 111 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 | 165 |
| 250 id<GREYMatcher> OpenLinkInNewTabButton() { | 166 id<GREYMatcher> OpenLinkInNewTabButton() { |
| 251 return ButtonWithAccessibilityLabelId(IDS_IOS_CONTENT_CONTEXT_OPENLINKNEWTAB); | 167 return ButtonWithAccessibilityLabelId(IDS_IOS_CONTENT_CONTEXT_OPENLINKNEWTAB); |
| 252 } | 168 } |
| 253 | 169 |
| 254 id<GREYMatcher> NavigationBarDoneButton() { | 170 id<GREYMatcher> NavigationBarDoneButton() { |
| 255 return ButtonWithAccessibilityLabelId(IDS_IOS_NAVIGATION_BAR_DONE_BUTTON); | 171 return ButtonWithAccessibilityLabelId(IDS_IOS_NAVIGATION_BAR_DONE_BUTTON); |
| 256 } | 172 } |
| 257 | 173 |
| 258 } // namespace chrome_test_util | 174 } // namespace chrome_test_util |
| OLD | NEW |