Skip to content

Commit 9bfb3dd

Browse files
feat: test
1 parent 4a6bc0b commit 9bfb3dd

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import 'dart:convert';
2+
import 'dart:io';
3+
4+
import 'package:countly_flutter/countly_flutter.dart';
5+
import 'package:flutter/foundation.dart';
6+
import 'package:flutter_test/flutter_test.dart';
7+
import 'package:integration_test/integration_test.dart';
8+
9+
import '../utils.dart';
10+
import '../views_tests/auto_view_flow2_test.dart';
11+
12+
/// Tests for recording custom metrics using the Countly SDK.
13+
/// Verifies that metrics are correctly sent in the network request.
14+
/// Covers scenarios with normal key-value pairs and edge cases like empty keys/values.
15+
void main() {
16+
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
17+
testWidgets('recordMetrics_test', (WidgetTester tester) async {
18+
CountlyConfig config = CountlyConfig(SERVER_URL, APP_KEY).setLoggingEnabled(true);
19+
await Countly.initWithConfig(config);
20+
await Future.delayed(const Duration(seconds: 2));
21+
22+
// Add custom headers
23+
await Countly.instance.recordMetrics({'metric1': '100', 'metric2': '200', '_device': 'custom_device', '': 'empty_key', 'empty_value': ''});
24+
25+
var requestQueue = await getRequestQueue();
26+
expect(requestQueue.length, 2);
27+
validateBeginSessionRequest(requestQueue[0]);
28+
29+
Map<String, List<String>> queryParams = Uri.parse('?${requestQueue[1]}').queryParametersAll;
30+
var rqMetrics = jsonDecode(queryParams['metrics']![0]);
31+
Map<String, dynamic> customMetrics = {
32+
'metric1': '100',
33+
'metric2': '200',
34+
'_device': 'custom_device',
35+
'': 'empty_key',
36+
'empty_value': '',
37+
};
38+
39+
if(Platform.isAndroid) {
40+
// Android SDK ignores empty key metrics
41+
customMetrics.remove('');
42+
}
43+
44+
validateMetrics(rqMetrics, customMetrics);
45+
});
46+
}
47+
48+
const _androidMetricKeys = {
49+
'_os',
50+
'_os_version',
51+
'_app_version',
52+
'_device',
53+
'_device_type',
54+
'_resolution',
55+
'_density',
56+
'_locale',
57+
'_manufacturer',
58+
'_carrier',
59+
'_has_hinge',
60+
};
61+
62+
const _iosMetricKeys = {'_os', '_os_version', '_app_version', '_device', '_device_type', '_resolution', '_density', '_locale'};
63+
64+
const _webMetricKeys = {
65+
'_os',
66+
'_os_version',
67+
'_app_version',
68+
'_device',
69+
'_locale',
70+
'_browser',
71+
'_browser_version',
72+
'_ua',
73+
};
74+
75+
void validateMetrics(Map<String, dynamic> metrics, Map<String, dynamic>? customMetrics) {
76+
late Set<String> expectedKeys;
77+
78+
if (kIsWeb) {
79+
expectedKeys = _webMetricKeys;
80+
} else if (Platform.isAndroid) {
81+
expectedKeys = _androidMetricKeys;
82+
} else if (Platform.isIOS) {
83+
expectedKeys = _iosMetricKeys;
84+
} else {
85+
throw UnsupportedError('Unknown platform in metric validation');
86+
}
87+
88+
expectedKeys = expectedKeys.union(customMetrics?.keys.toSet() ?? {});
89+
90+
expect(metrics.length, equals(expectedKeys.length), reason: 'Metric key count mismatch');
91+
92+
for (final key in expectedKeys) {
93+
if (customMetrics != null && customMetrics.containsKey(key)) {
94+
expect(metrics[key], customMetrics[key]);
95+
} else {
96+
expect(
97+
metrics.containsKey(key),
98+
true,
99+
reason: 'Missing metric key: $key',
100+
);
101+
}
102+
}
103+
104+
for (final entry in (customMetrics ?? {}).entries) {
105+
expect(
106+
metrics[entry.key],
107+
entry.value,
108+
reason: 'Custom metric key-value mismatch for key: ${entry.key}',
109+
);
110+
}
111+
}

0 commit comments

Comments
 (0)