Skip to content

Commit 5b1f975

Browse files
authored
[camera]fix resolution test failure due to hardware restriction (flutter#13356)
This fixes a failure when running on iPhone 17. The reason is: ``` case .low: if videoCaptureSession.canSetSessionPreset(.cif352x288) { ... break } fallthrough default: if videoCaptureSession.canSetSessionPreset(.low) { videoCaptureSession.sessionPreset = .low // size is up to the device ``` On iPhone 17, `.cif352x288` is not supported, so it falls back to `.low`, which is 480p (480x640). This actually fails even before iOS 27, but we didn't catch it because it requires hardware device. To prevent similar failures in the future, we shouldn't assume hardware capabilities (e.g. photo resolution). So I updated the test to verify photos in higher settings have relatively higher resolutions. Note that I did not change Android - I think it was as fragile as iOS and could break in the future. However, unlike iOS, the Android tests do run on real devices in our CI, so we should be able to catch any failure if that happens, so I wouldn't be concerned about it. *List which issues are fixed by this PR. You must list at least one issue.* Fixes flutter/flutter#191202 ## Pre-Review Checklist - [ x I updated/added any relevant documentation (doc comments with `///`). **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 301ab86 commit 5b1f975

1 file changed

Lines changed: 50 additions & 87 deletions

File tree

‎packages/camera/camera_avfoundation/example/integration_test/camera_test.dart‎

Lines changed: 50 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -30,47 +30,51 @@ void main() {
3030
await testDir.delete(recursive: true);
3131
});
3232

