Java Code Examples for com.google.android.gms.tasks.TaskCompletionSource#trySetResult()

The following examples show how to use com.google.android.gms.tasks.TaskCompletionSource#trySetResult() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Utils.java    From firebase-android-sdk with Apache License 2.0 7 votes vote down vote up
/** @return A tasks that is resolved when either of the given tasks is resolved. */
public static <T> Task<T> race(Task<T> t1, Task<T> t2) {
  final TaskCompletionSource<T> result = new TaskCompletionSource<>();
  Continuation<T, Void> continuation =
      new Continuation<T, Void>() {
        @Override
        public Void then(@NonNull Task<T> task) throws Exception {
          if (task.isSuccessful()) {
            result.trySetResult(task.getResult());
          } else {
            result.trySetException(task.getException());
          }
          return null;
        }
      };
  t1.continueWith(continuation);
  t2.continueWith(continuation);
  return result.getTask();
}
 
Example 2
Source File: DefaultSettingsControllerTest.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
public void testCachedSettingsLoad_newInstanceIdentifier() throws Exception {
  final SettingsData fetchedSettings = new TestSettingsData();

  final JSONObject fetchedJson = new JSONObject();
  when(mockSettingsSpiCall.invoke(any(SettingsRequest.class), eq(true))).thenReturn(fetchedJson);

  when(mockSettingsJsonParser.parseSettingsJson(fetchedJson)).thenReturn(fetchedSettings);

  TaskCompletionSource<Void> dataCollectionPermission = new TaskCompletionSource<>();
  when(mockDataCollectionArbiter.waitForDataCollectionPermission())
      .thenReturn(dataCollectionPermission.getTask());

  final SettingsRequest requestData = buildSettingsRequest();
  final SettingsController controller =
      newSettingsController(
          requestData,
          mockCurrentTimeProvider,
          mockSettingsJsonParser,
          mockCachedSettingsIo,
          mockSettingsSpiCall,
          mockDataCollectionArbiter,
          true);

  controller.loadSettingsData(SettingsCacheBehavior.SKIP_CACHE_LOOKUP, networkExecutor);
  assertNotNull(controller.getSettings());

  dataCollectionPermission.trySetResult(null);
  assertEquals(fetchedSettings.appData, await(controller.getAppSettings()));
  assertEquals(fetchedSettings, controller.getSettings());

  verify(mockSettingsSpiCall).invoke(any(SettingsRequest.class), eq(true));
  verify(mockCachedSettingsIo).writeCachedSettings(fetchedSettings.expiresAtMillis, fetchedJson);
  verify(mockSettingsJsonParser).parseSettingsJson(fetchedJson);
  verify(mockCurrentTimeProvider).getCurrentTimeMillis();
}
 
Example 3
Source File: DefaultSettingsControllerTest.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
public void testNoAvailableSettingsLoad() throws Exception {
  when(mockSettingsSpiCall.invoke(any(SettingsRequest.class), eq(true))).thenReturn(null);

  when(mockCachedSettingsIo.readCachedSettings()).thenReturn(null);

  TaskCompletionSource<Void> dataCollectionPermission = new TaskCompletionSource<>();
  when(mockDataCollectionArbiter.waitForDataCollectionPermission())
      .thenReturn(dataCollectionPermission.getTask());

  final SettingsRequest requestData = buildSettingsRequest();
  final SettingsController controller =
      newSettingsController(
          requestData,
          mockCurrentTimeProvider,
          mockSettingsJsonParser,
          mockCachedSettingsIo,
          mockSettingsSpiCall,
          mockDataCollectionArbiter,
          false);

  Task<Void> loadFinished = controller.loadSettingsData(networkExecutor);
  assertNotNull(controller.getSettings());
  assertFalse(controller.getAppSettings().isComplete());

  dataCollectionPermission.trySetResult(null);
  await(loadFinished);

  assertNotNull(controller.getSettings());
  assertFalse(controller.getAppSettings().isComplete());

  verify(mockSettingsSpiCall).invoke(any(SettingsRequest.class), eq(true));
  verify(mockCachedSettingsIo, times(2)).readCachedSettings();
  verifyZeroInteractions(mockSettingsJsonParser);
  verify(mockCurrentTimeProvider).getCurrentTimeMillis();
}
 
