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

Side by Side Diff: ios/chrome/browser/ui/history/history_ui_egtest.mm

Issue 2590473002: Upstream Chrome on iOS source code [5/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 <EarlGrey/EarlGrey.h>
6 #import <UIKit/UIKit.h>
7 #import <XCTest/XCTest.h>
8
9 #include "base/strings/sys_string_conversions.h"
10 #include "components/browsing_data/core/pref_names.h"
11 #include "components/prefs/pref_service.h"
12 #include "components/strings/grit/components_strings.h"
13 #include "ios/chrome/browser/browser_state/chrome_browser_state.h"
14 #import "ios/chrome/browser/ui/history/history_entry_item.h"
15 #import "ios/chrome/browser/ui/settings/clear_browsing_data_collection_view_cont roller.h"
16 #include "ios/chrome/grit/ios_strings.h"
17 #import "ios/chrome/test/app/chrome_test_util.h"
18 #import "ios/chrome/test/earl_grey/accessibility_util.h"
19 #import "ios/chrome/test/earl_grey/chrome_assertions.h"
20 #import "ios/chrome/test/earl_grey/chrome_earl_grey.h"
21 #import "ios/chrome/test/earl_grey/chrome_earl_grey_ui.h"
22 #import "ios/chrome/test/earl_grey/chrome_matchers.h"
23 #import "ios/chrome/test/earl_grey/chrome_test_case.h"
24 #import "ios/testing/wait_util.h"
25 #import "ios/web/public/test/http_server.h"
26 #import "ios/web/public/test/http_server_util.h"
27 #import "net/base/mac/url_conversions.h"
28 #include "ui/base/l10n/l10n_util.h"
29
30 using chrome_test_util::buttonWithAccessibilityLabelId;
31
32 namespace {
33 char kURL1[] = "http://firstURL";
34 char kURL2[] = "http://secondURL";
35 char kURL3[] = "http://thirdURL";
36 char kResponse1[] = "Test Page 1";
37 char kResponse2[] = "Test Page 2";
38 char kResponse3[] = "Test Page 3";
39
40 // Matcher for entry in history for URL.
41 id<GREYMatcher> historyEntryWithURL(const GURL& url) {
42 NSString* url_spec = base::SysUTF8ToNSString(url.spec());
43 return grey_allOf(grey_text(url_spec), grey_sufficientlyVisible(), nil);
44 }
45 // Matcher for the history button in the tools menu.
46 id<GREYMatcher> historyButton() {
47 return buttonWithAccessibilityLabelId(IDS_HISTORY_SHOW_HISTORY);
48 }
49 // Matcher for the done button in the navigation bar.
50 id<GREYMatcher> navigationDoneButton() {
51 // Include sufficientlyVisible condition for the case of the clear browsing
52 // dialog, which also has a "Done" button and is displayed over the history
53 // panel.
54 return grey_allOf(
55 buttonWithAccessibilityLabelId(IDS_IOS_NAVIGATION_BAR_DONE_BUTTON),
56 grey_sufficientlyVisible(), nil);
57 }
58 // Matcher for the edit button in the navigation bar.
59 id<GREYMatcher> navigationEditButton() {
60 return buttonWithAccessibilityLabelId(IDS_HISTORY_START_EDITING_BUTTON);
61 }
62 // Matcher for the delete button.
63 id<GREYMatcher> deleteHistoryEntriesButton() {
64 // Include class restriction to exclude MDCCollectionViewInfoBar, which is
65 // hidden.
66 return grey_allOf(buttonWithAccessibilityLabelId(
67 IDS_HISTORY_DELETE_SELECTED_ENTRIES_BUTTON),
68 grey_kindOfClass([UIButton class]), nil);
69 }
70 // Matcher for the search button.
71 id<GREYMatcher> searchIconButton() {
72 return buttonWithAccessibilityLabelId(IDS_IOS_ICON_SEARCH);
73 }
74 // Matcher for the cancel button.
75 id<GREYMatcher> cancelButton() {
76 return buttonWithAccessibilityLabelId(IDS_HISTORY_CANCEL_EDITING_BUTTON);
77 }
78 // Matcher for the button to open the clear browsing data panel.
79 id<GREYMatcher> openClearBrowsingDataButton() {
80 return buttonWithAccessibilityLabelId(
81 IDS_HISTORY_OPEN_CLEAR_BROWSING_DATA_DIALOG);
82 }
83 // Matcher for the Open in New Tab option in the context menu.
84 id<GREYMatcher> openInNewTabButton() {
85 return buttonWithAccessibilityLabelId(IDS_IOS_CONTENT_CONTEXT_OPENLINKNEWTAB);
86 }
87 // Matcher for the Open in New Incognito Tab option in the context menu.
88 id<GREYMatcher> openInNewIncognitoTabButton() {
89 return buttonWithAccessibilityLabelId(
90 IDS_IOS_CONTENT_CONTEXT_OPENLINKNEWINCOGNITOTAB);
91 }
92 // Matcher for the Copy URL option in the context menu.
93 id<GREYMatcher> copyUrlButton() {
94 return buttonWithAccessibilityLabelId(IDS_IOS_CONTENT_CONTEXT_COPY);
95 }
96 // Matcher for the clear cookies cell on the clear browsing data panel.
97 id<GREYMatcher> clearCookiesButton() {
98 return grey_accessibilityID(kClearCookiesCellId);
99 }
100 // Matcher for the clear cache cell on the clear browsing data panel.
101 id<GREYMatcher> clearCacheButton() {
102 return grey_allOf(grey_accessibilityID(kClearCacheCellId),
103 grey_sufficientlyVisible(), nil);
104 }
105 // Matcher for the clear browsing data button on the clear browsing data panel.
106 id<GREYMatcher> clearBrowsingDataButton() {
107 return buttonWithAccessibilityLabelId(IDS_IOS_CLEAR_BUTTON);
108 }
109
110 } // namespace
111
112 // History UI tests.
113 @interface HistoryUITestCase : ChromeTestCase {
114 GURL _URL1;
115 GURL _URL2;
116 GURL _URL3;
117 }
118
119 // Loads three test URLs.
120 - (void)loadTestURLs;
121 // Displays the history UI.
122 - (void)openHistoryPanel;
123 // Asserts that the history UI displays no history entries.
124 - (void)assertNoHistoryShown;
125 // Resets which data is selected in the Clear Browsing Data UI.
126 - (void)resetBrowsingDataPrefs;
127
128 @end
129
130 @implementation HistoryUITestCase
131
132 // Set up called once for the class.
133 + (void)setUp {
134 [super setUp];
135 std::map<GURL, std::string> responses;
136 responses[web::test::HttpServer::MakeUrl(kURL1)] = kResponse1;
137 responses[web::test::HttpServer::MakeUrl(kURL2)] = kResponse2;
138 responses[web::test::HttpServer::MakeUrl(kURL3)] = kResponse3;
139 web::test::SetUpSimpleHttpServer(responses);
140 }
141
142 - (void)setUp {
143 [super setUp];
144 _URL1 = web::test::HttpServer::MakeUrl(kURL1);
145 _URL2 = web::test::HttpServer::MakeUrl(kURL2);
146 _URL3 = web::test::HttpServer::MakeUrl(kURL3);
147 [ChromeEarlGrey clearBrowsingHistory];
148 // Some tests rely on a clean state for the "Clear Browsing Data" settings
149 // screen.
150 [self resetBrowsingDataPrefs];
151 }
152
153 - (void)tearDown {
154 NSError* error = nil;
155 // Dismiss search bar by pressing cancel, if present. Passing error prevents
156 // failure if the element is not found.
157 [[EarlGrey selectElementWithMatcher:cancelButton()] performAction:grey_tap()
158 error:&error];
159 // Dismiss history panel by pressing done, if present. Passing error prevents
160 // failure if the element is not found.
161 [[EarlGrey selectElementWithMatcher:navigationDoneButton()]
162 performAction:grey_tap()
163 error:&error];
164
165 // Some tests change the default values for the "Clear Browsing Data" settings
166 // screen.
167 [self resetBrowsingDataPrefs];
168 [super tearDown];
169 }
170
171 #pragma mark Tests
172
173 // Tests that no history is shown if there has been no navigation.
174 - (void)testDisplayNoHistory {
175 [self openHistoryPanel];
176 [self assertNoHistoryShown];
177 }
178
179 // Tests that the history panel displays navigation history.
180 - (void)testDisplayHistory {
181 [self loadTestURLs];
182 [self openHistoryPanel];
183
184 // Assert that history displays three entries.
185 [[EarlGrey selectElementWithMatcher:historyEntryWithURL(_URL1)]
186 assertWithMatcher:grey_notNil()];
187 [[EarlGrey selectElementWithMatcher:historyEntryWithURL(_URL2)]
188 assertWithMatcher:grey_notNil()];
189 [[EarlGrey selectElementWithMatcher:historyEntryWithURL(_URL3)]
190 assertWithMatcher:grey_notNil()];
191
192 // Tap a history entry and assert that navigation to that entry's URL occurs.
193 [[EarlGrey selectElementWithMatcher:historyEntryWithURL(_URL1)]
194 performAction:grey_tap()];
195 id<GREYMatcher> webViewMatcher =
196 chrome_test_util::webViewContainingText(kResponse1);
197 [[EarlGrey selectElementWithMatcher:webViewMatcher]
198 assertWithMatcher:grey_notNil()];
199 }
200
201 // Tests that searching history displays only entries matching the search term.
202 - (void)testSearchHistory {
203 [self loadTestURLs];
204 [self openHistoryPanel];
205 [[EarlGrey selectElementWithMatcher:searchIconButton()]
206 performAction:grey_tap()];
207
208 NSString* searchString =
209 [NSString stringWithFormat:@"%s", _URL1.path().c_str()];
210 [[EarlGrey selectElementWithMatcher:grey_keyWindow()]
211 performAction:grey_typeText(searchString)];
212 [[EarlGrey selectElementWithMatcher:historyEntryWithURL(_URL1)]
213 assertWithMatcher:grey_notNil()];
214 [[EarlGrey selectElementWithMatcher:historyEntryWithURL(_URL2)]
215 assertWithMatcher:grey_nil()];
216 [[EarlGrey selectElementWithMatcher:historyEntryWithURL(_URL3)]
217 assertWithMatcher:grey_nil()];
218 }
219
220 // Tests deletion of history entries.
221 - (void)testDeleteHistory {
222 [self loadTestURLs];
223 [self openHistoryPanel];
224
225 // Assert that three history elements are present.
226 [[EarlGrey selectElementWithMatcher:historyEntryWithURL(_URL1)]
227 assertWithMatcher:grey_notNil()];
228 [[EarlGrey selectElementWithMatcher:historyEntryWithURL(_URL2)]
229 assertWithMatcher:grey_notNil()];
230 [[EarlGrey selectElementWithMatcher:historyEntryWithURL(_URL3)]
231 assertWithMatcher:grey_notNil()];
232
233 // Enter edit mode, select a history element, and press delete.
234 [[EarlGrey selectElementWithMatcher:navigationEditButton()]
235 performAction:grey_tap()];
236 [[EarlGrey selectElementWithMatcher:historyEntryWithURL(_URL1)]
237 performAction:grey_tap()];
238 [[EarlGrey selectElementWithMatcher:deleteHistoryEntriesButton()]
239 performAction:grey_tap()];
240
241 // Assert that the deleted entry is gone and the other two remain.
242 [[EarlGrey selectElementWithMatcher:historyEntryWithURL(_URL1)]
243 assertWithMatcher:grey_nil()];
244 [[EarlGrey selectElementWithMatcher:historyEntryWithURL(_URL2)]
245 assertWithMatcher:grey_notNil()];
246 [[EarlGrey selectElementWithMatcher:historyEntryWithURL(_URL3)]
247 assertWithMatcher:grey_notNil()];
248
249 // Enter edit mode, select both remaining entries, and press delete.
250 [[EarlGrey selectElementWithMatcher:navigationEditButton()]
251 performAction:grey_tap()];
252 [[EarlGrey selectElementWithMatcher:historyEntryWithURL(_URL2)]
253 performAction:grey_tap()];
254 [[EarlGrey selectElementWithMatcher:historyEntryWithURL(_URL3)]
255 performAction:grey_tap()];
256 [[EarlGrey selectElementWithMatcher:deleteHistoryEntriesButton()]
257 performAction:grey_tap()];
258
259 [self assertNoHistoryShown];
260 }
261
262 // Tests clear browsing history.
263 - (void)testClearBrowsingHistory {
264 [self loadTestURLs];
265 [self openHistoryPanel];
266
267 // Open the Clear Browsing Data dialog.
268 [[EarlGrey selectElementWithMatcher:openClearBrowsingDataButton()]
269 performAction:grey_tap()];
270
271 // Uncheck "Cookies, Site Data" and "Cached Images and Files," which are
272 // checked by default, and press "Clear Browsing Data"
273 [[EarlGrey selectElementWithMatcher:clearCookiesButton()]
274 performAction:grey_tap()];
275 [[EarlGrey selectElementWithMatcher:clearCacheButton()]
276 performAction:grey_tap()];
277 [[EarlGrey selectElementWithMatcher:clearBrowsingDataButton()]
278 performAction:grey_tap()];
279
280 // There is not currently a matcher for acessibilityElementIsFocused or
281 // userInteractionEnabled which could be used here instead of checking that
282 // the button is not a MDCCollectionViewTextCell. Use when available.
283 // TODO(crbug.com/638674): Evaluate if this can move to shared code.
284 id<GREYMatcher> confirmClear = grey_allOf(
285 clearBrowsingDataButton(),
286 grey_not(grey_kindOfClass([MDCCollectionViewTextCell class])), nil);
287 [[EarlGrey selectElementWithMatcher:confirmClear] performAction:grey_tap()];
288 [[EarlGrey selectElementWithMatcher:navigationDoneButton()]
289 performAction:grey_tap()];
290
291 [self assertNoHistoryShown];
292 }
293
294 // Tests display and selection of 'Open in New Tab' in a context menu on a
295 // history entry.
296 - (void)testContextMenuOpenInNewTab {
297 [self loadTestURLs];
298 [self openHistoryPanel];
299
300 // Long press on the history element.
301 [[EarlGrey selectElementWithMatcher:historyEntryWithURL(_URL1)]
302 performAction:grey_longPress()];
303
304 // Select "Open in New Tab" and confirm that new tab is opened with selected
305 // URL.
306 [[EarlGrey selectElementWithMatcher:openInNewTabButton()]
307 performAction:grey_tap()];
308 [[EarlGrey selectElementWithMatcher:chrome_test_util::omniboxText(
309 _URL1.GetContent())]
310 assertWithMatcher:grey_notNil()];
311 chrome_test_util::AssertMainTabCount(2);
312 }
313
314 // Tests display and selection of 'Open in New Incognito Tab' in a context menu
315 // on a history entry.
316 - (void)testContextMenuOpenInNewIncognitoTab {
317 [self loadTestURLs];
318 [self openHistoryPanel];
319
320 // Long press on the history element.
321 [[EarlGrey selectElementWithMatcher:historyEntryWithURL(_URL1)]
322 performAction:grey_longPress()];
323
324 // Select "Open in New Incognito Tab" and confirm that new tab is opened in
325 // incognito with the selected URL.
326 [[EarlGrey selectElementWithMatcher:openInNewIncognitoTabButton()]
327 performAction:grey_tap()];
328 [[EarlGrey selectElementWithMatcher:chrome_test_util::omniboxText(
329 _URL1.GetContent())]
330 assertWithMatcher:grey_notNil()];
331 chrome_test_util::AssertMainTabCount(1);
332 chrome_test_util::AssertIncognitoTabCount(1);
333 }
334
335 // Tests display and selection of 'Copy URL' in a context menu on a history
336 // entry.
337 - (void)testContextMenuCopy {
338 ProceduralBlock clearPasteboard = ^{
339 [[UIPasteboard generalPasteboard] setURLs:nil];
340 };
341 [self setTearDownHandler:clearPasteboard];
342 clearPasteboard();
343
344 [self loadTestURLs];
345 [self openHistoryPanel];
346
347 // Long press on the history element.
348 [[EarlGrey selectElementWithMatcher:historyEntryWithURL(_URL1)]
349 performAction:grey_longPress()];
350
351 // Tap "Copy URL" and wait for the URL to be copied to the pasteboard.
352 [[EarlGrey selectElementWithMatcher:copyUrlButton()]
353 performAction:grey_tap()];
354 bool success =
355 testing::WaitUntilConditionOrTimeout(testing::kWaitForUIElementTimeout, ^{
356 return _URL1 ==
357 net::GURLWithNSURL([UIPasteboard generalPasteboard].URL);
358 });
359 GREYAssertTrue(success, @"Pasteboard URL was not set to %s",
360 _URL1.spec().c_str());
361 }
362
363 // Navigates to history and checks elements for accessibility.
364 - (void)testAccessibilityOnHistory {
365 [self openHistoryPanel];
366 chrome_test_util::VerifyAccessibilityForCurrentScreen();
367 // Close history.
368 [[EarlGrey
369 selectElementWithMatcher:chrome_test_util::buttonWithAccessibilityLabelId(
370 IDS_IOS_NAVIGATION_BAR_DONE_BUTTON)]
371 performAction:grey_tap()];
372 }
373
374 #pragma mark Helper Methods
375
376 - (void)loadTestURLs {
377 [ChromeEarlGrey loadURL:_URL1];
378 id<GREYMatcher> response1Matcher =
379 chrome_test_util::webViewContainingText(kResponse1);
380 [[EarlGrey selectElementWithMatcher:response1Matcher]
381 assertWithMatcher:grey_notNil()];
382
383 [ChromeEarlGrey loadURL:_URL2];
384 id<GREYMatcher> response2Matcher =
385 chrome_test_util::webViewContainingText(kResponse2);
386 [[EarlGrey selectElementWithMatcher:response2Matcher]
387 assertWithMatcher:grey_notNil()];
388
389 [ChromeEarlGrey loadURL:_URL3];
390 id<GREYMatcher> response3Matcher =
391 chrome_test_util::webViewContainingText(kResponse3);
392 [[EarlGrey selectElementWithMatcher:response3Matcher]
393 assertWithMatcher:grey_notNil()];
394 }
395
396 - (void)openHistoryPanel {
397 [ChromeEarlGreyUI openToolsMenu];
398 [[EarlGrey selectElementWithMatcher:historyButton()]
399 performAction:grey_tap()];
400 }
401
402 - (void)assertNoHistoryShown {
403 id<GREYMatcher> noHistoryMessageMatcher =
404 grey_allOf(grey_text(l10n_util::GetNSString(IDS_HISTORY_NO_RESULTS)),
405 grey_sufficientlyVisible(), nil);
406 [[EarlGrey selectElementWithMatcher:noHistoryMessageMatcher]
407 assertWithMatcher:grey_notNil()];
408
409 id<GREYMatcher> historyEntryMatcher =
410 grey_allOf(grey_kindOfClass([HistoryEntryCell class]),
411 grey_sufficientlyVisible(), nil);
412 [[EarlGrey selectElementWithMatcher:historyEntryMatcher]
413 assertWithMatcher:grey_nil()];
414 }
415
416 - (void)resetBrowsingDataPrefs {
417 PrefService* prefs = chrome_test_util::GetOriginalBrowserState()->GetPrefs();
418 prefs->ClearPref(browsing_data::prefs::kDeleteBrowsingHistory);
419 prefs->ClearPref(browsing_data::prefs::kDeleteCookies);
420 prefs->ClearPref(browsing_data::prefs::kDeleteCache);
421 prefs->ClearPref(browsing_data::prefs::kDeletePasswords);
422 prefs->ClearPref(browsing_data::prefs::kDeleteFormData);
423 }
424
425 @end
OLDNEW
« no previous file with comments | « ios/chrome/browser/ui/history/history_service_facade_unittest.mm ('k') | ios/chrome/browser/ui/history/history_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698