Skip to content

Commit

Permalink
feat: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arifBurakDemiray committed Aug 21, 2024
1 parent 9af1ad8 commit 23264b0
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1758,6 +1758,64 @@ public void startView_restartAfterActivityComesFromForeground() throws Interrupt
TestUtils.assertRQSize(10);
}

/**
* "startView" with bg/fg switch case
* - Validate that after an auto stopped view is started and app gone to background
* running view should stop and send a stop view request
* - After coming from the background to foreground the stopped view should start again,
* and it should contain the previous segmentation
* - When we stop the view it should be removed from the cached views
*
* @throws InterruptedException if the thread is interrupted
* @throws JSONException if the JSON is not valid
*/
@Test
public void startView_restartAfterActivityComesFromForeground_copyPreviousSegmentation() throws InterruptedException, JSONException {
CountlyConfig countlyConfig = TestUtils.createScenarioEventIDConfig(TestUtils.incrementalViewIdGenerator(), TestUtils.incrementalEventIdGenerator());
countlyConfig.setApplication(null);
countlyConfig.setContext(TestUtils.getContext());
countlyConfig.setGlobalViewSegmentation(TestUtils.map("try", "this", "maybe", false));

countlyConfig.setEventQueueSizeToSend(1);
Countly countly = new Countly().init(countlyConfig);

Map<String, Object> segmentation = TestUtils.map(
"a", "a",
"b", 55,
"c", new JSONArray(Arrays.asList("aa", "asd", "ad8")),
"d", true
);

Activity activity = mock(Activity.class);

TestUtils.assertRQSize(0);

countly.onStart(activity);
countly.views().startView("test", segmentation);

countly.views().addSegmentationToViewWithName("test", TestUtils.map("yama", "de", "kuda", "sai"));

segmentation.put("try", "this");
segmentation.put("maybe", false);

ModuleSessionsTests.validateSessionBeginRequest(0, TestUtils.commonDeviceId);
ModuleEventsTests.validateEventInRQ(TestUtils.commonDeviceId, ModuleViews.ORIENTATION_EVENT_KEY, null, 1, 0.0, 0.0, "ide1", "_CLY_", "", "_CLY_", 1, 3, 0, 1);
validateView("test", 0.0, 2, 3, true, true, segmentation, "idv1", "");

Thread.sleep(1000);

countly.onStop();
ModuleSessionsTests.validateSessionEndRequest(3, 1, TestUtils.commonDeviceId);
validateView("test", 1.0, 4, 5, false, false, TestUtils.map("try", "this", "maybe", false, "yama", "de", "kuda", "sai"), "idv1", "");

countly.onStart(activity);

ModuleSessionsTests.validateSessionBeginRequest(5, TestUtils.commonDeviceId);
ModuleEventsTests.validateEventInRQ(TestUtils.commonDeviceId, ModuleViews.ORIENTATION_EVENT_KEY, null, 1, 0.0, 0.0, "ide2", "_CLY_", "idv1", "_CLY_", 6, 8, 0, 1);
validateView("test", 0.0, 7, 8, true, true, TestUtils.map("try", "this", "maybe", false, "yama", "de", "kuda", "sai"), "idv2", "idv1");
TestUtils.assertRQSize(8);
}

static void validateView(String viewName, Double viewDuration, int idx, int size, boolean start, boolean visit, Map<String, Object> customSegmentation, String id, String pvid) throws JSONException {
Map<String, Object> viewSegmentation = TestUtils.map("name", viewName, "segment", "Android");
if (start) {
Expand Down
2 changes: 0 additions & 2 deletions sdk/src/main/java/ly/count/android/sdk/ModuleViews.java
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,6 @@ void stopViewWithIDInternal(@Nullable String viewID, @Nullable Map<String, Objec
if (!vd.willStartAgain) {
viewDataMap.remove(vd.viewID);
}

vd.viewSegmentation = null; // clear the segmentation to release cache
}

void recordViewEndEvent(ViewData vd, @Nullable Map<String, Object> customViewSegmentation, String viewRecordingSource) {
Expand Down

0 comments on commit 23264b0

Please sign in to comment.