Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 080812a

Browse files
committed
Moved test for image stream in separate file
1 parent 35f1172 commit 080812a

File tree

3 files changed

+192
-159
lines changed

3 files changed

+192
-159
lines changed

packages/camera/camera/lib/src/camera_controller.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,8 @@ class CameraController extends ValueNotifier<CameraValue> {
269269
///
270270
// TODO(bmparr): Add settings for resolution and fps.
271271
Future<void> startImageStream(onLatestImageAvailable onAvailable) async {
272-
assert(defaultTargetPlatform != TargetPlatform.android &&
273-
defaultTargetPlatform != TargetPlatform.iOS);
272+
assert(defaultTargetPlatform == TargetPlatform.android ||
273+
defaultTargetPlatform == TargetPlatform.iOS);
274274

275275
if (!value.isInitialized || _isDisposed) {
276276
throw CameraException(
@@ -315,8 +315,8 @@ class CameraController extends ValueNotifier<CameraValue> {
315315
/// The `stopImageStream` method is only available on Android and iOS (other
316316
/// platforms won't be supported in current setup).
317317
Future<void> stopImageStream() async {
318-
assert(defaultTargetPlatform != TargetPlatform.android &&
319-
defaultTargetPlatform != TargetPlatform.iOS);
318+
assert(defaultTargetPlatform == TargetPlatform.android ||
319+
defaultTargetPlatform == TargetPlatform.iOS);
320320

321321
if (!value.isInitialized || _isDisposed) {
322322
throw CameraException(
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
import 'package:camera/camera.dart';
2+
import 'package:camera_platform_interface/camera_platform_interface.dart';
3+
import 'package:flutter/widgets.dart';
4+
import 'package:flutter_test/flutter_test.dart';
5+
6+
import 'camera_test.dart';
7+
import 'utils/method_channel_mock.dart';
8+
9+
void main() {
10+
WidgetsFlutterBinding.ensureInitialized();
11+
12+
setUp(() {
13+
CameraPlatform.instance = MockCameraPlatform();
14+
});
15+
16+
test('startImageStream() throws $CameraException when uninitialized', () {
17+
CameraController cameraController = CameraController(
18+
CameraDescription(
19+
name: 'cam',
20+
lensDirection: CameraLensDirection.back,
21+
sensorOrientation: 90),
22+
ResolutionPreset.max);
23+
24+
expect(
25+
() => cameraController.startImageStream((image) => null),
26+
throwsA(isA<CameraException>().having(
27+
(error) => error.description,
28+
'Uninitialized CameraController.',
29+
'startImageStream was called on uninitialized CameraController.',
30+
)));
31+
});
32+
33+
test('startImageStream() throws $CameraException when recording videos',
34+
() async {
35+
CameraController cameraController = CameraController(
36+
CameraDescription(
37+
name: 'cam',
38+
lensDirection: CameraLensDirection.back,
39+
sensorOrientation: 90),
40+
ResolutionPreset.max);
41+
42+
await cameraController.initialize();
43+
44+
cameraController.value =
45+
cameraController.value.copyWith(isRecordingVideo: true);
46+
47+
expect(
48+
() => cameraController.startImageStream((image) => null),
49+
throwsA(isA<CameraException>().having(
50+
(error) => error.description,
51+
'A video recording is already started.',
52+
'startImageStream was called while a video is being recorded.',
53+
)));
54+
});
55+
test(
56+
'startImageStream() throws $CameraException when already streaming images',
57+
() async {
58+
CameraController cameraController = CameraController(
59+
CameraDescription(
60+
name: 'cam',
61+
lensDirection: CameraLensDirection.back,
62+
sensorOrientation: 90),
63+
ResolutionPreset.max);
64+
await cameraController.initialize();
65+
66+
cameraController.value =
67+
cameraController.value.copyWith(isStreamingImages: true);
68+
expect(
69+
() => cameraController.startImageStream((image) => null),
70+
throwsA(isA<CameraException>().having(
71+
(error) => error.description,
72+
'A camera has started streaming images.',
73+
'startImageStream was called while a camera was streaming images.',
74+
)));
75+
});
76+
77+
test('startImageStream() calls CameraPlatform', () async {
78+
MethodChannelMock cameraChannelMock = MethodChannelMock(
79+
channelName: 'plugins.flutter.io/camera',
80+
methods: {'startImageStream': {}});
81+
MethodChannelMock streamChannelMock = MethodChannelMock(
82+
channelName: 'plugins.flutter.io/camera/imageStream',
83+
methods: {'listen': {}});
84+
85+
CameraController cameraController = CameraController(
86+
CameraDescription(
87+
name: 'cam',
88+
lensDirection: CameraLensDirection.back,
89+
sensorOrientation: 90),
90+
ResolutionPreset.max);
91+
await cameraController.initialize();
92+
93+
await cameraController.startImageStream((image) => null);
94+
95+
expect(cameraChannelMock.log,
96+
<Matcher>[isMethodCall('startImageStream', arguments: null)]);
97+
expect(streamChannelMock.log,
98+
<Matcher>[isMethodCall('listen', arguments: null)]);
99+
});
100+
101+
test('stopImageStream() throws $CameraException when uninitialized', () {
102+
CameraController cameraController = CameraController(
103+
CameraDescription(
104+
name: 'cam',
105+
lensDirection: CameraLensDirection.back,
106+
sensorOrientation: 90),
107+
ResolutionPreset.max);
108+
109+
expect(
110+
cameraController.stopImageStream,
111+
throwsA(isA<CameraException>().having(
112+
(error) => error.description,
113+
'Uninitialized CameraController.',
114+
'stopImageStream was called on uninitialized CameraController.',
115+
)));
116+
});
117+
118+
test('stopImageStream() throws $CameraException when recording videos',
119+
() async {
120+
CameraController cameraController = CameraController(
121+
CameraDescription(
122+
name: 'cam',
123+
lensDirection: CameraLensDirection.back,
124+
sensorOrientation: 90),
125+
ResolutionPreset.max);
126+
await cameraController.initialize();
127+
128+
await cameraController.startImageStream((image) => null);
129+
cameraController.value =
130+
cameraController.value.copyWith(isRecordingVideo: true);
131+
expect(
132+
cameraController.stopImageStream,
133+
throwsA(isA<CameraException>().having(
134+
(error) => error.description,
135+
'A video recording is already started.',
136+
'stopImageStream was called while a video is being recorded.',
137+
)));
138+
});
139+
140+
test('stopImageStream() throws $CameraException when not streaming images',
141+
() async {
142+
CameraController cameraController = CameraController(
143+
CameraDescription(
144+
name: 'cam',
145+
lensDirection: CameraLensDirection.back,
146+
sensorOrientation: 90),
147+
ResolutionPreset.max);
148+
await cameraController.initialize();
149+
150+
expect(
151+
cameraController.stopImageStream,
152+
throwsA(isA<CameraException>().having(
153+
(error) => error.description,
154+
'No camera is streaming images',
155+
'stopImageStream was called when no camera is streaming images.',
156+
)));
157+
});
158+
159+
test('stopImageStream() intended behaviour', () async {
160+
MethodChannelMock cameraChannelMock = MethodChannelMock(
161+
channelName: 'plugins.flutter.io/camera',
162+
methods: {'startImageStream': {}, 'stopImageStream': {}});
163+
MethodChannelMock streamChannelMock = MethodChannelMock(
164+
channelName: 'plugins.flutter.io/camera/imageStream',
165+
methods: {'listen': {}, 'cancel': {}});
166+
167+
CameraController cameraController = CameraController(
168+
CameraDescription(
169+
name: 'cam',
170+
lensDirection: CameraLensDirection.back,
171+
sensorOrientation: 90),
172+
ResolutionPreset.max);
173+
await cameraController.initialize();
174+
await cameraController.startImageStream((image) => null);
175+
await cameraController.stopImageStream();
176+
177+
expect(cameraChannelMock.log, <Matcher>[
178+
isMethodCall('startImageStream', arguments: null),
179+
isMethodCall('stopImageStream', arguments: null)
180+
]);
181+
182+
expect(streamChannelMock.log, <Matcher>[
183+
isMethodCall('listen', arguments: null),
184+
isMethodCall('cancel', arguments: null)
185+
]);
186+
});
187+
}

packages/camera/camera/test/camera_test.dart

Lines changed: 1 addition & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ import 'package:flutter_test/flutter_test.dart';
1313
import 'package:mockito/mockito.dart';
1414
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
1515

16-
import 'utils/method_channel_mock.dart';
17-
1816
get mockAvailableCameras => [
1917
CameraDescription(
2018
name: 'camBack',
@@ -42,6 +40,7 @@ bool mockPlatformException = false;
4240

4341
void main() {
4442
WidgetsFlutterBinding.ensureInitialized();
43+
4544
group('camera', () {
4645
test('debugCheckIsDisposed should not throw assertion error when disposed',
4746
() {
@@ -248,159 +247,6 @@ void main() {
248247
mockPlatformException = false;
249248
});
250249

251-
test('startImageStream() throws $CameraException when uninitialized', () {
252-
CameraController cameraController = CameraController(
253-
CameraDescription(
254-
name: 'cam',
255-
lensDirection: CameraLensDirection.back,
256-
sensorOrientation: 90),
257-
ResolutionPreset.max);
258-
expect(
259-
cameraController.startImageStream((image) => null),
260-
throwsA(isA<CameraException>().having(
261-
(error) => error.description,
262-
'Uninitialized CameraController.',
263-
'startImageStream was called on uninitialized CameraController.',
264-
)));
265-
});
266-
267-
test('startImageStream() throws $CameraException when recording videos',
268-
() async {
269-
CameraController cameraController = CameraController(
270-
CameraDescription(
271-
name: 'cam',
272-
lensDirection: CameraLensDirection.back,
273-
sensorOrientation: 90),
274-
ResolutionPreset.max);
275-
await cameraController.initialize();
276-
277-
cameraController.value =
278-
cameraController.value.copyWith(isRecordingVideo: true);
279-
expect(
280-
cameraController.startImageStream((image) => null),
281-
throwsA(isA<CameraException>().having(
282-
(error) => error.description,
283-
'A video recording is already started.',
284-
'startImageStream was called while a video is being recorded.',
285-
)));
286-
});
287-
test(
288-
'startImageStream() throws $CameraException when already streaming images',
289-
() async {
290-
CameraController cameraController = CameraController(
291-
CameraDescription(
292-
name: 'cam',
293-
lensDirection: CameraLensDirection.back,
294-
sensorOrientation: 90),
295-
ResolutionPreset.max);
296-
await cameraController.initialize();
297-
298-
cameraController.value =
299-
cameraController.value.copyWith(isStreamingImages: true);
300-
expect(
301-
cameraController.startImageStream((image) => null),
302-
throwsA(isA<CameraException>().having(
303-
(error) => error.description,
304-
'A camera has started streaming images.',
305-
'startImageStream was called while a camera was streaming images.',
306-
)));
307-
});
308-
309-
test('startImageStream() calls CameraPlatform', () async {
310-
MethodChannelMock methodChannelMock = MethodChannelMock(
311-
channelName: 'plugins.flutter.io/camera',
312-
methods: {'startImageStream': {}});
313-
CameraController cameraController = CameraController(
314-
CameraDescription(
315-
name: 'cam',
316-
lensDirection: CameraLensDirection.back,
317-
sensorOrientation: 90),
318-
ResolutionPreset.max);
319-
await cameraController.initialize();
320-
321-
await cameraController.startImageStream((image) => null);
322-
323-
expect(methodChannelMock.log,
324-
<Matcher>[isMethodCall('startImageStream', arguments: null)]);
325-
});
326-
327-
test('stopImageStream() throws $CameraException when uninitialized', () {
328-
CameraController cameraController = CameraController(
329-
CameraDescription(
330-
name: 'cam',
331-
lensDirection: CameraLensDirection.back,
332-
sensorOrientation: 90),
333-
ResolutionPreset.max);
334-
335-
expect(
336-
cameraController.stopImageStream(),
337-
throwsA(isA<CameraException>().having(
338-
(error) => error.description,
339-
'Uninitialized CameraController.',
340-
'stopImageStream was called on uninitialized CameraController.',
341-
)));
342-
});
343-
344-
test('stopImageStream() throws $CameraException when recording videos',
345-
() async {
346-
CameraController cameraController = CameraController(
347-
CameraDescription(
348-
name: 'cam',
349-
lensDirection: CameraLensDirection.back,
350-
sensorOrientation: 90),
351-
ResolutionPreset.max);
352-
await cameraController.initialize();
353-
354-
await cameraController.startImageStream((image) => null);
355-
cameraController.value =
356-
cameraController.value.copyWith(isRecordingVideo: true);
357-
expect(
358-
cameraController.stopImageStream(),
359-
throwsA(isA<CameraException>().having(
360-
(error) => error.description,
361-
'A video recording is already started.',
362-
'stopImageStream was called while a video is being recorded.',
363-
)));
364-
});
365-
366-
test('stopImageStream() throws $CameraException when not streaming images',
367-
() async {
368-
CameraController cameraController = CameraController(
369-
CameraDescription(
370-
name: 'cam',
371-
lensDirection: CameraLensDirection.back,
372-
sensorOrientation: 90),
373-
ResolutionPreset.max);
374-
await cameraController.initialize();
375-
376-
expect(
377-
cameraController.stopImageStream(),
378-
throwsA(isA<CameraException>().having(
379-
(error) => error.description,
380-
'No camera is streaming images',
381-
'stopImageStream was called when no camera is streaming images.',
382-
)));
383-
});
384-
385-
test('stopImageStream() intended behaviour', () async {
386-
MethodChannelMock methodChannelMock = MethodChannelMock(
387-
channelName: 'plugins.flutter.io/camera',
388-
methods: {'startImageStream': {}, 'stopImageStream': {}});
389-
CameraController cameraController = CameraController(
390-
CameraDescription(
391-
name: 'cam',
392-
lensDirection: CameraLensDirection.back,
393-
sensorOrientation: 90),
394-
ResolutionPreset.max);
395-
await cameraController.initialize();
396-
await cameraController.startImageStream((image) => null);
397-
await cameraController.stopImageStream();
398-
expect(methodChannelMock.log, <Matcher>[
399-
isMethodCall('startImageStream', arguments: null),
400-
isMethodCall('stopImageStream', arguments: null)
401-
]);
402-
});
403-
404250
test('startVideoRecording() throws $CameraException when uninitialized',
405251
() async {
406252
CameraController cameraController = CameraController(

0 commit comments

Comments
 (0)