|
| 1 | +/**************************************************************************** |
| 2 | + * Copyright 2022, Optimizely, Inc. and contributors * |
| 3 | + * * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); * |
| 5 | + * you may not use this file except in compliance with the License. * |
| 6 | + * You may obtain a copy of the License at * |
| 7 | + * * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 * |
| 9 | + * * |
| 10 | + * Unless required by applicable law or agreed to in writing, software * |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, * |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * |
| 13 | + * See the License for the specific language governing permissions and * |
| 14 | + * limitations under the License. * |
| 15 | + ***************************************************************************/ |
| 16 | + |
| 17 | +package com.optimizely.ab.android.sdk; |
| 18 | + |
| 19 | +import static junit.framework.Assert.assertEquals; |
| 20 | +import static org.mockito.Mockito.mock; |
| 21 | +import static org.mockito.Mockito.timeout; |
| 22 | +import static org.mockito.Mockito.verify; |
| 23 | +import android.content.Context; |
| 24 | +import androidx.test.ext.junit.runners.AndroidJUnit4; |
| 25 | +import androidx.test.platform.app.InstrumentationRegistry; |
| 26 | +import com.optimizely.ab.event.EventHandler; |
| 27 | +import com.optimizely.ab.event.LogEvent; |
| 28 | +import org.junit.Test; |
| 29 | +import org.junit.runner.RunWith; |
| 30 | +import org.mockito.ArgumentCaptor; |
| 31 | +import java.util.concurrent.TimeUnit; |
| 32 | + |
| 33 | +@RunWith(AndroidJUnit4.class) |
| 34 | +public class OptimizelyManagerEventHandlerTest { |
| 35 | + |
| 36 | + private String minDatafileWithEvent = "{\n" + |
| 37 | + "experiments: [ ],\n" + |
| 38 | + "version: \"2\",\n" + |
| 39 | + "audiences: [ ],\n" + |
| 40 | + "groups: [ ],\n" + |
| 41 | + "attributes: [ ],\n" + |
| 42 | + "projectId: \"123\",\n" + |
| 43 | + "accountId: \"6365361536\",\n" + |
| 44 | + "events: [{\"experimentIds\": [\"8509139139\"], \"id\": \"8505434668\", \"key\": \"test_event\"}],\n" + |
| 45 | + "revision: \"1\"\n" + |
| 46 | + "}"; |
| 47 | + |
| 48 | + @Test |
| 49 | + public void eventClientNameAndVersion() throws Exception { |
| 50 | + EventHandler mockEventHandler = mock(EventHandler.class); |
| 51 | + |
| 52 | + Context context = InstrumentationRegistry.getInstrumentation().getTargetContext(); |
| 53 | + OptimizelyManager optimizelyManager = OptimizelyManager.builder() |
| 54 | + .withSDKKey("any-sdk-key") |
| 55 | + .withEventDispatchInterval(0, TimeUnit.SECONDS) |
| 56 | + .withEventHandler(mockEventHandler) |
| 57 | + .build(context); |
| 58 | + |
| 59 | + OptimizelyClient optimizelyClient = optimizelyManager.initialize(context, minDatafileWithEvent); |
| 60 | + optimizelyClient.track("test_event", "tester"); |
| 61 | + |
| 62 | + ArgumentCaptor<LogEvent> argument = ArgumentCaptor.forClass(LogEvent.class); |
| 63 | + verify(mockEventHandler, timeout(5000)).dispatchEvent(argument.capture()); |
| 64 | + assertEquals(argument.getValue().getEventBatch().getClientName(), "android-sdk"); |
| 65 | + assertEquals(argument.getValue().getEventBatch().getClientVersion(), BuildConfig.CLIENT_VERSION); |
| 66 | + } |
| 67 | + |
| 68 | +} |
0 commit comments