Chromium Code Reviews| 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.SharedPreferences; | 11 import android.content.SharedPreferences; |
| 12 | 12 |
| 13 import org.chromium.base.test.util.Feature; | 13 import org.chromium.base.test.util.Feature; |
| 14 import org.chromium.testing.local.BackgroundShadowAsyncTask; | 14 import org.chromium.testing.local.BackgroundShadowAsyncTask; |
| 15 import org.chromium.testing.local.LocalRobolectricTestRunner; | 15 import org.chromium.testing.local.LocalRobolectricTestRunner; |
| 16 import org.junit.Before; | 16 import org.junit.Before; |
| 17 import org.junit.Test; | 17 import org.junit.Test; |
| 18 import org.junit.runner.RunWith; | 18 import org.junit.runner.RunWith; |
| 19 import org.robolectric.Robolectric; | 19 import org.robolectric.Robolectric; |
| 20 import org.robolectric.annotation.Config; | 20 import org.robolectric.annotation.Config; |
| 21 | 21 |
| 22 import java.util.Collections; | 22 import java.util.Collections; |
| 23 import java.util.HashSet; | 23 import java.util.HashSet; |
| 24 import java.util.Map; | |
| 24 import java.util.Set; | 25 import java.util.Set; |
| 25 | 26 |
| 26 /** | 27 /** |
| 27 * Tests the WebappRegistry class by ensuring that it persists data to | 28 * Tests the WebappRegistry class by ensuring that it persists data to |
| 28 * SharedPreferences as expected. | 29 * SharedPreferences as expected. |
| 29 */ | 30 */ |
| 30 @RunWith(LocalRobolectricTestRunner.class) | 31 @RunWith(LocalRobolectricTestRunner.class) |
| 31 @Config(manifest = Config.NONE, shadows = {BackgroundShadowAsyncTask.class}) | 32 @Config(manifest = Config.NONE, shadows = {BackgroundShadowAsyncTask.class}) |
| 32 public class WebappRegistryTest { | 33 public class WebappRegistryTest { |
| 33 | 34 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 75 SharedPreferences webAppPrefs = Robolectric.application.getSharedPrefere nces( | 76 SharedPreferences webAppPrefs = Robolectric.application.getSharedPrefere nces( |
| 76 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "test", Context.MOD E_PRIVATE); | 77 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "test", Context.MOD E_PRIVATE); |
| 77 long actual = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USED, | 78 long actual = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USED, |
| 78 WebappDataStorage.INVALID_LAST_USED); | 79 WebappDataStorage.INVALID_LAST_USED); |
| 79 assertTrue("Timestamp is out of range", before <= actual && actual <= af ter); | 80 assertTrue("Timestamp is out of range", before <= actual && actual <= af ter); |
| 80 } | 81 } |
| 81 | 82 |
| 82 @Test | 83 @Test |
| 83 @Feature({"Webapp"}) | 84 @Feature({"Webapp"}) |
| 84 public void testWebappIdsRetrieval() throws Exception { | 85 public void testWebappIdsRetrieval() throws Exception { |
| 85 final Set<String> expected = new HashSet<String>(); | 86 final Set<String> expected = addWebappsToRegistry("first", "second"); |
| 86 expected.add("first"); | |
| 87 expected.add("second"); | |
| 88 | |
| 89 mSharedPreferences.edit() | |
| 90 .putStringSet(WebappRegistry.KEY_WEBAPP_SET, expected) | |
| 91 .commit(); | |
| 92 | 87 |
| 93 WebappRegistry.getRegisteredWebappIds(Robolectric.application, | 88 WebappRegistry.getRegisteredWebappIds(Robolectric.application, |
| 94 new WebappRegistry.FetchCallback() { | 89 new WebappRegistry.FetchCallback() { |
| 95 @Override | 90 @Override |
| 96 public void onWebappIdsRetrieved(Set<String> actual) { | 91 public void onWebappIdsRetrieved(Set<String> actual) { |
| 97 mCallbackCalled = true; | 92 mCallbackCalled = true; |
| 98 assertEquals(expected, actual); | 93 assertEquals(expected, actual); |
| 99 } | 94 } |
| 100 }); | 95 }); |
| 101 BackgroundShadowAsyncTask.runBackgroundTasks(); | 96 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 102 Robolectric.runUiThreadTasks(); | 97 Robolectric.runUiThreadTasks(); |
| 103 | 98 |
| 104 assertTrue(mCallbackCalled); | 99 assertTrue(mCallbackCalled); |
| 105 } | 100 } |
| 106 | 101 |
| 107 @Test | 102 @Test |
| 108 @Feature({"Webapp"}) | 103 @Feature({"Webapp"}) |
| 109 public void testWebappIdsRetrievalRegisterRetrival() throws Exception { | 104 public void testWebappIdsRetrievalRegisterRetrival() throws Exception { |
| 110 final Set<String> expected = new HashSet<String>(); | 105 final Set<String> expected = addWebappsToRegistry("first"); |
| 111 expected.add("first"); | |
| 112 | |
| 113 mSharedPreferences.edit() | |
| 114 .putStringSet(WebappRegistry.KEY_WEBAPP_SET, expected) | |
| 115 .commit(); | |
| 116 | 106 |
| 117 WebappRegistry.getRegisteredWebappIds(Robolectric.application, | 107 WebappRegistry.getRegisteredWebappIds(Robolectric.application, |
| 118 new WebappRegistry.FetchCallback() { | 108 new WebappRegistry.FetchCallback() { |
| 119 @Override | 109 @Override |
| 120 public void onWebappIdsRetrieved(Set<String> actual) { | 110 public void onWebappIdsRetrieved(Set<String> actual) { |
| 121 mCallbackCalled = true; | 111 mCallbackCalled = true; |
| 122 assertEquals(expected, actual); | 112 assertEquals(expected, actual); |
| 123 } | 113 } |
| 124 }); | 114 }); |
| 125 BackgroundShadowAsyncTask.runBackgroundTasks(); | 115 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 142 public void onWebappIdsRetrieved(Set<String> actual) { | 132 public void onWebappIdsRetrieved(Set<String> actual) { |
| 143 mCallbackCalled = true; | 133 mCallbackCalled = true; |
| 144 assertEquals(secondExpected, actual); | 134 assertEquals(secondExpected, actual); |
| 145 } | 135 } |
| 146 }); | 136 }); |
| 147 BackgroundShadowAsyncTask.runBackgroundTasks(); | 137 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 148 Robolectric.runUiThreadTasks(); | 138 Robolectric.runUiThreadTasks(); |
| 149 | 139 |
| 150 assertTrue(mCallbackCalled); | 140 assertTrue(mCallbackCalled); |
| 151 } | 141 } |
| 142 | |
| 143 @Test | |
| 144 @Feature({"Webapp"}) | |
| 145 public void testUnregisterClearsRegistry() throws Exception { | |
| 146 addWebappsToRegistry("test"); | |
| 147 | |
| 148 WebappRegistry.unregisterAllWebapps(Robolectric.application); | |
| 149 BackgroundShadowAsyncTask.runBackgroundTasks(); | |
| 150 | |
| 151 assertTrue(getRegisteredWebapps().isEmpty()); | |
| 152 } | |
| 153 | |
| 154 @Test | |
| 155 @Feature({"Webapp"}) | |
| 156 public void testUnregisterClearsWebappDataStorage() throws Exception { | |
| 157 addWebappsToRegistry("test"); | |
| 158 | |
|
gone
2015/09/09 23:30:40
You should actually check if the file exists befor
Lalit Maganti
2015/09/10 09:41:57
Yeah this test is sort of broken. Fixed.
| |
| 159 WebappRegistry.unregisterAllWebapps(Robolectric.application); | |
| 160 BackgroundShadowAsyncTask.runBackgroundTasks(); | |
| 161 | |
| 162 SharedPreferences webAppPrefs = Robolectric.application.getSharedPrefere nces( | |
| 163 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "test", Context.MOD E_PRIVATE); | |
| 164 Map<String, ?> actual = webAppPrefs.getAll(); | |
| 165 assertTrue(actual.isEmpty()); | |
| 166 } | |
| 167 | |
| 168 private Set<String> addWebappsToRegistry(String... webapps) { | |
| 169 final Set<String> expected = new HashSet<String>(Arrays.asList(webapps)) ; | |
| 170 mSharedPreferences.edit() | |
| 171 .putStringSet(WebappRegistry.KEY_WEBAPP_SET, expected) | |
| 172 .commit(); | |
| 173 return expected; | |
| 174 } | |
| 175 | |
| 176 private Set<String> getRegisteredWebapps() { | |
| 177 return mSharedPreferences.getStringSet( | |
| 178 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); | |
| 179 } | |
| 152 } | 180 } |
| OLD | NEW |