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.chrome.browser.banners; | 5 package org.chromium.chrome.browser.banners; |
6 | 6 |
7 import android.content.pm.PackageInfo; | 7 import android.content.pm.PackageInfo; |
8 import android.os.HandlerThread; | 8 import android.os.HandlerThread; |
9 import android.support.test.filters.SmallTest; | 9 import android.support.test.filters.SmallTest; |
10 import android.test.InstrumentationTestCase; | |
11 import android.test.mock.MockPackageManager; | 10 import android.test.mock.MockPackageManager; |
12 | 11 |
| 12 import org.junit.After; |
| 13 import org.junit.Assert; |
| 14 import org.junit.Before; |
| 15 import org.junit.Test; |
| 16 import org.junit.runner.RunWith; |
| 17 |
| 18 import org.chromium.base.test.BaseJUnit4ClassRunner; |
13 import org.chromium.base.test.util.RetryOnFailure; | 19 import org.chromium.base.test.util.RetryOnFailure; |
14 import org.chromium.content.browser.test.util.Criteria; | 20 import org.chromium.content.browser.test.util.Criteria; |
15 import org.chromium.content.browser.test.util.CriteriaHelper; | 21 import org.chromium.content.browser.test.util.CriteriaHelper; |
16 | 22 |
17 /** | 23 /** |
18 * Tests the InstallerDelegate to make sure that it functions correctly and resp
onds to changes | 24 * Tests the InstallerDelegate to make sure that it functions correctly and resp
onds to changes |
19 * in the PackageManager. | 25 * in the PackageManager. |
20 */ | 26 */ |
21 public class InstallerDelegateTest extends InstrumentationTestCase | 27 @RunWith(BaseJUnit4ClassRunner.class) |
22 implements InstallerDelegate.Observer{ | 28 public class InstallerDelegateTest implements InstallerDelegate.Observer { |
23 private static final String MOCK_PACKAGE_NAME = "mock.package.name"; | 29 private static final String MOCK_PACKAGE_NAME = "mock.package.name"; |
24 | 30 |
25 /** | 31 /** |
26 * Returns a mocked set of installed packages. | 32 * Returns a mocked set of installed packages. |
27 */ | 33 */ |
28 public static class TestPackageManager extends MockPackageManager { | 34 public static class TestPackageManager extends MockPackageManager { |
29 public boolean isInstalled = false; | 35 public boolean isInstalled = false; |
30 | 36 |
31 @Override | 37 @Override |
32 public PackageInfo getPackageInfo(String packageName, int flags) | 38 public PackageInfo getPackageInfo(String packageName, int flags) |
(...skipping 15 matching lines...) Expand all Loading... |
48 private boolean mResultFinished; | 54 private boolean mResultFinished; |
49 private InstallerDelegate mResultDelegate; | 55 private InstallerDelegate mResultDelegate; |
50 private boolean mResultSuccess; | 56 private boolean mResultSuccess; |
51 private boolean mInstallStarted; | 57 private boolean mInstallStarted; |
52 | 58 |
53 @Override | 59 @Override |
54 public void onInstallFinished(InstallerDelegate delegate, boolean success) { | 60 public void onInstallFinished(InstallerDelegate delegate, boolean success) { |
55 mResultDelegate = delegate; | 61 mResultDelegate = delegate; |
56 mResultSuccess = success; | 62 mResultSuccess = success; |
57 mResultFinished = true; | 63 mResultFinished = true; |
58 assertTrue(mInstallStarted); | 64 Assert.assertTrue(mInstallStarted); |
59 } | 65 } |
60 | 66 |
61 @Override | 67 @Before |
62 public void setUp() throws Exception { | 68 public void setUp() throws Exception { |
63 super.setUp(); | |
64 | |
65 mPackageManager = new TestPackageManager(); | 69 mPackageManager = new TestPackageManager(); |
66 | 70 |
67 // Create a thread for the InstallerDelegate to run on. We need this th
read because the | 71 // Create a thread for the InstallerDelegate to run on. We need this th
read because the |
68 // InstallerDelegate's handler fails to be processed otherwise. | 72 // InstallerDelegate's handler fails to be processed otherwise. |
69 mThread = new HandlerThread("InstallerDelegateTest thread"); | 73 mThread = new HandlerThread("InstallerDelegateTest thread"); |
70 mThread.start(); | 74 mThread.start(); |
71 mTestDelegate = new InstallerDelegate( | 75 mTestDelegate = new InstallerDelegate( |
72 mThread.getLooper(), mPackageManager, this, MOCK_PACKAGE_NAME); | 76 mThread.getLooper(), mPackageManager, this, MOCK_PACKAGE_NAME); |
73 | 77 |
74 // Clear out the results from last time. | 78 // Clear out the results from last time. |
75 mResultDelegate = null; | 79 mResultDelegate = null; |
76 mResultSuccess = false; | 80 mResultSuccess = false; |
77 mResultFinished = false; | 81 mResultFinished = false; |
78 } | 82 } |
79 | 83 |
80 @Override | 84 @After |
81 public void tearDown() throws Exception { | 85 public void tearDown() throws Exception { |
82 mThread.quit(); | 86 mThread.quit(); |
83 super.tearDown(); | |
84 } | 87 } |
85 | 88 |
86 private void startMonitoring() { | 89 private void startMonitoring() { |
87 mTestDelegate.start(); | 90 mTestDelegate.start(); |
88 mInstallStarted = true; | 91 mInstallStarted = true; |
89 } | 92 } |
90 | 93 |
91 private void checkResults(boolean expectedResult) { | 94 private void checkResults(boolean expectedResult) { |
92 CriteriaHelper.pollInstrumentationThread(new Criteria() { | 95 CriteriaHelper.pollInstrumentationThread(new Criteria() { |
93 @Override | 96 @Override |
94 public boolean isSatisfied() { | 97 public boolean isSatisfied() { |
95 return !mTestDelegate.isRunning() && mResultFinished; | 98 return !mTestDelegate.isRunning() && mResultFinished; |
96 } | 99 } |
97 }); | 100 }); |
98 | 101 |
99 assertEquals(expectedResult, mResultSuccess); | 102 Assert.assertEquals(expectedResult, mResultSuccess); |
100 assertEquals(mTestDelegate, mResultDelegate); | 103 Assert.assertEquals(mTestDelegate, mResultDelegate); |
101 } | 104 } |
102 | 105 |
103 /** | 106 /** |
104 * Tests what happens when the InstallerDelegate detects that the package ha
s successfully | 107 * Tests what happens when the InstallerDelegate detects that the package ha
s successfully |
105 * been installed. | 108 * been installed. |
106 */ | 109 */ |
| 110 @Test |
107 @SmallTest | 111 @SmallTest |
108 public void testInstallSuccessful() { | 112 public void testInstallSuccessful() { |
109 mTestDelegate.setTimingForTests(1, 5000); | 113 mTestDelegate.setTimingForTests(1, 5000); |
110 startMonitoring(); | 114 startMonitoring(); |
111 | 115 |
112 assertFalse(mResultSuccess); | 116 Assert.assertFalse(mResultSuccess); |
113 assertNull(mResultDelegate); | 117 Assert.assertNull(mResultDelegate); |
114 assertFalse(mResultFinished); | 118 Assert.assertFalse(mResultFinished); |
115 | 119 |
116 mPackageManager.isInstalled = true; | 120 mPackageManager.isInstalled = true; |
117 checkResults(true); | 121 checkResults(true); |
118 } | 122 } |
119 | 123 |
120 /** | 124 /** |
121 * Tests what happens when the InstallerDelegate task is canceled. | 125 * Tests what happens when the InstallerDelegate task is canceled. |
122 */ | 126 */ |
| 127 @Test |
123 @SmallTest | 128 @SmallTest |
124 public void testInstallWaitUntilCancel() { | 129 public void testInstallWaitUntilCancel() { |
125 mTestDelegate.setTimingForTests(1, 5000); | 130 mTestDelegate.setTimingForTests(1, 5000); |
126 startMonitoring(); | 131 startMonitoring(); |
127 | 132 |
128 assertFalse(mResultSuccess); | 133 Assert.assertFalse(mResultSuccess); |
129 assertNull(mResultDelegate); | 134 Assert.assertNull(mResultDelegate); |
130 assertFalse(mResultFinished); | 135 Assert.assertFalse(mResultFinished); |
131 | 136 |
132 mTestDelegate.cancel(); | 137 mTestDelegate.cancel(); |
133 checkResults(false); | 138 checkResults(false); |
134 } | 139 } |
135 | 140 |
136 /** | 141 /** |
137 * Tests what happens when the InstallerDelegate times out. | 142 * Tests what happens when the InstallerDelegate times out. |
138 */ | 143 */ |
| 144 @Test |
139 @SmallTest | 145 @SmallTest |
140 public void testInstallTimeout() { | 146 public void testInstallTimeout() { |
141 mTestDelegate.setTimingForTests(1, 50); | 147 mTestDelegate.setTimingForTests(1, 50); |
142 startMonitoring(); | 148 startMonitoring(); |
143 checkResults(false); | 149 checkResults(false); |
144 } | 150 } |
145 | 151 |
146 /** | 152 /** |
147 * Makes sure that the runnable isn't called until returning from start(). | 153 * Makes sure that the runnable isn't called until returning from start(). |
148 */ | 154 */ |
| 155 @Test |
149 @SmallTest | 156 @SmallTest |
150 @RetryOnFailure | 157 @RetryOnFailure |
151 public void testRunnableRaceCondition() { | 158 public void testRunnableRaceCondition() { |
152 mPackageManager.isInstalled = true; | 159 mPackageManager.isInstalled = true; |
153 mTestDelegate.setTimingForTests(1, 5000); | 160 mTestDelegate.setTimingForTests(1, 5000); |
154 startMonitoring(); | 161 startMonitoring(); |
155 checkResults(true); | 162 checkResults(true); |
156 } | 163 } |
157 } | 164 } |
OLD | NEW |