Search in sources :

Example 1 with EventHandler

use of com.optimizely.ab.event.EventHandler in project android-sdk by optimizely.

the class APISamplesInJava method samplesForDoc_EventBatchingAdvanced.

public static void samplesForDoc_EventBatchingAdvanced(Context context) {
    // -- sample starts here
    EventHandler eventHandler = DefaultEventHandler.getInstance(context);
    // Here we are using the builder options to set batch size
    // to 5 events and flush interval to a minute.
    BatchEventProcessor batchProcessor = BatchEventProcessor.builder().withBatchSize(5).withEventHandler(eventHandler).withFlushInterval(TimeUnit.MINUTES.toMillis(1L)).build();
    OptimizelyManager optimizelyManager = OptimizelyManager.builder().withSDKKey("<Your_SDK_Key>").withEventHandler(eventHandler).withDatafileDownloadInterval(15, TimeUnit.MINUTES).withEventProcessor(batchProcessor).build(context);
    OptimizelyClient optimizely = optimizelyManager.initialize(context, R.raw.datafile);
    // log event
    // -- sample starts here
    optimizely.addLogEventNotificationHandler(logEvent -> {
        Log.d("Optimizely", "event dispatched: " + logEvent);
    });
}
Also used : EventHandler(com.optimizely.ab.event.EventHandler) DefaultEventHandler(com.optimizely.ab.android.event_handler.DefaultEventHandler) BatchEventProcessor(com.optimizely.ab.event.BatchEventProcessor) OptimizelyClient(com.optimizely.ab.android.sdk.OptimizelyClient) OptimizelyManager(com.optimizely.ab.android.sdk.OptimizelyManager)

Example 2 with EventHandler

use of com.optimizely.ab.event.EventHandler in project android-sdk by optimizely.

the class OptimizelyManagerTest method setup.

@Before
public void setup() throws Exception {
    logger = mock(Logger.class);
    executor = Executors.newSingleThreadExecutor();
    defaultDatafileHandler = mock(DefaultDatafileHandler.class);
    EventHandler eventHandler = mock(DefaultEventHandler.class);
    EventProcessor eventProcessor = mock(EventProcessor.class);
    optimizelyManager = OptimizelyManager.builder(testProjectId).withLogger(logger).withDatafileDownloadInterval(3600L).withDatafileHandler(defaultDatafileHandler).withEventDispatchInterval(3600L).withEventHandler(eventHandler).withEventProcessor(eventProcessor).build(InstrumentationRegistry.getInstrumentation().getTargetContext());
    defaultDatafile = optimizelyManager.getDatafile(InstrumentationRegistry.getInstrumentation().getTargetContext(), R.raw.datafile);
    ProjectConfig config = new DatafileProjectConfig.Builder().withDatafile(defaultDatafile).build();
    when(defaultDatafileHandler.getConfig()).thenReturn(config);
}
Also used : ProjectConfig(com.optimizely.ab.config.ProjectConfig) DatafileProjectConfig(com.optimizely.ab.config.DatafileProjectConfig) EventProcessor(com.optimizely.ab.event.EventProcessor) EventHandler(com.optimizely.ab.event.EventHandler) DefaultEventHandler(com.optimizely.ab.android.event_handler.DefaultEventHandler) Logger(org.slf4j.Logger) DefaultDatafileHandler(com.optimizely.ab.android.datafile_handler.DefaultDatafileHandler) Before(org.junit.Before)

Example 3 with EventHandler

use of com.optimizely.ab.event.EventHandler in project android-sdk by optimizely.

the class OptimizelyManagerTest method initializeAsyncWithEnvironment.

@Test
public void initializeAsyncWithEnvironment() {
    Logger logger = mock(Logger.class);
    DatafileHandler datafileHandler = mock(DefaultDatafileHandler.class);
    EventHandler eventHandler = mock(DefaultEventHandler.class);
    EventProcessor eventProcessor = mock(EventProcessor.class);
    final OptimizelyManager optimizelyManager = new OptimizelyManager(testProjectId, testSdkKey, null, logger, 3600L, datafileHandler, null, 3600L, eventHandler, eventProcessor, null, null, null);
    /*
         * Scenario#1: when datafile is not Empty
         * Scenario#2: when datafile is Empty
        */
    doAnswer(new Answer<Object>() {

        public Object answer(InvocationOnMock invocation) {
            ((DatafileLoadedListener) invocation.getArguments()[2]).onDatafileLoaded(null);
            return null;
        }
    }).when(optimizelyManager.getDatafileHandler()).downloadDatafile(any(Context.class), any(DatafileConfig.class), any(DatafileLoadedListener.class));
    OptimizelyStartListener listener = new OptimizelyStartListener() {

        @Override
        public void onStart(OptimizelyClient optimizely) {
            assertNotNull(optimizelyManager.getOptimizely());
            assertNotNull(optimizelyManager.getDatafileHandler());
            assertNull(optimizelyManager.getOptimizelyStartListener());
        }
    };
    optimizelyManager.initialize(InstrumentationRegistry.getInstrumentation().getContext(), R.raw.datafile, listener);
    verify(optimizelyManager.getDatafileHandler()).startBackgroundUpdates(any(Context.class), eq(new DatafileConfig(testProjectId, testSdkKey)), eq(3600L), any(DatafileLoadedListener.class));
    assertEquals(optimizelyManager.isDatafileCached(InstrumentationRegistry.getInstrumentation().getTargetContext()), false);
    assertEquals(optimizelyManager.getDatafileUrl(), String.format((DatafileConfig.defaultHost + DatafileConfig.environmentUrlSuffix), testSdkKey));
}
Also used : Context(android.content.Context) DatafileLoadedListener(com.optimizely.ab.android.datafile_handler.DatafileLoadedListener) EventHandler(com.optimizely.ab.event.EventHandler) DefaultEventHandler(com.optimizely.ab.android.event_handler.DefaultEventHandler) DatafileConfig(com.optimizely.ab.android.shared.DatafileConfig) Logger(org.slf4j.Logger) InvocationOnMock(org.mockito.invocation.InvocationOnMock) EventProcessor(com.optimizely.ab.event.EventProcessor) DatafileHandler(com.optimizely.ab.android.datafile_handler.DatafileHandler) DefaultDatafileHandler(com.optimizely.ab.android.datafile_handler.DefaultDatafileHandler) Test(org.junit.Test)

