22
33import android .content .Context ;
44import android .content .SharedPreferences ;
5+ import android .graphics .Bitmap ;
6+ import android .graphics .BitmapFactory ;
57
68import com .smartdevicelink .AndroidTestCase2 ;
9+ import com .smartdevicelink .util .AndroidTools ;
710
811import org .json .JSONException ;
912import org .json .JSONObject ;
1013import org .mockito .Mockito ;
1114
15+ import java .io .IOException ;
16+
1217import static org .mockito .ArgumentMatchers .anyInt ;
1318import static org .mockito .ArgumentMatchers .anyString ;
1419import static org .mockito .ArgumentMatchers .isNull ;
20+ import static org .mockito .Mockito .times ;
21+ import static org .mockito .Mockito .verify ;
1522
1623public class LockScreenDeviceIconManagerTests extends AndroidTestCase2 {
1724
@@ -29,7 +36,7 @@ public void tearDown() throws Exception {
2936 super .tearDown ();
3037 }
3138
32- public void testShouldReturnTrueWhenSharedPreferencesDoesNotExist () {
39+ public void testUpdateCacheImageShouldReturnTrueWhenSharedPreferencesDoesNotExist () {
3340 final SharedPreferences sharedPrefs = Mockito .mock (SharedPreferences .class );
3441 final Context context = Mockito .mock (Context .class );
3542 Mockito .when (context .getSharedPreferences (anyString (), anyInt ())).thenReturn (sharedPrefs );
@@ -40,7 +47,7 @@ public void testShouldReturnTrueWhenSharedPreferencesDoesNotExist() {
4047 assertTrue (shouldUpdate );
4148 }
4249
43- public void testShouldReturnTrueWhenUnableToReadSharedPreference () {
50+ public void testUpdateCacheImageShouldReturnTrueWhenUnableToReadSharedPreference () {
4451 final SharedPreferences sharedPrefs = Mockito .mock (SharedPreferences .class );
4552 final Context context = Mockito .mock (Context .class );
4653 Mockito .when (context .getSharedPreferences (anyString (), anyInt ())).thenReturn (sharedPrefs );
@@ -52,7 +59,7 @@ public void testShouldReturnTrueWhenUnableToReadSharedPreference() {
5259 assertTrue (shouldUpdate );
5360 }
5461
55- public void testShouldReturnTrueSharedPreferenceReturnsAnOutdatedIcon () {
62+ public void testUpdateCacheImageShouldReturnTrueSharedPreferenceReturnsAnOutdatedIcon () {
5663 final SharedPreferences sharedPrefs = Mockito .mock (SharedPreferences .class );
5764 final Context context = Mockito .mock (Context .class );
5865 Mockito .when (context .getSharedPreferences (anyString (), anyInt ())).thenReturn (sharedPrefs );
@@ -63,7 +70,7 @@ public void testShouldReturnTrueSharedPreferenceReturnsAnOutdatedIcon() {
6370 assertTrue (shouldUpdate );
6471 }
6572
66- public void testShouldReturnFalseWhenSharedPreferenceReturnsAnUpdatedIcon () {
73+ public void testUpdateCacheImageShouldReturnFalseWhenSharedPreferenceReturnsAnUpdatedIcon () {
6774 final SharedPreferences sharedPrefs = Mockito .mock (SharedPreferences .class );
6875 final Context context = Mockito .mock (Context .class );
6976 Mockito .when (context .getSharedPreferences (anyString (), anyInt ())).thenReturn (sharedPrefs );
@@ -74,6 +81,58 @@ public void testShouldReturnFalseWhenSharedPreferenceReturnsAnUpdatedIcon() {
7481 assertFalse (shouldUpdate );
7582 }
7683
84+ public void testSaveFileToCacheShouldReturnBeforeWritingSharedPrefsIfSavingToCacheFails () {
85+ final SharedPreferences sharedPrefs = Mockito .mock (SharedPreferences .class );
86+ final SharedPreferences .Editor sharedPrefsEditor = Mockito .mock (SharedPreferences .Editor .class );
87+ final Context context = Mockito .mock (Context .class );
88+ Mockito .when (context .getSharedPreferences (anyString (), anyInt ())).thenReturn (sharedPrefs );
89+ Mockito .when (sharedPrefs .edit ()).thenReturn (sharedPrefsEditor );
90+
91+ Bitmap deviceLogo = null ;
92+ try {
93+ deviceLogo = AndroidTools .downloadImage (ICON_URL );
94+ lockScreenDeviceIconManager = new LockScreenDeviceIconManager (context );
95+ lockScreenDeviceIconManager .saveFileToCache (deviceLogo , ICON_URL );
96+ verify (sharedPrefs , times (0 )).edit ();
97+ verify (sharedPrefsEditor , times (0 )).putString (anyString (), anyString ());
98+ } catch (IOException e ) {
99+ e .printStackTrace ();
100+ }
101+ }
102+
103+ //TODO Add Test For Passing saveFileToCache
104+
105+ public void testGetFileFromCacheShouldReturnNullIfFailedToGetSystemPref () {
106+ final SharedPreferences sharedPrefs = Mockito .mock (SharedPreferences .class );
107+ final Context context = Mockito .mock (Context .class );
108+ Mockito .when (context .getSharedPreferences (anyString (), anyInt ())).thenReturn (sharedPrefs );
109+ Mockito .when (sharedPrefs .getString (anyString (), (String ) isNull ())).thenReturn (null );
110+
111+ lockScreenDeviceIconManager = new LockScreenDeviceIconManager (context );
112+ Bitmap cachedIcon = lockScreenDeviceIconManager .getFileFromCache (ICON_URL );
113+ assertNull (cachedIcon );
114+ }
115+
116+ public void testGetFileFromCacheShouldReturnNullIfInvalidDataFromSharedPref () {
117+ final SharedPreferences sharedPrefs = Mockito .mock (SharedPreferences .class );
118+ final SharedPreferences .Editor sharedPrefsEditor = Mockito .mock (SharedPreferences .Editor .class );
119+ final Context context = Mockito .mock (Context .class );
120+ Mockito .when (context .getSharedPreferences (anyString (), anyInt ())).thenReturn (sharedPrefs );
121+ Mockito .when (sharedPrefs .edit ()).thenReturn (sharedPrefsEditor );
122+ Mockito .when (sharedPrefsEditor .remove (anyString ())).thenReturn (sharedPrefsEditor );
123+ Mockito .when (sharedPrefsEditor .commit ()).thenReturn (true );
124+ Mockito .when (sharedPrefs .getString (anyString (), (String ) isNull ())).thenReturn (INVALID_JSON_STRING );
125+
126+
127+ lockScreenDeviceIconManager = new LockScreenDeviceIconManager (context );
128+ Bitmap cachedIcon = lockScreenDeviceIconManager .getFileFromCache (ICON_URL );
129+ assertNull (cachedIcon );
130+ }
131+
132+ //TODO Add test for passing getFileFromCache
133+
134+ //TODO Add test for failing to read file from cache
135+
77136 private String buildJSONAsString (long DaysOld ) {
78137 JSONObject jsonObject = new JSONObject ();
79138 try {
0 commit comments