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; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
56 @Feature({"Webapp"}) | 56 @Feature({"Webapp"}) |
57 public void testBackwardCompatibility() { | 57 public void testBackwardCompatibility() { |
58 assertEquals(REGISTRY_FILE_NAME, WebappRegistry.REGISTRY_FILE_NAME); | 58 assertEquals(REGISTRY_FILE_NAME, WebappRegistry.REGISTRY_FILE_NAME); |
59 assertEquals(KEY_WEBAPP_SET, WebappRegistry.KEY_WEBAPP_SET); | 59 assertEquals(KEY_WEBAPP_SET, WebappRegistry.KEY_WEBAPP_SET); |
60 assertEquals(KEY_LAST_CLEANUP, WebappRegistry.KEY_LAST_CLEANUP); | 60 assertEquals(KEY_LAST_CLEANUP, WebappRegistry.KEY_LAST_CLEANUP); |
61 } | 61 } |
62 | 62 |
63 @Test | 63 @Test |
64 @Feature({"Webapp"}) | 64 @Feature({"Webapp"}) |
65 public void testWebappRegistrationAddsToSharedPrefs() throws Exception { | 65 public void testWebappRegistrationAddsToSharedPrefs() throws Exception { |
66 WebappRegistry.registerWebapp(Robolectric.application, "test"); | 66 WebappRegistry.registerWebapp(Robolectric.application, "test", "https:// www.google.com"); |
67 BackgroundShadowAsyncTask.runBackgroundTasks(); | 67 BackgroundShadowAsyncTask.runBackgroundTasks(); |
68 | 68 |
69 Set<String> actual = mSharedPreferences.getStringSet( | 69 Set<String> actual = mSharedPreferences.getStringSet( |
70 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); | 70 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); |
71 assertEquals(1, actual.size()); | 71 assertEquals(1, actual.size()); |
72 assertTrue(actual.contains("test")); | 72 assertTrue(actual.contains("test")); |
73 } | 73 } |
74 | 74 |
75 @Test | 75 @Test |
76 @Feature({"Webapp"}) | 76 @Feature({"Webapp"}) |
77 public void testWebappRegistrationUpdatesLastUsed() throws Exception { | 77 public void testWebappRegistrationUpdatesLastUsedAndOrigin() throws Exceptio n { |
78 long before = System.currentTimeMillis(); | 78 long before = System.currentTimeMillis(); |
79 WebappRegistry.registerWebapp(Robolectric.application, "test"); | 79 final String origin = "http://drive.google.com"; |
80 | |
81 WebappRegistry.registerWebapp(Robolectric.application, "test", origin); | |
80 BackgroundShadowAsyncTask.runBackgroundTasks(); | 82 BackgroundShadowAsyncTask.runBackgroundTasks(); |
81 long after = System.currentTimeMillis(); | 83 long after = System.currentTimeMillis(); |
82 | 84 |
83 SharedPreferences webAppPrefs = Robolectric.application.getSharedPrefere nces( | 85 SharedPreferences webAppPrefs = Robolectric.application.getSharedPrefere nces( |
84 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "test", Context.MOD E_PRIVATE); | 86 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "test", Context.MOD E_PRIVATE); |
85 long actual = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USED, | 87 long actual = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USED, |
86 WebappDataStorage.INVALID_LAST_USED); | 88 WebappDataStorage.INVALID_LAST_USED); |
89 String webAppOrigin = webAppPrefs.getString(WebappDataStorage.KEY_ORIGIN _URL, | |
90 WebappDataStorage.INVALID_ORIGIN_URL); | |
87 assertTrue("Timestamp is out of range", before <= actual && actual <= af ter); | 91 assertTrue("Timestamp is out of range", before <= actual && actual <= af ter); |
92 assertEquals(origin, webAppOrigin); | |
88 } | 93 } |
89 | 94 |
90 @Test | 95 @Test |
91 @Feature({"Webapp"}) | 96 @Feature({"Webapp"}) |
92 public void testWebappIdsRetrieval() throws Exception { | 97 public void testWebappIdsRetrieval() throws Exception { |
93 final Set<String> expected = addWebappsToRegistry("first", "second"); | 98 final Set<String> expected = addWebappsToRegistry("first", "second"); |
94 | 99 |
95 WebappRegistry.getRegisteredWebappIds(Robolectric.application, | 100 WebappRegistry.getRegisteredWebappIds(Robolectric.application, |
96 new WebappRegistry.FetchCallback() { | 101 new WebappRegistry.FetchCallback() { |
97 @Override | 102 @Override |
(...skipping 20 matching lines...) Expand all Loading... | |
118 mCallbackCalled = true; | 123 mCallbackCalled = true; |
119 assertEquals(expected, actual); | 124 assertEquals(expected, actual); |
120 } | 125 } |
121 }); | 126 }); |
122 BackgroundShadowAsyncTask.runBackgroundTasks(); | 127 BackgroundShadowAsyncTask.runBackgroundTasks(); |
123 Robolectric.runUiThreadTasks(); | 128 Robolectric.runUiThreadTasks(); |
124 | 129 |
125 assertTrue(mCallbackCalled); | 130 assertTrue(mCallbackCalled); |
126 mCallbackCalled = false; | 131 mCallbackCalled = false; |
127 | 132 |
128 WebappRegistry.registerWebapp(Robolectric.application, "second"); | 133 WebappRegistry.registerWebapp(Robolectric.application, "second", "https: //www.google.com"); |
129 BackgroundShadowAsyncTask.runBackgroundTasks(); | 134 BackgroundShadowAsyncTask.runBackgroundTasks(); |
130 | 135 |
131 // A copy of the expected set needs to be made as the SharedPreferences is using the copy | 136 // A copy of the expected set needs to be made as the SharedPreferences is using the copy |
132 // that was paassed to it. | 137 // that was paassed to it. |
133 final Set<String> secondExpected = new HashSet<String>(expected); | 138 final Set<String> secondExpected = new HashSet<String>(expected); |
134 secondExpected.add("second"); | 139 secondExpected.add("second"); |
135 | 140 |
136 WebappRegistry.getRegisteredWebappIds(Robolectric.application, | 141 WebappRegistry.getRegisteredWebappIds(Robolectric.application, |
137 new WebappRegistry.FetchCallback() { | 142 new WebappRegistry.FetchCallback() { |
138 @Override | 143 @Override |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
277 assertTrue(actual.isEmpty()); | 282 assertTrue(actual.isEmpty()); |
278 | 283 |
279 long actualLastUsed = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USE D, | 284 long actualLastUsed = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USE D, |
280 WebappDataStorage.INVALID_LAST_USED); | 285 WebappDataStorage.INVALID_LAST_USED); |
281 assertEquals(WebappDataStorage.INVALID_LAST_USED, actualLastUsed); | 286 assertEquals(WebappDataStorage.INVALID_LAST_USED, actualLastUsed); |
282 | 287 |
283 long lastCleanup = mSharedPreferences.getLong(WebappRegistry.KEY_LAST_CL EANUP, -1); | 288 long lastCleanup = mSharedPreferences.getLong(WebappRegistry.KEY_LAST_CL EANUP, -1); |
284 assertEquals(currentTime, lastCleanup); | 289 assertEquals(currentTime, lastCleanup); |
285 } | 290 } |
286 | 291 |
292 @Test | |
293 @Feature({"Webapp"}) | |
294 public void testClearWebappHistoryRunsCallback() throws Exception { | |
295 WebappRegistry.clearWebappHistory(Robolectric.application, new Runnable( ) { | |
296 @Override | |
297 public void run() { | |
298 mCallbackCalled = true; | |
299 } | |
300 }); | |
301 BackgroundShadowAsyncTask.runBackgroundTasks(); | |
302 Robolectric.runUiThreadTasks(); | |
303 | |
304 assertTrue(mCallbackCalled); | |
305 } | |
306 | |
307 @Test | |
308 @Feature({"Webapp"}) | |
309 public void testClearWebappHistory() throws Exception { | |
310 final String webappOrigin1 = "https://www.google.com"; | |
311 final String webappOrigin2 = "https://drive.google.com"; | |
312 WebappRegistry.registerWebapp(Robolectric.application, "webapp1", webapp Origin1); | |
313 WebappRegistry.registerWebapp(Robolectric.application, "webapp2", webapp Origin2); | |
314 BackgroundShadowAsyncTask.runBackgroundTasks(); | |
315 | |
316 SharedPreferences webapp1Prefs = Robolectric.application.getSharedPrefer ences( | |
317 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "webapp1", Context. MODE_PRIVATE); | |
318 SharedPreferences webapp2Prefs = Robolectric.application.getSharedPrefer ences( | |
319 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "webapp2", Context. MODE_PRIVATE); | |
320 | |
321 WebappRegistry.clearWebappHistory(Robolectric.application, null); | |
322 BackgroundShadowAsyncTask.runBackgroundTasks(); | |
323 | |
324 Set<String> actual = mSharedPreferences.getStringSet( | |
325 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); | |
326 assertEquals(2, actual.size()); | |
327 assertTrue(actual.contains("webapp1")); | |
gone
2016/03/08 23:03:52
Should this check for equality with TextUtils.equa
dominickn
2016/03/09 08:18:33
I just followed what was already in this test - ha
gone
2016/03/10 23:27:21
Acknowledged.
| |
328 assertTrue(actual.contains("webapp2")); | |
329 | |
330 // Verify that the last used time for both web apps is WebappDataStorage .NULL_LAST_USED. | |
331 long actualLastUsed = webapp1Prefs.getLong( | |
332 WebappDataStorage.KEY_LAST_USED, WebappDataStorage.NULL_LAST_USE D); | |
333 assertEquals(WebappDataStorage.NULL_LAST_USED, actualLastUsed); | |
334 actualLastUsed = webapp2Prefs.getLong( | |
335 WebappDataStorage.KEY_LAST_USED, WebappDataStorage.NULL_LAST_USE D); | |
336 assertEquals(WebappDataStorage.NULL_LAST_USED, actualLastUsed); | |
337 | |
338 // Verify that the origin URL for both web apps is WebappDataStorage.INV ALID_ORIGIN_URL. | |
339 String actualOriginUrl = webapp1Prefs.getString( | |
340 WebappDataStorage.KEY_ORIGIN_URL, WebappDataStorage.INVALID_ORIG IN_URL); | |
341 assertEquals(WebappDataStorage.INVALID_ORIGIN_URL, actualOriginUrl); | |
342 actualOriginUrl = webapp2Prefs.getString( | |
343 WebappDataStorage.KEY_ORIGIN_URL, WebappDataStorage.INVALID_ORIG IN_URL); | |
344 assertEquals(WebappDataStorage.INVALID_ORIGIN_URL, actualOriginUrl); | |
345 } | |
346 | |
347 @Test | |
348 @Feature({"Webapp"}) | |
349 public void testOpenAfterClearWebappHistory() throws Exception { | |
350 final String webappOrigin = "https://www.google.com"; | |
351 WebappRegistry.registerWebapp(Robolectric.application, "webapp", webappO rigin); | |
352 BackgroundShadowAsyncTask.runBackgroundTasks(); | |
353 | |
354 SharedPreferences webappPrefs = Robolectric.application.getSharedPrefere nces( | |
355 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "webapp", Context.M ODE_PRIVATE); | |
356 | |
357 WebappRegistry.clearWebappHistory(Robolectric.application, null); | |
358 BackgroundShadowAsyncTask.runBackgroundTasks(); | |
359 | |
360 // Open the webapp up and set the origin. | |
361 WebappDataStorage.open(Robolectric.application, "webapp"); | |
362 WebappDataStorage.updateOriginUrl(Robolectric.application, "webapp", web appOrigin); | |
363 BackgroundShadowAsyncTask.runBackgroundTasks(); | |
364 | |
365 // Verify that the last used time is valid and the origin URL is updated . | |
366 long actualLastUsed = webappPrefs.getLong( | |
367 WebappDataStorage.KEY_LAST_USED, WebappDataStorage.INVALID_LAST_ USED); | |
368 assertTrue(WebappDataStorage.INVALID_LAST_USED != actualLastUsed); | |
369 assertTrue(WebappDataStorage.NULL_LAST_USED != actualLastUsed); | |
370 String actualOriginUrl = webappPrefs.getString( | |
371 WebappDataStorage.KEY_ORIGIN_URL, WebappDataStorage.INVALID_ORIG IN_URL); | |
372 assertEquals(webappOrigin, actualOriginUrl); | |
373 } | |
374 | |
375 | |
287 private Set<String> addWebappsToRegistry(String... webapps) { | 376 private Set<String> addWebappsToRegistry(String... webapps) { |
288 final Set<String> expected = new HashSet<String>(Arrays.asList(webapps)) ; | 377 final Set<String> expected = new HashSet<String>(Arrays.asList(webapps)) ; |
289 mSharedPreferences.edit() | 378 mSharedPreferences.edit() |
290 .putStringSet(WebappRegistry.KEY_WEBAPP_SET, expected) | 379 .putStringSet(WebappRegistry.KEY_WEBAPP_SET, expected) |
291 .commit(); | 380 .commit(); |
292 return expected; | 381 return expected; |
293 } | 382 } |
294 | 383 |
295 private Set<String> getRegisteredWebapps() { | 384 private Set<String> getRegisteredWebapps() { |
296 return mSharedPreferences.getStringSet( | 385 return mSharedPreferences.getStringSet( |
297 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); | 386 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); |
298 } | 387 } |
299 } | 388 } |
OLD | NEW |