Chromium Code Reviews| Index: ios/chrome/browser/net/cookies_egtest.mm |
| diff --git a/ios/chrome/browser/net/cookies_egtest.mm b/ios/chrome/browser/net/cookies_egtest.mm |
| index ec866b072ff62a48349b8a88f3a36d88b9418e18..0da8155b049c51a98d00a7ca3941372602e27450 100644 |
| --- a/ios/chrome/browser/net/cookies_egtest.mm |
| +++ b/ios/chrome/browser/net/cookies_egtest.mm |
| @@ -29,43 +29,11 @@ |
| const char kTestUrlIncognitoSetCookie[] = "http://choux/incognito/set_cookie"; |
| const char kTestResponse[] = "fleur"; |
| -NSString* const kNoCookieText = @"No cookies!"; |
| NSString* const kNormalCookieKey = @"request"; |
| NSString* const kNormalCookieValue = @"pony"; |
| NSString* const kIncognitoCookieKey = @"secret"; |
| NSString* const kIncognitoCookieValue = @"rainbow"; |
| -NSString* const kGetCookieScript = |
| - @"var c = document.cookie ? document.cookie.split(/;\\s*/) : [];" |
| - "if (!c.length) {" |
| - " document.documentElement.innerHTML = 'No cookies!';" |
| - "} else {" |
| - " document.documentElement.innerHTML = 'OK: ' + c.join(',');" |
| - "}"; |
| - |
| -// Checks that a cookie of key=value is the only cookie exists in current domain |
| -// in current web state. |
| -// Returns true if cookie |key| of value |value| is the only cookie that exists. |
| -bool CheckCookieExists(NSString* key, NSString* value) { |
| - NSError* error = nil; |
| - id result = chrome_test_util::ExecuteJavaScript(kGetCookieScript, &error); |
| - if (error) |
| - return false; |
| - NSString* stringResult = base::mac::ObjCCastStrict<NSString>(result); |
| - NSString* expectedText = [NSString stringWithFormat:@"OK: %@=%@", key, value]; |
| - return [stringResult isEqualToString:expectedText]; |
| -} |
| - |
| -// Checks that there is no cookie in current domain in current web state. |
| -bool CheckNoCookieExists() { |
| - NSError* error = nil; |
| - id result = chrome_test_util::ExecuteJavaScript(kGetCookieScript, &error); |
| - if (error) |
| - return false; |
| - NSString* stringResult = base::mac::ObjCCastStrict<NSString>(result); |
| - return [stringResult isEqualToString:kNoCookieText]; |
| -} |
| - |
| } // namespace |
| @interface CookiesTestCase : ChromeTestCase |
| @@ -132,39 +100,48 @@ - (void)testClearIncognitoFromMain { |
| // the incognito test cookie is not found. |
| [ChromeEarlGrey |
| loadURL:web::test::HttpServer::MakeUrl(kTestUrlNormalSetCookie)]; |
| - GREYAssertTrue(CheckCookieExists(kNormalCookieKey, kNormalCookieValue), |
| - @"Failed to set normal cookie in normal mode. (1)"); |
| - GREYAssertFalse(CheckCookieExists(kIncognitoCookieKey, kIncognitoCookieValue), |
| - @"Incognito cookie should not be found in normal mode. (1)"); |
| + NSDictionary* cookies = [ChromeEarlGrey getCookies]; |
| + GREYAssertTrue(cookies, @"Failed to get cookies."); |
| + GREYAssertTrue([kNormalCookieValue isEqualToString:cookies[kNormalCookieKey]], |
| + @"Failed to set normal cookie in normal mode."); |
| + GREYAssertEqual(1U, cookies.count, |
| + @"Only one cookie should be found in normal mode."); |
|
baxley
2017/03/13 21:29:04
Do we need to check all three things for every cas
liaoyuke
2017/03/13 22:50:40
Done.
|
| // Opens an incognito tab, loads the dummy page, and sets incognito test |
| // cookie. |
| chrome_test_util::OpenNewIncognitoTab(); |
| [ChromeEarlGrey |
| loadURL:web::test::HttpServer::MakeUrl(kTestUrlIncognitoSetCookie)]; |
| - GREYAssertTrue(CheckCookieExists(kIncognitoCookieKey, kIncognitoCookieValue), |
| - @"Failed to set incognito cookie in incognito mode. (2)"); |
| + cookies = [ChromeEarlGrey getCookies]; |
| + GREYAssertTrue(cookies, @"Failed to get cookies."); |
| + GREYAssertTrue( |
| + [kIncognitoCookieValue isEqualToString:cookies[kIncognitoCookieKey]], |
| + @"Failed to set incognito cookie in incognito mode."); |
| + GREYAssertEqual(1U, cookies.count, |
| + @"Only one cookie should be found in incognito mode."); |
| // Switches back to normal profile by opening up a new tab. Test cookie |
| // should not be found. |
| chrome_test_util::OpenNewTab(); |
| [ChromeEarlGrey |
| loadURL:web::test::HttpServer::MakeUrl(kTestUrlNormalBrowsing)]; |
| - GREYAssertTrue(CheckCookieExists(kNormalCookieKey, kNormalCookieValue), |
| - @"Normal cookie should be found in normal mode. (3)"); |
| - GREYAssertFalse(CheckCookieExists(kIncognitoCookieKey, kIncognitoCookieValue), |
| - @"Incognito cookie should not be found in normal mode. (3)"); |
| + cookies = [ChromeEarlGrey getCookies]; |
| + GREYAssertTrue(cookies, @"Failed to get cookies."); |
| + GREYAssertTrue([kNormalCookieValue isEqualToString:cookies[kNormalCookieKey]], |
| + @"Normal cookie should still exist in normal mode."); |
| + GREYAssertEqual(1U, cookies.count, |
| + @"Only one cookie should be found in normal mode."); |
| // Finally, closes all incognito tabs while still in normal tab. |
| // Checks that incognito cookie is gone. |
| chrome_test_util::CloseAllIncognitoTabs(); |
| chrome_test_util::OpenNewTab(); |
| [ChromeEarlGrey |
| - loadURL:web::test::HttpServer::MakeUrl(kTestUrlNormalBrowsing)]; |
| - GREYAssertTrue(CheckCookieExists(kNormalCookieKey, kNormalCookieValue), |
| - @"Normal cookie should be found in normal mode. (4)"); |
| - GREYAssertFalse(CheckCookieExists(kIncognitoCookieKey, kIncognitoCookieValue), |
| - @"Incognito cookie should be gone from normal mode. (4)"); |
| + loadURL:web::test::HttpServer::MakeUrl(kTestUrlIncognitoBrowsing)]; |
| + cookies = [ChromeEarlGrey getCookies]; |
| + GREYAssertTrue(cookies, @"Failed to get cookies."); |
| + GREYAssertEqual(0U, cookies.count, |
| + @"Incognito cookie should be gone from normal mode."); |
| } |
| // Tests that a cookie set in incognito tab is removed after closing all |
| @@ -179,8 +156,13 @@ - (void)testClearIncognitoFromIncognito { |
| chrome_test_util::OpenNewIncognitoTab(); |
| [ChromeEarlGrey |
| loadURL:web::test::HttpServer::MakeUrl(kTestUrlIncognitoSetCookie)]; |
| - GREYAssertTrue(CheckCookieExists(kIncognitoCookieKey, kIncognitoCookieValue), |
| - @"Failed to set incognito cookie in incognito mode. (1)"); |
| + NSDictionary* cookies = [ChromeEarlGrey getCookies]; |
| + GREYAssertTrue(cookies, @"Failed to get cookies."); |
| + GREYAssertTrue( |
| + [kIncognitoCookieValue isEqualToString:cookies[kIncognitoCookieKey]], |
| + @"Failed to set incognito cookie in incognito mode."); |
| + GREYAssertEqual(1U, cookies.count, |
| + @"Only one cookie should be found in incognito mode."); |
| // Closes all incognito tabs and switch back to a normal tab. |
| chrome_test_util::CloseAllIncognitoTabs(); |
| @@ -193,14 +175,21 @@ - (void)testClearIncognitoFromIncognito { |
| chrome_test_util::OpenNewIncognitoTab(); |
| [ChromeEarlGrey |
| loadURL:web::test::HttpServer::MakeUrl(kTestUrlIncognitoBrowsing)]; |
| - GREYAssertTrue(CheckNoCookieExists(), |
| - @"Incognito cookie should be gone from incognito mode. (2)"); |
| + cookies = [ChromeEarlGrey getCookies]; |
| + GREYAssertTrue(cookies, @"Failed to get cookies."); |
| + GREYAssertEqual(0U, cookies.count, |
| + @"Incognito cookie should be gone from incognito mode."); |
| // Verifies that new incognito cookies can be set. |
| [ChromeEarlGrey |
| loadURL:web::test::HttpServer::MakeUrl(kTestUrlIncognitoSetCookie)]; |
| - GREYAssertTrue(CheckCookieExists(kIncognitoCookieKey, kIncognitoCookieValue), |
| - @"Failed to set incognito cookie in incognito mode. (3)"); |
| + cookies = [ChromeEarlGrey getCookies]; |
| + GREYAssertTrue(cookies, @"Failed to get cookies."); |
| + GREYAssertTrue( |
| + [kIncognitoCookieValue isEqualToString:cookies[kIncognitoCookieKey]], |
| + @"Failed to set incognito cookie in incognito mode."); |
| + GREYAssertEqual(1U, cookies.count, |
| + @"Only one cookie should be found in incognito mode."); |
| } |
| // Tests that a cookie set in normal tab is not available in an incognito tab. |
| @@ -208,15 +197,21 @@ - (void)testSwitchToIncognito { |
| // Sets cookie in normal tab. |
| [ChromeEarlGrey |
| loadURL:web::test::HttpServer::MakeUrl(kTestUrlNormalSetCookie)]; |
| - GREYAssertTrue(CheckCookieExists(kNormalCookieKey, kNormalCookieValue), |
| - @"Failed to set normal cookie in normal mode. (1)"); |
| + NSDictionary* cookies = [ChromeEarlGrey getCookies]; |
| + GREYAssertTrue(cookies, @"Failed to get cookies."); |
| + GREYAssertTrue([kNormalCookieValue isEqualToString:cookies[kNormalCookieKey]], |
| + @"Normal cookie should still exist in normal mode."); |
| + GREYAssertEqual(1U, cookies.count, |
| + @"Only one cookie should be found in normal mode."); |
| // Switches to a new incognito tab and verifies that cookie is not there. |
| chrome_test_util::OpenNewIncognitoTab(); |
| [ChromeEarlGrey |
| loadURL:web::test::HttpServer::MakeUrl(kTestUrlIncognitoBrowsing)]; |
| - GREYAssertTrue(CheckNoCookieExists(), |
| - @"Normal cookie should not be found in incognito mode. (2)"); |
| + cookies = [ChromeEarlGrey getCookies]; |
| + GREYAssertTrue(cookies, @"Failed to get cookies."); |
| + GREYAssertEqual(0U, cookies.count, |
| + @"Normal cookie should not be found in incognito mode."); |
| // Closes all incognito tabs and then switching back to a normal tab. Verifies |
| // that the cookie set earlier is still there. |
| @@ -224,8 +219,12 @@ - (void)testSwitchToIncognito { |
| chrome_test_util::OpenNewTab(); |
| [ChromeEarlGrey |
| loadURL:web::test::HttpServer::MakeUrl(kTestUrlNormalBrowsing)]; |
| - GREYAssertTrue(CheckCookieExists(kNormalCookieKey, kNormalCookieValue), |
| - @"Normal cookie should still be found in normal mode. (3)"); |
| + cookies = [ChromeEarlGrey getCookies]; |
| + GREYAssertTrue(cookies, @"Failed to get cookies."); |
| + GREYAssertTrue([kNormalCookieValue isEqualToString:cookies[kNormalCookieKey]], |
| + @"Normal cookie should still be found in normal mode."); |
| + GREYAssertEqual(1U, cookies.count, |
| + @"Only one cookie should be found in normal mode."); |
| } |
| // Tests that a cookie set in incognito tab is only available in another |
| @@ -238,21 +237,35 @@ - (void)testSwitchToMain { |
| chrome_test_util::OpenNewIncognitoTab(); |
| [ChromeEarlGrey |
| loadURL:web::test::HttpServer::MakeUrl(kTestUrlIncognitoSetCookie)]; |
| - GREYAssertTrue(CheckCookieExists(kIncognitoCookieKey, kIncognitoCookieValue), |
| - @"Failed to set incognito cookie in incognito mode. (1)"); |
| + NSDictionary* cookies = [ChromeEarlGrey getCookies]; |
| + GREYAssertTrue(cookies, @"Failed to get cookies."); |
| + GREYAssertTrue( |
| + [kIncognitoCookieValue isEqualToString:cookies[kIncognitoCookieKey]], |
| + @"Failed to set incognito cookie in incognito mode."); |
| + GREYAssertEqual(1U, cookies.count, |
| + @"Only one cookie should be found in incognito mode."); |
| + |
| // Switches back to a normal tab and verifies that cookie set in incognito tab |
| // is not available. |
| chrome_test_util::OpenNewTab(); |
| [ChromeEarlGrey |
| loadURL:web::test::HttpServer::MakeUrl(kTestUrlNormalBrowsing)]; |
| - GREYAssertTrue(CheckNoCookieExists(), |
| - @"Incognito cookie should not be found in normal mode. (2)"); |
| + cookies = [ChromeEarlGrey getCookies]; |
| + GREYAssertTrue(cookies, @"Failed to get cookies."); |
| + GREYAssertEqual(0U, cookies.count, |
| + @"Incognito cookie should not be found in normal mode."); |
| + |
| // Returns back to Incognito tab and cookie is still there. |
| chrome_test_util::OpenNewIncognitoTab(); |
| [ChromeEarlGrey |
| loadURL:web::test::HttpServer::MakeUrl(kTestUrlIncognitoBrowsing)]; |
| - GREYAssertTrue(CheckCookieExists(kIncognitoCookieKey, kIncognitoCookieValue), |
| - @"Incognito cookie should be found in incognito mode. (3)"); |
| + cookies = [ChromeEarlGrey getCookies]; |
| + GREYAssertTrue(cookies, @"Failed to get cookies."); |
| + GREYAssertTrue( |
| + [kIncognitoCookieValue isEqualToString:cookies[kIncognitoCookieKey]], |
| + @"Incognito cookie should be found in incognito mode."); |
| + GREYAssertEqual(1U, cookies.count, |
| + @"Only one cookie should be found in incognito mode."); |
| } |
| // Tests that a cookie set in a normal tab can be found in another normal tab. |
| @@ -260,15 +273,23 @@ - (void)testShareCookiesBetweenTabs { |
| // Loads page and sets cookie in first normal tab. |
| [ChromeEarlGrey |
| loadURL:web::test::HttpServer::MakeUrl(kTestUrlNormalSetCookie)]; |
| - GREYAssertTrue(CheckCookieExists(kNormalCookieKey, kNormalCookieValue), |
| - @"Failed to set normal cookie in normal mode. (1)"); |
| + NSDictionary* cookies = [ChromeEarlGrey getCookies]; |
| + GREYAssertTrue(cookies, @"Failed to get cookies."); |
| + GREYAssertTrue([kNormalCookieValue isEqualToString:cookies[kNormalCookieKey]], |
| + @"Failed to set normal cookie in normal mode."); |
| + GREYAssertEqual(1U, cookies.count, |
| + @"Only one cookie should be found in normal mode."); |
| // Creates another normal tab and verifies that the cookie is also there. |
| chrome_test_util::OpenNewTab(); |
| [ChromeEarlGrey |
| loadURL:web::test::HttpServer::MakeUrl(kTestUrlNormalBrowsing)]; |
| - GREYAssertTrue(CheckCookieExists(kNormalCookieKey, kNormalCookieValue), |
| - @"Normal cookie should still be found in normal mode. (2)"); |
| + cookies = [ChromeEarlGrey getCookies]; |
| + GREYAssertTrue(cookies, @"Failed to get cookies."); |
| + GREYAssertTrue([kNormalCookieValue isEqualToString:cookies[kNormalCookieKey]], |
| + @"Normal cookie should still be found in normal mode."); |
| + GREYAssertEqual(1U, cookies.count, |
| + @"Only one cookie should be found in normal mode."); |
| } |
| @end |