Commit fc4c9765 authored by Rohit Agarwal's avatar Rohit Agarwal Committed by Commit Bot

Audit Incognito profile usage in WarmupManagerTest.

Bug: 1060940
Change-Id: I922c5711383d340425bab3682f79923b049d8cbb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2497452
Commit-Queue: Rohit Agarwal <roagarwal@chromium.org>
Reviewed-by: default avatarMarc Treib <treib@chromium.org>
Cr-Commit-Position: refs/heads/master@{#821155}
parent 90da3bc3
......@@ -26,6 +26,7 @@ import org.chromium.base.test.params.ParameterizedRunner;
import org.chromium.base.test.util.MetricsUtils;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.init.ChromeBrowserInitializer;
import org.chromium.chrome.browser.profiles.OTRProfileID;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.test.ChromeBrowserTestRule;
import org.chromium.chrome.test.ChromeJUnit4RunnerDelegate;
......@@ -38,6 +39,7 @@ import org.chromium.content_public.browser.test.util.WebContentsUtils;
import org.chromium.net.test.EmbeddedTestServer;
import java.util.Arrays;
import java.util.concurrent.Callable;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
......@@ -47,12 +49,21 @@ import java.util.concurrent.atomic.AtomicReference;
@RunWith(ParameterizedRunner.class)
@UseRunnerDelegate(ChromeJUnit4RunnerDelegate.class)
public class WarmupManagerTest {
public enum ProfileType { REGULAR_PROFILE, PRIMARY_OTR_PROFILE, NON_PRIMARY_OTR_PROFILE }
/** Provides parameter for testPreconnect to run it with both regular and incognito profiles.*/
public static class IncognitoParamsForProfile implements ParameterProvider {
public static class ProfileParams implements ParameterProvider {
@Override
public Iterable<ParameterSet> getParameters() {
return Arrays.asList(new ParameterSet().value(true).name("IncognitoProfile"),
new ParameterSet().value(false).name("RegularProfile"));
return Arrays.asList(new ParameterSet()
.value(ProfileType.PRIMARY_OTR_PROFILE.toString())
.name("PrimaryIncognitoProfile"),
new ParameterSet()
.value(ProfileType.NON_PRIMARY_OTR_PROFILE.toString())
.name("NonPrimaryIncognitoProfile"),
new ParameterSet()
.value(ProfileType.REGULAR_PROFILE.toString())
.name("RegularProfile"));
}
}
......@@ -78,6 +89,35 @@ public class WarmupManagerTest {
TestThreadUtils.runOnUiThreadBlocking(() -> mWarmupManager.destroySpareWebContents());
}
private static Profile getNonPrimaryOTRProfile() {
return TestThreadUtils.runOnUiThreadBlockingNoException((Callable<Profile>) () -> {
OTRProfileID otrProfileID = OTRProfileID.createUnique("CCT:Incognito");
return Profile.getLastUsedRegularProfile().getOffTheRecordProfile(otrProfileID);
});
}
private static Profile getPrimaryOTRProfile() {
return TestThreadUtils.runOnUiThreadBlockingNoException(
(Callable<Profile>) ()
-> Profile.getLastUsedRegularProfile().getPrimaryOTRProfile());
}
private static Profile getRegularProfile() {
return TestThreadUtils.runOnUiThreadBlockingNoException(
(Callable<Profile>) () -> Profile.getLastUsedRegularProfile());
}
private static Profile getProfile(ProfileType profileType) {
switch (profileType) {
case NON_PRIMARY_OTR_PROFILE:
return getNonPrimaryOTRProfile();
case PRIMARY_OTR_PROFILE:
return getPrimaryOTRProfile();
default:
return getRegularProfile();
}
}
@Test
@SmallTest
public void testCreateAndTakeSpareRenderer() {
......@@ -213,13 +253,16 @@ public class WarmupManagerTest {
/**
* Tests that pre-connects can be initiated from the Java side.
*
* @param isIncognito Boolean to use regular or incognito profile for pre-connect.
* @param profileParameter String value to indicate which profile to use for pre-connect. This
* is passed by {@link ProfileParams}.
* @throws InterruptedException May come from tryAcquire method call.
*/
@Test
@SmallTest
@UseMethodParameter(IncognitoParamsForProfile.class)
public void testPreconnect(boolean isIncognito) throws InterruptedException {
@UseMethodParameter(ProfileParams.class)
public void testPreconnect(String profileParameter) throws InterruptedException {
ProfileType profileType = ProfileType.valueOf(profileParameter);
Profile profile = getProfile(profileType);
EmbeddedTestServer server = new EmbeddedTestServer();
try {
// The predictor prepares 2 connections when asked to preconnect. Initializes the
......@@ -240,20 +283,15 @@ public class WarmupManagerTest {
final String url = server.getURL("/hello_world.html");
PostTask.runOrPostTask(UiThreadTaskTraits.DEFAULT,
()
-> mWarmupManager.maybePreconnectUrlAndSubResources(isIncognito
? Profile.getLastUsedRegularProfile()
.getOffTheRecordProfile()
: Profile.getLastUsedRegularProfile(),
url));
() -> { mWarmupManager.maybePreconnectUrlAndSubResources(profile, url); });
boolean isAcquired = connectionsSemaphore.tryAcquire(5, TimeUnit.SECONDS);
if (!isIncognito && !isAcquired) {
if (profileType == ProfileType.REGULAR_PROFILE && !isAcquired) {
// Starts at -1.
int actualConnections = connectionsSemaphore.availablePermits() + 1;
Assert.fail("Pre-connect failed for regular profile: Expected 2 connections, got "
+ actualConnections);
} else if (isIncognito && isAcquired) {
Assert.fail("Pre-connect should fail for incognito profile.");
} else if (profileType != ProfileType.REGULAR_PROFILE && isAcquired) {
Assert.fail("Pre-connect should fail for incognito profiles.");
}
} finally {
server.stopAndDestroyServer();
......
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