| 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 14 matching lines...) Expand all Loading... |
| 25 * Tests the WebappDataStorage class by ensuring that it persists data to | 25 * Tests the WebappDataStorage class by ensuring that it persists data to |
| 26 * SharedPreferences as expected. | 26 * SharedPreferences as expected. |
| 27 */ | 27 */ |
| 28 @RunWith(LocalRobolectricTestRunner.class) | 28 @RunWith(LocalRobolectricTestRunner.class) |
| 29 @Config(manifest = Config.NONE, shadows = {BackgroundShadowAsyncTask.class}) | 29 @Config(manifest = Config.NONE, shadows = {BackgroundShadowAsyncTask.class}) |
| 30 public class WebappDataStorageTest { | 30 public class WebappDataStorageTest { |
| 31 | 31 |
| 32 private SharedPreferences mSharedPreferences; | 32 private SharedPreferences mSharedPreferences; |
| 33 private boolean mCallbackCalled; | 33 private boolean mCallbackCalled; |
| 34 | 34 |
| 35 private class FetchCallback<T> implements WebappDataStorage.FetchCallback<T>
{ |
| 36 T mExpected; |
| 37 |
| 38 FetchCallback(T expected) { |
| 39 mExpected = expected; |
| 40 } |
| 41 |
| 42 @Override |
| 43 public void onDataRetrieved(T readObject) { |
| 44 mCallbackCalled = true; |
| 45 assertEquals(mExpected, readObject); |
| 46 } |
| 47 } |
| 48 |
| 35 @Before | 49 @Before |
| 36 public void setUp() throws Exception { | 50 public void setUp() throws Exception { |
| 37 mSharedPreferences = Robolectric.application | 51 mSharedPreferences = Robolectric.application |
| 38 .getSharedPreferences("webapp_test", Context.MODE_PRIVATE); | 52 .getSharedPreferences("webapp_test", Context.MODE_PRIVATE); |
| 39 | 53 |
| 40 // Set the last_used as if the web app had been registered by WebappRegi
stry. | 54 // Set the last_used as if the web app had been registered by WebappRegi
stry. |
| 41 mSharedPreferences.edit().putLong("last_used", 0).commit(); | 55 mSharedPreferences.edit().putLong("last_used", 0).commit(); |
| 42 | 56 |
| 43 mCallbackCalled = false; | 57 mCallbackCalled = false; |
| 44 } | 58 } |
| 45 | 59 |
| 46 @Test | 60 @Test |
| 47 @Feature({"Webapp"}) | 61 @Feature({"Webapp"}) |
| 48 public void testBackwardCompat() { | 62 public void testBackwardCompat() { |
| 49 assertEquals("webapp_", WebappDataStorage.SHARED_PREFS_FILE_PREFIX); | 63 assertEquals("webapp_", WebappDataStorage.SHARED_PREFS_FILE_PREFIX); |
| 50 assertEquals("splash_icon", WebappDataStorage.KEY_SPLASH_ICON); | 64 assertEquals("splash_icon", WebappDataStorage.KEY_SPLASH_ICON); |
| 51 assertEquals("last_used", WebappDataStorage.KEY_LAST_USED); | 65 assertEquals("last_used", WebappDataStorage.KEY_LAST_USED); |
| 66 assertEquals("scope", WebappDataStorage.KEY_SCOPE); |
| 52 } | 67 } |
| 53 | 68 |
| 54 @Test | 69 @Test |
| 55 @Feature({"Webapp"}) | 70 @Feature({"Webapp"}) |
| 56 public void testLastUsedRetrieval() throws Exception { | 71 public void testLastUsedRetrieval() throws Exception { |
| 57 mSharedPreferences.edit() | 72 mSharedPreferences.edit() |
| 58 .putLong(WebappDataStorage.KEY_LAST_USED, 100L) | 73 .putLong(WebappDataStorage.KEY_LAST_USED, 100L) |
| 59 .commit(); | 74 .commit(); |
| 60 | 75 |
| 61 WebappDataStorage.getLastUsedTime(Robolectric.application, "test", | 76 WebappDataStorage.getLastUsedTime(Robolectric.application, "test", |
| 62 new WebappDataStorage.FetchCallback<Long>() { | 77 new FetchCallback<Long>(new Long(100L))); |
| 63 @Override | |
| 64 public void onDataRetrieved(Long readObject) { | |
| 65 mCallbackCalled = true; | |
| 66 assertEquals(100L, (long) readObject); | |
| 67 } | |
| 68 }); | |
| 69 BackgroundShadowAsyncTask.runBackgroundTasks(); | 78 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 70 Robolectric.runUiThreadTasks(); | 79 Robolectric.runUiThreadTasks(); |
| 71 | 80 |
| 72 assertTrue(mCallbackCalled); | 81 assertTrue(mCallbackCalled); |
| 73 } | 82 } |
| 74 | 83 |
| 75 @Test | 84 @Test |
| 76 @Feature({"Webapp"}) | 85 @Feature({"Webapp"}) |
| 77 public void testOpenUpdatesLastUsed() throws Exception { | 86 public void testOpenUpdatesLastUsed() throws Exception { |
| 78 long before = System.currentTimeMillis(); | 87 long before = System.currentTimeMillis(); |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 for (int j = 0; j < actual.getHeight(); j++) { | 145 for (int j = 0; j < actual.getHeight(); j++) { |
| 137 if (actual.getPixel(i, j) != 0) return false; | 146 if (actual.getPixel(i, j) != 0) return false; |
| 138 } | 147 } |
| 139 } | 148 } |
| 140 return true; | 149 return true; |
| 141 } | 150 } |
| 142 | 151 |
| 143 private static Bitmap createBitmap() { | 152 private static Bitmap createBitmap() { |
| 144 return Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_4444); | 153 return Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_4444); |
| 145 } | 154 } |
| 146 } | 155 |
| 156 @Test |
| 157 @Feature({"Webapp"}) |
| 158 public void testScopeRetrieval() throws Exception { |
| 159 final String scope = "http://drive.google.com"; |
| 160 mSharedPreferences.edit() |
| 161 .putString(WebappDataStorage.KEY_SCOPE, scope) |
| 162 .commit(); |
| 163 |
| 164 WebappDataStorage.getScope(Robolectric.application, "test", |
| 165 new FetchCallback<String>(scope)); |
| 166 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 167 Robolectric.runUiThreadTasks(); |
| 168 |
| 169 assertTrue(mCallbackCalled); |
| 170 } |
| 171 |
| 172 @Test |
| 173 @Feature({"Webapp"}) |
| 174 public void testScopeUpdate() throws Exception { |
| 175 final String scope1 = "http://maps.google.com"; |
| 176 final String scope2 = "http://drive.google.com"; |
| 177 |
| 178 WebappDataStorage.setScope(Robolectric.application, "test", scope1); |
| 179 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 180 Robolectric.runUiThreadTasks(); |
| 181 |
| 182 assertEquals(scope1, mSharedPreferences.getString(WebappDataStorage.KEY_
SCOPE, null)); |
| 183 |
| 184 // Ensure that calling setScope with a different URL after it has been s
et |
| 185 // doesn't change the scope stored. |
| 186 WebappDataStorage.setScope(Robolectric.application, "test", scope2); |
| 187 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 188 Robolectric.runUiThreadTasks(); |
| 189 |
| 190 assertEquals(scope1, mSharedPreferences.getString(WebappDataStorage.KEY_
SCOPE, null)); |
| 191 } |
| 192 } |
| OLD | NEW |