Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Unified Diff: content/public/android/javatests/src/org/chromium/content/browser/ChildProcessLauncherTest.java

Issue 2818643002: Support ChildProcessAllocatorSettings in JUnit4 tests (Closed)
Patch Set: add missing imports Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/public/android/javatests/src/org/chromium/content/browser/ChildProcessLauncherTest.java
diff --git a/content/public/android/javatests/src/org/chromium/content/browser/ChildProcessLauncherTest.java b/content/public/android/javatests/src/org/chromium/content/browser/ChildProcessLauncherTest.java
index 8d9b33a68c41c48a3d961993437e4aa8ab5757d6..d013171769eec5098d391d72bf64960beb55f4f2 100644
--- a/content/public/android/javatests/src/org/chromium/content/browser/ChildProcessLauncherTest.java
+++ b/content/public/android/javatests/src/org/chromium/content/browser/ChildProcessLauncherTest.java
@@ -14,8 +14,13 @@ import android.os.Looper;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
+import android.support.test.InstrumentationRegistry;
import android.support.test.filters.MediumTest;
-import android.test.InstrumentationTestCase;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
import org.chromium.base.BaseSwitches;
import org.chromium.base.ThreadUtils;
@@ -25,6 +30,7 @@ import org.chromium.base.process_launcher.ChildProcessCreationParams;
import org.chromium.base.process_launcher.FileDescriptorInfo;
import org.chromium.base.test.util.Feature;
import org.chromium.content.browser.test.ChildProcessAllocatorSettings;
+import org.chromium.content.browser.test.ContentJUnit4ClassRunner;
import org.chromium.content.browser.test.util.Criteria;
import org.chromium.content.browser.test.util.CriteriaHelper;
import org.chromium.content.common.ContentSwitches;
@@ -36,7 +42,8 @@ import java.util.concurrent.Semaphore;
/**
* Instrumentation tests for ChildProcessLauncher.
*/
-public class ChildProcessLauncherTest extends InstrumentationTestCase {
+@RunWith(ContentJUnit4ClassRunner.class)
+public class ChildProcessLauncherTest {
// Pseudo command line arguments to instruct the child process to wait until being killed.
// Allowing the process to continue would lead to a crash when attempting to initialize IPC
// channels that are not being set up in this test.
@@ -46,26 +53,26 @@ public class ChildProcessLauncherTest extends InstrumentationTestCase {
private static final String DEFAULT_SANDBOXED_PROCESS_SERVICE =
"org.chromium.content.app.SandboxedProcessService";
- @Override
- protected void setUp() throws Exception {
- super.setUp();
+ @Before
+ public void setUp() throws Exception {
LibraryLoader.get(LibraryProcessType.PROCESS_CHILD).ensureInitialized();
}
/**
* Tests cleanup for a connection that fails to connect in the first place.
*/
+ @Test
@MediumTest
@Feature({"ProcessManagement"})
@ChildProcessAllocatorSettings(sandboxedServiceCount = 4)
public void testServiceFailedToBind() {
- assertEquals(0, allocatedChromeSandboxedConnectionsCount());
- assertEquals(0, ChildProcessLauncher.connectedServicesCountForTesting());
+ Assert.assertEquals(0, allocatedChromeSandboxedConnectionsCount());
+ Assert.assertEquals(0, ChildProcessLauncher.connectedServicesCountForTesting());
// Try to allocate a connection to service class in incorrect package. We can do that by
// using the instrumentation context (getContext()) instead of the app context
// (getTargetContext()).
- Context context = getInstrumentation().getContext();
+ Context context = InstrumentationRegistry.getInstrumentation().getContext();
ChildProcessLauncher.allocateBoundConnectionForTesting(
context, getDefaultChildProcessCreationParams(context.getPackageName()));
@@ -88,19 +95,20 @@ public class ChildProcessLauncherTest extends InstrumentationTestCase {
/**
* Tests cleanup for a connection that terminates before setup.
*/
+ @Test
@MediumTest
@Feature({"ProcessManagement"})
public void testServiceCrashedBeforeSetup() throws RemoteException {
- assertEquals(0, allocatedChromeSandboxedConnectionsCount());
- assertEquals(0, ChildProcessLauncher.connectedServicesCountForTesting());
+ Assert.assertEquals(0, allocatedChromeSandboxedConnectionsCount());
+ Assert.assertEquals(0, ChildProcessLauncher.connectedServicesCountForTesting());
// Start and connect to a new service.
final ChildProcessConnectionImpl connection = startConnection();
- assertEquals(1, allocatedChromeSandboxedConnectionsCount());
+ Assert.assertEquals(1, allocatedChromeSandboxedConnectionsCount());
// Verify that the service is not yet set up.
- assertEquals(0, connection.getPid());
- assertEquals(0, ChildProcessLauncher.connectedServicesCountForTesting());
+ Assert.assertEquals(0, connection.getPid());
+ Assert.assertEquals(0, ChildProcessLauncher.connectedServicesCountForTesting());
// Crash the service.
connection.crashServiceForTesting();
@@ -124,14 +132,15 @@ public class ChildProcessLauncherTest extends InstrumentationTestCase {
/**
* Tests cleanup for a connection that terminates after setup.
*/
+ @Test
@MediumTest
@Feature({"ProcessManagement"})
public void testServiceCrashedAfterSetup() throws RemoteException {
- assertEquals(0, allocatedChromeSandboxedConnectionsCount());
+ Assert.assertEquals(0, allocatedChromeSandboxedConnectionsCount());
// Start and connect to a new service.
final ChildProcessConnectionImpl connection = startConnection();
- assertEquals(1, allocatedChromeSandboxedConnectionsCount());
+ Assert.assertEquals(1, allocatedChromeSandboxedConnectionsCount());
// Initiate the connection setup.
triggerConnectionSetup(connection);
@@ -171,21 +180,22 @@ public class ChildProcessLauncherTest extends InstrumentationTestCase {
}));
// Verify that the connection pid remains set after termination.
- assertTrue(connection.getPid() != 0);
+ Assert.assertTrue(connection.getPid() != 0);
}
/**
* Tests spawning a pending process from queue.
*/
+ @Test
@MediumTest
@Feature({"ProcessManagement"})
public void testPendingSpawnQueue() throws RemoteException {
- final Context appContext = getInstrumentation().getTargetContext();
- assertEquals(0, allocatedChromeSandboxedConnectionsCount());
+ final Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
+ Assert.assertEquals(0, allocatedChromeSandboxedConnectionsCount());
// Start and connect to a new service.
final ChildProcessConnectionImpl connection = startConnection();
- assertEquals(1, allocatedChromeSandboxedConnectionsCount());
+ Assert.assertEquals(1, allocatedChromeSandboxedConnectionsCount());
// Queue up a new spawn request. There is no way to kill the pending connection, leak it
// until the browser restart.
@@ -193,8 +203,9 @@ public class ChildProcessLauncherTest extends InstrumentationTestCase {
final boolean inSandbox = true;
ChildProcessLauncher.enqueuePendingSpawnForTesting(appContext, sProcessWaitArguments,
getDefaultChildProcessCreationParams(packageName), inSandbox);
- assertEquals(1, ChildProcessLauncher.pendingSpawnsCountForTesting(appContext, packageName,
- inSandbox));
+ Assert.assertEquals(1,
+ ChildProcessLauncher.pendingSpawnsCountForTesting(
+ appContext, packageName, inSandbox));
// Initiate the connection setup.
triggerConnectionSetup(connection);
@@ -249,15 +260,17 @@ public class ChildProcessLauncherTest extends InstrumentationTestCase {
* Tests service number of connections for external APKs and regular tabs are assigned properly,
* i.e. from different ChildConnectionAllocators.
*/
+ @Test
@MediumTest
@Feature({"ProcessManagement"})
@ChildProcessAllocatorSettings(
sandboxedServiceCount = 4, sandboxedServiceName = DEFAULT_SANDBOXED_PROCESS_SERVICE)
public void testServiceNumberAllocation() {
- Context appContext = getInstrumentation().getTargetContext();
- assertEquals(0, ChildProcessLauncher.allocatedSandboxedConnectionsCountForTesting(
- appContext, EXTERNAL_APK_PACKAGE_NAME));
- assertEquals(0, allocatedChromeSandboxedConnectionsCount());
+ Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
+ Assert.assertEquals(0,
+ ChildProcessLauncher.allocatedSandboxedConnectionsCountForTesting(
+ appContext, EXTERNAL_APK_PACKAGE_NAME));
+ Assert.assertEquals(0, allocatedChromeSandboxedConnectionsCount());
// Start and connect to a new service of an external APK.
ChildProcessConnectionImpl externalApkConnection =
@@ -267,15 +280,16 @@ public class ChildProcessLauncherTest extends InstrumentationTestCase {
// Verify that one connection is allocated for an external APK and a regular tab
// respectively.
- assertEquals(1, ChildProcessLauncher.allocatedSandboxedConnectionsCountForTesting(
- appContext, EXTERNAL_APK_PACKAGE_NAME));
- assertEquals(1, allocatedChromeSandboxedConnectionsCount());
+ Assert.assertEquals(1,
+ ChildProcessLauncher.allocatedSandboxedConnectionsCountForTesting(
+ appContext, EXTERNAL_APK_PACKAGE_NAME));
+ Assert.assertEquals(1, allocatedChromeSandboxedConnectionsCount());
// Verify that connections allocated for an external APK and the regular tab are from
// different ChildConnectionAllocators, since both ChildConnectionAllocators start
// allocating connections from number 0.
- assertEquals(0, externalApkConnection.getServiceNumber());
- assertEquals(0, tabConnection.getServiceNumber());
+ Assert.assertEquals(0, externalApkConnection.getServiceNumber());
+ Assert.assertEquals(0, tabConnection.getServiceNumber());
}
/**
@@ -283,28 +297,30 @@ public class ChildProcessLauncherTest extends InstrumentationTestCase {
* allocate a new connection to the APK, but we can still allocate a connection for a regular
* tab.
*/
+ @Test
@MediumTest
@Feature({"ProcessManagement"})
@ChildProcessAllocatorSettings(
sandboxedServiceCount = 1, sandboxedServiceName = DEFAULT_SANDBOXED_PROCESS_SERVICE)
public void testExceedMaximumConnectionNumber() {
- Context appContext = getInstrumentation().getTargetContext();
- assertEquals(0, ChildProcessLauncher.allocatedSandboxedConnectionsCountForTesting(
- appContext, EXTERNAL_APK_PACKAGE_NAME));
+ Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
+ Assert.assertEquals(0,
+ ChildProcessLauncher.allocatedSandboxedConnectionsCountForTesting(
+ appContext, EXTERNAL_APK_PACKAGE_NAME));
// Setup a connection for an external APK to reach the maximum allowed connection number.
ChildProcessConnectionImpl externalApkConnection =
allocateConnection(EXTERNAL_APK_PACKAGE_NAME);
- assertNotNull(externalApkConnection);
+ Assert.assertNotNull(externalApkConnection);
// Verify that there isn't any connection available for the external APK.
ChildProcessConnectionImpl exceedNumberExternalApkConnection =
allocateConnection(EXTERNAL_APK_PACKAGE_NAME);
- assertNull(exceedNumberExternalApkConnection);
+ Assert.assertNull(exceedNumberExternalApkConnection);
// Verify that we can still allocate connection for a regular tab.
ChildProcessConnectionImpl tabConnection = allocateConnection(appContext.getPackageName());
- assertNotNull(tabConnection);
+ Assert.assertNotNull(tabConnection);
}
/**
@@ -314,10 +330,11 @@ public class ChildProcessLauncherTest extends InstrumentationTestCase {
* instrumentation test then tries to bind the same slot, which fails, so the
* ChildProcessLauncher retries on a new connection.
*/
+ @Test
@MediumTest
@Feature({"ProcessManagement"})
public void testBindServiceFromMultipleProcesses() throws RemoteException {
- final Context context = getInstrumentation().getTargetContext();
+ final Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
// Start the Helper service.
class HelperConnection implements ServiceConnection {
@@ -336,7 +353,7 @@ public class ChildProcessLauncherTest extends InstrumentationTestCase {
Intent intent = new Intent();
intent.setComponent(new ComponentName(context.getPackageName(),
context.getPackageName() + ".ChildProcessLauncherTestHelperService"));
- assertTrue(context.bindService(intent, serviceConn, Context.BIND_AUTO_CREATE));
+ Assert.assertTrue(context.bindService(intent, serviceConn, Context.BIND_AUTO_CREATE));
// Wait for the Helper service to connect.
CriteriaHelper.pollInstrumentationThread(
@@ -347,7 +364,7 @@ public class ChildProcessLauncherTest extends InstrumentationTestCase {
}
});
- assertNotNull(serviceConn.mMessenger);
+ Assert.assertNotNull(serviceConn.mMessenger);
class ReplyHandler implements Handler.Callback {
Message mMessage;
@@ -378,13 +395,14 @@ public class ChildProcessLauncherTest extends InstrumentationTestCase {
});
// Verify that the Helper was able to launch the sandboxed service.
- assertNotNull(replyHandler.mMessage);
- assertEquals(ChildProcessLauncherTestHelperService.MSG_BIND_SERVICE_REPLY,
+ Assert.assertNotNull(replyHandler.mMessage);
+ Assert.assertEquals(ChildProcessLauncherTestHelperService.MSG_BIND_SERVICE_REPLY,
replyHandler.mMessage.what);
- assertEquals("Connection slot from helper service is not 0", 0, replyHandler.mMessage.arg2);
+ Assert.assertEquals(
+ "Connection slot from helper service is not 0", 0, replyHandler.mMessage.arg2);
final int helperConnPid = replyHandler.mMessage.arg1;
- assertTrue(helperConnPid > 0);
+ Assert.assertTrue(helperConnPid > 0);
// Launch a service from this process. Since slot 0 is already bound by the Helper, it
// will fail to start and the ChildProcessLauncher will retry.
@@ -402,7 +420,7 @@ public class ChildProcessLauncherTest extends InstrumentationTestCase {
}
});
- assertEquals(0, conn.getServiceNumber());
+ Assert.assertEquals(0, conn.getServiceNumber());
final ChildProcessConnection[] sandboxedConnections =
ChildProcessLauncher.getSandboxedConnectionArrayForTesting(
@@ -426,23 +444,23 @@ public class ChildProcessLauncherTest extends InstrumentationTestCase {
for (int i = 0; i < sandboxedConnections.length; ++i) {
ChildProcessConnection sandboxedConn = sandboxedConnections[i];
if (i <= 1) {
- assertNotNull(sandboxedConn);
- assertNotNull(sandboxedConn.getService());
+ Assert.assertNotNull(sandboxedConn);
+ Assert.assertNotNull(sandboxedConn.getService());
} else {
- assertNull(sandboxedConn);
+ Assert.assertNull(sandboxedConn);
}
}
- assertTrue(conn == sandboxedConnections[0]);
+ Assert.assertTrue(conn == sandboxedConnections[0]);
final ChildProcessConnection retryConn = sandboxedConnections[1];
- assertFalse(conn == retryConn);
+ Assert.assertFalse(conn == retryConn);
- assertEquals(0, conn.getServiceNumber());
- assertEquals(0, conn.getPid());
- assertFalse(conn.getService().bindToCaller());
+ Assert.assertEquals(0, conn.getServiceNumber());
+ Assert.assertEquals(0, conn.getPid());
+ Assert.assertFalse(conn.getService().bindToCaller());
- assertEquals(1, retryConn.getServiceNumber());
+ Assert.assertEquals(1, retryConn.getServiceNumber());
CriteriaHelper.pollInstrumentationThread(
new Criteria("Failed waiting retry connection to get pid") {
@Override
@@ -450,8 +468,8 @@ public class ChildProcessLauncherTest extends InstrumentationTestCase {
return retryConn.getPid() > 0;
}
});
- assertTrue(retryConn.getPid() != helperConnPid);
- assertTrue(retryConn.getService().bindToCaller());
+ Assert.assertTrue(retryConn.getPid() != helperConnPid);
+ Assert.assertTrue(retryConn.getService().bindToCaller());
}
private static void warmUpOnUiThreadBlocking(final Context context) {
@@ -475,19 +493,20 @@ public class ChildProcessLauncherTest extends InstrumentationTestCase {
done.acquireUninterruptibly();
}
+ @Test
@MediumTest
@Feature({"ProcessManagement"})
public void testWarmUp() {
- final Context context = getInstrumentation().getTargetContext();
+ final Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
warmUpOnUiThreadBlocking(context);
runOnLauncherThreadBlocking(new Runnable() {
@Override
public void run() {
- assertEquals(1, allocatedChromeSandboxedConnectionsCount());
+ Assert.assertEquals(1, allocatedChromeSandboxedConnectionsCount());
final ChildProcessConnection conn = ChildProcessLauncher.startForTesting(
context, new String[0], new FileDescriptorInfo[0], null);
- assertEquals(
+ Assert.assertEquals(
1, allocatedChromeSandboxedConnectionsCount()); // Used warmup connection.
ChildProcessLauncher.stop(conn.getPid());
@@ -495,11 +514,12 @@ public class ChildProcessLauncherTest extends InstrumentationTestCase {
});
}
+ @Test
@MediumTest
@Feature({"ProcessManagement"})
public void testCustomCreationParamDoesNotReuseWarmupConnection() {
// Since warmUp only uses default params.
- final Context context = getInstrumentation().getTargetContext();
+ final Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
// Check uses object identity, having the params match exactly is fine.
ChildProcessCreationParams.registerDefault(
getDefaultChildProcessCreationParams(context.getPackageName()));
@@ -510,14 +530,15 @@ public class ChildProcessLauncherTest extends InstrumentationTestCase {
runOnLauncherThreadBlocking(new Runnable() {
@Override
public void run() {
- assertEquals(1, allocatedChromeSandboxedConnectionsCount());
+ Assert.assertEquals(1, allocatedChromeSandboxedConnectionsCount());
startRendererProcess(context, paramId, new FileDescriptorInfo[0]);
- assertEquals(2, allocatedChromeSandboxedConnectionsCount()); // Warmup not used.
+ Assert.assertEquals(
+ 2, allocatedChromeSandboxedConnectionsCount()); // Warmup not used.
startRendererProcess(
context, ChildProcessCreationParams.DEFAULT_ID, new FileDescriptorInfo[0]);
- assertEquals(2, allocatedChromeSandboxedConnectionsCount()); // Warmup used.
+ Assert.assertEquals(2, allocatedChromeSandboxedConnectionsCount()); // Warmup used.
ChildProcessCreationParams.unregister(paramId);
}
@@ -526,7 +547,7 @@ public class ChildProcessLauncherTest extends InstrumentationTestCase {
private ChildProcessConnectionImpl startConnection() {
// Allocate a new connection.
- Context context = getInstrumentation().getTargetContext();
+ Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
final ChildProcessConnectionImpl connection =
(ChildProcessConnectionImpl) ChildProcessLauncher.allocateBoundConnectionForTesting(
context, getDefaultChildProcessCreationParams(context.getPackageName()));
@@ -557,7 +578,7 @@ public class ChildProcessLauncherTest extends InstrumentationTestCase {
*/
private ChildProcessConnectionImpl allocateConnection(String packageName) {
// Allocate a new connection.
- Context context = getInstrumentation().getTargetContext();
+ Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
return (ChildProcessConnectionImpl) ChildProcessLauncher.allocateConnectionForTesting(
context, getDefaultChildProcessCreationParams(packageName));
}
@@ -566,7 +587,7 @@ public class ChildProcessLauncherTest extends InstrumentationTestCase {
* Returns the number of Chrome's sandboxed connections.
*/
private int allocatedChromeSandboxedConnectionsCount() {
- Context context = getInstrumentation().getTargetContext();
+ Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
return ChildProcessLauncher.allocatedSandboxedConnectionsCountForTesting(
context, context.getPackageName());
}

Powered by Google App Engine
This is Rietveld 408576698