This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[camera] availableCameras() implementation for CameraX re-write #6945
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
1d7f439
Adding availableCameras to camerax camera implementation
1797d1a
Merge branch 'flutter:main' into available_cameras
gmackall 22fd08a
Marking loop variables as nullable in availableCameras
3cc21d9
adding testing for android_camerax availableCameras()
92136ad
marking variable final
eb8a032
removing commented import
04d34c9
cleaning up tests for availableCamera camerax re-write
183a8ba
chaning import
4adc031
merging with upstream
396b673
respondingt to review comments
43dd546
fixing tests
9c3660d
fixing presubmit issues
26562aa
adding changelog message
4194464
formatting changes for presubmit tests
fd6e09b
another formatting change for presubmit checks
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
packages/camera/camera_android_camerax/test/android_camera_camerax_test.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'package:camera_android_camerax/camera_android_camerax.dart'; | ||
import 'package:camera_android_camerax/src/camera_info.dart'; | ||
import 'package:camera_android_camerax/src/camera_selector.dart'; | ||
import 'package:camera_android_camerax/src/process_camera_provider.dart'; | ||
import 'package:camera_platform_interface/camera_platform_interface.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:mockito/annotations.dart'; | ||
import 'package:mockito/mockito.dart'; | ||
|
||
import 'android_camera_camerax_test.mocks.dart'; | ||
|
||
@GenerateMocks(<Type>[ | ||
ProcessCameraProvider, | ||
CameraSelector, | ||
CameraInfo, | ||
]) | ||
void main() { | ||
TestWidgetsFlutterBinding.ensureInitialized(); | ||
|
||
test('Should fetch CameraDescription instances for available cameras', | ||
() async { | ||
// Arrange | ||
final List<dynamic> returnData = <dynamic>[ | ||
<String, dynamic>{ | ||
'name': 'Camera 0', | ||
'lensFacing': 'back', | ||
'sensorOrientation': 0 | ||
}, | ||
<String, dynamic>{ | ||
'name': 'Camera 1', | ||
'lensFacing': 'front', | ||
'sensorOrientation': 90 | ||
} | ||
]; | ||
|
||
//Create mocks to use | ||
final MockProcessCameraProvider mockProcessCameraProvider = | ||
MockProcessCameraProvider(); | ||
final MockCameraSelector mockBackCameraSelector = MockCameraSelector(); | ||
final MockCameraSelector mockFrontCameraSelector = MockCameraSelector(); | ||
final MockCameraInfo mockFrontCameraInfo = MockCameraInfo(); | ||
final MockCameraInfo mockBackCameraInfo = MockCameraInfo(); | ||
AndroidCameraCameraX.registerWith(); | ||
|
||
//Set class level ProcessCameraProvider and camera selectors to created mocks | ||
final AndroidCameraCameraX androidCameraCamerax = AndroidCameraCameraX(); | ||
androidCameraCamerax.backCameraSelector = mockBackCameraSelector; | ||
androidCameraCamerax.frontCameraSelector = mockFrontCameraSelector; | ||
androidCameraCamerax.processCameraProvider = mockProcessCameraProvider; | ||
|
||
//Mock calls to native platform | ||
when(mockProcessCameraProvider.getAvailableCameraInfos()).thenAnswer( | ||
(_) async => <MockCameraInfo>[mockBackCameraInfo, mockFrontCameraInfo]); | ||
when(mockBackCameraSelector.filter(<MockCameraInfo>[mockFrontCameraInfo])) | ||
.thenAnswer((_) async => <MockCameraInfo>[]); | ||
when(mockBackCameraSelector.filter(<MockCameraInfo>[mockBackCameraInfo])) | ||
.thenAnswer((_) async => <MockCameraInfo>[mockBackCameraInfo]); | ||
when(mockFrontCameraSelector.filter(<MockCameraInfo>[mockBackCameraInfo])) | ||
.thenAnswer((_) async => <MockCameraInfo>[]); | ||
when(mockFrontCameraSelector.filter(<MockCameraInfo>[mockFrontCameraInfo])) | ||
.thenAnswer((_) async => <MockCameraInfo>[mockFrontCameraInfo]); | ||
when(mockBackCameraInfo.getSensorRotationDegrees()) | ||
.thenAnswer((_) async => 0); | ||
when(mockFrontCameraInfo.getSensorRotationDegrees()) | ||
.thenAnswer((_) async => 90); | ||
|
||
final List<CameraDescription> cameraDescriptions = | ||
await androidCameraCamerax.availableCameras(); | ||
|
||
expect(cameraDescriptions.length, returnData.length); | ||
for (int i = 0; i < returnData.length; i++) { | ||
final Map<String, Object?> typedData = | ||
(returnData[i] as Map<dynamic, dynamic>).cast<String, Object?>(); | ||
final CameraDescription cameraDescription = CameraDescription( | ||
name: typedData['name']! as String, | ||
lensDirection: (typedData['lensFacing']! as String) == 'front' | ||
? CameraLensDirection.front | ||
: CameraLensDirection.back, | ||
sensorOrientation: typedData['sensorOrientation']! as int, | ||
); | ||
expect(cameraDescriptions[i], cameraDescription); | ||
} | ||
}); | ||
} |
79 changes: 79 additions & 0 deletions
79
packages/camera/camera_android_camerax/test/android_camera_camerax_test.mocks.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Mocks generated by Mockito 5.3.2 from annotations | ||
// in camera_android_camerax/test/android_camera_camerax_test.dart. | ||
// Do not manually edit this file. | ||
|
||
// ignore_for_file: no_leading_underscores_for_library_prefixes | ||
import 'dart:async' as _i3; | ||
|
||
import 'package:camera_android_camerax/src/camera_info.dart' as _i4; | ||
import 'package:camera_android_camerax/src/camera_selector.dart' as _i5; | ||
import 'package:camera_android_camerax/src/process_camera_provider.dart' as _i2; | ||
import 'package:mockito/mockito.dart' as _i1; | ||
|
||
// ignore_for_file: type=lint | ||
// ignore_for_file: avoid_redundant_argument_values | ||
// ignore_for_file: avoid_setters_without_getters | ||
// ignore_for_file: comment_references | ||
// ignore_for_file: implementation_imports | ||
// ignore_for_file: invalid_use_of_visible_for_testing_member | ||
// ignore_for_file: prefer_const_constructors | ||
// ignore_for_file: unnecessary_parenthesis | ||
// ignore_for_file: camel_case_types | ||
// ignore_for_file: subtype_of_sealed_class | ||
|
||
/// A class which mocks [ProcessCameraProvider]. | ||
/// | ||
/// See the documentation for Mockito's code generation for more information. | ||
class MockProcessCameraProvider extends _i1.Mock | ||
implements _i2.ProcessCameraProvider { | ||
MockProcessCameraProvider() { | ||
_i1.throwOnMissingStub(this); | ||
} | ||
|
||
@override | ||
_i3.Future<List<_i4.CameraInfo>> getAvailableCameraInfos() => | ||
(super.noSuchMethod( | ||
Invocation.method( | ||
#getAvailableCameraInfos, | ||
[], | ||
), | ||
returnValue: _i3.Future<List<_i4.CameraInfo>>.value(<_i4.CameraInfo>[]), | ||
) as _i3.Future<List<_i4.CameraInfo>>); | ||
} | ||
|
||
/// A class which mocks [CameraSelector]. | ||
/// | ||
/// See the documentation for Mockito's code generation for more information. | ||
class MockCameraSelector extends _i1.Mock implements _i5.CameraSelector { | ||
MockCameraSelector() { | ||
_i1.throwOnMissingStub(this); | ||
} | ||
|
||
@override | ||
_i3.Future<List<_i4.CameraInfo>> filter(List<_i4.CameraInfo>? cameraInfos) => | ||
(super.noSuchMethod( | ||
Invocation.method( | ||
#filter, | ||
[cameraInfos], | ||
), | ||
returnValue: _i3.Future<List<_i4.CameraInfo>>.value(<_i4.CameraInfo>[]), | ||
) as _i3.Future<List<_i4.CameraInfo>>); | ||
} | ||
|
||
/// A class which mocks [CameraInfo]. | ||
/// | ||
/// See the documentation for Mockito's code generation for more information. | ||
class MockCameraInfo extends _i1.Mock implements _i4.CameraInfo { | ||
MockCameraInfo() { | ||
_i1.throwOnMissingStub(this); | ||
} | ||
|
||
@override | ||
_i3.Future<int> getSensorRotationDegrees() => (super.noSuchMethod( | ||
Invocation.method( | ||
#getSensorRotationDegrees, | ||
[], | ||
), | ||
returnValue: _i3.Future<int>.value(0), | ||
) as _i3.Future<int>); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you keep the setter for this, I'd use it here for consistency. You can move the null check to that method, too.