Skip to content

Commit f58e68a

Browse files
authored
feat: add setMocks method for use in testing (g123k#73)
1 parent 33cbee6 commit f58e68a

File tree

1 file changed

+44
-3
lines changed

1 file changed

+44
-3
lines changed

lib/flutter_app_badger.dart

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,62 @@
11
import 'dart:async';
22

3+
import 'package:flutter/foundation.dart';
34
import 'package:flutter/services.dart';
45

56
class FlutterAppBadger {
67
static const MethodChannel _channel =
78
const MethodChannel('g123k/flutter_app_badger');
89

9-
static Future<void> updateBadgeCount(int count) {
10+
static Future<void> updateBadgeCount(int count) async {
11+
final mock = _mockUpdateBadgeCount;
12+
if (mock != null) {
13+
await mock(count);
14+
return;
15+
}
16+
1017
return _channel.invokeMethod('updateBadgeCount', {"count": count});
1118
}
1219

13-
static Future<void> removeBadge() {
20+
static Future<void> removeBadge() async {
21+
final mock = _mockRemoveBadge;
22+
if (mock != null) {
23+
await mock();
24+
return;
25+
}
26+
1427
return _channel.invokeMethod('removeBadge');
1528
}
1629

1730
static Future<bool> isAppBadgeSupported() async {
18-
bool? appBadgeSupported = await _channel.invokeMethod('isAppBadgeSupported');
31+
final mock = _mockIsAppBadgeSupported;
32+
if (mock != null) {
33+
return mock();
34+
}
35+
36+
bool? appBadgeSupported =
37+
await _channel.invokeMethod('isAppBadgeSupported');
1938
return appBadgeSupported ?? false;
2039
}
40+
41+
static Future<void> Function(int count)? _mockUpdateBadgeCount;
42+
static Future<void> Function()? _mockRemoveBadge;
43+
static Future<bool> Function()? _mockIsAppBadgeSupported;
44+
45+
@visibleForTesting
46+
static void setMocks({
47+
Future<void> Function(int count)? updateBadgeCount,
48+
Future<void> Function()? removeBadge,
49+
Future<bool> Function()? isAppBadgeSupported,
50+
}) {
51+
_mockUpdateBadgeCount = updateBadgeCount;
52+
_mockRemoveBadge = removeBadge;
53+
_mockIsAppBadgeSupported = isAppBadgeSupported;
54+
}
55+
56+
@visibleForTesting
57+
static void clearMocks() {
58+
_mockUpdateBadgeCount = null;
59+
_mockRemoveBadge = null;
60+
_mockIsAppBadgeSupported = null;
61+
}
2162
}

0 commit comments

Comments
 (0)