Skip to content

Commit e2ae4ac

Browse files
feat: new view test
1 parent 829a420 commit e2ae4ac

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import 'dart:convert';
2+
import 'dart:math';
3+
import 'package:flutter_foreground_task/flutter_foreground_task.dart';
4+
import 'dart:io';
5+
6+
import 'package:countly_flutter/countly_flutter.dart';
7+
import 'package:flutter_test/flutter_test.dart';
8+
import 'package:integration_test/integration_test.dart';
9+
import '../utils.dart';
10+
11+
///
12+
/// This test is to check the flow of views is auto stopped and restarted
13+
/// and also when the app is in background and then comes to foreground
14+
/// and also to check if the previous view names are recorded
15+
///
16+
void main() {
17+
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
18+
testWidgets('Start auto stopped views and stop views', (WidgetTester tester) async {
19+
// Initialize the SDK
20+
CountlyConfig config = CountlyConfig(SERVER_URL, APP_KEY);
21+
await Countly.initWithConfig(config);
22+
23+
await Countly.instance.views.startView("V1");
24+
await Countly.instance.views.startAutoStoppedView("V2");
25+
26+
FlutterForegroundTask.minimizeApp();
27+
sleep(Duration(seconds: 2));
28+
29+
await Countly.instance.views.startAutoStoppedView("V4");
30+
await Countly.instance.views.startView("V3");
31+
32+
sleep(Duration(seconds: 2));
33+
34+
FlutterForegroundTask.launchApp();
35+
if (Platform.isIOS) {
36+
printMessageMultipleTimes('waiting for 3 seconds, now go to foreground', 3);
37+
}
38+
39+
sleep(Duration(seconds: 5));
40+
41+
// REQUESTS: begin session, events before going in to the background, and end session
42+
// EVENTS: end view for V1, V2, start view of V4, end view for V4, start view V3, orientation, start view for V1, V2
43+
List<String> requestList = await getRequestQueue();
44+
List<String> eventList = await getEventQueue();
45+
46+
printQueues(requestList, eventList);
47+
expect(requestList.length, Platform.isAndroid ? 4 : 3);
48+
expect(eventList.length, Platform.isAndroid ? anyOf(8, 9) : 3); // 3 for iOS
49+
validateBeginSessionRequest(requestList[0]); // validate begin session on 0th idx
50+
51+
Map<String, List<String>> queryParams = Uri.parse('?${requestList[1]}').queryParametersAll;
52+
var rqEvents = jsonDecode(queryParams['events']![0]);
53+
expect(rqEvents.length, Platform.isAndroid ? 3 : 4);
54+
int index = 0;
55+
if(Platform.isAndroid){
56+
validateEvent("[CLY]_orientation", <String, dynamic>{'mode': 'portrait'}, eventGiven: rqEvents[index++]);
57+
}
58+
validateView("V1", true, true, viewGiven: rqEvents[index++]);
59+
validateView("V2", false, true, viewGiven: rqEvents[index++]);
60+
if(Platform.isIOS){
61+
validateView("V2", false, false, viewGiven: rqEvents[index++]);
62+
validateView("V1", false, false, viewGiven: rqEvents[index++]);
63+
}
64+
65+
validateEndSessionRequest(requestList[2]); // validate end session on 2nd idx
66+
if(Platform.isAndroid) {
67+
validateBeginSessionRequest(requestList[3]); // validate begin session on 3rd idx
68+
}
69+
70+
index = 0;
71+
if (Platform.isAndroid) {
72+
try {
73+
validateView("V1", false, false, viewStr: eventList[index++]);
74+
validateView("V2", false, false, viewStr: eventList[index++]);
75+
} catch (e) {
76+
index = 0;
77+
validateView("V2", false, false, viewStr: eventList[index++]);
78+
validateView("V1", false, false, viewStr: eventList[index++]);
79+
}
80+
}
81+
82+
validateView("V4", Platform.isAndroid ? true : false, true, viewStr: eventList[index++]); //TODO: check this
83+
validateView("V4", false, false, viewStr: eventList[index++]);
84+
validateView("V3", false, true, viewStr: eventList[index++]);
85+
if(Platform.isAndroid){
86+
validateEvent("[CLY]_orientation", <String, dynamic>{'mode': 'portrait'}, eventStr: eventList[index++]);
87+
}
88+
89+
int iCached = index;
90+
if (Platform.isAndroid) {
91+
try {
92+
validateView("V2", false, true, viewStr: eventList[index++]);
93+
validateView("V2", false, false, viewStr: eventList[index++]);
94+
validateView("V1", false, true, viewStr: eventList[index++]);
95+
} catch (e) {
96+
index = iCached;
97+
validateView("V1", false, true, viewStr: eventList[index++]);
98+
validateView("V2", false, true, viewStr: eventList[index++]);
99+
}
100+
}
101+
});
102+
}
103+
104+
void validateView(String name, bool start, bool visit, {String? viewStr, Map<String, dynamic>? viewGiven}) {
105+
Map<String, dynamic> segmentation = <String, dynamic>{'name': name, 'segment': Platform.isAndroid ? 'Android' : 'iOS'};
106+
107+
if (visit) {
108+
segmentation['visit'] = Platform.isAndroid ? '1': 1;
109+
}
110+
if (start) {
111+
segmentation['start'] = Platform.isAndroid ? '1': 1;
112+
}
113+
validateEvent("[CLY]_view", segmentation, eventGiven: viewGiven, eventStr: viewStr);
114+
}
115+
116+
void validateBeginSessionRequest(String request) {
117+
Map<String, List<String>> queryParams = Uri.parse('?$request').queryParametersAll;
118+
testCommonRequestParams(queryParams);
119+
120+
expect(queryParams['begin_session'], ['1']);
121+
expect(queryParams['metrics'], isNotNull);
122+
}
123+
124+
void validateEndSessionRequest(String request) {
125+
Map<String, List<String>> queryParams = Uri.parse('?$request').queryParametersAll;
126+
testCommonRequestParams(queryParams);
127+
128+
expect(queryParams['end_session'], ['1']);
129+
expect(queryParams['metrics'], isNull);
130+
}
131+
132+
void validateEvent(String key, Map<String, dynamic> segmentation, {String? eventStr, Map<String, dynamic>? eventGiven}) {
133+
Map<String, dynamic> event = eventStr != null ? jsonDecode(eventStr) : eventGiven!;
134+
print("================");
135+
print(event);
136+
expect(event['key'], key);
137+
expect(segmentation.length, event['segmentation'].length);
138+
for (var key in segmentation.keys) {
139+
expect(event['segmentation'][key], segmentation[key]);
140+
}
141+
}

0 commit comments

Comments
 (0)