Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jae/recover 459 #487

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ public void setup() throws Exception {
notificationCenter,
null,
odpManager,
"test-vuid");
"test-vuid",
null,
null);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,20 @@
@RunWith(AndroidJUnit4.class)
public class OptimizelyClientEngineTest {
@Test
public void testGetClientEngineFromContextAndroidTV() {
public void testGetClientEngineNameFromContextAndroidTV() {
Context context = mock(Context.class);
UiModeManager uiModeManager = mock(UiModeManager.class);
when(context.getSystemService(Context.UI_MODE_SERVICE)).thenReturn(uiModeManager);
when(uiModeManager.getCurrentModeType()).thenReturn(Configuration.UI_MODE_TYPE_TELEVISION);
assertEquals(EventBatch.ClientEngine.ANDROID_TV_SDK, OptimizelyClientEngine.getClientEngineFromContext(context));
assertEquals("android-tv-sdk", OptimizelyClientEngine.getClientEngineNameFromContext(context));
}

@Test
public void testGetClientEngineFromContextAndroid() {
public void testGetClientEngineNameFromContextAndroid() {
Context context = mock(Context.class);
UiModeManager uiModeManager = mock(UiModeManager.class);
when(context.getSystemService(Context.UI_MODE_SERVICE)).thenReturn(uiModeManager);
when(uiModeManager.getCurrentModeType()).thenReturn(Configuration.UI_MODE_TYPE_NORMAL);
assertEquals(EventBatch.ClientEngine.ANDROID_SDK, OptimizelyClientEngine.getClientEngineFromContext(context));
assertEquals("android-sdk", OptimizelyClientEngine.getClientEngineNameFromContext(context));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,25 @@ public void eventClientNameAndVersion() throws Exception {
assertEquals(argument.getValue().getEventBatch().getClientVersion(), BuildConfig.CLIENT_VERSION);
}

@Test
public void eventClientWithCustomNameAndVersion() throws Exception {
EventHandler mockEventHandler = mock(EventHandler.class);

Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
OptimizelyManager optimizelyManager = OptimizelyManager.builder()
.withSDKKey("any-sdk-key")
.withEventDispatchInterval(0, TimeUnit.SECONDS)
.withEventHandler(mockEventHandler)
.withClientInfo("test-sdk", "test-version")
.build(context);

OptimizelyClient optimizelyClient = optimizelyManager.initialize(context, minDatafileWithEvent);
optimizelyClient.track("test_event", "tester");

ArgumentCaptor<LogEvent> argument = ArgumentCaptor.forClass(LogEvent.class);
verify(mockEventHandler, timeout(5000)).dispatchEvent(argument.capture());
assertEquals(argument.getValue().getEventBatch().getClientName(), "test-sdk");
assertEquals(argument.getValue().getEventBatch().getClientVersion(), "test-version");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public void initializeSyncWithEnvironment() {
EventHandler eventHandler = mock(DefaultEventHandler.class);
EventProcessor eventProcessor = mock(EventProcessor.class);
OptimizelyManager optimizelyManager = new OptimizelyManager(testProjectId, testSdkKey, null, logger, 3600L, datafileHandler, null, 3600L,
eventHandler, eventProcessor, null, null, null, null, null);
eventHandler, eventProcessor, null, null, null, null, null, null, null);
/*
* Scenario#1: when datafile is not Empty
* Scenario#2: when datafile is Empty
Expand Down Expand Up @@ -226,7 +226,7 @@ public void initializeAsyncWithEnvironment() {
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, null, null);
eventHandler, eventProcessor, null, null, null, null, null, null, null);

/*
* Scenario#1: when datafile is not Empty
Expand Down Expand Up @@ -498,7 +498,7 @@ public void initializeSyncWithUpdateOnNewDatafileDisabled() {
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();

OptimizelyManager manager = new OptimizelyManager(testProjectId, testSdkKey, null, logger, pollingInterval, datafileHandler, null, 0,
null, null, null, null, null, null, null);
null, null, null, null, null, null, null, null, null);

doAnswer(
new Answer<Object>() {
Expand Down Expand Up @@ -531,7 +531,7 @@ public void initializeSyncWithUpdateOnNewDatafileEnabled() {
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();

OptimizelyManager manager = new OptimizelyManager(testProjectId, testSdkKey, null, logger, pollingInterval, datafileHandler, null, 0,
null, null, null, null, null, null, null);
null, null, null, null, null, null, null, null, null);

doAnswer(
new Answer<Object>() {
Expand Down Expand Up @@ -564,7 +564,7 @@ public void initializeSyncWithDownloadToCacheDisabled() {
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();

OptimizelyManager manager = new OptimizelyManager(testProjectId, testSdkKey, null, logger, pollingInterval, datafileHandler, null, 0,
null, null, null, null, null, null, null);
null, null, null, null, null, null, null, null, null);

doAnswer(
new Answer<Object>() {
Expand Down Expand Up @@ -597,7 +597,7 @@ public void initializeSyncWithUpdateOnNewDatafileDisabledWithPeriodicPollingEnab
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();

OptimizelyManager manager = new OptimizelyManager(testProjectId, testSdkKey, null, logger, pollingInterval, datafileHandler, null, 0,
null, null, null, null, null, null, null);
null, null, null, null, null, null, null, null, null);

doAnswer(
(Answer<Object>) invocation -> {
Expand Down Expand Up @@ -629,7 +629,7 @@ public void initializeSyncWithUpdateOnNewDatafileEnabledWithPeriodicPollingEnabl
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();

OptimizelyManager manager = new OptimizelyManager(testProjectId, testSdkKey, null, logger, pollingInterval, datafileHandler, null, 0,
null, null, null, null, null, null, null);
null, null, null, null, null, null, null, null, null);

doAnswer(
new Answer<Object>() {
Expand Down Expand Up @@ -662,7 +662,7 @@ public void initializeSyncWithUpdateOnNewDatafileDisabledWithPeriodicPollingDisa
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();

OptimizelyManager manager = new OptimizelyManager(testProjectId, testSdkKey, null, logger, pollingInterval, datafileHandler, null, 0,
null, null, null, null, null, null, null);
null, null, null, null, null, null, null, null, null);

doAnswer(
new Answer<Object>() {
Expand Down Expand Up @@ -696,7 +696,7 @@ public void initializeSyncWithUpdateOnNewDatafileEnabledWithPeriodicPollingDisab
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();

OptimizelyManager manager = new OptimizelyManager(testProjectId, testSdkKey, null, logger, pollingInterval, datafileHandler, null, 0,
null, null, null, null, null, null, null);
null, null, null, null, null, null, null, null, null);

doAnswer(
new Answer<Object>() {
Expand Down Expand Up @@ -729,7 +729,7 @@ public void initializeSyncWithResourceDatafileNoCache() {
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();

OptimizelyManager manager = spy(new OptimizelyManager(testProjectId, testSdkKey, null, logger, pollingInterval, datafileHandler, null, 0,
null, null, null, null, null, null, null));
null, null, null, null, null, null, null, null, null));

datafileHandler.removeSavedDatafile(context, manager.getDatafileConfig());
OptimizelyClient client = manager.initialize(context, R.raw.datafile, downloadToCache, updateConfigOnNewDatafile);
Expand All @@ -746,7 +746,7 @@ public void initializeSyncWithResourceDatafileNoCacheWithDefaultParams() {
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();

OptimizelyManager manager = spy(new OptimizelyManager(testProjectId, testSdkKey, null, logger, pollingInterval, datafileHandler, null, 0,
null, null, null, null, null, null, null));
null, null, null, null, null, null, null, null, null));

datafileHandler.removeSavedDatafile(context, manager.getDatafileConfig());
OptimizelyClient client = manager.initialize(context, R.raw.datafile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,30 @@
*/
public class OptimizelyClientEngine {

/**
* Get client engine name for current UI mode type
*
* @param context any valid Android {@link Context}
* @return client engine name ("android-sdk" or "android-tv-sdk")
*/
public static String getClientEngineNameFromContext(@NonNull Context context) {
UiModeManager uiModeManager = (UiModeManager) context.getSystemService(Context.UI_MODE_SERVICE);

if (uiModeManager != null && uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION) {
return "android-tv-sdk";
}

return "android-sdk";
}

/**
* Get client engine value for current UI mode type
*
* @param context any valid Android {@link Context}
* @return String value of client engine
* @deprecated Consider using {@link #getClientEngineNameFromContext(Context)}
*/
@Deprecated
public static EventBatch.ClientEngine getClientEngineFromContext(@NonNull Context context) {
UiModeManager uiModeManager = (UiModeManager) context.getSystemService(Context.UI_MODE_SERVICE);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ public class OptimizelyManager {
private boolean returnInMainThreadFromAsyncInit = true;

@Nullable private final List<OptimizelyDecideOption> defaultDecideOptions;
private String sdkVersion = null;
private String customSdkName = null;
private String customSdkVersion = null;

OptimizelyManager(@Nullable String projectId,
@Nullable String sdkKey,
Expand All @@ -116,7 +117,9 @@ public class OptimizelyManager {
@NonNull NotificationCenter notificationCenter,
@Nullable List<OptimizelyDecideOption> defaultDecideOptions,
@Nullable ODPManager odpManager,
@Nullable String vuid) {
@Nullable String vuid,
@Nullable String clientEngineName,
@Nullable String clientVersion) {

if (projectId == null && sdkKey == null) {
logger.error("projectId and sdkKey are both null!");
Expand All @@ -142,12 +145,8 @@ public class OptimizelyManager {
this.notificationCenter = notificationCenter;
this.defaultDecideOptions = defaultDecideOptions;

try {
sdkVersion = BuildConfig.CLIENT_VERSION;
logger.info("SDK Version: {}", sdkVersion);
} catch (Exception e) {
logger.warn("Error getting BuildConfig version");
}
this.customSdkName = clientEngineName;
this.customSdkVersion = clientVersion;
}

@VisibleForTesting
Expand Down Expand Up @@ -537,6 +536,29 @@ public DatafileHandler getDatafileHandler() {
return datafileHandler;
}

@NonNull
public String getSdkName(Context context) {
String sdkName = customSdkName;
if (sdkName == null) {
sdkName = OptimizelyClientEngine.getClientEngineNameFromContext(context);
}
return sdkName;
}

@NonNull
public String getSdkVersion() {
String sdkVersion = customSdkVersion;
if (sdkVersion == null) {
try {
sdkVersion = BuildConfig.CLIENT_VERSION;
} catch (Exception e) {
logger.warn("Error getting BuildConfig version");
sdkVersion = "UNKNOWN";
}
}
return sdkVersion;
}

private boolean datafileDownloadEnabled() {
return datafileDownloadInterval > 0;
}
Expand Down Expand Up @@ -600,7 +622,8 @@ public void onStartComplete(UserProfileService userProfileService) {
private OptimizelyClient buildOptimizely(@NonNull Context context, @NonNull String datafile) throws ConfigParseException {
EventHandler eventHandler = getEventHandler(context);

EventBatch.ClientEngine clientEngine = OptimizelyClientEngine.getClientEngineFromContext(context);
String sdkName = getSdkName(context);
String sdkVersion = getSdkVersion();

Optimizely.Builder builder = Optimizely.builder();

Expand All @@ -617,7 +640,8 @@ private OptimizelyClient buildOptimizely(@NonNull Context context, @NonNull Stri
}

// override client sdk name/version to be included in events
builder.withClientInfo(clientEngine, sdkVersion);
builder.withClientInfo(sdkName, sdkVersion);
logger.info("SDK name: {} and version: {}", sdkName, sdkVersion);

if (errorHandler != null) {
builder.withErrorHandler(errorHandler);
Expand Down Expand Up @@ -770,6 +794,9 @@ public static class Builder {
private boolean odpEnabled = true;
private String vuid = null;

private String customSdkName = null;
private String customSdkVersion = null;

@Deprecated
/**
* @deprecated use {@link #Builder()} instead and pass in an SDK Key with {@link #withSDKKey(String)}
Expand Down Expand Up @@ -1014,6 +1041,18 @@ public Builder withVuid(String vuid) {
return this;
}

/**
* Override the SDK name and version (for client SDKs like flutter-sdk wrapping the core android-sdk) to be included in events.
*
* @param clientEngineName the client engine name ("flutter/android-sdk", etc.).
* @param clientVersion the client SDK version.
* @return this {@link Builder} instance
*/
public Builder withClientInfo(@Nullable String clientEngineName, @Nullable String clientVersion) {
this.customSdkName = clientEngineName;
this.customSdkVersion = clientVersion;
return this;
}
/**
* Get a new {@link Builder} instance to create {@link OptimizelyManager} with.
* @param context the application context used to create default service if not provided.
Expand Down Expand Up @@ -1126,7 +1165,10 @@ public OptimizelyManager build(Context context) {
notificationCenter,
defaultDecideOptions,
odpManager,
vuid);
vuid,
customSdkName,
customSdkVersion
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -216,18 +216,14 @@ public void testBuildWithDatafileDownloadInterval_workerCancelledWhenIntervalIsN
}

@Test
public void testBuildWithDatafileDownloadInterval_workerCancelledWhenNoIntervalProvided() throws Exception {
public void testBuildWithCustomSdkNameAndVersion() throws Exception {
OptimizelyManager manager = OptimizelyManager.builder()
.withSDKKey(testSdkKey)
.withDatafileHandler(mockDatafileHandler)
.withVuid("any-to-avoid-generate")
.build(mockContext);
OptimizelyManager spyManager = spy(manager);
when(spyManager.isAndroidVersionSupported()).thenReturn(true);
spyManager.initialize(mockContext, "");

verify(mockDatafileHandler).stopBackgroundUpdates(any(), any());
verify(mockDatafileHandler, never()).startBackgroundUpdates(any(), any(), any(), any());
.withSDKKey(testSdkKey)
.withClientInfo("test-sdk", "test-version")
.withVuid("any-to-avoid-generate")
.build(mockContext);
assertEquals(manager.getSdkName(mockContext), "test-sdk");
assertEquals(manager.getSdkVersion(), "test-version");
}

@Test
Expand All @@ -254,7 +250,9 @@ public void testBuildWithDefaultODP_defaultEnabled() throws Exception {
any(NotificationCenter.class),
any(), // nullable (DefaultDecideOptions)
any(ODPManager.class),
eq("test-vuid"));
eq("test-vuid"),
any(),
any());
}

@Test
Expand Down Expand Up @@ -282,7 +280,9 @@ public void testBuildWithDefaultODP_disabled() throws Exception {
any(NotificationCenter.class),
any(), // nullable (DefaultDecideOptions)
isNull(),
eq("test-vuid"));
eq("test-vuid"),
any(),
any());
}

@Test
Expand Down
Loading
Loading