1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.content.browser;
7 import android.content.Context;
8 import android.os.RemoteException;
9 import android.test.InstrumentationTestCase;
10 import android.test.suitebuilder.annotation.MediumTest;
12 import org.chromium.base.library_loader.LibraryLoader;
13 import org.chromium.base.test.util.Feature;
14 import org.chromium.content.browser.test.util.Criteria;
15 import org.chromium.content.browser.test.util.CriteriaHelper;
18 * Instrumentation tests for ChildProcessLauncher.
20 public class ChildProcessLauncherTest extends InstrumentationTestCase {
22 * Tests cleanup for a connection that fails to connect in the first place.
25 @Feature({"ProcessManagement"})
26 public void testServiceFailedToBind() throws InterruptedException, RemoteException {
27 assertEquals(0, ChildProcessLauncher.allocatedConnectionsCountForTesting());
28 assertEquals(0, ChildProcessLauncher.connectedServicesCountForTesting());
30 // Try to allocate a connection to service class in incorrect package. We can do that by
31 // using the instrumentation context (getContext()) instead of the app context
32 // (getTargetContext()).
33 Context context = getInstrumentation().getContext();
34 ChildProcessLauncher.allocateBoundConnectionForTesting(context);
36 // Verify that the connection is not considered as allocated.
37 assertEquals(0, ChildProcessLauncher.allocatedConnectionsCountForTesting());
38 assertEquals(0, ChildProcessLauncher.connectedServicesCountForTesting());
42 * Tests cleanup for a connection that terminates before setup.
45 @Feature({"ProcessManagement"})
46 public void testServiceCrashedBeforeSetup() throws InterruptedException, RemoteException {
47 assertEquals(0, ChildProcessLauncher.allocatedConnectionsCountForTesting());
48 assertEquals(0, ChildProcessLauncher.connectedServicesCountForTesting());
50 // Start and connect to a new service.
51 final ChildProcessConnectionImpl connection = startConnection();
52 assertEquals(1, ChildProcessLauncher.allocatedConnectionsCountForTesting());
54 // Verify that the service is not yet set up.
55 assertEquals(0, connection.getPid());
56 assertEquals(0, ChildProcessLauncher.connectedServicesCountForTesting());
59 assertTrue(connection.crashServiceForTesting());
61 // Verify that the connection gets cleaned-up.
62 assertTrue("Crashed connection wasn't released from the allocator.",
63 CriteriaHelper.pollForCriteria(new Criteria() {
65 public boolean isSatisfied() {
66 return ChildProcessLauncher.allocatedConnectionsCountForTesting() == 0;
70 assertTrue("Crashed connection wasn't released from ChildProcessLauncher.",
71 CriteriaHelper.pollForCriteria(new Criteria() {
73 public boolean isSatisfied() {
74 return ChildProcessLauncher.connectedServicesCountForTesting() == 0;
80 * Tests cleanup for a connection that terminates after setup.
83 @Feature({"ProcessManagement"})
84 public void testServiceCrashedAfterSetup() throws InterruptedException, RemoteException {
85 assertEquals(0, ChildProcessLauncher.allocatedConnectionsCountForTesting());
87 // Start and connect to a new service.
88 final ChildProcessConnectionImpl connection = startConnection();
89 assertEquals(1, ChildProcessLauncher.allocatedConnectionsCountForTesting());
91 // Initiate the connection setup.
92 ChildProcessLauncher.triggerConnectionSetup(connection, new String[0], 1,
93 new FileDescriptorInfo[0], ChildProcessLauncher.CALLBACK_FOR_RENDERER_PROCESS, 0);
95 // Verify that the connection completes the setup.
96 assertTrue("The connection wasn't registered in ChildProcessLauncher after setup.",
97 CriteriaHelper.pollForCriteria(new Criteria() {
99 public boolean isSatisfied() {
100 return ChildProcessLauncher.connectedServicesCountForTesting() == 1;
104 assertTrue("The connection failed to get a pid in setup.",
105 CriteriaHelper.pollForCriteria(new Criteria() {
107 public boolean isSatisfied() {
108 return connection.getPid() != 0;
112 // Crash the service.
113 assertTrue(connection.crashServiceForTesting());
115 // Verify that the connection gets cleaned-up.
116 assertTrue("Crashed connection wasn't released from the allocator.",
117 CriteriaHelper.pollForCriteria(new Criteria() {
119 public boolean isSatisfied() {
120 return ChildProcessLauncher.allocatedConnectionsCountForTesting() == 0;
124 assertTrue("Crashed connection wasn't released from ChildProcessLauncher.",
125 CriteriaHelper.pollForCriteria(new Criteria() {
127 public boolean isSatisfied() {
128 return ChildProcessLauncher.connectedServicesCountForTesting() == 0;
132 // Verify that the connection pid remains set after termination.
133 assertTrue(connection.getPid() != 0);
136 private ChildProcessConnectionImpl startConnection() throws InterruptedException {
137 // Allocate a new connection.
138 Context context = getInstrumentation().getTargetContext();
139 final ChildProcessConnectionImpl connection = (ChildProcessConnectionImpl)
140 ChildProcessLauncher.allocateBoundConnectionForTesting(context);
142 // Wait for the service to connect.
143 assertTrue("The connection wasn't established.",
144 CriteriaHelper.pollForCriteria(new Criteria() {
146 public boolean isSatisfied() {
147 return connection.isConnected();
154 protected void setUp() throws Exception {
156 LibraryLoader.ensureInitialized();