public static ParseInstallation getCurrentInstallation() { try { return ParseTaskUtils.wait( getCurrentInstallationController().getAsync()); } catch (ParseException e) { // In order to have backward compatibility, we swallow the exception silently. return null; } }
@Override public Task<Void> then(Task<Void> task) { return getCurrentInstallationController().setAsync(ParseInstallation.this); } });
ParseUser.getCurrentUserController().clearFromMemory(); } else if (className.equals(getClassName(ParseInstallation.class))) { ParseInstallation.getCurrentInstallationController().clearFromMemory();
private static void mocksForUpdateBeforeSave() { // Mock currentInstallationController to make setAsync work ParseCurrentInstallationController controller = mock(ParseCurrentInstallationController.class); when(controller.isCurrent(any(ParseInstallation.class))).thenReturn(true); ParseCorePlugins.getInstance().registerCurrentInstallationController(controller); // Mock App Name RuntimeEnvironment.application.getApplicationInfo().name = "parseTest"; ParsePlugins plugins = mock(ParsePlugins.class); // Mock installationId InstallationId installationId = mock(InstallationId.class); when(installationId.get()).thenReturn("installationId"); when(plugins.installationId()).thenReturn(installationId); // Mock application context when(plugins.applicationContext()).thenReturn(RuntimeEnvironment.application); ParsePlugins.set(plugins); }
@After public void tearDown() throws Exception { super.tearDown(); if (ParsePlugins.get() != null) { ParseCurrentInstallationController installationController = ParseCorePlugins.getInstance().getCurrentInstallationController(); if (installationController != null) { installationController.clearFromDisk(); } ParseCurrentUserController userController = ParseCorePlugins.getInstance().getCurrentUserController(); if (userController != null) { userController.clearFromDisk(); } } Parse.destroy(); }
@Override /* package */ void updateBeforeSave() { super.updateBeforeSave(); if (getCurrentInstallationController().isCurrent(ParseInstallation.this)) { updateTimezone(); updateVersionInfo(); updateDeviceInfo(); updateLocaleIdentifier(); } }
public Task<Void> subscribeInBackground(final String channel) { if (channel == null) { throw new IllegalArgumentException("Can't subscribe to null channel."); } return getCurrentInstallationController().getAsync().onSuccessTask(new Continuation<ParseInstallation, Task<Void>>() { @Override public Task<Void> then(Task<ParseInstallation> task) { ParseInstallation installation = task.getResult(); List<String> channels = installation.getList(ParseInstallation.KEY_CHANNELS); if (channels == null || installation.isDirty(ParseInstallation.KEY_CHANNELS) || !channels.contains(channel)) { installation.addUnique(ParseInstallation.KEY_CHANNELS, channel); return installation.saveInBackground(); } else { return Task.forResult(null); } } }); }
@Override public Task<Void> then(Task<Void> task) { return getCurrentInstallationController().setAsync(ParseInstallation.this); } });
public Task<Void> unsubscribeInBackground(final String channel) { if (channel == null) { throw new IllegalArgumentException("Can't unsubscribe from null channel."); } return getCurrentInstallationController().getAsync().onSuccessTask(new Continuation<ParseInstallation, Task<Void>>() { @Override public Task<Void> then(Task<ParseInstallation> task) { ParseInstallation installation = task.getResult(); List<String> channels = installation.getList(ParseInstallation.KEY_CHANNELS); if (channels != null && channels.contains(channel)) { installation.removeAll( ParseInstallation.KEY_CHANNELS, Collections.singletonList(channel)); return installation.saveInBackground(); } else { return Task.forResult(null); } } }); } }
@Test public void testHandleFetchResultAsync() throws Exception { // Mock currentInstallationController to make setAsync work ParseCurrentInstallationController controller = mock(ParseCurrentInstallationController.class); when(controller.setAsync(any(ParseInstallation.class))).thenReturn(Task.<Void>forResult(null)); ParseCorePlugins.getInstance().registerCurrentInstallationController(controller); // Mock return state ParseInstallation.State state = new ParseInstallation.State.Builder("_Installation") .put("key", "value") .isComplete(true) .build(); ParseInstallation installation = new ParseInstallation(); ParseTaskUtils.wait(installation.handleFetchResultAsync(state)); // Make sure the installation data is correct assertEquals("value", installation.get("key")); // Make sure we set the currentInstallation verify(controller, times(1)).setAsync(installation); }
private ParseCurrentInstallationController mockCurrentInstallationController() { ParseCurrentInstallationController controller = mock(ParseCurrentInstallationController.class); ParseInstallation currentInstallation = new ParseInstallation(); when(controller.getAsync()) .thenReturn(Task.forResult(currentInstallation)); ParseCorePlugins.getInstance() .registerCurrentInstallationController(controller); return controller; } }
@Test public void testHandleSaveResultAsync() throws Exception { // Mock currentInstallationController to make setAsync work ParseCurrentInstallationController controller = mock(ParseCurrentInstallationController.class); when(controller.setAsync(any(ParseInstallation.class))).thenReturn(Task.<Void>forResult(null)); ParseCorePlugins.getInstance().registerCurrentInstallationController(controller); // Mock return state ParseInstallation.State state = new ParseInstallation.State.Builder("_Installation") .put("key", "value") .build(); ParseInstallation installation = new ParseInstallation(); installation.put("keyAgain", "valueAgain"); ParseOperationSet operationSet = installation.startSave(); ParseTaskUtils.wait(installation.handleSaveResultAsync(state, operationSet)); // Make sure the installation data is correct assertEquals("value", installation.get("key")); assertEquals("valueAgain", installation.get("keyAgain")); // Make sure we set the currentInstallation verify(controller, times(1)).setAsync(installation); }
@Test public void testGetCurrentInstallation() { // Mock currentInstallationController to make setAsync work ParseCurrentInstallationController controller = mock(ParseCurrentInstallationController.class); ParseInstallation currentInstallation = new ParseInstallation(); when(controller.getAsync()).thenReturn(Task.forResult(currentInstallation)); ParseCorePlugins.getInstance().registerCurrentInstallationController(controller); ParseInstallation installation = ParseInstallation.getCurrentInstallation(); assertEquals(currentInstallation, installation); verify(controller, times(1)).getAsync(); }
@Test public void testMissingRequiredFieldWhenSaveAsync() throws Exception { String sessionToken = "sessionToken"; Task<Void> toAwait = Task.forResult(null); ParseCurrentInstallationController controller = mockCurrentInstallationController(); ParseObjectController objController = mock(ParseObjectController.class); // mock return task when Installation was deleted on the server Task<ParseObject.State> taskError = Task.forError(new ParseException(ParseException.MISSING_REQUIRED_FIELD_ERROR, "")); // mock return task when Installation was re-saved to the server Task<ParseObject.State> task = Task.forResult(null); when(objController.saveAsync( any(ParseObject.State.class), any(ParseOperationSet.class), eq(sessionToken), any(ParseDecoder.class))) .thenReturn(taskError) .thenReturn(task); ParseCorePlugins.getInstance() .registerObjectController(objController); ParseInstallation installation = ParseInstallation.getCurrentInstallation(); assertNotNull(installation); installation.put("key", "value"); ParseTaskUtils.wait(installation.saveAsync(sessionToken, toAwait)); verify(controller).getAsync(); verify(objController, times(2)).saveAsync( any(ParseObject.State.class), any(ParseOperationSet.class), eq(sessionToken), any(ParseDecoder.class)); }
ParseTaskUtils.wait(installation.saveAsync(sessionToken, toAwait)); verify(controller).getAsync(); verify(objController, times(2)).saveAsync( any(ParseObject.State.class),