Skip to content

Commit 5583dea

Browse files
lightbox test [nfc]: Refactor FakeVideoPlayerPlatform
1 parent d5725d9 commit 5583dea

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

test/widgets/lightbox_test.dart

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,43 @@ import '../model/binding.dart';
1616
class FakeVideoPlayerPlatform extends Fake
1717
with MockPlatformInterfaceMixin
1818
implements VideoPlayerPlatform {
19-
static const int _textureId = 0xffffffff;
20-
21-
static StreamController<VideoEvent> _streamController = StreamController<VideoEvent>();
22-
static bool initialized = false;
23-
static bool isPlaying = false;
19+
static final FakeVideoPlayerPlatform instance = FakeVideoPlayerPlatform();
2420

2521
static void registerWith() {
26-
VideoPlayerPlatform.instance = FakeVideoPlayerPlatform();
22+
VideoPlayerPlatform.instance = instance;
2723
}
2824

29-
static void reset() {
25+
static const int _textureId = 0xffffffff;
26+
27+
StreamController<VideoEvent> _streamController = StreamController<VideoEvent>();
28+
29+
bool get initialized => _initialized;
30+
bool _initialized = false;
31+
32+
bool get isPlaying => _isPlaying;
33+
bool _isPlaying = false;
34+
35+
void reset() {
3036
_streamController.close();
3137
_streamController = StreamController<VideoEvent>();
32-
initialized = false;
33-
isPlaying = false;
38+
_initialized = false;
39+
_isPlaying = false;
3440
}
3541

3642
@override
3743
Future<void> init() async {}
3844

3945
@override
4046
Future<void> dispose(int textureId) async {
41-
assert(initialized);
47+
assert(_initialized);
4248
assert(textureId == _textureId);
43-
initialized = false;
49+
_initialized = false;
4450
}
4551

4652
@override
4753
Future<int?> create(DataSource dataSource) async {
48-
assert(!initialized);
49-
initialized = true;
54+
assert(!_initialized);
55+
_initialized = true;
5056
_streamController.add(VideoEvent(
5157
eventType: VideoEventType.initialized,
5258
duration: const Duration(seconds: 1),
@@ -71,7 +77,7 @@ class FakeVideoPlayerPlatform extends Fake
7177
@override
7278
Future<void> play(int textureId) async {
7379
assert(textureId == _textureId);
74-
isPlaying = true;
80+
_isPlaying = true;
7581
_streamController.add(VideoEvent(
7682
eventType: VideoEventType.isPlayingStateUpdate,
7783
isPlaying: true,
@@ -81,7 +87,7 @@ class FakeVideoPlayerPlatform extends Fake
8187
@override
8288
Future<void> pause(int textureId) async {
8389
assert(textureId == _textureId);
84-
isPlaying = false;
90+
_isPlaying = false;
8591
_streamController.add(VideoEvent(
8692
eventType: VideoEventType.isPlayingStateUpdate,
8793
isPlaying: false,
@@ -134,13 +140,14 @@ void main() {
134140

135141
group("VideoLightboxPage", () {
136142
FakeVideoPlayerPlatform.registerWith();
143+
final platform = FakeVideoPlayerPlatform.instance;
137144

138145
Future<void> setupPage(WidgetTester tester, {
139146
required Uri videoSrc,
140147
}) async {
141148
addTearDown(testBinding.reset);
142149
await testBinding.globalStore.add(eg.selfAccount, eg.initialSnapshot());
143-
addTearDown(FakeVideoPlayerPlatform.reset);
150+
addTearDown(platform.reset);
144151

145152
await tester.pumpWidget(GlobalStoreWidget(child: MaterialApp(
146153
localizationsDelegates: ZulipLocalizations.localizationsDelegates,
@@ -157,24 +164,24 @@ void main() {
157164
testWidgets('shows a VideoPlayer, and video is playing', (tester) async {
158165
await setupPage(tester, videoSrc: Uri.parse('https://a/b.mp4'));
159166

160-
check(FakeVideoPlayerPlatform.initialized).isTrue();
161-
check(FakeVideoPlayerPlatform.isPlaying).isTrue();
167+
check(platform.initialized).isTrue();
168+
check(platform.isPlaying).isTrue();
162169

163170
await tester.ensureVisible(find.byType(VideoPlayer));
164171
});
165172

166173
testWidgets('toggles between play and pause', (tester) async {
167174
await setupPage(tester, videoSrc: Uri.parse('https://a/b.mp4'));
168-
check(FakeVideoPlayerPlatform.isPlaying).isTrue();
175+
check(platform.isPlaying).isTrue();
169176

170177
await tester.tap(find.byIcon(Icons.pause_circle_rounded));
171-
check(FakeVideoPlayerPlatform.isPlaying).isFalse();
178+
check(platform.isPlaying).isFalse();
172179

173180
// re-render to update player controls
174181
await tester.pump();
175182

176183
await tester.tap(find.byIcon(Icons.play_circle_rounded));
177-
check(FakeVideoPlayerPlatform.isPlaying).isTrue();
184+
check(platform.isPlaying).isTrue();
178185
});
179186
});
180187
}

0 commit comments

Comments
 (0)