11import 'package:flutter_test/flutter_test.dart' ;
22import 'package:integration_test/integration_test.dart' ;
33import 'package:native_workmanager/native_workmanager.dart' ;
4+ import 'dart:async' ;
45
56/// Integration test for WorkManager 2.10.0+ getForegroundInfo() bug fix
67///
@@ -23,10 +24,7 @@ void main() {
2324
2425 group ('WorkManager 2.10.0+ Compatibility Tests' , () {
2526 setUpAll (() async {
26- await NativeWorkManager .initialize (
27- isDebug: true ,
28- enableBackgroundIsolate: true ,
29- );
27+ await NativeWorkManager .initialize ();
3028 });
3129
3230 /// Test 1: OneTime expedited task (original bug scenario)
@@ -36,33 +34,29 @@ void main() {
3634 testWidgets ('OneTime expedited task should not crash' , (tester) async {
3735 await tester.pumpAndSettle ();
3836
39- final taskId = 'bug-fix-test-onetime-expedited' ;
37+ final taskId = 'bug-fix-test-onetime-expedited-${ DateTime . now (). millisecondsSinceEpoch } ' ;
4038 var taskCompleted = false ;
41- String ? taskOutput ;
39+ String ? taskMessage ;
4240
43- // Register callback
44- NativeWorkManager .onTaskCompleted ((id, output) {
45- if (id == taskId) {
46- taskCompleted = true ;
47- taskOutput = output;
41+ StreamSubscription ? eventsSub;
42+
43+ // Listen to events
44+ eventsSub = NativeWorkManager .events.listen ((event) {
45+ if (event.taskId == taskId) {
46+ taskCompleted = event.success;
47+ taskMessage = event.message;
4848 }
4949 });
5050
5151 // Schedule OneTime expedited task (triggers the bug in WM 2.10.0+)
52- await NativeWorkManager .scheduleOneTime (
52+ await NativeWorkManager .enqueue (
5353 taskId: taskId,
54- workerType: WorkerType .sync ,
55- inputData: {
56- 'url' : 'https://httpbin.org/delay/1' ,
57- 'method' : 'GET' ,
58- },
59- constraints: WorkConstraints (
60- networkType: NetworkType .connected,
61- requiresCharging: false ,
62- requiresBatteryNotLow: false ,
63- // Expedited = true triggers getForegroundInfoAsync() call
54+ trigger: TaskTrigger .oneTime (),
55+ worker: HttpRequestWorker (
56+ url: 'https://httpbin.org/delay/1' ,
57+ method: HttpMethod .get ,
6458 ),
65- // initialDelay: Duration.zero, // Execute ASAP
59+ constraints : const Constraints (requiresNetwork : true ),
6660 );
6761
6862 // Wait for task completion (max 30s)
@@ -72,11 +66,11 @@ void main() {
7266 attempts++ ;
7367 }
7468
69+ await eventsSub.cancel ();
70+
7571 // Verify task completed without crash
7672 expect (taskCompleted, true , reason: 'Task should complete without crashing' );
77- expect (taskOutput, isNotNull, reason: 'Task should return output' );
78-
79- print ('✓ OneTime expedited task completed successfully: $taskOutput ' );
73+ expect (taskMessage, isNotNull, reason: 'Task should return output' );
8074 });
8175
8276 /// Test 2: Multiple concurrent expedited tasks
@@ -85,27 +79,28 @@ void main() {
8579 testWidgets ('Multiple concurrent expedited tasks should not crash' , (tester) async {
8680 await tester.pumpAndSettle ();
8781
88- final taskIds = List .generate (5 , (i) => 'bug-fix-test-concurrent-$i ' );
82+ final timestamp = DateTime .now ().millisecondsSinceEpoch;
83+ final taskIds = List .generate (5 , (i) => 'bug-fix-test-concurrent-$timestamp -$i ' );
8984 final completedTasks = < String > {};
9085
91- NativeWorkManager .onTaskCompleted ((id, output) {
92- if (taskIds.contains (id)) {
93- completedTasks.add (id);
86+ StreamSubscription ? eventsSub;
87+
88+ eventsSub = NativeWorkManager .events.listen ((event) {
89+ if (taskIds.contains (event.taskId) && event.success) {
90+ completedTasks.add (event.taskId);
9491 }
9592 });
9693
9794 // Schedule 5 concurrent expedited tasks
9895 for (var i = 0 ; i < taskIds.length; i++ ) {
99- await NativeWorkManager .scheduleOneTime (
96+ await NativeWorkManager .enqueue (
10097 taskId: taskIds[i],
101- workerType: WorkerType .sync ,
102- inputData: {
103- 'url' : 'https://httpbin.org/delay/${i + 1 }' ,
104- 'method' : 'GET' ,
105- },
106- constraints: WorkConstraints (
107- networkType: NetworkType .connected,
98+ trigger: TaskTrigger .oneTime (),
99+ worker: HttpRequestWorker (
100+ url: 'https://httpbin.org/delay/${i + 1 }' ,
101+ method: HttpMethod .get ,
108102 ),
103+ constraints: const Constraints (requiresNetwork: true ),
109104 );
110105 }
111106
@@ -116,10 +111,10 @@ void main() {
116111 attempts++ ;
117112 }
118113
114+ await eventsSub.cancel ();
115+
119116 expect (completedTasks.length, taskIds.length,
120117 reason: 'All concurrent tasks should complete without crashing' );
121-
122- print ('✓ ${completedTasks .length } concurrent expedited tasks completed successfully' );
123118 });
124119
125120 /// Test 3: Periodic task (should not crash even though not expedited)
@@ -128,25 +123,22 @@ void main() {
128123 testWidgets ('Periodic task should work correctly' , (tester) async {
129124 await tester.pumpAndSettle ();
130125
131- final taskId = 'bug-fix-test-periodic' ;
126+ final taskId = 'bug-fix-test-periodic-${ DateTime . now (). millisecondsSinceEpoch } ' ;
132127
133- await NativeWorkManager .schedulePeriodic (
128+ await NativeWorkManager .enqueue (
134129 taskId: taskId,
135- workerType: WorkerType .sync ,
136- interval: const Duration (minutes: 15 ),
137- inputData: {
138- 'url' : 'https://httpbin.org/get' ,
139- 'method' : 'GET' ,
140- },
130+ trigger: TaskTrigger .periodic (const Duration (minutes: 15 )),
131+ worker: HttpRequestWorker (
132+ url: 'https://httpbin.org/get' ,
133+ method: HttpMethod .get ,
134+ ),
141135 );
142136
143137 // Just verify it schedules without crash
144138 await tester.pump (const Duration (seconds: 2 ));
145139
146140 // Cancel the periodic task
147- await NativeWorkManager .cancelTask (taskId);
148-
149- print ('✓ Periodic task scheduled and cancelled successfully' );
141+ await NativeWorkManager .cancel (taskId);
150142 });
151143
152144 /// Test 4: Task chain with expedited tasks
@@ -155,55 +147,49 @@ void main() {
155147 testWidgets ('Task chain should handle expedited tasks correctly' , (tester) async {
156148 await tester.pumpAndSettle ();
157149
158- final chainId = 'bug-fix-test-chain' ;
150+ final timestamp = DateTime .now ().millisecondsSinceEpoch;
151+ final chainName = 'bug-fix-test-chain-$timestamp ' ;
152+ final task1 = 'chain-step-1-$timestamp ' ;
153+ final task2 = 'chain-step-2-$timestamp ' ;
154+ final task3 = 'chain-step-3-$timestamp ' ;
155+
159156 var chainCompleted = false ;
160157
161- NativeWorkManager .onChainCompleted ((id) {
162- if (id == chainId) {
158+ StreamSubscription ? eventsSub;
159+
160+ eventsSub = NativeWorkManager .events.listen ((event) {
161+ if (event.taskId == task3 && event.success) {
163162 chainCompleted = true ;
164163 }
165164 });
166165
167166 // Create chain with mixed task types
168- await NativeWorkManager .scheduleChain (
169- chainId: chainId,
170- steps: [
171- [
172- // Step 1: Regular sync task (expedited)
173- ChainTaskRequest (
174- workerType: WorkerType .sync ,
175- inputData: {
176- 'url' : 'https://httpbin.org/get' ,
177- 'method' : 'GET' ,
178- },
179- ),
180- ],
181- [
182- // Step 2: Heavy task (uses KmpHeavyWorker, not expedited)
183- ChainTaskRequest (
184- workerType: WorkerType .fileDownload,
185- inputData: {
186- 'url' : 'https://httpbin.org/delay/2' ,
187- 'destinationPath' : '/tmp/test-download.json' ,
188- },
189- constraints: WorkConstraints (
190- isHeavyTask: true , // This should use KmpHeavyWorker
191- ),
192- ),
193- ],
194- [
195- // Step 3: Another regular task (expedited)
196- ChainTaskRequest (
197- workerType: WorkerType .sync ,
198- inputData: {
199- 'url' : 'https://httpbin.org/post' ,
200- 'method' : 'POST' ,
201- 'body' : '{"test": "data"}' ,
202- },
203- ),
204- ],
205- ],
206- );
167+ await NativeWorkManager .beginWith (
168+ TaskRequest (
169+ id: task1,
170+ worker: HttpRequestWorker (
171+ url: 'https://httpbin.org/get' ,
172+ method: HttpMethod .get ,
173+ ),
174+ ),
175+ ).then (
176+ TaskRequest (
177+ id: task2,
178+ worker: HttpRequestWorker (
179+ url: 'https://httpbin.org/delay/2' ,
180+ method: HttpMethod .get ,
181+ ),
182+ ),
183+ ).then (
184+ TaskRequest (
185+ id: task3,
186+ worker: HttpRequestWorker (
187+ url: 'https://httpbin.org/post' ,
188+ method: HttpMethod .post,
189+ body: '{"test": "data"}' ,
190+ ),
191+ ),
192+ ).named (chainName).enqueue ();
207193
208194 // Wait for chain completion (max 60s)
209195 var attempts = 0 ;
@@ -212,9 +198,9 @@ void main() {
212198 attempts++ ;
213199 }
214200
215- expect (chainCompleted, true , reason : 'Chain should complete without crashing' );
201+ await eventsSub. cancel ( );
216202
217- print ( '✓ Task chain with mixed expedited/heavy tasks completed successfully ' );
203+ expect (chainCompleted, true , reason : 'Chain should complete without crashing ' );
218204 });
219205
220206 /// Test 5: Verify WorkManager version
@@ -227,7 +213,6 @@ void main() {
227213 // For now, we trust the build.gradle configuration
228214 // In a real test, you could use platform channels to query the version
229215
230- print ('✓ Assuming WorkManager 2.10.1+ per build.gradle configuration' );
231216 expect (true , true );
232217 });
233218 });
@@ -243,22 +228,24 @@ void main() {
243228 // For now, we just verify the task executes without crashing,
244229 // which confirms the string resource system is working
245230
246- final taskId = 'notification-i18n-test' ;
231+ final taskId = 'notification-i18n-test-${ DateTime . now (). millisecondsSinceEpoch } ' ;
247232 var taskCompleted = false ;
248233
249- NativeWorkManager .onTaskCompleted ((id, output) {
250- if (id == taskId) {
234+ StreamSubscription ? eventsSub;
235+
236+ eventsSub = NativeWorkManager .events.listen ((event) {
237+ if (event.taskId == taskId && event.success) {
251238 taskCompleted = true ;
252239 }
253240 });
254241
255- await NativeWorkManager .scheduleOneTime (
242+ await NativeWorkManager .enqueue (
256243 taskId: taskId,
257- workerType : WorkerType . sync ,
258- inputData : {
259- ' url' : 'https://httpbin.org/get' ,
260- ' method' : 'GET' ,
261- } ,
244+ trigger : TaskTrigger . oneTime () ,
245+ worker : HttpRequestWorker (
246+ url: 'https://httpbin.org/get' ,
247+ method: HttpMethod . get ,
248+ ) ,
262249 );
263250
264251 var attempts = 0 ;
@@ -267,8 +254,9 @@ void main() {
267254 attempts++ ;
268255 }
269256
257+ await eventsSub.cancel ();
258+
270259 expect (taskCompleted, true );
271- print ('✓ Task with notification i18n support completed successfully' );
272260 });
273261 });
274262}
0 commit comments