Skip to content

Commit 02c1207

Browse files
committed
throw when parsing invalid orientation
1 parent e346344 commit 02c1207

File tree

3 files changed

+63
-28
lines changed

3 files changed

+63
-28
lines changed

lib/src/method_channel/android_surface_producer_delegate.dart

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,10 @@ class AndroidSurfaceProducerDelegate {
3434
'naturalDeviceOrientation': final String naturalDeviceOrientation,
3535
'sensorOrientation': final int sensorOrientation
3636
}) {
37-
final DeviceOrientation? currentOrientation =
38-
deviceOrientation.tryParseDeviceOrientation();
39-
final DeviceOrientation? naturalOrientation =
40-
naturalDeviceOrientation.tryParseDeviceOrientation();
41-
42-
if (currentOrientation == null) {
43-
throw const MobileScannerException(
44-
errorCode: MobileScannerErrorCode.genericError,
45-
errorDetails: MobileScannerErrorDetails(
46-
message:
47-
'The start method did not return a valid current device orientation.',
48-
),
49-
);
50-
}
51-
52-
if (naturalOrientation == null) {
53-
throw const MobileScannerException(
54-
errorCode: MobileScannerErrorCode.genericError,
55-
errorDetails: MobileScannerErrorDetails(
56-
message:
57-
'The start method did not return a valid natural device orientation.',
58-
),
59-
);
60-
}
37+
final DeviceOrientation currentOrientation =
38+
deviceOrientation.parseDeviceOrientation();
39+
final DeviceOrientation naturalOrientation =
40+
naturalDeviceOrientation.parseDeviceOrientation();
6141

6242
return AndroidSurfaceProducerDelegate(
6343
cameraIsFrontFacing: cameraDirection == CameraFacing.front,

lib/src/utils/parse_device_orientation_extension.dart

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,19 @@ import 'package:flutter/services.dart';
55
extension ParseDeviceOrientation on String {
66
/// Parse `this` into a [DeviceOrientation].
77
///
8-
/// Returns the parsed device orientation,
9-
/// or null if `this` is not a valid device orientation.
10-
DeviceOrientation? tryParseDeviceOrientation() {
8+
/// Returns the parsed device orientation.
9+
/// Throws an [ArgumentError] if `this` is an invalid device orientation.
10+
DeviceOrientation parseDeviceOrientation() {
1111
return switch (this) {
1212
'PORTRAIT_UP' => DeviceOrientation.portraitUp,
1313
'PORTRAIT_DOWN' => DeviceOrientation.portraitDown,
1414
'LANDSCAPE_LEFT' => DeviceOrientation.landscapeLeft,
1515
'LANDSCAPE_RIGHT' => DeviceOrientation.landscapeRight,
16-
_ => null,
16+
_ => throw ArgumentError.value(
17+
this,
18+
'deviceOrientation',
19+
'Received an invalid device orientation',
20+
),
1721
};
1822
}
1923
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import 'package:flutter/services.dart';
2+
import 'package:flutter_test/flutter_test.dart';
3+
import 'package:mobile_scanner/src/utils/parse_device_orientation_extension.dart';
4+
5+
void main() {
6+
group('parseDeviceOrientation', () {
7+
test('can parse valid device orientation string', () {
8+
expect(
9+
'PORTRAIT_UP'.parseDeviceOrientation(),
10+
DeviceOrientation.portraitUp,
11+
);
12+
expect(
13+
'PORTRAIT_DOWN'.parseDeviceOrientation(),
14+
DeviceOrientation.portraitDown,
15+
);
16+
expect(
17+
'LANDSCAPE_LEFT'.parseDeviceOrientation(),
18+
DeviceOrientation.landscapeLeft,
19+
);
20+
expect(
21+
'LANDSCAPE_RIGHT'.parseDeviceOrientation(),
22+
DeviceOrientation.landscapeRight,
23+
);
24+
});
25+
26+
test('throws ArgumentError when parsing invalid device orientation string',
27+
() {
28+
expect(
29+
() => ''.parseDeviceOrientation(),
30+
throwsA(
31+
isA<ArgumentError>().having(
32+
(error) => error.message,
33+
'message',
34+
'Received an invalid device orientation',
35+
),
36+
),
37+
);
38+
39+
expect(
40+
() => 'foo'.parseDeviceOrientation(),
41+
throwsA(
42+
isA<ArgumentError>().having(
43+
(error) => error.message,
44+
'message',
45+
'Received an invalid device orientation',
46+
),
47+
),
48+
);
49+
});
50+
});
51+
}

0 commit comments

Comments
 (0)