1
+ import 'package:flutter/services.dart' ;
2
+ import 'package:flutter_test/flutter_test.dart' ;
3
+ import 'package:workmanager_android/workmanager_android.dart' ;
4
+ import 'package:workmanager_platform_interface/workmanager_platform_interface.dart' ;
5
+
6
+ void main () {
7
+ TestWidgetsFlutterBinding .ensureInitialized ();
8
+
9
+ group ('WorkmanagerAndroid' , () {
10
+ late WorkmanagerAndroid workmanager;
11
+ late List <MethodCall > methodCalls;
12
+
13
+ setUp (() {
14
+ workmanager = WorkmanagerAndroid ();
15
+ methodCalls = < MethodCall > [];
16
+
17
+ // Mock the method channel
18
+ TestDefaultBinaryMessengerBinding .instance.defaultBinaryMessenger
19
+ .setMockMethodCallHandler (
20
+ const MethodChannel ('dev.fluttercommunity.workmanager/foreground_channel_work_manager' ),
21
+ (MethodCall methodCall) async {
22
+ methodCalls.add (methodCall);
23
+ return null ;
24
+ },
25
+ );
26
+ });
27
+
28
+ tearDown (() {
29
+ TestDefaultBinaryMessengerBinding .instance.defaultBinaryMessenger
30
+ .setMockMethodCallHandler (
31
+ const MethodChannel ('dev.fluttercommunity.workmanager/foreground_channel_work_manager' ),
32
+ null ,
33
+ );
34
+ });
35
+
36
+ group ('registerOneOffTask' , () {
37
+ test ('should serialize all parameters correctly' , () async {
38
+ await workmanager.registerOneOffTask (
39
+ 'testTask' ,
40
+ 'testTaskName' ,
41
+ inputData: {'key' : 'value' },
42
+ initialDelay: const Duration (seconds: 30 ),
43
+ constraints: Constraints (
44
+ networkType: NetworkType .connected,
45
+ requiresCharging: true ,
46
+ requiresBatteryNotLow: false ,
47
+ requiresDeviceIdle: true ,
48
+ requiresStorageNotLow: false ,
49
+ ),
50
+ existingWorkPolicy: ExistingWorkPolicy .replace,
51
+ backoffPolicy: BackoffPolicy .exponential,
52
+ backoffPolicyDelay: const Duration (minutes: 1 ),
53
+ tag: 'testTag' ,
54
+ outOfQuotaPolicy: OutOfQuotaPolicy .run_as_non_expedited_work_request,
55
+ );
56
+
57
+ expect (methodCalls, hasLength (1 ));
58
+ expect (methodCalls.first.method, 'registerOneOffTask' );
59
+
60
+ final arguments = Map <String , dynamic >.from (methodCalls.first.arguments);
61
+ expect (arguments['uniqueName' ], 'testTask' );
62
+ expect (arguments['taskName' ], 'testTaskName' );
63
+ expect (arguments['inputData' ], {'key' : 'value' });
64
+ expect (arguments['initialDelaySeconds' ], 30 );
65
+ expect (arguments['networkType' ], 'connected' );
66
+ expect (arguments['requiresCharging' ], true );
67
+ expect (arguments['requiresBatteryNotLow' ], false );
68
+ expect (arguments['requiresDeviceIdle' ], true );
69
+ expect (arguments['requiresStorageNotLow' ], false );
70
+ expect (arguments['existingWorkPolicy' ], 'replace' );
71
+ expect (arguments['backoffPolicy' ], 'exponential' );
72
+ expect (arguments['backoffDelayInMilliseconds' ], 60000 );
73
+ expect (arguments['tag' ], 'testTag' );
74
+ expect (arguments['outOfQuotaPolicy' ], 'run_as_non_expedited_work_request' );
75
+ });
76
+
77
+ test ('should handle null optional parameters' , () async {
78
+ await workmanager.registerOneOffTask ('testTask' , 'testTaskName' );
79
+
80
+ expect (methodCalls, hasLength (1 ));
81
+ final arguments = Map <String , dynamic >.from (methodCalls.first.arguments);
82
+ expect (arguments['inputData' ], null );
83
+ expect (arguments['initialDelaySeconds' ], null );
84
+ expect (arguments['networkType' ], null );
85
+ expect (arguments['requiresCharging' ], null );
86
+ expect (arguments['requiresBatteryNotLow' ], null );
87
+ expect (arguments['requiresDeviceIdle' ], null );
88
+ expect (arguments['requiresStorageNotLow' ], null );
89
+ expect (arguments['existingWorkPolicy' ], null );
90
+ expect (arguments['backoffPolicy' ], null );
91
+ expect (arguments['backoffDelayInMilliseconds' ], null );
92
+ expect (arguments['tag' ], null );
93
+ expect (arguments['outOfQuotaPolicy' ], null );
94
+ });
95
+ });
96
+
97
+ group ('registerPeriodicTask' , () {
98
+ test ('should serialize all parameters correctly' , () async {
99
+ await workmanager.registerPeriodicTask (
100
+ 'periodicTask' ,
101
+ 'periodicTaskName' ,
102
+ frequency: const Duration (hours: 1 ),
103
+ flexInterval: const Duration (minutes: 15 ),
104
+ inputData: {'periodic' : 'data' },
105
+ initialDelay: const Duration (minutes: 5 ),
106
+ constraints: Constraints (networkType: NetworkType .unmetered),
107
+ existingWorkPolicy: ExistingWorkPolicy .keep,
108
+ backoffPolicy: BackoffPolicy .linear,
109
+ backoffPolicyDelay: const Duration (seconds: 30 ),
110
+ tag: 'periodicTag' ,
111
+ );
112
+
113
+ expect (methodCalls, hasLength (1 ));
114
+ expect (methodCalls.first.method, 'registerPeriodicTask' );
115
+
116
+ final arguments = Map <String , dynamic >.from (methodCalls.first.arguments);
117
+ expect (arguments['uniqueName' ], 'periodicTask' );
118
+ expect (arguments['taskName' ], 'periodicTaskName' );
119
+ expect (arguments['frequencySeconds' ], 3600 );
120
+ expect (arguments['flexIntervalSeconds' ], 900 );
121
+ expect (arguments['inputData' ], {'periodic' : 'data' });
122
+ expect (arguments['initialDelaySeconds' ], 300 );
123
+ expect (arguments['networkType' ], 'unmetered' );
124
+ expect (arguments['existingWorkPolicy' ], 'keep' );
125
+ expect (arguments['backoffPolicy' ], 'linear' );
126
+ expect (arguments['backoffDelayInMilliseconds' ], 30000 );
127
+ expect (arguments['tag' ], 'periodicTag' );
128
+ });
129
+ });
130
+
131
+ group ('registerProcessingTask' , () {
132
+ test ('should throw UnsupportedError on Android' , () async {
133
+ expect (
134
+ () => workmanager.registerProcessingTask ('processingTask' , 'processingTaskName' ),
135
+ throwsA (isA <UnsupportedError >()),
136
+ );
137
+ });
138
+ });
139
+
140
+ group ('cancelByUniqueName' , () {
141
+ test ('should call correct method with parameters' , () async {
142
+ await workmanager.cancelByUniqueName ('testTask' );
143
+
144
+ expect (methodCalls, hasLength (1 ));
145
+ expect (methodCalls.first.method, 'cancelTaskByUniqueName' );
146
+ expect (methodCalls.first.arguments, {'uniqueName' : 'testTask' });
147
+ });
148
+ });
149
+
150
+ group ('cancelByTag' , () {
151
+ test ('should call correct method with parameters' , () async {
152
+ await workmanager.cancelByTag ('testTag' );
153
+
154
+ expect (methodCalls, hasLength (1 ));
155
+ expect (methodCalls.first.method, 'cancelTaskByTag' );
156
+ expect (methodCalls.first.arguments, {'tag' : 'testTag' });
157
+ });
158
+ });
159
+
160
+ group ('cancelAll' , () {
161
+ test ('should call correct method' , () async {
162
+ await workmanager.cancelAll ();
163
+
164
+ expect (methodCalls, hasLength (1 ));
165
+ expect (methodCalls.first.method, 'cancelAllTasks' );
166
+ expect (methodCalls.first.arguments, null );
167
+ });
168
+ });
169
+
170
+ group ('isScheduledByUniqueName' , () {
171
+ test ('should return result from method channel' , () async {
172
+ TestDefaultBinaryMessengerBinding .instance.defaultBinaryMessenger
173
+ .setMockMethodCallHandler (
174
+ const MethodChannel ('dev.fluttercommunity.workmanager/foreground_channel_work_manager' ),
175
+ (MethodCall methodCall) async {
176
+ if (methodCall.method == 'isScheduledByUniqueName' ) {
177
+ return true ;
178
+ }
179
+ return null ;
180
+ },
181
+ );
182
+
183
+ final result = await workmanager.isScheduledByUniqueName ('testTask' );
184
+
185
+ expect (result, true );
186
+ });
187
+
188
+ test ('should return false when method channel returns null' , () async {
189
+ TestDefaultBinaryMessengerBinding .instance.defaultBinaryMessenger
190
+ .setMockMethodCallHandler (
191
+ const MethodChannel ('dev.fluttercommunity.workmanager/foreground_channel_work_manager' ),
192
+ (MethodCall methodCall) async => null ,
193
+ );
194
+
195
+ final result = await workmanager.isScheduledByUniqueName ('testTask' );
196
+
197
+ expect (result, false );
198
+ });
199
+ });
200
+
201
+ group ('printScheduledTasks' , () {
202
+ test ('should throw UnsupportedError on Android' , () async {
203
+ expect (
204
+ () => workmanager.printScheduledTasks (),
205
+ throwsA (isA <UnsupportedError >()),
206
+ );
207
+ });
208
+ });
209
+ });
210
+ }
0 commit comments