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

Side by Side Diff: ios/chrome/browser/ui/webui/web_ui_egtest.mm

Issue 2642193012: Make EarlGrey matchers compliant with Chromium style. (Closed)
Patch Set: mistake Created 3 years, 11 months 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
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 <XCTest/XCTest.h> 5 #import <XCTest/XCTest.h>
6 6
7 #include "base/mac/foundation_util.h" 7 #include "base/mac/foundation_util.h"
8 #include "base/metrics/field_trial.h" 8 #include "base/metrics/field_trial.h"
9 #include "base/strings/sys_string_conversions.h" 9 #include "base/strings/sys_string_conversions.h"
10 #include "components/strings/grit/components_strings.h" 10 #include "components/strings/grit/components_strings.h"
11 #include "components/version_info/version_info.h" 11 #include "components/version_info/version_info.h"
12 #include "ios/chrome/browser/chrome_url_constants.h" 12 #include "ios/chrome/browser/chrome_url_constants.h"
13 #include "ios/chrome/browser/experimental_flags.h" 13 #include "ios/chrome/browser/experimental_flags.h"
14 #import "ios/chrome/browser/ui/omnibox/omnibox_text_field_ios.h" 14 #import "ios/chrome/browser/ui/omnibox/omnibox_text_field_ios.h"
15 #include "ios/chrome/test/app/navigation_test_util.h" 15 #include "ios/chrome/test/app/navigation_test_util.h"
16 #include "ios/chrome/test/app/web_view_interaction_test_util.h" 16 #include "ios/chrome/test/app/web_view_interaction_test_util.h"
17 #import "ios/chrome/test/earl_grey/chrome_earl_grey.h" 17 #import "ios/chrome/test/earl_grey/chrome_earl_grey.h"
18 #import "ios/chrome/test/earl_grey/chrome_matchers.h" 18 #import "ios/chrome/test/earl_grey/chrome_matchers.h"
19 #import "ios/chrome/test/earl_grey/chrome_test_case.h" 19 #import "ios/chrome/test/earl_grey/chrome_test_case.h"
20 #import "ios/testing/wait_util.h" 20 #import "ios/testing/wait_util.h"
21 #import "ios/web/public/web_client.h" 21 #import "ios/web/public/web_client.h"
22 #include "ui/base/device_form_factor.h" 22 #include "ui/base/device_form_factor.h"
23 #include "ui/base/l10n/l10n_util.h" 23 #include "ui/base/l10n/l10n_util.h"
24 #include "url/scheme_host_port.h" 24 #include "url/scheme_host_port.h"
25 25
26 using chrome_test_util::TapWebViewElementWithId; 26 using chrome_test_util::TapWebViewElementWithId;
27 using chrome_test_util::webViewContainingText; 27 using chrome_test_util::WebViewContainingText;
28 using chrome_test_util::staticHtmlViewContainingText; 28 using chrome_test_util::StaticHtmlViewContainingText;
29 29
30 namespace { 30 namespace {
31 31
32 // Loads WebUI page with given |host|. 32 // Loads WebUI page with given |host|.
33 void LoadWebUIUrl(const std::string& host) { 33 void LoadWebUIUrl(const std::string& host) {
34 GURL web_ui_url(url::SchemeHostPort(kChromeUIScheme, host, 0).Serialize()); 34 GURL web_ui_url(url::SchemeHostPort(kChromeUIScheme, host, 0).Serialize());
35 [ChromeEarlGrey loadURL:web_ui_url]; 35 [ChromeEarlGrey loadURL:web_ui_url];
36 } 36 }
37 37
38 // Matcher for the navigate backward button. 38 // Matcher for the navigate backward button.
39 id<GREYMatcher> backButton() { 39 id<GREYMatcher> BackButton() {
40 return chrome_test_util::buttonWithAccessibilityLabelId(IDS_ACCNAME_BACK); 40 return chrome_test_util::ButtonWithAccessibilityLabelId(IDS_ACCNAME_BACK);
Eugene But (OOO till 7-30) 2017/01/23 19:39:04 Could you please use existing matcher from chrome_
baxley 2017/01/24 22:18:49 Done.
41 } 41 }
42 42
43 // Matcher for the navigate forward button. 43 // Matcher for the navigate forward button.
44 id<GREYMatcher> forwardButton() { 44 id<GREYMatcher> ForwardButton() {
45 return chrome_test_util::buttonWithAccessibilityLabelId(IDS_ACCNAME_FORWARD); 45 return chrome_test_util::ButtonWithAccessibilityLabelId(IDS_ACCNAME_FORWARD);
Eugene But (OOO till 7-30) 2017/01/23 19:39:04 ditto
baxley 2017/01/24 22:18:49 Done.
46 } 46 }
47 47
48 // Adds wait for omnibox text matcher so that omnibox text can be updated. 48 // Adds wait for omnibox text matcher so that omnibox text can be updated.
49 // TODO(crbug.com/642207): This method has to be unified with the omniboxText 49 // TODO(crbug.com/642207): This method has to be unified with the omniboxText
50 // matcher or resides in the same location with the omniboxText matcher. 50 // matcher or resides in the same location with the omniboxText matcher.
51 id<GREYMatcher> waitForOmniboxText(std::string text) { 51 id<GREYMatcher> WaitForOmniboxText(std::string text) {
Eugene But (OOO till 7-30) 2017/01/23 19:39:04 This name does not look like a matcher name.
baxley 2017/01/24 22:18:49 Agreed. I filed crbug.com/684803
52 MatchesBlock matches = ^BOOL(UIView* view) { 52 MatchesBlock matches = ^BOOL(UIView* view) {
53 if (![view isKindOfClass:[OmniboxTextFieldIOS class]]) { 53 if (![view isKindOfClass:[OmniboxTextFieldIOS class]]) {
54 return NO; 54 return NO;
55 } 55 }
56 OmniboxTextFieldIOS* omnibox = 56 OmniboxTextFieldIOS* omnibox =
57 base::mac::ObjCCast<OmniboxTextFieldIOS>(view); 57 base::mac::ObjCCast<OmniboxTextFieldIOS>(view);
58 GREYAssert(testing::WaitUntilConditionOrTimeout( 58 GREYAssert(testing::WaitUntilConditionOrTimeout(
59 testing::kWaitForUIElementTimeout, 59 testing::kWaitForUIElementTimeout,
60 ^{ 60 ^{
61 return base::SysNSStringToUTF8(omnibox.text) == text; 61 return base::SysNSStringToUTF8(omnibox.text) == text;
62 }), 62 }),
63 @"Omnibox did not contain %@", base::SysUTF8ToNSString(text)); 63 @"Omnibox did not contain %@", base::SysUTF8ToNSString(text));
64 return YES; 64 return YES;
65 }; 65 };
66 66
67 DescribeToBlock describe = ^(id<GREYDescription> description) { 67 DescribeToBlock describe = ^(id<GREYDescription> description) {
68 [description appendText:@"omnibox text "]; 68 [description appendText:@"omnibox text "];
69 [description appendText:base::SysUTF8ToNSString(text)]; 69 [description appendText:base::SysUTF8ToNSString(text)];
70 }; 70 };
71 71
72 return grey_allOf(chrome_test_util::omnibox(), 72 return grey_allOf(chrome_test_util::Omnibox(),
73 [[[GREYElementMatcherBlock alloc] 73 [[[GREYElementMatcherBlock alloc]
74 initWithMatchesBlock:matches 74 initWithMatchesBlock:matches
75 descriptionBlock:describe] autorelease], 75 descriptionBlock:describe] autorelease],
76 nil); 76 nil);
77 } 77 }
78 78
79 } // namespace 79 } // namespace
80 80
81 // Test case for chrome://* WebUI pages. 81 // Test case for chrome://* WebUI pages.
82 @interface WebUITestCase : ChromeTestCase 82 @interface WebUITestCase : ChromeTestCase
83 @end 83 @end
84 84
85 @implementation WebUITestCase 85 @implementation WebUITestCase
86 86
87 // Tests that chrome://version renders and contains correct version number and 87 // Tests that chrome://version renders and contains correct version number and
88 // user agent string. 88 // user agent string.
89 - (void)testVersion { 89 - (void)testVersion {
90 LoadWebUIUrl(kChromeUIVersionHost); 90 LoadWebUIUrl(kChromeUIVersionHost);
91 91
92 // Verify that app version is present on the page. 92 // Verify that app version is present on the page.
93 const std::string version = version_info::GetVersionNumber(); 93 const std::string version = version_info::GetVersionNumber();
94 [[EarlGrey selectElementWithMatcher:webViewContainingText(version)] 94 [[EarlGrey selectElementWithMatcher:WebViewContainingText(version)]
95 assertWithMatcher:grey_notNil()]; 95 assertWithMatcher:grey_notNil()];
96 96
97 // Verify that mobile User Agent string is present on the page. 97 // Verify that mobile User Agent string is present on the page.
98 const bool isDesktopUA = false; 98 const bool isDesktopUA = false;
99 const std::string userAgent = web::GetWebClient()->GetUserAgent(isDesktopUA); 99 const std::string userAgent = web::GetWebClient()->GetUserAgent(isDesktopUA);
100 [[EarlGrey selectElementWithMatcher:webViewContainingText(userAgent)] 100 [[EarlGrey selectElementWithMatcher:WebViewContainingText(userAgent)]
101 assertWithMatcher:grey_notNil()]; 101 assertWithMatcher:grey_notNil()];
102 } 102 }
103 103
104 // Tests that chrome://physical-web renders and the page title is present. 104 // Tests that chrome://physical-web renders and the page title is present.
105 - (void)testPhysicalWeb { 105 - (void)testPhysicalWeb {
106 // Enable the Physical Web via Chrome variation. 106 // Enable the Physical Web via Chrome variation.
107 base::FieldTrialList::CreateFieldTrial("PhysicalWebEnabled", "Enabled"); 107 base::FieldTrialList::CreateFieldTrial("PhysicalWebEnabled", "Enabled");
108 108
109 LoadWebUIUrl(kChromeUIPhysicalWebHost); 109 LoadWebUIUrl(kChromeUIPhysicalWebHost);
110 110
111 // Verify that the title string is present on the page. 111 // Verify that the title string is present on the page.
112 std::string pageTitle = l10n_util::GetStringUTF8(IDS_PHYSICAL_WEB_UI_TITLE); 112 std::string pageTitle = l10n_util::GetStringUTF8(IDS_PHYSICAL_WEB_UI_TITLE);
113 [[EarlGrey selectElementWithMatcher:webViewContainingText(pageTitle)] 113 [[EarlGrey selectElementWithMatcher:WebViewContainingText(pageTitle)]
114 assertWithMatcher:grey_notNil()]; 114 assertWithMatcher:grey_notNil()];
115 } 115 }
116 116
117 // Tests that clicking on a link for a native page from chrome://chrome-urls 117 // Tests that clicking on a link for a native page from chrome://chrome-urls
118 // navigates to that page. 118 // navigates to that page.
119 - (void)testChromeURLNavigateToNativePage { 119 - (void)testChromeURLNavigateToNativePage {
120 LoadWebUIUrl(kChromeUIChromeURLsHost); 120 LoadWebUIUrl(kChromeUIChromeURLsHost);
121 121
122 // Tap on chrome://terms link on the page. 122 // Tap on chrome://terms link on the page.
123 chrome_test_util::TapWebViewElementWithId(kChromeUITermsHost); 123 chrome_test_util::TapWebViewElementWithId(kChromeUITermsHost);
124 124
125 // Verify that the resulting page is chrome://terms. 125 // Verify that the resulting page is chrome://terms.
126 [[EarlGrey selectElementWithMatcher:waitForOmniboxText("chrome://terms")] 126 [[EarlGrey selectElementWithMatcher:WaitForOmniboxText("chrome://terms")]
127 assertWithMatcher:grey_sufficientlyVisible()]; 127 assertWithMatcher:grey_sufficientlyVisible()];
128 NSString* kTermsText = @"Google Chrome Terms of Service"; 128 NSString* kTermsText = @"Google Chrome Terms of Service";
129 [[EarlGrey selectElementWithMatcher:staticHtmlViewContainingText(kTermsText)] 129 [[EarlGrey selectElementWithMatcher:StaticHtmlViewContainingText(kTermsText)]
130 assertWithMatcher:grey_notNil()]; 130 assertWithMatcher:grey_notNil()];
131 } 131 }
132 132
133 // Tests that back navigation functions properly after navigation via anchor 133 // Tests that back navigation functions properly after navigation via anchor
134 // click. 134 // click.
135 - (void)testChromeURLBackNavigationFromAnchorClick { 135 - (void)testChromeURLBackNavigationFromAnchorClick {
136 LoadWebUIUrl(kChromeUIChromeURLsHost); 136 LoadWebUIUrl(kChromeUIChromeURLsHost);
137 137
138 // Tap on chrome://version link on the page. 138 // Tap on chrome://version link on the page.
139 chrome_test_util::TapWebViewElementWithId(kChromeUIVersionHost); 139 chrome_test_util::TapWebViewElementWithId(kChromeUIVersionHost);
140 140
141 // Verify that the resulting page is chrome://version. 141 // Verify that the resulting page is chrome://version.
142 [[EarlGrey selectElementWithMatcher:waitForOmniboxText("chrome://version")] 142 [[EarlGrey selectElementWithMatcher:WaitForOmniboxText("chrome://version")]
143 assertWithMatcher:grey_sufficientlyVisible()]; 143 assertWithMatcher:grey_sufficientlyVisible()];
144 const std::string kAuthorsText = "The Chromium Authors"; 144 const std::string kAuthorsText = "The Chromium Authors";
145 [[EarlGrey selectElementWithMatcher:webViewContainingText(kAuthorsText)] 145 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kAuthorsText)]
146 assertWithMatcher:grey_notNil()]; 146 assertWithMatcher:grey_notNil()];
147 147
148 // Tap the back button in the toolbar and verify that the resulting page is 148 // Tap the back button in the toolbar and verify that the resulting page is
149 // the previously visited page chrome://chrome-urls. 149 // the previously visited page chrome://chrome-urls.
150 [[EarlGrey selectElementWithMatcher:backButton()] performAction:grey_tap()]; 150 [[EarlGrey selectElementWithMatcher:BackButton()] performAction:grey_tap()];
151 [[EarlGrey 151 [[EarlGrey
152 selectElementWithMatcher:waitForOmniboxText("chrome://chrome-urls")] 152 selectElementWithMatcher:WaitForOmniboxText("chrome://chrome-urls")]
153 assertWithMatcher:grey_sufficientlyVisible()]; 153 assertWithMatcher:grey_sufficientlyVisible()];
154 const std::string kChromeURLsText = "List of Chrome URLs"; 154 const std::string kChromeURLsText = "List of Chrome URLs";
155 [[EarlGrey selectElementWithMatcher:webViewContainingText(kChromeURLsText)] 155 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kChromeURLsText)]
156 assertWithMatcher:grey_notNil()]; 156 assertWithMatcher:grey_notNil()];
157 } 157 }
158 158
159 // Tests that back and forward navigation between chrome URLs functions 159 // Tests that back and forward navigation between chrome URLs functions
160 // properly. 160 // properly.
161 - (void)testChromeURLBackAndForwardNavigation { 161 - (void)testChromeURLBackAndForwardNavigation {
162 // Navigate to the first URL chrome://version. 162 // Navigate to the first URL chrome://version.
163 LoadWebUIUrl(kChromeUIVersionHost); 163 LoadWebUIUrl(kChromeUIVersionHost);
164 164
165 // Navigate to the second URL chrome://flags. 165 // Navigate to the second URL chrome://flags.
166 LoadWebUIUrl(kChromeUIFlagsHost); 166 LoadWebUIUrl(kChromeUIFlagsHost);
167 167
168 // Tap the back button in the toolbar and verify that the resulting page's URL 168 // Tap the back button in the toolbar and verify that the resulting page's URL
169 // corresponds to the first URL chrome://version that was loaded. 169 // corresponds to the first URL chrome://version that was loaded.
170 [[EarlGrey selectElementWithMatcher:backButton()] performAction:grey_tap()]; 170 [[EarlGrey selectElementWithMatcher:BackButton()] performAction:grey_tap()];
171 [[EarlGrey selectElementWithMatcher:waitForOmniboxText("chrome://version")] 171 [[EarlGrey selectElementWithMatcher:WaitForOmniboxText("chrome://version")]
172 assertWithMatcher:grey_sufficientlyVisible()]; 172 assertWithMatcher:grey_sufficientlyVisible()];
173 173
174 // Tap the forward button in the toolbar and verify that the resulting page's 174 // Tap the forward button in the toolbar and verify that the resulting page's
175 // URL corresponds the second URL chrome://flags that was loaded. 175 // URL corresponds the second URL chrome://flags that was loaded.
176 [[EarlGrey selectElementWithMatcher:forwardButton()] 176 [[EarlGrey selectElementWithMatcher:ForwardButton()]
177 performAction:grey_tap()]; 177 performAction:grey_tap()];
178 [[EarlGrey selectElementWithMatcher:waitForOmniboxText("chrome://flags")] 178 [[EarlGrey selectElementWithMatcher:WaitForOmniboxText("chrome://flags")]
179 assertWithMatcher:grey_sufficientlyVisible()]; 179 assertWithMatcher:grey_sufficientlyVisible()];
180 } 180 }
181 181
182 // Tests that all URLs on chrome://chrome-urls page load without error. 182 // Tests that all URLs on chrome://chrome-urls page load without error.
183 - (void)testChromeURLsLoadWithoutError { 183 - (void)testChromeURLsLoadWithoutError {
184 // Load WebUI pages and verify they load without any error. 184 // Load WebUI pages and verify they load without any error.
185 for (size_t i = 0; i < kNumberOfChromeHostURLs; ++i) { 185 for (size_t i = 0; i < kNumberOfChromeHostURLs; ++i) {
186 const char* host = kChromeHostURLs[i]; 186 const char* host = kChromeHostURLs[i];
187 // Exclude non-WebUI pages, as they do not go through a "loading" phase as 187 // Exclude non-WebUI pages, as they do not go through a "loading" phase as
188 // expected in LoadWebUIUrl. 188 // expected in LoadWebUIUrl.
189 if (host == kChromeUIBookmarksHost || host == kChromeUINewTabHost || 189 if (host == kChromeUIBookmarksHost || host == kChromeUINewTabHost ||
190 host == kChromeUITermsHost) { 190 host == kChromeUITermsHost) {
191 continue; 191 continue;
192 } 192 }
193 if (host == kChromeUIPhysicalWebHost && 193 if (host == kChromeUIPhysicalWebHost &&
194 !experimental_flags::IsPhysicalWebEnabled()) { 194 !experimental_flags::IsPhysicalWebEnabled()) {
195 continue; 195 continue;
196 } 196 }
197 LoadWebUIUrl(host); 197 LoadWebUIUrl(host);
198 const std::string chrome_url_path = 198 const std::string chrome_url_path =
199 url::SchemeHostPort(kChromeUIScheme, kChromeHostURLs[i], 0).Serialize(); 199 url::SchemeHostPort(kChromeUIScheme, kChromeHostURLs[i], 0).Serialize();
200 [[EarlGrey selectElementWithMatcher:waitForOmniboxText(chrome_url_path)] 200 [[EarlGrey selectElementWithMatcher:WaitForOmniboxText(chrome_url_path)]
201 assertWithMatcher:grey_sufficientlyVisible()]; 201 assertWithMatcher:grey_sufficientlyVisible()];
202 } 202 }
203 // Load chrome://terms differently since it is a Native page and is never in 203 // Load chrome://terms differently since it is a Native page and is never in
204 // the "loading" phase. 204 // the "loading" phase.
205 chrome_test_util::LoadUrl(GURL(kChromeUITermsURL)); 205 chrome_test_util::LoadUrl(GURL(kChromeUITermsURL));
206 [[EarlGrey selectElementWithMatcher:waitForOmniboxText("chrome://terms")] 206 [[EarlGrey selectElementWithMatcher:WaitForOmniboxText("chrome://terms")]
207 assertWithMatcher:grey_sufficientlyVisible()]; 207 assertWithMatcher:grey_sufficientlyVisible()];
208 } 208 }
209 209
210 // Tests that loading an invalid Chrome URL results in an error page. 210 // Tests that loading an invalid Chrome URL results in an error page.
211 - (void)testChromeURLInvalid { 211 - (void)testChromeURLInvalid {
212 // Navigate to the native error page chrome://invalidchromeurl. 212 // Navigate to the native error page chrome://invalidchromeurl.
213 const std::string kChromeInvalidURL = "chrome://invalidchromeurl"; 213 const std::string kChromeInvalidURL = "chrome://invalidchromeurl";
214 chrome_test_util::LoadUrl(GURL(kChromeInvalidURL)); 214 chrome_test_util::LoadUrl(GURL(kChromeInvalidURL));
215 215
216 // Verify that the resulting page is an error page. 216 // Verify that the resulting page is an error page.
217 [[EarlGrey selectElementWithMatcher:waitForOmniboxText(kChromeInvalidURL)] 217 [[EarlGrey selectElementWithMatcher:WaitForOmniboxText(kChromeInvalidURL)]
218 assertWithMatcher:grey_sufficientlyVisible()]; 218 assertWithMatcher:grey_sufficientlyVisible()];
219 NSString* kError = 219 NSString* kError =
220 l10n_util::GetNSString(IDS_ERRORPAGES_HEADING_NOT_AVAILABLE); 220 l10n_util::GetNSString(IDS_ERRORPAGES_HEADING_NOT_AVAILABLE);
221 id<GREYMatcher> messageMatcher = [GREYMatchers matcherForText:kError]; 221 id<GREYMatcher> messageMatcher = [GREYMatchers matcherForText:kError];
222 [[EarlGrey selectElementWithMatcher:messageMatcher] 222 [[EarlGrey selectElementWithMatcher:messageMatcher]
223 assertWithMatcher:grey_notNil()]; 223 assertWithMatcher:grey_notNil()];
224 } 224 }
225 225
226 @end 226 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698