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 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(expected = RuntimeException.class) public void testInstallationObjectIdCannotBeChanged() { boolean hasException = false; ParseInstallation installation = new ParseInstallation(); try { installation.put("objectId", "abc"); } catch (IllegalArgumentException e) { assertTrue(e.getMessage().contains("Cannot modify")); hasException = true; } assertTrue(hasException); installation.setObjectId("abc"); }
@Test public void testIsCurrent() { // Create test controller CachedCurrentInstallationController controller = new CachedCurrentInstallationController(null, null); ParseInstallation installation = mock(ParseInstallation.class); controller.currentInstallation = installation; assertTrue(controller.isCurrent(installation)); assertFalse(controller.isCurrent(new ParseInstallation())); } }
}; ParseInstallation installation = new ParseInstallation(); installation.put("foo", "bar");
@Test public void testLocaleIdentifierSpecialCases() { mocksForUpdateBeforeSave(); ParseInstallation installation = new ParseInstallation(); // Deprecated two-letter codes (Java issue). Locale.setDefault(new Locale("iw", "US")); installation.updateBeforeSave(); assertEquals("he-US", installation.getString(KEY_LOCALE_IDENTIFIER)); Locale.setDefault(new Locale("in", "US")); installation.updateBeforeSave(); assertEquals("id-US", installation.getString(KEY_LOCALE_IDENTIFIER)); Locale.setDefault(new Locale("ji", "US")); installation.updateBeforeSave(); assertEquals("yi-US", installation.getString(KEY_LOCALE_IDENTIFIER)); // No country code. Locale.setDefault(new Locale("en")); installation.updateBeforeSave(); assertEquals("en", installation.getString(KEY_LOCALE_IDENTIFIER)); }
@Test public void testGetAsyncFromMemory() throws Exception { // Create test controller CachedCurrentInstallationController controller = new CachedCurrentInstallationController(null, null); ParseInstallation currentInstallationInMemory = new ParseInstallation(); controller.currentInstallation = currentInstallationInMemory; ParseInstallation currentInstallation = ParseTaskUtils.wait(controller.getAsync()); assertSame(currentInstallationInMemory, currentInstallation); }
@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); }
@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); }
ParseInstallation installation = new ParseInstallation(); installation.updateBeforeSave();
@Test public void testDeviceToken() { ParseInstallation installation = new ParseInstallation(); installation.setDeviceToken("deviceToken"); assertEquals("deviceToken", installation.getDeviceToken()); installation.removeDeviceToken(); assertNull(installation.getDeviceToken()); // Make sure we add the pushType to operationSetQueue instead of serverData assertEquals(1, installation.operationSetQueue.getLast().size()); }
@Test public void testDeviceTokenWithNullDeviceToken() { ParseInstallation installation = new ParseInstallation(); installation.setDeviceToken("deviceToken"); assertEquals("deviceToken", installation.getDeviceToken()); installation.setDeviceToken(null); assertEquals("deviceToken", installation.getDeviceToken()); }