@MediumTest
  @Feature({"AndroidWebView", "Privacy"})
  public void testRemoveSessionCookiesNullCallback() throws Throwable {
    final String url = "http://www.example.com";
    final String sessionCookie = "cookie1=peter";
    final String normalCookie = "cookie2=sue";

    mCookieManager.setCookie(url, sessionCookie);
    mCookieManager.setCookie(url, makeExpiringCookie(normalCookie, 600));
    String allCookies = mCookieManager.getCookie(url);
    assertTrue(allCookies.contains(sessionCookie));
    assertTrue(allCookies.contains(normalCookie));

    mCookieManager.removeSessionCookies(null);

    // Eventually the session cookie is removed.
    poll(
        new Callable<Boolean>() {
          @Override
          public Boolean call() throws Exception {
            String c = mCookieManager.getCookie(url);
            return !c.contains(sessionCookie) && c.contains(normalCookie);
          }
        });
  }
  @MediumTest
  @Feature({"AndroidWebView", "Privacy"})
  public void testThirdPartyJavascriptCookie() throws Throwable {
    TestWebServer webServer = null;
    try {
      // This test again uses 127.0.0.1/localhost trick to simulate a third party.
      webServer = new TestWebServer(false);
      ThirdPartyCookiesTestHelper thirdParty = new ThirdPartyCookiesTestHelper(webServer);

      mCookieManager.setAcceptCookie(true);
      assertTrue(mCookieManager.acceptCookie());

      // When third party cookies are disabled...
      thirdParty.getSettings().setAcceptThirdPartyCookies(false);
      assertFalse(thirdParty.getSettings().getAcceptThirdPartyCookies());

      // ...we can't set third party cookies.
      thirdParty.assertThirdPartyIFrameCookieResult("1", false);

      // When third party cookies are enabled...
      thirdParty.getSettings().setAcceptThirdPartyCookies(true);
      assertTrue(thirdParty.getSettings().getAcceptThirdPartyCookies());

      // ...we can set third party cookies.
      thirdParty.assertThirdPartyIFrameCookieResult("2", true);
    } finally {
      if (webServer != null) webServer.shutdown();
    }
  }
 @MediumTest
 @Feature({"AndroidWebView", "Privacy"})
 public void testHasCookie() throws Throwable {
   assertFalse(mCookieManager.hasCookies());
   mCookieManager.setCookie("http://www.example.com", "name=test");
   assertTrue(mCookieManager.hasCookies());
 }
  @MediumTest
  @Feature({"AndroidWebView", "Privacy"})
  public void testRemoveSessionCookiesCallback() throws Throwable {
    final String url = "http://www.example.com";
    final String sessionCookie = "cookie1=peter";
    final String normalCookie = "cookie2=sue";

    TestValueCallback<Boolean> callback = new TestValueCallback<Boolean>();
    int callCount = callback.getOnReceiveValueHelper().getCallCount();

    mCookieManager.setCookie(url, sessionCookie);
    mCookieManager.setCookie(url, makeExpiringCookie(normalCookie, 600));

    // When there is a session cookie then it is removed.
    removeSessionCookiesOnUiThread(callback);
    callback.getOnReceiveValueHelper().waitForCallback(callCount);
    assertTrue(callback.getValue());
    String allCookies = mCookieManager.getCookie(url);
    assertTrue(!allCookies.contains(sessionCookie));
    assertTrue(allCookies.contains(normalCookie));

    callCount = callback.getOnReceiveValueHelper().getCallCount();

    // If there are no session cookies then none are removed.
    removeSessionCookiesOnUiThread(callback);
    callback.getOnReceiveValueHelper().waitForCallback(callCount);
    assertFalse(callback.getValue());
  }
 @MediumTest
 @Feature({"AndroidWebView", "Privacy"})
 public void testSetCookie() throws Throwable {
   String url = "http://www.example.com";
   String cookie = "name=test";
   mCookieManager.setCookie(url, cookie);
   assertEquals(cookie, mCookieManager.getCookie(url));
 }
 @MediumTest
 @Feature({"AndroidWebView", "Privacy"})
 public void testRemoveAllCookies() throws Exception {
   mCookieManager.setCookie("http://www.example.com", "name=test");
   assertTrue(mCookieManager.hasCookies());
   mCookieManager.removeAllCookies();
   assertFalse(mCookieManager.hasCookies());
 }
  @MediumTest
  @Feature({"AndroidWebView", "Privacy"})
  public void testExpiredCookiesAreNotSet() throws Exception {
    final String url = "http://www.example.com";
    final String cookie = "cookie1=peter";

    mCookieManager.setCookie(url, makeExpiringCookie(cookie, -1));
    assertNull(mCookieManager.getCookie(url));
  }
  @MediumTest
  @Feature({"AndroidWebView", "Privacy"})
  public void testThirdPartyCookie() throws Throwable {
    TestWebServer webServer = null;
    try {
      // In theory we need two servers to test this, one server ('the first party')
      // which returns a response with a link to a second server ('the third party')
      // at different origin. This second server attempts to set a cookie which should
      // fail if AcceptThirdPartyCookie() is false.
      // Strictly according to the letter of RFC6454 it should be possible to set this
      // situation up with two TestServers on different ports (these count as having
      // different origins) but Chrome is not strict about this and does not check the
      // port. Instead we cheat making some of the urls come from localhost and some
      // from 127.0.0.1 which count (both in theory and pratice) as having different
      // origins.
      webServer = new TestWebServer(false);

      // Turn global allow on.
      mCookieManager.setAcceptCookie(true);
      assertTrue(mCookieManager.acceptCookie());

      // When third party cookies are disabled...
      mAwContents.getSettings().setAcceptThirdPartyCookies(false);
      assertFalse(mAwContents.getSettings().getAcceptThirdPartyCookies());

      // ...we can't set third party cookies.
      // First on the third party server we create a url which tries to set a cookie.
      String cookieUrl =
          toThirdPartyUrl(makeCookieUrl(webServer, "/cookie_1.js", "test1", "value1"));
      // Then we create a url on the first party server which links to the first url.
      String url = makeScriptLinkUrl(webServer, "/content_1.html", cookieUrl);
      loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url);
      assertNull(mCookieManager.getCookie(cookieUrl));

      // When third party cookies are enabled...
      mAwContents.getSettings().setAcceptThirdPartyCookies(true);
      assertTrue(mAwContents.getSettings().getAcceptThirdPartyCookies());

      // ...we can set third party cookies.
      cookieUrl = toThirdPartyUrl(makeCookieUrl(webServer, "/cookie_2.js", "test2", "value2"));
      url = makeScriptLinkUrl(webServer, "/content_2.html", cookieUrl);
      loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url);
      waitForCookie(cookieUrl);
      String cookie = mCookieManager.getCookie(cookieUrl);
      assertNotNull(cookie);
      validateCookies(cookie, "test2");
    } finally {
      if (webServer != null) webServer.shutdown();
    }
  }
  @Override
  protected void setUp() throws Exception {
    super.setUp();

    mCookieManager = new AwCookieManager();
    mContentsClient = new TestAwContentsClient();
    final AwTestContainerView testContainerView =
        createAwTestContainerViewOnMainSync(mContentsClient);
    mAwContents = testContainerView.getAwContents();
    mAwContents.getSettings().setJavaScriptEnabled(true);
    assertNotNull(mCookieManager);

    mCookieManager.setAcceptCookie(true);
    assertTrue(mCookieManager.acceptCookie());
  }
 private boolean fileURLCanSetCookie(String suffix) throws Throwable {
   String value = "value" + suffix;
   String url = "file:///android_asset/cookie_test.html?value=" + value;
   loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url);
   String cookie = mCookieManager.getCookie(url);
   return cookie != null && cookie.contains("test=" + value);
 }
  @MediumTest
  @Feature({"AndroidWebView", "Privacy"})
  public void testRemoveSessionCookies() throws Exception {
    final String url = "http://www.example.com";
    final String sessionCookie = "cookie1=peter";
    final String normalCookie = "cookie2=sue";

    mCookieManager.setCookie(url, sessionCookie);
    mCookieManager.setCookie(url, makeExpiringCookie(normalCookie, 600));

    mCookieManager.removeSessionCookies();

    String allCookies = mCookieManager.getCookie(url);
    assertFalse(allCookies.contains(sessionCookie));
    assertTrue(allCookies.contains(normalCookie));
  }
  @MediumTest
  @Feature({"AndroidWebView", "Privacy"})
  public void testRemoveAllCookiesNullCallback() throws Throwable {
    mCookieManager.setCookie("http://www.example.com", "name=test");

    mCookieManager.removeAllCookies(null);

    // Eventually the cookies are removed.
    poll(
        new Callable<Boolean>() {
          @Override
          public Boolean call() throws Exception {
            return !mCookieManager.hasCookies();
          }
        });
  }
  @MediumTest
  @Feature({"AndroidWebView", "Privacy"})
  public void testSetCookieNullCallback() throws Throwable {
    mCookieManager.setAcceptCookie(true);
    assertTrue(mCookieManager.acceptCookie());

    final String url = "http://www.example.com";
    final String cookie = "name=test";

    mCookieManager.setCookie(url, cookie, null);

    poll(
        new Callable<Boolean>() {
          @Override
          public Boolean call() throws Exception {
            return mCookieManager.hasCookies();
          }
        });
  }
  @MediumTest
  @Feature({"AndroidWebView", "Privacy"})
  public void testThirdPartyCookiesArePerWebview() throws Throwable {
    TestWebServer webServer = null;
    try {
      webServer = new TestWebServer(false);
      mCookieManager.setAcceptCookie(true);
      mCookieManager.removeAllCookie();
      assertTrue(mCookieManager.acceptCookie());
      assertFalse(mCookieManager.hasCookies());

      ThirdPartyCookiesTestHelper helperOne = new ThirdPartyCookiesTestHelper(webServer);
      ThirdPartyCookiesTestHelper helperTwo = new ThirdPartyCookiesTestHelper(webServer);

      helperOne.getSettings().setAcceptThirdPartyCookies(false);
      helperTwo.getSettings().setAcceptThirdPartyCookies(false);
      assertFalse(helperOne.getSettings().getAcceptThirdPartyCookies());
      assertFalse(helperTwo.getSettings().getAcceptThirdPartyCookies());
      helperOne.assertThirdPartyIFrameCookieResult("1", false);
      helperTwo.assertThirdPartyIFrameCookieResult("2", false);

      helperTwo.getSettings().setAcceptThirdPartyCookies(true);
      assertFalse(helperOne.getSettings().getAcceptThirdPartyCookies());
      assertTrue(helperTwo.getSettings().getAcceptThirdPartyCookies());
      helperOne.assertThirdPartyIFrameCookieResult("3", false);
      helperTwo.assertThirdPartyIFrameCookieResult("4", true);

      helperOne.getSettings().setAcceptThirdPartyCookies(true);
      assertTrue(helperOne.getSettings().getAcceptThirdPartyCookies());
      assertTrue(helperTwo.getSettings().getAcceptThirdPartyCookies());
      helperOne.assertThirdPartyIFrameCookieResult("5", true);
      helperTwo.assertThirdPartyIFrameCookieResult("6", true);

      helperTwo.getSettings().setAcceptThirdPartyCookies(false);
      assertTrue(helperOne.getSettings().getAcceptThirdPartyCookies());
      assertFalse(helperTwo.getSettings().getAcceptThirdPartyCookies());
      helperOne.assertThirdPartyIFrameCookieResult("7", true);
      helperTwo.assertThirdPartyIFrameCookieResult("8", false);
    } finally {
      if (webServer != null) webServer.shutdown();
    }
  }
  @MediumTest
  @Feature({"AndroidWebView", "Privacy"})
  public void testRejectFileSchemeCookies() throws Throwable {
    mCookieManager.setAcceptFileSchemeCookies(false);
    mAwContents.getSettings().setAllowFileAccess(true);

    mAwContents.getSettings().setAcceptThirdPartyCookies(true);
    assertFalse(fileURLCanSetCookie("3"));
    mAwContents.getSettings().setAcceptThirdPartyCookies(false);
    assertFalse(fileURLCanSetCookie("4"));
  }
  @MediumTest
  @Feature({"AndroidWebView", "Privacy"})
  public void testCookiesExpire() throws Exception {
    final String url = "http://www.example.com";
    final String cookie = "cookie1=peter";

    mCookieManager.setCookie(url, makeExpiringCookieMs(cookie, 1200));

    // The cookie exists:
    assertTrue(mCookieManager.hasCookies());

    // But eventually expires:
    poll(
        new Callable<Boolean>() {
          @Override
          public Boolean call() throws Exception {
            return !mCookieManager.hasCookies();
          }
        });
  }
  @MediumTest
  @Feature({"AndroidWebView", "Privacy"})
  public void testRemoveAllCookiesCallback() throws Throwable {
    TestValueCallback<Boolean> callback = new TestValueCallback<Boolean>();
    int callCount = callback.getOnReceiveValueHelper().getCallCount();

    mCookieManager.setCookie("http://www.example.com", "name=test");

    // When we remove all cookies the first time some cookies are removed.
    removeAllCookiesOnUiThread(callback);
    callback.getOnReceiveValueHelper().waitForCallback(callCount);
    assertTrue(callback.getValue());
    assertFalse(mCookieManager.hasCookies());

    callCount = callback.getOnReceiveValueHelper().getCallCount();

    // The second time none are removed.
    removeAllCookiesOnUiThread(callback);
    callback.getOnReceiveValueHelper().waitForCallback(callCount);
    assertFalse(callback.getValue());
  }
  @MediumTest
  @Feature({"AndroidWebView", "Privacy"})
  public void testSetCookieCallback() throws Throwable {
    final String url = "http://www.example.com";
    final String cookie = "name=test";
    final String brokenUrl = "foo";

    final TestValueCallback<Boolean> callback = new TestValueCallback<Boolean>();
    int callCount = callback.getOnReceiveValueHelper().getCallCount();

    setCookieOnUiThread(url, cookie, callback);
    callback.getOnReceiveValueHelper().waitForCallback(callCount);
    assertTrue(callback.getValue());
    assertEquals(cookie, mCookieManager.getCookie(url));

    callCount = callback.getOnReceiveValueHelper().getCallCount();

    setCookieOnUiThread(brokenUrl, cookie, callback);
    callback.getOnReceiveValueHelper().waitForCallback(callCount);
    assertFalse(callback.getValue());
    assertEquals(null, mCookieManager.getCookie(brokenUrl));
  }
  @MediumTest
  @Feature({"AndroidWebView", "Privacy"})
  public void testCookieExpiration() throws Exception {
    final String url = "http://www.example.com";
    final String sessionCookie = "cookie1=peter";
    final String longCookie = "cookie2=marc";

    mCookieManager.setCookie(url, sessionCookie);
    mCookieManager.setCookie(url, makeExpiringCookie(longCookie, 600));

    String allCookies = mCookieManager.getCookie(url);
    assertTrue(allCookies.contains(sessionCookie));
    assertTrue(allCookies.contains(longCookie));

    // Removing expired cookies doesn't have an observable effect but since people will still
    // be calling it for a while it shouldn't break anything either.
    mCookieManager.removeExpiredCookies();

    allCookies = mCookieManager.getCookie(url);
    assertTrue(allCookies.contains(sessionCookie));
    assertTrue(allCookies.contains(longCookie));
  }
  @MediumTest
  @Feature({"AndroidWebView", "Privacy"})
  public void testAcceptCookie() throws Throwable {
    TestWebServer webServer = null;
    try {
      webServer = new TestWebServer(false);
      String path = "/cookie_test.html";
      String responseStr = "<html><head><title>TEST!</title></head><body>HELLO!</body></html>";
      String url = webServer.setResponse(path, responseStr, null);

      mCookieManager.setAcceptCookie(false);
      assertFalse(mCookieManager.acceptCookie());

      loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url);
      setCookieWithJavaScript("test1", "value1");
      assertNull(mCookieManager.getCookie(url));

      List<Pair<String, String>> responseHeaders = new ArrayList<Pair<String, String>>();
      responseHeaders.add(Pair.create("Set-Cookie", "header-test1=header-value1; path=" + path));
      url = webServer.setResponse(path, responseStr, responseHeaders);
      loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url);
      assertNull(mCookieManager.getCookie(url));

      mCookieManager.setAcceptCookie(true);
      assertTrue(mCookieManager.acceptCookie());

      url = webServer.setResponse(path, responseStr, null);
      loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url);
      setCookieWithJavaScript("test2", "value2");
      waitForCookie(url);
      String cookie = mCookieManager.getCookie(url);
      assertNotNull(cookie);
      validateCookies(cookie, "test2");

      responseHeaders = new ArrayList<Pair<String, String>>();
      responseHeaders.add(Pair.create("Set-Cookie", "header-test2=header-value2 path=" + path));
      url = webServer.setResponse(path, responseStr, responseHeaders);
      loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url);
      waitForCookie(url);
      cookie = mCookieManager.getCookie(url);
      assertNotNull(cookie);
      validateCookies(cookie, "test2", "header-test2");
    } finally {
      if (webServer != null) webServer.shutdown();
    }
  }