| 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 // A ResponseProvider that provides user agent for httpServer request. |
| 51 class UserAgentResponseProvider : public web::DataResponseProvider { |
| 52 public: |
| 53 bool CanHandleRequest(const Request& request) override { return true; } |
| 54 |
| 55 void GetResponseHeadersAndBody( |
| 56 const Request& request, |
| 57 scoped_refptr<net::HttpResponseHeaders>* headers, |
| 58 std::string* response_body) override { |
| 59 // Do not return anything if static plist file has been requested, |
| 60 // as plain text is not a valid property list content. |
| 61 if ([[base::SysUTF8ToNSString(request.url.spec()) pathExtension] |
| 62 isEqualToString:@"plist"]) { |
| 63 *headers = |
| 64 web::ResponseProvider::GetResponseHeaders("", net::HTTP_NO_CONTENT); |
| 65 return; |
| 66 } |
| 67 |
| 68 *headers = web::ResponseProvider::GetDefaultResponseHeaders(); |
| 69 std::string userAgent; |
| 70 const std::string kDesktopUserAgent = |
| 71 base::SysNSStringToUTF8(ChromeWebView::kDesktopUserAgent); |
| 72 if (request.headers.GetHeader("User-Agent", &userAgent) && |
| 73 userAgent == kDesktopUserAgent) { |
| 74 response_body->assign("Desktop"); |
| 75 } else { |
| 76 response_body->assign("Mobile"); |
| 77 } |
| 78 } |
| 79 }; |
| 80 } // namespace |
| 81 |
| 82 // Tests for the tools popup menu. |
| 83 @interface RequestDesktopMobileSiteTestCase : ChromeTestCase |
| 84 @end |
| 85 |
| 86 @implementation RequestDesktopMobileSiteTestCase |
| 87 |
| 88 // Tests that requesting desktop site of a page works and the user agent |
| 89 // propagates to the next navigations in the same tab. |
| 90 - (void)testRequestDesktopSitePropagatesToNextNavigations { |
| 91 std::unique_ptr<web::DataResponseProvider> provider( |
| 92 new UserAgentResponseProvider()); |
| 93 web::test::SetUpHttpServer(std::move(provider)); |
| 94 |
| 95 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl("http://1.com")]; |
| 96 // Verify initial reception of the mobile site. |
| 97 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kMobileSiteLabel)] |
| 98 assertWithMatcher:grey_notNil()]; |
| 99 |
| 100 // Request and verify reception of the desktop site. |
| 101 [ChromeEarlGreyUI openToolsMenu]; |
| 102 [[EarlGrey selectElementWithMatcher:RequestDesktopButton()] |
| 103 performAction:grey_tap()]; |
| 104 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kDesktopSiteLabel)] |
| 105 assertWithMatcher:grey_notNil()]; |
| 106 |
| 107 // Verify that desktop user agent propagates. |
| 108 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl("http://2.com")]; |
| 109 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kDesktopSiteLabel)] |
| 110 assertWithMatcher:grey_notNil()]; |
| 111 } |
| 112 |
| 113 // Tests that requesting desktop site of a page works and desktop user agent |
| 114 // does not propagate to next the new tab. |
| 115 - (void)testRequestDesktopSiteDoesNotPropagateToNewTab { |
| 116 std::unique_ptr<web::DataResponseProvider> provider( |
| 117 new UserAgentResponseProvider()); |
| 118 web::test::SetUpHttpServer(std::move(provider)); |
| 119 |
| 120 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl("http://1.com")]; |
| 121 // Verify initial reception of the mobile site. |
| 122 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kMobileSiteLabel)] |
| 123 assertWithMatcher:grey_notNil()]; |
| 124 |
| 125 // Request and verify reception of the desktop site. |
| 126 [ChromeEarlGreyUI openToolsMenu]; |
| 127 [[EarlGrey selectElementWithMatcher:RequestDesktopButton()] |
| 128 performAction:grey_tap()]; |
| 129 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kDesktopSiteLabel)] |
| 130 assertWithMatcher:grey_notNil()]; |
| 131 |
| 132 // Verify that desktop user agent does not propagate to new tab. |
| 133 [ChromeEarlGreyUI openNewTab]; |
| 134 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl("http://2.com")]; |
| 135 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kMobileSiteLabel)] |
| 136 assertWithMatcher:grey_notNil()]; |
| 137 } |
| 138 |
| 139 // Tests that requesting desktop site of a page works and going back re-opens |
| 140 // mobile version of the page. |
| 141 - (void)testRequestDesktopSiteGoBackToMobile { |
| 142 std::unique_ptr<web::DataResponseProvider> provider( |
| 143 new UserAgentResponseProvider()); |
| 144 web::test::SetUpHttpServer(std::move(provider)); |
| 145 |
| 146 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl("http://1.com")]; |
| 147 // Verify initial reception of the mobile site. |
| 148 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kMobileSiteLabel)] |
| 149 assertWithMatcher:grey_notNil()]; |
| 150 |
| 151 // Request and verify reception of the desktop site. |
| 152 [ChromeEarlGreyUI openToolsMenu]; |
| 153 [[EarlGrey selectElementWithMatcher:RequestDesktopButton()] |
| 154 performAction:grey_tap()]; |
| 155 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kDesktopSiteLabel)] |
| 156 assertWithMatcher:grey_notNil()]; |
| 157 |
| 158 // Verify that going back returns to the mobile site. |
| 159 [[EarlGrey selectElementWithMatcher:chrome_test_util::BackButton()] |
| 160 performAction:grey_tap()]; |
| 161 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kMobileSiteLabel)] |
| 162 assertWithMatcher:grey_notNil()]; |
| 163 } |
| 164 |
| 165 // Tests that requesting mobile site of a page works and the user agent |
| 166 // propagates to the next navigations in the same tab. |
| 167 - (void)testRequestMobileSitePropagatesToNextNavigations { |
| 168 std::unique_ptr<web::DataResponseProvider> provider( |
| 169 new UserAgentResponseProvider()); |
| 170 web::test::SetUpHttpServer(std::move(provider)); |
| 171 |
| 172 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl("http://1.com")]; |
| 173 // Verify initial reception of the mobile site. |
| 174 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kMobileSiteLabel)] |
| 175 assertWithMatcher:grey_notNil()]; |
| 176 |
| 177 // Request and verify reception of the desktop site. |
| 178 [ChromeEarlGreyUI openToolsMenu]; |
| 179 [[EarlGrey selectElementWithMatcher:RequestDesktopButton()] |
| 180 performAction:grey_tap()]; |
| 181 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kDesktopSiteLabel)] |
| 182 assertWithMatcher:grey_notNil()]; |
| 183 |
| 184 // Request and verify reception of the mobile site. |
| 185 [ChromeEarlGreyUI openToolsMenu]; |
| 186 [[EarlGrey selectElementWithMatcher:RequestMobileButton()] |
| 187 performAction:grey_tap()]; |
| 188 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kMobileSiteLabel)] |
| 189 assertWithMatcher:grey_notNil()]; |
| 190 |
| 191 // Verify that mobile user agent propagates. |
| 192 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl("http://2.com")]; |
| 193 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kMobileSiteLabel)] |
| 194 assertWithMatcher:grey_notNil()]; |
| 195 } |
| 196 |
| 197 // Tests that requesting mobile site of a page works and going back re-opens |
| 198 // desktop version of the page. |
| 199 - (void)testRequestMobileSiteGoBackToDesktop { |
| 200 std::unique_ptr<web::DataResponseProvider> provider( |
| 201 new UserAgentResponseProvider()); |
| 202 web::test::SetUpHttpServer(std::move(provider)); |
| 203 |
| 204 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl("http://1.com")]; |
| 205 // Verify initial reception of the mobile site. |
| 206 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kMobileSiteLabel)] |
| 207 assertWithMatcher:grey_notNil()]; |
| 208 |
| 209 // Request and verify reception of the desktop site. |
| 210 [ChromeEarlGreyUI openToolsMenu]; |
| 211 [[EarlGrey selectElementWithMatcher:RequestDesktopButton()] |
| 212 performAction:grey_tap()]; |
| 213 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kDesktopSiteLabel)] |
| 214 assertWithMatcher:grey_notNil()]; |
| 215 |
| 216 // Request and verify reception of the mobile site. |
| 217 [ChromeEarlGreyUI openToolsMenu]; |
| 218 [[EarlGrey selectElementWithMatcher:RequestMobileButton()] |
| 219 performAction:grey_tap()]; |
| 220 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kMobileSiteLabel)] |
| 221 assertWithMatcher:grey_notNil()]; |
| 222 |
| 223 // Verify that going back returns to the desktop site. |
| 224 [[EarlGrey selectElementWithMatcher:chrome_test_util::BackButton()] |
| 225 performAction:grey_tap()]; |
| 226 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kDesktopSiteLabel)] |
| 227 assertWithMatcher:grey_notNil()]; |
| 228 } |
| 229 |
| 230 // Tests that requesting desktop site button is not enabled on new tab pages. |
| 231 - (void)testRequestDesktopSiteNotEnabledOnNewTabPage { |
| 232 // Verify tapping on request desktop button is no-op. |
| 233 [ChromeEarlGreyUI openToolsMenu]; |
| 234 [[[EarlGrey selectElementWithMatcher:RequestDesktopButton()] |
| 235 assertWithMatcher:grey_notNil()] performAction:grey_tap()]; |
| 236 [[EarlGrey selectElementWithMatcher:RequestDesktopButton()] |
| 237 assertWithMatcher:grey_notNil()]; |
| 238 } |
| 239 |
| 240 // Tests that requesting desktop site button is not enabled on WebUI pages. |
| 241 - (void)testRequestDesktopSiteNotEnabledOnWebUIPage { |
| 242 [ChromeEarlGrey loadURL:GURL("chrome://version")]; |
| 243 |
| 244 // Verify tapping on request desktop button is no-op. |
| 245 [ChromeEarlGreyUI openToolsMenu]; |
| 246 [[[EarlGrey selectElementWithMatcher:RequestDesktopButton()] |
| 247 assertWithMatcher:grey_notNil()] performAction:grey_tap()]; |
| 248 [[EarlGrey selectElementWithMatcher:RequestDesktopButton()] |
| 249 assertWithMatcher:grey_notNil()]; |
| 250 } |
| 251 |
| 252 // Tests that navigator.appVersion JavaScript API returns correct string for |
| 253 // desktop User Agent. |
| 254 - (void)testAppVersionJSAPIWithDesktopUserAgent { |
| 255 web::test::SetUpFileBasedHttpServer(); |
| 256 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kUserAgentTestURL)]; |
| 257 // Verify initial reception of the mobile site. |
| 258 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kMobileSiteLabel)] |
| 259 assertWithMatcher:grey_notNil()]; |
| 260 |
| 261 // Request and verify reception of the desktop site. |
| 262 [ChromeEarlGreyUI openToolsMenu]; |
| 263 [[EarlGrey selectElementWithMatcher:RequestDesktopButton()] |
| 264 performAction:grey_tap()]; |
| 265 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kDesktopSiteLabel)] |
| 266 assertWithMatcher:grey_notNil()]; |
| 267 } |
| 268 |
| 269 // Tests that navigator.appVersion JavaScript API returns correct string for |
| 270 // mobile User Agent. |
| 271 - (void)testAppVersionJSAPIWithMobileUserAgent { |
| 272 web::test::SetUpFileBasedHttpServer(); |
| 273 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kUserAgentTestURL)]; |
| 274 // Verify initial reception of the mobile site. |
| 275 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kMobileSiteLabel)] |
| 276 assertWithMatcher:grey_notNil()]; |
| 277 |
| 278 // Request and verify reception of the desktop site. |
| 279 [ChromeEarlGreyUI openToolsMenu]; |
| 280 [[EarlGrey selectElementWithMatcher:RequestDesktopButton()] |
| 281 performAction:grey_tap()]; |
| 282 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kDesktopSiteLabel)] |
| 283 assertWithMatcher:grey_notNil()]; |
| 284 |
| 285 // Request and verify reception of the mobile site. |
| 286 [ChromeEarlGreyUI openToolsMenu]; |
| 287 [[EarlGrey selectElementWithMatcher:RequestMobileButton()] |
| 288 performAction:grey_tap()]; |
| 289 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kMobileSiteLabel)] |
| 290 assertWithMatcher:grey_notNil()]; |
| 291 } |
| 292 |
| 293 @end |
| OLD | NEW |