| OLD | NEW |
| (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 <XCTest/XCTest.h> |
| 7 |
| 8 #include "base/strings/sys_string_conversions.h" |
| 9 #include "components/strings/grit/components_strings.h" |
| 10 #import "ios/chrome/browser/ui/chrome_web_view_factory.h" |
| 11 #import "ios/chrome/browser/ui/toolbar/toolbar_controller.h" |
| 12 #include "ios/chrome/browser/ui/tools_menu/tools_menu_constants.h" |
| 13 #import "ios/chrome/browser/ui/uikit_ui_util.h" |
| 14 #include "ios/chrome/grit/ios_strings.h" |
| 15 #import "ios/chrome/test/earl_grey/accessibility_util.h" |
| 16 #import "ios/chrome/test/earl_grey/chrome_earl_grey.h" |
| 17 #import "ios/chrome/test/earl_grey/chrome_earl_grey_ui.h" |
| 18 #import "ios/chrome/test/earl_grey/chrome_matchers.h" |
| 19 #import "ios/chrome/test/earl_grey/chrome_test_case.h" |
| 20 #import "ios/web/public/test/http_server.h" |
| 21 #include "ios/web/public/test/http_server_util.h" |
| 22 #include "ios/web/public/test/response_providers/data_response_provider.h" |
| 23 #include "ui/base/l10n/l10n_util.h" |
| 24 |
| 25 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 26 #error "This file requires ARC support." |
| 27 #endif |
| 28 |
| 29 using chrome_test_util::WebViewContainingText; |
| 30 |
| 31 namespace { |
| 32 |
| 33 const char kUserAgentTestURL[] = |
| 34 "http://ios/testing/data/http_server_files/user_agent_test_page.html"; |
| 35 |
| 36 const char kMobileSiteLabel[] = "Mobile"; |
| 37 |
| 38 const char kDesktopSiteLabel[] = "Desktop"; |
| 39 |
| 40 // Matcher for the button to request desktop site. |
| 41 id<GREYMatcher> RequestDesktopButton() { |
| 42 return grey_accessibilityID(kToolsMenuRequestDesktopId); |
| 43 } |
| 44 |
| 45 // Matcher for the button to request mobile site. |
| 46 id<GREYMatcher> RequestMobileButton() { |
| 47 return grey_accessibilityID(kToolsMenuRequestMobileId); |
| 48 } |
| 49 |
| 50 // Matcher for the button to close the tools menu. |
| 51 id<GREYMatcher> CloseToolsMenuButton() { |
| 52 NSString* closeMenuButtonText = |
| 53 l10n_util::GetNSString(IDS_IOS_TOOLBAR_CLOSE_MENU); |
| 54 return grey_allOf(grey_accessibilityID(kToolbarToolsMenuButtonIdentifier), |
| 55 grey_accessibilityLabel(closeMenuButtonText), nil); |
| 56 } |
| 57 |
| 58 // A ResponseProvider that provides user agent for httpServer request. |
| 59 class UserAgentResponseProvider : public web::DataResponseProvider { |
| 60 public: |
| 61 bool CanHandleRequest(const Request& request) override { return true; } |
| 62 |
| 63 void GetResponseHeadersAndBody( |
| 64 const Request& request, |
| 65 scoped_refptr<net::HttpResponseHeaders>* headers, |
| 66 std::string* response_body) override { |
| 67 // Do not return anything if static plist file has been requested, |
| 68 // as plain text is not a valid property list content. |
| 69 if ([[base::SysUTF8ToNSString(request.url.spec()) pathExtension] |
| 70 isEqualToString:@"plist"]) { |
| 71 *headers = |
| 72 web::ResponseProvider::GetResponseHeaders("", net::HTTP_NO_CONTENT); |
| 73 return; |
| 74 } |
| 75 |
| 76 *headers = web::ResponseProvider::GetDefaultResponseHeaders(); |
| 77 std::string userAgent; |
| 78 const std::string kDesktopUserAgent = |
| 79 base::SysNSStringToUTF8(ChromeWebView::kDesktopUserAgent); |
| 80 if (request.headers.GetHeader("User-Agent", &userAgent) && |
| 81 userAgent == kDesktopUserAgent) { |
| 82 response_body->assign("Desktop"); |
| 83 } else { |
| 84 response_body->assign("Mobile"); |
| 85 } |
| 86 } |
| 87 }; |
| 88 } // namespace |
| 89 |
| 90 // Tests for the tools popup menu. |
| 91 @interface RequestDesktopMobileSiteTestCase : ChromeTestCase |
| 92 @end |
| 93 |
| 94 @implementation RequestDesktopMobileSiteTestCase |
| 95 |
| 96 // Tests that requesting desktop site of a page works and the user agent |
| 97 // propagates to the next navigations in the same tab. |
| 98 - (void)testRequestDesktopSitePropagatesToNextNavigations { |
| 99 std::unique_ptr<web::DataResponseProvider> provider( |
| 100 new UserAgentResponseProvider()); |
| 101 web::test::SetUpHttpServer(std::move(provider)); |
| 102 |
| 103 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl("http://1.com")]; |
| 104 // Verify initial reception of the mobile site. |
| 105 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kMobileSiteLabel)] |
| 106 assertWithMatcher:grey_notNil()]; |
| 107 |
| 108 // Request and verify reception of the desktop site. |
| 109 [ChromeEarlGreyUI openToolsMenu]; |
| 110 [[EarlGrey selectElementWithMatcher:RequestDesktopButton()] |
| 111 performAction:grey_tap()]; |
| 112 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kDesktopSiteLabel)] |
| 113 assertWithMatcher:grey_notNil()]; |
| 114 |
| 115 // Verify that desktop user agent propagates. |
| 116 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl("http://2.com")]; |
| 117 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kDesktopSiteLabel)] |
| 118 assertWithMatcher:grey_notNil()]; |
| 119 } |
| 120 |
| 121 // Tests that requesting desktop site of a page works and desktop user agent |
| 122 // does not propagate to next the new tab. |
| 123 - (void)testRequestDesktopSiteDoesNotPropagateToNewTab { |
| 124 std::unique_ptr<web::DataResponseProvider> provider( |
| 125 new UserAgentResponseProvider()); |
| 126 web::test::SetUpHttpServer(std::move(provider)); |
| 127 |
| 128 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl("http://1.com")]; |
| 129 // Verify initial reception of the mobile site. |
| 130 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kMobileSiteLabel)] |
| 131 assertWithMatcher:grey_notNil()]; |
| 132 |
| 133 // Request and verify reception of the desktop site. |
| 134 [ChromeEarlGreyUI openToolsMenu]; |
| 135 [[EarlGrey selectElementWithMatcher:RequestDesktopButton()] |
| 136 performAction:grey_tap()]; |
| 137 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kDesktopSiteLabel)] |
| 138 assertWithMatcher:grey_notNil()]; |
| 139 |
| 140 // Verify that desktop user agent does not propagate to new tab. |
| 141 [ChromeEarlGreyUI openNewTab]; |
| 142 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl("http://2.com")]; |
| 143 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kMobileSiteLabel)] |
| 144 assertWithMatcher:grey_notNil()]; |
| 145 } |
| 146 |
| 147 // Tests that requesting desktop site of a page works and going back re-opens |
| 148 // mobile version of the page. |
| 149 - (void)testRequestDesktopSiteGoBackToMobile { |
| 150 std::unique_ptr<web::DataResponseProvider> provider( |
| 151 new UserAgentResponseProvider()); |
| 152 web::test::SetUpHttpServer(std::move(provider)); |
| 153 |
| 154 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl("http://1.com")]; |
| 155 // Verify initial reception of the mobile site. |
| 156 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kMobileSiteLabel)] |
| 157 assertWithMatcher:grey_notNil()]; |
| 158 |
| 159 // Request and verify reception of the desktop site. |
| 160 [ChromeEarlGreyUI openToolsMenu]; |
| 161 [[EarlGrey selectElementWithMatcher:RequestDesktopButton()] |
| 162 performAction:grey_tap()]; |
| 163 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kDesktopSiteLabel)] |
| 164 assertWithMatcher:grey_notNil()]; |
| 165 |
| 166 // Verify that going back returns to the mobile site. |
| 167 [[EarlGrey selectElementWithMatcher:chrome_test_util::BackButton()] |
| 168 performAction:grey_tap()]; |
| 169 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kMobileSiteLabel)] |
| 170 assertWithMatcher:grey_notNil()]; |
| 171 } |
| 172 |
| 173 // Tests that requesting mobile site of a page works and the user agent |
| 174 // propagates to the next navigations in the same tab. |
| 175 - (void)testRequestMobileSitePropagatesToNextNavigations { |
| 176 std::unique_ptr<web::DataResponseProvider> provider( |
| 177 new UserAgentResponseProvider()); |
| 178 web::test::SetUpHttpServer(std::move(provider)); |
| 179 |
| 180 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl("http://1.com")]; |
| 181 // Verify initial reception of the mobile site. |
| 182 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kMobileSiteLabel)] |
| 183 assertWithMatcher:grey_notNil()]; |
| 184 |
| 185 // Request and verify reception of the desktop site. |
| 186 [ChromeEarlGreyUI openToolsMenu]; |
| 187 [[EarlGrey selectElementWithMatcher:RequestDesktopButton()] |
| 188 performAction:grey_tap()]; |
| 189 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kDesktopSiteLabel)] |
| 190 assertWithMatcher:grey_notNil()]; |
| 191 |
| 192 // Request and verify reception of the mobile site. |
| 193 [ChromeEarlGreyUI openToolsMenu]; |
| 194 [[EarlGrey selectElementWithMatcher:RequestMobileButton()] |
| 195 performAction:grey_tap()]; |
| 196 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kMobileSiteLabel)] |
| 197 assertWithMatcher:grey_notNil()]; |
| 198 |
| 199 // Verify that mobile user agent propagates. |
| 200 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl("http://2.com")]; |
| 201 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kMobileSiteLabel)] |
| 202 assertWithMatcher:grey_notNil()]; |
| 203 } |
| 204 |
| 205 // Tests that requesting mobile site of a page works and going back re-opens |
| 206 // desktop version of the page. |
| 207 - (void)testRequestMobileSiteGoBackToDesktop { |
| 208 std::unique_ptr<web::DataResponseProvider> provider( |
| 209 new UserAgentResponseProvider()); |
| 210 web::test::SetUpHttpServer(std::move(provider)); |
| 211 |
| 212 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl("http://1.com")]; |
| 213 // Verify initial reception of the mobile site. |
| 214 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kMobileSiteLabel)] |
| 215 assertWithMatcher:grey_notNil()]; |
| 216 |
| 217 // Request and verify reception of the desktop site. |
| 218 [ChromeEarlGreyUI openToolsMenu]; |
| 219 [[EarlGrey selectElementWithMatcher:RequestDesktopButton()] |
| 220 performAction:grey_tap()]; |
| 221 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kDesktopSiteLabel)] |
| 222 assertWithMatcher:grey_notNil()]; |
| 223 |
| 224 // Request and verify reception of the mobile site. |
| 225 [ChromeEarlGreyUI openToolsMenu]; |
| 226 [[EarlGrey selectElementWithMatcher:RequestMobileButton()] |
| 227 performAction:grey_tap()]; |
| 228 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kMobileSiteLabel)] |
| 229 assertWithMatcher:grey_notNil()]; |
| 230 |
| 231 // Verify that going back returns to the desktop site. |
| 232 [[EarlGrey selectElementWithMatcher:chrome_test_util::BackButton()] |
| 233 performAction:grey_tap()]; |
| 234 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kDesktopSiteLabel)] |
| 235 assertWithMatcher:grey_notNil()]; |
| 236 } |
| 237 |
| 238 // Tests that requesting desktop site button is not enabled on new tab pages. |
| 239 - (void)testRequestDesktopSiteNotEnabledOnNewTabPage { |
| 240 // Verify tapping on request desktop button is no-op. |
| 241 [ChromeEarlGreyUI openToolsMenu]; |
| 242 [[[EarlGrey selectElementWithMatcher:RequestDesktopButton()] |
| 243 assertWithMatcher:grey_notNil()] performAction:grey_tap()]; |
| 244 [[EarlGrey selectElementWithMatcher:RequestDesktopButton()] |
| 245 assertWithMatcher:grey_notNil()]; |
| 246 |
| 247 [[EarlGrey selectElementWithMatcher:CloseToolsMenuButton()] |
| 248 performAction:grey_tap()]; |
| 249 } |
| 250 |
| 251 // Tests that requesting desktop site button is not enabled on WebUI pages. |
| 252 - (void)testRequestDesktopSiteNotEnabledOnWebUIPage { |
| 253 [ChromeEarlGrey loadURL:GURL("chrome://version")]; |
| 254 |
| 255 // Verify tapping on request desktop button is no-op. |
| 256 [ChromeEarlGreyUI openToolsMenu]; |
| 257 [[[EarlGrey selectElementWithMatcher:RequestDesktopButton()] |
| 258 assertWithMatcher:grey_notNil()] performAction:grey_tap()]; |
| 259 [[EarlGrey selectElementWithMatcher:RequestDesktopButton()] |
| 260 assertWithMatcher:grey_notNil()]; |
| 261 |
| 262 [[EarlGrey selectElementWithMatcher:CloseToolsMenuButton()] |
| 263 performAction:grey_tap()]; |
| 264 } |
| 265 |
| 266 // Tests that requesting desktop site works correctly for script-based |
| 267 // desktop/mobile differentation. |
| 268 - (void)testRequestDesktopScript { |
| 269 web::test::SetUpFileBasedHttpServer(); |
| 270 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kUserAgentTestURL)]; |
| 271 // Verify initial reception of the mobile site. |
| 272 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kMobileSiteLabel)] |
| 273 assertWithMatcher:grey_notNil()]; |
| 274 |
| 275 // Request and verify reception of the desktop site. |
| 276 [ChromeEarlGreyUI openToolsMenu]; |
| 277 [[EarlGrey selectElementWithMatcher:RequestDesktopButton()] |
| 278 performAction:grey_tap()]; |
| 279 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kDesktopSiteLabel)] |
| 280 assertWithMatcher:grey_notNil()]; |
| 281 } |
| 282 |
| 283 // Tests that requesting mobile site works correctly for script-based |
| 284 // desktop/mobile differentation. |
| 285 - (void)testRequestMobileScript { |
| 286 web::test::SetUpFileBasedHttpServer(); |
| 287 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kUserAgentTestURL)]; |
| 288 // Verify initial reception of the mobile site. |
| 289 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kMobileSiteLabel)] |
| 290 assertWithMatcher:grey_notNil()]; |
| 291 |
| 292 // Request and verify reception of the desktop site. |
| 293 [ChromeEarlGreyUI openToolsMenu]; |
| 294 [[EarlGrey selectElementWithMatcher:RequestDesktopButton()] |
| 295 performAction:grey_tap()]; |
| 296 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kDesktopSiteLabel)] |
| 297 assertWithMatcher:grey_notNil()]; |
| 298 |
| 299 // Request and verify reception of the mobile site. |
| 300 [ChromeEarlGreyUI openToolsMenu]; |
| 301 [[EarlGrey selectElementWithMatcher:RequestMobileButton()] |
| 302 performAction:grey_tap()]; |
| 303 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kMobileSiteLabel)] |
| 304 assertWithMatcher:grey_notNil()]; |
| 305 } |
| 306 |
| 307 @end |
| OLD | NEW |