@Test public void testSetAsyncWithCurrentInstallation() throws Exception { InstallationId installationId = mock(InstallationId.class); //noinspection unchecked ParseObjectStore<ParseInstallation> store = mock(ParseObjectStore.class); // Create test controller CachedCurrentInstallationController controller = new CachedCurrentInstallationController(store, installationId); ParseInstallation currentInstallation = mock(ParseInstallation.class); when(currentInstallation.getInstallationId()).thenReturn("testInstallationId"); controller.currentInstallation = currentInstallation; ParseTaskUtils.wait(controller.setAsync(currentInstallation)); // Verify that we persist it verify(store, times(1)).setAsync(currentInstallation); // Make sure installationId is updated verify(installationId, times(1)).set("testInstallationId"); }
@Test public void testGetAsyncFromStore() throws Exception { // Mock installationId InstallationId installationId = mock(InstallationId.class); //noinspection unchecked ParseObjectStore<ParseInstallation> store = mock(ParseObjectStore.class); ParseInstallation installation = mock(ParseInstallation.class); when(installation.getInstallationId()).thenReturn("testInstallationId"); when(store.getAsync()).thenReturn(Task.forResult(installation)); // Create test controller CachedCurrentInstallationController controller = new CachedCurrentInstallationController(store, installationId); ParseInstallation currentInstallation = ParseTaskUtils.wait(controller.getAsync()); verify(store, times(1)).getAsync(); // Make sure installationId is updated verify(installationId, times(1)).set("testInstallationId"); // Make sure controller state is update to date assertSame(installation, controller.currentInstallation); // Make sure the installation we get is correct assertSame(installation, currentInstallation); }
@Test public void testGetAsyncWithNoInstallation() throws Exception { // Mock installationId InstallationId installationId = mock(InstallationId.class); when(installationId.get()).thenReturn("testInstallationId"); //noinspection unchecked ParseObjectStore<ParseInstallation> store = mock(ParseObjectStore.class); when(store.getAsync()).thenReturn(Task.<ParseInstallation>forResult(null)); // Create test controller CachedCurrentInstallationController controller = new CachedCurrentInstallationController(store, installationId); ParseInstallation currentInstallation = ParseTaskUtils.wait(controller.getAsync()); verify(store, times(1)).getAsync(); // Make sure controller state is update to date assertSame(controller.currentInstallation, currentInstallation); // Make sure device info is updated assertEquals("testInstallationId", currentInstallation.getInstallationId()); assertEquals("android", currentInstallation.get(KEY_DEVICE_TYPE)); }
@Test public void testGetAsyncWithNoInstallationRaceCondition() throws ParseException { // Mock installationId InstallationId installationId = mock(InstallationId.class); when(installationId.get()).thenReturn("testInstallationId"); //noinspection unchecked ParseObjectStore<ParseInstallation> store = mock(ParseObjectStore.class); TaskCompletionSource<ParseInstallation> tcs = new TaskCompletionSource(); when(store.getAsync()).thenReturn(tcs.getTask()); // Create test controller CachedCurrentInstallationController controller = new CachedCurrentInstallationController(store, installationId); Task<ParseInstallation> taskA = controller.getAsync(); Task<ParseInstallation> taskB = controller.getAsync(); tcs.setResult(null); ParseInstallation installationA = ParseTaskUtils.wait(taskA); ParseInstallation installationB = ParseTaskUtils.wait(taskB); verify(store, times(1)).getAsync(); assertSame(controller.currentInstallation, installationA); assertSame(controller.currentInstallation, installationB); // Make sure device info is updated assertEquals("testInstallationId", installationA.getInstallationId()); assertEquals("android", installationA.get(KEY_DEVICE_TYPE)); }