Skip to content

Commit

Permalink
add opt-in to options for screenshot feature
Browse files Browse the repository at this point in the history
  • Loading branch information
denrase committed Oct 25, 2022
1 parent e477d14 commit a0a094a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 12 deletions.
3 changes: 3 additions & 0 deletions dart/lib/src/sentry_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,9 @@ class SentryOptions {
late SentryStackTraceFactory stackTraceFactory =
SentryStackTraceFactory(this);

/// Automatically attaches a screenshot when capturing an error or exception.
bool attachScreenshot = false;

@internal
late SentryClientAttachmentProcessor clientAttachmentProcessor =
SentryClientAttachmentProcessor();
Expand Down
21 changes: 12 additions & 9 deletions flutter/lib/src/integrations/screenshot_integration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@ class ScreenshotIntegration implements Integration<SentryFlutterOptions> {

@override
FutureOr<void> call(Hub hub, SentryFlutterOptions options) {
// ignore: invalid_use_of_internal_member
options.clientAttachmentProcessor = ScreenshotAttachmentProcessor(() {
try {
/// Flutter >= 2.12 throws if SchedulerBinding.instance isn't initialized.
return SchedulerBinding.instance;
} catch (_) {}
return null;
}, options);
_options = options;
if (options.attachScreenshot) {
// ignore: invalid_use_of_internal_member
options.clientAttachmentProcessor = ScreenshotAttachmentProcessor(() {
try {
/// Flutter >= 2.12 throws if SchedulerBinding.instance isn't initialized.
return SchedulerBinding.instance;
} catch (_) {}
return null;
}, options);
_options = options;
}
options.sdk.addIntegration('screenshotIntegration');
}

@override
Expand Down
3 changes: 3 additions & 0 deletions flutter/lib/src/sentry_flutter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ mixin SentryFlutter {
!platformChecker.isWeb &&
(platform.isAndroid || platform.isIOS || platform.isMacOS)) {
integrations.add(LoadImageListIntegration(channel));
}

if (platform.isAndroid || platform.isIOS || platform.isMacOS) {
integrations.add(ScreenshotIntegration());
}

Expand Down
24 changes: 22 additions & 2 deletions flutter/test/integrations/screenshot_integration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void main() {
});

test('screenshotIntegration creates screenshot processor', () async {
final integration = ScreenshotIntegration();
final integration = fixture.getSut();

await integration(fixture.hub, fixture.options);

Expand All @@ -26,8 +26,23 @@ void main() {
true);
});

test(
'screenshotIntegration does not creates screenshot processor if opt out in options',
() async {
final integration = fixture.getSut();
fixture.options.attachScreenshot = false;

await integration(fixture.hub, fixture.options);

expect(
// ignore: invalid_use_of_internal_member
fixture.options.clientAttachmentProcessor
is ScreenshotAttachmentProcessor,
false);
});

test('screenshotIntegration close resets processor', () async {
final integration = ScreenshotIntegration();
final integration = fixture.getSut();

await integration(fixture.hub, fixture.options);
await integration.close();
Expand All @@ -43,4 +58,9 @@ void main() {
class Fixture {
final hub = MockHub();
final options = SentryFlutterOptions();

ScreenshotIntegration getSut() {
options.attachScreenshot = true;
return ScreenshotIntegration();
}
}
8 changes: 7 additions & 1 deletion flutter/test/sentry_flutter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:sentry_flutter/sentry_flutter.dart';
import 'package:sentry_flutter/src/integrations/integrations.dart';
import 'package:sentry_flutter/src/integrations/screenshot_integration.dart';
import 'package:sentry_flutter/src/version.dart';
import 'mocks.dart';
import 'mocks.mocks.dart';
Expand All @@ -20,12 +21,14 @@ final platformAgnosticIntegrations = [
// These should only be added to Android
final androidIntegrations = [
LoadImageListIntegration,
ScreenshotIntegration,
];

// These should be added to iOS and macOS
final iOsAndMacOsIntegrations = [
LoadImageListIntegration,
LoadContextsIntegration,
ScreenshotIntegration,
];

// These should be added to every platform which has a native integration.
Expand All @@ -48,6 +51,7 @@ void main() {
await SentryFlutter.init(
(options) async {
options.dsn = fakeDsn;
options.attachScreenshot = true;
integrations = options.integrations;
transport = options.transport;
},
Expand Down Expand Up @@ -88,6 +92,7 @@ void main() {
await SentryFlutter.init(
(options) async {
options.dsn = fakeDsn;
options.attachScreenshot = true;
integrations = options.integrations;
transport = options.transport;
},
Expand Down Expand Up @@ -126,6 +131,7 @@ void main() {
await SentryFlutter.init(
(options) async {
options.dsn = fakeDsn;
options.attachScreenshot = true;
integrations = options.integrations;
transport = options.transport;
},
Expand Down Expand Up @@ -364,7 +370,7 @@ void main() {
List<Integration> integrations = [];
Transport transport = MockTransport();

// Tests that Android integrations aren't added on an Android browswer
// Tests that Android integrations aren't added on an Android browser
await SentryFlutter.init(
(options) async {
options.dsn = fakeDsn;
Expand Down

0 comments on commit a0a094a

Please sign in to comment.