|
1 | 1 | import 'dart:async';
|
2 | 2 |
|
| 3 | +import 'package:flutter/foundation.dart'; |
3 | 4 | import 'package:flutter/services.dart';
|
4 | 5 |
|
5 | 6 | class FlutterAppBadger {
|
6 | 7 | static const MethodChannel _channel =
|
7 | 8 | const MethodChannel('g123k/flutter_app_badger');
|
8 | 9 |
|
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 | + |
10 | 17 | return _channel.invokeMethod('updateBadgeCount', {"count": count});
|
11 | 18 | }
|
12 | 19 |
|
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 | + |
14 | 27 | return _channel.invokeMethod('removeBadge');
|
15 | 28 | }
|
16 | 29 |
|
17 | 30 | 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'); |
19 | 38 | return appBadgeSupported ?? false;
|
20 | 39 | }
|
| 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 | + } |
21 | 62 | }
|
0 commit comments