This source file includes following definitions.
- getInstalledPackages
- onInstallFinished
- setUp
- tearDown
- startMonitoring
- checkResults
- testInstallSuccessful
- testInstallWaitUntilCancel
- testInstallTimeout
- testRunnableRaceCondition
package org.chromium.chrome.browser.banners;
import android.content.pm.PackageInfo;
import android.os.HandlerThread;
import android.test.mock.MockPackageManager;
import org.chromium.chrome.shell.ChromeShellTestBase;
import org.chromium.content.browser.test.util.Criteria;
import org.chromium.content.browser.test.util.CriteriaHelper;
import java.util.ArrayList;
import java.util.List;
public class InstallerDelegateTest extends ChromeShellTestBase
implements InstallerDelegate.Observer{
private static final String MOCK_PACKAGE_NAME = "mock.package.name";
public static class TestPackageManager extends MockPackageManager {
public boolean isInstalled = false;
@Override
public List<PackageInfo> getInstalledPackages(int flags) {
List<PackageInfo> packages = new ArrayList<PackageInfo>();
if (isInstalled) {
PackageInfo info = new PackageInfo();
info.packageName = MOCK_PACKAGE_NAME;
packages.add(info);
}
return packages;
}
}
private TestPackageManager mPackageManager;
private InstallerDelegate mTestDelegate;
private HandlerThread mThread;
private boolean mResultFinished;
private InstallerDelegate mResultDelegate;
private boolean mResultSuccess;
private boolean mInstallStarted;
@Override
public void onInstallFinished(InstallerDelegate delegate, boolean success) {
mResultDelegate = delegate;
mResultSuccess = success;
mResultFinished = true;
assertTrue(mInstallStarted);
}
@Override
public void setUp() throws Exception {
super.setUp();
mPackageManager = new TestPackageManager();
mThread = new HandlerThread("InstallerDelegateTest thread");
mThread.start();
mTestDelegate = new InstallerDelegate(
mThread.getLooper(), mPackageManager, this, MOCK_PACKAGE_NAME);
mResultDelegate = null;
mResultSuccess = false;
mResultFinished = false;
}
@Override
public void tearDown() throws Exception {
mThread.quit();
super.tearDown();
}
private void startMonitoring() throws InterruptedException {
mTestDelegate.start();
mInstallStarted = true;
assertTrue(CriteriaHelper.pollForCriteria(new Criteria() {
@Override
public boolean isSatisfied() {
return mTestDelegate.isRunning();
}
}));
}
private void checkResults(boolean expectedResult) throws InterruptedException {
assertTrue(CriteriaHelper.pollForCriteria(new Criteria() {
@Override
public boolean isSatisfied() {
return !mTestDelegate.isRunning() && mResultFinished;
}
}));
assertEquals(expectedResult, mResultSuccess);
assertEquals(mTestDelegate, mResultDelegate);
}
public void testInstallSuccessful() throws InterruptedException {
mTestDelegate.setTimingForTests(1, 5000);
startMonitoring();
assertFalse(mResultSuccess);
assertNull(mResultDelegate);
assertFalse(mResultFinished);
mPackageManager.isInstalled = true;
checkResults(true);
}
public void testInstallWaitUntilCancel() throws InterruptedException {
mTestDelegate.setTimingForTests(1, 5000);
startMonitoring();
assertFalse(mResultSuccess);
assertNull(mResultDelegate);
assertFalse(mResultFinished);
mTestDelegate.cancel();
checkResults(false);
}
public void testInstallTimeout() throws InterruptedException {
mTestDelegate.setTimingForTests(1, 50);
startMonitoring();
checkResults(false);
}
public void testRunnableRaceCondition() throws InterruptedException {
mPackageManager.isInstalled = true;
mTestDelegate.setTimingForTests(1, 5000);
startMonitoring();
checkResults(true);
}
}