Index: chrome/android/javatests/src/org/chromium/chrome/browser/banners/InstallerDelegateTest.java |
diff --git a/chrome/android/javatests/src/org/chromium/chrome/browser/banners/InstallerDelegateTest.java b/chrome/android/javatests/src/org/chromium/chrome/browser/banners/InstallerDelegateTest.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..94c41ac016226a55bafbad0ed1131cfa1c62c0fa |
--- /dev/null |
+++ b/chrome/android/javatests/src/org/chromium/chrome/browser/banners/InstallerDelegateTest.java |
@@ -0,0 +1,124 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+package org.chromium.chrome.browser.banners; |
+ |
+import android.content.pm.PackageInfo; |
+import android.test.mock.MockPackageManager; |
+ |
+import org.chromium.base.ThreadUtils; |
+import org.chromium.chrome.testshell.ChromiumTestShellTestBase; |
+import org.chromium.content.browser.test.util.Criteria; |
+import org.chromium.content.browser.test.util.CriteriaHelper; |
+ |
+import java.util.ArrayList; |
+import java.util.List; |
+ |
+/** |
+ * Tests the InstallerDelegate to make sure that it functions correctly and responds to changes |
+ * in the PackageManager. |
+ */ |
+public class InstallerDelegateTest extends ChromiumTestShellTestBase |
+ implements InstallerDelegate.Observer{ |
+ private static final String MOCK_PACKAGE_NAME = "mock.package.name"; |
+ private static final long MS_MAX_TIME_TO_WAIT = 5000; |
+ private static final long MS_BETWEEN_CHECKS = 1000; |
+ |
+ /** |
+ * Returns a mocked set of installed packages. |
+ */ |
+ 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 mResultDelegate; |
+ private boolean mResultSuccess; |
+ private boolean mWasCalled; |
+ |
+ @Override |
+ public void onInstallFinished(InstallerDelegate delegate, boolean success) { |
+ mWasCalled = true; |
+ mResultDelegate = delegate; |
+ mResultSuccess = success; |
+ } |
+ |
+ @Override |
+ public void setUp() throws Exception { |
+ super.setUp(); |
+ mPackageManager = new TestPackageManager(); |
Yaron
2014/02/13 08:45:43
Reset other member variables to avoid test interac
gone
2014/02/14 05:16:26
Done.
|
+ } |
+ |
+ /** |
+ * Tests what happens when the InstallerDelegate detects that the package has successfully |
+ * been installed. |
+ */ |
+ public void testInstallSuccessful() throws InterruptedException { |
+ final InstallerDelegate monitor = |
+ new InstallerDelegate(mPackageManager, this, MOCK_PACKAGE_NAME); |
+ ThreadUtils.runOnUiThread(monitor); |
+ assertFalse(mResultSuccess); |
+ assertNull(mResultDelegate); |
+ |
+ assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { |
+ @Override |
+ public boolean isSatisfied() { |
+ return monitor.isRunning(); |
+ } |
+ }, MS_MAX_TIME_TO_WAIT, MS_BETWEEN_CHECKS)); |
Yaron
2014/02/13 08:45:43
This can probably be made faster by mocking the cl
gone
2014/02/14 05:16:26
Made it so that the timers are configurable on the
|
+ |
+ mPackageManager.isInstalled = true; |
+ |
+ assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { |
+ @Override |
+ public boolean isSatisfied() { |
+ return mResultSuccess; |
+ } |
+ }, MS_MAX_TIME_TO_WAIT, MS_BETWEEN_CHECKS)); |
+ assertEquals(mResultDelegate, monitor); |
+ } |
+ |
+ /** |
+ * Tests what happens when the InstallerDelegate task is canceled. |
+ */ |
+ public void testInstallWaitUntilCancel() throws InterruptedException { |
+ final InstallerDelegate monitor = |
+ new InstallerDelegate(mPackageManager, this, MOCK_PACKAGE_NAME); |
+ ThreadUtils.runOnUiThread(monitor); |
+ assertFalse(mResultSuccess); |
+ assertNull(mResultDelegate); |
+ |
+ assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { |
+ @Override |
+ public boolean isSatisfied() { |
+ return monitor.isRunning(); |
+ } |
+ }, MS_MAX_TIME_TO_WAIT, MS_BETWEEN_CHECKS)); |
+ |
+ monitor.cancel(); |
+ |
+ assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { |
+ @Override |
+ public boolean isSatisfied() { |
+ return mWasCalled; |
+ } |
+ }, MS_MAX_TIME_TO_WAIT, MS_BETWEEN_CHECKS)); |
+ assertFalse(monitor.isRunning()); |
+ assertFalse(mResultSuccess); |
+ assertEquals(mResultDelegate, monitor); |
+ } |
+} |