OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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.webapps; | 5 package org.chromium.chrome.browser.webapps; |
6 | 6 |
7 import static org.junit.Assert.assertEquals; | 7 import static org.junit.Assert.assertEquals; |
8 import static org.junit.Assert.assertTrue; | 8 import static org.junit.Assert.assertTrue; |
9 | 9 |
10 import android.content.Context; | 10 import android.content.Context; |
11 import android.content.Intent; | 11 import android.content.Intent; |
12 import android.content.SharedPreferences; | 12 import android.content.SharedPreferences; |
13 import android.graphics.Bitmap; | 13 import android.graphics.Bitmap; |
14 | 14 |
15 import org.chromium.base.test.util.Feature; | 15 import org.chromium.base.test.util.Feature; |
16 import org.chromium.chrome.browser.ShortcutHelper; | 16 import org.chromium.chrome.browser.ShortcutHelper; |
17 import org.chromium.testing.local.BackgroundShadowAsyncTask; | 17 import org.chromium.testing.local.BackgroundShadowAsyncTask; |
18 import org.chromium.testing.local.LocalRobolectricTestRunner; | 18 import org.chromium.testing.local.LocalRobolectricTestRunner; |
19 import org.junit.Before; | 19 import org.junit.Before; |
20 import org.junit.Test; | 20 import org.junit.Test; |
21 import org.junit.runner.RunWith; | 21 import org.junit.runner.RunWith; |
22 import org.robolectric.Robolectric; | 22 import org.robolectric.Robolectric; |
23 import org.robolectric.annotation.Config; | 23 import org.robolectric.annotation.Config; |
24 | 24 |
25 import java.util.concurrent.TimeUnit; | |
26 | |
25 /** | 27 /** |
26 * Tests the WebappDataStorage class by ensuring that it persists data to | 28 * Tests the WebappDataStorage class by ensuring that it persists data to |
27 * SharedPreferences as expected. | 29 * SharedPreferences as expected. |
28 */ | 30 */ |
29 @RunWith(LocalRobolectricTestRunner.class) | 31 @RunWith(LocalRobolectricTestRunner.class) |
30 @Config(manifest = Config.NONE, shadows = {BackgroundShadowAsyncTask.class}) | 32 @Config(manifest = Config.NONE, shadows = {BackgroundShadowAsyncTask.class}) |
31 public class WebappDataStorageTest { | 33 public class WebappDataStorageTest { |
32 | 34 |
33 private SharedPreferences mSharedPreferences; | 35 private SharedPreferences mSharedPreferences; |
34 private boolean mCallbackCalled; | 36 private boolean mCallbackCalled; |
35 | 37 |
36 private class FetchCallback<T> implements WebappDataStorage.FetchCallback<T> { | 38 private class FetchCallback<T> implements WebappDataStorage.FetchCallback<T> { |
37 T mExpected; | 39 T mExpected; |
38 | 40 |
39 FetchCallback(T expected) { | 41 FetchCallback(T expected) { |
40 mExpected = expected; | 42 mExpected = expected; |
41 } | 43 } |
42 | 44 |
43 @Override | 45 @Override |
44 public void onDataRetrieved(T readObject) { | 46 public void onDataRetrieved(T readObject) { |
45 mCallbackCalled = true; | 47 mCallbackCalled = true; |
46 assertEquals(mExpected, readObject); | 48 assertEquals(mExpected, readObject); |
47 } | 49 } |
48 } | 50 } |
49 | 51 |
52 private static class TestClock extends WebappDataStorage.Clock { | |
53 private long mCurrentTime; | |
54 | |
55 public TestClock(long currentTime) { | |
56 updateTime(currentTime); | |
57 } | |
58 | |
59 public void updateTime(long currentTime) { | |
60 mCurrentTime = currentTime; | |
61 } | |
62 | |
63 @Override | |
64 public long currentTimeMillis() { | |
65 return mCurrentTime; | |
66 } | |
67 } | |
68 | |
50 @Before | 69 @Before |
51 public void setUp() throws Exception { | 70 public void setUp() throws Exception { |
52 mSharedPreferences = Robolectric.application | 71 mSharedPreferences = Robolectric.application |
53 .getSharedPreferences("webapp_test", Context.MODE_PRIVATE); | 72 .getSharedPreferences("webapp_test", Context.MODE_PRIVATE); |
54 | 73 |
55 // Set the last_used as if the web app had been registered by WebappRegi stry. | 74 // Set the last_used as if the web app had been registered by WebappRegi stry. |
56 mSharedPreferences.edit().putLong("last_used", 0).commit(); | 75 mSharedPreferences.edit().putLong("last_used", 0).commit(); |
57 | 76 |
58 mCallbackCalled = false; | 77 mCallbackCalled = false; |
59 } | 78 } |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
142 WebappDataStorage.getScope(Robolectric.application, "test", | 161 WebappDataStorage.getScope(Robolectric.application, "test", |
143 new FetchCallback<String>(scope)); | 162 new FetchCallback<String>(scope)); |
144 BackgroundShadowAsyncTask.runBackgroundTasks(); | 163 BackgroundShadowAsyncTask.runBackgroundTasks(); |
145 Robolectric.runUiThreadTasks(); | 164 Robolectric.runUiThreadTasks(); |
146 | 165 |
147 assertTrue(mCallbackCalled); | 166 assertTrue(mCallbackCalled); |
148 } | 167 } |
149 | 168 |
150 @Test | 169 @Test |
151 @Feature({"Webapp"}) | 170 @Feature({"Webapp"}) |
152 public void testURLRetrieval() throws Exception { | 171 public void testUrlRetrieval() throws Exception { |
153 final String url = "https://www.google.com"; | 172 final String url = "https://www.google.com"; |
154 mSharedPreferences.edit() | 173 mSharedPreferences.edit() |
155 .putString(WebappDataStorage.KEY_URL, url) | 174 .putString(WebappDataStorage.KEY_URL, url) |
156 .commit(); | 175 .commit(); |
157 | 176 |
158 WebappDataStorage.getURL(Robolectric.application, "test", | 177 WebappDataStorage.getUrl(Robolectric.application, "test", |
159 new FetchCallback<String>(url)); | 178 new FetchCallback<String>(url)); |
160 BackgroundShadowAsyncTask.runBackgroundTasks(); | 179 BackgroundShadowAsyncTask.runBackgroundTasks(); |
161 Robolectric.runUiThreadTasks(); | 180 Robolectric.runUiThreadTasks(); |
162 | 181 |
163 assertTrue(mCallbackCalled); | 182 assertTrue(mCallbackCalled); |
164 } | 183 } |
165 | 184 |
166 @Test | 185 @Test |
167 @Feature({"Webapp"}) | 186 @Feature({"Webapp"}) |
187 public void testWasLaunchedRecently() throws Exception { | |
188 final TestClock clock = new TestClock(System.currentTimeMillis()); | |
189 WebappDataStorage.setClockForTests(clock); | |
190 | |
191 WebappDataStorage storage = WebappDataStorage.open(Robolectric.applicati on, "test"); | |
192 storage.updateLastUsedTime(); | |
193 BackgroundShadowAsyncTask.runBackgroundTasks(); | |
194 Robolectric.runUiThreadTasks(); | |
195 assertTrue(!storage.wasLaunchedRecently()); | |
196 | |
197 long lastUsedTime = mSharedPreferences.getLong(WebappDataStorage.KEY_LAS T_USED, | |
198 WebappDataStorage.LAST_USED_INVALID); | |
199 | |
200 assertTrue(lastUsedTime != WebappDataStorage.LAST_USED_UNSET); | |
201 assertTrue(lastUsedTime != WebappDataStorage.LAST_USED_INVALID); | |
202 | |
203 // Mark as launched, check launched recently. | |
204 mSharedPreferences.edit() | |
205 .putBoolean(WebappDataStorage.KEY_LAUNCHED, true) | |
206 .commit(); | |
207 assertTrue(storage.wasLaunchedRecently()); | |
208 | |
209 // Move the last used time one day in the past. | |
210 mSharedPreferences.edit() | |
211 .putLong(WebappDataStorage.KEY_LAST_USED, lastUsedTime - TimeUni t.DAYS.toMillis(1L)) | |
212 .commit(); | |
213 assertTrue(storage.wasLaunchedRecently()); | |
214 | |
215 // Move the last used time three days in the past. | |
216 mSharedPreferences.edit() | |
217 .putLong(WebappDataStorage.KEY_LAST_USED, lastUsedTime - TimeUni t.DAYS.toMillis(3L)) | |
218 .commit(); | |
219 assertTrue(storage.wasLaunchedRecently()); | |
220 | |
221 // Move the last used time one week in the past. | |
222 mSharedPreferences.edit() | |
223 .putLong(WebappDataStorage.KEY_LAST_USED, lastUsedTime - TimeUni t.DAYS.toMillis(7L)) | |
224 .commit(); | |
225 assertTrue(storage.wasLaunchedRecently()); | |
226 | |
227 // Move the last used time just under ten days in the past. | |
228 mSharedPreferences.edit().putLong(WebappDataStorage.KEY_LAST_USED, | |
229 lastUsedTime - TimeUnit.DAYS.toMillis(10L) + 1).commit(); | |
230 assertTrue(storage.wasLaunchedRecently()); | |
231 | |
232 // Mark as not launched. | |
233 mSharedPreferences.edit() | |
234 .putBoolean(WebappDataStorage.KEY_LAUNCHED, false) | |
235 .commit(); | |
236 assertTrue(!storage.wasLaunchedRecently()); | |
237 | |
238 // Mark as launched | |
gone
2016/04/07 19:10:44
nit: launched.
dominickn
2016/04/08 01:10:46
Done.
| |
239 mSharedPreferences.edit() | |
240 .putBoolean(WebappDataStorage.KEY_LAUNCHED, true) | |
241 .commit(); | |
242 assertTrue(storage.wasLaunchedRecently()); | |
243 | |
244 // Move the last used time to exactly ten days in the past. | |
245 mSharedPreferences.edit().putLong(WebappDataStorage.KEY_LAST_USED, | |
246 lastUsedTime - TimeUnit.DAYS.toMillis(10L)).commit(); | |
247 assertTrue(!storage.wasLaunchedRecently()); | |
248 } | |
249 | |
250 @Test | |
251 @Feature({"Webapp"}) | |
168 public void testIntentUpdate() throws Exception { | 252 public void testIntentUpdate() throws Exception { |
169 final String id = "id"; | 253 final String id = "id"; |
170 final String action = "action"; | 254 final String action = "action"; |
171 final String url = "url"; | 255 final String url = "url"; |
172 final String scope = "scope"; | 256 final String scope = "scope"; |
173 final String name = "name"; | 257 final String name = "name"; |
174 final String shortName = "shortName"; | 258 final String shortName = "shortName"; |
175 final Bitmap icon = createBitmap(); | 259 final Bitmap icon = createBitmap(); |
176 final int orientation = 1; | 260 final int orientation = 1; |
177 final long themeColor = 2; | 261 final long themeColor = 2; |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
267 if (actual.getPixel(i, j) != 0) return false; | 351 if (actual.getPixel(i, j) != 0) return false; |
268 } | 352 } |
269 } | 353 } |
270 return true; | 354 return true; |
271 } | 355 } |
272 | 356 |
273 private static Bitmap createBitmap() { | 357 private static Bitmap createBitmap() { |
274 return Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_4444); | 358 return Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_4444); |
275 } | 359 } |
276 } | 360 } |
OLD | NEW |