com.google.android.apps.common.testing.ui.espresso.IdlingPolicy

Here are the examples of the java api class com.google.android.apps.common.testing.ui.espresso.IdlingPolicy taken from open source projects.

1. IdlingResourceRegistry#scheduleTimeoutMessages()

Project: double-espresso
File: IdlingResourceRegistry.java
private void scheduleTimeoutMessages() {
    IdlingPolicy warning = IdlingPolicies.getDynamicIdlingResourceWarningPolicy();
    Message timeoutWarning = handler.obtainMessage(IDLE_WARNING_REACHED, TIMEOUT_MESSAGE_TAG);
    handler.sendMessageDelayed(timeoutWarning, warning.getIdleTimeoutUnit().toMillis(warning.getIdleTimeout()));
    Message timeoutError = handler.obtainMessage(TIMEOUT_OCCURRED, TIMEOUT_MESSAGE_TAG);
    IdlingPolicy error = IdlingPolicies.getDynamicIdlingResourceErrorPolicy();
    handler.sendMessageDelayed(timeoutError, error.getIdleTimeoutUnit().toMillis(error.getIdleTimeout()));
}

2. UiControllerImpl#loopUntil()

Project: double-espresso
File: UiControllerImpl.java
/**
   * Loops the main thread until all IdleConditions have been signaled.
   *
   * Once they've been signaled, the conditions are reset and the generation value
   * is incremented.
   *
   * Signals should only be raised thru SignalingTask instances, and care should be
   * taken to ensure that the signaling task is created before loopUntil is called.
   *
   * Good:
   * idlingType.runOnIdle(new SignalingTask(NO_OP, IdleCondition.MY_IDLE_CONDITION, generation));
   * loopUntil(IdleCondition.MY_IDLE_CONDITION);
   *
   * Bad:
   * idlingType.runOnIdle(new CustomCallback() {
   *   @Override
   *   public void itsDone() {
   *     // oh no - The creation of this signaling task is delayed until this method is
   *     // called, so it will not have the right value for generation.
   *     new SignalingTask(NO_OP, IdleCondition.MY_IDLE_CONDITION, generation).run();
   *  }
   * })
   * loopUntil(IdleCondition.MY_IDLE_CONDITION);
   */
private void loopUntil(EnumSet<IdleCondition> conditions) {
    checkState(!looping, "Recursive looping detected!");
    looping = true;
    IdlingPolicy masterIdlePolicy = IdlingPolicies.getMasterIdlingPolicy();
    try {
        int loopCount = 0;
        long start = SystemClock.uptimeMillis();
        long end = start + masterIdlePolicy.getIdleTimeoutUnit().toMillis(masterIdlePolicy.getIdleTimeout());
        while (SystemClock.uptimeMillis() < end) {
            boolean conditionsMet = true;
            boolean shouldLogConditionState = loopCount > 0 && loopCount % 100 == 0;
            for (IdleCondition condition : conditions) {
                if (!condition.isSignaled(conditionSet)) {
                    conditionsMet = false;
                    if (shouldLogConditionState) {
                        Log.w(TAG, "Waiting for: " + condition.name() + " for " + loopCount + " iterations.");
                    } else {
                        break;
                    }
                }
            }
            if (conditionsMet) {
                QueueState queueState = queueInterrogator.determineQueueState();
                if (queueState == QueueState.EMPTY || queueState == QueueState.TASK_DUE_LONG) {
                    return;
                } else {
                    Log.v("ESP_TRACE", "Barrier detected or task avaliable for running shortly.");
                }
            }
            Message message = queueInterrogator.getNextMessage();
            String callbackString = "unknown";
            String messageString = "unknown";
            try {
                if (null == message.getCallback()) {
                    callbackString = "no callback.";
                } else {
                    callbackString = message.getCallback().toString();
                }
                messageString = message.toString();
            } catch (NullPointerException e) {
            }
            Log.v("ESP_TRACE", String.format("%s: MessageQueue.next(): %s, with target: %s, callback: %s", TAG, messageString, message.getTarget().getClass().getCanonicalName(), callbackString));
            message.getTarget().dispatchMessage(message);
            message.recycle();
            loopCount++;
        }
        List<String> idleConditions = Lists.newArrayList();
        for (IdleCondition condition : conditions) {
            if (!condition.isSignaled(conditionSet)) {
                idleConditions.add(condition.name());
            }
        }
        masterIdlePolicy.handleTimeout(idleConditions, String.format("Looped for %s iterations over %s %s.", loopCount, masterIdlePolicy.getIdleTimeout(), masterIdlePolicy.getIdleTimeoutUnit().name()));
    } finally {
        looping = false;
        generation++;
        for (IdleCondition condition : conditions) {
            condition.reset(conditionSet);
        }
    }
}