| OLD | NEW |
| 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 <XCTest/XCTest.h> | 6 #import <XCTest/XCTest.h> |
| 7 | 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" | 8 #import "ios/chrome/browser/ui/toolbar/toolbar_controller.h" |
| 12 #include "ios/chrome/browser/ui/tools_menu/tools_menu_constants.h" | 9 #include "ios/chrome/browser/ui/tools_menu/tools_menu_constants.h" |
| 13 #import "ios/chrome/browser/ui/uikit_ui_util.h" | 10 #import "ios/chrome/browser/ui/uikit_ui_util.h" |
| 14 #include "ios/chrome/grit/ios_strings.h" | 11 #include "ios/chrome/grit/ios_strings.h" |
| 15 #import "ios/chrome/test/earl_grey/accessibility_util.h" | 12 #import "ios/chrome/test/earl_grey/accessibility_util.h" |
| 16 #import "ios/chrome/test/earl_grey/chrome_earl_grey.h" | 13 #import "ios/chrome/test/earl_grey/chrome_earl_grey.h" |
| 17 #import "ios/chrome/test/earl_grey/chrome_earl_grey_ui.h" | 14 #import "ios/chrome/test/earl_grey/chrome_earl_grey_ui.h" |
| 18 #import "ios/chrome/test/earl_grey/chrome_matchers.h" | 15 #import "ios/chrome/test/earl_grey/chrome_matchers.h" |
| 19 #import "ios/chrome/test/earl_grey/chrome_test_case.h" | 16 #import "ios/chrome/test/earl_grey/chrome_test_case.h" |
| 20 #import "ios/web/public/test/http_server.h" | 17 #import "ios/web/public/test/http_server.h" |
| 21 #include "ios/web/public/test/http_server_util.h" | 18 #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" | 19 #include "ui/base/l10n/l10n_util.h" |
| 24 | 20 |
| 25 #if !defined(__has_feature) || !__has_feature(objc_arc) | 21 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 26 #error "This file requires ARC support." | 22 #error "This file requires ARC support." |
| 27 #endif | 23 #endif |
| 28 | 24 |
| 29 namespace { | 25 namespace { |
| 30 | 26 |
| 31 // A ResponseProvider that provides user agent for httpServer request. | 27 const char kPDFURL[] = "http://ios/testing/data/http_server_files/testpage.pdf"; |
| 32 class UserAgentResponseProvider : public web::DataResponseProvider { | |
| 33 public: | |
| 34 bool CanHandleRequest(const Request& request) override { return true; } | |
| 35 | |
| 36 void GetResponseHeadersAndBody( | |
| 37 const Request& request, | |
| 38 scoped_refptr<net::HttpResponseHeaders>* headers, | |
| 39 std::string* response_body) override { | |
| 40 // Do not return anything if static plist file has been requested, | |
| 41 // as plain text is not a valid property list content. | |
| 42 if ([[base::SysUTF8ToNSString(request.url.spec()) pathExtension] | |
| 43 isEqualToString:@"plist"]) { | |
| 44 *headers = | |
| 45 web::ResponseProvider::GetResponseHeaders("", net::HTTP_NO_CONTENT); | |
| 46 return; | |
| 47 } | |
| 48 | |
| 49 *headers = web::ResponseProvider::GetDefaultResponseHeaders(); | |
| 50 std::string userAgent; | |
| 51 const std::string kDesktopUserAgent = | |
| 52 base::SysNSStringToUTF8(ChromeWebView::kDesktopUserAgent); | |
| 53 if (request.headers.GetHeader("User-Agent", &userAgent) && | |
| 54 userAgent == kDesktopUserAgent) { | |
| 55 response_body->assign("Desktop"); | |
| 56 } else { | |
| 57 response_body->assign("Mobile"); | |
| 58 } | |
| 59 } | |
| 60 }; | |
| 61 | 28 |
| 62 // Matcher for the button to find in page. | 29 // Matcher for the button to find in page. |
| 63 id<GREYMatcher> FindInPageButton() { | 30 id<GREYMatcher> FindInPageButton() { |
| 64 return chrome_test_util::ButtonWithAccessibilityLabel( | 31 return chrome_test_util::ButtonWithAccessibilityLabel( |
| 65 l10n_util::GetNSStringWithFixup(IDS_IOS_TOOLS_MENU_FIND_IN_PAGE)); | 32 l10n_util::GetNSStringWithFixup(IDS_IOS_TOOLS_MENU_FIND_IN_PAGE)); |
| 66 } | 33 } |
| 67 | |
| 68 // Matcher for the button to request desktop version. | |
| 69 id<GREYMatcher> RequestDesktopButton() { | |
| 70 return grey_accessibilityID(kToolsMenuRequestDesktopId); | |
| 71 } | |
| 72 | |
| 73 const char kPDFURL[] = "http://ios/testing/data/http_server_files/testpage.pdf"; | |
| 74 | |
| 75 } // namespace | 34 } // namespace |
| 76 | 35 |
| 77 // Tests for the tools popup menu. | 36 // Tests for the tools popup menu. |
| 78 @interface ToolsPopupMenuTestCase : ChromeTestCase | 37 @interface ToolsPopupMenuTestCase : ChromeTestCase |
| 79 - (void)verifyMobileAndDesktopVersions:(const GURL&)url; | |
| 80 @end | 38 @end |
| 81 | 39 |
| 82 @implementation ToolsPopupMenuTestCase | 40 @implementation ToolsPopupMenuTestCase |
| 83 | 41 |
| 84 // Verify that requesting desktop and mobile versions works. | |
| 85 - (void)verifyMobileAndDesktopVersions:(const GURL&)url { | |
| 86 NSString* const kMobileSiteLabel = @"Mobile"; | |
| 87 NSString* const kDesktopSiteLabel = @"Desktop"; | |
| 88 | |
| 89 [ChromeEarlGrey loadURL:url]; | |
| 90 | |
| 91 // Verify initial reception of the mobile site. | |
| 92 [[EarlGrey | |
| 93 selectElementWithMatcher:chrome_test_util::WebViewContainingText( | |
| 94 base::SysNSStringToUTF8(kMobileSiteLabel))] | |
| 95 assertWithMatcher:grey_notNil()]; | |
| 96 | |
| 97 // Request and verify reception of the desktop site. | |
| 98 [ChromeEarlGreyUI openToolsMenu]; | |
| 99 [[EarlGrey selectElementWithMatcher:RequestDesktopButton()] | |
| 100 performAction:grey_tap()]; | |
| 101 [[EarlGrey | |
| 102 selectElementWithMatcher:chrome_test_util::WebViewContainingText( | |
| 103 base::SysNSStringToUTF8(kDesktopSiteLabel))] | |
| 104 assertWithMatcher:grey_notNil()]; | |
| 105 | |
| 106 // Verify that going back returns to the mobile site. | |
| 107 [[EarlGrey selectElementWithMatcher:chrome_test_util::BackButton()] | |
| 108 performAction:grey_tap()]; | |
| 109 [[EarlGrey | |
| 110 selectElementWithMatcher:chrome_test_util::WebViewContainingText( | |
| 111 base::SysNSStringToUTF8(kMobileSiteLabel))] | |
| 112 assertWithMatcher:grey_notNil()]; | |
| 113 } | |
| 114 | |
| 115 // Tests that the menu is closed when tapping the close button. | 42 // Tests that the menu is closed when tapping the close button. |
| 116 - (void)testOpenAndCloseToolsMenu { | 43 - (void)testOpenAndCloseToolsMenu { |
| 117 [ChromeEarlGreyUI openToolsMenu]; | 44 [ChromeEarlGreyUI openToolsMenu]; |
| 118 | 45 |
| 119 if (!IsCompact()) { | 46 if (!IsCompact()) { |
| 120 [[EarlGrey | 47 [[EarlGrey |
| 121 selectElementWithMatcher:grey_accessibilityLabel(l10n_util::GetNSString( | 48 selectElementWithMatcher:grey_accessibilityLabel(l10n_util::GetNSString( |
| 122 IDS_IOS_TOOLBAR_CLOSE_MENU))] | 49 IDS_IOS_TOOLBAR_CLOSE_MENU))] |
| 123 performAction:grey_tap()]; | 50 performAction:grey_tap()]; |
| 124 } else { | 51 } else { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 138 const GURL URL = web::test::HttpServer::MakeUrl(kPDFURL); | 65 const GURL URL = web::test::HttpServer::MakeUrl(kPDFURL); |
| 139 | 66 |
| 140 // Navigate to a mock pdf and verify that the find button is disabled. | 67 // Navigate to a mock pdf and verify that the find button is disabled. |
| 141 [ChromeEarlGrey loadURL:URL]; | 68 [ChromeEarlGrey loadURL:URL]; |
| 142 [ChromeEarlGreyUI openToolsMenu]; | 69 [ChromeEarlGreyUI openToolsMenu]; |
| 143 [[EarlGrey selectElementWithMatcher:FindInPageButton()] | 70 [[EarlGrey selectElementWithMatcher:FindInPageButton()] |
| 144 assertWithMatcher:grey_accessibilityTrait( | 71 assertWithMatcher:grey_accessibilityTrait( |
| 145 UIAccessibilityTraitNotEnabled)]; | 72 UIAccessibilityTraitNotEnabled)]; |
| 146 } | 73 } |
| 147 | 74 |
| 148 // Test requesting desktop version of page works and going back re-opens mobile | |
| 149 // version of page. | |
| 150 - (void)testToolsMenuRequestDesktopNetwork { | |
| 151 std::unique_ptr<web::DataResponseProvider> provider( | |
| 152 new UserAgentResponseProvider()); | |
| 153 web::test::SetUpHttpServer(std::move(provider)); | |
| 154 | |
| 155 const GURL networkLayerTestURL = | |
| 156 web::test::HttpServer::MakeUrl("http://network"); | |
| 157 [self verifyMobileAndDesktopVersions:networkLayerTestURL]; | |
| 158 } | |
| 159 | |
| 160 // Test requesting the desktop version of a page works correctly for | |
| 161 // script-based desktop/mobile differentation. | |
| 162 - (void)testToolsMenuRequestDesktopScript { | |
| 163 web::test::SetUpFileBasedHttpServer(); | |
| 164 const GURL scriptLayerTestURL = web::test::HttpServer::MakeUrl( | |
| 165 "http://ios/testing/data/http_server_files/" | |
| 166 "request_desktop_test_page.html"); | |
| 167 [self verifyMobileAndDesktopVersions:scriptLayerTestURL]; | |
| 168 } | |
| 169 | |
| 170 // Open tools menu and verify elements are accessible. | 75 // Open tools menu and verify elements are accessible. |
| 171 - (void)testAccessibilityOnToolsMenu { | 76 - (void)testAccessibilityOnToolsMenu { |
| 172 [ChromeEarlGreyUI openToolsMenu]; | 77 [ChromeEarlGreyUI openToolsMenu]; |
| 173 chrome_test_util::VerifyAccessibilityForCurrentScreen(); | 78 chrome_test_util::VerifyAccessibilityForCurrentScreen(); |
| 174 // Close Tools menu. | 79 // Close Tools menu. |
| 175 [ChromeTestCase removeAnyOpenMenusAndInfoBars]; | 80 [ChromeTestCase removeAnyOpenMenusAndInfoBars]; |
| 176 } | 81 } |
| 177 | 82 |
| 178 @end | 83 @end |
| OLD | NEW |