Search in sources :

Example 31 with DatafileConfig

use of com.optimizely.ab.android.shared.DatafileConfig in project android-sdk by optimizely.

the class DefaultDatafileHandlerTest method testBackgroundWithoutEnvironment.

@Test
public void testBackgroundWithoutEnvironment() throws Exception {
    // Context of the app under test.
    Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
    datafileHandler.startBackgroundUpdates(appContext, new DatafileConfig("1", null), 24 * 60 * 60L, null);
    assertTrue(true);
    datafileHandler.stopBackgroundUpdates(appContext, new DatafileConfig("1", null));
    assertTrue(true);
}
Also used : Context(android.content.Context) DatafileConfig(com.optimizely.ab.android.shared.DatafileConfig) Test(org.junit.Test)

Example 32 with DatafileConfig

use of com.optimizely.ab.android.shared.DatafileConfig in project android-sdk by optimizely.

the class BackgroundWatchersCache method getWatchingDatafileConfigs.

/**
 * Get a list of all project ids that are being watched for backgrounding.
 * @return a list of DatafileConfig
 */
List<DatafileConfig> getWatchingDatafileConfigs() {
    List<DatafileConfig> datafileConfigs = new ArrayList<>();
    try {
        JSONObject backgroundWatchers = load();
        if (backgroundWatchers != null) {
            Iterator<String> iterator = backgroundWatchers.keys();
            while (iterator.hasNext()) {
                final String projectKey = iterator.next();
                if (backgroundWatchers.getBoolean(projectKey)) {
                    DatafileConfig datafileConfig = null;
                    boolean sdkKey = projectKey.matches(".*[A-Za-z].*");
                    // TODO: This should be changed to store a jsonized datafile config.
                    if (sdkKey) {
                        datafileConfig = new DatafileConfig(null, projectKey);
                    } else {
                        datafileConfig = new DatafileConfig(projectKey, null);
                    }
                    datafileConfigs.add(datafileConfig);
                }
            }
        }
    } catch (JSONException e) {
        logger.error("Unable to get watching project ids", e);
    }
    return datafileConfigs;
}
Also used : JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) DatafileConfig(com.optimizely.ab.android.shared.DatafileConfig)

Example 33 with DatafileConfig

use of com.optimizely.ab.android.shared.DatafileConfig in project android-sdk by optimizely.

the class OptimizelyManagerDatafileServiceConnectionTest method onServiceConnected.

@Test
public void onServiceConnected() {
    DatafileService.LocalBinder binder = mock(DatafileService.LocalBinder.class);
    DatafileService service = mock(DatafileService.class);
    Context context = mock(Context.class);
    when(service.getApplicationContext()).thenReturn(context);
    when(binder.getService()).thenReturn(service);
    when(optimizelyManager.getDatafileConfig()).thenReturn(new DatafileConfig("1", (String) null));
    when(optimizelyManager.getDatafileLoadedListener(context, null)).thenReturn(mock(DatafileLoadedListener.class));
    ArgumentCaptor<DatafileLoadedListener> captor = ArgumentCaptor.forClass(DatafileLoadedListener.class);
    datafileServiceConnection = new DatafileServiceConnection(optimizelyManager.getDatafileConfig(), context, optimizelyManager.getDatafileLoadedListener(context, null));
    datafileServiceConnection.onServiceConnected(null, binder);
    String sameString = optimizelyManager.getDatafileConfig().getUrl();
    verify(service).getDatafile(eq(sameString), any(DatafileLoader.class), any(DatafileLoadedListener.class));
}
Also used : Context(android.content.Context) DatafileLoadedListener(com.optimizely.ab.android.datafile_handler.DatafileLoadedListener) DatafileLoader(com.optimizely.ab.android.datafile_handler.DatafileLoader) DatafileServiceConnection(com.optimizely.ab.android.datafile_handler.DatafileServiceConnection) DatafileConfig(com.optimizely.ab.android.shared.DatafileConfig) DatafileService(com.optimizely.ab.android.datafile_handler.DatafileService) Test(org.junit.Test)

Example 34 with DatafileConfig

use of com.optimizely.ab.android.shared.DatafileConfig in project android-sdk by optimizely.

the class BackgroundWatchersCacheTest method testExceptionHandling.

@Test
public void testExceptionHandling() throws IOException {
    Cache cache = mock(Cache.class);
    BackgroundWatchersCache backgroundWatchersCache = new BackgroundWatchersCache(cache, logger);
    // Cause a JSONException to be thrown
    when(cache.load(BackgroundWatchersCache.BACKGROUND_WATCHERS_FILE_NAME)).thenReturn("{");
    assertFalse(backgroundWatchersCache.setIsWatching(new DatafileConfig("1", null), true));
    verify(logger).error(contains("Unable to update watching state for project id"), any(JSONException.class));
    assertFalse(backgroundWatchersCache.isWatching(new DatafileConfig("1", null)));
    verify(logger).error(contains("Unable check if project id is being watched"), any(JSONException.class));
    List<DatafileConfig> watchingProjectIds = backgroundWatchersCache.getWatchingDatafileConfigs();
    assertTrue(watchingProjectIds.isEmpty());
    verify(logger).error(contains("Unable to get watching project ids"), any(JSONException.class));
}
Also used : JSONException(org.json.JSONException) DatafileConfig(com.optimizely.ab.android.shared.DatafileConfig) Cache(com.optimizely.ab.android.shared.Cache) Test(org.junit.Test)

Example 35 with DatafileConfig

use of com.optimizely.ab.android.shared.DatafileConfig in project android-sdk by optimizely.

the class DefaultDatafileHandlerUnitTest method testHandler.

@Test
public void testHandler() throws Exception {
    handler = new DefaultDatafileHandler();
    Context context = mock(Context.class);
    when(context.getApplicationContext()).thenReturn(context);
    assertFalse(handler.isDatafileSaved(context, new DatafileConfig("1", null)));
}
Also used : Context(android.content.Context) DatafileConfig(com.optimizely.ab.android.shared.DatafileConfig) Test(org.junit.Test)

Aggregations

DatafileConfig (com.optimizely.ab.android.shared.DatafileConfig)38 Test (org.junit.Test)34 Context (android.content.Context)15 URL (java.net.URL)8 Client (com.optimizely.ab.android.shared.Client)7 Cache (com.optimizely.ab.android.shared.Cache)5 SdkSuppress (androidx.test.filters.SdkSuppress)4 DatafileLoadedListener (com.optimizely.ab.android.datafile_handler.DatafileLoadedListener)4 Intent (android.content.Intent)2 Data (androidx.work.Data)2 DefaultDatafileHandler (com.optimizely.ab.android.datafile_handler.DefaultDatafileHandler)2 DefaultUserProfileService (com.optimizely.ab.android.user_profile.DefaultUserProfileService)2 UserProfileService (com.optimizely.ab.bucketing.UserProfileService)2 ArrayList (java.util.ArrayList)2 JSONException (org.json.JSONException)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Logger (org.slf4j.Logger)2 AlarmManager (android.app.AlarmManager)1 IBinder (android.os.IBinder)1 NonNull (androidx.annotation.NonNull)1