Skip to content

Commit

Permalink
#6 - New tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tomalabaster committed Dec 30, 2023
1 parent 6aa3330 commit 0faf312
Show file tree
Hide file tree
Showing 3 changed files with 995 additions and 10 deletions.
1 change: 1 addition & 0 deletions example/lib/app/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class MyApp extends StatelessWidget {
enabled: enabled,
backgroundLockLatency: backgroundLockLatency,
inactiveBuilder: (context) => const Scaffold(
key: Key('InactiveScreen'),
body: Center(
child: FlutterLogo(size: 80),
),
Expand Down
127 changes: 117 additions & 10 deletions example/test/main_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:integration_test/integration_test.dart';

final myHomePage = find.byKey(const Key('MyHomePage'));
final lockScreen = find.byKey(const Key('LockScreen'));
final inactiveScreen = find.byKey(const Key('InactiveScreen'));
final showButton = find.byKey(const Key('ShowButton'));
final passwordField = find.byKey(const Key('PasswordField'));
final unlockButton = find.byKey(const Key('UnlockButton'));
Expand Down Expand Up @@ -53,6 +54,25 @@ Future<void> enterBackgroundForDuration(
await tester.pumpAndSettle();
}

Future<void> becomeInactiveForDuration(
WidgetTester tester, Duration duration) async {
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive);

if (tester.binding is IntegrationTestWidgetsFlutterBinding) {
await Future.delayed(duration);
} else {
await tester.pumpAndSettle(duration);
}

await tester.pumpAndSettle();
}

Future<void> becomeResumed(WidgetTester tester) async {
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);

await tester.pumpAndSettle();
}

void main() {
group('Given an active lock screen', () {
group('When entering a correct password', () {
Expand Down Expand Up @@ -125,17 +145,50 @@ void main() {
});

group('When enabling it after launch', () {
testWidgets(
'The lock screen is shown when the app has been in background for longer than the specified duration',
(WidgetTester tester) async {
app.main(
enabled: true, backgroundLockLatency: const Duration(seconds: 1));

await enterCorrectPassword(tester);
await enableAfterLaunch(tester);
await enterBackgroundForDuration(tester, const Duration(seconds: 1));
group(
'And the app has been in the background for longer than the specified duration',
() {
testWidgets('The lock screen should be shown',
(WidgetTester tester) async {
app.main(
enabled: true, backgroundLockLatency: const Duration(seconds: 1));

await enterCorrectPassword(tester);
await enableAfterLaunch(tester);
await enterBackgroundForDuration(tester, const Duration(seconds: 1));

expect(lockScreen, findsOneWidget);
});
});

expect(lockScreen, findsOneWidget);
group('And the app becomes inactive', () {
group('And there is an inactive builder set', () {
testWidgets('The widget from the inactive builder should be shown',
(widgetTester) async {
app.main(
enabled: true,
backgroundLockLatency: const Duration(seconds: 2));

await enterCorrectPassword(widgetTester);
await enableAfterLaunch(widgetTester);
await becomeInactiveForDuration(
widgetTester, const Duration(seconds: 1));

expect(inactiveScreen, findsOne);
});

testWidgets('The lock screen should not be shown',
(widgetTester) async {
app.main(
enabled: true,
backgroundLockLatency: const Duration(seconds: 2));

await becomeInactiveForDuration(
widgetTester, const Duration(seconds: 1));

expect(lockScreen, findsNothing);
});
});
});
});

Expand All @@ -150,6 +203,34 @@ void main() {
expect(lockScreen, findsOneWidget);
});
});

group('When the app becomes inactive', () {
group('And there is an inactive builder set', () {
testWidgets('The widget from the inactive builder should not be shown',
(widgetTester) async {
app.main(
enabled: false,
backgroundLockLatency: const Duration(seconds: 2));

await becomeInactiveForDuration(
widgetTester, const Duration(seconds: 1));

expect(inactiveScreen, findsNothing);
});

testWidgets('The lock screen should not be shown',
(widgetTester) async {
app.main(
enabled: false,
backgroundLockLatency: const Duration(seconds: 2));

await becomeInactiveForDuration(
widgetTester, const Duration(seconds: 1));

expect(lockScreen, findsNothing);
});
});
});
});

group('Given an app with AppLock enabled', () {
Expand Down Expand Up @@ -215,5 +296,31 @@ void main() {
expect(lockScreen, findsOneWidget);
});
});

group('When the app becomes inactive', () {
group('And there is an inactive builder set', () {
testWidgets('The widget from the inactive builder should not be shown',
(widgetTester) async {
app.main(
enabled: true, backgroundLockLatency: const Duration(seconds: 2));

await becomeInactiveForDuration(
widgetTester, const Duration(seconds: 1));

expect(inactiveScreen, findsNothing);
});

testWidgets('The lock screen should still be shown',
(widgetTester) async {
app.main(
enabled: true, backgroundLockLatency: const Duration(seconds: 2));

await becomeInactiveForDuration(
widgetTester, const Duration(seconds: 1));

expect(lockScreen, findsOneWidget);
});
});
});
});
}
Loading

0 comments on commit 0faf312

Please sign in to comment.