public void testReRegisterFailNative() throws Throwable {
    try {

      Context context = getInstrumentation().getTargetContext();
      final SharedPreferences sharedPreferences =
          PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());

      final Container container = new Container();
      final String handle = "handle";

      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.register(handle, new String[] {"tag1"}).get();
      } catch (Exception exception) {
        fail(exception.getMessage());
      }

      try {
        reRegistrationPush.register(handle, 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 + DEFAULT_REGISTRATION_NAME, 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 testReRegisterNative() throws Throwable {
    try {

      Context context = getInstrumentation().getTargetContext();
      final SharedPreferences sharedPreferences =
          PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());

      final Container container = new Container();
      final String handle = "handle";

      String registrationId1 = "registrationId1";
      String registrationId2 = "registrationId2";

      String[] tags1 = new String[] {"tag1"};
      final String[] tags2 = new String[] {"tag2"};

      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.register(handle, tags1).get();

        Registration registration2 = reRegistrationPush.register(handle, tags2).get();

        container.storedRegistrationId =
            sharedPreferences.getString(
                STORAGE_PREFIX + REGISTRATION_NAME_STORAGE_KEY + DEFAULT_REGISTRATION_NAME, null);
        container.tags = registration2.getTags();

      } 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));
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  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));
    }
  }
 private static void forceRefreshSync(MobileServicePush push, String handle)
     throws InterruptedException, ExecutionException {
   push.unregisterAll(handle).get();
 }
  @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);
    }
  }