This source file includes following definitions.
- getPid
- isInitialBindingBound
- isStrongBindingBound
- removeInitialBinding
- isOomProtectedOrWasWhenDied
- dropOomBindings
- addStrongBinding
- removeStrongBinding
- stop
- getServiceNumber
- isInSandbox
- getService
- start
- setupConnection
- getErrorMessage
- setUp
- Feature
- testNewConnectionDropsPreviousOnLowEnd
- Feature
- testBindingRemovalOnLowEnd
- Feature
- testBindingRemovalOnHighEnd
- Feature
- testIsOomProtected
- Feature
- testBackgroundPeriodBinding
package org.chromium.content.browser;
import android.os.Bundle;
import android.test.InstrumentationTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import org.chromium.base.test.util.Feature;
import org.chromium.content.common.IChildProcessCallback;
import org.chromium.content.common.IChildProcessService;
public class BindingManagerImplTest extends InstrumentationTestCase {
private static class MockChildProcessConnection implements ChildProcessConnection {
boolean mInitialBindingBound;
int mStrongBindingCount;
final int mPid;
MockChildProcessConnection(int pid) {
mInitialBindingBound = true;
mStrongBindingCount = 0;
mPid = pid;
}
@Override
public int getPid() {
return mPid;
}
@Override
public boolean isInitialBindingBound() {
return mInitialBindingBound;
}
@Override
public boolean isStrongBindingBound() {
return mStrongBindingCount > 0;
}
@Override
public void removeInitialBinding() {
mInitialBindingBound = false;
}
@Override
public boolean isOomProtectedOrWasWhenDied() {
return mInitialBindingBound || mStrongBindingCount > 0;
}
@Override
public void dropOomBindings() {
mInitialBindingBound = false;
mStrongBindingCount = 0;
}
@Override
public void addStrongBinding() {
mStrongBindingCount++;
}
@Override
public void removeStrongBinding() {
assert mStrongBindingCount > 0;
mStrongBindingCount--;
}
@Override
public void stop() {
mInitialBindingBound = false;
mStrongBindingCount = 0;
}
@Override
public int getServiceNumber() { throw new UnsupportedOperationException(); }
@Override
public boolean isInSandbox() { throw new UnsupportedOperationException(); }
@Override
public IChildProcessService getService() { throw new UnsupportedOperationException(); }
@Override
public void start(String[] commandLine) { throw new UnsupportedOperationException(); }
@Override
public void setupConnection(String[] commandLine, FileDescriptorInfo[] filesToBeMapped,
IChildProcessCallback processCallback, ConnectionCallback connectionCallbacks,
Bundle sharedRelros) {
throw new UnsupportedOperationException();
}
}
private static class ManagerEntry {
BindingManagerImpl mManager;
String mLabel;
ManagerEntry(BindingManagerImpl manager, String label) {
mManager = manager;
mLabel = label;
}
String getErrorMessage() {
return "Failed for the " + mLabel + " manager.";
}
}
BindingManagerImpl mLowEndManager;
BindingManagerImpl mHighEndManager;
ManagerEntry[] mAllManagers;
@Override
protected void setUp() {
mLowEndManager = BindingManagerImpl.createBindingManagerForTesting(true);
mHighEndManager = BindingManagerImpl.createBindingManagerForTesting(false);
mAllManagers = new ManagerEntry[] {
new ManagerEntry(mLowEndManager, "low-end"),
new ManagerEntry(mHighEndManager, "high-end")};
}
@SmallTest
@Feature({"ProcessManagement"})
public void testNewConnectionDropsPreviousOnLowEnd() {
BindingManagerImpl manager = mLowEndManager;
MockChildProcessConnection firstConnection = new MockChildProcessConnection(1);
manager.addNewConnection(firstConnection.getPid(), firstConnection);
manager.setInForeground(firstConnection.getPid(), true);
assertTrue(firstConnection.isStrongBindingBound());
MockChildProcessConnection secondConnection = new MockChildProcessConnection(2);
manager.addNewConnection(secondConnection.getPid(), secondConnection);
assertFalse(firstConnection.isStrongBindingBound());
}
@SmallTest
@Feature({"ProcessManagement"})
public void testBindingRemovalOnLowEnd() throws Throwable {
final BindingManagerImpl manager = mLowEndManager;
final MockChildProcessConnection connection = new MockChildProcessConnection(1);
manager.addNewConnection(connection.getPid(), connection);
assertTrue(connection.isInitialBindingBound());
assertFalse(connection.isStrongBindingBound());
runTestOnUiThread(new Runnable() {
@Override
public void run() {
manager.setInForeground(connection.getPid(), true);
assertTrue(connection.isStrongBindingBound());
assertTrue(connection.isInitialBindingBound());
manager.setInForeground(connection.getPid(), false);
assertFalse(connection.isStrongBindingBound());
}
});
getInstrumentation().waitForIdleSync();
assertFalse(connection.isInitialBindingBound());
}
@SmallTest
@Feature({"ProcessManagement"})
public void testBindingRemovalOnHighEnd() throws Throwable {
final BindingManagerImpl manager = mHighEndManager;
final MockChildProcessConnection connection = new MockChildProcessConnection(1);
manager.addNewConnection(connection.getPid(), connection);
assertTrue(connection.isInitialBindingBound());
assertFalse(connection.isStrongBindingBound());
runTestOnUiThread(new Runnable() {
@Override
public void run() {
manager.setInForeground(connection.getPid(), true);
assertTrue(connection.isStrongBindingBound());
assertTrue(connection.isInitialBindingBound());
manager.setInForeground(connection.getPid(), false);
assertTrue(connection.isStrongBindingBound());
}
});
getInstrumentation().waitForIdleSync();
assertFalse(connection.isInitialBindingBound());
assertFalse(connection.isStrongBindingBound());
}
@SmallTest
@Feature({"ProcessManagement"})
public void testIsOomProtected() {
for (ManagerEntry managerEntry : mAllManagers) {
BindingManagerImpl manager = managerEntry.mManager;
String message = managerEntry.getErrorMessage();
MockChildProcessConnection connection = new MockChildProcessConnection(1);
manager.addNewConnection(connection.getPid(), connection);
assertTrue(message, manager.isOomProtected(connection.getPid()));
manager.setInForeground(connection.getPid(), false);
getInstrumentation().waitForIdleSync();
assertFalse(message, manager.isOomProtected(connection.getPid()));
manager.setInForeground(connection.getPid(), true);
assertTrue(message, manager.isOomProtected(connection.getPid()));
assertFalse(manager.isConnectionCleared(connection.getPid()));
manager.clearConnection(connection.getPid());
assertTrue(manager.isConnectionCleared(connection.getPid()));
connection.stop();
assertFalse(message, connection.isInitialBindingBound());
assertFalse(message, connection.isStrongBindingBound());
assertTrue(message, manager.isOomProtected(connection.getPid()));
}
}
@SmallTest
@Feature({"ProcessManagement"})
public void testBackgroundPeriodBinding() {
for (ManagerEntry managerEntry : mAllManagers) {
BindingManagerImpl manager = managerEntry.mManager;
String message = managerEntry.getErrorMessage();
MockChildProcessConnection firstConnection = new MockChildProcessConnection(1);
manager.addNewConnection(firstConnection.getPid(), firstConnection);
manager.setInForeground(firstConnection.getPid(), true);
manager.setInForeground(firstConnection.getPid(), false);
MockChildProcessConnection secondConnection = new MockChildProcessConnection(2);
manager.addNewConnection(secondConnection.getPid(), secondConnection);
manager.setInForeground(secondConnection.getPid(), true);
manager.setInForeground(secondConnection.getPid(), false);
MockChildProcessConnection thirdConnection = new MockChildProcessConnection(3);
manager.addNewConnection(thirdConnection.getPid(), thirdConnection);
manager.setInForeground(thirdConnection.getPid(), false);
getInstrumentation().waitForIdleSync();
assertFalse(message, firstConnection.isStrongBindingBound());
assertFalse(message, secondConnection.isStrongBindingBound());
assertFalse(message, thirdConnection.isStrongBindingBound());
manager.onSentToBackground();
assertFalse(message, firstConnection.isStrongBindingBound());
assertTrue(message, secondConnection.isStrongBindingBound());
assertFalse(message, thirdConnection.isStrongBindingBound());
manager.onBroughtToForeground();
getInstrumentation().waitForIdleSync();
assertFalse(message, firstConnection.isStrongBindingBound());
assertFalse(message, secondConnection.isStrongBindingBound());
assertFalse(message, thirdConnection.isStrongBindingBound());
}
}
}