-
-
Notifications
You must be signed in to change notification settings - Fork 932
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat!: Pause game when backgrounded #2642
Changes from all commits
c0c900f
3a49b64
a6fa4ae
653dab7
8ac9626
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,8 +9,8 @@ import 'package:flame/game.dart'; | |
import 'package:flame/src/events/flame_game_mixins/multi_tap_dispatcher.dart'; | ||
import 'package:flame/src/game/game_render_box.dart'; | ||
import 'package:flame_test/flame_test.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter/rendering.dart'; | ||
import 'package:flutter/widgets.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
import '../components/component_test.dart'; | ||
|
@@ -47,8 +47,8 @@ void main() { | |
await game.ready(); | ||
|
||
expect(innerGame.canvasSize, closeToVector(Vector2(800, 600))); | ||
expect(innerGame.isLoaded, true); | ||
expect(innerGame.isMounted, true); | ||
expect(innerGame.isLoaded, isTrue); | ||
expect(innerGame.isMounted, isTrue); | ||
}); | ||
|
||
group('components', () { | ||
|
@@ -58,8 +58,8 @@ void main() { | |
final component = Component(); | ||
await game.ensureAdd(component); | ||
|
||
expect(component.isMounted, true); | ||
expect(game.children.contains(component), true); | ||
expect(component.isMounted, isTrue); | ||
expect(game.children.contains(component), isTrue); | ||
}, | ||
); | ||
|
||
|
@@ -69,7 +69,7 @@ void main() { | |
final component = _MyAsyncComponent(); | ||
await game.ensureAdd(component); | ||
|
||
expect(game.children.contains(component), true); | ||
expect(game.children.contains(component), isTrue); | ||
expect(component.gameSize, game.size); | ||
expect(component.game, game); | ||
}, | ||
|
@@ -128,12 +128,12 @@ void main() { | |
await game.add(component); | ||
renderBox.gameLoopCallback(1.0); | ||
|
||
expect(component.isUpdateCalled, true); | ||
expect(component.isUpdateCalled, isTrue); | ||
renderBox.paint( | ||
PaintingContext(ContainerLayer(), Rect.zero), | ||
Offset.zero, | ||
); | ||
expect(component.isRenderCalled, true); | ||
expect(component.isRenderCalled, isTrue); | ||
renderBox.detach(); | ||
}, | ||
); | ||
|
@@ -165,7 +165,7 @@ void main() { | |
expect(world.children.length, equals(1)); | ||
component.removeFromParent(); | ||
game.updateTree(0); | ||
expect(world.children.isEmpty, equals(true)); | ||
expect(world.children.isEmpty, equals(isTrue)); | ||
}, | ||
); | ||
|
||
|
@@ -175,7 +175,7 @@ void main() { | |
final game = FlameGame(); | ||
final world = game.world; | ||
final component = Component()..addToParent(world); | ||
expect(game.hasLayout, false); | ||
expect(game.hasLayout, isFalse); | ||
|
||
await tester.pumpWidget(GameWidget(game: game)); | ||
game.update(0); | ||
|
@@ -698,6 +698,66 @@ void main() { | |
}); | ||
}); | ||
}); | ||
|
||
group('pauseWhenBackgrounded:', () { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add some tests where it is using a widget and not calling There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll add some tests, so that we can get this PR into the release. |
||
testWithFlameGame('true', (game) async { | ||
game.pauseWhenBackgrounded = true; | ||
|
||
game.lifecycleStateChange(AppLifecycleState.paused); | ||
expect(game.paused, isTrue); | ||
|
||
game.lifecycleStateChange(AppLifecycleState.resumed); | ||
expect(game.paused, isFalse); | ||
}); | ||
|
||
testWithFlameGame('false', (game) async { | ||
game.pauseWhenBackgrounded = false; | ||
|
||
game.lifecycleStateChange(AppLifecycleState.paused); | ||
expect(game.paused, isFalse); | ||
|
||
game.lifecycleStateChange(AppLifecycleState.resumed); | ||
expect(game.paused, isFalse); | ||
}); | ||
|
||
testWidgets( | ||
'game is not paused on start', | ||
(tester) async { | ||
final game = FlameGame(); | ||
|
||
await tester.pumpWidget( | ||
GameWidget(game: game), | ||
); | ||
|
||
await game.toBeLoaded(); | ||
await tester.pump(); | ||
|
||
expect(game.paused, isFalse); | ||
}, | ||
); | ||
|
||
testWidgets( | ||
'game is paused when app is backgrounded', | ||
(tester) async { | ||
final game = FlameGame(); | ||
|
||
await tester.pumpWidget(GameWidget(game: game)); | ||
|
||
await game.toBeLoaded(); | ||
await tester.pump(); | ||
|
||
expect(game.paused, isFalse); | ||
WidgetsBinding.instance.handleAppLifecycleStateChanged( | ||
AppLifecycleState.paused, | ||
); | ||
expect(game.paused, isTrue); | ||
WidgetsBinding.instance.handleAppLifecycleStateChanged( | ||
AppLifecycleState.resumed, | ||
); | ||
expect(game.paused, isFalse); | ||
}, | ||
); | ||
}); | ||
}); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, it's pretty weird to call these manually since these state changes should be coming from the framework.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it makes more sense for the game to act identically to if the app is backgrounded when really it was just unmounted.
This makes the link between "backgrounding the app" and "backgrounding the game (widget)"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Plus as an added bonus, it makes the behaviour more consistent on non-mobile platforms since the game will be paused when the widget is disposed of
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see what you mean, it feels like the state machine could become inconsistent with the one on Flutter's side though, which might confuse developers and be bug prone.