|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +// TODO(mvanbeusekom): Remove this once flutter_driver supports null safety. |
| 6 | +// https://github.com/flutter/flutter/issues/71379 |
| 7 | +// @dart = 2.9 |
| 8 | +import 'dart:async'; |
| 9 | +import 'dart:io'; |
| 10 | +import 'dart:ui'; |
| 11 | + |
| 12 | +import 'package:camera/camera.dart'; |
| 13 | +import 'package:flutter/painting.dart'; |
| 14 | +import 'package:flutter_test/flutter_test.dart'; |
| 15 | +import 'package:path_provider/path_provider.dart'; |
| 16 | +import 'package:video_player/video_player.dart'; |
| 17 | +import 'package:integration_test/integration_test.dart'; |
| 18 | + |
| 19 | +void main() { |
| 20 | + Directory testDir; |
| 21 | + |
| 22 | + IntegrationTestWidgetsFlutterBinding.ensureInitialized(); |
| 23 | + |
| 24 | + setUpAll(() async { |
| 25 | + final Directory extDir = await getTemporaryDirectory(); |
| 26 | + testDir = await Directory('${extDir.path}/test').create(recursive: true); |
| 27 | + }); |
| 28 | + |
| 29 | + tearDownAll(() async { |
| 30 | + await testDir.delete(recursive: true); |
| 31 | + }); |
| 32 | + |
| 33 | + // final Map<ResolutionPreset, Size> presetExpectedSizes = |
| 34 | + // <ResolutionPreset, Size>{ |
| 35 | + // ResolutionPreset.low: |
| 36 | + // Platform.isAndroid ? const Size(240, 320) : const Size(288, 352), |
| 37 | + // ResolutionPreset.medium: |
| 38 | + // Platform.isAndroid ? const Size(480, 720) : const Size(480, 640), |
| 39 | + // ResolutionPreset.high: const Size(720, 1280), |
| 40 | + // ResolutionPreset.veryHigh: const Size(1080, 1920), |
| 41 | + // ResolutionPreset.ultraHigh: const Size(2160, 3840), |
| 42 | + // // Don't bother checking for max here since it could be anything. |
| 43 | + // }; |
| 44 | + |
| 45 | + final Map<ResolutionPreset, Size> presetExpectedSizes = |
| 46 | + <ResolutionPreset, Size>{ |
| 47 | + ResolutionPreset.medium: const Size(480, 720), |
| 48 | + // Don't bother checking for max here since it could be anything. |
| 49 | + }; |
| 50 | + |
| 51 | + /// Verify that [actual] has dimensions that are at least as large as |
| 52 | + /// [expectedSize]. Allows for a mismatch in portrait vs landscape. Returns |
| 53 | + /// whether the dimensions exactly match. |
| 54 | + bool assertExpectedDimensions(Size expectedSize, Size actual) { |
| 55 | + expect(actual.shortestSide, lessThanOrEqualTo(expectedSize.shortestSide)); |
| 56 | + expect(actual.longestSide, lessThanOrEqualTo(expectedSize.longestSide)); |
| 57 | + return actual.shortestSide == expectedSize.shortestSide && |
| 58 | + actual.longestSide == expectedSize.longestSide; |
| 59 | + } |
| 60 | + |
| 61 | + // This tests that the capture is no bigger than the preset, since we have |
| 62 | + // automatic code to fall back to smaller sizes when we need to. Returns |
| 63 | + // whether the image is exactly the desired resolution. |
| 64 | + Future<bool> testCaptureImageResolution( |
| 65 | + CameraController controller, ResolutionPreset preset) async { |
| 66 | + final Size expectedSize = presetExpectedSizes[preset]; |
| 67 | + print( |
| 68 | + 'Capturing photo at $preset (${expectedSize.width}x${expectedSize.height}) using camera ${controller.description.name}'); |
| 69 | + |
| 70 | + // Take Picture |
| 71 | + final file = await controller.takePicture(); |
| 72 | + |
| 73 | + // Load picture |
| 74 | + final File fileImage = File(file.path); |
| 75 | + final Image image = await decodeImageFromList(fileImage.readAsBytesSync()); |
| 76 | + |
| 77 | + // Verify image dimensions are as expected |
| 78 | + expect(image, isNotNull); |
| 79 | + return assertExpectedDimensions( |
| 80 | + expectedSize, Size(image.height.toDouble(), image.width.toDouble())); |
| 81 | + } |
| 82 | + |
| 83 | + testWidgets('Capture specific image resolutions', |
| 84 | + (WidgetTester tester) async { |
| 85 | + final List<CameraDescription> cameras = await availableCameras(); |
| 86 | + if (cameras.isEmpty) { |
| 87 | + return; |
| 88 | + } |
| 89 | + for (CameraDescription cameraDescription in cameras) { |
| 90 | + bool previousPresetExactlySupported = true; |
| 91 | + for (MapEntry<ResolutionPreset, Size> preset |
| 92 | + in presetExpectedSizes.entries) { |
| 93 | + final CameraController controller = |
| 94 | + CameraController(cameraDescription, preset.key); |
| 95 | + await controller.initialize(); |
| 96 | + final bool presetExactlySupported = |
| 97 | + await testCaptureImageResolution(controller, preset.key); |
| 98 | + assert(!(!previousPresetExactlySupported && presetExactlySupported), |
| 99 | + 'The camera took higher resolution pictures at a lower resolution.'); |
| 100 | + previousPresetExactlySupported = presetExactlySupported; |
| 101 | + await controller.dispose(); |
| 102 | + } |
| 103 | + } |
| 104 | + }); |
| 105 | + |
| 106 | + // This tests that the capture is no bigger than the preset, since we have |
| 107 | + // automatic code to fall back to smaller sizes when we need to. Returns |
| 108 | + // whether the image is exactly the desired resolution. |
| 109 | + Future<bool> testCaptureVideoResolution( |
| 110 | + CameraController controller, ResolutionPreset preset) async { |
| 111 | + final Size expectedSize = presetExpectedSizes[preset]; |
| 112 | + print( |
| 113 | + 'Capturing video at $preset (${expectedSize.width}x${expectedSize.height}) using camera ${controller.description.name}'); |
| 114 | + |
| 115 | + // Take Video |
| 116 | + await controller.startVideoRecording(); |
| 117 | + sleep(const Duration(milliseconds: 300)); |
| 118 | + final file = await controller.stopVideoRecording(); |
| 119 | + |
| 120 | + // Load video metadata |
| 121 | + final File videoFile = File(file.path); |
| 122 | + final VideoPlayerController videoController = |
| 123 | + VideoPlayerController.file(videoFile); |
| 124 | + await videoController.initialize(); |
| 125 | + final Size video = videoController.value.size; |
| 126 | + |
| 127 | + // Verify image dimensions are as expected |
| 128 | + expect(video, isNotNull); |
| 129 | + return assertExpectedDimensions( |
| 130 | + expectedSize, Size(video.height, video.width)); |
| 131 | + } |
| 132 | + |
| 133 | + testWidgets('Capture specific video resolutions', |
| 134 | + (WidgetTester tester) async { |
| 135 | + final List<CameraDescription> cameras = await availableCameras(); |
| 136 | + if (cameras.isEmpty) { |
| 137 | + return; |
| 138 | + } |
| 139 | + for (CameraDescription cameraDescription in cameras) { |
| 140 | + bool previousPresetExactlySupported = true; |
| 141 | + for (MapEntry<ResolutionPreset, Size> preset |
| 142 | + in presetExpectedSizes.entries) { |
| 143 | + final CameraController controller = |
| 144 | + CameraController(cameraDescription, preset.key); |
| 145 | + await controller.initialize(); |
| 146 | + // await controller.prepareForVideoRecording(); |
| 147 | + final bool presetExactlySupported = |
| 148 | + await testCaptureVideoResolution(controller, preset.key); |
| 149 | + assert(!(!previousPresetExactlySupported && presetExactlySupported), |
| 150 | + 'The camera took higher resolution pictures at a lower resolution.'); |
| 151 | + previousPresetExactlySupported = presetExactlySupported; |
| 152 | + await controller.dispose(); |
| 153 | + } |
| 154 | + } |
| 155 | + }); |
| 156 | + |
| 157 | + testWidgets('Pause and resume video recording', (WidgetTester tester) async { |
| 158 | + final List<CameraDescription> cameras = await availableCameras(); |
| 159 | + if (cameras.isEmpty) { |
| 160 | + return; |
| 161 | + } |
| 162 | + |
| 163 | + final CameraController controller = CameraController( |
| 164 | + cameras[0], |
| 165 | + ResolutionPreset.low, |
| 166 | + enableAudio: false, |
| 167 | + ); |
| 168 | + |
| 169 | + await controller.initialize(); |
| 170 | + // await controller.prepareForVideoRecording(); |
| 171 | + |
| 172 | + int startPause; |
| 173 | + int timePaused = 0; |
| 174 | + |
| 175 | + await controller.startVideoRecording(); |
| 176 | + final int recordingStart = DateTime.now().millisecondsSinceEpoch; |
| 177 | + sleep(const Duration(milliseconds: 500)); |
| 178 | + |
| 179 | + await controller.pauseVideoRecording(); |
| 180 | + startPause = DateTime.now().millisecondsSinceEpoch; |
| 181 | + sleep(const Duration(milliseconds: 500)); |
| 182 | + await controller.resumeVideoRecording(); |
| 183 | + timePaused += DateTime.now().millisecondsSinceEpoch - startPause; |
| 184 | + |
| 185 | + sleep(const Duration(milliseconds: 500)); |
| 186 | + |
| 187 | + await controller.pauseVideoRecording(); |
| 188 | + startPause = DateTime.now().millisecondsSinceEpoch; |
| 189 | + sleep(const Duration(milliseconds: 500)); |
| 190 | + await controller.resumeVideoRecording(); |
| 191 | + timePaused += DateTime.now().millisecondsSinceEpoch - startPause; |
| 192 | + |
| 193 | + sleep(const Duration(milliseconds: 500)); |
| 194 | + |
| 195 | + final file = await controller.stopVideoRecording(); |
| 196 | + final int recordingTime = |
| 197 | + DateTime.now().millisecondsSinceEpoch - recordingStart; |
| 198 | + |
| 199 | + final File videoFile = File(file.path); |
| 200 | + final VideoPlayerController videoController = VideoPlayerController.file( |
| 201 | + videoFile, |
| 202 | + ); |
| 203 | + await videoController.initialize(); |
| 204 | + final int duration = videoController.value.duration.inMilliseconds; |
| 205 | + await videoController.dispose(); |
| 206 | + |
| 207 | + expect(duration, lessThan(recordingTime - timePaused)); |
| 208 | + }); |
| 209 | + |
| 210 | + testWidgets( |
| 211 | + 'Android image streaming', |
| 212 | + (WidgetTester tester) async { |
| 213 | + final List<CameraDescription> cameras = await availableCameras(); |
| 214 | + if (cameras.isEmpty) { |
| 215 | + return; |
| 216 | + } |
| 217 | + |
| 218 | + final CameraController controller = CameraController( |
| 219 | + cameras[0], |
| 220 | + ResolutionPreset.low, |
| 221 | + enableAudio: false, |
| 222 | + ); |
| 223 | + |
| 224 | + await controller.initialize(); |
| 225 | + bool _isDetecting = false; |
| 226 | + |
| 227 | + await controller.startImageStream((CameraImage image) { |
| 228 | + if (_isDetecting) return; |
| 229 | + |
| 230 | + _isDetecting = true; |
| 231 | + |
| 232 | + expectLater(image, isNotNull).whenComplete(() => _isDetecting = false); |
| 233 | + }); |
| 234 | + |
| 235 | + expect(controller.value.isStreamingImages, true); |
| 236 | + |
| 237 | + sleep(const Duration(milliseconds: 500)); |
| 238 | + |
| 239 | + await controller.stopImageStream(); |
| 240 | + await controller.dispose(); |
| 241 | + }, |
| 242 | + skip: !Platform.isAndroid, |
| 243 | + ); |
| 244 | +} |
0 commit comments