| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.content.browser; | 5 package org.chromium.content.browser; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.os.RemoteException; | 8 import android.os.RemoteException; |
| 9 import android.test.InstrumentationTestCase; | 9 import android.test.InstrumentationTestCase; |
| 10 import android.test.suitebuilder.annotation.MediumTest; | 10 import android.test.suitebuilder.annotation.MediumTest; |
| 11 | 11 |
| 12 import org.chromium.base.BaseSwitches; | 12 import org.chromium.base.BaseSwitches; |
| 13 import org.chromium.base.library_loader.LibraryLoader; | 13 import org.chromium.base.library_loader.LibraryLoader; |
| 14 import org.chromium.base.library_loader.LibraryProcessType; | 14 import org.chromium.base.library_loader.LibraryProcessType; |
| 15 import org.chromium.base.test.util.CommandLineFlags; |
| 15 import org.chromium.base.test.util.Feature; | 16 import org.chromium.base.test.util.Feature; |
| 16 import org.chromium.content.browser.test.util.Criteria; | 17 import org.chromium.content.browser.test.util.Criteria; |
| 17 import org.chromium.content.browser.test.util.CriteriaHelper; | 18 import org.chromium.content.browser.test.util.CriteriaHelper; |
| 18 | 19 |
| 19 import java.util.concurrent.Callable; | 20 import java.util.concurrent.Callable; |
| 20 | 21 |
| 21 /** | 22 /** |
| 22 * Instrumentation tests for ChildProcessLauncher. | 23 * Instrumentation tests for ChildProcessLauncher. |
| 24 * TODO(hanxi): Add tests for assigning {@ChildConnectionAllocator} for differen
t package names |
| 25 * when render processes can be run in WebAPKs. |
| 23 */ | 26 */ |
| 24 public class ChildProcessLauncherTest extends InstrumentationTestCase { | 27 public class ChildProcessLauncherTest extends InstrumentationTestCase { |
| 25 // Pseudo command line arguments to instruct the child process to wait until
being killed. | 28 // Pseudo command line arguments to instruct the child process to wait until
being killed. |
| 26 // Allowing the process to continue would lead to a crash when attempting to
initialize IPC | 29 // Allowing the process to continue would lead to a crash when attempting to
initialize IPC |
| 27 // channels that are not being set up in this test. | 30 // channels that are not being set up in this test. |
| 28 private static final String[] sProcessWaitArguments = { | 31 private static final String[] sProcessWaitArguments = { |
| 29 "_", "--" + BaseSwitches.RENDERER_WAIT_FOR_JAVA_DEBUGGER }; | 32 "_", "--" + BaseSwitches.RENDERER_WAIT_FOR_JAVA_DEBUGGER }; |
| 30 | 33 |
| 31 /** | 34 /** |
| 32 * Tests cleanup for a connection that fails to connect in the first place. | 35 * Tests cleanup for a connection that fails to connect in the first place. |
| 33 */ | 36 */ |
| 34 @MediumTest | 37 @MediumTest |
| 35 @Feature({"ProcessManagement"}) | 38 @Feature({"ProcessManagement"}) |
| 39 @CommandLineFlags.Add(ChildProcessLauncher.SWITCH_NUM_SANDBOXED_SERVICES_FOR
_TESTING + "=4") |
| 36 public void testServiceFailedToBind() throws InterruptedException, RemoteExc
eption { | 40 public void testServiceFailedToBind() throws InterruptedException, RemoteExc
eption { |
| 37 final Context appContext = getInstrumentation().getTargetContext(); | 41 assertEquals(0, allocatedChromeSandboxedConnectionsCount()); |
| 38 assertEquals(0, ChildProcessLauncher.allocatedConnectionsCountForTesting
(appContext)); | |
| 39 assertEquals(0, ChildProcessLauncher.connectedServicesCountForTesting())
; | 42 assertEquals(0, ChildProcessLauncher.connectedServicesCountForTesting())
; |
| 40 | 43 |
| 41 // Try to allocate a connection to service class in incorrect package. W
e can do that by | 44 // Try to allocate a connection to service class in incorrect package. W
e can do that by |
| 42 // using the instrumentation context (getContext()) instead of the app c
ontext | 45 // using the instrumentation context (getContext()) instead of the app c
ontext |
| 43 // (getTargetContext()). | 46 // (getTargetContext()). |
| 44 Context context = getInstrumentation().getContext(); | 47 Context context = getInstrumentation().getContext(); |
| 45 ChildProcessLauncher.allocateBoundConnectionForTesting(context); | 48 ChildProcessLauncher.allocateBoundConnectionForTesting( |
| 49 context, new ChildProcessCreationParams(context.getPackageName()
, 0, |
| 50 LibraryProcessType.PROCESS_CHILD)); |
| 46 | 51 |
| 47 // Verify that the connection is not considered as allocated. | 52 // Verify that the connection is not considered as allocated. |
| 48 CriteriaHelper.pollInstrumentationThread(Criteria.equals(0, new Callable
<Integer>() { | 53 CriteriaHelper.pollInstrumentationThread(Criteria.equals(0, new Callable
<Integer>() { |
| 49 @Override | 54 @Override |
| 50 public Integer call() { | 55 public Integer call() { |
| 51 return ChildProcessLauncher.allocatedConnectionsCountForTesting( | 56 return allocatedChromeSandboxedConnectionsCount(); |
| 52 appContext); | |
| 53 } | 57 } |
| 54 })); | 58 })); |
| 55 | 59 |
| 56 CriteriaHelper.pollInstrumentationThread(Criteria.equals(0, new Callable
<Integer>() { | 60 CriteriaHelper.pollInstrumentationThread(Criteria.equals(0, new Callable
<Integer>() { |
| 57 @Override | 61 @Override |
| 58 public Integer call() { | 62 public Integer call() { |
| 59 return ChildProcessLauncher.connectedServicesCountForTesting(); | 63 return ChildProcessLauncher.connectedServicesCountForTesting(); |
| 60 } | 64 } |
| 61 })); | 65 })); |
| 62 } | 66 } |
| 63 | 67 |
| 64 /** | 68 /** |
| 65 * Tests cleanup for a connection that terminates before setup. | 69 * Tests cleanup for a connection that terminates before setup. |
| 66 */ | 70 */ |
| 67 @MediumTest | 71 @MediumTest |
| 68 @Feature({"ProcessManagement"}) | 72 @Feature({"ProcessManagement"}) |
| 69 public void testServiceCrashedBeforeSetup() throws InterruptedException, Rem
oteException { | 73 public void testServiceCrashedBeforeSetup() throws InterruptedException, Rem
oteException { |
| 70 final Context appContext = getInstrumentation().getTargetContext(); | 74 assertEquals(0, allocatedChromeSandboxedConnectionsCount()); |
| 71 assertEquals(0, ChildProcessLauncher.allocatedConnectionsCountForTesting
(appContext)); | |
| 72 assertEquals(0, ChildProcessLauncher.connectedServicesCountForTesting())
; | 75 assertEquals(0, ChildProcessLauncher.connectedServicesCountForTesting())
; |
| 73 | 76 |
| 74 // Start and connect to a new service. | 77 // Start and connect to a new service. |
| 75 final ChildProcessConnectionImpl connection = startConnection(); | 78 final ChildProcessConnectionImpl connection = startConnection(); |
| 76 assertEquals(1, ChildProcessLauncher.allocatedConnectionsCountForTesting
(appContext)); | 79 assertEquals(1, allocatedChromeSandboxedConnectionsCount()); |
| 77 | 80 |
| 78 // Verify that the service is not yet set up. | 81 // Verify that the service is not yet set up. |
| 79 assertEquals(0, connection.getPid()); | 82 assertEquals(0, connection.getPid()); |
| 80 assertEquals(0, ChildProcessLauncher.connectedServicesCountForTesting())
; | 83 assertEquals(0, ChildProcessLauncher.connectedServicesCountForTesting())
; |
| 81 | 84 |
| 82 // Crash the service. | 85 // Crash the service. |
| 83 assertTrue(connection.crashServiceForTesting()); | 86 assertTrue(connection.crashServiceForTesting()); |
| 84 | 87 |
| 85 // Verify that the connection gets cleaned-up. | 88 // Verify that the connection gets cleaned-up. |
| 86 CriteriaHelper.pollInstrumentationThread(Criteria.equals(0, new Callable
<Integer>() { | 89 CriteriaHelper.pollInstrumentationThread(Criteria.equals(0, new Callable
<Integer>() { |
| 87 @Override | 90 @Override |
| 88 public Integer call() { | 91 public Integer call() { |
| 89 return ChildProcessLauncher.allocatedConnectionsCountForTesting( | 92 return allocatedChromeSandboxedConnectionsCount(); |
| 90 appContext); | |
| 91 } | 93 } |
| 92 })); | 94 })); |
| 93 | 95 |
| 94 CriteriaHelper.pollInstrumentationThread(Criteria.equals(0, new Callable
<Integer>() { | 96 CriteriaHelper.pollInstrumentationThread(Criteria.equals(0, new Callable
<Integer>() { |
| 95 @Override | 97 @Override |
| 96 public Integer call() { | 98 public Integer call() { |
| 97 return ChildProcessLauncher.connectedServicesCountForTesting(); | 99 return ChildProcessLauncher.connectedServicesCountForTesting(); |
| 98 } | 100 } |
| 99 })); | 101 })); |
| 100 } | 102 } |
| 101 | 103 |
| 102 /** | 104 /** |
| 103 * Tests cleanup for a connection that terminates after setup. | 105 * Tests cleanup for a connection that terminates after setup. |
| 104 */ | 106 */ |
| 105 @MediumTest | 107 @MediumTest |
| 106 @Feature({"ProcessManagement"}) | 108 @Feature({"ProcessManagement"}) |
| 107 public void testServiceCrashedAfterSetup() throws InterruptedException, Remo
teException { | 109 public void testServiceCrashedAfterSetup() throws InterruptedException, Remo
teException { |
| 108 final Context appContext = getInstrumentation().getTargetContext(); | 110 assertEquals(0, allocatedChromeSandboxedConnectionsCount()); |
| 109 assertEquals(0, ChildProcessLauncher.allocatedConnectionsCountForTesting
(appContext)); | |
| 110 | 111 |
| 111 // Start and connect to a new service. | 112 // Start and connect to a new service. |
| 112 final ChildProcessConnectionImpl connection = startConnection(); | 113 final ChildProcessConnectionImpl connection = startConnection(); |
| 113 assertEquals(1, ChildProcessLauncher.allocatedConnectionsCountForTesting
(appContext)); | 114 assertEquals(1, allocatedChromeSandboxedConnectionsCount()); |
| 114 | 115 |
| 115 // Initiate the connection setup. | 116 // Initiate the connection setup. |
| 116 triggerConnectionSetup(connection); | 117 triggerConnectionSetup(connection); |
| 117 | 118 |
| 118 // Verify that the connection completes the setup. | 119 // Verify that the connection completes the setup. |
| 119 CriteriaHelper.pollInstrumentationThread(Criteria.equals(1, new Callable
<Integer>() { | 120 CriteriaHelper.pollInstrumentationThread(Criteria.equals(1, new Callable
<Integer>() { |
| 120 @Override | 121 @Override |
| 121 public Integer call() { | 122 public Integer call() { |
| 122 return ChildProcessLauncher.connectedServicesCountForTesting(); | 123 return ChildProcessLauncher.connectedServicesCountForTesting(); |
| 123 } | 124 } |
| 124 })); | 125 })); |
| 125 | 126 |
| 126 CriteriaHelper.pollInstrumentationThread( | 127 CriteriaHelper.pollInstrumentationThread( |
| 127 new Criteria("The connection failed to get a pid in setup.") { | 128 new Criteria("The connection failed to get a pid in setup.") { |
| 128 @Override | 129 @Override |
| 129 public boolean isSatisfied() { | 130 public boolean isSatisfied() { |
| 130 return connection.getPid() != 0; | 131 return connection.getPid() != 0; |
| 131 } | 132 } |
| 132 }); | 133 }); |
| 133 | 134 |
| 134 // Crash the service. | 135 // Crash the service. |
| 135 assertTrue(connection.crashServiceForTesting()); | 136 assertTrue(connection.crashServiceForTesting()); |
| 136 | 137 |
| 137 // Verify that the connection gets cleaned-up. | 138 // Verify that the connection gets cleaned-up. |
| 138 CriteriaHelper.pollInstrumentationThread(Criteria.equals(0, new Callable
<Integer>() { | 139 CriteriaHelper.pollInstrumentationThread(Criteria.equals(0, new Callable
<Integer>() { |
| 139 @Override | 140 @Override |
| 140 public Integer call() { | 141 public Integer call() { |
| 141 return ChildProcessLauncher.allocatedConnectionsCountForTesting( | 142 return allocatedChromeSandboxedConnectionsCount(); |
| 142 appContext); | |
| 143 } | 143 } |
| 144 })); | 144 })); |
| 145 | 145 |
| 146 CriteriaHelper.pollInstrumentationThread(Criteria.equals(0, new Callable
<Integer>() { | 146 CriteriaHelper.pollInstrumentationThread(Criteria.equals(0, new Callable
<Integer>() { |
| 147 @Override | 147 @Override |
| 148 public Integer call() { | 148 public Integer call() { |
| 149 return ChildProcessLauncher.connectedServicesCountForTesting(); | 149 return ChildProcessLauncher.connectedServicesCountForTesting(); |
| 150 } | 150 } |
| 151 })); | 151 })); |
| 152 | 152 |
| 153 // Verify that the connection pid remains set after termination. | 153 // Verify that the connection pid remains set after termination. |
| 154 assertTrue(connection.getPid() != 0); | 154 assertTrue(connection.getPid() != 0); |
| 155 } | 155 } |
| 156 | 156 |
| 157 /** | 157 /** |
| 158 * Tests spawning a pending process from queue. | 158 * Tests spawning a pending process from queue. |
| 159 */ | 159 */ |
| 160 @MediumTest | 160 @MediumTest |
| 161 @Feature({"ProcessManagement"}) | 161 @Feature({"ProcessManagement"}) |
| 162 public void testPendingSpawnQueue() throws InterruptedException, RemoteExcep
tion { | 162 public void testPendingSpawnQueue() throws InterruptedException, RemoteExcep
tion { |
| 163 final Context appContext = getInstrumentation().getTargetContext(); | 163 final Context appContext = getInstrumentation().getTargetContext(); |
| 164 assertEquals(0, ChildProcessLauncher.allocatedConnectionsCountForTesting
(appContext)); | 164 assertEquals(0, allocatedChromeSandboxedConnectionsCount()); |
| 165 | 165 |
| 166 // Start and connect to a new service. | 166 // Start and connect to a new service. |
| 167 final ChildProcessConnectionImpl connection = startConnection(); | 167 final ChildProcessConnectionImpl connection = startConnection(); |
| 168 assertEquals(1, ChildProcessLauncher.allocatedConnectionsCountForTesting
(appContext)); | 168 assertEquals(1, allocatedChromeSandboxedConnectionsCount()); |
| 169 | 169 |
| 170 // Queue up a new spawn request. There is no way to kill the pending con
nection, leak it | 170 // Queue up a new spawn request. There is no way to kill the pending con
nection, leak it |
| 171 // until the browser restart. | 171 // until the browser restart. |
| 172 ChildProcessLauncher.enqueuePendingSpawnForTesting(appContext, sProcessW
aitArguments); | 172 ChildProcessLauncher.enqueuePendingSpawnForTesting(appContext, sProcessW
aitArguments); |
| 173 assertEquals(1, ChildProcessLauncher.pendingSpawnsCountForTesting()); | 173 assertEquals(1, ChildProcessLauncher.pendingSpawnsCountForTesting()); |
| 174 | 174 |
| 175 // Initiate the connection setup. | 175 // Initiate the connection setup. |
| 176 triggerConnectionSetup(connection); | 176 triggerConnectionSetup(connection); |
| 177 | 177 |
| 178 // Verify that the connection completes the setup. | 178 // Verify that the connection completes the setup. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 200 @Override | 200 @Override |
| 201 public Integer call() { | 201 public Integer call() { |
| 202 return ChildProcessLauncher.pendingSpawnsCountForTesting(); | 202 return ChildProcessLauncher.pendingSpawnsCountForTesting(); |
| 203 } | 203 } |
| 204 })); | 204 })); |
| 205 | 205 |
| 206 CriteriaHelper.pollInstrumentationThread( | 206 CriteriaHelper.pollInstrumentationThread( |
| 207 Criteria.equals(1, new Callable<Integer>() { | 207 Criteria.equals(1, new Callable<Integer>() { |
| 208 @Override | 208 @Override |
| 209 public Integer call() { | 209 public Integer call() { |
| 210 return ChildProcessLauncher.allocatedConnectionsCountFor
Testing( | 210 return allocatedChromeSandboxedConnectionsCount(); |
| 211 appContext); | |
| 212 } | 211 } |
| 213 })); | 212 })); |
| 214 | 213 |
| 215 // Verify that the connection completes the setup for the pending spawn. | 214 // Verify that the connection completes the setup for the pending spawn. |
| 216 CriteriaHelper.pollInstrumentationThread(Criteria.equals(1, new Callable
<Integer>() { | 215 CriteriaHelper.pollInstrumentationThread(Criteria.equals(1, new Callable
<Integer>() { |
| 217 @Override | 216 @Override |
| 218 public Integer call() { | 217 public Integer call() { |
| 219 return ChildProcessLauncher.connectedServicesCountForTesting(); | 218 return ChildProcessLauncher.connectedServicesCountForTesting(); |
| 220 } | 219 } |
| 221 })); | 220 })); |
| 222 } | 221 } |
| 223 | 222 |
| 224 private ChildProcessConnectionImpl startConnection() throws InterruptedExcep
tion { | 223 private ChildProcessConnectionImpl startConnection() throws InterruptedExcep
tion { |
| 225 // Allocate a new connection. | 224 // Allocate a new connection. |
| 226 Context context = getInstrumentation().getTargetContext(); | 225 Context context = getInstrumentation().getTargetContext(); |
| 227 final ChildProcessConnectionImpl connection = (ChildProcessConnectionImp
l) | 226 final ChildProcessConnectionImpl connection = |
| 228 ChildProcessLauncher.allocateBoundConnectionForTesting(context); | 227 (ChildProcessConnectionImpl) ChildProcessLauncher.allocateBoundC
onnectionForTesting( |
| 228 context, new ChildProcessCreationParams(context.getPacka
geName(), 0, |
| 229 LibraryProcessType.PROCESS_CHILD)); |
| 229 | 230 |
| 230 // Wait for the service to connect. | 231 // Wait for the service to connect. |
| 231 CriteriaHelper.pollInstrumentationThread( | 232 CriteriaHelper.pollInstrumentationThread( |
| 232 new Criteria("The connection wasn't established.") { | 233 new Criteria("The connection wasn't established.") { |
| 233 @Override | 234 @Override |
| 234 public boolean isSatisfied() { | 235 public boolean isSatisfied() { |
| 235 return connection.isConnected(); | 236 return connection.isConnected(); |
| 236 } | 237 } |
| 237 }); | 238 }); |
| 238 return connection; | 239 return connection; |
| 239 } | 240 } |
| 240 | 241 |
| 242 /** |
| 243 * Returns the number of Chrome's sandboxed connections. |
| 244 */ |
| 245 private int allocatedChromeSandboxedConnectionsCount() { |
| 246 Context context = getInstrumentation().getTargetContext(); |
| 247 return ChildProcessLauncher.allocatedSandboxedConnectionsCountForTesting
( |
| 248 context, context.getPackageName()); |
| 249 } |
| 250 |
| 241 private void triggerConnectionSetup(ChildProcessConnectionImpl connection) { | 251 private void triggerConnectionSetup(ChildProcessConnectionImpl connection) { |
| 242 ChildProcessLauncher.triggerConnectionSetup(connection, sProcessWaitArgu
ments, 1, | 252 ChildProcessLauncher.triggerConnectionSetup(connection, sProcessWaitArgu
ments, 1, |
| 243 new FileDescriptorInfo[0], ChildProcessLauncher.CALLBACK_FOR_REN
DERER_PROCESS, 0); | 253 new FileDescriptorInfo[0], ChildProcessLauncher.CALLBACK_FOR_REN
DERER_PROCESS, 0); |
| 244 } | 254 } |
| 245 | 255 |
| 246 @Override | 256 @Override |
| 247 protected void setUp() throws Exception { | 257 protected void setUp() throws Exception { |
| 248 super.setUp(); | 258 super.setUp(); |
| 249 LibraryLoader.get(LibraryProcessType.PROCESS_CHILD) | 259 LibraryLoader.get(LibraryProcessType.PROCESS_CHILD) |
| 250 .ensureInitialized(getInstrumentation().getTargetContext()); | 260 .ensureInitialized(getInstrumentation().getTargetContext()); |
| 251 } | 261 } |
| 252 } | 262 } |
| OLD | NEW |