Commit ddbe089b authored by Mohammad Refaat's avatar Mohammad Refaat Committed by Commit Bot

Fix BrowsingDataRemover unittest for iOS 14

for iOS 13 we added fetchDataRecordsOfTypes call to make sure that
cookiestore exist. but with iOS 14 this doesn't seem to be enough as
the order of callbacks is not grantueed anymore. We have to wait for
the fetchDataRecordsOfType callback to finish before asking for cookies
operations.

Bug: 1121425
Change-Id: I6a52e54ff2663ac77e8edc6dde1359a68f11604e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2381091
Commit-Queue: Mohammad Refaat <mrefaat@chromium.org>
Auto-Submit: Mohammad Refaat <mrefaat@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Reviewed-by: default avatarJustin Cohen <justincohen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#802850}
parent 158c453d
......@@ -21,11 +21,27 @@ using base::test::ios::kWaitForCookiesTimeout;
namespace {
// Makes sure that the DataStore is created, otherwise cookies can't be set in
// some cases.
bool FetchCookieStore() WARN_UNUSED_RESULT;
bool FetchCookieStore() {
__block bool fetch_done = false;
NSSet* data_types = [NSSet setWithObject:WKWebsiteDataTypeCookies];
[[WKWebsiteDataStore defaultDataStore]
fetchDataRecordsOfTypes:data_types
completionHandler:^(NSArray<WKWebsiteDataRecord*>* records) {
fetch_done = true;
}];
return WaitUntilConditionOrTimeout(kWaitForCookiesTimeout, ^bool {
return fetch_done;
});
}
// Adds cookies to the default data store. Returns whether it succeed to add
// cookies. Declare the function to have the "WARN_UNUSED_RESULT".
bool AddCookie() WARN_UNUSED_RESULT;
bool AddCookie() {
WKWebsiteDataStore* data_store = [WKWebsiteDataStore defaultDataStore];
NSHTTPCookie* cookie = [NSHTTPCookie cookieWithProperties:@{
NSHTTPCookiePath : @"path",
NSHTTPCookieName : @"cookieName",
......@@ -33,19 +49,11 @@ bool AddCookie() {
NSHTTPCookieOriginURL : @"http://chromium.org"
}];
// This is needed to forces the DataStore to be created, otherwise the cookie
// isn't set in some cases.
__block bool cookie_set = false;
NSSet* data_types = [NSSet setWithObject:WKWebsiteDataTypeCookies];
[[WKWebsiteDataStore defaultDataStore]
fetchDataRecordsOfTypes:data_types
completionHandler:^(NSArray<WKWebsiteDataRecord*>* records){
}];
[data_store.httpCookieStore setCookie:cookie
completionHandler:^{
cookie_set = true;
}];
[[WKWebsiteDataStore defaultDataStore].httpCookieStore setCookie:cookie
completionHandler:^{
cookie_set = true;
}];
return WaitUntilConditionOrTimeout(kWaitForCookiesTimeout, ^bool {
return cookie_set;
......@@ -59,14 +67,6 @@ bool HasCookies(bool should_have_cookies) {
__block bool has_cookies = false;
__block bool completion_called = false;
// This is needed to forces the DataStore to be created, otherwise the cookie
// isn't fetched in some cases.
NSSet* data_types = [NSSet setWithObject:WKWebsiteDataTypeCookies];
[[WKWebsiteDataStore defaultDataStore]
fetchDataRecordsOfTypes:data_types
completionHandler:^(NSArray<WKWebsiteDataRecord*>* records){
}];
[[WKWebsiteDataStore defaultDataStore].httpCookieStore
getAllCookies:^(NSArray<NSHTTPCookie*>* cookies) {
has_cookies = cookies.count > 0;
......@@ -107,6 +107,12 @@ bool RemoveCookies(web::BrowserState* browser_state,
namespace web {
class BrowsingDataRemoverTest : public PlatformTest {
public:
void SetUp() override {
PlatformTest::SetUp();
ASSERT_TRUE(FetchCookieStore());
}
protected:
BrowsingDataRemover* GetRemover() {
return BrowsingDataRemover::FromBrowserState(&browser_state_);
......@@ -134,10 +140,6 @@ TEST_F(BrowsingDataRemoverTest, DifferentRemoverForDifferentBrowserState) {
// Tests that removing the cookies remove them from the cookie store.
TEST_F(BrowsingDataRemoverTest, RemoveCookie) {
// TODO(crbug.com/1121425): Currently failing on iOS14.
if (@available(iOS 14, *)) {
return;
}
ASSERT_TRUE(AddCookie());
ASSERT_TRUE(HasCookies(true));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment