@@ -31,7 +31,6 @@ public class LockScreenDeviceIconManagerTests extends AndroidTestCase2 {
3131 private static final String ICON_URL = "http://i.imgur.com/TgkvOIZ.png" ;
3232 private static final String LAST_UPDATED_TIME = "lastUpdatedTime" ;
3333 private static final String STORED_PATH = "storedPath" ;
34- private static final String INVALID_JSON_STRING = "Invalid JSON" ;
3534
3635 public void setup () throws Exception {
3736 super .setUp ();
@@ -48,8 +47,8 @@ public void testUpdateCacheImageShouldReturnTrueWhenSharedPreferencesDoesNotExis
4847 Mockito .when (sharedPrefs .getString (anyString (), (String ) isNull ())).thenReturn (null );
4948
5049 lockScreenDeviceIconManager = new LockScreenDeviceIconManager (context );
51- boolean shouldUpdate = lockScreenDeviceIconManager .shouldUpdateCachedImage (ICON_URL );
52- assertTrue ( shouldUpdate );
50+ boolean imageUpToDate = lockScreenDeviceIconManager .isIconCachedAndValid (ICON_URL );
51+ assertFalse ( imageUpToDate );
5352 }
5453
5554 public void testUpdateCacheImageShouldReturnTrueWhenUnableToReadSharedPreference () {
@@ -65,8 +64,8 @@ public void testUpdateCacheImageShouldReturnTrueWhenUnableToReadSharedPreference
6564
6665
6766 lockScreenDeviceIconManager = new LockScreenDeviceIconManager (context );
68- boolean shouldUpdate = lockScreenDeviceIconManager .shouldUpdateCachedImage (ICON_URL );
69- assertTrue ( shouldUpdate );
67+ boolean imageUpToDate = lockScreenDeviceIconManager .isIconCachedAndValid (ICON_URL );
68+ assertFalse ( imageUpToDate );
7069 }
7170
7271 public void testUpdateCacheImageShouldReturnTrueSharedPreferenceReturnsAnOutdatedIcon () {
@@ -76,8 +75,8 @@ public void testUpdateCacheImageShouldReturnTrueSharedPreferenceReturnsAnOutdate
7675 Mockito .when (sharedPrefs .getString (anyString (), (String ) isNull ())).thenReturn (daysToMillisecondsAsString (35 ));
7776
7877 lockScreenDeviceIconManager = new LockScreenDeviceIconManager (context );
79- boolean shouldUpdate = lockScreenDeviceIconManager .shouldUpdateCachedImage (ICON_URL );
80- assertTrue ( shouldUpdate );
78+ boolean imageUpToDate = lockScreenDeviceIconManager .isIconCachedAndValid (ICON_URL );
79+ assertFalse ( imageUpToDate );
8180 }
8281
8382 public void testUpdateCacheImageShouldReturnFalseWhenSharedPreferenceReturnsAnUpdatedIcon () {
@@ -87,8 +86,8 @@ public void testUpdateCacheImageShouldReturnFalseWhenSharedPreferenceReturnsAnUp
8786 Mockito .when (sharedPrefs .getString (anyString (), (String ) isNull ())).thenReturn (daysToMillisecondsAsString (15 ));
8887
8988 lockScreenDeviceIconManager = new LockScreenDeviceIconManager (context );
90- boolean shouldUpdate = lockScreenDeviceIconManager .shouldUpdateCachedImage (ICON_URL );
91- assertFalse ( shouldUpdate );
89+ boolean imageUpToDate = lockScreenDeviceIconManager .isIconCachedAndValid (ICON_URL );
90+ assertTrue ( imageUpToDate );
9291 }
9392
9493 public void testSaveFileToCacheShouldReturnBeforeWritingSharedPrefsIfSavingToCacheFails () {
@@ -170,7 +169,7 @@ public void testGetFileFromCacheShouldReturnNullIfFailedToFindIcon() {
170169 Mockito .when (sharedPrefs .edit ()).thenReturn (sharedPrefsEditor );
171170 Mockito .when (sharedPrefsEditor .remove (anyString ())).thenReturn (sharedPrefsEditor );
172171 Mockito .when (sharedPrefsEditor .commit ()).thenReturn (true );
173- Mockito .when (sharedPrefs .getString (anyString (), (String ) isNull ())).thenReturn (buildJSONAsString (15 , "" ));
172+ Mockito .when (sharedPrefs .getString (anyString (), (String ) isNull ())).thenReturn (daysToMillisecondsAsString (15 ));
174173
175174 try {
176175 tempFolder .create ();
@@ -195,12 +194,14 @@ public void testGetFileFromCacheShouldReturnBitmapIfIconFoundInCache() {
195194 Mockito .when (sharedPrefsEditor .commit ()).thenReturn (true );
196195 Bitmap deviceLogo = null ;
197196
197+ lockScreenDeviceIconManager = new LockScreenDeviceIconManager (context );
198+
198199 try {
199200 tempFolder .create ();
200201 File newFolder = tempFolder .newFolder ();
201202 Mockito .when (context .getCacheDir ()).thenReturn (newFolder );
202203 deviceLogo = AndroidTools .downloadImage (ICON_URL );
203- Mockito .when (sharedPrefs .getString (anyString (), (String ) isNull ())).thenReturn (buildJSONAsString (15 , newFolder . getPath () + "/sdl/lock_screen_icon/" + getMD5HashFromIconUrl ( ICON_URL ) ));
204+ Mockito .when (sharedPrefs .getString (anyString (), (String ) isNull ())).thenReturn (daysToMillisecondsAsString (15 ));
204205 } catch (IOException e ) {
205206 e .printStackTrace ();
206207 }
@@ -211,41 +212,9 @@ public void testGetFileFromCacheShouldReturnBitmapIfIconFoundInCache() {
211212 assertNotNull (cachedIcon );
212213 }
213214
214- private String buildJSONAsString (long DaysOld , String cahceIconPath ) {
215- JSONObject jsonObject = new JSONObject ();
216- try {
217- jsonObject .put (STORED_PATH , cahceIconPath );
218- long timeDifferenceInMilliSeconds = DaysOld * 1000 * 60 * 60 * 24 ;
219- jsonObject .put (LAST_UPDATED_TIME , System .currentTimeMillis () - timeDifferenceInMilliSeconds );
220- return jsonObject .toString ();
221- } catch (JSONException e ) {
222- e .printStackTrace ();
223- return null ;
224- }
225- }
226-
227- private String getMD5HashFromIconUrl (String iconUrl ) {
228- String iconHash = null ;
229- try {
230- MessageDigest md = MessageDigest .getInstance ("MD5" );
231- byte [] messageDigest = md .digest (iconUrl .getBytes ());
232- BigInteger no = new BigInteger (1 , messageDigest );
233- String hashtext = no .toString (16 );
234- while (hashtext .length () < 32 ) {
235- hashtext = "0" + hashtext ;
236- }
237- iconHash = hashtext ;
238- } catch (NoSuchAlgorithmException e ) {
239- e .printStackTrace ();
240- }
241- return iconHash ;
242- }
243-
244215 private String daysToMillisecondsAsString (int days ) {
245216 long milliSeconds = (long ) days * 24 * 60 * 60 * 1000 ;
246217 long previousDay = System .currentTimeMillis () - milliSeconds ;
247- return previousDay + "" ;
218+ return String . valueOf ( previousDay ) ;
248219 }
249-
250-
251220}
0 commit comments