Skip to content

Commit

Permalink
Merge pull request #313 from Countly/new_test
Browse files Browse the repository at this point in the history
New test for start param
  • Loading branch information
turtledreams authored Dec 3, 2024
2 parents 9dce0ea + 0b2065e commit 5e5a57b
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ void main() {
validateView("V1", true, true, viewGiven: rqEvents[index++]);
validateView("V2", false, true, viewGiven: rqEvents[index++]);
if(Platform.isIOS){
validateView("V2", false, false, viewGiven: rqEvents[index++]);
validateView("V1", false, false, viewGiven: rqEvents[index++]);
validateView("V2", false, false, viewGiven: rqEvents[index++]);
}

validateEndSessionRequest(requestList[2]); // validate end session on 2nd idx
Expand All @@ -79,7 +79,7 @@ void main() {
}
}

validateView("V4", Platform.isAndroid ? true : false, true, viewStr: eventList[index++]); //TODO: check this
validateView("V4", false, true, viewStr: eventList[index++]);
validateView("V4", false, false, viewStr: eventList[index++]);
validateView("V3", false, true, viewStr: eventList[index++]);
if(Platform.isAndroid){
Expand All @@ -89,12 +89,12 @@ void main() {
int iCached = index;
if (Platform.isAndroid) {
try {
validateView("V2", false, true, viewStr: eventList[index++]);
validateView("V2", true, true, viewStr: eventList[index++]);
validateView("V2", false, false, viewStr: eventList[index++]);
validateView("V1", false, true, viewStr: eventList[index++]);
} catch (e) {
index = iCached;
validateView("V1", false, true, viewStr: eventList[index++]);
validateView("V1", true, true, viewStr: eventList[index++]);
validateView("V2", false, true, viewStr: eventList[index++]);
}
}
Expand Down
98 changes: 98 additions & 0 deletions example/integration_test/views_tests/auto_view_flow2_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import 'dart:convert';
import 'dart:math';
import 'package:flutter_foreground_task/flutter_foreground_task.dart';
import 'dart:io';

import 'package:countly_flutter/countly_flutter.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import '../utils.dart';

///
/// This test is to check the start parameter is only added to the first views of a session
///
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
testWidgets('Start auto stopped views and stop views start parameter', (WidgetTester tester) async {
// Initialize the SDK
CountlyConfig config = CountlyConfig(SERVER_URL, APP_KEY);
config.enableManualSessionHandling();
await Countly.initWithConfig(config);

await Countly.instance.views.startAutoStoppedView("V1");
await Countly.instance.sessions.beginSession();
sleep(Duration(seconds: 2));
await Countly.instance.views.startAutoStoppedView("V2");
await Countly.instance.sessions.endSession();
await Countly.instance.views.startView("V3");
await Countly.instance.sessions.beginSession();
await Countly.instance.views.startAutoStoppedView("V4");

List<String> requestList = await getRequestQueue();
List<String> eventList = await getEventQueue();
Map<String, List<String>> queryParams = Uri.parse('?${requestList[1]}').queryParametersAll;
var rqEvents = jsonDecode(queryParams['events']![0]);

printQueues(requestList, eventList);
expect(4, requestList.length);
expect(Platform.isAndroid ? 4 : 3, eventList.length);
expect(4, rqEvents.length);

validateBeginSessionRequest(requestList[0]); // validate begin session on 0th idx
validateEndSessionRequest(requestList[2], 2); // validate end session on 1st idx
validateBeginSessionRequest(requestList[3]); // validate begin session on 2nd idx

int index = 0;
validateView("V1", false, true, viewGiven: rqEvents[index++]); // begin session not called
validateEvent("[CLY]_orientation", <String, dynamic>{'mode': 'portrait'}, eventGiven: rqEvents[index++]);
validateView("V1", false, false, viewGiven: rqEvents[index++]); // it is because auto view
validateView("V2", true, true, viewGiven: rqEvents[index++]); // after begin session called
index = 0;
validateView("V2", false, false, viewStr: eventList[index++]); // it is because auto view
validateView("V3", false, true, viewStr: eventList[index++]); // begin session not called
if(Platform.isAndroid){
validateEvent("[CLY]_orientation", <String, dynamic>{'mode': 'portrait'}, eventStr: eventList[index++]);
}
validateView("V4", true, true, viewStr: eventList[index++]); // after begin session called
});
}

void validateView(String name, bool start, bool visit, {String? viewStr, Map<String, dynamic>? viewGiven}) {
Map<String, dynamic> segmentation = <String, dynamic>{'name': name, 'segment': Platform.isAndroid ? 'Android' : 'iOS'};

if (visit) {
segmentation['visit'] = Platform.isAndroid ? '1': 1;
}
if (start) {
segmentation['start'] = Platform.isAndroid ? '1': 1;
}
validateEvent("[CLY]_view", segmentation, eventGiven: viewGiven, eventStr: viewStr);
}

void validateBeginSessionRequest(String request) {
Map<String, List<String>> queryParams = Uri.parse('?$request').queryParametersAll;
testCommonRequestParams(queryParams);

expect(queryParams['begin_session'], ['1']);
expect(queryParams['metrics'], isNotNull);
}

void validateEndSessionRequest(String request, int duration) {
Map<String, List<String>> queryParams = Uri.parse('?$request').queryParametersAll;
testCommonRequestParams(queryParams);

expect(queryParams['end_session'], ['1']);
expect(queryParams['metrics'], isNull);
expect(queryParams['session_duration'], [duration.toString()]);
}

void validateEvent(String key, Map<String, dynamic> segmentation, {String? eventStr, Map<String, dynamic>? eventGiven}) {
Map<String, dynamic> event = eventStr != null ? jsonDecode(eventStr) : eventGiven!;
print("================");
print(event);
expect(event['key'], key);
expect(segmentation.length, event['segmentation'].length);
for (var key in segmentation.keys) {
expect(event['segmentation'][key], segmentation[key]);
}
}

0 comments on commit 5e5a57b

Please sign in to comment.