Chromium Code Reviews| 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 const char kNoCookieText[] = "No cookies!"; |
| 65 const char kCookie[] = "a=b"; | 66 const char kCookie[] = "a=b"; |
| 66 const char kJavaScriptGetCookies[] = | 67 const char kJavaScriptGetCookies[] = |
| 67 "var c = document.cookie ? document.cookie.split(/;\\s*/) : [];" | 68 "var c = document.cookie ? document.cookie.split(/;\\s*/) : [];" |
| 68 "if (!c.length) {" | 69 "if (!c.length) {" |
| 69 " document.documentElement.innerHTML = 'No cookies!';" | 70 " document.documentElement.innerHTML = 'No cookies!';" |
| 70 "} else {" | 71 "} else {" |
| 71 " document.documentElement.innerHTML = 'OK: ' + c.join(',');" | 72 " document.documentElement.innerHTML = 'OK: ' + c.join(',');" |
| 72 "}"; | 73 "}"; |
| 73 | 74 |
| 74 enum MetricsServiceType { | 75 enum MetricsServiceType { |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 157 } | 158 } |
| 158 // Matcher for the Privacy Translate Settings button on the privacy UI. | 159 // Matcher for the Privacy Translate Settings button on the privacy UI. |
| 159 id<GREYMatcher> TranslateSettingsButton() { | 160 id<GREYMatcher> TranslateSettingsButton() { |
| 160 return ButtonWithAccessibilityLabelId(IDS_IOS_TRANSLATE_SETTING); | 161 return ButtonWithAccessibilityLabelId(IDS_IOS_TRANSLATE_SETTING); |
| 161 } | 162 } |
| 162 // Matcher for the save button in the save password bar. | 163 // Matcher for the save button in the save password bar. |
| 163 id<GREYMatcher> savePasswordButton() { | 164 id<GREYMatcher> savePasswordButton() { |
| 164 return ButtonWithAccessibilityLabelId(IDS_IOS_PASSWORD_MANAGER_SAVE_BUTTON); | 165 return ButtonWithAccessibilityLabelId(IDS_IOS_PASSWORD_MANAGER_SAVE_BUTTON); |
| 165 } | 166 } |
| 166 | 167 |
| 167 // Asserts that there is no cookie in current web state. | 168 // Returns true if there is no cookie in current web state. |
|
baxley
2017/03/10 21:58:17
nit: s/current/the current
liaoyuke
2017/03/13 16:12:50
Done.
| |
| 168 void AssertNoCookieExists() { | 169 bool IsCookieEmpty() { |
|
baxley
2017/03/10 21:58:17
Would IsCookiePresent() be more clear? We're not c
liaoyuke
2017/03/13 16:12:50
Acknowledged.
| |
| 169 NSError* error = nil; | 170 NSError* error; |
| 170 chrome_test_util::ExecuteJavaScript( | 171 id result = chrome_test_util::ExecuteJavaScript( |
| 171 base::SysUTF8ToNSString(kJavaScriptGetCookies), &error); | 172 base::SysUTF8ToNSString(kJavaScriptGetCookies), &error); |
| 172 [[EarlGrey selectElementWithMatcher:chrome_test_util::WebViewContainingText( | 173 |
| 173 kNoCookieText)] | 174 if (error) |
| 174 assertWithMatcher:grey_notNil()]; | 175 return false; |
| 176 | |
| 177 NSString* const resultCookie = base::mac::ObjCCastStrict<NSString>(result); | |
| 178 NSString* const expectedCookie = base::SysUTF8ToNSString(kNoCookieText); | |
| 179 | |
| 180 return [resultCookie isEqualToString:expectedCookie]; | |
| 175 } | 181 } |
| 176 | 182 |
| 177 // Asserts |cookie| exists in current web state. | 183 // Returns true if |cookie| is the only cookie exists in current web state. |
|
baxley
2017/03/10 21:58:17
Returns true if |cookie| is the only cookie that e
liaoyuke
2017/03/13 16:12:50
Acknowledged.
| |
| 178 void AssertCookieExists(const char cookie[]) { | 184 bool IsCookieEquals(const char cookie[]) { |
|
baxley
2017/03/10 21:58:17
Sorry, I don't have great suggestions, but IsCooki
liaoyuke
2017/03/13 16:12:50
Acknowledged.
| |
| 179 NSError* error = nil; | 185 NSError* error; |
| 180 chrome_test_util::ExecuteJavaScript( | 186 id result = chrome_test_util::ExecuteJavaScript( |
| 181 base::SysUTF8ToNSString(kJavaScriptGetCookies), &error); | 187 base::SysUTF8ToNSString(kJavaScriptGetCookies), &error); |
| 182 NSString* const expectedCookieText = | 188 |
| 189 if (error) | |
| 190 return false; | |
| 191 | |
| 192 NSString* const resultCookie = base::mac::ObjCCastStrict<NSString>(result); | |
| 193 NSString* const expectedCookie = | |
| 183 [NSString stringWithFormat:@"OK: %@", base::SysUTF8ToNSString(cookie)]; | 194 [NSString stringWithFormat:@"OK: %@", base::SysUTF8ToNSString(cookie)]; |
| 184 [[EarlGrey | 195 |
| 185 selectElementWithMatcher:chrome_test_util::WebViewContainingText( | 196 return [resultCookie isEqualToString:expectedCookie]; |
|
baxley
2017/03/10 21:58:17
Do you see any value in having a helper method to
liaoyuke
2017/03/13 16:12:50
Done.
| |
| 186 base::SysNSStringToUTF8(expectedCookieText))] | |
| 187 assertWithMatcher:grey_notNil()]; | |
| 188 } | 197 } |
| 189 | 198 |
| 190 // Run as a task to check if a certificate has been added to the ChannelIDStore. | 199 // 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 | 200 // Signals the given |semaphore| if the cert was added, or reposts itself |
| 192 // otherwise. | 201 // otherwise. |
| 193 void CheckCertificate(scoped_refptr<net::URLRequestContextGetter> getter, | 202 void CheckCertificate(scoped_refptr<net::URLRequestContextGetter> getter, |
| 194 dispatch_semaphore_t semaphore) { | 203 dispatch_semaphore_t semaphore) { |
| 195 net::ChannelIDService* channel_id_service = | 204 net::ChannelIDService* channel_id_service = |
| 196 getter->GetURLRequestContext()->channel_id_service(); | 205 getter->GetURLRequestContext()->channel_id_service(); |
| 197 if (channel_id_service->channel_id_count() == 0) { | 206 if (channel_id_service->channel_id_count() == 0) { |
| (...skipping 482 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 680 response[web::test::HttpServer::MakeUrl(kUrl)] = | 689 response[web::test::HttpServer::MakeUrl(kUrl)] = |
| 681 std::pair<std::string, std::string>("", kResponse); | 690 std::pair<std::string, std::string>("", kResponse); |
| 682 | 691 |
| 683 web::test::SetUpSimpleHttpServerWithSetCookies(response); | 692 web::test::SetUpSimpleHttpServerWithSetCookies(response); |
| 684 | 693 |
| 685 // Load |kUrl| and check that cookie is not set. | 694 // Load |kUrl| and check that cookie is not set. |
| 686 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kUrl)]; | 695 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kUrl)]; |
| 687 [[EarlGrey selectElementWithMatcher:chrome_test_util::WebViewContainingText( | 696 [[EarlGrey selectElementWithMatcher:chrome_test_util::WebViewContainingText( |
| 688 kResponse)] | 697 kResponse)] |
| 689 assertWithMatcher:grey_notNil()]; | 698 assertWithMatcher:grey_notNil()]; |
| 690 AssertNoCookieExists(); | 699 |
| 700 GREYAssertTrue(IsCookieEmpty(), @"Web state is expected to have no cookie."); | |
| 691 | 701 |
| 692 // Visit |kUrlWithSetCookie| to set a cookie and then load |kUrl| to check it | 702 // Visit |kUrlWithSetCookie| to set a cookie and then load |kUrl| to check it |
| 693 // is still set. | 703 // is still set. |
| 694 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kUrlWithSetCookie)]; | 704 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kUrlWithSetCookie)]; |
| 695 [[EarlGrey selectElementWithMatcher:chrome_test_util::WebViewContainingText( | 705 [[EarlGrey selectElementWithMatcher:chrome_test_util::WebViewContainingText( |
| 696 kResponseWithSetCookie)] | 706 kResponseWithSetCookie)] |
| 697 assertWithMatcher:grey_notNil()]; | 707 assertWithMatcher:grey_notNil()]; |
| 698 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kUrl)]; | 708 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kUrl)]; |
| 699 [[EarlGrey selectElementWithMatcher:chrome_test_util::WebViewContainingText( | 709 [[EarlGrey selectElementWithMatcher:chrome_test_util::WebViewContainingText( |
| 700 kResponse)] | 710 kResponse)] |
| 701 assertWithMatcher:grey_notNil()]; | 711 assertWithMatcher:grey_notNil()]; |
| 702 | 712 |
| 703 AssertCookieExists(kCookie); | 713 NSString* const errorMessage = [NSString |
| 714 stringWithFormat:@"Web state is expected to have following cookie: %@.", | |
| 715 base::SysUTF8ToNSString(kCookie)]; | |
| 716 GREYAssertTrue(IsCookieEquals(kCookie), errorMessage); | |
| 704 | 717 |
| 705 // Restore the Clear Browsing Data checkmarks prefs to their default state in | 718 // Restore the Clear Browsing Data checkmarks prefs to their default state in |
| 706 // Teardown. | 719 // Teardown. |
| 707 [self setTearDownHandler:^{ | 720 [self setTearDownHandler:^{ |
| 708 [self restoreClearBrowsingDataCheckmarksToDefault]; | 721 [self restoreClearBrowsingDataCheckmarksToDefault]; |
| 709 }]; | 722 }]; |
| 710 | 723 |
| 711 // Clear all cookies. | 724 // Clear all cookies. |
| 712 [self clearCookiesAndSiteData]; | 725 [self clearCookiesAndSiteData]; |
| 713 | 726 |
| 714 // Reload and test that there are no cookies left. | 727 // Reload and test that there are no cookies left. |
| 715 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kUrl)]; | 728 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kUrl)]; |
| 716 [[EarlGrey selectElementWithMatcher:chrome_test_util::WebViewContainingText( | 729 [[EarlGrey selectElementWithMatcher:chrome_test_util::WebViewContainingText( |
| 717 kResponse)] | 730 kResponse)] |
| 718 assertWithMatcher:grey_notNil()]; | 731 assertWithMatcher:grey_notNil()]; |
| 719 AssertNoCookieExists(); | 732 GREYAssertTrue(IsCookieEmpty(), @"Web state is expected to have no cookie."); |
| 720 chrome_test_util::CloseAllTabs(); | 733 chrome_test_util::CloseAllTabs(); |
| 721 } | 734 } |
| 722 | 735 |
| 723 // Verifies that logging into a form on a web page allows the user to save and | 736 // Verifies that logging into a form on a web page allows the user to save and |
| 724 // then clear a password. | 737 // then clear a password. |
| 725 - (void)testClearPasswords { | 738 - (void)testClearPasswords { |
| 726 | 739 |
| 727 ios::ChromeBrowserState* browserState = | 740 ios::ChromeBrowserState* browserState = |
| 728 chrome_test_util::GetOriginalBrowserState(); | 741 chrome_test_util::GetOriginalBrowserState(); |
| 729 PrefService* preferences = browserState->GetPrefs(); | 742 PrefService* preferences = browserState->GetPrefs(); |
| (...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1055 performAction:grey_tap()]; | 1068 performAction:grey_tap()]; |
| 1056 [[EarlGrey selectElementWithMatcher:PrivacyButton()] | 1069 [[EarlGrey selectElementWithMatcher:PrivacyButton()] |
| 1057 performAction:grey_tap()]; | 1070 performAction:grey_tap()]; |
| 1058 [[EarlGrey selectElementWithMatcher:SendUsageDataButton()] | 1071 [[EarlGrey selectElementWithMatcher:SendUsageDataButton()] |
| 1059 performAction:grey_tap()]; | 1072 performAction:grey_tap()]; |
| 1060 chrome_test_util::VerifyAccessibilityForCurrentScreen(); | 1073 chrome_test_util::VerifyAccessibilityForCurrentScreen(); |
| 1061 [self closeSubSettingsMenu]; | 1074 [self closeSubSettingsMenu]; |
| 1062 } | 1075 } |
| 1063 | 1076 |
| 1064 @end | 1077 @end |
| OLD | NEW |