Files
ux/test/camera/camera_channel_test.dart
agra 6d6a871c53 camera: iOS implementation (Phase 2+3)
Native plugin owning AVCaptureSession + AVAssetWriter, mirroring
telegram-iOS's Camera module decomposition. Photo + video capture with
the writer-track transform set from a per-call orientation snapshot
(the three-way preview/capture/device split that camera_avfoundation
can't give us).

Modules:
  CameraPlugin           channels + per-handle instance map
  CameraInstance         session + texture + outputs + recorder
  CameraSession          AVCaptureSession + runtime-error/interrupt obs
  CaptureDevice          front/back discovery, per-device config
  PhotoOutput            AVCapturePhotoOutput, per-shot orientation
  VideoRecorder          AVAssetWriter, lazy inputs, pending-audio queue,
                         stop()/cancel() pair (matches telegram)
  PreviewSink            CVPixelBuffer → FlutterTexture
  AudioSession           setCategory + setActive(true) (only-widen)
  DeviceOrientationBridge

Recorder details:
  - Lazy videoInput/audioInput on first sample, sourceFormatHint:.
  - Audio settings derived from CMAudioFormatDescriptionGet*
    + recommendedAudioSettingsForAssetWriter, gated startWriting.
  - Stop sets stopSampleTime; next sample crossing it triggers
    maybeFinish → finishWriting. No watchdog — telegram pattern.
  - cancel() drops pending audio + cancelWriting + deletes file,
    used by CameraInstance.dispose when teardown finds in-flight
    recording.
  - Diagnostic stream → ux/camera/events {event: "diagnostic"}.

Dart surface extensions over Phase 1:
  - UxCameraValue.audioPermissionGranted
  - UxCameraController.refreshAudioPermission()
  - Static UxCameraController.audioPermissionGranted() /
    openSystemSettings()
  - UxCameraDiagnostic event variant
  - FakeUxCameraBackend.{emitDiagnostic, audioPermission,
    openSettingsCalls}

Tests: 32/32 in test/camera (controller + channel) green.
2026-05-13 16:56:49 +03:00

225 lines
7.1 KiB
Dart

import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:ux/ux.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
group('MethodChannelUxCameraBackend — arg/return parsing', () {
const channel = MethodChannel('ux/camera');
late MethodChannelUxCameraBackend backend;
late List<MethodCall> calls;
setUp(() {
backend = MethodChannelUxCameraBackend();
calls = [];
});
tearDown(() {
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(channel, null);
});
void handle(Object? Function(MethodCall) reply) {
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(channel, (call) async {
calls.add(call);
return reply(call);
});
}
test('availableCameras decodes the platform list', () async {
handle((_) => [
{'id': 'a', 'lens': 'front', 'sensorOrientation': 270},
{'id': 'b', 'lens': 'back', 'sensorOrientation': 90},
]);
final result = await backend.availableCameras();
expect(calls.single.method, 'availableCameras');
expect(result, [
const UxCameraDescription(
id: 'a', lens: UxCameraLens.front, sensorOrientation: 270),
const UxCameraDescription(
id: 'b', lens: UxCameraLens.back, sensorOrientation: 90),
]);
});
test('create sends cameraId/enableAudio/preset and decodes the tuple',
() async {
handle((_) => {
'handle': 42,
'textureId': 7,
'previewSize': {'width': 1920, 'height': 1080},
});
final result = await backend.create(
cameraId: 'cam-1',
enableAudio: true,
preset: UxResolutionPreset.high,
);
expect(calls.single.method, 'create');
expect(calls.single.arguments, {
'cameraId': 'cam-1',
'enableAudio': true,
'preset': 'high',
});
expect(result.handle, 42);
expect(result.textureId, 7);
expect(result.previewSize, const Size(1920, 1080));
});
test('initialize / dispose send only the handle', () async {
handle((_) => null);
await backend.initialize(11);
await backend.disposeInstance(11);
expect(calls.map((c) => c.method).toList(), ['initialize', 'dispose']);
expect(calls.every((c) => (c.arguments as Map)['handle'] == 11), isTrue);
});
test('setDescription returns the new previewSize', () async {
handle((_) => {
'previewSize': {'width': 1280, 'height': 720},
});
final size = await backend.setDescription(7, 'next');
expect(calls.single.method, 'setDescription');
expect(calls.single.arguments, {'handle': 7, 'cameraId': 'next'});
expect(size, const Size(1280, 720));
});
test('setFlashMode encodes the enum', () async {
handle((_) => null);
await backend.setFlashMode(3, UxFlashMode.always);
await backend.setFlashMode(3, UxFlashMode.off);
expect(calls.map((c) => (c.arguments as Map)['mode']).toList(),
['always', 'off']);
});
test('lock/unlockCaptureOrientation encode the orientation string',
() async {
handle((_) => null);
await backend.lockCaptureOrientation(5, DeviceOrientation.portraitUp);
await backend.lockCaptureOrientation(5, DeviceOrientation.landscapeLeft);
await backend.lockCaptureOrientation(5, DeviceOrientation.landscapeRight);
await backend.lockCaptureOrientation(5, DeviceOrientation.portraitDown);
await backend.unlockCaptureOrientation(5);
expect(
calls.take(4).map((c) => (c.arguments as Map)['orientation']).toList(),
['portraitUp', 'landscapeLeft', 'landscapeRight', 'portraitDown'],
);
expect(calls.last.method, 'unlockCaptureOrientation');
});
test('takePicture sends snapshotOrientation and decodes the file path',
() async {
handle((_) => {'path': '/tmp/a.jpg'});
final file = await backend.takePicture(
9, DeviceOrientation.landscapeLeft);
expect(calls.single.method, 'takePicture');
expect(calls.single.arguments,
{'handle': 9, 'snapshotOrientation': 'landscapeLeft'});
expect(file.path, '/tmp/a.jpg');
});
test('startVideoRecording sends snapshotOrientation', () async {
handle((_) => null);
await backend.startVideoRecording(9, DeviceOrientation.landscapeRight);
expect(calls.single.method, 'startVideoRecording');
expect(calls.single.arguments,
{'handle': 9, 'snapshotOrientation': 'landscapeRight'});
});
test('stopVideoRecording decodes the file path', () async {
handle((_) => {'path': '/tmp/v.mp4'});
final file = await backend.stopVideoRecording(9);
expect(calls.single.method, 'stopVideoRecording');
expect(file.path, '/tmp/v.mp4');
});
test('events stream filters by handle and decodes diagnostic events',
() async {
const eventsChannel = EventChannel('ux/camera/events');
final messenger =
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger;
messenger.setMockStreamHandler(
eventsChannel,
MockStreamHandler.inline(
onListen: (_, sink) {
sink.success({
'event': 'diagnostic',
'handle': 4,
'message': 'video input added',
});
sink.success({
'event': 'diagnostic',
'handle': 7,
'message': 'audio input added sr=44100 ch=1',
});
sink.endOfStream();
},
),
);
addTearDown(() => messenger.setMockStreamHandler(eventsChannel, null));
final received = <UxCameraEvent>[];
await backend.events(4).forEach(received.add);
expect(received, hasLength(1));
final e = received.single as UxCameraDiagnostic;
expect(e.handle, 4);
expect(e.message, 'video input added');
});
test('audioPermissionStatus + openSettings round-trip', () async {
var permissionReply = true;
handle((call) {
if (call.method == 'audioPermissionStatus') return permissionReply;
if (call.method == 'openSettings') return null;
return null;
});
expect(await backend.audioPermissionGranted(), isTrue);
permissionReply = false;
expect(await backend.audioPermissionGranted(), isFalse);
await backend.openSettings();
expect(
calls.map((c) => c.method).toList(),
['audioPermissionStatus', 'audioPermissionStatus', 'openSettings'],
);
});
test('PlatformException maps to UxCameraException carrying code/message',
() async {
handle((_) => throw PlatformException(
code: 'device_busy',
message: 'front camera in use',
));
await expectLater(
backend.initialize(1),
throwsA(isA<UxCameraException>()
.having((e) => e.code, 'code', 'device_busy')
.having((e) => e.description, 'description', 'front camera in use')),
);
});
});
}