| 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; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 assertEquals(0, ChildProcessLauncher.allocatedConnectionsCountForTesting
(appContext)); | 38 assertEquals(0, ChildProcessLauncher.allocatedConnectionsCountForTesting
(appContext)); |
| 39 assertEquals(0, ChildProcessLauncher.connectedServicesCountForTesting())
; | 39 assertEquals(0, ChildProcessLauncher.connectedServicesCountForTesting())
; |
| 40 | 40 |
| 41 // Try to allocate a connection to service class in incorrect package. W
e can do that by | 41 // 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 | 42 // using the instrumentation context (getContext()) instead of the app c
ontext |
| 43 // (getTargetContext()). | 43 // (getTargetContext()). |
| 44 Context context = getInstrumentation().getContext(); | 44 Context context = getInstrumentation().getContext(); |
| 45 ChildProcessLauncher.allocateBoundConnectionForTesting(context); | 45 ChildProcessLauncher.allocateBoundConnectionForTesting(context); |
| 46 | 46 |
| 47 // Verify that the connection is not considered as allocated. | 47 // Verify that the connection is not considered as allocated. |
| 48 CriteriaHelper.pollForCriteria(Criteria.equals(0, new Callable<Integer>(
) { | 48 CriteriaHelper.pollInstrumentationThread(Criteria.equals(0, new Callable
<Integer>() { |
| 49 @Override | 49 @Override |
| 50 public Integer call() { | 50 public Integer call() { |
| 51 return ChildProcessLauncher.allocatedConnectionsCountForTesting( | 51 return ChildProcessLauncher.allocatedConnectionsCountForTesting( |
| 52 appContext); | 52 appContext); |
| 53 } | 53 } |
| 54 })); | 54 })); |
| 55 | 55 |
| 56 CriteriaHelper.pollForCriteria(Criteria.equals(0, new Callable<Integer>(
) { | 56 CriteriaHelper.pollInstrumentationThread(Criteria.equals(0, new Callable
<Integer>() { |
| 57 @Override | 57 @Override |
| 58 public Integer call() { | 58 public Integer call() { |
| 59 return ChildProcessLauncher.connectedServicesCountForTesting(); | 59 return ChildProcessLauncher.connectedServicesCountForTesting(); |
| 60 } | 60 } |
| 61 })); | 61 })); |
| 62 } | 62 } |
| 63 | 63 |
| 64 /** | 64 /** |
| 65 * Tests cleanup for a connection that terminates before setup. | 65 * Tests cleanup for a connection that terminates before setup. |
| 66 */ | 66 */ |
| 67 @MediumTest | 67 @MediumTest |
| 68 @Feature({"ProcessManagement"}) | 68 @Feature({"ProcessManagement"}) |
| 69 public void testServiceCrashedBeforeSetup() throws InterruptedException, Rem
oteException { | 69 public void testServiceCrashedBeforeSetup() throws InterruptedException, Rem
oteException { |
| 70 final Context appContext = getInstrumentation().getTargetContext(); | 70 final Context appContext = getInstrumentation().getTargetContext(); |
| 71 assertEquals(0, ChildProcessLauncher.allocatedConnectionsCountForTesting
(appContext)); | 71 assertEquals(0, ChildProcessLauncher.allocatedConnectionsCountForTesting
(appContext)); |
| 72 assertEquals(0, ChildProcessLauncher.connectedServicesCountForTesting())
; | 72 assertEquals(0, ChildProcessLauncher.connectedServicesCountForTesting())
; |
| 73 | 73 |
| 74 // Start and connect to a new service. | 74 // Start and connect to a new service. |
| 75 final ChildProcessConnectionImpl connection = startConnection(); | 75 final ChildProcessConnectionImpl connection = startConnection(); |
| 76 assertEquals(1, ChildProcessLauncher.allocatedConnectionsCountForTesting
(appContext)); | 76 assertEquals(1, ChildProcessLauncher.allocatedConnectionsCountForTesting
(appContext)); |
| 77 | 77 |
| 78 // Verify that the service is not yet set up. | 78 // Verify that the service is not yet set up. |
| 79 assertEquals(0, connection.getPid()); | 79 assertEquals(0, connection.getPid()); |
| 80 assertEquals(0, ChildProcessLauncher.connectedServicesCountForTesting())
; | 80 assertEquals(0, ChildProcessLauncher.connectedServicesCountForTesting())
; |
| 81 | 81 |
| 82 // Crash the service. | 82 // Crash the service. |
| 83 assertTrue(connection.crashServiceForTesting()); | 83 assertTrue(connection.crashServiceForTesting()); |
| 84 | 84 |
| 85 // Verify that the connection gets cleaned-up. | 85 // Verify that the connection gets cleaned-up. |
| 86 CriteriaHelper.pollForCriteria(Criteria.equals(0, new Callable<Integer>(
) { | 86 CriteriaHelper.pollInstrumentationThread(Criteria.equals(0, new Callable
<Integer>() { |
| 87 @Override | 87 @Override |
| 88 public Integer call() { | 88 public Integer call() { |
| 89 return ChildProcessLauncher.allocatedConnectionsCountForTesting( | 89 return ChildProcessLauncher.allocatedConnectionsCountForTesting( |
| 90 appContext); | 90 appContext); |
| 91 } | 91 } |
| 92 })); | 92 })); |
| 93 | 93 |
| 94 CriteriaHelper.pollForCriteria(Criteria.equals(0, new Callable<Integer>(
) { | 94 CriteriaHelper.pollInstrumentationThread(Criteria.equals(0, new Callable
<Integer>() { |
| 95 @Override | 95 @Override |
| 96 public Integer call() { | 96 public Integer call() { |
| 97 return ChildProcessLauncher.connectedServicesCountForTesting(); | 97 return ChildProcessLauncher.connectedServicesCountForTesting(); |
| 98 } | 98 } |
| 99 })); | 99 })); |
| 100 } | 100 } |
| 101 | 101 |
| 102 /** | 102 /** |
| 103 * Tests cleanup for a connection that terminates after setup. | 103 * Tests cleanup for a connection that terminates after setup. |
| 104 */ | 104 */ |
| 105 @MediumTest | 105 @MediumTest |
| 106 @Feature({"ProcessManagement"}) | 106 @Feature({"ProcessManagement"}) |
| 107 public void testServiceCrashedAfterSetup() throws InterruptedException, Remo
teException { | 107 public void testServiceCrashedAfterSetup() throws InterruptedException, Remo
teException { |
| 108 final Context appContext = getInstrumentation().getTargetContext(); | 108 final Context appContext = getInstrumentation().getTargetContext(); |
| 109 assertEquals(0, ChildProcessLauncher.allocatedConnectionsCountForTesting
(appContext)); | 109 assertEquals(0, ChildProcessLauncher.allocatedConnectionsCountForTesting
(appContext)); |
| 110 | 110 |
| 111 // Start and connect to a new service. | 111 // Start and connect to a new service. |
| 112 final ChildProcessConnectionImpl connection = startConnection(); | 112 final ChildProcessConnectionImpl connection = startConnection(); |
| 113 assertEquals(1, ChildProcessLauncher.allocatedConnectionsCountForTesting
(appContext)); | 113 assertEquals(1, ChildProcessLauncher.allocatedConnectionsCountForTesting
(appContext)); |
| 114 | 114 |
| 115 // Initiate the connection setup. | 115 // Initiate the connection setup. |
| 116 triggerConnectionSetup(connection); | 116 triggerConnectionSetup(connection); |
| 117 | 117 |
| 118 // Verify that the connection completes the setup. | 118 // Verify that the connection completes the setup. |
| 119 CriteriaHelper.pollForCriteria(Criteria.equals(1, new Callable<Integer>(
) { | 119 CriteriaHelper.pollInstrumentationThread(Criteria.equals(1, new Callable
<Integer>() { |
| 120 @Override | 120 @Override |
| 121 public Integer call() { | 121 public Integer call() { |
| 122 return ChildProcessLauncher.connectedServicesCountForTesting(); | 122 return ChildProcessLauncher.connectedServicesCountForTesting(); |
| 123 } | 123 } |
| 124 })); | 124 })); |
| 125 | 125 |
| 126 CriteriaHelper.pollForCriteria( | 126 CriteriaHelper.pollInstrumentationThread( |
| 127 new Criteria("The connection failed to get a pid in setup.") { | 127 new Criteria("The connection failed to get a pid in setup.") { |
| 128 @Override | 128 @Override |
| 129 public boolean isSatisfied() { | 129 public boolean isSatisfied() { |
| 130 return connection.getPid() != 0; | 130 return connection.getPid() != 0; |
| 131 } | 131 } |
| 132 }); | 132 }); |
| 133 | 133 |
| 134 // Crash the service. | 134 // Crash the service. |
| 135 assertTrue(connection.crashServiceForTesting()); | 135 assertTrue(connection.crashServiceForTesting()); |
| 136 | 136 |
| 137 // Verify that the connection gets cleaned-up. | 137 // Verify that the connection gets cleaned-up. |
| 138 CriteriaHelper.pollForCriteria(Criteria.equals(0, new Callable<Integer>(
) { | 138 CriteriaHelper.pollInstrumentationThread(Criteria.equals(0, new Callable
<Integer>() { |
| 139 @Override | 139 @Override |
| 140 public Integer call() { | 140 public Integer call() { |
| 141 return ChildProcessLauncher.allocatedConnectionsCountForTesting( | 141 return ChildProcessLauncher.allocatedConnectionsCountForTesting( |
| 142 appContext); | 142 appContext); |
| 143 } | 143 } |
| 144 })); | 144 })); |
| 145 | 145 |
| 146 CriteriaHelper.pollForCriteria(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 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 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. |
| 179 CriteriaHelper.pollForCriteria( | 179 CriteriaHelper.pollInstrumentationThread( |
| 180 Criteria.equals(1, new Callable<Integer>() { | 180 Criteria.equals(1, new Callable<Integer>() { |
| 181 @Override | 181 @Override |
| 182 public Integer call() { | 182 public Integer call() { |
| 183 return ChildProcessLauncher.connectedServicesCountForTes
ting(); | 183 return ChildProcessLauncher.connectedServicesCountForTes
ting(); |
| 184 } | 184 } |
| 185 })); | 185 })); |
| 186 | 186 |
| 187 CriteriaHelper.pollForCriteria( | 187 CriteriaHelper.pollInstrumentationThread( |
| 188 new Criteria("The connection failed to get a pid in setup.") { | 188 new Criteria("The connection failed to get a pid in setup.") { |
| 189 @Override | 189 @Override |
| 190 public boolean isSatisfied() { | 190 public boolean isSatisfied() { |
| 191 return connection.getPid() != 0; | 191 return connection.getPid() != 0; |
| 192 } | 192 } |
| 193 }); | 193 }); |
| 194 | 194 |
| 195 // Crash the service. | 195 // Crash the service. |
| 196 assertTrue(connection.crashServiceForTesting()); | 196 assertTrue(connection.crashServiceForTesting()); |
| 197 | 197 |
| 198 // Verify that a new service is started for the pending spawn. | 198 // Verify that a new service is started for the pending spawn. |
| 199 CriteriaHelper.pollForCriteria(Criteria.equals(0, new Callable<Integer>(
) { | 199 CriteriaHelper.pollInstrumentationThread(Criteria.equals(0, new Callable
<Integer>() { |
| 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.pollForCriteria( | 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 ChildProcessLauncher.allocatedConnectionsCountFor
Testing( |
| 211 appContext); | 211 appContext); |
| 212 } | 212 } |
| 213 })); | 213 })); |
| 214 | 214 |
| 215 // Verify that the connection completes the setup for the pending spawn. | 215 // Verify that the connection completes the setup for the pending spawn. |
| 216 CriteriaHelper.pollForCriteria(Criteria.equals(1, new Callable<Integer>(
) { | 216 CriteriaHelper.pollInstrumentationThread(Criteria.equals(1, new Callable
<Integer>() { |
| 217 @Override | 217 @Override |
| 218 public Integer call() { | 218 public Integer call() { |
| 219 return ChildProcessLauncher.connectedServicesCountForTesting(); | 219 return ChildProcessLauncher.connectedServicesCountForTesting(); |
| 220 } | 220 } |
| 221 })); | 221 })); |
| 222 } | 222 } |
| 223 | 223 |
| 224 private ChildProcessConnectionImpl startConnection() throws InterruptedExcep
tion { | 224 private ChildProcessConnectionImpl startConnection() throws InterruptedExcep
tion { |
| 225 // Allocate a new connection. | 225 // Allocate a new connection. |
| 226 Context context = getInstrumentation().getTargetContext(); | 226 Context context = getInstrumentation().getTargetContext(); |
| 227 final ChildProcessConnectionImpl connection = (ChildProcessConnectionImp
l) | 227 final ChildProcessConnectionImpl connection = (ChildProcessConnectionImp
l) |
| 228 ChildProcessLauncher.allocateBoundConnectionForTesting(context); | 228 ChildProcessLauncher.allocateBoundConnectionForTesting(context); |
| 229 | 229 |
| 230 // Wait for the service to connect. | 230 // Wait for the service to connect. |
| 231 CriteriaHelper.pollForCriteria(new Criteria("The connection wasn't estab
lished.") { | 231 CriteriaHelper.pollInstrumentationThread( |
| 232 @Override | 232 new Criteria("The connection wasn't established.") { |
| 233 public boolean isSatisfied() { | 233 @Override |
| 234 return connection.isConnected(); | 234 public boolean isSatisfied() { |
| 235 } | 235 return connection.isConnected(); |
| 236 }); | 236 } |
| 237 }); |
| 237 return connection; | 238 return connection; |
| 238 } | 239 } |
| 239 | 240 |
| 240 private void triggerConnectionSetup(ChildProcessConnectionImpl connection) { | 241 private void triggerConnectionSetup(ChildProcessConnectionImpl connection) { |
| 241 ChildProcessLauncher.triggerConnectionSetup(connection, sProcessWaitArgu
ments, 1, | 242 ChildProcessLauncher.triggerConnectionSetup(connection, sProcessWaitArgu
ments, 1, |
| 242 new FileDescriptorInfo[0], ChildProcessLauncher.CALLBACK_FOR_REN
DERER_PROCESS, 0); | 243 new FileDescriptorInfo[0], ChildProcessLauncher.CALLBACK_FOR_REN
DERER_PROCESS, 0); |
| 243 } | 244 } |
| 244 | 245 |
| 245 @Override | 246 @Override |
| 246 protected void setUp() throws Exception { | 247 protected void setUp() throws Exception { |
| 247 super.setUp(); | 248 super.setUp(); |
| 248 LibraryLoader.get(LibraryProcessType.PROCESS_CHILD) | 249 LibraryLoader.get(LibraryProcessType.PROCESS_CHILD) |
| 249 .ensureInitialized(getInstrumentation().getContext()); | 250 .ensureInitialized(getInstrumentation().getContext()); |
| 250 } | 251 } |
| 251 } | 252 } |
| OLD | NEW |