Skip to content

Commit

Permalink
Merge pull request #398 from Countly/lifecycle_check_device_id
Browse files Browse the repository at this point in the history
feat: lifecycle check to without merge
  • Loading branch information
turtledreams authored Oct 3, 2024
2 parents 4b13838 + 1c8782d commit a507dd9
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Disabled caching for webviews.
* Expanded the flag (enablePreviousNameRecording) to add current view name as segmentation to custom events. (Experimental!)

* Mitigated an issue where a session could have started while the app was in the background when the device ID was changed (non-merge).
* Mitigated an issue where content fetching was enabled after initialization of the SDK.

* Fixed an issue where the validation of the parameters during content retrieval was improper.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ly.count.android.sdk;

class TestLifecycleObserver implements Countly.LifecycleObserver {
boolean isForeground = false;

@Override
public boolean LifeCycleAtleastStarted() {
return isForeground;
}

protected void bringToForeground() {
this.isForeground = true;
}

protected void goToBackground() {
this.isForeground = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,11 @@ public void SE_203_CR_CG_M_id_change() throws InterruptedException {
@Test
public void SE_204_CNR_A_id_change() throws InterruptedException {
CountlyConfig config = TestUtils.createBaseConfig(TestUtils.getContext());
TestLifecycleObserver testLifecycleObserver = new TestLifecycleObserver();
config.lifecycleObserver = testLifecycleObserver;
Countly countly = new Countly().init(config);

flowAutomaticSessions(countly);
flowAutomaticSessions(countly, testLifecycleObserver);

Assert.assertEquals(16, TestUtils.getCurrentRQ().length);
validateSessionBeginRequest(0, TestUtils.commonDeviceId);
Expand Down Expand Up @@ -266,7 +268,7 @@ public void SE_205_CR_CG_A_id_change() throws InterruptedException {
CountlyConfig config = TestUtils.createBaseConfig(TestUtils.getContext()).setRequiresConsent(true).setConsentEnabled(new String[] { "sessions" });
Countly countly = new Countly().init(config);

flowAutomaticSessions(countly);
flowAutomaticSessions(countly, new TestLifecycleObserver());

Assert.assertEquals(7, TestUtils.getCurrentRQ().length);
validateSessionConsentRequest(0, true, TestUtils.commonDeviceId);
Expand Down Expand Up @@ -308,7 +310,7 @@ public void SE_206_CR_CNG_A_id_change() throws InterruptedException {
CountlyConfig config = TestUtils.createBaseConfig(TestUtils.getContext()).setRequiresConsent(true);
Countly countly = new Countly().init(config);

flowAutomaticSessions(countly);
flowAutomaticSessions(countly, new TestLifecycleObserver());

Assert.assertEquals(5, TestUtils.getCurrentRQ().length);
validateSessionConsentRequest(0, false, TestUtils.commonDeviceId);
Expand Down Expand Up @@ -339,8 +341,8 @@ private void flowManualSessions(Countly countly) throws InterruptedException {
countly.sessions().updateSession();
}

private void flowAutomaticSessions(Countly countly) throws InterruptedException {

private void flowAutomaticSessions(Countly countly, TestLifecycleObserver testLifecycleObserver) throws InterruptedException {
testLifecycleObserver.bringToForeground();
countly.onStart(null);

Thread.sleep(1000);
Expand All @@ -353,15 +355,19 @@ private void flowAutomaticSessions(Countly countly) throws InterruptedException
countly.deviceId().changeWithoutMerge("newID_2");
Thread.sleep(1000);

testLifecycleObserver.goToBackground();
countly.onStop();

Thread.sleep(1000);

testLifecycleObserver.bringToForeground();
countly.onStart(null);

countly.deviceId().changeWithMerge("newID");
testLifecycleObserver.goToBackground();
countly.onStop();
Thread.sleep(1000);
testLifecycleObserver.bringToForeground();
countly.onStart(null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public void eventSaveScenario_onTimer() throws InterruptedException, JSONExcepti
* Related user properties should be saved before event recordings
* call order, user property with "dark_mode", event, user property with "light_mode"
* generated request order first user property request + 3 events + user property request with light_mode + begin session
* UPDATE: no begin session anymore because isForeground is false, and it is added to begin session to validate that device in foreground
*/
@Test
public void eventSaveScenario_changeDeviceIDWithoutMerge() throws JSONException {
Expand All @@ -115,8 +116,8 @@ public void eventSaveScenario_changeDeviceIDWithoutMerge() throws JSONException
countly.deviceId().changeWithoutMerge("new_device_id"); // this will begin a new session

// first user property request + 3 events + user property request with light_mode
ModuleUserProfileTests.validateUserProfileRequest(0, 4, TestUtils.map(), TestUtils.map("theme", "dark_mode"));
ModuleUserProfileTests.validateUserProfileRequest(2, 4, TestUtils.map(), TestUtils.map("theme", "light_mode"));
ModuleUserProfileTests.validateUserProfileRequest(0, 3, TestUtils.map(), TestUtils.map("theme", "dark_mode"));
ModuleUserProfileTests.validateUserProfileRequest(2, 3, TestUtils.map(), TestUtils.map("theme", "light_mode"));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/main/java/ly/count/android/sdk/ModuleSessions.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ void halt() {

@Override
void deviceIdChanged(boolean withoutMerge) {
if (!manualSessionControlEnabled && withoutMerge) {
if (!manualSessionControlEnabled && withoutMerge && _cly.config_.lifecycleObserver.LifeCycleAtleastStarted()) {
L.d("[ModuleSessions] deviceIdChanged, automatic session control enabled and device id changed without merge, starting a new session");
beginSessionInternal();
}
Expand Down

0 comments on commit a507dd9

Please sign in to comment.