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

Side by Side Diff: ios/chrome/browser/context_menu/context_menu_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 <EarlGrey/EarlGrey.h> 5 #import <EarlGrey/EarlGrey.h>
6 #import <UIKit/UIKit.h> 6 #import <UIKit/UIKit.h>
7 #import <XCTest/XCTest.h> 7 #import <XCTest/XCTest.h>
8 8
9 #include "base/strings/sys_string_conversions.h" 9 #include "base/strings/sys_string_conversions.h"
10 #include "ios/chrome/browser/ui/ui_util.h" 10 #include "ios/chrome/browser/ui/ui_util.h"
11 #include "ios/chrome/grit/ios_strings.h" 11 #include "ios/chrome/grit/ios_strings.h"
12 #import "ios/chrome/test/app/chrome_test_util.h" 12 #import "ios/chrome/test/app/chrome_test_util.h"
13 #import "ios/chrome/test/app/settings_test_util.h" 13 #import "ios/chrome/test/app/settings_test_util.h"
14 #import "ios/chrome/test/app/tab_test_util.h" 14 #import "ios/chrome/test/app/tab_test_util.h"
15 #include "ios/chrome/test/app/web_view_interaction_test_util.h" 15 #include "ios/chrome/test/app/web_view_interaction_test_util.h"
16 #import "ios/chrome/test/earl_grey/chrome_actions.h" 16 #import "ios/chrome/test/earl_grey/chrome_actions.h"
17 #import "ios/chrome/test/earl_grey/chrome_assertions.h" 17 #import "ios/chrome/test/earl_grey/chrome_assertions.h"
18 #import "ios/chrome/test/earl_grey/chrome_earl_grey.h" 18 #import "ios/chrome/test/earl_grey/chrome_earl_grey.h"
19 #import "ios/chrome/test/earl_grey/chrome_matchers.h" 19 #import "ios/chrome/test/earl_grey/chrome_matchers.h"
20 #import "ios/chrome/test/earl_grey/chrome_test_case.h" 20 #import "ios/chrome/test/earl_grey/chrome_test_case.h"
21 #include "ios/chrome/test/earl_grey/chrome_util.h" 21 #include "ios/chrome/test/earl_grey/chrome_util.h"
22 #import "ios/testing/wait_util.h" 22 #import "ios/testing/wait_util.h"
23 #import "ios/web/public/test/earl_grey/web_view_matchers.h" 23 #import "ios/web/public/test/earl_grey/web_view_matchers.h"
24 #import "ios/web/public/test/http_server.h" 24 #import "ios/web/public/test/http_server.h"
25 #import "ios/web/public/test/http_server_util.h" 25 #import "ios/web/public/test/http_server_util.h"
26 #include "url/gurl.h" 26 #include "url/gurl.h"
27 27
28 using chrome_test_util::buttonWithAccessibilityLabelId; 28 using chrome_test_util::ButtonWithAccessibilityLabelId;
29 29
30 namespace { 30 namespace {
31 const char kUrlChromiumLogoPage[] = 31 const char kUrlChromiumLogoPage[] =
32 "http://ios/testing/data/http_server_files/chromium_logo_page.html"; 32 "http://ios/testing/data/http_server_files/chromium_logo_page.html";
33 const char kUrlChromiumLogoImg[] = 33 const char kUrlChromiumLogoImg[] =
34 "http://ios/testing/data/http_server_files/chromium_logo.png"; 34 "http://ios/testing/data/http_server_files/chromium_logo.png";
35 const char kUrlInitialPage[] = "http://scenarioContextMenuOpenInNewTab"; 35 const char kUrlInitialPage[] = "http://scenarioContextMenuOpenInNewTab";
36 const char kUrlDestinationPage[] = "http://destination"; 36 const char kUrlDestinationPage[] = "http://destination";
37 const char kChromiumImageID[] = "chromium_image"; 37 const char kChromiumImageID[] = "chromium_image";
38 const char kDestinationLinkID[] = "link"; 38 const char kDestinationLinkID[] = "link";
39 39
40 // HTML content of the destination page that sets the page title. 40 // HTML content of the destination page that sets the page title.
41 const char kDestinationHtml[] = 41 const char kDestinationHtml[] =
42 "<script>document.title='new doc'</script>You made it!"; 42 "<script>document.title='new doc'</script>You made it!";
43 43
44 // Matcher for the open image button in the context menu. 44 // Matcher for the open image button in the context menu.
45 id<GREYMatcher> openImageButton() { 45 id<GREYMatcher> OpenImageButton() {
46 return buttonWithAccessibilityLabelId(IDS_IOS_CONTENT_CONTEXT_OPENIMAGE); 46 return ButtonWithAccessibilityLabelId(IDS_IOS_CONTENT_CONTEXT_OPENIMAGE);
47 } 47 }
48 48
49 // Matcher for the open image in new tab button in the context menu. 49 // Matcher for the open image in new tab button in the context menu.
50 id<GREYMatcher> openImageInNewTabButton() { 50 id<GREYMatcher> OpenImageInNewTabButton() {
51 return buttonWithAccessibilityLabelId( 51 return ButtonWithAccessibilityLabelId(
52 IDS_IOS_CONTENT_CONTEXT_OPENIMAGENEWTAB); 52 IDS_IOS_CONTENT_CONTEXT_OPENIMAGENEWTAB);
53 } 53 }
54 54
55 // Matcher for the open link in new tab button in the context menu. 55 // Matcher for the open link in new tab button in the context menu.
56 id<GREYMatcher> openLinkInNewTabButton() { 56 id<GREYMatcher> OpenLinkInNewTabButton() {
Eugene But (OOO till 7-30) 2017/01/23 19:39:03 Could you please add TODO(crbug.com/638674) to cle
baxley 2017/01/24 22:18:48 Done.
57 return buttonWithAccessibilityLabelId(IDS_IOS_CONTENT_CONTEXT_OPENLINKNEWTAB); 57 return ButtonWithAccessibilityLabelId(IDS_IOS_CONTENT_CONTEXT_OPENLINKNEWTAB);
58 } 58 }
59 59
60 // Waits for the context menu item to disappear. TODO(crbug.com/682871): Remove 60 // Waits for the context menu item to disappear. TODO(crbug.com/682871): Remove
61 // this once EarlGrey is synchronized with context menu. 61 // this once EarlGrey is synchronized with context menu.
62 void WaitForContextMenuItemDisappeared(id<GREYMatcher> contextMenuItemButton) { 62 void WaitForContextMenuItemDisappeared(id<GREYMatcher> contextMenuItemButton) {
63 ConditionBlock condition = ^{ 63 ConditionBlock condition = ^{
64 NSError* error = nil; 64 NSError* error = nil;
65 [[EarlGrey selectElementWithMatcher:contextMenuItemButton] 65 [[EarlGrey selectElementWithMatcher:contextMenuItemButton]
66 assertWithMatcher:grey_nil() 66 assertWithMatcher:grey_nil()
67 error:&error]; 67 error:&error];
68 return error == nil; 68 return error == nil;
69 }; 69 };
70 GREYAssert(testing::WaitUntilConditionOrTimeout( 70 GREYAssert(testing::WaitUntilConditionOrTimeout(
71 testing::kWaitForUIElementTimeout, condition), 71 testing::kWaitForUIElementTimeout, condition),
72 [NSString stringWithFormat:@"Waiting for matcher %@ failed.", 72 [NSString stringWithFormat:@"Waiting for matcher %@ failed.",
73 contextMenuItemButton]); 73 contextMenuItemButton]);
74 } 74 }
75 75
76 // Long press on |elementId| to trigger context menu and then tap on 76 // Long press on |elementId| to trigger context menu and then tap on
77 // |contextMenuItemButton| item. 77 // |contextMenuItemButton| item.
78 void LongPressElementAndTapOnButton(const char* elementId, 78 void LongPressElementAndTapOnButton(const char* elementId,
79 id<GREYMatcher> contextMenuItemButton) { 79 id<GREYMatcher> contextMenuItemButton) {
80 [[EarlGrey selectElementWithMatcher:chrome_test_util:: 80 [[EarlGrey selectElementWithMatcher:chrome_test_util::
81 webViewBelongingToWebController()] 81 WebViewBelongingToWebController()]
82 performAction:chrome_test_util::longPressElementForContextMenu(elementId, 82 performAction:chrome_test_util::longPressElementForContextMenu(elementId,
83 true)]; 83 true)];
84 84
85 [[EarlGrey selectElementWithMatcher:contextMenuItemButton] 85 [[EarlGrey selectElementWithMatcher:contextMenuItemButton]
86 assertWithMatcher:grey_notNil()]; 86 assertWithMatcher:grey_notNil()];
87 [[EarlGrey selectElementWithMatcher:contextMenuItemButton] 87 [[EarlGrey selectElementWithMatcher:contextMenuItemButton]
88 performAction:grey_tap()]; 88 performAction:grey_tap()];
89 WaitForContextMenuItemDisappeared(contextMenuItemButton); 89 WaitForContextMenuItemDisappeared(contextMenuItemButton);
90 } 90 }
91 91
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 123
124 // Tests that selecting "Open Image" from the context menu properly opens the 124 // Tests that selecting "Open Image" from the context menu properly opens the
125 // image in the current tab. 125 // image in the current tab.
126 - (void)testOpenImageInCurrentTabFromContextMenu { 126 - (void)testOpenImageInCurrentTabFromContextMenu {
127 GURL pageURL = web::test::HttpServer::MakeUrl(kUrlChromiumLogoPage); 127 GURL pageURL = web::test::HttpServer::MakeUrl(kUrlChromiumLogoPage);
128 GURL imageURL = web::test::HttpServer::MakeUrl(kUrlChromiumLogoImg); 128 GURL imageURL = web::test::HttpServer::MakeUrl(kUrlChromiumLogoImg);
129 web::test::SetUpFileBasedHttpServer(); 129 web::test::SetUpFileBasedHttpServer();
130 [ChromeEarlGrey loadURL:pageURL]; 130 [ChromeEarlGrey loadURL:pageURL];
131 chrome_test_util::AssertMainTabCount(1U); 131 chrome_test_util::AssertMainTabCount(1U);
132 132
133 LongPressElementAndTapOnButton(kChromiumImageID, openImageButton()); 133 LongPressElementAndTapOnButton(kChromiumImageID, OpenImageButton());
134 134
135 // Verify url and tab count. 135 // Verify url and tab count.
136 [[EarlGrey selectElementWithMatcher:chrome_test_util::omniboxText( 136 [[EarlGrey selectElementWithMatcher:chrome_test_util::OmniboxText(
137 imageURL.GetContent())] 137 imageURL.GetContent())]
138 assertWithMatcher:grey_notNil()]; 138 assertWithMatcher:grey_notNil()];
139 chrome_test_util::AssertMainTabCount(1U); 139 chrome_test_util::AssertMainTabCount(1U);
140 } 140 }
141 141
142 // Tests that selecting "Open Image in New Tab" from the context menu properly 142 // Tests that selecting "Open Image in New Tab" from the context menu properly
143 // opens the image in a new background tab. 143 // opens the image in a new background tab.
144 - (void)testOpenImageInNewTabFromContextMenu { 144 - (void)testOpenImageInNewTabFromContextMenu {
145 GURL pageURL = web::test::HttpServer::MakeUrl(kUrlChromiumLogoPage); 145 GURL pageURL = web::test::HttpServer::MakeUrl(kUrlChromiumLogoPage);
146 GURL imageURL = web::test::HttpServer::MakeUrl(kUrlChromiumLogoImg); 146 GURL imageURL = web::test::HttpServer::MakeUrl(kUrlChromiumLogoImg);
147 web::test::SetUpFileBasedHttpServer(); 147 web::test::SetUpFileBasedHttpServer();
148 [ChromeEarlGrey loadURL:pageURL]; 148 [ChromeEarlGrey loadURL:pageURL];
149 chrome_test_util::AssertMainTabCount(1U); 149 chrome_test_util::AssertMainTabCount(1U);
150 150
151 LongPressElementAndTapOnButton(kChromiumImageID, openImageInNewTabButton()); 151 LongPressElementAndTapOnButton(kChromiumImageID, OpenImageInNewTabButton());
152 152
153 SelectTabAtIndexInCurrentMode(1U); 153 SelectTabAtIndexInCurrentMode(1U);
154 154
155 // Verify url and tab count. 155 // Verify url and tab count.
156 [[EarlGrey selectElementWithMatcher:chrome_test_util::omniboxText( 156 [[EarlGrey selectElementWithMatcher:chrome_test_util::OmniboxText(
157 imageURL.GetContent())] 157 imageURL.GetContent())]
158 assertWithMatcher:grey_notNil()]; 158 assertWithMatcher:grey_notNil()];
159 chrome_test_util::AssertMainTabCount(2U); 159 chrome_test_util::AssertMainTabCount(2U);
160 } 160 }
161 161
162 // Tests "Open in New Tab" on context menu. 162 // Tests "Open in New Tab" on context menu.
163 - (void)testContextMenuOpenInNewTab { 163 - (void)testContextMenuOpenInNewTab {
164 // Set up test simple http server. 164 // Set up test simple http server.
165 std::map<GURL, std::string> responses; 165 std::map<GURL, std::string> responses;
166 GURL initialURL = web::test::HttpServer::MakeUrl(kUrlInitialPage); 166 GURL initialURL = web::test::HttpServer::MakeUrl(kUrlInitialPage);
167 GURL destinationURL = web::test::HttpServer::MakeUrl(kUrlDestinationPage); 167 GURL destinationURL = web::test::HttpServer::MakeUrl(kUrlDestinationPage);
168 168
169 // The initial page contains a link to the destination page. 169 // The initial page contains a link to the destination page.
170 responses[initialURL] = "<a style='margin-left:50px' href='" + 170 responses[initialURL] = "<a style='margin-left:50px' href='" +
171 destinationURL.spec() + "' id='link'>link</a>"; 171 destinationURL.spec() + "' id='link'>link</a>";
172 responses[destinationURL] = kDestinationHtml; 172 responses[destinationURL] = kDestinationHtml;
173 173
174 web::test::SetUpSimpleHttpServer(responses); 174 web::test::SetUpSimpleHttpServer(responses);
175 [ChromeEarlGrey loadURL:initialURL]; 175 [ChromeEarlGrey loadURL:initialURL];
176 chrome_test_util::AssertMainTabCount(1U); 176 chrome_test_util::AssertMainTabCount(1U);
177 177
178 LongPressElementAndTapOnButton(kDestinationLinkID, openLinkInNewTabButton()); 178 LongPressElementAndTapOnButton(kDestinationLinkID, OpenLinkInNewTabButton());
179 179
180 SelectTabAtIndexInCurrentMode(1U); 180 SelectTabAtIndexInCurrentMode(1U);
181 181
182 // Verify url and tab count. 182 // Verify url and tab count.
183 [[EarlGrey selectElementWithMatcher:chrome_test_util::omniboxText( 183 [[EarlGrey selectElementWithMatcher:chrome_test_util::OmniboxText(
184 destinationURL.GetContent())] 184 destinationURL.GetContent())]
185 assertWithMatcher:grey_notNil()]; 185 assertWithMatcher:grey_notNil()];
186 chrome_test_util::AssertMainTabCount(2U); 186 chrome_test_util::AssertMainTabCount(2U);
187 } 187 }
188 188
189 // Tests "Open in New Tab" on context menu on a link that requires scrolling 189 // Tests "Open in New Tab" on context menu on a link that requires scrolling
190 // on the page to verify that context menu can be properly triggered in the 190 // on the page to verify that context menu can be properly triggered in the
191 // current screen view. 191 // current screen view.
192 - (void)testContextMenuOpenInNewTabFromTallPage { 192 - (void)testContextMenuOpenInNewTabFromTallPage {
193 // Set up test simple http server. 193 // Set up test simple http server.
194 std::map<GURL, std::string> responses; 194 std::map<GURL, std::string> responses;
195 GURL initialURL = 195 GURL initialURL =
196 web::test::HttpServer::MakeUrl("http://scenarioContextMenuOpenInNewTab"); 196 web::test::HttpServer::MakeUrl("http://scenarioContextMenuOpenInNewTab");
197 GURL destinationURL = web::test::HttpServer::MakeUrl("http://destination"); 197 GURL destinationURL = web::test::HttpServer::MakeUrl("http://destination");
198 198
199 // The initial page contains a link to the destination page that is below a 199 // The initial page contains a link to the destination page that is below a
200 // really tall div so that scrolling is required. 200 // really tall div so that scrolling is required.
201 responses[initialURL] = 201 responses[initialURL] =
202 "<div style='height:4000px'></div>" 202 "<div style='height:4000px'></div>"
203 "<a style='margin-left:50px' href='" + 203 "<a style='margin-left:50px' href='" +
204 destinationURL.spec() + "' id='link'>link</a>"; 204 destinationURL.spec() + "' id='link'>link</a>";
205 responses[destinationURL] = kDestinationHtml; 205 responses[destinationURL] = kDestinationHtml;
206 206
207 web::test::SetUpSimpleHttpServer(responses); 207 web::test::SetUpSimpleHttpServer(responses);
208 [ChromeEarlGrey loadURL:initialURL]; 208 [ChromeEarlGrey loadURL:initialURL];
209 chrome_test_util::AssertMainTabCount(1U); 209 chrome_test_util::AssertMainTabCount(1U);
210 210
211 // Scroll down on the web view to make the link visible. 211 // Scroll down on the web view to make the link visible.
212 [[EarlGrey 212 [[EarlGrey
213 selectElementWithMatcher:webViewScrollView( 213 selectElementWithMatcher:WebViewScrollView(
214 chrome_test_util::GetCurrentWebState())] 214 chrome_test_util::GetCurrentWebState())]
215 performAction:grey_swipeFastInDirection(kGREYDirectionUp)]; 215 performAction:grey_swipeFastInDirection(kGREYDirectionUp)];
216 [[EarlGrey selectElementWithMatcher:chrome_test_util::webViewContainingText( 216 [[EarlGrey selectElementWithMatcher:chrome_test_util::WebViewContainingText(
217 kDestinationLinkID)] 217 kDestinationLinkID)]
218 assertWithMatcher:grey_notNil()]; 218 assertWithMatcher:grey_notNil()];
219 219
220 LongPressElementAndTapOnButton(kDestinationLinkID, openLinkInNewTabButton()); 220 LongPressElementAndTapOnButton(kDestinationLinkID, OpenLinkInNewTabButton());
221 221
222 // Earl Grey cannot preperly synchronize some animations, so adding a 222 // Earl Grey cannot preperly synchronize some animations, so adding a
223 // WaitUntilCondition to wait for the new tab opening animation to finish 223 // WaitUntilCondition to wait for the new tab opening animation to finish
224 // and the scroll view to become interactable. 224 // and the scroll view to become interactable.
225 ConditionBlock condition = ^{ 225 ConditionBlock condition = ^{
226 NSError* error = nil; 226 NSError* error = nil;
227 [[EarlGrey 227 [[EarlGrey
228 selectElementWithMatcher:webViewScrollView( 228 selectElementWithMatcher:WebViewScrollView(
229 chrome_test_util::GetCurrentWebState())] 229 chrome_test_util::GetCurrentWebState())]
230 assertWithMatcher:grey_interactable() 230 assertWithMatcher:grey_interactable()
231 error:&error]; 231 error:&error];
232 return !error; 232 return !error;
233 }; 233 };
234 GREYAssert(testing::WaitUntilConditionOrTimeout( 234 GREYAssert(testing::WaitUntilConditionOrTimeout(
235 testing::kWaitForUIElementTimeout, condition), 235 testing::kWaitForUIElementTimeout, condition),
236 @"Web view did not become interactable"); 236 @"Web view did not become interactable");
237 237
238 // Make the toolbar visible by scrolling up on the web view to select the 238 // Make the toolbar visible by scrolling up on the web view to select the
239 // newly opened tab. 239 // newly opened tab.
240 [[EarlGrey 240 [[EarlGrey
241 selectElementWithMatcher:webViewScrollView( 241 selectElementWithMatcher:WebViewScrollView(
242 chrome_test_util::GetCurrentWebState())] 242 chrome_test_util::GetCurrentWebState())]
243 performAction:grey_swipeFastInDirection(kGREYDirectionDown)]; 243 performAction:grey_swipeFastInDirection(kGREYDirectionDown)];
244 chrome_test_util::AssertToolbarVisible(); 244 chrome_test_util::AssertToolbarVisible();
245 245
246 SelectTabAtIndexInCurrentMode(1U); 246 SelectTabAtIndexInCurrentMode(1U);
247 247
248 // Verify url and tab count. 248 // Verify url and tab count.
249 [[EarlGrey selectElementWithMatcher:chrome_test_util::omniboxText( 249 [[EarlGrey selectElementWithMatcher:chrome_test_util::OmniboxText(
250 destinationURL.GetContent())] 250 destinationURL.GetContent())]
251 assertWithMatcher:grey_notNil()]; 251 assertWithMatcher:grey_notNil()];
252 chrome_test_util::AssertMainTabCount(2U); 252 chrome_test_util::AssertMainTabCount(2U);
253 } 253 }
254 254
255 @end 255 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698