44import android .content .SharedPreferences ;
55import android .graphics .Bitmap ;
66import android .graphics .BitmapFactory ;
7+ import android .util .Log ;
78
89import com .smartdevicelink .AndroidTestCase2 ;
910import com .smartdevicelink .util .AndroidTools ;
1516
1617import java .io .File ;
1718import java .io .IOException ;
19+ import java .math .BigInteger ;
20+ import java .security .MessageDigest ;
21+ import java .security .NoSuchAlgorithmException ;
1822
1923import static org .mockito .ArgumentMatchers .anyInt ;
2024import static org .mockito .ArgumentMatchers .anyString ;
@@ -66,7 +70,7 @@ public void testUpdateCacheImageShouldReturnTrueSharedPreferenceReturnsAnOutdate
6670 final SharedPreferences sharedPrefs = Mockito .mock (SharedPreferences .class );
6771 final Context context = Mockito .mock (Context .class );
6872 Mockito .when (context .getSharedPreferences (anyString (), anyInt ())).thenReturn (sharedPrefs );
69- Mockito .when (sharedPrefs .getString (anyString (), (String ) isNull ())).thenReturn (buildJSONAsString (35 ));
73+ Mockito .when (sharedPrefs .getString (anyString (), (String ) isNull ())).thenReturn (buildJSONAsString (35 , "" ));
7074
7175 lockScreenDeviceIconManager = new LockScreenDeviceIconManager (context );
7276 boolean shouldUpdate = lockScreenDeviceIconManager .updateCachedImage (ICON_URL );
@@ -77,7 +81,7 @@ public void testUpdateCacheImageShouldReturnFalseWhenSharedPreferenceReturnsAnUp
7781 final SharedPreferences sharedPrefs = Mockito .mock (SharedPreferences .class );
7882 final Context context = Mockito .mock (Context .class );
7983 Mockito .when (context .getSharedPreferences (anyString (), anyInt ())).thenReturn (sharedPrefs );
80- Mockito .when (sharedPrefs .getString (anyString (), (String ) isNull ())).thenReturn (buildJSONAsString (15 ));
84+ Mockito .when (sharedPrefs .getString (anyString (), (String ) isNull ())).thenReturn (buildJSONAsString (15 , "" ));
8185
8286 lockScreenDeviceIconManager = new LockScreenDeviceIconManager (context );
8387 boolean shouldUpdate = lockScreenDeviceIconManager .updateCachedImage (ICON_URL );
@@ -163,7 +167,7 @@ public void testGetFileFromCacheShouldReturnNullIfFailedToFindIcon() {
163167 Mockito .when (sharedPrefs .edit ()).thenReturn (sharedPrefsEditor );
164168 Mockito .when (sharedPrefsEditor .remove (anyString ())).thenReturn (sharedPrefsEditor );
165169 Mockito .when (sharedPrefsEditor .commit ()).thenReturn (true );
166- Mockito .when (sharedPrefs .getString (anyString (), (String ) isNull ())).thenReturn (buildJSONAsString (15 ));
170+ Mockito .when (sharedPrefs .getString (anyString (), (String ) isNull ())).thenReturn (buildJSONAsString (15 , "" ));
167171
168172 try {
169173 tempFolder .create ();
@@ -178,12 +182,36 @@ public void testGetFileFromCacheShouldReturnNullIfFailedToFindIcon() {
178182 assertNull (cachedIcon );
179183 }
180184
181- //TODO Add test for passing getFileFromCache
185+ public void testGetFileFromCacheShouldReturnBitmapIfIconFoundInCache () {
186+ final SharedPreferences sharedPrefs = Mockito .mock (SharedPreferences .class );
187+ final SharedPreferences .Editor sharedPrefsEditor = Mockito .mock (SharedPreferences .Editor .class );
188+ final Context context = Mockito .mock (Context .class );
189+ Mockito .when (context .getSharedPreferences (anyString (), anyInt ())).thenReturn (sharedPrefs );
190+ Mockito .when (sharedPrefs .edit ()).thenReturn (sharedPrefsEditor );
191+ Mockito .when (sharedPrefsEditor .remove (anyString ())).thenReturn (sharedPrefsEditor );
192+ Mockito .when (sharedPrefsEditor .commit ()).thenReturn (true );
193+ Bitmap deviceLogo = null ;
194+
195+ try {
196+ tempFolder .create ();
197+ File newFolder = tempFolder .newFolder ();
198+ Mockito .when (context .getCacheDir ()).thenReturn (newFolder );
199+ deviceLogo = AndroidTools .downloadImage (ICON_URL );
200+ Mockito .when (sharedPrefs .getString (anyString (), (String ) isNull ())).thenReturn (buildJSONAsString (15 , newFolder .getPath () + "/sdl/lock_screen_icon/" + getMD5HashFromIconUrl (ICON_URL )));
201+ } catch (IOException e ) {
202+ e .printStackTrace ();
203+ }
204+
205+ lockScreenDeviceIconManager = new LockScreenDeviceIconManager (context );
206+ lockScreenDeviceIconManager .saveFileToCache (deviceLogo , ICON_URL );
207+ Bitmap cachedIcon = lockScreenDeviceIconManager .getFileFromCache (ICON_URL );
208+ assertNotNull (cachedIcon );
209+ }
182210
183- private String buildJSONAsString (long DaysOld ) {
211+ private String buildJSONAsString (long DaysOld , String cahceIconUrl ) {
184212 JSONObject jsonObject = new JSONObject ();
185213 try {
186- jsonObject .put (STORED_URL , "STORED_URL" );
214+ jsonObject .put (STORED_URL , cahceIconUrl );
187215 long timeDifferenceInMilliSeconds = DaysOld * 1000 * 60 * 60 * 24 ;
188216 jsonObject .put (LAST_UPDATED_TIME , System .currentTimeMillis () - timeDifferenceInMilliSeconds );
189217 return jsonObject .toString ();
@@ -193,5 +221,22 @@ private String buildJSONAsString(long DaysOld) {
193221 }
194222 }
195223
224+ private String getMD5HashFromIconUrl (String iconUrl ) {
225+ String iconHash = null ;
226+ try {
227+ MessageDigest md = MessageDigest .getInstance ("MD5" );
228+ byte [] messageDigest = md .digest (iconUrl .getBytes ());
229+ BigInteger no = new BigInteger (1 , messageDigest );
230+ String hashtext = no .toString (16 );
231+ while (hashtext .length () < 32 ) {
232+ hashtext = "0" + hashtext ;
233+ }
234+ iconHash = hashtext ;
235+ } catch (NoSuchAlgorithmException e ) {
236+ e .printStackTrace ();
237+ }
238+ return iconHash ;
239+ }
240+
196241
197242}
0 commit comments