Example 4 with EventHandler

use of com.optimizely.ab.event.EventHandler in project android-sdk by optimizely.

the class OptimizelyManager method buildOptimizely.

private OptimizelyClient buildOptimizely(@NonNull Context context, @NonNull String datafile) throws ConfigParseException {
    EventHandler eventHandler = getEventHandler(context);
    EventBatch.ClientEngine clientEngine = OptimizelyClientEngine.getClientEngineFromContext(context);
    Optimizely.Builder builder = Optimizely.builder();
    builder.withEventHandler(eventHandler);
    builder.withEventProcessor(eventProcessor);
    if (datafileHandler instanceof DefaultDatafileHandler) {
        DefaultDatafileHandler handler = (DefaultDatafileHandler) datafileHandler;
        handler.setDatafile(datafile);
        builder.withConfigManager(handler);
    } else {
        builder.withDatafile(datafile);
    }
    // override client sdk name/version to be included in events
    builder.withClientInfo(clientEngine, sdkVersion);
    if (errorHandler != null) {
        builder.withErrorHandler(errorHandler);
    }
    builder.withUserProfileService(userProfileService);
    builder.withNotificationCenter(notificationCenter);
    builder.withDefaultDecideOptions(defaultDecideOptions);
    Optimizely optimizely = builder.build();
    return new OptimizelyClient(optimizely, LoggerFactory.getLogger(OptimizelyClient.class));
}
Also used : EventHandler(com.optimizely.ab.event.EventHandler) DefaultEventHandler(com.optimizely.ab.android.event_handler.DefaultEventHandler) Optimizely(com.optimizely.ab.Optimizely) EventBatch(com.optimizely.ab.event.internal.payload.EventBatch) DefaultDatafileHandler(com.optimizely.ab.android.datafile_handler.DefaultDatafileHandler)

Example 5 with EventHandler

use of com.optimizely.ab.event.EventHandler in project android-sdk by optimizely.

the class OptimizelyManagerBuilderTest method testBuildWithEventHandler.

@Test
public void testBuildWithEventHandler() {
    EventHandler eventHandler = mock(EventHandler.class);
    OptimizelyManager manager = OptimizelyManager.builder().withSDKKey(testSdkKey).withDatafileDownloadInterval(901L, TimeUnit.SECONDS).withEventHandler(eventHandler).build(mockContext);
    assertEquals(901L, manager.getDatafileDownloadInterval().longValue());
    assertEquals(manager.getEventHandler(mockContext), eventHandler);
}
Also used : EventHandler(com.optimizely.ab.event.EventHandler) Test(org.junit.Test)

Aggregations

EventHandler (com.optimizely.ab.event.EventHandler)10 DefaultEventHandler (com.optimizely.ab.android.event_handler.DefaultEventHandler)6 Test (org.junit.Test)6 DefaultDatafileHandler (com.optimizely.ab.android.datafile_handler.DefaultDatafileHandler)4 EventProcessor (com.optimizely.ab.event.EventProcessor)4 LogEvent (com.optimizely.ab.event.LogEvent)3 Logger (org.slf4j.Logger)3 Context (android.content.Context)2 DatafileHandler (com.optimizely.ab.android.datafile_handler.DatafileHandler)2 OptimizelyManager (com.optimizely.ab.android.sdk.OptimizelyManager)2 BatchEventProcessor (com.optimizely.ab.event.BatchEventProcessor)2 IntentFilter (android.content.IntentFilter)1 Optimizely (com.optimizely.ab.Optimizely)1 DatafileLoadedListener (com.optimizely.ab.android.datafile_handler.DatafileLoadedListener)1 EventRescheduler (com.optimizely.ab.android.event_handler.EventRescheduler)1 OptimizelyClient (com.optimizely.ab.android.sdk.OptimizelyClient)1 DatafileConfig (com.optimizely.ab.android.shared.DatafileConfig)1 DatafileProjectConfig (com.optimizely.ab.config.DatafileProjectConfig)1 ProjectConfig (com.optimizely.ab.config.ProjectConfig)1 EventBatch (com.optimizely.ab.event.internal.payload.EventBatch)1