From f9b3dba9e36564e370de47e08610077a5f933c5c Mon Sep 17 00:00:00 2001 From: RHenigan Date: Fri, 3 Apr 2020 11:40:52 -0400 Subject: Pull logic into DeviceIconManager and use listener --- .../LockScreenDeviceIconManagerTests.java | 162 +++------------------ 1 file changed, 24 insertions(+), 138 deletions(-) (limited to 'android/sdl_android/src/androidTest/java/com') diff --git a/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/lockscreen/LockScreenDeviceIconManagerTests.java b/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/lockscreen/LockScreenDeviceIconManagerTests.java index 0526d3088..04e661b9e 100644 --- a/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/lockscreen/LockScreenDeviceIconManagerTests.java +++ b/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/lockscreen/LockScreenDeviceIconManagerTests.java @@ -18,6 +18,7 @@ import java.math.BigInteger; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.isNull; @@ -28,7 +29,7 @@ public class LockScreenDeviceIconManagerTests extends AndroidTestCase2 { TemporaryFolder tempFolder = new TemporaryFolder(); private LockScreenDeviceIconManager lockScreenDeviceIconManager; - private static final String ICON_URL = "http://i.imgur.com/TgkvOIZ.png"; + private static final String ICON_URL = "https://i.imgur.com/TgkvOIZ.png"; private static final String LAST_UPDATED_TIME = "lastUpdatedTime"; private static final String STORED_PATH = "storedPath"; @@ -40,178 +41,63 @@ public class LockScreenDeviceIconManagerTests extends AndroidTestCase2 { super.tearDown(); } - public void testUpdateCacheImageShouldReturnTrueWhenSharedPreferencesDoesNotExist() { + public void testRetrieveIconShouldCallOnErrorAndOnImageRetrievedWithNullWhenGivenURLThatCannotDownloadAndIconIsNotCached() { final SharedPreferences sharedPrefs = Mockito.mock(SharedPreferences.class); final Context context = Mockito.mock(Context.class); + final LockScreenDeviceIconManager.OnIconRetrievedListener listener = Mockito.mock(LockScreenDeviceIconManager.OnIconRetrievedListener.class); + Mockito.when(context.getSharedPreferences(anyString(), anyInt())).thenReturn(sharedPrefs); Mockito.when(sharedPrefs.getString(anyString(), (String) isNull())).thenReturn(null); lockScreenDeviceIconManager = new LockScreenDeviceIconManager(context); - boolean imageUpToDate = lockScreenDeviceIconManager.isIconCachedAndValid(ICON_URL); - assertFalse(imageUpToDate); + lockScreenDeviceIconManager.retrieveIcon("", listener); + verify(listener, times(1)).onError(anyString()); + verify(listener, times(1)).onImageRetrieved(null); } - public void testUpdateCacheImageShouldReturnTrueWhenUnableToReadSharedPreference() { + public void testRetrieveIconShouldCallOnImageOnImageRetrievedWithIconWhenIconUpdateTimeIsNullFromSharedPref() { final SharedPreferences sharedPrefs = Mockito.mock(SharedPreferences.class); - final SharedPreferences.Editor sharedPrefsEditor = Mockito.mock(SharedPreferences.Editor.class); final Context context = Mockito.mock(Context.class); - Mockito.when(context.getSharedPreferences(anyString(), anyInt())).thenReturn(sharedPrefs); - Mockito.when(sharedPrefs.getString(anyString(), (String) isNull())).thenReturn(""); - Mockito.when(context.getSharedPreferences(anyString(), anyInt())).thenReturn(sharedPrefs); - Mockito.when(sharedPrefs.edit()).thenReturn(sharedPrefsEditor); - Mockito.when(sharedPrefsEditor.remove(anyString())).thenReturn(sharedPrefsEditor); - Mockito.when(sharedPrefsEditor.commit()).thenReturn(true); - - - lockScreenDeviceIconManager = new LockScreenDeviceIconManager(context); - boolean imageUpToDate = lockScreenDeviceIconManager.isIconCachedAndValid(ICON_URL); - assertFalse(imageUpToDate); - } + final LockScreenDeviceIconManager.OnIconRetrievedListener listener = Mockito.mock(LockScreenDeviceIconManager.OnIconRetrievedListener.class); - public void testUpdateCacheImageShouldReturnTrueSharedPreferenceReturnsAnOutdatedIcon() { - final SharedPreferences sharedPrefs = Mockito.mock(SharedPreferences.class); - final Context context = Mockito.mock(Context.class); Mockito.when(context.getSharedPreferences(anyString(), anyInt())).thenReturn(sharedPrefs); - Mockito.when(sharedPrefs.getString(anyString(), (String) isNull())).thenReturn(daysToMillisecondsAsString(35)); + Mockito.when(sharedPrefs.getString(anyString(), (String) isNull())).thenReturn(null); lockScreenDeviceIconManager = new LockScreenDeviceIconManager(context); - boolean imageUpToDate = lockScreenDeviceIconManager.isIconCachedAndValid(ICON_URL); - assertFalse(imageUpToDate); + lockScreenDeviceIconManager.retrieveIcon(ICON_URL, listener); + verify(listener, times(1)).onImageRetrieved((Bitmap) any()); } - public void testUpdateCacheImageShouldReturnFalseWhenSharedPreferenceReturnsAnUpdatedIcon() { - final SharedPreferences sharedPrefs = Mockito.mock(SharedPreferences.class); - final Context context = Mockito.mock(Context.class); - Mockito.when(context.getSharedPreferences(anyString(), anyInt())).thenReturn(sharedPrefs); - Mockito.when(sharedPrefs.getString(anyString(), (String) isNull())).thenReturn(daysToMillisecondsAsString(15)); - - lockScreenDeviceIconManager = new LockScreenDeviceIconManager(context); - boolean imageUpToDate = lockScreenDeviceIconManager.isIconCachedAndValid(ICON_URL); - assertTrue(imageUpToDate); - } - public void testSaveFileToCacheShouldReturnBeforeWritingSharedPrefsIfSavingToCacheFails() { + public void testRetrieveIconShouldCallOnImageOnImageRetrievedWithIconWhenCachedIconExpired() { final SharedPreferences sharedPrefs = Mockito.mock(SharedPreferences.class); - final SharedPreferences.Editor sharedPrefsEditor = Mockito.mock(SharedPreferences.Editor.class); final Context context = Mockito.mock(Context.class); - Mockito.when(context.getSharedPreferences(anyString(), anyInt())).thenReturn(sharedPrefs); - Mockito.when(sharedPrefs.edit()).thenReturn(sharedPrefsEditor); - - Bitmap deviceLogo = null; - try { - deviceLogo = AndroidTools.downloadImage(ICON_URL); - lockScreenDeviceIconManager = new LockScreenDeviceIconManager(context); - lockScreenDeviceIconManager.saveFileToCache(deviceLogo, ICON_URL); - verify(sharedPrefs, times(0)).edit(); - verify(sharedPrefsEditor, times(0)).putString(anyString(), anyString()); - } catch (IOException e) { - e.printStackTrace(); - } - } + final LockScreenDeviceIconManager.OnIconRetrievedListener listener = Mockito.mock(LockScreenDeviceIconManager.OnIconRetrievedListener.class); - public void testSaveFileToCacheShouldWriteToSharedPrefsIfSaveIconIsSuccessful() { - final SharedPreferences sharedPrefs = Mockito.mock(SharedPreferences.class); - final SharedPreferences.Editor sharedPrefsEditor = Mockito.mock(SharedPreferences.Editor.class); - final Context context = Mockito.mock(Context.class); Mockito.when(context.getSharedPreferences(anyString(), anyInt())).thenReturn(sharedPrefs); - Mockito.when(sharedPrefs.edit()).thenReturn(sharedPrefsEditor); - try { - tempFolder.create(); - Mockito.when(context.getCacheDir()).thenReturn(tempFolder.newFolder()); - } catch (IOException e) { - e.printStackTrace(); - } - - Bitmap deviceLogo = null; - try { - deviceLogo = AndroidTools.downloadImage(ICON_URL); - lockScreenDeviceIconManager = new LockScreenDeviceIconManager(context); - lockScreenDeviceIconManager.saveFileToCache(deviceLogo, ICON_URL); - verify(sharedPrefs, times(1)).edit(); - verify(sharedPrefsEditor, times(1)).putString(anyString(), anyString()); - } catch (IOException e) { - e.printStackTrace(); - } - } - - public void testGetFileFromCacheShouldReturnNullIfFailedToGetSystemPref() { - final SharedPreferences sharedPrefs = Mockito.mock(SharedPreferences.class); - final Context context = Mockito.mock(Context.class); - Mockito.when(context.getSharedPreferences(anyString(), anyInt())).thenReturn(sharedPrefs); - Mockito.when(sharedPrefs.getString(anyString(), (String) isNull())).thenReturn(null); + Mockito.when(sharedPrefs.getString(anyString(), (String) isNull())).thenReturn(daysToMillisecondsAsString(31)); lockScreenDeviceIconManager = new LockScreenDeviceIconManager(context); - Bitmap cachedIcon = lockScreenDeviceIconManager.getFileFromCache(ICON_URL); - assertNull(cachedIcon); + lockScreenDeviceIconManager.retrieveIcon(ICON_URL, listener); + verify(listener, times(1)).onImageRetrieved((Bitmap) any()); } - public void testGetFileFromCacheShouldReturnNullIfInvalidDataFromSharedPref() { + public void testRetrieveIconShouldCallOnImageRetrievedWithIconWhenCachedIconIsUpToDate() { final SharedPreferences sharedPrefs = Mockito.mock(SharedPreferences.class); - final SharedPreferences.Editor sharedPrefsEditor = Mockito.mock(SharedPreferences.Editor.class); final Context context = Mockito.mock(Context.class); - Mockito.when(context.getSharedPreferences(anyString(), anyInt())).thenReturn(sharedPrefs); - Mockito.when(sharedPrefs.edit()).thenReturn(sharedPrefsEditor); - Mockito.when(sharedPrefsEditor.remove(anyString())).thenReturn(sharedPrefsEditor); - Mockito.when(sharedPrefsEditor.commit()).thenReturn(true); - Mockito.when(sharedPrefs.getString(anyString(), (String) isNull())).thenReturn(""); - - - lockScreenDeviceIconManager = new LockScreenDeviceIconManager(context); - Bitmap cachedIcon = lockScreenDeviceIconManager.getFileFromCache(ICON_URL); - assertNull(cachedIcon); - } - - public void testGetFileFromCacheShouldReturnNullIfFailedToFindIcon() { - final SharedPreferences sharedPrefs = Mockito.mock(SharedPreferences.class); final SharedPreferences.Editor sharedPrefsEditor = Mockito.mock(SharedPreferences.Editor.class); - final Context context = Mockito.mock(Context.class); - Mockito.when(context.getSharedPreferences(anyString(), anyInt())).thenReturn(sharedPrefs); - Mockito.when(sharedPrefs.edit()).thenReturn(sharedPrefsEditor); - Mockito.when(sharedPrefsEditor.remove(anyString())).thenReturn(sharedPrefsEditor); - Mockito.when(sharedPrefsEditor.commit()).thenReturn(true); - Mockito.when(sharedPrefs.getString(anyString(), (String) isNull())).thenReturn(daysToMillisecondsAsString(15)); - - try { - tempFolder.create(); - Mockito.when(context.getCacheDir()).thenReturn(tempFolder.newFolder()); - } catch (IOException e) { - e.printStackTrace(); - } - + final LockScreenDeviceIconManager.OnIconRetrievedListener listener = Mockito.mock(LockScreenDeviceIconManager.OnIconRetrievedListener.class); - lockScreenDeviceIconManager = new LockScreenDeviceIconManager(context); - Bitmap cachedIcon = lockScreenDeviceIconManager.getFileFromCache(ICON_URL); - assertNull(cachedIcon); - } - - public void testGetFileFromCacheShouldReturnBitmapIfIconFoundInCache() { - final SharedPreferences sharedPrefs = Mockito.mock(SharedPreferences.class); - final SharedPreferences.Editor sharedPrefsEditor = Mockito.mock(SharedPreferences.Editor.class); - final Context context = Mockito.mock(Context.class); Mockito.when(context.getSharedPreferences(anyString(), anyInt())).thenReturn(sharedPrefs); + Mockito.when(sharedPrefs.getString(anyString(), (String) isNull())).thenReturn(daysToMillisecondsAsString(15)); Mockito.when(sharedPrefs.edit()).thenReturn(sharedPrefsEditor); Mockito.when(sharedPrefsEditor.remove(anyString())).thenReturn(sharedPrefsEditor); - Mockito.when(sharedPrefsEditor.commit()).thenReturn(true); - Bitmap deviceLogo = null; lockScreenDeviceIconManager = new LockScreenDeviceIconManager(context); - - try { - tempFolder.create(); - File newFolder = tempFolder.newFolder(); - Mockito.when(context.getCacheDir()).thenReturn(newFolder); - deviceLogo = AndroidTools.downloadImage(ICON_URL); - Mockito.when(sharedPrefs.getString(anyString(), (String) isNull())).thenReturn(daysToMillisecondsAsString(15)); - } catch (IOException e) { - e.printStackTrace(); - } - - lockScreenDeviceIconManager = new LockScreenDeviceIconManager(context); - lockScreenDeviceIconManager.saveFileToCache(deviceLogo, ICON_URL); - Bitmap cachedIcon = lockScreenDeviceIconManager.getFileFromCache(ICON_URL); - assertNotNull(cachedIcon); + lockScreenDeviceIconManager.retrieveIcon(ICON_URL, listener); + verify(listener, times(1)).onImageRetrieved((Bitmap) any()); } - + private String daysToMillisecondsAsString(int days) { long milliSeconds = (long) days * 24 * 60 * 60 * 1000; long previousDay = System.currentTimeMillis() - milliSeconds; -- cgit v1.2.1