Esempio n. 1
0
 @Override
 public void call() {
   if (compareAndSet(false, true)) {
     try {
       dispose.call(resource);
     } finally {
       resource = null;
       dispose = null;
     }
   }
 }
 @Override
 public void onNext(ValidationResult result) {
   if (result == null) onError(new NullPointerException("Can't validate, view is null"));
   switch (result.status()) {
     case SUCCESS:
       if (onSuccess != null) onSuccess.call(result.view());
       break;
     case ERROR:
       if (onError != null) onError.call(result.view(), result.error());
       break;
   }
 }
  /**
   * Run the command via {@link com.netflix.hystrix.HystrixCommand#observe()}, let the {@link
   * rx.Observable} terminal states unblock a {@link java.util.concurrent.CountDownLatch} and then
   * assert
   *
   * @param command command to run
   * @param assertion assertions to check
   * @param isSuccess should the command succeed?
   */
  protected void assertNonBlockingObserve(C command, Action1<C> assertion, boolean isSuccess) {
    System.out.println(
        "Running command.observe(), awaiting terminal state of Observable, then running assertions...");
    final CountDownLatch latch = new CountDownLatch(1);

    Observable<Integer> o = command.observe();

    o.subscribe(
        new Subscriber<Integer>() {
          @Override
          public void onCompleted() {
            latch.countDown();
          }

          @Override
          public void onError(Throwable e) {
            latch.countDown();
          }

          @Override
          public void onNext(Integer i) {
            // do nothing
          }
        });

    try {
      latch.await(3, TimeUnit.SECONDS);
      assertion.call(command);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }

    if (isSuccess) {
      try {
        o.toList().toBlocking().single();
      } catch (Exception ex) {
        throw new RuntimeException(ex);
      }
    } else {
      try {
        o.toList().toBlocking().single();
        fail("Expected a command failure!");
      } catch (Exception ex) {
        System.out.println("Received expected ex : " + ex);
        ex.printStackTrace();
      }
    }
  }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    setTitle(R.string.font_pack_picker_dialog_title);
    final View view = View.inflate(getContext(), R.layout.file_path_dialog, null);
    setView(view);

    mInputLayout = ButterKnife.findById(view, R.id.input_layout);
    final EditText inputView = ButterKnife.findById(view, R.id.file_path_input);
    final Subscription textChanges =
        textChanges(inputView)
            .debounce(400, TimeUnit.MILLISECONDS)
            .map(CharSequence::toString)
            .map(FontPackage::validFontPackFolder)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(
                pathIsValid -> {
                  if (pathIsValid) enableOkButton();
                  else showError();
                });

    setButton(
        BUTTON_POSITIVE,
        getContext().getString(R.string.ok),
        (dialog, which) -> {
          if (!mPathIsValid) return;
          textChanges.unsubscribe();
          mCallback.call(fontPackageFromEditText(inputView));
        });

    setButton(
        BUTTON_NEGATIVE,
        getContext().getString(R.string.cancel),
        (dialog, which) -> {
          textChanges.unsubscribe();
          dismiss();
        });

    super.onCreate(savedInstanceState);

    mPositiveButton = getButton(AlertDialog.BUTTON_POSITIVE);
    mPositiveButton.setEnabled(false);
  }
  /**
   * Run the command via {@link com.netflix.hystrix.HystrixCommand#observe()}, immediately block and
   * then assert
   *
   * @param command command to run
   * @param assertion assertions to check
   * @param isSuccess should the command succeed?
   */
  protected void assertBlockingObserve(C command, Action1<C> assertion, boolean isSuccess) {
    System.out.println(
        "Running command.observe(), immediately blocking and then running assertions...");
    if (isSuccess) {
      try {
        command.observe().toList().toBlocking().single();
      } catch (Exception ex) {
        throw new RuntimeException(ex);
      }
    } else {
      try {
        command.observe().toList().toBlocking().single();
        fail("Expected a command failure!");
      } catch (Exception ex) {
        System.out.println("Received expected ex : " + ex);
        ex.printStackTrace();
      }
    }

    assertion.call(command);
  }
Esempio n. 6
0
 @SuppressWarnings("unused")
 @OnClick(R.id.done_button)
 public void doneButtonClicked() {
   tryCallback.call(input.getText().toString());
   dismiss();
 }
Esempio n. 7
0
  protected void completeRequest(@NonNull String uri) {
    Preconditions.checkNotNull(uri, "URI cannot be null.");

    Log.v(TAG, "completeRequest(" + uri + ")");
    updateNetworkRequestStatus.call(NetworkRequestStatus.completed(uri));
  }
Esempio n. 8
0
  protected void errorRequest(@NonNull String uri, int errorCode, String errorMessage) {
    Preconditions.checkNotNull(uri, "URI cannot be null.");

    Log.v(TAG, "errorRequest(" + uri + ", " + errorCode + ", " + errorMessage + ")");
    updateNetworkRequestStatus.call(NetworkRequestStatus.error(uri, errorCode, errorMessage));
  }