|
| 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 | +import 'dart:html'; |
| 6 | + |
| 7 | +import 'package:camera_platform_interface/camera_platform_interface.dart'; |
| 8 | +import 'package:camera_web/src/camera_settings.dart'; |
| 9 | +import 'package:camera_web/src/types/types.dart'; |
| 10 | +import 'package:flutter_test/flutter_test.dart'; |
| 11 | +import 'package:integration_test/integration_test.dart'; |
| 12 | +import 'package:mocktail/mocktail.dart'; |
| 13 | + |
| 14 | +import 'helpers/helpers.dart'; |
| 15 | + |
| 16 | +void main() { |
| 17 | + IntegrationTestWidgetsFlutterBinding.ensureInitialized(); |
| 18 | + |
| 19 | + group('CameraSettings', () { |
| 20 | + late Window window; |
| 21 | + late Navigator navigator; |
| 22 | + late MediaDevices mediaDevices; |
| 23 | + late CameraSettings settings; |
| 24 | + |
| 25 | + setUp(() async { |
| 26 | + window = MockWindow(); |
| 27 | + navigator = MockNavigator(); |
| 28 | + mediaDevices = MockMediaDevices(); |
| 29 | + |
| 30 | + when(() => window.navigator).thenReturn(navigator); |
| 31 | + when(() => navigator.mediaDevices).thenReturn(mediaDevices); |
| 32 | + |
| 33 | + settings = CameraSettings()..window = window; |
| 34 | + }); |
| 35 | + |
| 36 | + group('getFacingModeForVideoTrack', () { |
| 37 | + testWidgets( |
| 38 | + 'throws CameraException ' |
| 39 | + 'with notSupported error ' |
| 40 | + 'when there are no media devices', (tester) async { |
| 41 | + when(() => navigator.mediaDevices).thenReturn(null); |
| 42 | + |
| 43 | + expect( |
| 44 | + () => settings.getFacingModeForVideoTrack(MockMediaStreamTrack()), |
| 45 | + throwsA( |
| 46 | + isA<CameraException>().having( |
| 47 | + (e) => e.code, |
| 48 | + 'code', |
| 49 | + CameraErrorCodes.notSupported, |
| 50 | + ), |
| 51 | + ), |
| 52 | + ); |
| 53 | + }); |
| 54 | + |
| 55 | + testWidgets( |
| 56 | + 'returns null ' |
| 57 | + 'when the facing mode is not supported', (tester) async { |
| 58 | + when(mediaDevices.getSupportedConstraints).thenReturn({ |
| 59 | + 'facingMode': false, |
| 60 | + }); |
| 61 | + |
| 62 | + final facingMode = |
| 63 | + settings.getFacingModeForVideoTrack(MockMediaStreamTrack()); |
| 64 | + |
| 65 | + expect( |
| 66 | + facingMode, |
| 67 | + equals(null), |
| 68 | + ); |
| 69 | + }); |
| 70 | + |
| 71 | + group('when the facing mode is supported', () { |
| 72 | + setUp(() { |
| 73 | + when(mediaDevices.getSupportedConstraints).thenReturn({ |
| 74 | + 'facingMode': true, |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + testWidgets( |
| 79 | + 'returns an appropriate facing mode ' |
| 80 | + 'based on the video track settings', (tester) async { |
| 81 | + final videoTrack = MockMediaStreamTrack(); |
| 82 | + |
| 83 | + when(videoTrack.getSettings).thenReturn({'facingMode': 'user'}); |
| 84 | + |
| 85 | + final facingMode = settings.getFacingModeForVideoTrack(videoTrack); |
| 86 | + |
| 87 | + expect( |
| 88 | + facingMode, |
| 89 | + equals('user'), |
| 90 | + ); |
| 91 | + }); |
| 92 | + |
| 93 | + testWidgets( |
| 94 | + 'returns an appropriate facing mode ' |
| 95 | + 'based on the video track capabilities ' |
| 96 | + 'when the facing mode setting is empty', (tester) async { |
| 97 | + final videoTrack = MockMediaStreamTrack(); |
| 98 | + |
| 99 | + when(videoTrack.getSettings).thenReturn({}); |
| 100 | + when(videoTrack.getCapabilities).thenReturn({ |
| 101 | + 'facingMode': ['environment', 'left'] |
| 102 | + }); |
| 103 | + |
| 104 | + final facingMode = settings.getFacingModeForVideoTrack(videoTrack); |
| 105 | + |
| 106 | + expect( |
| 107 | + facingMode, |
| 108 | + equals('environment'), |
| 109 | + ); |
| 110 | + }); |
| 111 | + |
| 112 | + testWidgets( |
| 113 | + 'returns null ' |
| 114 | + 'when the facing mode setting ' |
| 115 | + 'and capabilities are empty', (tester) async { |
| 116 | + final videoTrack = MockMediaStreamTrack(); |
| 117 | + |
| 118 | + when(videoTrack.getSettings).thenReturn({}); |
| 119 | + when(videoTrack.getCapabilities).thenReturn({'facingMode': []}); |
| 120 | + |
| 121 | + final facingMode = settings.getFacingModeForVideoTrack(videoTrack); |
| 122 | + |
| 123 | + expect( |
| 124 | + facingMode, |
| 125 | + equals(null), |
| 126 | + ); |
| 127 | + }); |
| 128 | + |
| 129 | + testWidgets( |
| 130 | + 'returns null ' |
| 131 | + 'when the facing mode setting is empty and ' |
| 132 | + 'the video track capabilities are not supported', (tester) async { |
| 133 | + final videoTrack = MockMediaStreamTrack(); |
| 134 | + |
| 135 | + when(videoTrack.getSettings).thenReturn({}); |
| 136 | + when(videoTrack.getCapabilities).thenThrow(JSNoSuchMethodError()); |
| 137 | + |
| 138 | + final facingMode = settings.getFacingModeForVideoTrack(videoTrack); |
| 139 | + |
| 140 | + expect( |
| 141 | + facingMode, |
| 142 | + equals(null), |
| 143 | + ); |
| 144 | + }); |
| 145 | + |
| 146 | + testWidgets( |
| 147 | + 'throws CameraException ' |
| 148 | + 'with unknown error ' |
| 149 | + 'when getting the video track capabilities ' |
| 150 | + 'throws an unknown error', (tester) async { |
| 151 | + final videoTrack = MockMediaStreamTrack(); |
| 152 | + |
| 153 | + when(videoTrack.getSettings).thenReturn({}); |
| 154 | + when(videoTrack.getCapabilities).thenThrow(Exception('Unknown')); |
| 155 | + |
| 156 | + expect( |
| 157 | + () => settings.getFacingModeForVideoTrack(videoTrack), |
| 158 | + throwsA( |
| 159 | + isA<CameraException>().having( |
| 160 | + (e) => e.code, |
| 161 | + 'code', |
| 162 | + CameraErrorCodes.unknown, |
| 163 | + ), |
| 164 | + ), |
| 165 | + ); |
| 166 | + }); |
| 167 | + }); |
| 168 | + }); |
| 169 | + |
| 170 | + group('mapFacingModeToLensDirection', () { |
| 171 | + testWidgets( |
| 172 | + 'returns front ' |
| 173 | + 'when the facing mode is user', (tester) async { |
| 174 | + expect( |
| 175 | + settings.mapFacingModeToLensDirection('user'), |
| 176 | + equals(CameraLensDirection.front), |
| 177 | + ); |
| 178 | + }); |
| 179 | + |
| 180 | + testWidgets( |
| 181 | + 'returns back ' |
| 182 | + 'when the facing mode is environment', (tester) async { |
| 183 | + expect( |
| 184 | + settings.mapFacingModeToLensDirection('environment'), |
| 185 | + equals(CameraLensDirection.back), |
| 186 | + ); |
| 187 | + }); |
| 188 | + |
| 189 | + testWidgets( |
| 190 | + 'returns external ' |
| 191 | + 'when the facing mode is left', (tester) async { |
| 192 | + expect( |
| 193 | + settings.mapFacingModeToLensDirection('left'), |
| 194 | + equals(CameraLensDirection.external), |
| 195 | + ); |
| 196 | + }); |
| 197 | + |
| 198 | + testWidgets( |
| 199 | + 'returns external ' |
| 200 | + 'when the facing mode is right', (tester) async { |
| 201 | + expect( |
| 202 | + settings.mapFacingModeToLensDirection('right'), |
| 203 | + equals(CameraLensDirection.external), |
| 204 | + ); |
| 205 | + }); |
| 206 | + }); |
| 207 | + }); |
| 208 | +} |
| 209 | + |
| 210 | +class JSNoSuchMethodError implements Exception {} |
0 commit comments