OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <map> |
| 6 #include <memory> |
| 7 #include <string> |
| 8 |
| 9 #import <EarlGrey/EarlGrey.h> |
| 10 #import <XCTest/XCTest.h> |
| 11 |
| 12 #include "base/mac/foundation_util.h" |
| 13 #include "base/strings/sys_string_conversions.h" |
| 14 #import "ios/chrome/test/app/chrome_test_util.h" |
| 15 #import "ios/chrome/test/app/tab_test_util.h" |
| 16 #import "ios/chrome/test/earl_grey/chrome_earl_grey.h" |
| 17 #import "ios/chrome/test/earl_grey/chrome_test_case.h" |
| 18 #include "ios/web/public/test/http_server.h" |
| 19 #include "ios/web/public/test/http_server_util.h" |
| 20 #include "ios/web/public/test/response_providers/html_response_provider.h" |
| 21 #include "ios/web/public/test/response_providers/response_provider.h" |
| 22 #include "url/gurl.h" |
| 23 |
| 24 namespace { |
| 25 |
| 26 const char kTestUrlNormalBrowsing[] = "http://choux/normal/browsing"; |
| 27 const char kTestUrlNormalSetCookie[] = "http://choux/normal/set_cookie"; |
| 28 const char kTestUrlIncognitoBrowsing[] = "http://choux/incognito/browsing"; |
| 29 const char kTestUrlIncognitoSetCookie[] = "http://choux/incognito/set_cookie"; |
| 30 |
| 31 const char kTestResponse[] = "fleur"; |
| 32 NSString* const kNoCookieText = @"No cookies!"; |
| 33 NSString* const kNormalCookieKey = @"request"; |
| 34 NSString* const kNormalCookieValue = @"pony"; |
| 35 NSString* const kIncognitoCookieKey = @"secret"; |
| 36 NSString* const kIncognitoCookieValue = @"rainbow"; |
| 37 |
| 38 NSString* const kGetCookieScript = |
| 39 @"var c = document.cookie ? document.cookie.split(/;\\s*/) : [];" |
| 40 "if (!c.length) {" |
| 41 " document.documentElement.innerHTML = 'No cookies!';" |
| 42 "} else {" |
| 43 " document.documentElement.innerHTML = 'OK: ' + c.join(',');" |
| 44 "}"; |
| 45 |
| 46 // Checks that a cookie of key=value is the only cookie exists in current domain |
| 47 // in current web state. |
| 48 // Returns true if cookie |key| of value |value| is the only cookie that exists. |
| 49 bool CheckCookieExists(NSString* key, NSString* value) { |
| 50 NSError* error = nil; |
| 51 id result = chrome_test_util::ExecuteJavaScript(kGetCookieScript, &error); |
| 52 if (error) |
| 53 return false; |
| 54 NSString* stringResult = base::mac::ObjCCastStrict<NSString>(result); |
| 55 NSString* expectedText = [NSString stringWithFormat:@"OK: %@=%@", key, value]; |
| 56 return [stringResult isEqualToString:expectedText]; |
| 57 } |
| 58 |
| 59 // Checks that there is no cookie in current domain in current web state. |
| 60 bool CheckNoCookieExists() { |
| 61 NSError* error = nil; |
| 62 id result = chrome_test_util::ExecuteJavaScript(kGetCookieScript, &error); |
| 63 if (error) |
| 64 return false; |
| 65 NSString* stringResult = base::mac::ObjCCastStrict<NSString>(result); |
| 66 return [stringResult isEqualToString:kNoCookieText]; |
| 67 } |
| 68 |
| 69 } // namespace |
| 70 |
| 71 @interface CookiesTestCase : ChromeTestCase |
| 72 @end |
| 73 |
| 74 @implementation CookiesTestCase |
| 75 |
| 76 #pragma mark - Overrides superclass |
| 77 |
| 78 + (void)setUp { |
| 79 [super setUp]; |
| 80 // Creates a map of canned responses and set up the test HTML server. |
| 81 // |kTestUrlNormalSetCookie| and |kTestUrlIncognitoSetCookie| always sets |
| 82 // cookie in response header while |kTestUrlNormalBrowsing| and |
| 83 // |kTestUrlIncognitoBrowsing| doesn't. |
| 84 std::map<GURL, std::pair<std::string, std::string>> responses; |
| 85 |
| 86 NSString* normalCookie = [NSString |
| 87 stringWithFormat:@"%@=%@", kNormalCookieKey, kNormalCookieValue]; |
| 88 NSString* incognitoCookie = [NSString |
| 89 stringWithFormat:@"%@=%@", kIncognitoCookieKey, kIncognitoCookieValue]; |
| 90 |
| 91 responses[web::test::HttpServer::MakeUrl(kTestUrlNormalBrowsing)] = |
| 92 std::pair<std::string, std::string>("", kTestResponse); |
| 93 responses[web::test::HttpServer::MakeUrl(kTestUrlNormalSetCookie)] = |
| 94 std::pair<std::string, std::string>(base::SysNSStringToUTF8(normalCookie), |
| 95 kTestResponse); |
| 96 responses[web::test::HttpServer::MakeUrl(kTestUrlIncognitoBrowsing)] = |
| 97 std::pair<std::string, std::string>("", kTestResponse); |
| 98 responses[web::test::HttpServer::MakeUrl(kTestUrlIncognitoSetCookie)] = |
| 99 std::pair<std::string, std::string>( |
| 100 base::SysNSStringToUTF8(incognitoCookie), kTestResponse); |
| 101 |
| 102 web::test::SetUpSimpleHttpServerWithSetCookies(responses); |
| 103 } |
| 104 |
| 105 // Clear cookies to make sure that tests do not interfere each other. |
| 106 // TODO(crbug.com/638674): Evaluate if this can move to shared code. |
| 107 - (void)tearDown { |
| 108 [ChromeEarlGrey |
| 109 loadURL:web::test::HttpServer::MakeUrl(kTestUrlNormalBrowsing)]; |
| 110 NSString* const clearCookieScript = |
| 111 @"var cookies = document.cookie.split(';');" |
| 112 "for (var i = 0; i < cookies.length; i++) {" |
| 113 " var cookie = cookies[i];" |
| 114 " var eqPos = cookie.indexOf('=');" |
| 115 " var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;" |
| 116 " document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:00 GMT';" |
| 117 "}"; |
| 118 NSError* error = nil; |
| 119 chrome_test_util::ExecuteJavaScript(clearCookieScript, &error); |
| 120 [super tearDown]; |
| 121 } |
| 122 |
| 123 #pragma mark - Tests |
| 124 |
| 125 // Tests toggling between Normal tabs and Incognito tabs. Different cookies |
| 126 // (request=pony for normal tabs, secret=rainbow for incognito tabs) are set. |
| 127 // The goal is to verify that cookies set in incognito tabs are available in |
| 128 // incognito tabs but not available in normal tabs. Cookies set in incognito |
| 129 // tabs are also deleted when all incognito tabs are closed. |
| 130 - (void)testClearIncognitoFromMain { |
| 131 // Loads a dummy page in normal tab. Sets a normal test cookie. Verifies that |
| 132 // the incognito test cookie is not found. |
| 133 [ChromeEarlGrey |
| 134 loadURL:web::test::HttpServer::MakeUrl(kTestUrlNormalSetCookie)]; |
| 135 GREYAssertTrue(CheckCookieExists(kNormalCookieKey, kNormalCookieValue), |
| 136 @"Failed to set normal cookie in normal mode. (1)"); |
| 137 GREYAssertFalse(CheckCookieExists(kIncognitoCookieKey, kIncognitoCookieValue), |
| 138 @"Incognito cookie should not be found in normal mode. (1)"); |
| 139 |
| 140 // Opens an incognito tab, loads the dummy page, and sets incognito test |
| 141 // cookie. |
| 142 chrome_test_util::OpenNewIncognitoTab(); |
| 143 [ChromeEarlGrey |
| 144 loadURL:web::test::HttpServer::MakeUrl(kTestUrlIncognitoSetCookie)]; |
| 145 GREYAssertTrue(CheckCookieExists(kIncognitoCookieKey, kIncognitoCookieValue), |
| 146 @"Failed to set incognito cookie in incognito mode. (2)"); |
| 147 |
| 148 // Switches back to normal profile by opening up a new tab. Test cookie |
| 149 // should not be found. |
| 150 chrome_test_util::OpenNewTab(); |
| 151 [ChromeEarlGrey |
| 152 loadURL:web::test::HttpServer::MakeUrl(kTestUrlNormalBrowsing)]; |
| 153 GREYAssertTrue(CheckCookieExists(kNormalCookieKey, kNormalCookieValue), |
| 154 @"Normal cookie should be found in normal mode. (3)"); |
| 155 GREYAssertFalse(CheckCookieExists(kIncognitoCookieKey, kIncognitoCookieValue), |
| 156 @"Incognito cookie should not be found in normal mode. (3)"); |
| 157 |
| 158 // Finally, closes all incognito tabs while still in normal tab. |
| 159 // Checks that incognito cookie is gone. |
| 160 chrome_test_util::CloseAllIncognitoTabs(); |
| 161 chrome_test_util::OpenNewTab(); |
| 162 [ChromeEarlGrey |
| 163 loadURL:web::test::HttpServer::MakeUrl(kTestUrlNormalBrowsing)]; |
| 164 GREYAssertTrue(CheckCookieExists(kNormalCookieKey, kNormalCookieValue), |
| 165 @"Normal cookie should be found in normal mode. (4)"); |
| 166 GREYAssertFalse(CheckCookieExists(kIncognitoCookieKey, kIncognitoCookieValue), |
| 167 @"Incognito cookie should be gone from normal mode. (4)"); |
| 168 } |
| 169 |
| 170 // Tests that a cookie set in incognito tab is removed after closing all |
| 171 // incognito tabs and then when new incognito tab is created the cookie will |
| 172 // not reappear. |
| 173 - (void)testClearIncognitoFromIncognito { |
| 174 // Loads a page in normal tab. |
| 175 [ChromeEarlGrey |
| 176 loadURL:web::test::HttpServer::MakeUrl(kTestUrlNormalBrowsing)]; |
| 177 |
| 178 // Opens an incognito tab, loads a page, and sets an incognito cookie. |
| 179 chrome_test_util::OpenNewIncognitoTab(); |
| 180 [ChromeEarlGrey |
| 181 loadURL:web::test::HttpServer::MakeUrl(kTestUrlIncognitoSetCookie)]; |
| 182 GREYAssertTrue(CheckCookieExists(kIncognitoCookieKey, kIncognitoCookieValue), |
| 183 @"Failed to set incognito cookie in incognito mode. (1)"); |
| 184 |
| 185 // Closes all incognito tabs and switch back to a normal tab. |
| 186 chrome_test_util::CloseAllIncognitoTabs(); |
| 187 chrome_test_util::OpenNewTab(); |
| 188 [ChromeEarlGrey |
| 189 loadURL:web::test::HttpServer::MakeUrl(kTestUrlNormalBrowsing)]; |
| 190 |
| 191 // Opens a new incognito tab and verify that the previously set cookie |
| 192 // is no longer there. |
| 193 chrome_test_util::OpenNewIncognitoTab(); |
| 194 [ChromeEarlGrey |
| 195 loadURL:web::test::HttpServer::MakeUrl(kTestUrlIncognitoBrowsing)]; |
| 196 GREYAssertTrue(CheckNoCookieExists(), |
| 197 @"Incognito cookie should be gone from incognito mode. (2)"); |
| 198 |
| 199 // Verifies that new incognito cookies can be set. |
| 200 [ChromeEarlGrey |
| 201 loadURL:web::test::HttpServer::MakeUrl(kTestUrlIncognitoSetCookie)]; |
| 202 GREYAssertTrue(CheckCookieExists(kIncognitoCookieKey, kIncognitoCookieValue), |
| 203 @"Failed to set incognito cookie in incognito mode. (3)"); |
| 204 } |
| 205 |
| 206 // Tests that a cookie set in normal tab is not available in an incognito tab. |
| 207 - (void)testSwitchToIncognito { |
| 208 // Sets cookie in normal tab. |
| 209 [ChromeEarlGrey |
| 210 loadURL:web::test::HttpServer::MakeUrl(kTestUrlNormalSetCookie)]; |
| 211 GREYAssertTrue(CheckCookieExists(kNormalCookieKey, kNormalCookieValue), |
| 212 @"Failed to set normal cookie in normal mode. (1)"); |
| 213 |
| 214 // Switches to a new incognito tab and verifies that cookie is not there. |
| 215 chrome_test_util::OpenNewIncognitoTab(); |
| 216 [ChromeEarlGrey |
| 217 loadURL:web::test::HttpServer::MakeUrl(kTestUrlIncognitoBrowsing)]; |
| 218 GREYAssertTrue(CheckNoCookieExists(), |
| 219 @"Normal cookie should not be found in incognito mode. (2)"); |
| 220 |
| 221 // Closes all incognito tabs and then switching back to a normal tab. Verifies |
| 222 // that the cookie set earlier is still there. |
| 223 chrome_test_util::CloseAllIncognitoTabs(); |
| 224 chrome_test_util::OpenNewTab(); |
| 225 [ChromeEarlGrey |
| 226 loadURL:web::test::HttpServer::MakeUrl(kTestUrlNormalBrowsing)]; |
| 227 GREYAssertTrue(CheckCookieExists(kNormalCookieKey, kNormalCookieValue), |
| 228 @"Normal cookie should still be found in normal mode. (3)"); |
| 229 } |
| 230 |
| 231 // Tests that a cookie set in incognito tab is only available in another |
| 232 // incognito tab. They are not available in a normal tab. |
| 233 - (void)testSwitchToMain { |
| 234 // Loads a page in normal tab and then switches to a new incognito tab. Sets |
| 235 // cookie in incognito tab. |
| 236 [ChromeEarlGrey |
| 237 loadURL:web::test::HttpServer::MakeUrl(kTestUrlNormalBrowsing)]; |
| 238 chrome_test_util::OpenNewIncognitoTab(); |
| 239 [ChromeEarlGrey |
| 240 loadURL:web::test::HttpServer::MakeUrl(kTestUrlIncognitoSetCookie)]; |
| 241 GREYAssertTrue(CheckCookieExists(kIncognitoCookieKey, kIncognitoCookieValue), |
| 242 @"Failed to set incognito cookie in incognito mode. (1)"); |
| 243 // Switches back to a normal tab and verifies that cookie set in incognito tab |
| 244 // is not available. |
| 245 chrome_test_util::OpenNewTab(); |
| 246 [ChromeEarlGrey |
| 247 loadURL:web::test::HttpServer::MakeUrl(kTestUrlNormalBrowsing)]; |
| 248 GREYAssertTrue(CheckNoCookieExists(), |
| 249 @"Incognito cookie should not be found in normal mode. (2)"); |
| 250 // Returns back to Incognito tab and cookie is still there. |
| 251 chrome_test_util::OpenNewIncognitoTab(); |
| 252 [ChromeEarlGrey |
| 253 loadURL:web::test::HttpServer::MakeUrl(kTestUrlIncognitoBrowsing)]; |
| 254 GREYAssertTrue(CheckCookieExists(kIncognitoCookieKey, kIncognitoCookieValue), |
| 255 @"Incognito cookie should be found in incognito mode. (3)"); |
| 256 } |
| 257 |
| 258 // Tests that a cookie set in a normal tab can be found in another normal tab. |
| 259 - (void)testShareCookiesBetweenTabs { |
| 260 // Loads page and sets cookie in first normal tab. |
| 261 [ChromeEarlGrey |
| 262 loadURL:web::test::HttpServer::MakeUrl(kTestUrlNormalSetCookie)]; |
| 263 GREYAssertTrue(CheckCookieExists(kNormalCookieKey, kNormalCookieValue), |
| 264 @"Failed to set normal cookie in normal mode. (1)"); |
| 265 |
| 266 // Creates another normal tab and verifies that the cookie is also there. |
| 267 chrome_test_util::OpenNewTab(); |
| 268 [ChromeEarlGrey |
| 269 loadURL:web::test::HttpServer::MakeUrl(kTestUrlNormalBrowsing)]; |
| 270 GREYAssertTrue(CheckCookieExists(kNormalCookieKey, kNormalCookieValue), |
| 271 @"Normal cookie should still be found in normal mode. (2)"); |
| 272 } |
| 273 |
| 274 @end |
OLD | NEW |