Commit 6402e8cb authored by Benoit Lize's avatar Benoit Lize Committed by Commit Bot

customtabs: More tests for Leapfrog.

This CL adds two types of tests:
- An End-to-End (Java + native) test that a cookie can be set.
- Third party cookie blocking tests, which is not easily doable from
  native.

Bug: 816837
Change-Id: I713b9c00689edae9944bf10bb92bb805ffa60746
Reviewed-on: https://chromium-review.googlesource.com/955582Reviewed-by: default avatarBernhard Bauer <bauerb@chromium.org>
Commit-Queue: Benoit L <lizeb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#542106}
parent 6a2bbd85
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
package org.chromium.chrome.browser.customtabs; package org.chromium.chrome.browser.customtabs;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.support.customtabs.CustomTabsService; import android.support.customtabs.CustomTabsService;
import android.support.customtabs.CustomTabsSessionToken; import android.support.customtabs.CustomTabsSessionToken;
...@@ -26,25 +27,34 @@ import org.chromium.base.library_loader.LibraryProcessType; ...@@ -26,25 +27,34 @@ import org.chromium.base.library_loader.LibraryProcessType;
import org.chromium.base.test.util.CallbackHelper; import org.chromium.base.test.util.CallbackHelper;
import org.chromium.chrome.browser.ChromeFeatureList; import org.chromium.chrome.browser.ChromeFeatureList;
import org.chromium.chrome.browser.browserservices.OriginVerifier; import org.chromium.chrome.browser.browserservices.OriginVerifier;
import org.chromium.chrome.browser.firstrun.FirstRunStatus;
import org.chromium.chrome.browser.preferences.PrefServiceBridge;
import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner; import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.chrome.test.util.browser.Features; import org.chromium.chrome.test.util.browser.Features;
import org.chromium.chrome.test.util.browser.Features.EnableFeatures; import org.chromium.chrome.test.util.browser.Features.EnableFeatures;
import org.chromium.content.browser.test.util.JavaScriptUtils;
import org.chromium.net.GURLUtils;
import org.chromium.net.test.EmbeddedTestServer; import org.chromium.net.test.EmbeddedTestServer;
/** Tests for detached resource requests. */ /** Tests for detached resource requests. */
@RunWith(ChromeJUnit4ClassRunner.class) @RunWith(ChromeJUnit4ClassRunner.class)
public class DetachedResourceRequestTest { public class DetachedResourceRequestTest {
@Rule @Rule
public TestRule processor = new Features.InstrumentationProcessor(); public TestRule mProcessor = new Features.InstrumentationProcessor();
@Rule
public CustomTabActivityTestRule mCustomTabActivityTestRule = new CustomTabActivityTestRule();
private CustomTabsConnection mConnection; private CustomTabsConnection mConnection;
private Context mContext; private Context mContext;
private EmbeddedTestServer mServer;
private static final String PRIVATE_DATA_DIRECTORY_SUFFIX = "chrome"; private static final String PRIVATE_DATA_DIRECTORY_SUFFIX = "chrome";
private static final String ORIGIN = "http://cats.google.com"; private static final String ORIGIN = "http://cats.google.com";
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
ThreadUtils.runOnUiThreadBlocking(() -> FirstRunStatus.setFirstRunFlowComplete(true));
PathUtils.setPrivateDataDirectorySuffix(PRIVATE_DATA_DIRECTORY_SUFFIX); PathUtils.setPrivateDataDirectorySuffix(PRIVATE_DATA_DIRECTORY_SUFFIX);
LibraryLoader.get(LibraryProcessType.PROCESS_BROWSER).ensureInitialized(); LibraryLoader.get(LibraryProcessType.PROCESS_BROWSER).ensureInitialized();
mConnection = CustomTabsTestUtils.setUpConnection(); mConnection = CustomTabsTestUtils.setUpConnection();
...@@ -56,6 +66,8 @@ public class DetachedResourceRequestTest { ...@@ -56,6 +66,8 @@ public class DetachedResourceRequestTest {
@After @After
public void tearDown() throws Exception { public void tearDown() throws Exception {
CustomTabsTestUtils.cleanupSessions(mConnection); CustomTabsTestUtils.cleanupSessions(mConnection);
if (mServer != null) mServer.stopAndDestroyServer();
mServer = null;
} }
@Test @Test
...@@ -99,38 +111,109 @@ public class DetachedResourceRequestTest { ...@@ -99,38 +111,109 @@ public class DetachedResourceRequestTest {
@EnableFeatures(ChromeFeatureList.CCT_PARALLEL_REQUEST) @EnableFeatures(ChromeFeatureList.CCT_PARALLEL_REQUEST)
public void testCanStartParallelRequest() throws Exception { public void testCanStartParallelRequest() throws Exception {
CustomTabsSessionToken session = prepareSession(); CustomTabsSessionToken session = prepareSession();
mServer = new EmbeddedTestServer();
final CallbackHelper cb = new CallbackHelper(); final CallbackHelper cb = new CallbackHelper();
EmbeddedTestServer server = new EmbeddedTestServer(); mServer.initializeNative(mContext, EmbeddedTestServer.ServerHTTPSSetting.USE_HTTP);
try { mServer.setConnectionListener(new EmbeddedTestServer.ConnectionListener() {
server.initializeNative(mContext, EmbeddedTestServer.ServerHTTPSSetting.USE_HTTP);
server.setConnectionListener(new EmbeddedTestServer.ConnectionListener() {
@Override @Override
public void readFromSocket(long socketId) { public void readFromSocket(long socketId) {
cb.notifyCalled(); cb.notifyCalled();
} }
}); });
server.start(); mServer.start();
ThreadUtils.runOnUiThread(() -> { String url = mServer.getURL("/echotitle");
String url = server.getURL("/echotitle"); ThreadUtils.runOnUiThread(
Assert.assertTrue(mConnection.startParallelRequest(session, url, ORIGIN)); () -> Assert.assertTrue(mConnection.startParallelRequest(session, url, ORIGIN)));
});
cb.waitForCallback(0, 1); cb.waitForCallback(0, 1);
} finally {
server.stopAndDestroyServer();
} }
@Test
@SmallTest
@EnableFeatures(ChromeFeatureList.CCT_PARALLEL_REQUEST)
public void testCanSetCookie() throws Exception {
CustomTabsSessionToken session = prepareSession();
mServer = EmbeddedTestServer.createAndStartServer(mContext);
final String url = mServer.getURL("/set-cookie?acookie");
ThreadUtils.runOnUiThreadBlocking(
() -> Assert.assertTrue(mConnection.startParallelRequest(session, url, ORIGIN)));
String echoUrl = mServer.getURL("/echoheader?Cookie");
Intent intent = CustomTabsTestUtils.createMinimalCustomTabIntent(mContext, echoUrl);
mCustomTabActivityTestRule.startCustomTabActivityWithIntent(intent);
Tab tab = mCustomTabActivityTestRule.getActivity().getActivityTab();
String content = JavaScriptUtils.executeJavaScriptAndWaitForResult(
tab.getWebContents(), "document.body.textContent");
Assert.assertEquals("\"acookie\"", content);
}
@Test
@SmallTest
@EnableFeatures(ChromeFeatureList.CCT_PARALLEL_REQUEST)
public void testCanBlockThirdPartyCookies() throws Exception {
CustomTabsSessionToken session = prepareSession();
mServer = EmbeddedTestServer.createAndStartServer(mContext);
ThreadUtils.runOnUiThreadBlocking(() -> {
PrefServiceBridge prefs = PrefServiceBridge.getInstance();
Assert.assertFalse(prefs.isBlockThirdPartyCookiesEnabled());
prefs.setBlockThirdPartyCookiesEnabled(true);
});
final String url = mServer.getURL("/set-cookie?acookie");
ThreadUtils.runOnUiThreadBlocking(
() -> Assert.assertTrue(mConnection.startParallelRequest(session, url, ORIGIN)));
String echoUrl = mServer.getURL("/echoheader?Cookie");
Intent intent = CustomTabsTestUtils.createMinimalCustomTabIntent(mContext, echoUrl);
mCustomTabActivityTestRule.startCustomTabActivityWithIntent(intent);
Tab tab = mCustomTabActivityTestRule.getActivity().getActivityTab();
String content = JavaScriptUtils.executeJavaScriptAndWaitForResult(
tab.getWebContents(), "document.body.textContent");
Assert.assertEquals("\"None\"", content);
}
@Test
@SmallTest
@EnableFeatures(ChromeFeatureList.CCT_PARALLEL_REQUEST)
public void testThirdPartyCookieBlockingAllowsFirstParty() throws Exception {
CustomTabsTestUtils.warmUpAndWait();
mServer = EmbeddedTestServer.createAndStartServer(mContext);
ThreadUtils.runOnUiThreadBlocking(() -> {
PrefServiceBridge prefs = PrefServiceBridge.getInstance();
Assert.assertFalse(prefs.isBlockThirdPartyCookiesEnabled());
prefs.setBlockThirdPartyCookiesEnabled(true);
});
final String url = mServer.getURL("/set-cookie?acookie");
String origin = GURLUtils.getOrigin(url);
CustomTabsSessionToken session = prepareSession(origin);
ThreadUtils.runOnUiThreadBlocking(
() -> Assert.assertTrue(mConnection.startParallelRequest(session, url, origin)));
String echoUrl = mServer.getURL("/echoheader?Cookie");
Intent intent = CustomTabsTestUtils.createMinimalCustomTabIntent(mContext, echoUrl);
mCustomTabActivityTestRule.startCustomTabActivityWithIntent(intent);
Tab tab = mCustomTabActivityTestRule.getActivity().getActivityTab();
String content = JavaScriptUtils.executeJavaScriptAndWaitForResult(
tab.getWebContents(), "document.body.textContent");
Assert.assertEquals("\"acookie\"", content);
} }
private CustomTabsSessionToken prepareSession() throws Exception { private CustomTabsSessionToken prepareSession() throws Exception {
return prepareSession(ORIGIN);
}
private CustomTabsSessionToken prepareSession(String origin) throws Exception {
final CustomTabsSessionToken session = final CustomTabsSessionToken session =
CustomTabsSessionToken.createMockSessionTokenForTesting(); CustomTabsSessionToken.createMockSessionTokenForTesting();
Assert.assertTrue(mConnection.newSession(session)); Assert.assertTrue(mConnection.newSession(session));
CustomTabsTestUtils.warmUpAndWait(); CustomTabsTestUtils.warmUpAndWait();
ThreadUtils.runOnUiThreadBlocking(() -> { ThreadUtils.runOnUiThreadBlocking(() -> {
OriginVerifier.addVerifiedOriginForPackage(mContext.getPackageName(), Uri.parse(ORIGIN), OriginVerifier.addVerifiedOriginForPackage(mContext.getPackageName(), Uri.parse(origin),
CustomTabsService.RELATION_USE_AS_ORIGIN); CustomTabsService.RELATION_USE_AS_ORIGIN);
Assert.assertTrue(mConnection.canDoParallelRequest(session, ORIGIN)); Assert.assertTrue(mConnection.canDoParallelRequest(session, origin));
}); });
return session; return session;
} }
......
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