| 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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 GREYAssert( | 46 GREYAssert( |
| 47 testing::WaitUntilConditionOrTimeout(testing::kWaitForJSCompletionTimeout, | 47 testing::WaitUntilConditionOrTimeout(testing::kWaitForJSCompletionTimeout, |
| 48 ^{ | 48 ^{ |
| 49 return did_finish; | 49 return did_finish; |
| 50 }), | 50 }), |
| 51 @"JavaScript did not complete"); | 51 @"JavaScript did not complete"); |
| 52 | 52 |
| 53 return [result autorelease]; | 53 return [result autorelease]; |
| 54 } | 54 } |
| 55 | 55 |
| 56 id<GREYMatcher> webViewWithNavDelegateOfClass(Class cls) { | 56 // TODO(crbug.com/684142): This matcher uses too many implementation details, |
| 57 // it would be good to replace it. |
| 58 id<GREYMatcher> WebViewWithNavDelegateOfClass(Class cls) { |
| 57 MatchesBlock matches = ^BOOL(UIView* view) { | 59 MatchesBlock matches = ^BOOL(UIView* view) { |
| 58 return [view isKindOfClass:[WKWebView class]] && | 60 return [view isKindOfClass:[WKWebView class]] && |
| 59 [base::mac::ObjCCast<WKWebView>(view).navigationDelegate | 61 [base::mac::ObjCCast<WKWebView>(view).navigationDelegate |
| 60 isKindOfClass:cls]; | 62 isKindOfClass:cls]; |
| 61 }; | 63 }; |
| 62 | 64 |
| 63 DescribeToBlock describe = ^(id<GREYDescription> description) { | 65 DescribeToBlock describe = ^(id<GREYDescription> description) { |
| 64 [description appendText:@"web view with "]; | 66 [description appendText:@"web view with "]; |
| 65 [description appendText:NSStringFromClass(cls)]; | 67 [description appendText:NSStringFromClass(cls)]; |
| 66 [description appendText:@"navigation delegate"]; | 68 [description appendText:@"navigation delegate"]; |
| 67 }; | 69 }; |
| 68 | 70 |
| 69 return [[[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches | 71 return [[[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches |
| 70 descriptionBlock:describe] | 72 descriptionBlock:describe] |
| 71 autorelease]; | 73 autorelease]; |
| 72 } | 74 } |
| 73 | 75 |
| 74 id<GREYMatcher> collectionViewSwitchIsOn(BOOL isOn) { | 76 id<GREYMatcher> CollectionViewSwitchIsOn(BOOL isOn) { |
| 75 MatchesBlock matches = ^BOOL(id element) { | 77 MatchesBlock matches = ^BOOL(id element) { |
| 76 CollectionViewSwitchCell* switchCell = | 78 CollectionViewSwitchCell* switchCell = |
| 77 base::mac::ObjCCastStrict<CollectionViewSwitchCell>(element); | 79 base::mac::ObjCCastStrict<CollectionViewSwitchCell>(element); |
| 78 UISwitch* switchView = switchCell.switchView; | 80 UISwitch* switchView = switchCell.switchView; |
| 79 return (switchView.on && isOn) || (!switchView.on && !isOn); | 81 return (switchView.on && isOn) || (!switchView.on && !isOn); |
| 80 }; | 82 }; |
| 81 DescribeToBlock describe = ^void(id<GREYDescription> description) { | 83 DescribeToBlock describe = ^void(id<GREYDescription> description) { |
| 82 NSString* name = | 84 NSString* name = |
| 83 [NSString stringWithFormat:@"collectionViewSwitchInState(%@)", | 85 [NSString stringWithFormat:@"collectionViewSwitchInState(%@)", |
| 84 isOn ? @"ON" : @"OFF"]; | 86 isOn ? @"ON" : @"OFF"]; |
| 85 [description appendText:name]; | 87 [description appendText:name]; |
| 86 }; | 88 }; |
| 87 return [[[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches | 89 return [[[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches |
| 88 descriptionBlock:describe] | 90 descriptionBlock:describe] |
| 89 autorelease]; | 91 autorelease]; |
| 90 } | 92 } |
| 91 | 93 |
| 92 } // namespace | 94 } // namespace |
| 93 | 95 |
| 94 namespace chrome_test_util { | 96 namespace chrome_test_util { |
| 95 | 97 |
| 96 id<GREYMatcher> buttonWithAccessibilityLabel(NSString* label) { | 98 id<GREYMatcher> ButtonWithAccessibilityLabel(NSString* label) { |
| 97 return grey_allOf(grey_accessibilityLabel(label), | 99 return grey_allOf(grey_accessibilityLabel(label), |
| 98 grey_accessibilityTrait(UIAccessibilityTraitButton), nil); | 100 grey_accessibilityTrait(UIAccessibilityTraitButton), nil); |
| 99 } | 101 } |
| 100 | 102 |
| 101 id<GREYMatcher> buttonWithAccessibilityLabelId(int message_id) { | 103 id<GREYMatcher> ButtonWithAccessibilityLabelId(int message_id) { |
| 102 return buttonWithAccessibilityLabel( | 104 return ButtonWithAccessibilityLabel( |
| 103 l10n_util::GetNSStringWithFixup(message_id)); | 105 l10n_util::GetNSStringWithFixup(message_id)); |
| 104 } | 106 } |
| 105 | 107 |
| 106 id<GREYMatcher> staticTextWithAccessibilityLabel(NSString* label) { | 108 id<GREYMatcher> StaticTextWithAccessibilityLabel(NSString* label) { |
| 107 return grey_allOf(grey_accessibilityLabel(label), | 109 return grey_allOf(grey_accessibilityLabel(label), |
| 108 grey_accessibilityTrait(UIAccessibilityTraitStaticText), | 110 grey_accessibilityTrait(UIAccessibilityTraitStaticText), |
| 109 nil); | 111 nil); |
| 110 } | 112 } |
| 111 | 113 |
| 112 id<GREYMatcher> staticTextWithAccessibilityLabelId(int message_id) { | 114 id<GREYMatcher> StaticTextWithAccessibilityLabelId(int message_id) { |
| 113 return staticTextWithAccessibilityLabel( | 115 return StaticTextWithAccessibilityLabel( |
| 114 l10n_util::GetNSStringWithFixup(message_id)); | 116 l10n_util::GetNSStringWithFixup(message_id)); |
| 115 } | 117 } |
| 116 | 118 |
| 117 id<GREYMatcher> webViewBelongingToWebController() { | 119 id<GREYMatcher> WebViewContainingText(std::string text) { |
| 118 return webViewWithNavDelegateOfClass(NSClassFromString(@"CRWWebController")); | 120 return web::WebViewContainingText(std::move(text), GetCurrentWebState()); |
| 119 } | 121 } |
| 120 | 122 |
| 121 id<GREYMatcher> webViewContainingText(std::string text) { | 123 id<GREYMatcher> WebViewNotContainingText(std::string text) { |
| 122 return web::webViewContainingText(std::move(text), GetCurrentWebState()); | 124 return web::WebViewNotContainingText(std::move(text), GetCurrentWebState()); |
| 123 } | 125 } |
| 124 | 126 |
| 125 id<GREYMatcher> webViewNotContainingText(std::string text) { | 127 id<GREYMatcher> StaticHtmlViewContainingText(NSString* text) { |
| 126 return web::webViewNotContainingText(std::move(text), GetCurrentWebState()); | |
| 127 } | |
| 128 | |
| 129 id<GREYMatcher> staticHtmlViewContainingText(NSString* text) { | |
| 130 // The WKWebView in a static HTML view isn't part of a webState, but it | 128 // The WKWebView in a static HTML view isn't part of a webState, but it |
| 131 // does have the StaticHtmlViewController as its navigation delegate. | 129 // does have the StaticHtmlViewController as its navigation delegate. |
| 132 MatchesBlock matches = ^BOOL(WKWebView* webView) { | 130 MatchesBlock matches = ^BOOL(WKWebView* webView) { |
| 133 StaticHtmlViewController* html_view_controller = | 131 StaticHtmlViewController* html_view_controller = |
| 134 base::mac::ObjCCast<StaticHtmlViewController>( | 132 base::mac::ObjCCast<StaticHtmlViewController>( |
| 135 webView.navigationDelegate); | 133 webView.navigationDelegate); |
| 136 | 134 |
| 137 __block BOOL did_succeed = NO; | 135 __block BOOL did_succeed = NO; |
| 138 NSDate* deadline = | 136 NSDate* deadline = |
| 139 [NSDate dateWithTimeIntervalSinceNow:testing::kWaitForUIElementTimeout]; | 137 [NSDate dateWithTimeIntervalSinceNow:testing::kWaitForUIElementTimeout]; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 150 } | 148 } |
| 151 return did_succeed; | 149 return did_succeed; |
| 152 }; | 150 }; |
| 153 | 151 |
| 154 DescribeToBlock describe = ^(id<GREYDescription> description) { | 152 DescribeToBlock describe = ^(id<GREYDescription> description) { |
| 155 [description appendText:@"static HTML web view containing "]; | 153 [description appendText:@"static HTML web view containing "]; |
| 156 [description appendText:text]; | 154 [description appendText:text]; |
| 157 }; | 155 }; |
| 158 | 156 |
| 159 return grey_allOf( | 157 return grey_allOf( |
| 160 webViewWithNavDelegateOfClass([StaticHtmlViewController class]), | 158 WebViewWithNavDelegateOfClass([StaticHtmlViewController class]), |
| 161 [[[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches | 159 [[[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches |
| 162 descriptionBlock:describe] | 160 descriptionBlock:describe] |
| 163 autorelease], | 161 autorelease], |
| 164 nil); | 162 nil); |
| 165 } | 163 } |
| 166 | 164 |
| 167 id<GREYMatcher> cancelButton() { | 165 id<GREYMatcher> CancelButton() { |
| 168 return buttonWithAccessibilityLabelId(IDS_CANCEL); | 166 return ButtonWithAccessibilityLabelId(IDS_CANCEL); |
| 169 } | 167 } |
| 170 | 168 |
| 171 id<GREYMatcher> forwardButton() { | 169 id<GREYMatcher> ForwardButton() { |
| 172 return buttonWithAccessibilityLabelId(IDS_ACCNAME_FORWARD); | 170 return ButtonWithAccessibilityLabelId(IDS_ACCNAME_FORWARD); |
| 173 } | 171 } |
| 174 | 172 |
| 175 id<GREYMatcher> backButton() { | 173 id<GREYMatcher> BackButton() { |
| 176 return buttonWithAccessibilityLabelId(IDS_ACCNAME_BACK); | 174 return ButtonWithAccessibilityLabelId(IDS_ACCNAME_BACK); |
| 177 } | 175 } |
| 178 | 176 |
| 179 id<GREYMatcher> reloadButton() { | 177 id<GREYMatcher> ReloadButton() { |
| 180 return buttonWithAccessibilityLabelId(IDS_IOS_ACCNAME_RELOAD); | 178 return ButtonWithAccessibilityLabelId(IDS_IOS_ACCNAME_RELOAD); |
| 181 } | 179 } |
| 182 | 180 |
| 183 id<GREYMatcher> stopButton() { | 181 id<GREYMatcher> StopButton() { |
| 184 return buttonWithAccessibilityLabelId(IDS_IOS_ACCNAME_STOP); | 182 return ButtonWithAccessibilityLabelId(IDS_IOS_ACCNAME_STOP); |
| 185 } | 183 } |
| 186 | 184 |
| 187 id<GREYMatcher> omnibox() { | 185 id<GREYMatcher> Omnibox() { |
| 188 return grey_kindOfClass([OmniboxTextFieldIOS class]); | 186 return grey_kindOfClass([OmniboxTextFieldIOS class]); |
| 189 } | 187 } |
| 190 | 188 |
| 191 id<GREYMatcher> pageSecurityInfoButton() { | 189 id<GREYMatcher> PageSecurityInfoButton() { |
| 192 return grey_accessibilityLabel(@"Page Security Info"); | 190 return grey_accessibilityLabel(@"Page Security Info"); |
| 193 } | 191 } |
| 194 | 192 |
| 195 id<GREYMatcher> omniboxText(std::string text) { | 193 id<GREYMatcher> OmniboxText(std::string text) { |
| 196 return grey_allOf(omnibox(), | 194 return grey_allOf(Omnibox(), |
| 197 hasProperty(@"text", base::SysUTF8ToNSString(text)), nil); | 195 hasProperty(@"text", base::SysUTF8ToNSString(text)), nil); |
| 198 } | 196 } |
| 199 | 197 |
| 200 id<GREYMatcher> toolsMenuButton() { | 198 id<GREYMatcher> ToolsMenuButton() { |
| 201 return grey_allOf(grey_accessibilityID(kToolbarToolsMenuButtonIdentifier), | 199 return grey_allOf(grey_accessibilityID(kToolbarToolsMenuButtonIdentifier), |
| 202 grey_sufficientlyVisible(), nil); | 200 grey_sufficientlyVisible(), nil); |
| 203 } | 201 } |
| 204 | 202 |
| 205 id<GREYMatcher> shareButton() { | 203 id<GREYMatcher> ShareButton() { |
| 206 return buttonWithAccessibilityLabelId(IDS_IOS_TOOLS_MENU_SHARE); | 204 return ButtonWithAccessibilityLabelId(IDS_IOS_TOOLS_MENU_SHARE); |
| 207 } | 205 } |
| 208 | 206 |
| 209 id<GREYMatcher> showTabsButton() { | 207 id<GREYMatcher> ShowTabsButton() { |
| 210 return grey_allOf(grey_accessibilityID(kToolbarStackButtonIdentifier), | 208 return grey_allOf(grey_accessibilityID(kToolbarStackButtonIdentifier), |
| 211 grey_sufficientlyVisible(), nil); | 209 grey_sufficientlyVisible(), nil); |
| 212 } | 210 } |
| 213 | 211 |
| 214 id<GREYMatcher> collectionViewSwitchCell(NSString* accessibilityIdentifier, | 212 id<GREYMatcher> CollectionViewSwitchCell(NSString* accessibilityIdentifier, |
| 215 BOOL isOn) { | 213 BOOL isOn) { |
| 216 return grey_allOf(grey_accessibilityID(accessibilityIdentifier), | 214 return grey_allOf(grey_accessibilityID(accessibilityIdentifier), |
| 217 collectionViewSwitchIsOn(isOn), grey_sufficientlyVisible(), | 215 CollectionViewSwitchIsOn(isOn), grey_sufficientlyVisible(), |
| 218 nil); | 216 nil); |
| 219 } | 217 } |
| 220 | 218 |
| 221 } // namespace chrome_test_util | 219 } // namespace chrome_test_util |
| OLD | NEW |