|
10 | 10 |
|
11 | 11 | import org.json.JSONException; |
12 | 12 | import org.json.JSONObject; |
| 13 | +import org.junit.rules.TemporaryFolder; |
13 | 14 | import org.mockito.Mockito; |
14 | 15 |
|
| 16 | +import java.io.File; |
15 | 17 | import java.io.IOException; |
16 | 18 |
|
17 | 19 | import static org.mockito.ArgumentMatchers.anyInt; |
|
22 | 24 |
|
23 | 25 | public class LockScreenDeviceIconManagerTests extends AndroidTestCase2 { |
24 | 26 |
|
| 27 | + TemporaryFolder tempFolder = new TemporaryFolder(); |
25 | 28 | private LockScreenDeviceIconManager lockScreenDeviceIconManager; |
26 | 29 | private static final String ICON_URL = "http://i.imgur.com/TgkvOIZ.png"; |
27 | 30 | private static final String LAST_UPDATED_TIME = "lastUpdatedTime"; |
@@ -100,7 +103,30 @@ public void testSaveFileToCacheShouldReturnBeforeWritingSharedPrefsIfSavingToCac |
100 | 103 | } |
101 | 104 | } |
102 | 105 |
|
103 | | - //TODO Add Test For Passing saveFileToCache |
| 106 | + public void testSaveFileToCacheShouldWriteToSharedPrefsIfSaveIconIsSuccessful() { |
| 107 | + final SharedPreferences sharedPrefs = Mockito.mock(SharedPreferences.class); |
| 108 | + final SharedPreferences.Editor sharedPrefsEditor = Mockito.mock(SharedPreferences.Editor.class); |
| 109 | + final Context context = Mockito.mock(Context.class); |
| 110 | + Mockito.when(context.getSharedPreferences(anyString(), anyInt())).thenReturn(sharedPrefs); |
| 111 | + Mockito.when(sharedPrefs.edit()).thenReturn(sharedPrefsEditor); |
| 112 | + try { |
| 113 | + tempFolder.create(); |
| 114 | + Mockito.when(context.getCacheDir()).thenReturn(tempFolder.newFolder()); |
| 115 | + } catch (IOException e) { |
| 116 | + e.printStackTrace(); |
| 117 | + } |
| 118 | + |
| 119 | + Bitmap deviceLogo = null; |
| 120 | + try { |
| 121 | + deviceLogo = AndroidTools.downloadImage(ICON_URL); |
| 122 | + lockScreenDeviceIconManager = new LockScreenDeviceIconManager(context); |
| 123 | + lockScreenDeviceIconManager.saveFileToCache(deviceLogo, ICON_URL); |
| 124 | + verify(sharedPrefs, times(1)).edit(); |
| 125 | + verify(sharedPrefsEditor, times(1)).putString(anyString(), anyString()); |
| 126 | + } catch (IOException e) { |
| 127 | + e.printStackTrace(); |
| 128 | + } |
| 129 | + } |
104 | 130 |
|
105 | 131 | public void testGetFileFromCacheShouldReturnNullIfFailedToGetSystemPref() { |
106 | 132 | final SharedPreferences sharedPrefs = Mockito.mock(SharedPreferences.class); |
|
0 commit comments