Skip to content

Commit

Permalink
test: Improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Dev-hwang committed Sep 4, 2024
1 parent 7619520 commit e1535ff
Show file tree
Hide file tree
Showing 5 changed files with 693 additions and 355 deletions.
76 changes: 42 additions & 34 deletions lib/flutter_foreground_task.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ class FlutterForegroundTask {
@visibleForTesting
static bool isInitialized = false;

@visibleForTesting
static bool skipServiceResponseCheck = false;

// platform instance: MethodChannelFlutterForegroundTask
static FlutterForegroundTaskPlatform get _platform =>
FlutterForegroundTaskPlatform.instance;
Expand All @@ -69,6 +72,7 @@ class FlutterForegroundTask {
iosNotificationOptions = null;
foregroundTaskOptions = null;
isInitialized = false;
skipServiceResponseCheck = false;

receivePort?.close();
receivePort = null;
Expand Down Expand Up @@ -120,24 +124,26 @@ class FlutterForegroundTask {
callback: callback,
);

final Stopwatch stopwatch = Stopwatch()..start();
bool isStarted = false;
await Future.doWhile(() async {
isStarted = await isRunningService;

// official doc: Once the service has been created, the service must call its startForeground() method within five seconds.
// ref: https://developer.android.com/guide/components/services#StartingAService
if (isStarted || stopwatch.elapsedMilliseconds > 5 * 1000) {
return false;
} else {
await Future.delayed(const Duration(milliseconds: 100));
return true;
if (!skipServiceResponseCheck) {
final Stopwatch stopwatch = Stopwatch()..start();
bool isStarted = false;
await Future.doWhile(() async {
isStarted = await isRunningService;

// official doc: Once the service has been created, the service must call its startForeground() method within five seconds.
// ref: https://developer.android.com/guide/components/services#StartingAService
if (isStarted || stopwatch.elapsedMilliseconds > 5 * 1000) {
return false;
} else {
await Future.delayed(const Duration(milliseconds: 100));
return true;
}
});

// no response :(
if (!isStarted) {
throw ServiceTimeoutException();
}
});

// no response :(
if (!isStarted) {
throw ServiceTimeoutException();
}

return ServiceRequestResult.success();
Expand Down Expand Up @@ -199,24 +205,26 @@ class FlutterForegroundTask {

await _platform.stopService();

final Stopwatch stopwatch = Stopwatch()..start();
bool isStopped = false;
await Future.doWhile(() async {
isStopped = !(await isRunningService);

// official doc: Once the service has been created, the service must call its startForeground() method within five seconds.
// ref: https://developer.android.com/guide/components/services#StartingAService
if (isStopped || stopwatch.elapsedMilliseconds > 5 * 1000) {
return false;
} else {
await Future.delayed(const Duration(milliseconds: 100));
return true;
if (!skipServiceResponseCheck) {
final Stopwatch stopwatch = Stopwatch()..start();
bool isStopped = false;
await Future.doWhile(() async {
isStopped = !(await isRunningService);

// official doc: Once the service has been created, the service must call its startForeground() method within five seconds.
// ref: https://developer.android.com/guide/components/services#StartingAService
if (isStopped || stopwatch.elapsedMilliseconds > 5 * 1000) {
return false;
} else {
await Future.delayed(const Duration(milliseconds: 100));
return true;
}
});

// no response :(
if (!isStopped) {
throw ServiceTimeoutException();
}
});

// no response :(
if (!isStopped) {
throw ServiceTimeoutException();
}

return ServiceRequestResult.success();
Expand Down
37 changes: 37 additions & 0 deletions test/dummy/service_dummy_data.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter_foreground_task/flutter_foreground_task.dart';
import 'package:platform/platform.dart';

@pragma('vm:entry-point')
void testCallback() {
print('test');
}

class ServiceDummyData {
final AndroidNotificationOptions androidNotificationOptions =
Expand Down Expand Up @@ -49,4 +57,33 @@ class ServiceDummyData {
const NotificationButton(
id: 'id_test2', text: 'test2', textColor: Colors.green),
];

Map<String, dynamic> getStartServiceArgs(String platform) {
return {
'serviceId': serviceId,
if (platform == Platform.android)
...androidNotificationOptions.toJson()
else if (platform == Platform.iOS)
...iosNotificationOptions.toJson(),
...foregroundTaskOptions.toJson(),
'notificationContentTitle': notificationTitle,
'notificationContentText': notificationText,
'iconData': notificationIcon.toJson(),
'buttons': notificationButtons.map((e) => e.toJson()).toList(),
'callbackHandle':
PluginUtilities.getCallbackHandle(testCallback)?.toRawHandle(),
};
}

Map<String, dynamic> getUpdateServiceArgs() {
return {
...foregroundTaskOptions.toJson(),
'notificationContentTitle': notificationTitle,
'notificationContentText': notificationText,
'iconData': notificationIcon.toJson(),
'buttons': notificationButtons.map((e) => e.toJson()).toList(),
'callbackHandle':
PluginUtilities.getCallbackHandle(testCallback)?.toRawHandle(),
};
}
}
Loading

0 comments on commit e1535ff

Please sign in to comment.