33-
final presetExpectedSizes = <ResolutionPreset, Size>{
34-
ResolutionPreset.low: const Size(288, 352),
35-
ResolutionPreset.medium: const Size(480, 640),
36-
ResolutionPreset.high: const Size(720, 1280),
37-
ResolutionPreset.veryHigh: const Size(1080, 1920),
38-
ResolutionPreset.ultraHigh: const Size(2160, 3840),
39-
// Don't bother checking for max here since it could be anything.
40-
};
41-
42-
/// Verify that [actual] has dimensions that are at least as large as
43-
/// [expectedSize]. Allows for a mismatch in portrait vs landscape. Returns
44-
/// whether the dimensions exactly match.
45-
bool assertExpectedDimensions(Size expectedSize, Size actual) {
46-
expect(actual.shortestSide, lessThanOrEqualTo(expectedSize.shortestSide));
47-
expect(actual.longestSide, lessThanOrEqualTo(expectedSize.longestSide));
48-
return actual.shortestSide == expectedSize.shortestSide &&
49-
actual.longestSide == expectedSize.longestSide;
50-
}
51-
52-
// This tests that the capture is no bigger than the preset, since we have
53-
// automatic code to fall back to smaller sizes when we need to. Returns
54-
// whether the image is exactly the desired resolution.
55-
Future<bool> testCaptureImageResolution(
56-
CameraController controller,
57-
ResolutionPreset preset,
33+
// Presets in increasing order of requested resolution. `max` is excluded
34+
// since its resolution could be anything.
35+
const presets = <ResolutionPreset>[
36+
ResolutionPreset.low,
37+
ResolutionPreset.medium,
38+
ResolutionPreset.high,
39+
ResolutionPreset.veryHigh,
40+
ResolutionPreset.ultraHigh,
41+
];
42+
43+
/// Captures with each of [presets] using [capture], and verifies that the
44+
/// captured size never decreases as the requested preset increases.
45+
///
46+
/// Exact sizes aren't checked because a preset that the device doesn't
47+
/// support falls back to a device-specific resolution, which varies across
48+
/// hardware. Allows for a mismatch in portrait vs landscape.
49+
Future<void> expectNonDecreasingCaptureSizes(
50+
CameraDescription cameraDescription,
51+
Future<Size> Function(CameraController controller) capture,
5852
) async {
59-
final Size expectedSize = presetExpectedSizes[preset]!;
53+
ResolutionPreset? previousPreset;
54+
Size? previousSize;
55+
for (final preset in presets) {
56+
final controller = CameraController(cameraDescription, preset);
57+
await controller.initialize();
58+
final Size size = await capture(controller);
59+
await controller.dispose();
60+
61+
expect(size.isEmpty, isFalse, reason: '${cameraDescription.name} $preset captured $size');
62+
if (previousSize != null) {
63+
final reason =
64+
'${cameraDescription.name}: $preset captured $size, which is smaller '
65+
'than $previousSize captured by $previousPreset';
66+
expect(size.shortestSide, greaterThanOrEqualTo(previousSize.shortestSide), reason: reason);
67+
expect(size.longestSide, greaterThanOrEqualTo(previousSize.longestSide), reason: reason);
68+
}
69+
previousPreset = preset;
70+
previousSize = size;
71+
}
72+
}
6073

61-
// Take Picture
74+
Future<Size> captureImageSize(CameraController controller) async {
6275
final XFile file = await controller.takePicture();
63-
64-
// Load picture
65-
final fileImage = File(file.path);
66-
final Image image = await decodeImageFromList(fileImage.readAsBytesSync());
67-
68-
// Verify image dimensions are as expected
69-
expect(image, isNotNull);
70-
return assertExpectedDimensions(
71-
expectedSize,
72-
Size(image.height.toDouble(), image.width.toDouble()),
73-
);
76+
final Image image = await decodeImageFromList(File(file.path).readAsBytesSync());
77+
return Size(image.width.toDouble(), image.height.toDouble());
7478
}
7579

7680
testWidgets('Capture specific image resolutions', (WidgetTester tester) async {
@@ -79,47 +83,21 @@ void main() {
7983
return;
8084
}
8185
for (final cameraDescription in cameras) {
82-
var previousPresetExactlySupported = true;
83-
for (final MapEntry<ResolutionPreset, Size> preset in presetExpectedSizes.entries) {
84-
final controller = CameraController(cameraDescription, preset.key);
85-
await controller.initialize();
86-
final bool presetExactlySupported = await testCaptureImageResolution(
87-
controller,
88-
preset.key,
89-
);
90-
assert(
91-
!(!previousPresetExactlySupported && presetExactlySupported),
92-
'The camera took higher resolution pictures at a lower resolution.',
93-
);
94-
previousPresetExactlySupported = presetExactlySupported;
95-
await controller.dispose();
96-
}
86+
await expectNonDecreasingCaptureSizes(cameraDescription, captureImageSize);
9787
}
9888
});
9989

100-
// This tests that the capture is no bigger than the preset, since we have
101-
// automatic code to fall back to smaller sizes when we need to. Returns
102-
// whether the image is exactly the desired resolution.
103-
Future<bool> testCaptureVideoResolution(
104-
CameraController controller,
105-
ResolutionPreset preset,
106-
) async {
107-
final Size expectedSize = presetExpectedSizes[preset]!;
108-
109-
// Take Video
90+
Future<Size> captureVideoSize(CameraController controller) async {
91+
await controller.prepareForVideoRecording();
11092
await controller.startVideoRecording();
11193
await Future<void>.delayed(const Duration(milliseconds: 300));
11294
final XFile file = await controller.stopVideoRecording();
11395

114-
// Load video metadata
115-
final videoFile = File(file.path);
116-
final videoController = VideoPlayerController.file(videoFile);
96+
final videoController = VideoPlayerController.file(File(file.path));
11797
await videoController.initialize();
118-
final Size video = videoController.value.size;
119-
120-
// Verify image dimensions are as expected
121-
expect(video, isNotNull);
122-
return assertExpectedDimensions(expectedSize, Size(video.height, video.width));
98+
final Size size = videoController.value.size;
99+
await videoController.dispose();
100+
return size;
123101
}
124102

125103
testWidgets('Capture specific video resolutions', (WidgetTester tester) async {
@@ -128,22 +106,7 @@ void main() {
128106
return;
129107
}
130108
for (final cameraDescription in cameras) {
131-
var previousPresetExactlySupported = true;
132-
for (final MapEntry<ResolutionPreset, Size> preset in presetExpectedSizes.entries) {
133-
final controller = CameraController(cameraDescription, preset.key);
134-
await controller.initialize();
135-
await controller.prepareForVideoRecording();
136-
final bool presetExactlySupported = await testCaptureVideoResolution(
137-
controller,
138-
preset.key,
139-
);
140-
assert(
141-
!(!previousPresetExactlySupported && presetExactlySupported),
142-
'The camera took higher resolution pictures at a lower resolution.',
143-
);
144-
previousPresetExactlySupported = presetExactlySupported;
145-
await controller.dispose();
146-
}
109+
await expectNonDecreasingCaptureSizes(cameraDescription, captureVideoSize);
147110
}
148111
});
149112

0 commit comments

Comments
 (0)