Skip to content

Fix: Improve Sentry Integration tests #421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Apr 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
98 changes: 68 additions & 30 deletions flutter/test/load_android_image_list_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ import 'package:sentry_flutter/src/sentry_flutter_options.dart';
import 'mocks.dart';

void main() {
const _channel = MethodChannel('sentry_flutter');

TestWidgetsFlutterBinding.ensureInitialized();

var called = false;
late Fixture fixture;

final imageList = [
{
Expand All @@ -26,49 +23,76 @@ void main() {
];

setUp(() {
_channel.setMockMethodCallHandler((MethodCall methodCall) async {
called = true;
fixture = Fixture();
fixture.channel.setMockMethodCallHandler((MethodCall methodCall) async {
return imageList;
});
});

tearDown(() {
_channel.setMockMethodCallHandler(null);
called = false;
fixture.channel.setMockMethodCallHandler(null);
});

test('$LoadAndroidImageListIntegration adds itself to sdk.integrations',
() async {
final sut = fixture.getSut();

sut.call(fixture.hub, fixture.options);

expect(
fixture.options.sdk.integrations
.contains('loadAndroidImageListIntegration'),
true,
);
});

test('Native layer is not called as the event is symbolicated', () async {
final options = SentryFlutterOptions()..dsn = fakeDsn;
final hub = Hub(options);
var called = false;

LoadAndroidImageListIntegration(_channel)(hub, options);
final sut = fixture.getSut();
fixture.channel.setMockMethodCallHandler((MethodCall methodCall) async {
called = true;
return imageList;
});

expect(options.eventProcessors.length, 1);
sut.call(fixture.hub, fixture.options);

await hub.captureException(StateError('error'),
stackTrace: StackTrace.current);
expect(fixture.options.eventProcessors.length, 1);

await fixture.hub
.captureException(StateError('error'), stackTrace: StackTrace.current);

expect(called, false);
});

test('Native layer is not called as the event has no stack traces', () async {
final options = SentryFlutterOptions()..dsn = fakeDsn;
final hub = Hub(options);
var called = false;

LoadAndroidImageListIntegration(_channel)(hub, options);
final sut = fixture.getSut();
fixture.channel.setMockMethodCallHandler((MethodCall methodCall) async {
called = true;
return imageList;
});

sut.call(fixture.hub, fixture.options);

await hub.captureException(StateError('error'));
await fixture.hub.captureException(StateError('error'));

expect(called, false);
});

test('Native layer is called as stack traces are not symbolicated', () async {
final options = SentryFlutterOptions()..dsn = fakeDsn;
final hub = Hub(options);
var called = false;

final sut = fixture.getSut();
fixture.channel.setMockMethodCallHandler((MethodCall methodCall) async {
called = true;
return imageList;
});

LoadAndroidImageListIntegration(_channel)(hub, options);
sut.call(fixture.hub, fixture.options);

await hub.captureException(StateError('error'), stackTrace: '''
await fixture.hub.captureException(StateError('error'), stackTrace: '''
warning: This VM has been configured to produce stack traces that violate the Dart standard.
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
unparsed pid: 30930, tid: 30990, name 1.ui
Expand All @@ -83,23 +107,22 @@ void main() {
});

test('Event processor adds image list to the event', () async {
final options = SentryFlutterOptions()..dsn = fakeDsn;
final hub = Hub(options);
final sut = fixture.getSut();

LoadAndroidImageListIntegration(_channel)(hub, options);
final ep = options.eventProcessors.first;
sut.call(fixture.hub, fixture.options);

final ep = fixture.options.eventProcessors.first;
SentryEvent? event = getEvent();
event = await ep(event);

expect(1, event!.debugMeta!.images.length);
});

test('Event processor asserts image list', () async {
final options = SentryFlutterOptions()..dsn = fakeDsn;
final hub = Hub(options);
final sut = fixture.getSut();

LoadAndroidImageListIntegration(_channel)(hub, options);
final ep = options.eventProcessors.first;
sut.call(fixture.hub, fixture.options);
final ep = fixture.options.eventProcessors.first;
SentryEvent? event = getEvent();
event = await ep(event);

Expand All @@ -125,3 +148,18 @@ SentryEvent getEvent({bool symbolicated = false}) {
);
return SentryEvent(exception: ex);
}

class Fixture {
Fixture() {
hub = Hub(options);
}

final channel = MethodChannel('sentry_flutter');
final options = SentryFlutterOptions(dsn: fakeDsn);

late final Hub hub;

LoadAndroidImageListIntegration getSut() {
return LoadAndroidImageListIntegration(channel);
}
}
10 changes: 10 additions & 0 deletions flutter/test/load_contexts_integrations_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ void main() {
);
}

test('$LoadContextsIntegration adds itself to sdk.integrations', () async {
final integration = fixture.getSut();
integration(fixture.hub, fixture.options);

expect(
fixture.options.sdk.integrations.contains('loadContextsIntegration'),
true,
);
});

test('should apply the loadContextsIntegration eventProcessor', () async {
final integration = fixture.getSut();
integration(fixture.hub, fixture.options);
Expand Down
20 changes: 20 additions & 0 deletions flutter/test/mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,26 @@ class MockPlatform implements Platform {
operatingSystemVersion = osVersion ?? '',
localHostname = hostname ?? '';

factory MockPlatform.android() {
return MockPlatform(os: 'android');
}

factory MockPlatform.iOs() {
return MockPlatform(os: 'ios');
}

factory MockPlatform.macOs() {
return MockPlatform(os: 'macos');
}

factory MockPlatform.windows() {
return MockPlatform(os: 'windows');
}

factory MockPlatform.linux() {
return MockPlatform(os: 'linux');
}

@override
String operatingSystem;

Expand Down
Loading