Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(224)

Side by Side Diff: ios/chrome/browser/ui/settings/settings_egtest.mm

Issue 2734963004: Refactor Assert[No]CookieExists to return a boolean (Closed)
Patch Set: Move cookie helpers to a shared location Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 kCookieKey = @"a";
65 const char kCookie[] = "a=b"; 66 NSString* const kCookieValue = @"b";
baxley 2017/03/13 21:29:04 nit: would this be better for debugging if it had
liaoyuke 2017/03/13 22:50:40 Done.
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
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 458 matching lines...) Expand 10 before | Expand all | Expand 10 after
658 // kMetricsReportingEnabled ON and kMetricsReportingWifiOnly OFF 629 // kMetricsReportingEnabled ON and kMetricsReportingWifiOnly OFF
659 chrome_test_util::SetBooleanLocalStatePref( 630 chrome_test_util::SetBooleanLocalStatePref(
660 metrics::prefs::kMetricsReportingEnabled, YES); 631 metrics::prefs::kMetricsReportingEnabled, YES);
661 chrome_test_util::SetBooleanLocalStatePref(prefs::kMetricsReportingWifiOnly, 632 chrome_test_util::SetBooleanLocalStatePref(prefs::kMetricsReportingWifiOnly,
662 NO); 633 NO);
663 // Service should remain disabled. 634 // Service should remain disabled.
664 [self assertMetricsServiceDisabled:serviceType]; 635 [self assertMetricsServiceDisabled:serviceType];
665 #endif 636 #endif
666 } 637 }
667 638
639 // Creates a map of canned responses and set up the test HTML server, which
640 // always sets cookie in response header.
641 - (void)setUpSimpleHttpServerWithSetCookies {
baxley 2017/03/13 21:29:04 Does only one method use this? Do you think it mak
liaoyuke 2017/03/13 22:50:40 Makes sense. But ideally, I think we should separa
642 // Creates a map of canned responses and set up the test HTML server.
643 std::map<GURL, std::pair<std::string, std::string>> response;
644
645 NSString* cookie =
646 [NSString stringWithFormat:@"%@=%@", kCookieKey, kCookieValue];
647
648 response[web::test::HttpServer::MakeUrl(kUrlWithSetCookie)] =
649 std::pair<std::string, std::string>(base::SysNSStringToUTF8(cookie),
650 kResponseWithSetCookie);
651 response[web::test::HttpServer::MakeUrl(kUrl)] =
652 std::pair<std::string, std::string>("", kResponse);
653
654 web::test::SetUpSimpleHttpServerWithSetCookies(response);
655 }
656
668 #pragma mark Tests 657 #pragma mark Tests
669 658
670 // Tests that clearing the cookies through the UI does clear all of them. Use a 659 // 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 660 // 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. 661 // clears the cookie and tests it is not set.
673 // TODO(crbug.com/638674): Evaluate if this can move to shared code. 662 // TODO(crbug.com/638674): Evaluate if this can move to shared code.
674 - (void)testClearCookies { 663 - (void)testClearCookies {
675 // Creates a map of canned responses and set up the test HTML server. 664 [self setUpSimpleHttpServerWithSetCookies];
676 std::map<GURL, std::pair<std::string, std::string>> response;
677
678 response[web::test::HttpServer::MakeUrl(kUrlWithSetCookie)] =
679 std::pair<std::string, std::string>(kCookie, kResponseWithSetCookie);
680 response[web::test::HttpServer::MakeUrl(kUrl)] =
681 std::pair<std::string, std::string>("", kResponse);
682
683 web::test::SetUpSimpleHttpServerWithSetCookies(response);
684 665
685 // Load |kUrl| and check that cookie is not set. 666 // Load |kUrl| and check that cookie is not set.
686 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kUrl)]; 667 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kUrl)];
687 [[EarlGrey selectElementWithMatcher:chrome_test_util::WebViewContainingText( 668 [[EarlGrey selectElementWithMatcher:chrome_test_util::WebViewContainingText(
688 kResponse)] 669 kResponse)]
689 assertWithMatcher:grey_notNil()]; 670 assertWithMatcher:grey_notNil()];
690 AssertNoCookieExists(); 671
672 NSDictionary* cookies = [ChromeEarlGrey getCookies];
673 GREYAssertTrue(cookies, @"Failed to get cookies.");
674 GREYAssertEqual(0U, cookies.count, @"No cookie should be found.");
baxley 2017/03/13 21:29:04 The two lines above are a little confusing to me.
liaoyuke 2017/03/13 22:50:40 Acknowledged.
691 675
692 // Visit |kUrlWithSetCookie| to set a cookie and then load |kUrl| to check it 676 // Visit |kUrlWithSetCookie| to set a cookie and then load |kUrl| to check it
693 // is still set. 677 // is still set.
694 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kUrlWithSetCookie)]; 678 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kUrlWithSetCookie)];
695 [[EarlGrey selectElementWithMatcher:chrome_test_util::WebViewContainingText( 679 [[EarlGrey selectElementWithMatcher:chrome_test_util::WebViewContainingText(
696 kResponseWithSetCookie)] 680 kResponseWithSetCookie)]
697 assertWithMatcher:grey_notNil()]; 681 assertWithMatcher:grey_notNil()];
698 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kUrl)]; 682 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kUrl)];
699 [[EarlGrey selectElementWithMatcher:chrome_test_util::WebViewContainingText( 683 [[EarlGrey selectElementWithMatcher:chrome_test_util::WebViewContainingText(
700 kResponse)] 684 kResponse)]
701 assertWithMatcher:grey_notNil()]; 685 assertWithMatcher:grey_notNil()];
702 686
703 AssertCookieExists(kCookie); 687 cookies = [ChromeEarlGrey getCookies];
688 GREYAssertTrue(cookies, @"Failed to get cookies.");
689 GREYAssertTrue([kCookieValue isEqualToString:cookies[kCookieKey]],
690 @"Failed to set cookie.");
691 GREYAssertEqual(1U, cookies.count, @"Only one cookie should be found.");
704 692
705 // Restore the Clear Browsing Data checkmarks prefs to their default state in 693 // Restore the Clear Browsing Data checkmarks prefs to their default state
706 // Teardown. 694 // in Teardown.
707 [self setTearDownHandler:^{ 695 [self setTearDownHandler:^{
708 [self restoreClearBrowsingDataCheckmarksToDefault]; 696 [self restoreClearBrowsingDataCheckmarksToDefault];
709 }]; 697 }];
710 698
711 // Clear all cookies. 699 // Clear all cookies.
712 [self clearCookiesAndSiteData]; 700 [self clearCookiesAndSiteData];
713 701
714 // Reload and test that there are no cookies left. 702 // Reload and test that there are no cookies left.
715 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kUrl)]; 703 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kUrl)];
716 [[EarlGrey selectElementWithMatcher:chrome_test_util::WebViewContainingText( 704 [[EarlGrey selectElementWithMatcher:chrome_test_util::WebViewContainingText(
717 kResponse)] 705 kResponse)]
718 assertWithMatcher:grey_notNil()]; 706 assertWithMatcher:grey_notNil()];
719 AssertNoCookieExists(); 707
708 cookies = [ChromeEarlGrey getCookies];
709 GREYAssertTrue(cookies, @"Failed to get cookies.");
710 GREYAssertEqual(0U, cookies.count, @"No cookie should be found.");
711
720 chrome_test_util::CloseAllTabs(); 712 chrome_test_util::CloseAllTabs();
721 } 713 }
722 714
723 // Verifies that logging into a form on a web page allows the user to save and 715 // Verifies that logging into a form on a web page allows the user to save and
724 // then clear a password. 716 // then clear a password.
725 - (void)testClearPasswords { 717 - (void)testClearPasswords {
726 718
727 ios::ChromeBrowserState* browserState = 719 ios::ChromeBrowserState* browserState =
728 chrome_test_util::GetOriginalBrowserState(); 720 chrome_test_util::GetOriginalBrowserState();
729 PrefService* preferences = browserState->GetPrefs(); 721 PrefService* preferences = browserState->GetPrefs();
(...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after
1055 performAction:grey_tap()]; 1047 performAction:grey_tap()];
1056 [[EarlGrey selectElementWithMatcher:PrivacyButton()] 1048 [[EarlGrey selectElementWithMatcher:PrivacyButton()]
1057 performAction:grey_tap()]; 1049 performAction:grey_tap()];
1058 [[EarlGrey selectElementWithMatcher:SendUsageDataButton()] 1050 [[EarlGrey selectElementWithMatcher:SendUsageDataButton()]
1059 performAction:grey_tap()]; 1051 performAction:grey_tap()];
1060 chrome_test_util::VerifyAccessibilityForCurrentScreen(); 1052 chrome_test_util::VerifyAccessibilityForCurrentScreen();
1061 [self closeSubSettingsMenu]; 1053 [self closeSubSettingsMenu];
1062 } 1054 }
1063 1055
1064 @end 1056 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698