|
| 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 start parameter is only added to the first views of a session |
| 13 | +/// |
| 14 | +void main() { |
| 15 | + IntegrationTestWidgetsFlutterBinding.ensureInitialized(); |
| 16 | + testWidgets('Start auto stopped views and stop views start parameter', (WidgetTester tester) async { |
| 17 | + // Initialize the SDK |
| 18 | + CountlyConfig config = CountlyConfig(SERVER_URL, APP_KEY); |
| 19 | + config.enableManualSessionHandling(); |
| 20 | + await Countly.initWithConfig(config); |
| 21 | + |
| 22 | + await Countly.instance.views.startAutoStoppedView("V1"); |
| 23 | + await Countly.instance.sessions.beginSession(); |
| 24 | + sleep(Duration(seconds: 2)); |
| 25 | + await Countly.instance.views.startAutoStoppedView("V2"); |
| 26 | + await Countly.instance.sessions.endSession(); |
| 27 | + await Countly.instance.views.startView("V3"); |
| 28 | + await Countly.instance.sessions.beginSession(); |
| 29 | + await Countly.instance.views.startAutoStoppedView("V4"); |
| 30 | + |
| 31 | + List<String> requestList = await getRequestQueue(); |
| 32 | + List<String> eventList = await getEventQueue(); |
| 33 | + Map<String, List<String>> queryParams = Uri.parse('?${requestList[1]}').queryParametersAll; |
| 34 | + var rqEvents = jsonDecode(queryParams['events']![0]); |
| 35 | + |
| 36 | + printQueues(requestList, eventList); |
| 37 | + expect(4, requestList.length); |
| 38 | + expect(Platform.isAndroid ? 4 : 3, eventList.length); |
| 39 | + expect(4, rqEvents.length); |
| 40 | + |
| 41 | + validateBeginSessionRequest(requestList[0]); // validate begin session on 0th idx |
| 42 | + validateEndSessionRequest(requestList[2], 2); // validate end session on 1st idx |
| 43 | + validateBeginSessionRequest(requestList[3]); // validate begin session on 2nd idx |
| 44 | + |
| 45 | + int index = 0; |
| 46 | + validateView("V1", false, true, viewGiven: rqEvents[index++]); // begin session not called |
| 47 | + if(Platform.isAndroid){ |
| 48 | + validateEvent("[CLY]_orientation", <String, dynamic>{'mode': 'portrait'}, eventGiven: rqEvents[index++]); |
| 49 | + } |
| 50 | + validateView("V1", false, false, viewGiven: rqEvents[index++]); // it is because auto view |
| 51 | + validateView("V2", true, true, viewGiven: rqEvents[index++]); // after begin session called |
| 52 | + index = 0; |
| 53 | + validateView("V2", false, false, viewStr: eventList[index++]); // it is because auto view |
| 54 | + validateView("V3", false, true, viewStr: eventList[index++]); // begin session not called |
| 55 | + if(Platform.isAndroid){ |
| 56 | + validateEvent("[CLY]_orientation", <String, dynamic>{'mode': 'portrait'}, eventStr: eventList[index++]); |
| 57 | + } |
| 58 | + validateView("V4", true, true, viewStr: eventList[index++]); // after begin session called |
| 59 | + }); |
| 60 | +} |
| 61 | + |
| 62 | +void validateView(String name, bool start, bool visit, {String? viewStr, Map<String, dynamic>? viewGiven}) { |
| 63 | + Map<String, dynamic> segmentation = <String, dynamic>{'name': name, 'segment': Platform.isAndroid ? 'Android' : 'iOS'}; |
| 64 | + |
| 65 | + if (visit) { |
| 66 | + segmentation['visit'] = Platform.isAndroid ? '1': 1; |
| 67 | + } |
| 68 | + if (start) { |
| 69 | + segmentation['start'] = Platform.isAndroid ? '1': 1; |
| 70 | + } |
| 71 | + validateEvent("[CLY]_view", segmentation, eventGiven: viewGiven, eventStr: viewStr); |
| 72 | +} |
| 73 | + |
| 74 | +void validateBeginSessionRequest(String request) { |
| 75 | + Map<String, List<String>> queryParams = Uri.parse('?$request').queryParametersAll; |
| 76 | + testCommonRequestParams(queryParams); |
| 77 | + |
| 78 | + expect(queryParams['begin_session'], ['1']); |
| 79 | + expect(queryParams['metrics'], isNotNull); |
| 80 | +} |
| 81 | + |
| 82 | +void validateEndSessionRequest(String request, int duration) { |
| 83 | + Map<String, List<String>> queryParams = Uri.parse('?$request').queryParametersAll; |
| 84 | + testCommonRequestParams(queryParams); |
| 85 | + |
| 86 | + expect(queryParams['end_session'], ['1']); |
| 87 | + expect(queryParams['metrics'], isNull); |
| 88 | + expect(queryParams['session_duration'], [duration.toString()]); |
| 89 | +} |
| 90 | + |
| 91 | +void validateEvent(String key, Map<String, dynamic> segmentation, {String? eventStr, Map<String, dynamic>? eventGiven}) { |
| 92 | + Map<String, dynamic> event = eventStr != null ? jsonDecode(eventStr) : eventGiven!; |
| 93 | + print("================"); |
| 94 | + print(event); |
| 95 | + expect(event['key'], key); |
| 96 | + expect(segmentation.length, event['segmentation'].length); |
| 97 | + for (var key in segmentation.keys) { |
| 98 | + expect(event['segmentation'][key], segmentation[key]); |
| 99 | + } |
| 100 | +} |
0 commit comments