@CheckReturnValue
  public static <T> Observable<T> create(
      @NonNull T data,
      @NonNull Activity activity,
      @Nullable CharSequence message,
      @LayoutRes int progressLayoutId) {
    Dialog dialog = new Dialog(activity, R.style.ProgressDialog);
    dialog.setContentView(progressLayoutId);
    if (!TextUtils.isEmpty(message)) {
      TextView messageView = dialog.findViewById(R.id.progress_dialog_message_text_view);
      messageView.setVisibility(View.VISIBLE);
      messageView.setText(message);
    }
    dialog.setTitle(null);
    dialog.setCancelable(false);
    dialog.setOwnerActivity(activity);
    dialog.show();

    return Observable.using(
        () -> dialog,
        (Function<Dialog, ObservableSource<T>>) d1 -> Observable.just(data),
        Dialog::dismiss,
        true);
  }
@Test
  public void testLifecycleWithError() throws Exception {
    ActivityController<FragmentActivity> controller =
        Robolectric.buildActivity(FragmentActivity.class);
    controller.setup();

    Data data = Mockito.mock(Data.class);
    Mockito.doThrow(new RuntimeException()).when(data).call(anyInt());

    final Data errorData = Mockito.mock(Data.class);

    Assert.assertNull(ShadowDialog.getLatestDialog());

    final Disposable disposable =
        RxProgressDialog.create(data, controller.get(), R.layout.progress_window_for_test)
            .map(
                d -> {
                  Assert.assertNotNull(ShadowDialog.getLatestDialog());
                  Assert.assertTrue(ShadowDialog.getLatestDialog().isShowing());
                  d.call(1);
                  return d;
                })
            .subscribe(d -> d.call(2), throwable -> errorData.call(0));

    Mockito.verify(errorData).call(0);
    Mockito.verifyNoMoreInteractions(errorData);

    Mockito.verify(data).call(1);
    Mockito.verifyNoMoreInteractions(data);

    Assert.assertNotNull(ShadowDialog.getLatestDialog());
    Assert.assertFalse(ShadowDialog.getLatestDialog().isShowing());

    disposable.dispose();

    Assert.assertNotNull(ShadowDialog.getLatestDialog());
    Assert.assertFalse(ShadowDialog.getLatestDialog().isShowing());
  }