Skip to content

Commit

Permalink
Add first integration test + github workflow
Browse files Browse the repository at this point in the history
A first integration test ensures that the background task runs and finishes.
This integration test is now running on every commit on Github Actions
via an emulator.
  • Loading branch information
holybiber committed Mar 28, 2024
1 parent 4d19a67 commit 4d487ec
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 1 deletion.
26 changes: 25 additions & 1 deletion .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
flutter-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: subosito/flutter-action@v2
with:
channel: 'stable'
Expand Down Expand Up @@ -36,3 +36,27 @@ jobs:
with:
files: coverage/lcov.info
verbose: true


integration_test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: subosito/flutter-action@v2
with:
channel: 'stable'

- name: Install packages
run: flutter pub get

- name: Enable KVM
run: |
echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-kvm4all.rules
sudo udevadm control --reload-rules
sudo udevadm trigger --name-match=kvm
- name: run integration test
uses: reactivecircus/android-emulator-runner@v2
with:
api-level: 29
script: flutter test integration_test
47 changes: 47 additions & 0 deletions integration_test/background_interaction_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import 'dart:async';
import 'dart:isolate';
import 'dart:ui';

import 'package:app4training/background_task.dart';
import 'package:app4training/data/globals.dart';
import 'package:app4training/main.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:workmanager/workmanager.dart';

void main() async {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();

testWidgets('Test that background task gets executed', (tester) async {
final prefs = await SharedPreferences.getInstance();
final packageInfo = await PackageInfo.fromPlatform();

final port = ReceivePort();
expect(IsolateNameServer.registerPortWithName(port.sendPort, 'test'), true);
final completer = Completer<String>();
port.listen((data) async {
// Waiting for the background task to finish its work
completer.complete(data);
});
await Workmanager().initialize(
backgroundTask, // The top level function, aka callbackDispatcher
isInDebugMode:
false // If enabled it will post a notification whenever the task is running. Handy for debugging tasks
);
await Workmanager().registerOneOffTask("task-identifier", "simpleTask",
initialDelay: const Duration(seconds: 2));

await tester.pumpWidget(ProviderScope(overrides: [
sharedPrefsProvider.overrideWithValue(prefs),
packageInfoProvider.overrideWithValue(packageInfo)
], child: const App4Training()));
expect(find.text('Loading'), findsOneWidget);

// Wait for the background isolate to finish
final msg = await completer.future.timeout(const Duration(seconds: 10));
expect(msg, equals('success'));
});
}
4 changes: 4 additions & 0 deletions lib/background_task.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:io';
import 'dart:ui';

import 'package:app4training/data/globals.dart';
import 'package:app4training/data/languages.dart';
Expand Down Expand Up @@ -30,6 +31,9 @@ void backgroundTask() {
Workmanager().executeTask((task, inputData) async {
try {
await backgroundMain();
// For the integration test: Send a message to indicate we're finished
final sendPort = IsolateNameServer.lookupPortByName('test');
if (sendPort != null) sendPort.send('success');
} catch (e) {
await writeLog('Unexpected error while trying to run backgroundMain: $e');
}
Expand Down
39 changes: 39 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,11 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_driver:
dependency: transitive
description: flutter
source: sdk
version: "0.0.0"
flutter_html:
dependency: "direct main"
description:
Expand Down Expand Up @@ -317,6 +322,11 @@ packages:
url: "https://pub.dev"
source: hosted
version: "3.2.0"
fuchsia_remote_debug_protocol:
dependency: transitive
description: flutter
source: sdk
version: "0.0.0"
full_coverage:
dependency: "direct dev"
description:
Expand Down Expand Up @@ -373,6 +383,11 @@ packages:
url: "https://pub.dev"
source: hosted
version: "4.0.2"
integration_test:
dependency: "direct dev"
description: flutter
source: sdk
version: "0.0.0"
intl:
dependency: "direct main"
description:
Expand Down Expand Up @@ -621,6 +636,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.5.1"
process:
dependency: transitive
description:
name: process
sha256: "21e54fd2faf1b5bdd5102afd25012184a6793927648ea81eea80552ac9405b32"
url: "https://pub.dev"
source: hosted
version: "5.0.2"
pub_semver:
dependency: transitive
description:
Expand Down Expand Up @@ -842,6 +865,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.2.0"
sync_http:
dependency: transitive
description:
name: sync_http
sha256: "7f0cd72eca000d2e026bcd6f990b81d0ca06022ef4e32fb257b30d3d1014a961"
url: "https://pub.dev"
source: hosted
version: "0.3.1"
term_glyph:
dependency: transitive
description:
Expand Down Expand Up @@ -994,6 +1025,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.4.3"
webdriver:
dependency: transitive
description:
name: webdriver
sha256: "003d7da9519e1e5f329422b36c4dcdf18d7d2978d1ba099ea4e45ba490ed845e"
url: "https://pub.dev"
source: hosted
version: "3.0.3"
webkit_inspection_protocol:
dependency: transitive
description:
Expand Down
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ dev_dependencies:
full_coverage: ^1.0.0
custom_lint: ^0.6.2
riverpod_lint: ^2.0.3
integration_test:
sdk: flutter

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
Expand Down

0 comments on commit 4d487ec

Please sign in to comment.