Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(39)

Side by Side Diff: chrome/android/javatests/src/org/chromium/chrome/browser/banners/InstallerDelegateTest.java

Issue 156013005: Add InstallerDelegate for watching that a package gets installed (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Removing logs Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 package org.chromium.chrome.browser.banners;
6
7 import android.content.pm.PackageInfo;
8 import android.test.mock.MockPackageManager;
9
10 import org.chromium.base.ThreadUtils;
11 import org.chromium.chrome.testshell.ChromiumTestShellTestBase;
12 import org.chromium.content.browser.test.util.Criteria;
13 import org.chromium.content.browser.test.util.CriteriaHelper;
14
15 import java.util.ArrayList;
16 import java.util.List;
17
18 /**
19 * Tests the InstallerDelegate to make sure that it functions correctly and resp onds to changes
20 * in the PackageManager.
21 */
22 public class InstallerDelegateTest extends ChromiumTestShellTestBase
23 implements InstallerDelegate.Observer{
24 private static final String MOCK_PACKAGE_NAME = "mock.package.name";
25 private static final long MS_MAX_TIME_TO_WAIT = 5000;
26 private static final long MS_BETWEEN_CHECKS = 1000;
27
28 /**
29 * Returns a mocked set of installed packages.
30 */
31 public static class TestPackageManager extends MockPackageManager {
32 public boolean isInstalled = false;
33
34 @Override
35 public List<PackageInfo> getInstalledPackages(int flags) {
36 List<PackageInfo> packages = new ArrayList<PackageInfo>();
37
38 if (isInstalled) {
39 PackageInfo info = new PackageInfo();
40 info.packageName = MOCK_PACKAGE_NAME;
41 packages.add(info);
42 }
43
44 return packages;
45 }
46 }
47
48 private TestPackageManager mPackageManager;
49 private InstallerDelegate mResultDelegate;
50 private boolean mResultSuccess;
51 private boolean mWasCalled;
52
53 @Override
54 public void onInstallFinished(InstallerDelegate delegate, boolean success) {
55 mWasCalled = true;
56 mResultDelegate = delegate;
57 mResultSuccess = success;
58 }
59
60 @Override
61 public void setUp() throws Exception {
62 super.setUp();
63 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.
64 }
65
66 /**
67 * Tests what happens when the InstallerDelegate detects that the package ha s successfully
68 * been installed.
69 */
70 public void testInstallSuccessful() throws InterruptedException {
71 final InstallerDelegate monitor =
72 new InstallerDelegate(mPackageManager, this, MOCK_PACKAGE_NAME);
73 ThreadUtils.runOnUiThread(monitor);
74 assertFalse(mResultSuccess);
75 assertNull(mResultDelegate);
76
77 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() {
78 @Override
79 public boolean isSatisfied() {
80 return monitor.isRunning();
81 }
82 }, 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
83
84 mPackageManager.isInstalled = true;
85
86 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() {
87 @Override
88 public boolean isSatisfied() {
89 return mResultSuccess;
90 }
91 }, MS_MAX_TIME_TO_WAIT, MS_BETWEEN_CHECKS));
92 assertEquals(mResultDelegate, monitor);
93 }
94
95 /**
96 * Tests what happens when the InstallerDelegate task is canceled.
97 */
98 public void testInstallWaitUntilCancel() throws InterruptedException {
99 final InstallerDelegate monitor =
100 new InstallerDelegate(mPackageManager, this, MOCK_PACKAGE_NAME);
101 ThreadUtils.runOnUiThread(monitor);
102 assertFalse(mResultSuccess);
103 assertNull(mResultDelegate);
104
105 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() {
106 @Override
107 public boolean isSatisfied() {
108 return monitor.isRunning();
109 }
110 }, MS_MAX_TIME_TO_WAIT, MS_BETWEEN_CHECKS));
111
112 monitor.cancel();
113
114 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() {
115 @Override
116 public boolean isSatisfied() {
117 return mWasCalled;
118 }
119 }, MS_MAX_TIME_TO_WAIT, MS_BETWEEN_CHECKS));
120 assertFalse(monitor.isRunning());
121 assertFalse(mResultSuccess);
122 assertEquals(mResultDelegate, monitor);
123 }
124 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698