Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(23)

Side by Side Diff: ios/chrome/test/earl_grey/chrome_matchers.mm

Issue 2580333003: Upstream Chrome on iOS source code [10/11]. (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #import "ios/chrome/test/earl_grey/chrome_matchers.h"
6
7 #import <OCHamcrest/OCHamcrest.h>
8
9 #import <WebKit/WebKit.h>
10
11 #include "base/mac/foundation_util.h"
12 #include "base/strings/sys_string_conversions.h"
13 #include "base/test/ios/wait_util.h"
14 #include "components/strings/grit/components_strings.h"
15 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_switch_item .h"
16 #import "ios/chrome/browser/ui/omnibox/omnibox_text_field_ios.h"
17 #import "ios/chrome/browser/ui/static_content/static_html_view_controller.h"
18 #import "ios/chrome/browser/ui/toolbar/toolbar_controller.h"
19 #include "ios/chrome/grit/ios_strings.h"
20 #import "ios/chrome/test/app/chrome_test_util.h"
21 #import "ios/testing/wait_util.h"
22 #import "ios/web/public/block_types.h"
23 #import "ios/web/public/test/earl_grey/web_view_matchers.h"
24 #include "ui/base/l10n/l10n_util.h"
25
26 namespace {
27
28 // Script that returns document.body as a string.
29 NSString* const kGetDocumentBodyJavaScript =
30 @"document.body ? document.body.textContent : null";
31
32 // Synchronously returns the result of executed JavaScript.
33 id ExecuteScriptInStaticController(
34 StaticHtmlViewController* html_view_controller,
35 NSString* script) {
36 __block id result = nil;
37 __block bool did_finish = false;
38 web::JavaScriptResultBlock completion_handler =
39 ^(id script_result, NSError* error) {
40 result = [script_result copy];
41 did_finish = true;
42 };
43 [html_view_controller executeJavaScript:script
44 completionHandler:completion_handler];
45
46 GREYAssert(
47 testing::WaitUntilConditionOrTimeout(testing::kWaitForJSCompletionTimeout,
48 ^{
49 return did_finish;
50 }),
51 @"JavaScript did not complete");
52
53 return [result autorelease];
54 }
55
56 id<GREYMatcher> webViewWithNavDelegateOfClass(Class cls) {
57 MatchesBlock matches = ^BOOL(UIView* view) {
58 return [view isKindOfClass:[WKWebView class]] &&
59 [base::mac::ObjCCast<WKWebView>(view).navigationDelegate
60 isKindOfClass:cls];
61 };
62
63 DescribeToBlock describe = ^(id<GREYDescription> description) {
64 [description appendText:@"web view with "];
65 [description appendText:NSStringFromClass(cls)];
66 [description appendText:@"navigation delegate"];
67 };
68
69 return [[[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches
70 descriptionBlock:describe]
71 autorelease];
72 }
73
74 id<GREYMatcher> collectionViewSwitchIsOn(BOOL isOn) {
75 MatchesBlock matches = ^BOOL(id element) {
76 CollectionViewSwitchCell* switchCell =
77 base::mac::ObjCCastStrict<CollectionViewSwitchCell>(element);
78 UISwitch* switchView = switchCell.switchView;
79 return (switchView.on && isOn) || (!switchView.on && !isOn);
80 };
81 DescribeToBlock describe = ^void(id<GREYDescription> description) {
82 NSString* name =
83 [NSString stringWithFormat:@"collectionViewSwitchInState(%@)",
84 isOn ? @"ON" : @"OFF"];
85 [description appendText:name];
86 };
87 return [[[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches
88 descriptionBlock:describe]
89 autorelease];
90 }
91
92 } // namespace
93
94 namespace chrome_test_util {
95
96 id<GREYMatcher> buttonWithAccessibilityLabel(NSString* label) {
97 return grey_allOf(grey_accessibilityLabel(label),
98 grey_accessibilityTrait(UIAccessibilityTraitButton), nil);
99 }
100
101 id<GREYMatcher> buttonWithAccessibilityLabelId(int message_id) {
102 return buttonWithAccessibilityLabel(
103 l10n_util::GetNSStringWithFixup(message_id));
104 }
105
106 id<GREYMatcher> staticTextWithAccessibilityLabel(NSString* label) {
107 return grey_allOf(grey_accessibilityLabel(label),
108 grey_accessibilityTrait(UIAccessibilityTraitStaticText),
109 nil);
110 }
111
112 id<GREYMatcher> staticTextWithAccessibilityLabelId(int message_id) {
113 return staticTextWithAccessibilityLabel(
114 l10n_util::GetNSStringWithFixup(message_id));
115 }
116
117 id<GREYMatcher> webViewBelongingToWebController() {
118 return webViewWithNavDelegateOfClass(NSClassFromString(@"CRWWebController"));
119 }
120
121 id<GREYMatcher> webViewContainingText(std::string text) {
122 return web::webViewContainingText(std::move(text), GetCurrentWebState());
123 }
124
125 id<GREYMatcher> webViewNotContainingText(std::string text) {
126 return web::webViewNotContainingText(std::move(text), GetCurrentWebState());
127 }
128
129 id<GREYMatcher> webViewContainingBlockedImage(std::string image_id) {
130 return web::webViewContainingBlockedImage(std::move(image_id),
131 GetCurrentWebState());
132 }
133
134 id<GREYMatcher> staticHtmlViewContainingText(NSString* text) {
135 // The WKWebView in a static HTML view isn't part of a webState, but it
136 // does have the StaticHtmlViewController as its navigation delegate.
137 MatchesBlock matches = ^BOOL(WKWebView* webView) {
138 StaticHtmlViewController* html_view_controller =
139 base::mac::ObjCCast<StaticHtmlViewController>(
140 webView.navigationDelegate);
141
142 __block BOOL did_succeed = NO;
143 NSDate* deadline =
144 [NSDate dateWithTimeIntervalSinceNow:testing::kWaitForUIElementTimeout];
145 while (([[NSDate date] compare:deadline] != NSOrderedDescending) &&
146 !did_succeed) {
147 id result = ExecuteScriptInStaticController(html_view_controller,
148 kGetDocumentBodyJavaScript);
149 if ([result isKindOfClass:[NSString class]]) {
150 NSString* body = base::mac::ObjCCast<NSString>(result);
151 did_succeed = [body containsString:text];
152 }
153 base::test::ios::SpinRunLoopWithMaxDelay(
154 base::TimeDelta::FromSecondsD(testing::kSpinDelaySeconds));
155 }
156 return did_succeed;
157 };
158
159 DescribeToBlock describe = ^(id<GREYDescription> description) {
160 [description appendText:@"static HTML web view containing "];
161 [description appendText:text];
162 };
163
164 return grey_allOf(
165 webViewWithNavDelegateOfClass([StaticHtmlViewController class]),
166 [[[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches
167 descriptionBlock:describe]
168 autorelease],
169 nil);
170 }
171
172 id<GREYMatcher> forwardButton() {
173 return buttonWithAccessibilityLabelId(IDS_ACCNAME_FORWARD);
174 }
175
176 id<GREYMatcher> backButton() {
177 return buttonWithAccessibilityLabelId(IDS_ACCNAME_BACK);
178 }
179
180 id<GREYMatcher> reloadButton() {
181 return buttonWithAccessibilityLabelId(IDS_IOS_ACCNAME_RELOAD);
182 }
183
184 id<GREYMatcher> stopButton() {
185 return buttonWithAccessibilityLabelId(IDS_IOS_ACCNAME_STOP);
186 }
187
188 id<GREYMatcher> omnibox() {
189 return grey_kindOfClass([OmniboxTextFieldIOS class]);
190 }
191
192 id<GREYMatcher> pageSecurityInfoButton() {
193 return grey_accessibilityLabel(@"Page Security Info");
194 }
195
196 id<GREYMatcher> omniboxText(std::string text) {
197 return grey_allOf(omnibox(),
198 hasProperty(@"text", base::SysUTF8ToNSString(text)), nil);
199 }
200
201 id<GREYMatcher> toolsMenuButton() {
202 return grey_allOf(grey_accessibilityID(kToolbarToolsMenuButtonIdentifier),
203 grey_sufficientlyVisible(), nil);
204 }
205
206 id<GREYMatcher> showTabsButton() {
207 return grey_allOf(grey_accessibilityID(kToolbarStackButtonIdentifier),
208 grey_sufficientlyVisible(), nil);
209 }
210
211 id<GREYMatcher> collectionViewSwitchCell(NSString* accessibilityIdentifier,
212 BOOL isOn) {
213 return grey_allOf(grey_accessibilityID(accessibilityIdentifier),
214 collectionViewSwitchIsOn(isOn), grey_sufficientlyVisible(),
215 nil);
216 }
217
218 } // namespace chrome_test_util
OLDNEW
« no previous file with comments | « ios/chrome/test/earl_grey/chrome_matchers.h ('k') | ios/chrome/test/earl_grey/chrome_test_case.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698