public void testRegisterUnregisterTemplate() throws Throwable { Context context = getInstrumentation().getTargetContext(); final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext()); final Container container = new Container(); final String handle = "handle"; final String templateName = "templateName"; String registrationId = "registrationId"; MobileServiceClient client = new MobileServiceClient(appUrl, appKey, context); client = client.withFilter(getUpsertTestFilter(registrationId)); final MobileServicePush push = client.getPush(); forceRefreshSync(push, handle); try { TemplateRegistration registration = push.registerTemplate(handle, templateName, "{ }", new String[] {"tag1"}).get(); container.registrationId = registration.getRegistrationId(); container.storedRegistrationId = sharedPreferences.getString( STORAGE_PREFIX + REGISTRATION_NAME_STORAGE_KEY + templateName, null); push.unregisterTemplate(templateName).get(); container.unregister = sharedPreferences.getString( STORAGE_PREFIX + REGISTRATION_NAME_STORAGE_KEY + templateName, null); Assert.assertEquals(registrationId, container.storedRegistrationId); Assert.assertEquals(registrationId, container.registrationId); Assert.assertNull(container.unregister); } catch (Exception exception) { if (exception instanceof ExecutionException) { container.exception = (Exception) exception.getCause(); } else { container.exception = exception; } fail(container.exception.getMessage()); } }
public void testRegisterFailTemplate() throws Throwable { final Container container = new Container(); Context context = getInstrumentation().getTargetContext(); final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext()); final String handle = "handle"; final String templateName = "templateName"; String registrationId = "registrationId"; MobileServiceClient client = new MobileServiceClient(appUrl, appKey, context); client = client.withFilter(getUpsertFailTestFilter(registrationId)); final MobileServicePush push = client.getPush(); forceRefreshSync(push, handle); try { push.registerTemplate(handle, templateName, "{ }", new String[] {"tag1"}).get(); fail("Expected Exception RegistrationGoneException"); } catch (Exception exception) { if (exception instanceof ExecutionException) { container.exception = (Exception) exception.getCause(); } else { container.exception = exception; } if (!(container.exception instanceof RegistrationGoneException)) { fail("Expected Exception RegistrationGoneException"); } Assert.assertNull( sharedPreferences.getString( STORAGE_PREFIX + REGISTRATION_NAME_STORAGE_KEY + DEFAULT_REGISTRATION_NAME, null)); } }
public void testReRegisterFailTemplate() throws Throwable { try { Context context = getInstrumentation().getTargetContext(); final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext()); final Container container = new Container(); final String handle = "handle"; final String templateName = "templateName"; String registrationId1 = "registrationId1"; String registrationId2 = "registrationId2"; MobileServiceClient client = new MobileServiceClient(appUrl, appKey, context); MobileServiceClient registrationclient = client.withFilter(getUpsertTestFilter(registrationId1)); MobileServiceClient reRegistrationclient = client.withFilter(getUpsertFailTestFilter(registrationId2)); final MobileServicePush registrationPush = registrationclient.getPush(); final MobileServicePush reRegistrationPush = reRegistrationclient.getPush(); forceRefreshSync(registrationPush, handle); forceRefreshSync(reRegistrationPush, handle); try { registrationPush.registerTemplate(handle, templateName, "{ }", new String[] {"tag1"}).get(); } catch (Exception exception) { fail(exception.getMessage()); } try { reRegistrationPush .registerTemplate(handle, templateName, "{ }", new String[] {"tag1"}) .get(); } catch (Exception exception) { if (exception instanceof ExecutionException) { container.exception = (Exception) exception.getCause(); } else { container.exception = exception; } container.storedRegistrationId = sharedPreferences.getString( STORAGE_PREFIX + REGISTRATION_NAME_STORAGE_KEY + templateName, null); } // Asserts Exception exception = container.exception; if (!(exception instanceof RegistrationGoneException)) { fail("Expected Exception RegistrationGoneException"); } Assert.assertNull(container.storedRegistrationId); } catch (Exception e) { e.printStackTrace(); } }
public void testReRegisterTemplate() throws Throwable { try { Context context = getInstrumentation().getTargetContext(); final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext()); final Container container = new Container(); final String handle = "handle"; final String templateName = "templateName"; String registrationId1 = "registrationId1"; String registrationId2 = "registrationId2"; String[] tags1 = new String[] {"tag1"}; final String[] tags2 = new String[] {"tag2"}; String templateBody1 = "\"data\"={\"text\"=\"$message1\"}"; final String templateBody2 = "\"data\"={\"text\"=\"$message2\"}"; MobileServiceClient client = new MobileServiceClient(appUrl, appKey, context); MobileServiceClient registrationclient = client.withFilter(getUpsertTestFilter(registrationId1)); MobileServiceClient reRegistrationclient = client.withFilter(getUpsertTestFilter(registrationId2)); final MobileServicePush registrationPush = registrationclient.getPush(); final MobileServicePush reRegistrationPush = reRegistrationclient.getPush(); forceRefreshSync(registrationPush, handle); forceRefreshSync(reRegistrationPush, handle); try { registrationPush.registerTemplate(handle, templateName, templateBody1, tags1).get(); TemplateRegistration registration2 = reRegistrationPush.registerTemplate(handle, templateName, templateBody2, tags2).get(); container.storedRegistrationId = sharedPreferences.getString( STORAGE_PREFIX + REGISTRATION_NAME_STORAGE_KEY + templateName, null); container.tags = registration2.getTags(); container.templateBody = registration2.getTemplateBody(); } catch (Exception exception) { container.exception = exception; } // Asserts Exception exception = container.exception; if (exception != null) { fail(exception.getMessage()); } else { Assert.assertEquals(registrationId2, container.storedRegistrationId); Assert.assertTrue(matchTags(tags2, container.tags)); Assert.assertEquals(templateBody2, container.templateBody); } } catch (Exception e) { e.printStackTrace(); } }
@SuppressWarnings("deprecation") public void testRegisterUnregisterTemplateCallback() throws Throwable { final CountDownLatch latch = new CountDownLatch(1); Context context = getInstrumentation().getTargetContext(); final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext()); final Container container = new Container(); final String handle = "handle"; final String templateName = "templateName"; String registrationId = "registrationId"; MobileServiceClient client = new MobileServiceClient(appUrl, appKey, context); client = client.withFilter(getUpsertTestFilter(registrationId)); final MobileServicePush push = client.getPush(); forceRefreshSync(push, handle); push.registerTemplate( handle, templateName, "{ }", new String[] {"tag1"}, new TemplateRegistrationCallback() { @Override public void onRegister(TemplateRegistration registration, Exception exception) { if (exception != null) { container.exception = exception; latch.countDown(); } else { container.registrationId = registration.getRegistrationId(); container.storedRegistrationId = sharedPreferences.getString( STORAGE_PREFIX + REGISTRATION_NAME_STORAGE_KEY + templateName, null); push.unregisterTemplate( templateName, new UnregisterCallback() { @Override public void onUnregister(Exception exception) { if (exception != null) { container.exception = exception; } else { container.unregister = sharedPreferences.getString( STORAGE_PREFIX + REGISTRATION_NAME_STORAGE_KEY + templateName, null); } latch.countDown(); } }); } } }); latch.await(); // Asserts Exception exception = container.exception; if (exception != null) { fail(exception.getMessage()); } else { Assert.assertEquals(registrationId, container.storedRegistrationId); Assert.assertEquals(registrationId, container.registrationId); Assert.assertNull(container.unregister); } }