OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 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 <XCTest/XCTest.h> | |
7 | |
8 #include "base/ios/ios_util.h" | |
9 #include "components/strings/grit/components_strings.h" | |
10 #include "ios/chrome/browser/ui/tools_menu/tools_menu_constants.h" | |
11 #import "ios/chrome/test/app/chrome_test_util.h" | |
12 #import "ios/chrome/test/app/navigation_test_util.h" | |
13 #import "ios/chrome/test/earl_grey/chrome_assertions.h" | |
14 #import "ios/chrome/test/earl_grey/chrome_earl_grey.h" | |
15 #import "ios/chrome/test/earl_grey/chrome_earl_grey_ui.h" | |
16 #import "ios/chrome/test/earl_grey/chrome_matchers.h" | |
17 #import "ios/chrome/test/earl_grey/chrome_test_case.h" | |
18 #import "ios/web/public/test/http_server/http_server.h" | |
19 #include "ios/web/public/test/http_server/http_server_util.h" | |
20 #include "ui/base/l10n/l10n_util_mac.h" | |
21 | |
22 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
23 #error "This file requires ARC support." | |
24 #endif | |
25 | |
26 namespace { | |
27 // Returns matcher that looks for text in UILabel, UITextView, and UITextField | |
28 // objects, checking if their displayed strings contain the provided |text|. | |
29 id<GREYMatcher> ContainsText(NSString* text) { | |
30 MatchesBlock matches = ^BOOL(id element) { | |
31 return [[element text] containsString:text]; | |
32 }; | |
33 DescribeToBlock describe = ^void(id<GREYDescription> description) { | |
34 [description appendText:[NSString stringWithFormat:@"hasText('%@')", text]]; | |
35 }; | |
36 id<GREYMatcher> matcher = | |
37 [[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches | |
38 descriptionBlock:describe]; | |
39 return grey_allOf(grey_anyOf(grey_kindOfClass([UILabel class]), | |
40 grey_kindOfClass([UITextField class]), | |
41 grey_kindOfClass([UITextView class]), nil), | |
42 matcher, nil); | |
43 } | |
44 | |
45 // A matcher for the main title of the Sad Tab in 'reload' mode. | |
46 id<GREYMatcher> reloadSadTabTitleText() { | |
47 static id matcher = nil; | |
48 static dispatch_once_t onceToken; | |
49 dispatch_once(&onceToken, ^{ | |
50 matcher = [GREYMatchers | |
51 matcherForText:l10n_util::GetNSString(IDS_SAD_TAB_MESSAGE)]; | |
52 }); | |
53 return matcher; | |
baxley
2017/06/27 06:02:11
Question more than comment, why is dispatch_once n
PL
2017/06/27 23:21:46
Good question!
If you look at the GREYMatchers im
| |
54 } | |
55 | |
56 // A matcher for the main title of the Sad Tab in 'feedback' mode. | |
57 id<GREYMatcher> feedbackSadTabTitleContainsText() { | |
58 static id matcher = nil; | |
59 static dispatch_once_t onceToken; | |
60 dispatch_once(&onceToken, ^{ | |
61 matcher = ContainsText(l10n_util::GetNSString(IDS_SAD_TAB_RELOAD_TRY)); | |
62 }); | |
63 return matcher; | |
64 } | |
65 | |
66 // A matcher for a help string suggesting the user use Incognito Mode. | |
67 id<GREYMatcher> incognitoHelpContainsText() { | |
68 static id matcher = nil; | |
69 static dispatch_once_t onceToken; | |
70 dispatch_once(&onceToken, ^{ | |
71 matcher = | |
72 ContainsText(l10n_util::GetNSString(IDS_SAD_TAB_RELOAD_INCOGNITO)); | |
73 }); | |
74 return matcher; | |
75 } | |
76 } | |
77 | |
78 // Sad Tab View integration tests for Chrome. | |
79 @interface SadTabViewTestCase : ChromeTestCase | |
80 @end | |
81 | |
82 @implementation SadTabViewTestCase | |
83 | |
84 // Verifies initial and repeated visits to the Sad Tab. | |
85 // N.B. There is a mechanism which changes the Sad Tab UI if a crash URL is | |
86 // visited within 60 seconds, for this reason this one test can not | |
87 // be easily split up across multiple tests | |
88 // as visiting Sad Tab may not be idempotent. | |
89 - (void)testSadTabView { | |
90 // Prepare a simple but known URL to avoid testing from the NTP. | |
91 web::test::SetUpFileBasedHttpServer(); | |
92 const GURL simple_URL = web::test::HttpServer::MakeUrl( | |
93 "http://ios/testing/data/http_server_files/destination.html"); | |
94 | |
95 // Prepare a helper block to test Sad Tab navigating from and to normal pages. | |
96 void (^loadAndCheckSimpleURL)() = ^void() { | |
97 [ChromeEarlGrey loadURL:simple_URL]; | |
98 [ChromeEarlGrey waitForWebViewContainingText:"You've arrived"]; | |
99 [[EarlGrey selectElementWithMatcher:reloadSadTabTitleText()] | |
100 assertWithMatcher:grey_nil()]; | |
101 [[EarlGrey selectElementWithMatcher:feedbackSadTabTitleContainsText()] | |
102 assertWithMatcher:grey_nil()]; | |
103 }; | |
104 | |
105 loadAndCheckSimpleURL(); | |
106 | |
107 // Navigate to the chrome://crash URL which should show the Sad Tab. | |
108 // Use chome_test_util::LoadURL() directly to avoid ChomeEarlGrey helper | |
baxley
2017/06/27 06:02:11
sorry for the super-nit...
is it chrome_test_util:
PL
2017/06/27 23:21:46
Yes! I love the super-nit don't worry, helps me lo
| |
109 // methods which expect to wait for web content. | |
110 const GURL crash_URL = GURL("chrome://crash"); | |
111 chrome_test_util::LoadUrl(crash_URL); | |
112 [[EarlGrey selectElementWithMatcher:reloadSadTabTitleText()] | |
113 assertWithMatcher:grey_notNil()]; | |
114 | |
115 // Ensure user can navigate away from Sad Tab, and the Sad Tab content | |
116 // is no longer visible. | |
117 loadAndCheckSimpleURL(); | |
118 | |
119 // A second visit to the crashing URL should show a feedback message. | |
120 // It should also show help messages including an invitation to use | |
121 // Incognito Mode. | |
122 chrome_test_util::LoadUrl(crash_URL); | |
123 [[EarlGrey selectElementWithMatcher:feedbackSadTabTitleContainsText()] | |
124 assertWithMatcher:grey_notNil()]; | |
125 [[EarlGrey selectElementWithMatcher:incognitoHelpContainsText()] | |
126 assertWithMatcher:grey_notNil()]; | |
127 | |
128 // Again ensure a user can navigate away from Sad Tab, and the Sad Tab content | |
129 // is no longer visible. | |
130 loadAndCheckSimpleURL(); | |
131 | |
132 // Open an Incognito tab and browse somewhere, the repeated crash UI changes | |
133 // dependent on the Incognito mode. | |
134 [ChromeEarlGreyUI openToolsMenu]; | |
135 id<GREYMatcher> newIncognitoTabButtonMatcher = | |
136 grey_accessibilityID(kToolsMenuNewIncognitoTabId); | |
137 [[EarlGrey selectElementWithMatcher:newIncognitoTabButtonMatcher] | |
138 performAction:grey_tap()]; | |
139 chrome_test_util::AssertIncognitoTabCount(1); | |
140 loadAndCheckSimpleURL(); | |
141 | |
142 // Test an initial crash, and then a second crash in Incognito mode, as above. | |
143 // Incognito mode should not be suggested if already in Incognito mode. | |
144 chrome_test_util::LoadUrl(crash_URL); | |
145 [[EarlGrey selectElementWithMatcher:reloadSadTabTitleText()] | |
146 assertWithMatcher:grey_notNil()]; | |
147 chrome_test_util::LoadUrl(crash_URL); | |
148 [[EarlGrey selectElementWithMatcher:feedbackSadTabTitleContainsText()] | |
149 assertWithMatcher:grey_notNil()]; | |
150 [[EarlGrey selectElementWithMatcher:incognitoHelpContainsText()] | |
151 assertWithMatcher:grey_nil()]; | |
152 | |
153 // Finally, ensure that the user can browse away from the Sad Tab page | |
154 // in Incognito Mode. | |
155 loadAndCheckSimpleURL(); | |
156 } | |
157 | |
158 @end | |
OLD | NEW |