Example 4
Source File: DefaultSettingsControllerTest.java    From firebase-android-sdk with Apache License 2.0 4 votes vote down vote up
public void testExpiredCachedSettingsLoad() throws Exception {

    final SettingsData cachedSettings = new TestSettingsData();
    final SettingsData fetchedSettings = new TestSettingsData();

    final JSONObject fetchedJson = new JSONObject();
    when(mockSettingsSpiCall.invoke(any(SettingsRequest.class), eq(true))).thenReturn(fetchedJson);

    final JSONObject cachedJson = new JSONObject();
    when(mockCachedSettingsIo.readCachedSettings()).thenReturn(cachedJson);

    when(mockCurrentTimeProvider.getCurrentTimeMillis())
        .thenReturn(Long.valueOf(EXPIRED_CURRENT_TIME_MILLIS));

    when(mockSettingsJsonParser.parseSettingsJson(cachedJson)).thenReturn(cachedSettings);
    when(mockSettingsJsonParser.parseSettingsJson(fetchedJson)).thenReturn(fetchedSettings);

    TaskCompletionSource<Void> dataCollectionPermission = new TaskCompletionSource<>();
    when(mockDataCollectionArbiter.waitForDataCollectionPermission())
        .thenReturn(dataCollectionPermission.getTask());

    final SettingsRequest requestData = buildSettingsRequest();
    final SettingsController controller =
        newSettingsController(
            requestData,
            mockCurrentTimeProvider,
            mockSettingsJsonParser,
            mockCachedSettingsIo,
            mockSettingsSpiCall,
            mockDataCollectionArbiter,
            false);

    Task<Void> loadFinished = controller.loadSettingsData(networkExecutor);

    assertEquals(cachedSettings, controller.getSettings());
    assertEquals(cachedSettings.appData, await(controller.getAppSettings()));

    dataCollectionPermission.trySetResult(null);
    await(loadFinished);

    assertEquals(fetchedSettings.appData, await(controller.getAppSettings()));
    assertEquals(fetchedSettings, controller.getSettings());

    verify(mockSettingsSpiCall).invoke(any(SettingsRequest.class), eq(true));
    verify(mockCachedSettingsIo, times(2)).readCachedSettings();
    verify(mockCachedSettingsIo).writeCachedSettings(fetchedSettings.expiresAtMillis, fetchedJson);
    verify(mockSettingsJsonParser, times(2)).parseSettingsJson(cachedJson);
    verify(mockSettingsJsonParser).parseSettingsJson(fetchedJson);
    verify(mockCurrentTimeProvider, times(3)).getCurrentTimeMillis();
  }
 
