Skip to content

Commit

Permalink
add tests for custom resolutions
Browse files Browse the repository at this point in the history
  • Loading branch information
denrase committed Mar 6, 2023
1 parent 77474d3 commit 8f236fd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
Expand Up @@ -4,7 +4,6 @@ import 'dart:typed_data';
import 'dart:ui' as ui show ImageByteFormat;
import 'dart:ui';

import 'package:flutter/widgets.dart';
import 'package:sentry/sentry.dart';
import '../screenshot/sentry_screenshot_widget.dart';
import '../sentry_flutter_options.dart';
Expand Down Expand Up @@ -45,7 +44,8 @@ class ScreenshotEventProcessor extends EventProcessor {

Future<Uint8List?> _createScreenshot() async {
try {
final renderObject = sentryScreenshotWidgetGlobalKey.currentContext?.findRenderObject();
final renderObject =
sentryScreenshotWidgetGlobalKey.currentContext?.findRenderObject();
if (renderObject is RenderRepaintBoundary) {
final pixelRatio = window.devicePixelRatio;
var image = await renderObject.toImage(pixelRatio: pixelRatio);
Expand Down
@@ -1,5 +1,8 @@
@Tags(['canvasKit']) // Web renderer where this test can run

import 'dart:math';
import 'dart:ui';

import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:sentry_flutter/src/event_processor/screenshot_event_processor.dart';
Expand All @@ -9,13 +12,15 @@ import 'package:sentry_flutter/sentry_flutter.dart';

void main() {
late Fixture fixture;

setUp(() {
fixture = Fixture();
TestWidgetsFlutterBinding.ensureInitialized();
});

Future<void> _addScreenshotAttachment(
WidgetTester tester, FlutterRenderer renderer, bool added) async {
WidgetTester tester, FlutterRenderer renderer, bool added,
{int? expectedMaxWidthOrHeight}) async {
// Run with real async https://stackoverflow.com/a/54021863
await tester.runAsync(() async {
final sut = fixture.getSut(renderer);
Expand All @@ -30,6 +35,16 @@ void main() {
await sut.apply(event, hint: hint);

expect(hint.screenshot != null, added);
if (expectedMaxWidthOrHeight != null) {
final bytes = await hint.screenshot?.bytes;
final codec = await instantiateImageCodec(bytes!);
final frameInfo = await codec.getNextFrame();
final image = frameInfo.image;
expect(
max(image.width, image.height).toDouble(),
moreOrLessEquals(expectedMaxWidthOrHeight.toDouble(), epsilon: 1.0),
);
}
});
}

Expand All @@ -51,6 +66,30 @@ void main() {
(tester) async {
await _addScreenshotAttachment(tester, FlutterRenderer.unknown, false);
});

testWidgets('does add screenshot in correct resolution for low',
(tester) async {
final height = SentryScreenshotQuality.low.targetResolution()!;
fixture.options.screenshotQuality = SentryScreenshotQuality.low;
await _addScreenshotAttachment(tester, FlutterRenderer.skia, true,
expectedMaxWidthOrHeight: height);
});

testWidgets('does add screenshot in correct resolution for medium',
(tester) async {
final height = SentryScreenshotQuality.medium.targetResolution()!;
fixture.options.screenshotQuality = SentryScreenshotQuality.medium;
await _addScreenshotAttachment(tester, FlutterRenderer.skia, true,
expectedMaxWidthOrHeight: height);
});

testWidgets('does add screenshot in correct resolution for high',
(tester) async {
final widthOrHeight = SentryScreenshotQuality.high.targetResolution()!;
fixture.options.screenshotQuality = SentryScreenshotQuality.high;
await _addScreenshotAttachment(tester, FlutterRenderer.skia, true,
expectedMaxWidthOrHeight: widthOrHeight);
});
}

class Fixture {
Expand Down

0 comments on commit 8f236fd

Please sign in to comment.