|
13 | 13 | import org.junit.Rule;
|
14 | 14 | import org.junit.Test;
|
15 | 15 | import org.junit.rules.TemporaryFolder;
|
| 16 | +import org.mockito.ArgumentCaptor; |
16 | 17 | import org.mockito.Matchers;
|
17 | 18 |
|
18 | 19 | import java.io.File;
|
| 20 | +import java.io.InputStream; |
19 | 21 | import java.util.Arrays;
|
20 | 22 | import java.util.List;
|
21 | 23 |
|
22 | 24 | import bolts.Task;
|
23 | 25 |
|
| 26 | +import static org.junit.Assert.assertArrayEquals; |
24 | 27 | import static org.junit.Assert.assertEquals;
|
25 | 28 | import static org.junit.Assert.assertFalse;
|
26 | 29 | import static org.junit.Assert.assertTrue;
|
27 | 30 | import static org.mockito.Matchers.any;
|
| 31 | +import static org.mockito.Matchers.anyString; |
28 | 32 | import static org.mockito.Mockito.mock;
|
29 | 33 | import static org.mockito.Mockito.never;
|
| 34 | +import static org.mockito.Mockito.times; |
30 | 35 | import static org.mockito.Mockito.verify;
|
31 | 36 | import static org.mockito.Mockito.when;
|
32 | 37 |
|
@@ -163,7 +168,112 @@ public void testSaveAsyncCancelled() throws Exception {
|
163 | 168 |
|
164 | 169 | //endregion
|
165 | 170 |
|
166 |
| - // TODO(grantland): testGetDataAsync (same as saveAsync) |
| 171 | + |
| 172 | + //region testGetDataAsync |
| 173 | + |
| 174 | + @Test |
| 175 | + public void testGetDataAsyncSuccess() throws Exception { |
| 176 | + String content = "content"; |
| 177 | + File file = temporaryFolder.newFile("test"); |
| 178 | + ParseFileUtils.writeStringToFile(file, content); |
| 179 | + ParseFileController controller = mock(ParseFileController.class); |
| 180 | + when(controller.fetchAsync( |
| 181 | + any(ParseFile.State.class), |
| 182 | + any(String.class), |
| 183 | + any(ProgressCallback.class), |
| 184 | + Matchers.<Task<Void>>any())).thenReturn(Task.forResult(file)); |
| 185 | + ParseCorePlugins.getInstance().registerFileController(controller); |
| 186 | + |
| 187 | + String url = "url"; |
| 188 | + ParseFile.State state = new ParseFile.State.Builder() |
| 189 | + .url(url) |
| 190 | + .build(); |
| 191 | + ParseFile parseFile = new ParseFile(state); |
| 192 | + |
| 193 | + byte[] data = ParseTaskUtils.wait(parseFile.getDataInBackground()); |
| 194 | + |
| 195 | + // Verify controller get the correct data |
| 196 | + ArgumentCaptor<ParseFile.State> stateCaptor = ArgumentCaptor.forClass(ParseFile.State.class); |
| 197 | + verify(controller, times(1)).fetchAsync( |
| 198 | + stateCaptor.capture(), |
| 199 | + anyString(), |
| 200 | + any(ProgressCallback.class), |
| 201 | + Matchers.<Task<Void>>any() |
| 202 | + ); |
| 203 | + assertEquals(url, stateCaptor.getValue().url()); |
| 204 | + // Verify the data we get is correct |
| 205 | + assertArrayEquals(content.getBytes(), data); |
| 206 | + } |
| 207 | + |
| 208 | + @Test |
| 209 | + public void testGetDataStreamAsyncSuccess() throws Exception { |
| 210 | + String content = "content"; |
| 211 | + File file = temporaryFolder.newFile("test"); |
| 212 | + ParseFileUtils.writeStringToFile(file, content); |
| 213 | + ParseFileController controller = mock(ParseFileController.class); |
| 214 | + when(controller.fetchAsync( |
| 215 | + any(ParseFile.State.class), |
| 216 | + any(String.class), |
| 217 | + any(ProgressCallback.class), |
| 218 | + Matchers.<Task<Void>>any())).thenReturn(Task.forResult(file)); |
| 219 | + ParseCorePlugins.getInstance().registerFileController(controller); |
| 220 | + |
| 221 | + String url = "url"; |
| 222 | + ParseFile.State state = new ParseFile.State.Builder() |
| 223 | + .url(url) |
| 224 | + .build(); |
| 225 | + ParseFile parseFile = new ParseFile(state); |
| 226 | + |
| 227 | + InputStream dataStream = ParseTaskUtils.wait(parseFile.getDataStreamInBackground()); |
| 228 | + |
| 229 | + // Verify controller get the correct data |
| 230 | + ArgumentCaptor<ParseFile.State> stateCaptor = ArgumentCaptor.forClass(ParseFile.State.class); |
| 231 | + verify(controller, times(1)).fetchAsync( |
| 232 | + stateCaptor.capture(), |
| 233 | + anyString(), |
| 234 | + any(ProgressCallback.class), |
| 235 | + Matchers.<Task<Void>>any() |
| 236 | + ); |
| 237 | + assertEquals(url, stateCaptor.getValue().url()); |
| 238 | + // Verify the data we get is correct |
| 239 | + assertArrayEquals(content.getBytes(), ParseIOUtils.toByteArray(dataStream)); |
| 240 | + } |
| 241 | + |
| 242 | + @Test |
| 243 | + public void testGetFileAsyncSuccess() throws Exception { |
| 244 | + String content = "content"; |
| 245 | + File file = temporaryFolder.newFile("test"); |
| 246 | + ParseFileUtils.writeStringToFile(file, content); |
| 247 | + ParseFileController controller = mock(ParseFileController.class); |
| 248 | + when(controller.fetchAsync( |
| 249 | + any(ParseFile.State.class), |
| 250 | + any(String.class), |
| 251 | + any(ProgressCallback.class), |
| 252 | + Matchers.<Task<Void>>any())).thenReturn(Task.forResult(file)); |
| 253 | + ParseCorePlugins.getInstance().registerFileController(controller); |
| 254 | + |
| 255 | + String url = "url"; |
| 256 | + ParseFile.State state = new ParseFile.State.Builder() |
| 257 | + .url(url) |
| 258 | + .build(); |
| 259 | + ParseFile parseFile = new ParseFile(state); |
| 260 | + |
| 261 | + File fetchedFile = ParseTaskUtils.wait(parseFile.getFileInBackground()); |
| 262 | + |
| 263 | + // Verify controller get the correct data |
| 264 | + ArgumentCaptor<ParseFile.State> stateCaptor = ArgumentCaptor.forClass(ParseFile.State.class); |
| 265 | + verify(controller, times(1)).fetchAsync( |
| 266 | + stateCaptor.capture(), |
| 267 | + anyString(), |
| 268 | + any(ProgressCallback.class), |
| 269 | + Matchers.<Task<Void>>any() |
| 270 | + ); |
| 271 | + assertEquals(url, stateCaptor.getValue().url()); |
| 272 | + // Verify the data we get is correct |
| 273 | + assertArrayEquals(content.getBytes(), ParseFileUtils.readFileToByteArray(fetchedFile)); |
| 274 | + } |
| 275 | + |
| 276 | + //endregion |
167 | 277 |
|
168 | 278 | @Test
|
169 | 279 | public void testTaskQueuedMethods() throws Exception {
|
|
0 commit comments