Skip to content

Commit

Permalink
[camera_web] Add buildPreview implementation (flutter#4190)
Browse files Browse the repository at this point in the history
  • Loading branch information
bselwe authored Jul 26, 2021
1 parent 31c598c commit 530a187
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,24 @@ void main() {
});
});

group('getViewType', () {
testWidgets('returns a correct view type', (tester) async {
const textureId = 1;

final camera = Camera(
textureId: textureId,
window: window,
);

await camera.initialize();

expect(
camera.getViewType(),
equals('plugins.flutter.io/camera_$textureId'),
);
});
});

group('dispose', () {
testWidgets('resets the video element\'s source', (tester) async {
final camera = Camera(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:camera_web/camera_web.dart';
import 'package:camera_web/src/camera.dart';
import 'package:camera_web/src/camera_settings.dart';
import 'package:camera_web/src/types/types.dart';
import 'package:flutter/widgets.dart' as widgets;
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
Expand Down Expand Up @@ -634,10 +635,24 @@ void main() {
);
});

testWidgets('buildPreview throws UnimplementedError', (tester) async {
testWidgets(
'buildPreview returns an HtmlElementView '
'with an appropriate view type', (tester) async {
final camera = Camera(
textureId: cameraId,
window: window,
);

// Save the camera in the camera plugin.
(CameraPlatform.instance as CameraPlugin).cameras[cameraId] = camera;

expect(
() => CameraPlatform.instance.buildPreview(cameraId),
throwsUnimplementedError,
CameraPlatform.instance.buildPreview(cameraId),
isA<widgets.HtmlElementView>().having(
(view) => view.viewType,
'viewType',
camera.getViewType(),
),
);
});

Expand Down
3 changes: 3 additions & 0 deletions packages/camera/camera_web/lib/src/camera.dart
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ class Camera {
}
}

/// Returns the registered view type of the camera.
String getViewType() => _getViewType(textureId);

/// Disposes the camera by stopping the camera stream
/// and reloading the camera source.
void dispose() {
Expand Down
4 changes: 3 additions & 1 deletion packages/camera/camera_web/lib/src/camera_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,9 @@ class CameraPlugin extends CameraPlatform {

@override
Widget buildPreview(int cameraId) {
throw UnimplementedError('buildPreview() is not implemented.');
return HtmlElementView(
viewType: getCamera(cameraId).getViewType(),
);
}

@override
Expand Down

0 comments on commit 530a187

Please sign in to comment.