Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add beforeScreenshot to flutter options
  • Loading branch information
denrase committed Jan 8, 2024
commit 5fbaa72f2df2bcdffd0a820f6615077c954a107f
26 changes: 26 additions & 0 deletions flutter/lib/src/event_processor/screenshot_event_processor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,32 @@ class ScreenshotEventProcessor implements EventProcessor {
_hasSentryScreenshotWidget) {
return event;
}
final beforeScreenshot = _options.beforeScreenshot;
if (beforeScreenshot != null) {
try {
final result = beforeScreenshot(event, hint: hint);
bool takeScreenshot;
if (result is Future<bool>) {
takeScreenshot = await result;
} else {
takeScreenshot = result;
}
if (!takeScreenshot) {
return event;
}
} catch (exception, stackTrace) {
_options.logger(
SentryLevel.error,
'The beforeScreenshot callback threw an exception',
exception: exception,
stackTrace: stackTrace,
);
// ignore: invalid_use_of_internal_member
if (_options.automatedTestMode) {
rethrow;
}
}
}

final renderer = _options.rendererWrapper.getRenderer();

Expand Down
23 changes: 10 additions & 13 deletions flutter/lib/src/native/cocoa/binding.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37603,8 +37603,7 @@ class ObjCBlock_bool_ObjCObject_ffiUnsignedLong_bool extends _ObjCBlockBase {
ObjCBlock_bool_ObjCObject_ffiUnsignedLong_bool.fromFunctionPointer(
SentryCocoa lib,
ffi.Pointer<
ffi
.NativeFunction<
ffi.NativeFunction<
ffi.Bool Function(ffi.Pointer<ObjCObject> arg0,
ffi.UnsignedLong arg1, ffi.Pointer<ffi.Bool> arg2)>>
ptr)
Expand Down Expand Up @@ -42032,17 +42031,15 @@ class ObjCBlock_bool_ObjCObject_bool extends _ObjCBlockBase {
ffi.Pointer<ffi.Bool> arg1)>>
ptr)
: this._(
lib
._newBlock1(
_cFuncTrampoline ??= ffi.Pointer.fromFunction<
ffi.Bool Function(
ffi.Pointer<_ObjCBlock> block,
ffi.Pointer<ObjCObject> arg0,
ffi.Pointer<ffi.Bool> arg1)>(
_ObjCBlock_bool_ObjCObject_bool_fnPtrTrampoline,
false)
.cast(),
ptr.cast()),
lib._newBlock1(
_cFuncTrampoline ??= ffi.Pointer.fromFunction<
ffi.Bool Function(
ffi.Pointer<_ObjCBlock> block,
ffi.Pointer<ObjCObject> arg0,
ffi.Pointer<ffi.Bool> arg1)>(
_ObjCBlock_bool_ObjCObject_bool_fnPtrTrampoline, false)
.cast(),
ptr.cast()),
lib);
static ffi.Pointer<ffi.Void>? _cFuncTrampoline;

Expand Down
9 changes: 9 additions & 0 deletions flutter/lib/src/sentry_flutter_options.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:meta/meta.dart';
import 'package:sentry/sentry.dart';
import 'package:flutter/widgets.dart';
Expand Down Expand Up @@ -288,4 +290,11 @@ class SentryFlutterOptions extends SentryOptions {

/// The [navigatorKey] is used to add information of the currently used locale to the contexts.
GlobalKey<NavigatorState>? navigatorKey;

BeforeScreenshotCallback? beforeScreenshot;
}

typedef BeforeScreenshotCallback = FutureOr<bool> Function(
SentryEvent event, {
Hint? hint,
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ void main() {
TestWidgetsFlutterBinding.ensureInitialized();
late Fixture fixture;

late SentryEvent event;
late Hint hint;

setUp(() {
fixture = Fixture();
});
Expand All @@ -34,8 +37,8 @@ void main() {
textDirection: TextDirection.ltr)));

final throwable = Exception();
final event = SentryEvent(throwable: throwable);
final hint = Hint();
event = SentryEvent(throwable: throwable);
hint = Hint();
await sut.apply(event, hint: hint);

expect(hint.screenshot != null, added);
Expand Down Expand Up @@ -91,6 +94,87 @@ void main() {
await _addScreenshotAttachment(tester, null,
added: true, isWeb: false, expectedMaxWidthOrHeight: widthOrHeight);
});

group('beforeScreenshot', () {
testWidgets('does add screenshot if beforeScreenshot returns true',
(tester) async {
fixture.options.beforeScreenshot = (SentryEvent event, {Hint? hint}) {
return true;
};
await _addScreenshotAttachment(tester, FlutterRenderer.canvasKit,
added: true, isWeb: false);
});

testWidgets('does add screenshot if async beforeScreenshot returns true',
(tester) async {
fixture.options.beforeScreenshot =
(SentryEvent event, {Hint? hint}) async {
await Future<void>.delayed(Duration(milliseconds: 1));
return true;
};
await _addScreenshotAttachment(tester, FlutterRenderer.canvasKit,
added: true, isWeb: false);
});

testWidgets('does not add screenshot if beforeScreenshot returns false',
(tester) async {
fixture.options.beforeScreenshot = (SentryEvent event, {Hint? hint}) {
return false;
};
await _addScreenshotAttachment(tester, FlutterRenderer.canvasKit,
added: false, isWeb: false);
});

testWidgets(
'does not add screenshot if async beforeScreenshot returns false',
(tester) async {
fixture.options.beforeScreenshot =
(SentryEvent event, {Hint? hint}) async {
await Future<void>.delayed(Duration(milliseconds: 1));
return false;
};
await _addScreenshotAttachment(tester, FlutterRenderer.canvasKit,
added: false, isWeb: false);
});

testWidgets('does add screenshot if beforeScreenshot throws',
(tester) async {
fixture.options.beforeScreenshot = (SentryEvent event, {Hint? hint}) {
throw Error();
};
await _addScreenshotAttachment(tester, FlutterRenderer.canvasKit,
added: true, isWeb: false);
});

testWidgets('does add screenshot if async beforeScreenshot throws',
(tester) async {
fixture.options.beforeScreenshot =
(SentryEvent event, {Hint? hint}) async {
await Future<void>.delayed(Duration(milliseconds: 1));
throw Error();
};
await _addScreenshotAttachment(tester, FlutterRenderer.canvasKit,
added: true, isWeb: false);
});

testWidgets('passes event & hint to beforeScreenshot callback',
(tester) async {
SentryEvent? beforeScreenshotEvent;
Hint? beforeScreenshotHint;

fixture.options.beforeScreenshot = (SentryEvent event, {Hint? hint}) {
beforeScreenshotEvent = event;
beforeScreenshotHint = hint;
return true;
};

await _addScreenshotAttachment(tester, FlutterRenderer.canvasKit,
added: true, isWeb: false);

expect(beforeScreenshotEvent, event);
expect(beforeScreenshotHint, hint);
});
});
}

class Fixture {
Expand Down