Skip to content

Instantly share code, notes, and snippets.

@benelog
Last active December 29, 2015 10:19
Show Gist options
  • Save benelog/7655764 to your computer and use it in GitHub Desktop.
Save benelog/7655764 to your computer and use it in GitHub Desktop.
CookieManagerTest
package helloworld.android;
import java.util.concurrent.TimeUnit;
import android.content.Context;
import android.test.AndroidTestCase;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
public class CookieManagerTest extends AndroidTestCase {
String url = "robolectric.org/";
String httpUrl = "http://robolectric.org/";
String httpsUrl = "https://robolectric.org/";
CookieManager cookieManager;
public void setUp() {
Context context = getContext();
CookieSyncManager.createInstance(context);
cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
}
public void testHaveCookieWhenCookieIsSet() {
cookieManager.setCookie(url, "name=value; Expires=Wed, 09 Jun 2021 10:18:14 GMT");
assertTrue(cookieManager.hasCookies());
}
public void testNotHaveCookieWhenCookieIsNotSet() {
assertFalse(cookieManager.hasCookies());
}
public void testGetNullWhenCookieIsNotPresent() {
assertNull(cookieManager.getCookie(url));
}
public void testGetNullWhenCookieIsNotPresentInUrl() {
cookieManager.setCookie(httpUrl, "name=value; Expires=Wed, 11 Jul 2035 10:18:14 GMT");
assertNull(cookieManager.getCookie("http://google.com"));
}
public void testSetAndGetOneCookie() {
cookieManager.setCookie(httpUrl, "name=value; Expires=Wed, 11 Jul 2035 10:18:14 GMT");
assertEquals("name=value", cookieManager.getCookie(httpUrl));
}
public void testSetWithHttpAndGetWithoutHttp() {
cookieManager.setCookie(httpUrl, "name=value; Expires=Wed, 11 Jul 2035 10:18:14 GMT");
assertEquals("name=value", cookieManager.getCookie(url));
}
public void testSetWithHttpSchemeAndGetWithHttps() {
cookieManager.setCookie(httpUrl, "name=value; Expires=Wed, 11 Jul 2035 10:18:14 GMT");
assertEquals("name=value", cookieManager.getCookie(httpsUrl));
}
public void testSetTwoCookies() {
cookieManager.setCookie(url, "name=value; Expires=Wed, 11 Jul 2035 10:18:14 GMT");
cookieManager.setCookie(url, "name2=value2; Expires=Wed, 11 Jul 2035 10:18:14 GMT");
assertEquals("name=value; name2=value2", cookieManager.getCookie(url));
}
public void testSetCookieWithInvalidExpiesValue() {
cookieManager.setCookie(httpUrl, "name=value; Expires=3234asdfasdf10:18:14 GMT");
assertEquals("name=value", cookieManager.getCookie(url));
}
public void testSetCookieWithoutValue() {
cookieManager.setCookie(httpUrl, "name=");
assertEquals("name=", cookieManager.getCookie(url));
}
public void testSetAndGetCookieWhenAcceptCookieIsFalse() {
cookieManager.setAcceptCookie(false);
cookieManager.setCookie(httpUrl, "name=value; Expires=3234asdfasdf10:18:14 GMT");
assertEquals("name=value", cookieManager.getCookie(url));
assertFalse(cookieManager.acceptCookie());
}
public void testRemoveAllCookie() {
cookieManager.setCookie(url, "name=value; Expires=Wed, 11 Jul 2035 10:18:14 GMT");
cookieManager.setCookie(url, "name2=value2;");
cookieManager.removeAllCookie();
assertNull(cookieManager.getCookie(url));
}
public void testRemoveExpiredCookie() {
cookieManager.setCookie(url, "name=value; Expires=Wed, 11 Jul 2035 10:18:14 GMT");
cookieManager.setCookie(url, "name2=value2; Expires=Wed, 13 Jul 2011 10:18:14 GMT");
cookieManager.removeExpiredCookie();
assertEquals("name=value", cookieManager.getCookie(url));
}
public void testRemoveSessionCookie() throws InterruptedException {
cookieManager.setCookie(url, "name=value; Expires=Wed, 11 Jul 2035 10:18:14 GMT");
cookieManager.setCookie(url, "name2=value2;");
cookieManager.removeSessionCookie();
TimeUnit.SECONDS.sleep(1);
assertEquals("name=value", cookieManager.getCookie(url));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment