Skip to content

Commit 157fe11

Browse files
feat: test cases for it
1 parent 4862c83 commit 157fe11

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

sdk/src/androidTest/java/ly/count/android/sdk/ModuleViewsTests.java

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2062,6 +2062,92 @@ public void autoViewTracking_consentRemoval() throws JSONException {
20622062
Assert.assertEquals(9, TestUtils.getCurrentRQ().length);
20632063
}
20642064

2065+
/**
2066+
* "startView" with bg/fg switch case
2067+
* - Validate that after an auto stopped view is started and app gone to background
2068+
* running view should not stop because behavior is disabled
2069+
* - After coming from the background to foreground no view should be started because behavior is disabled
2070+
*
2071+
* @throws InterruptedException if the thread is interrupted
2072+
* @throws JSONException if the JSON is not valid
2073+
*/
2074+
@Test
2075+
public void startView_restartAfterActivityComesFromForeground_behaviorDisabled() throws InterruptedException, JSONException {
2076+
CountlyConfig countlyConfig = TestUtils.createScenarioEventIDConfig(TestUtils.incrementalViewIdGenerator(), TestUtils.incrementalEventIdGenerator());
2077+
countlyConfig.setApplication(null);
2078+
countlyConfig.setContext(TestUtils.getContext());
2079+
countlyConfig.disableViewRestartForManualRecording();
2080+
countlyConfig.setEventQueueSizeToSend(1);
2081+
Countly countly = new Countly().init(countlyConfig);
2082+
2083+
Activity activity = mock(Activity.class);
2084+
2085+
TestUtils.assertRQSize(0);
2086+
2087+
countly.onStart(activity);
2088+
countly.views().startView("test");
2089+
2090+
ModuleSessionsTests.validateSessionBeginRequest(0, TestUtils.commonDeviceId);
2091+
ModuleEventsTests.validateEventInRQ(TestUtils.commonDeviceId, ModuleViews.ORIENTATION_EVENT_KEY, null, 1, 0.0, 0.0, "ide1", "_CLY_", "", "_CLY_", 1, 3, 0, 1);
2092+
validateView("test", 0.0, 2, 3, true, true, null, "idv1", "");
2093+
2094+
Thread.sleep(1000);
2095+
2096+
countly.onStop();
2097+
ModuleSessionsTests.validateSessionEndRequest(3, 1, TestUtils.commonDeviceId);
2098+
Thread.sleep(1000);
2099+
countly.onStart(activity);
2100+
2101+
ModuleSessionsTests.validateSessionBeginRequest(4, TestUtils.commonDeviceId);
2102+
ModuleEventsTests.validateEventInRQ(TestUtils.commonDeviceId, ModuleViews.ORIENTATION_EVENT_KEY, null, 1, 0.0, 0.0, "ide2", "_CLY_", "idv1", "_CLY_", 5, 6, 0, 1);
2103+
2104+
Thread.sleep(1000);
2105+
2106+
countly.views().stopViewWithName("test");
2107+
validateView("test", 3.0, 6, 7, false, false, null, "idv1", "");
2108+
countly.onStop();
2109+
2110+
ModuleSessionsTests.validateSessionEndRequest(7, 1, TestUtils.commonDeviceId);
2111+
TestUtils.assertRQSize(8);
2112+
}
2113+
2114+
/**
2115+
* Auto view tracking with restart is disabled for manual views
2116+
* Validate that after an auto stopped view is started and app gone to background
2117+
* running view should stop because auto views are not affected by the disabled behavior
2118+
*
2119+
* @throws JSONException if the JSON is not valid
2120+
*/
2121+
@Test
2122+
public void autoViewTracking_restartDisabledForManualViews() throws JSONException, InterruptedException {
2123+
CountlyConfig countlyConfig = TestUtils.createBaseConfig(TestUtils.getContext());
2124+
countlyConfig.setLoggingEnabled(true);
2125+
countlyConfig.enableAutomaticViewTracking();
2126+
countlyConfig.disableViewRestartForManualRecording();
2127+
countlyConfig.setEventQueueSizeToSend(1);
2128+
2129+
Countly countly = new Countly().init(countlyConfig);
2130+
2131+
Activity activity = Mockito.mock(Activity.class);
2132+
countly.onStart(activity);
2133+
2134+
ModuleSessionsTests.validateSessionBeginRequest(0, TestUtils.commonDeviceId);
2135+
ModuleEventsTests.validateEventInRQ("[CLY]_orientation", TestUtils.map("mode", "portrait"), 1, 0, 0, 1, 3);
2136+
validateView(activity.getClass().getName(), 0.0, 2, 3, true, true, TestUtils.map(), "_CLY_", "_CLY_", null);
2137+
Thread.sleep(1000);
2138+
countly.onStop();
2139+
ModuleSessionsTests.validateSessionEndRequest(3, 1, TestUtils.commonDeviceId);
2140+
validateView(activity.getClass().getName(), 1.0, 4, 5, false, false, null, "_CLY_", "_CLY_");
2141+
2142+
countly.onStart(activity);
2143+
Thread.sleep(1000);
2144+
ModuleSessionsTests.validateSessionBeginRequest(5, TestUtils.commonDeviceId);
2145+
ModuleEventsTests.validateEventInRQ("[CLY]_orientation", TestUtils.map("mode", "portrait"), 1, 0, 0, 6, 8);
2146+
2147+
validateView(activity.getClass().getName(), 0.0, 7, 8, true, true, TestUtils.map(), "_CLY_", "_CLY_", null);
2148+
Assert.assertEquals(8, TestUtils.getCurrentRQ().length);
2149+
}
2150+
20652151
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 {
20662152
validateView(viewName, viewDuration, idx, size, start, visit, customSegmentation, id, pvid, null);
20672153
}

0 commit comments

Comments
 (0)