| 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 <UIKit/UIKit.h> | 6 #import <UIKit/UIKit.h> |
| 7 #import <XCTest/XCTest.h> | 7 #import <XCTest/XCTest.h> |
| 8 #include <map> | 8 #include <map> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #import "base/mac/bind_objc_block.h" | 11 #import "base/mac/bind_objc_block.h" |
| 12 #include "base/mac/foundation_util.h" |
| 12 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 13 #include "base/strings/sys_string_conversions.h" | 14 #include "base/strings/sys_string_conversions.h" |
| 14 #include "components/browsing_data/core/pref_names.h" | 15 #include "components/browsing_data/core/pref_names.h" |
| 15 #include "components/metrics/metrics_pref_names.h" | 16 #include "components/metrics/metrics_pref_names.h" |
| 16 #include "components/password_manager/core/common/password_manager_pref_names.h" | 17 #include "components/password_manager/core/common/password_manager_pref_names.h" |
| 17 #include "components/prefs/pref_member.h" | 18 #include "components/prefs/pref_member.h" |
| 18 #include "components/prefs/pref_service.h" | 19 #include "components/prefs/pref_service.h" |
| 19 #include "components/strings/grit/components_strings.h" | 20 #include "components/strings/grit/components_strings.h" |
| 20 #import "ios/chrome/app/main_controller.h" | 21 #import "ios/chrome/app/main_controller.h" |
| 21 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" | 22 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 using chrome_test_util::NavigationBarDoneButton; | 55 using chrome_test_util::NavigationBarDoneButton; |
| 55 | 56 |
| 56 namespace { | 57 namespace { |
| 57 | 58 |
| 58 const char kTestOrigin1[] = "http://host1:1/"; | 59 const char kTestOrigin1[] = "http://host1:1/"; |
| 59 | 60 |
| 60 const char kUrl[] = "http://foo/browsing"; | 61 const char kUrl[] = "http://foo/browsing"; |
| 61 const char kUrlWithSetCookie[] = "http://foo/set_cookie"; | 62 const char kUrlWithSetCookie[] = "http://foo/set_cookie"; |
| 62 const char kResponse[] = "bar"; | 63 const char kResponse[] = "bar"; |
| 63 const char kResponseWithSetCookie[] = "bar with set cookie"; | 64 const char kResponseWithSetCookie[] = "bar with set cookie"; |
| 64 const char kNoCookieText[] = "No cookies"; | 65 NSString* const kCookieName = @"name"; |
| 65 const char kCookie[] = "a=b"; | 66 NSString* const kCookieValue = @"value"; |
| 66 const char kJavaScriptGetCookies[] = | |
| 67 "var c = document.cookie ? document.cookie.split(/;\\s*/) : [];" | |
| 68 "if (!c.length) {" | |
| 69 " document.documentElement.innerHTML = 'No cookies!';" | |
| 70 "} else {" | |
| 71 " document.documentElement.innerHTML = 'OK: ' + c.join(',');" | |
| 72 "}"; | |
| 73 | 67 |
| 74 enum MetricsServiceType { | 68 enum MetricsServiceType { |
| 75 kMetrics, | 69 kMetrics, |
| 76 kBreakpad, | 70 kBreakpad, |
| 77 kBreakpadFirstLaunch, | 71 kBreakpadFirstLaunch, |
| 78 }; | 72 }; |
| 79 | 73 |
| 80 // Matcher for the clear browsing history cell on the clear browsing data panel. | 74 // Matcher for the clear browsing history cell on the clear browsing data panel. |
| 81 id<GREYMatcher> ClearBrowsingHistoryButton() { | 75 id<GREYMatcher> ClearBrowsingHistoryButton() { |
| 82 return grey_allOf(grey_accessibilityID(kClearBrowsingHistoryCellId), | 76 return grey_allOf(grey_accessibilityID(kClearBrowsingHistoryCellId), |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 } | 151 } |
| 158 // Matcher for the Privacy Translate Settings button on the privacy UI. | 152 // Matcher for the Privacy Translate Settings button on the privacy UI. |
| 159 id<GREYMatcher> TranslateSettingsButton() { | 153 id<GREYMatcher> TranslateSettingsButton() { |
| 160 return ButtonWithAccessibilityLabelId(IDS_IOS_TRANSLATE_SETTING); | 154 return ButtonWithAccessibilityLabelId(IDS_IOS_TRANSLATE_SETTING); |
| 161 } | 155 } |
| 162 // Matcher for the save button in the save password bar. | 156 // Matcher for the save button in the save password bar. |
| 163 id<GREYMatcher> savePasswordButton() { | 157 id<GREYMatcher> savePasswordButton() { |
| 164 return ButtonWithAccessibilityLabelId(IDS_IOS_PASSWORD_MANAGER_SAVE_BUTTON); | 158 return ButtonWithAccessibilityLabelId(IDS_IOS_PASSWORD_MANAGER_SAVE_BUTTON); |
| 165 } | 159 } |
| 166 | 160 |
| 167 // Asserts that there is no cookie in current web state. | |
| 168 void AssertNoCookieExists() { | |
| 169 NSError* error = nil; | |
| 170 chrome_test_util::ExecuteJavaScript( | |
| 171 base::SysUTF8ToNSString(kJavaScriptGetCookies), &error); | |
| 172 [[EarlGrey selectElementWithMatcher:chrome_test_util::WebViewContainingText( | |
| 173 kNoCookieText)] | |
| 174 assertWithMatcher:grey_notNil()]; | |
| 175 } | |
| 176 | |
| 177 // Asserts |cookie| exists in current web state. | |
| 178 void AssertCookieExists(const char cookie[]) { | |
| 179 NSError* error = nil; | |
| 180 chrome_test_util::ExecuteJavaScript( | |
| 181 base::SysUTF8ToNSString(kJavaScriptGetCookies), &error); | |
| 182 NSString* const expectedCookieText = | |
| 183 [NSString stringWithFormat:@"OK: %@", base::SysUTF8ToNSString(cookie)]; | |
| 184 [[EarlGrey | |
| 185 selectElementWithMatcher:chrome_test_util::WebViewContainingText( | |
| 186 base::SysNSStringToUTF8(expectedCookieText))] | |
| 187 assertWithMatcher:grey_notNil()]; | |
| 188 } | |
| 189 | |
| 190 // Run as a task to check if a certificate has been added to the ChannelIDStore. | 161 // Run as a task to check if a certificate has been added to the ChannelIDStore. |
| 191 // Signals the given |semaphore| if the cert was added, or reposts itself | 162 // Signals the given |semaphore| if the cert was added, or reposts itself |
| 192 // otherwise. | 163 // otherwise. |
| 193 void CheckCertificate(scoped_refptr<net::URLRequestContextGetter> getter, | 164 void CheckCertificate(scoped_refptr<net::URLRequestContextGetter> getter, |
| 194 dispatch_semaphore_t semaphore) { | 165 dispatch_semaphore_t semaphore) { |
| 195 net::ChannelIDService* channel_id_service = | 166 net::ChannelIDService* channel_id_service = |
| 196 getter->GetURLRequestContext()->channel_id_service(); | 167 getter->GetURLRequestContext()->channel_id_service(); |
| 197 if (channel_id_service->channel_id_count() == 0) { | 168 if (channel_id_service->channel_id_count() == 0) { |
| 198 // If the channel_id_count is still 0, no certs have been added yet. | 169 // If the channel_id_count is still 0, no certs have been added yet. |
| 199 // Re-post this task and check again later. | 170 // Re-post this task and check again later. |
| (...skipping 468 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 668 #pragma mark Tests | 639 #pragma mark Tests |
| 669 | 640 |
| 670 // Tests that clearing the cookies through the UI does clear all of them. Use a | 641 // Tests that clearing the cookies through the UI does clear all of them. Use a |
| 671 // local server to navigate to a page that sets then tests a cookie, and then | 642 // local server to navigate to a page that sets then tests a cookie, and then |
| 672 // clears the cookie and tests it is not set. | 643 // clears the cookie and tests it is not set. |
| 673 // TODO(crbug.com/638674): Evaluate if this can move to shared code. | 644 // TODO(crbug.com/638674): Evaluate if this can move to shared code. |
| 674 - (void)testClearCookies { | 645 - (void)testClearCookies { |
| 675 // Creates a map of canned responses and set up the test HTML server. | 646 // Creates a map of canned responses and set up the test HTML server. |
| 676 std::map<GURL, std::pair<std::string, std::string>> response; | 647 std::map<GURL, std::pair<std::string, std::string>> response; |
| 677 | 648 |
| 649 NSString* cookieForURL = |
| 650 [NSString stringWithFormat:@"%@=%@", kCookieName, kCookieValue]; |
| 651 |
| 678 response[web::test::HttpServer::MakeUrl(kUrlWithSetCookie)] = | 652 response[web::test::HttpServer::MakeUrl(kUrlWithSetCookie)] = |
| 679 std::pair<std::string, std::string>(kCookie, kResponseWithSetCookie); | 653 std::pair<std::string, std::string>(base::SysNSStringToUTF8(cookieForURL), |
| 654 kResponseWithSetCookie); |
| 680 response[web::test::HttpServer::MakeUrl(kUrl)] = | 655 response[web::test::HttpServer::MakeUrl(kUrl)] = |
| 681 std::pair<std::string, std::string>("", kResponse); | 656 std::pair<std::string, std::string>("", kResponse); |
| 682 | 657 |
| 683 web::test::SetUpSimpleHttpServerWithSetCookies(response); | 658 web::test::SetUpSimpleHttpServerWithSetCookies(response); |
| 684 | 659 |
| 685 // Load |kUrl| and check that cookie is not set. | 660 // Load |kUrl| and check that cookie is not set. |
| 686 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kUrl)]; | 661 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kUrl)]; |
| 687 [[EarlGrey selectElementWithMatcher:chrome_test_util::WebViewContainingText( | 662 [[EarlGrey selectElementWithMatcher:chrome_test_util::WebViewContainingText( |
| 688 kResponse)] | 663 kResponse)] |
| 689 assertWithMatcher:grey_notNil()]; | 664 assertWithMatcher:grey_notNil()]; |
| 690 AssertNoCookieExists(); | 665 |
| 666 NSDictionary* cookies = [ChromeEarlGrey cookies]; |
| 667 GREYAssertEqual(0U, cookies.count, @"No cookie should be found."); |
| 691 | 668 |
| 692 // Visit |kUrlWithSetCookie| to set a cookie and then load |kUrl| to check it | 669 // Visit |kUrlWithSetCookie| to set a cookie and then load |kUrl| to check it |
| 693 // is still set. | 670 // is still set. |
| 694 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kUrlWithSetCookie)]; | 671 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kUrlWithSetCookie)]; |
| 695 [[EarlGrey selectElementWithMatcher:chrome_test_util::WebViewContainingText( | 672 [[EarlGrey selectElementWithMatcher:chrome_test_util::WebViewContainingText( |
| 696 kResponseWithSetCookie)] | 673 kResponseWithSetCookie)] |
| 697 assertWithMatcher:grey_notNil()]; | 674 assertWithMatcher:grey_notNil()]; |
| 698 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kUrl)]; | 675 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kUrl)]; |
| 699 [[EarlGrey selectElementWithMatcher:chrome_test_util::WebViewContainingText( | 676 [[EarlGrey selectElementWithMatcher:chrome_test_util::WebViewContainingText( |
| 700 kResponse)] | 677 kResponse)] |
| 701 assertWithMatcher:grey_notNil()]; | 678 assertWithMatcher:grey_notNil()]; |
| 702 | 679 |
| 703 AssertCookieExists(kCookie); | 680 cookies = [ChromeEarlGrey cookies]; |
| 681 GREYAssertEqualObjects(kCookieValue, cookies[kCookieName], |
| 682 @"Failed to set cookie."); |
| 683 GREYAssertEqual(1U, cookies.count, @"Only one cookie should be found."); |
| 704 | 684 |
| 705 // Restore the Clear Browsing Data checkmarks prefs to their default state in | 685 // Restore the Clear Browsing Data checkmarks prefs to their default state |
| 706 // Teardown. | 686 // in Teardown. |
| 707 [self setTearDownHandler:^{ | 687 [self setTearDownHandler:^{ |
| 708 [self restoreClearBrowsingDataCheckmarksToDefault]; | 688 [self restoreClearBrowsingDataCheckmarksToDefault]; |
| 709 }]; | 689 }]; |
| 710 | 690 |
| 711 // Clear all cookies. | 691 // Clear all cookies. |
| 712 [self clearCookiesAndSiteData]; | 692 [self clearCookiesAndSiteData]; |
| 713 | 693 |
| 714 // Reload and test that there are no cookies left. | 694 // Reload and test that there are no cookies left. |
| 715 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kUrl)]; | 695 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kUrl)]; |
| 716 [[EarlGrey selectElementWithMatcher:chrome_test_util::WebViewContainingText( | 696 [[EarlGrey selectElementWithMatcher:chrome_test_util::WebViewContainingText( |
| 717 kResponse)] | 697 kResponse)] |
| 718 assertWithMatcher:grey_notNil()]; | 698 assertWithMatcher:grey_notNil()]; |
| 719 AssertNoCookieExists(); | 699 |
| 700 cookies = [ChromeEarlGrey cookies]; |
| 701 GREYAssertEqual(0U, cookies.count, @"No cookie should be found."); |
| 702 |
| 720 chrome_test_util::CloseAllTabs(); | 703 chrome_test_util::CloseAllTabs(); |
| 721 } | 704 } |
| 722 | 705 |
| 723 // Verifies that logging into a form on a web page allows the user to save and | 706 // Verifies that logging into a form on a web page allows the user to save and |
| 724 // then clear a password. | 707 // then clear a password. |
| 725 - (void)testClearPasswords { | 708 - (void)testClearPasswords { |
| 726 | 709 |
| 727 ios::ChromeBrowserState* browserState = | 710 ios::ChromeBrowserState* browserState = |
| 728 chrome_test_util::GetOriginalBrowserState(); | 711 chrome_test_util::GetOriginalBrowserState(); |
| 729 PrefService* preferences = browserState->GetPrefs(); | 712 PrefService* preferences = browserState->GetPrefs(); |
| (...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1055 performAction:grey_tap()]; | 1038 performAction:grey_tap()]; |
| 1056 [[EarlGrey selectElementWithMatcher:PrivacyButton()] | 1039 [[EarlGrey selectElementWithMatcher:PrivacyButton()] |
| 1057 performAction:grey_tap()]; | 1040 performAction:grey_tap()]; |
| 1058 [[EarlGrey selectElementWithMatcher:SendUsageDataButton()] | 1041 [[EarlGrey selectElementWithMatcher:SendUsageDataButton()] |
| 1059 performAction:grey_tap()]; | 1042 performAction:grey_tap()]; |
| 1060 chrome_test_util::VerifyAccessibilityForCurrentScreen(); | 1043 chrome_test_util::VerifyAccessibilityForCurrentScreen(); |
| 1061 [self closeSubSettingsMenu]; | 1044 [self closeSubSettingsMenu]; |
| 1062 } | 1045 } |
| 1063 | 1046 |
| 1064 @end | 1047 @end |
| OLD | NEW |