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

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

Powered by Google App Engine
This is Rietveld 408576698