Skip to content

Commit

Permalink
entrich event contexts app with text scale
Browse files Browse the repository at this point in the history
  • Loading branch information
denrase committed Feb 20, 2024
1 parent 732a7b4 commit a2d3c75
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 5 deletions.
9 changes: 9 additions & 0 deletions dart/lib/src/protocol/sentry_app.dart
Expand Up @@ -19,6 +19,7 @@ class SentryApp {
this.appMemory,
this.inForeground,
this.viewNames,
this.textScale,
});

/// Human readable application name, as it appears on the platform.
Expand Down Expand Up @@ -52,6 +53,9 @@ class SentryApp {
/// The names of the currently visible views.
final List<String>? viewNames;

/// The global text scale of the [SentryWidget]
final String? textScale;

/// Deserializes a [SentryApp] from JSON [Map].
factory SentryApp.fromJson(Map<String, dynamic> data) {
final viewNamesJson = data['view_names'] as List<dynamic>?;
Expand All @@ -68,6 +72,7 @@ class SentryApp {
appMemory: data['app_memory'],
inForeground: data['in_foreground'],
viewNames: viewNamesJson?.map((e) => e as String).toList(),
textScale: data['text_scale'],
);
}

Expand All @@ -84,6 +89,7 @@ class SentryApp {
if (appMemory != null) 'app_memory': appMemory!,
if (inForeground != null) 'in_foreground': inForeground!,
if (viewNames != null && viewNames!.isNotEmpty) 'view_names': viewNames!,
if (textScale != null) 'text_scale': textScale!,
};
}

Expand All @@ -98,6 +104,7 @@ class SentryApp {
appMemory: appMemory,
inForeground: inForeground,
viewNames: viewNames,
textScale: textScale,
);

SentryApp copyWith({
Expand All @@ -111,6 +118,7 @@ class SentryApp {
int? appMemory,
bool? inForeground,
List<String>? viewNames,
String? textScale,
}) =>
SentryApp(
name: name ?? this.name,
Expand All @@ -123,5 +131,6 @@ class SentryApp {
appMemory: appMemory ?? this.appMemory,
inForeground: inForeground ?? this.inForeground,
viewNames: viewNames ?? this.viewNames,
textScale: textScale ?? this.textScale,
);
}
6 changes: 6 additions & 0 deletions dart/test/protocol/sentry_app_test.dart
Expand Up @@ -15,6 +15,7 @@ void main() {
deviceAppHash: 'fixture-deviceAppHash',
inForeground: true,
viewNames: ['fixture-viewName', 'fixture-viewName2'],
textScale: 'fixture-textScale',
);

final sentryAppJson = <String, dynamic>{
Expand All @@ -27,6 +28,7 @@ void main() {
'device_app_hash': 'fixture-deviceAppHash',
'in_foreground': true,
'view_names': ['fixture-viewName', 'fixture-viewName2'],
'text_scale': 'fixture-textScale',
};

group('json', () {
Expand All @@ -42,6 +44,7 @@ void main() {
expect(json['device_app_hash'], 'fixture-deviceAppHash');
expect(json['in_foreground'], true);
expect(json['view_names'], ['fixture-viewName', 'fixture-viewName2']);
expect(json['text_scale'], 'fixture-textScale');
});
test('fromJson', () {
final sentryApp = SentryApp.fromJson(sentryAppJson);
Expand All @@ -56,6 +59,7 @@ void main() {
expect(json['device_app_hash'], 'fixture-deviceAppHash');
expect(json['in_foreground'], true);
expect(json['view_names'], ['fixture-viewName', 'fixture-viewName2']);
expect(json['text_scale'], 'fixture-textScale');
});
});

Expand Down Expand Up @@ -86,6 +90,7 @@ void main() {
deviceAppHash: 'hash1',
inForeground: true,
viewNames: ['screen1'],
textScale: 'textScale1'
);

expect('name1', copy.name);
Expand All @@ -97,6 +102,7 @@ void main() {
expect('hash1', copy.deviceAppHash);
expect(true, copy.inForeground);
expect(['screen1'], copy.viewNames);
expect('textScale1', copy.textScale);
});
});
}
1 change: 1 addition & 0 deletions flutter/example/lib/main.dart
Expand Up @@ -78,6 +78,7 @@ Future<void> setupSentry(AppRunner appRunner, String dsn,

options.maxRequestBodySize = MaxRequestBodySize.always;
options.maxResponseBodySize = MaxResponseBodySize.always;
options.navigatorKey = navigatorKey;

_isIntegrationTest = isIntegrationTest;
if (_isIntegrationTest) {
Expand Down
Expand Up @@ -50,7 +50,9 @@ class FlutterEnricherEventProcessor implements EventProcessor {

final app = contexts.app;
if (app != null) {
contexts.app = _appWithCurrentRouteViewName(app);
contexts.app = _appWithCurrentRouteViewName(app).copyWith(
textScale: _retrieveWidgetTextScale(_options.navigatorKey),
);
}

// Flutter has a lot of Accessibility Settings available and exposes them
Expand Down Expand Up @@ -260,11 +262,26 @@ class FlutterEnricherEventProcessor implements EventProcessor {
}

Locale? _retrieveWidgetLocale(GlobalKey<NavigatorState>? navigatorKey) {
final BuildContext? context = navigatorKey?.currentContext;
if (context != null) {
return Localizations.maybeLocaleOf(context);
final context = navigatorKey?.currentContext;
if (context == null) {
return null;
}
return Localizations.maybeLocaleOf(context);
}

String? _retrieveWidgetTextScale(GlobalKey<NavigatorState>? navigatorKey) {
final context = navigatorKey?.currentContext;
if (context == null) {
return null;
}
final textScaleFactor =
MediaQuery.maybeTextScalerOf(context)?.textScaleFactor;
if (textScaleFactor == null) {
return null;
}
return null;
return textScaleFactor == 1.0
? 'no scaling'
: 'linear (${textScaleFactor}x)';
}
}

Expand Down
Expand Up @@ -138,6 +138,30 @@ void main() {
expect(event?.contexts.culture?.locale, 'de-DE');
});

testWidgets(
'GIVEN MaterialApp WHEN setting sentryNavigatorKey THEN enrich event app with textScale',
(WidgetTester tester) async {
GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();

await tester.pumpWidget(MaterialApp(
navigatorKey: navigatorKey,
home: Material(),
));

final enricher = fixture.getSut(
binding: () => tester.binding,
optionsBuilder: (options) {
options.navigatorKey = navigatorKey;
return options;
},
);

tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
final event = await enricher.apply(SentryEvent());

expect(event?.contexts.app?.textScale, 'no scaling');
});

testWidgets('app context in foreground', (WidgetTester tester) async {
final enricher = fixture.getSut(
binding: () => tester.binding,
Expand Down

0 comments on commit a2d3c75

Please sign in to comment.