Skip to content

Commit

Permalink
add integration test in sample app
Browse files Browse the repository at this point in the history
  • Loading branch information
denrase committed Jan 29, 2024
1 parent 9683d1d commit 03663b0
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 3 deletions.
65 changes: 64 additions & 1 deletion flutter/example/integration_test/integration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:path_provider/path_provider.dart';
import 'package:sentry_file/sentry_file.dart';
import 'package:sentry_flutter/sentry_flutter.dart';
import 'package:sentry_flutter_example/main.dart';
import 'package:http/http.dart';
Expand All @@ -29,7 +33,7 @@ void main() {
await tester.pumpWidget(SentryScreenshotWidget(
child: DefaultAssetBundle(
bundle: SentryAssetBundle(enableStructuredDataTracing: true),
child: const MyApp(),
child: const MyApp(withNavigatorObserver: false),
)));
},
dsn ?? fakeDsn,
Expand Down Expand Up @@ -141,6 +145,65 @@ void main() {
await transaction.finish();
});

testWidgets(
'sync call in sentry file sets blocked_main_thread to true on main isolate',
(tester) async {
await setupSentryAndApp(tester);
final documentsDir = await getApplicationDocumentsDirectory();
final file = File('${documentsDir.path}/testfile.txt').sentryTrace();

// Start a transaction if there's no active transaction
Sentry.startTransaction(
'File',
'file',
bindToScope: true,
);

file.createSync();

var span = Sentry.getSpan() as dynamic;
var fileSpan = span.children.first;
expect(fileSpan?.data['blocked_main_thread'], true);
});

testWidgets(
'sync call in sentry file sets blocked_main_thread to false on background isolate',
(tester) async {
// Only setup app without sentry.
await tester.pumpWidget(SentryScreenshotWidget(
child: DefaultAssetBundle(
bundle: SentryAssetBundle(enableStructuredDataTracing: true),
child: const MyApp(withNavigatorObserver: false),
)));

final documentsDir = await getApplicationDocumentsDirectory();
final file = File('${documentsDir.path}/testfile.txt').sentryTrace();

final blockedMainThread = await compute<File, bool?>((path) async {
// Isolates do not share state. Init Sentry within isolate.
await Sentry.init((options) {
options.dsn = 'https://[email protected]/1234567';
options.tracesSampleRate = 1.0;
});

// Start a transaction if there's no active transaction
Sentry.startTransaction(
'File',
'file',
bindToScope: true,
);

final sentryFile = file.sentryTrace();
sentryFile.createSync();

var span = Sentry.getSpan() as dynamic;
var fileSpan = span.children.first;
return fileSpan?.data['blocked_main_thread'];
}, file);

expect(blockedMainThread, false);
});

// group('e2e', () {
// var output = find.byKey(const Key('output'));
// late Fixture fixture;
Expand Down
14 changes: 12 additions & 2 deletions flutter/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,23 @@ Future<void> setupSentry(AppRunner appRunner, String dsn,
}

class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
const MyApp({Key? key, this.withNavigatorObserver = true}) : super(key: key);

final bool withNavigatorObserver;

@override
_MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
late bool withNavigatorObserver;

@override
void initState() {
super.initState();
withNavigatorObserver = widget.withNavigatorObserver;
}

@override
Widget build(BuildContext context) {
return feedback.BetterFeedback(
Expand All @@ -109,7 +119,7 @@ class _MyAppState extends State<MyApp> {
builder: (context) => MaterialApp(
navigatorKey: navigatorKey,
navigatorObservers: [
SentryNavigatorObserver(),
if (withNavigatorObserver) SentryNavigatorObserver()
],
theme: Provider.of<ThemeProvider>(context).theme,
home: const MainScaffold(),
Expand Down

0 comments on commit 03663b0

Please sign in to comment.