|
| 1 | +import 'dart:convert'; |
| 2 | +import 'dart:html'; |
| 3 | +import 'package:countly_flutter/countly_flutter.dart'; |
| 4 | +import 'package:flutter_test/flutter_test.dart'; |
| 5 | +import 'package:integration_test/integration_test.dart'; |
| 6 | + |
| 7 | +import 'utils.dart'; |
| 8 | + |
| 9 | +/// Check if we can get stored queues from native side |
| 10 | +void main() { |
| 11 | + IntegrationTestWidgetsFlutterBinding.ensureInitialized(); |
| 12 | + |
| 13 | + group("Device ID change tests", () { |
| 14 | + tearDown(() async { |
| 15 | + await Countly.instance.halt(); |
| 16 | + window.localStorage.clear(); |
| 17 | + }); |
| 18 | + test("Check init time temp mode with setID", () async { |
| 19 | + CountlyConfig config = CountlyConfig(SERVER_URL, APP_KEY).setLoggingEnabled(true).enableTemporaryDeviceIDMode(); |
| 20 | + await Countly.initWithConfig(config); |
| 21 | + |
| 22 | + List<String> requestQueue = await getRequestQueue(); |
| 23 | + List<String> eventQueue = await getEventQueue(); |
| 24 | + |
| 25 | + expect(1, requestQueue.length); |
| 26 | + expect(1, eventQueue.length); |
| 27 | + |
| 28 | + dynamic request = jsonDecode(requestQueue[0]); |
| 29 | + expect("[CLY]_temp_id", request["device_id"]); |
| 30 | + |
| 31 | + await Countly.recordEvent({"key": "1"}); |
| 32 | + await Countly.instance.userProfile.setUserProperties({"name": "name"}); |
| 33 | + |
| 34 | + await Countly.instance.deviceId.setID("new ID"); |
| 35 | + |
| 36 | + await Countly.recordEvent({"key": "2"}); |
| 37 | + |
| 38 | + requestQueue = await getRequestQueue(); |
| 39 | + eventQueue = await getEventQueue(); |
| 40 | + |
| 41 | + expect(4, requestQueue.length); |
| 42 | + expect(1, eventQueue.length); |
| 43 | + |
| 44 | + // observe that all requests got new ID |
| 45 | + for (var request in requestQueue) { |
| 46 | + dynamic req = jsonDecode(request); |
| 47 | + expect("new ID", req["device_id"]); |
| 48 | + } |
| 49 | + }); |
| 50 | + |
| 51 | + test("Check init time temp mode with changeWithoutMerge", () async { |
| 52 | + CountlyConfig config = CountlyConfig(SERVER_URL, APP_KEY).setLoggingEnabled(true).enableTemporaryDeviceIDMode(); |
| 53 | + await Countly.initWithConfig(config); |
| 54 | + |
| 55 | + List<String> requestQueue = await getRequestQueue(); |
| 56 | + List<String> eventQueue = await getEventQueue(); |
| 57 | + |
| 58 | + expect(1, requestQueue.length); |
| 59 | + expect(0, eventQueue.length); |
| 60 | + |
| 61 | + dynamic request = jsonDecode(requestQueue[0]); |
| 62 | + expect("[CLY]_temp_id", request["device_id"]); |
| 63 | + |
| 64 | + await Countly.recordEvent({"key": "1"}); |
| 65 | + await Countly.instance.userProfile.setUserProperties({"name": "name"}); |
| 66 | + |
| 67 | + await Countly.instance.deviceId.changeWithoutMerge("new ID"); |
| 68 | + |
| 69 | + await Countly.recordEvent({"key": "2"}); |
| 70 | + |
| 71 | + requestQueue = await getRequestQueue(); |
| 72 | + eventQueue = await getEventQueue(); |
| 73 | + |
| 74 | + expect(4, requestQueue.length); |
| 75 | + expect(1, eventQueue.length); |
| 76 | + |
| 77 | + // observe that all requests got new ID |
| 78 | + for (var request in requestQueue) { |
| 79 | + dynamic req = jsonDecode(request); |
| 80 | + expect("new ID", req["device_id"]); |
| 81 | + } |
| 82 | + }); |
| 83 | + |
| 84 | + test("Check init time temp mode with changeWithMerge", () async { |
| 85 | + CountlyConfig config = CountlyConfig(SERVER_URL, APP_KEY).setLoggingEnabled(true).enableTemporaryDeviceIDMode(); |
| 86 | + await Countly.initWithConfig(config); |
| 87 | + |
| 88 | + List<String> requestQueue = await getRequestQueue(); |
| 89 | + List<String> eventQueue = await getEventQueue(); |
| 90 | + |
| 91 | + expect(1, requestQueue.length); |
| 92 | + expect(0, eventQueue.length); |
| 93 | + |
| 94 | + dynamic request = jsonDecode(requestQueue[0]); |
| 95 | + expect("[CLY]_temp_id", request["device_id"]); |
| 96 | + |
| 97 | + await Countly.recordEvent({"key": "1"}); |
| 98 | + await Countly.instance.userProfile.setUserProperties({"name": "name"}); |
| 99 | + |
| 100 | + await Countly.instance.deviceId.changeWithMerge("new ID"); |
| 101 | + |
| 102 | + await Countly.recordEvent({"key": "2"}); |
| 103 | + |
| 104 | + requestQueue = await getRequestQueue(); |
| 105 | + eventQueue = await getEventQueue(); |
| 106 | + |
| 107 | + expect(4, requestQueue.length); |
| 108 | + expect(1, eventQueue.length); |
| 109 | + |
| 110 | + // observe that all requests got new ID |
| 111 | + for (var request in requestQueue) { |
| 112 | + dynamic req = jsonDecode(request); |
| 113 | + expect("new ID", req["device_id"]); |
| 114 | + } |
| 115 | + }); |
| 116 | + }); |
| 117 | +} |
0 commit comments