/** Tests that the connection handler gives up after a number of connection attempts. */
  @Test
  @Feature({"GCore"})
  public void connectionFailureTest() {
    GoogleApiClientHelper helper = new GoogleApiClientHelper(mMockClient);

    helper.onConnectionFailed(new ConnectionResult(ConnectionResult.DEVELOPER_ERROR));
    ShadowLooper.runUiThreadTasksIncludingDelayedTasks();

    // Should not retry on unrecoverable errors
    verify(mMockClient, never()).connect();

    // Connection attempts
    for (int i = 0; i < ConnectedTask.RETRY_NUMBER_LIMIT; i++) {
      helper.onConnectionFailed(new ConnectionResult(ConnectionResult.SERVICE_UPDATING));
      ShadowLooper.runUiThreadTasksIncludingDelayedTasks();
    }

    // Should have tried to connect every time.
    verify(mMockClient, times(ConnectedTask.RETRY_NUMBER_LIMIT)).connect();

    // Try again
    helper.onConnectionFailed(new ConnectionResult(ConnectionResult.SERVICE_UPDATING));
    ShadowLooper.runUiThreadTasksIncludingDelayedTasks();

    // The connection handler should have given up, no new call.
    verify(mMockClient, times(ConnectedTask.RETRY_NUMBER_LIMIT)).connect();
  }
  /** Tests that when a connection succeeds, the retry limit is reset. */
  @Test
  @Feature({"GCore"})
  public void connectionAttemptsResetTest() {
    GoogleApiClientHelper helper = new GoogleApiClientHelper(mMockClient);

    // Connection attempts
    for (int i = 0; i < ConnectedTask.RETRY_NUMBER_LIMIT - 1; i++) {
      helper.onConnectionFailed(new ConnectionResult(ConnectionResult.SERVICE_UPDATING));
      ShadowLooper.runUiThreadTasksIncludingDelayedTasks();
    }

    // Should have tried to connect every time.
    verify(mMockClient, times(ConnectedTask.RETRY_NUMBER_LIMIT - 1)).connect();

    // Connection successful now
    helper.onConnected(null);
    ShadowLooper.runUiThreadTasksIncludingDelayedTasks();

    for (int i = 0; i < ConnectedTask.RETRY_NUMBER_LIMIT; i++) {
      helper.onConnectionFailed(new ConnectionResult(ConnectionResult.SERVICE_UPDATING));
      ShadowLooper.runUiThreadTasksIncludingDelayedTasks();
    }

    // A success should allow for more connection attempts.
    verify(mMockClient, times(ConnectedTask.RETRY_NUMBER_LIMIT * 2 - 1)).connect();

    // This should not result in a connection attempt, the limit is still there.
    helper.onConnectionFailed(new ConnectionResult(ConnectionResult.SERVICE_UPDATING));
    ShadowLooper.runUiThreadTasksIncludingDelayedTasks();

    // The connection handler should have given up, no new call.
    verify(mMockClient, times(ConnectedTask.RETRY_NUMBER_LIMIT * 2 - 1)).connect();
  }
  /** Tests that connection attempts are delayed. */
  @Test
  @Feature({"GCore"})
  public void connectionAttemptDelayTest() {
    GoogleApiClientHelper helper = new GoogleApiClientHelper(mMockClient);

    ShadowLooper.pauseMainLooper();
    helper.onConnectionFailed(new ConnectionResult(ConnectionResult.SERVICE_UPDATING));
    verify(mMockClient, times(0)).connect();
    ShadowLooper.unPauseMainLooper();

    ShadowLooper.runUiThreadTasksIncludingDelayedTasks();
    verify(mMockClient, times(1)).connect();
  }