Example 5
Source File: DefaultSettingsControllerTest.java    From firebase-android-sdk with Apache License 2.0 4 votes vote down vote up
public void testSkipCachedSettingsLoad() throws Exception {

    final SettingsData fetchedSettings = new TestSettingsData();

    final JSONObject fetchedJson = new JSONObject();
    when(mockSettingsSpiCall.invoke(any(SettingsRequest.class), eq(true))).thenReturn(fetchedJson);

    when(mockSettingsJsonParser.parseSettingsJson(fetchedJson)).thenReturn(fetchedSettings);

    final JSONObject expiredCachedSettingsJson = new JSONObject();
    when(mockCachedSettingsIo.readCachedSettings()).thenReturn(expiredCachedSettingsJson);

    when(mockCurrentTimeProvider.getCurrentTimeMillis())
        .thenReturn(Long.valueOf(EXPIRED_CURRENT_TIME_MILLIS));

    final SettingsData expiredCachedSettings = new TestSettingsData();
    when(mockSettingsJsonParser.parseSettingsJson(expiredCachedSettingsJson))
        .thenReturn(expiredCachedSettings);

    TaskCompletionSource<Void> dataCollectionPermission = new TaskCompletionSource<>();
    when(mockDataCollectionArbiter.waitForDataCollectionPermission())
        .thenReturn(dataCollectionPermission.getTask());

    final SettingsRequest requestData = buildSettingsRequest();
    final SettingsController controller =
        newSettingsController(
            requestData,
            mockCurrentTimeProvider,
            mockSettingsJsonParser,
            mockCachedSettingsIo,
            mockSettingsSpiCall,
            mockDataCollectionArbiter,
            false);

    Task<Void> loadFinished =
        controller.loadSettingsData(SettingsCacheBehavior.SKIP_CACHE_LOOKUP, networkExecutor);
    assertEquals(expiredCachedSettings.appData, await(controller.getAppSettings()));
    assertEquals(expiredCachedSettings, controller.getSettings());

    dataCollectionPermission.trySetResult(null);
    await(loadFinished);

    assertEquals(fetchedSettings.appData, await(controller.getAppSettings()));
    assertEquals(fetchedSettings, controller.getSettings());

    verify(mockSettingsSpiCall).invoke(any(SettingsRequest.class), eq(true));
    verify(mockCachedSettingsIo).readCachedSettings();
    verify(mockCachedSettingsIo).writeCachedSettings(fetchedSettings.expiresAtMillis, fetchedJson);
    verify(mockSettingsJsonParser).parseSettingsJson(fetchedJson);
    verify(mockCurrentTimeProvider, times(2)).getCurrentTimeMillis();
  }
 
Example 6
Source File: DefaultSettingsControllerTest.java    From firebase-android-sdk with Apache License 2.0 4 votes vote down vote up
/**
 * Test loading settings in the scenario that initial cache lookup is skipped and the remote call
 * returns null. Should attempt another cache lookup, this time forcing use of an expired cache
 * result.
 *
 * @throws Exception
 */
public void testLastDitchSettingsLoad() throws Exception {
  when(mockSettingsSpiCall.invoke(any(SettingsRequest.class), eq(true))).thenReturn(null);

  final JSONObject expiredCachedSettingsJson = new JSONObject();
  when(mockCachedSettingsIo.readCachedSettings()).thenReturn(expiredCachedSettingsJson);

  when(mockCurrentTimeProvider.getCurrentTimeMillis())
      .thenReturn(Long.valueOf(EXPIRED_CURRENT_TIME_MILLIS));

  final SettingsData expiredCachedSettings = new TestSettingsData();
  when(mockSettingsJsonParser.parseSettingsJson(expiredCachedSettingsJson))
      .thenReturn(expiredCachedSettings);

  TaskCompletionSource<Void> dataCollectionPermission = new TaskCompletionSource<>();
  when(mockDataCollectionArbiter.waitForDataCollectionPermission())
      .thenReturn(dataCollectionPermission.getTask());

  final SettingsRequest requestData = buildSettingsRequest();
  final SettingsController controller =
      newSettingsController(
          requestData,
          mockCurrentTimeProvider,
          mockSettingsJsonParser,
          mockCachedSettingsIo,
          mockSettingsSpiCall,
          mockDataCollectionArbiter,
          false);

  Task<Void> loadFinished =
      controller.loadSettingsData(SettingsCacheBehavior.SKIP_CACHE_LOOKUP, networkExecutor);
  assertEquals(expiredCachedSettings, controller.getSettings());
  assertEquals(expiredCachedSettings.appData, await(controller.getAppSettings()));

  dataCollectionPermission.trySetResult(null);
  await(loadFinished);

  assertEquals(expiredCachedSettings.appData, await(controller.getAppSettings()));
  assertEquals(expiredCachedSettings, controller.getSettings());

  verify(mockSettingsSpiCall).invoke(any(SettingsRequest.class), eq(true));
  verify(mockCachedSettingsIo).readCachedSettings();
  verify(mockSettingsJsonParser).parseSettingsJson(expiredCachedSettingsJson);
  verify(mockCurrentTimeProvider, times(2)).getCurrentTimeMillis();
}