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

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

Issue 2766393004: Convert most of the rest of instrumentation tests in content (Closed)
Patch Set: Keep @CommenLineFlags Created 3 years, 9 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 ab4af328b92e8b71105f3121e4c0d0f537a29e53..0c83aab9ddcf98be64862d360aa653502d0a6071 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,14 +14,21 @@ 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.library_loader.LibraryLoader;
import org.chromium.base.library_loader.LibraryProcessType;
+import org.chromium.base.library_loader.ProcessInitException;
import org.chromium.base.process_launcher.ChildProcessCreationParams;
import org.chromium.base.process_launcher.FileDescriptorInfo;
+import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.base.test.util.Feature;
import org.chromium.content.browser.test.util.Criteria;
@@ -34,7 +41,8 @@ import java.util.concurrent.Callable;
/**
* Instrumentation tests for ChildProcessLauncher.
*/
-public class ChildProcessLauncherTest extends InstrumentationTestCase {
+@RunWith(BaseJUnit4ClassRunner.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.
@@ -44,26 +52,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"})
@CommandLineFlags.Add(ChildProcessLauncher.SWITCH_NUM_SANDBOXED_SERVICES_FOR_TESTING + "=4")
- public void testServiceFailedToBind() {
- assertEquals(0, allocatedChromeSandboxedConnectionsCount());
- assertEquals(0, ChildProcessLauncher.connectedServicesCountForTesting());
+ public void testServiceFailedToBind() throws Exception {
+ 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()));
@@ -85,23 +93,25 @@ public class ChildProcessLauncherTest extends InstrumentationTestCase {
/**
* Tests cleanup for a connection that terminates before setup.
+ * @throws ProcessInitException
*/
+ @Test
@MediumTest
@Feature({"ProcessManagement"})
- public void testServiceCrashedBeforeSetup() throws RemoteException {
- assertEquals(0, allocatedChromeSandboxedConnectionsCount());
- assertEquals(0, ChildProcessLauncher.connectedServicesCountForTesting());
+ public void testServiceCrashedBeforeSetup() throws RemoteException, ProcessInitException {
+ 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.
- assertTrue(connection.crashServiceForTesting());
+ Assert.assertTrue(connection.crashServiceForTesting());
// Verify that the connection gets cleaned-up.
CriteriaHelper.pollInstrumentationThread(Criteria.equals(0, new Callable<Integer>() {
@@ -121,15 +131,17 @@ public class ChildProcessLauncherTest extends InstrumentationTestCase {
/**
* Tests cleanup for a connection that terminates after setup.
+ * @throws ProcessInitException
*/
+ @Test
@MediumTest
@Feature({"ProcessManagement"})
- public void testServiceCrashedAfterSetup() throws RemoteException {
- assertEquals(0, allocatedChromeSandboxedConnectionsCount());
+ public void testServiceCrashedAfterSetup() throws RemoteException, ProcessInitException {
+ 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);
@@ -151,7 +163,7 @@ public class ChildProcessLauncherTest extends InstrumentationTestCase {
});
// Crash the service.
- assertTrue(connection.crashServiceForTesting());
+ Assert.assertTrue(connection.crashServiceForTesting());
// Verify that the connection gets cleaned-up.
CriteriaHelper.pollInstrumentationThread(Criteria.equals(0, new Callable<Integer>() {
@@ -169,21 +181,23 @@ 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.
+ * @throws ProcessInitException
*/
+ @Test
@MediumTest
@Feature({"ProcessManagement"})
- public void testPendingSpawnQueue() throws RemoteException {
- final Context appContext = getInstrumentation().getTargetContext();
- assertEquals(0, allocatedChromeSandboxedConnectionsCount());
+ public void testPendingSpawnQueue() throws RemoteException, ProcessInitException {
+ 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.
@@ -191,8 +205,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);
@@ -215,7 +230,7 @@ public class ChildProcessLauncherTest extends InstrumentationTestCase {
});
// Crash the service.
- assertTrue(connection.crashServiceForTesting());
+ Assert.assertTrue(connection.crashServiceForTesting());
// Verify that a new service is started for the pending spawn.
CriteriaHelper.pollInstrumentationThread(Criteria.equals(0, new Callable<Integer>() {
@@ -246,17 +261,20 @@ public class ChildProcessLauncherTest extends InstrumentationTestCase {
/**
* Tests service number of connections for external APKs and regular tabs are assigned properly,
* i.e. from different ChildConnectionAllocators.
+ * @throws ProcessInitException
*/
+ @Test
@MediumTest
@Feature({"ProcessManagement"})
@CommandLineFlags.Add({ChildProcessLauncher.SWITCH_NUM_SANDBOXED_SERVICES_FOR_TESTING + "=4",
ChildProcessLauncher.SWITCH_SANDBOXED_SERVICES_NAME_FOR_TESTING + "="
- + DEFAULT_SANDBOXED_PROCESS_SERVICE})
- public void testServiceNumberAllocation() {
- Context appContext = getInstrumentation().getTargetContext();
- assertEquals(0, ChildProcessLauncher.allocatedSandboxedConnectionsCountForTesting(
- appContext, EXTERNAL_APK_PACKAGE_NAME));
- assertEquals(0, allocatedChromeSandboxedConnectionsCount());
+ + DEFAULT_SANDBOXED_PROCESS_SERVICE})
+ public void testServiceNumberAllocation() throws ProcessInitException {
+ 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 =
@@ -266,45 +284,49 @@ 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());
}
/**
* Tests that after reaching the maximum allowed connections for an external APK, we can't
* allocate a new connection to the APK, but we can still allocate a connection for a regular
* tab.
+ * @throws ProcessInitException
*/
+ @Test
@MediumTest
@Feature({"ProcessManagement"})
@CommandLineFlags.Add({ChildProcessLauncher.SWITCH_NUM_SANDBOXED_SERVICES_FOR_TESTING + "=1",
ChildProcessLauncher.SWITCH_SANDBOXED_SERVICES_NAME_FOR_TESTING + "="
- + DEFAULT_SANDBOXED_PROCESS_SERVICE})
- public void testExceedMaximumConnectionNumber() {
- Context appContext = getInstrumentation().getTargetContext();
- assertEquals(0, ChildProcessLauncher.allocatedSandboxedConnectionsCountForTesting(
- appContext, EXTERNAL_APK_PACKAGE_NAME));
+ + DEFAULT_SANDBOXED_PROCESS_SERVICE})
+ public void testExceedMaximumConnectionNumber() throws ProcessInitException {
+ 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);
}
/**
@@ -313,11 +335,14 @@ public class ChildProcessLauncherTest extends InstrumentationTestCase {
* ContentShell.apk as a separate android:process to bind the first (slot 0) service. The
* instrumentation test then tries to bind the same slot, which fails, so the
* ChildProcessLauncher retries on a new connection.
+ * @throws ProcessInitException
*/
+ @Test
@MediumTest
@Feature({"ProcessManagement"})
- public void testBindServiceFromMultipleProcesses() throws RemoteException {
- final Context context = getInstrumentation().getTargetContext();
+ public void testBindServiceFromMultipleProcesses()
+ throws RemoteException, ProcessInitException {
+ final Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
// Start the Helper service.
class HelperConnection implements ServiceConnection {
@@ -336,7 +361,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 +372,7 @@ public class ChildProcessLauncherTest extends InstrumentationTestCase {
}
});
- assertNotNull(serviceConn.mMessenger);
+ Assert.assertNotNull(serviceConn.mMessenger);
class ReplyHandler implements Handler.Callback {
Message mMessage;
@@ -378,13 +403,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.
@@ -400,7 +426,7 @@ public class ChildProcessLauncherTest extends InstrumentationTestCase {
}
});
- assertEquals(0, conn.getServiceNumber());
+ Assert.assertEquals(0, conn.getServiceNumber());
final ChildProcessConnection[] sandboxedConnections =
ChildProcessLauncher.getSandboxedConnectionArrayForTesting(
@@ -424,47 +450,50 @@ 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());
- assertTrue(retryConn.getPid() > 0);
- assertTrue(retryConn.getPid() != helperConnPid);
- assertTrue(retryConn.getService().bindToCaller());
+ Assert.assertEquals(1, retryConn.getServiceNumber());
+ Assert.assertTrue(retryConn.getPid() > 0);
+ Assert.assertTrue(retryConn.getPid() != helperConnPid);
+ Assert.assertTrue(retryConn.getService().bindToCaller());
}
+ @Test
@MediumTest
@Feature({"ProcessManagement"})
- public void testWarmUp() {
- Context context = getInstrumentation().getTargetContext();
+ public void testWarmUp() throws ProcessInitException {
+ Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
ChildProcessLauncher.warmUp(context); // Not on UI thread.
- assertEquals(1, allocatedChromeSandboxedConnectionsCount());
+ Assert.assertEquals(1, allocatedChromeSandboxedConnectionsCount());
final ChildProcessConnection conn = ChildProcessLauncher.startForTesting(
context, new String[0], new FileDescriptorInfo[0], null);
- assertEquals(1, allocatedChromeSandboxedConnectionsCount()); // Used warmup connection.
+ Assert.assertEquals(
+ 1, allocatedChromeSandboxedConnectionsCount()); // Used warmup connection.
ChildProcessLauncher.stop(conn.getPid());
}
+ @Test
@MediumTest
@Feature({"ProcessManagement"})
- public void testCustomCreationParamDoesNotReuseWarmupConnection() {
+ public void testCustomCreationParamDoesNotReuseWarmupConnection() throws ProcessInitException {
// Since warmUp only uses default params.
- Context context = getInstrumentation().getTargetContext();
+ Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
// Check uses object identity, having the params match exactly is fine.
ChildProcessCreationParams.registerDefault(
getDefaultChildProcessCreationParams(context.getPackageName()));
@@ -472,21 +501,21 @@ public class ChildProcessLauncherTest extends InstrumentationTestCase {
getDefaultChildProcessCreationParams(context.getPackageName()));
ChildProcessLauncher.warmUp(context); // Not on UI thread.
- 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);
}
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()));
@@ -517,7 +546,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));
}
@@ -526,7 +555,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