Skip to content

Commit 35cc26b

Browse files
committed
Add happy path test for getFileFromCache
1 parent 9855cac commit 35cc26b

2 files changed

Lines changed: 52 additions & 7 deletions

File tree

android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/lockscreen/LockScreenDeviceIconManagerTests.java

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.content.SharedPreferences;
55
import android.graphics.Bitmap;
66
import android.graphics.BitmapFactory;
7+
import android.util.Log;
78

89
import com.smartdevicelink.AndroidTestCase2;
910
import com.smartdevicelink.util.AndroidTools;
@@ -15,6 +16,9 @@
1516

1617
import java.io.File;
1718
import java.io.IOException;
19+
import java.math.BigInteger;
20+
import java.security.MessageDigest;
21+
import java.security.NoSuchAlgorithmException;
1822

1923
import static org.mockito.ArgumentMatchers.anyInt;
2024
import 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
}

android/sdl_android/src/main/java/com/smartdevicelink/managers/lockscreen/LockScreenDeviceIconManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ Bitmap getFileFromCache(String iconUrl) {
126126
}
127127
}
128128

129-
private void writeDeviceIconParametersToSystemPreferences(String iconHash, JSONObject jsonObject) throws JSONException {
129+
private void writeDeviceIconParametersToSystemPreferences(String iconHash, JSONObject jsonObject) {
130130
Log.d(TAG, "Attempting to write to system preferences");
131131
SharedPreferences sharedPref = this.context.getSharedPreferences(SDL_DEVICE_STATUS_SHARED_PREFS, Context.MODE_PRIVATE);
132132
SharedPreferences.Editor editor = sharedPref.edit();

0 commit comments

Comments
 (0)