Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vaind committed Aug 21, 2024
1 parent d913348 commit dd15eaa
Show file tree
Hide file tree
Showing 2 changed files with 216 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ class _SentryUserInteractionWidgetState
return false;
}

final widgetName = element.widget.toStringShort();
final widgetName = element.widget.runtimeType.toString();
if (!widgetName.startsWith('_')) {
final info = {
'name': WidgetUtils.toStringValue(element.widget.key),
Expand Down
274 changes: 215 additions & 59 deletions flutter/test/user_interaction/sentry_user_interaction_widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,24 @@ void main() {

await tapMe(tester, sut, 'btn_1');

Breadcrumb? crumb;
fixture.hub.configureScope((scope) {
crumb = scope.breadcrumbs.last;
});
expect(crumb?.category, 'ui.click');
expect(crumb?.data?['view.id'], 'btn_1');
expect(crumb?.data?['view.class'], 'MaterialButton');
expect(
fixture.getBreadcrumb().data?.replaceHashCodes(),
equals({
'path': [
{'name': 'btn_1', 'element': 'MaterialButton'},
{'element': 'Column'},
{'element': 'Center'},
{'name': '[GlobalKey#00000]', 'element': 'KeyedSubtree'},
{'element': 'MediaQuery'},
{'name': '_ScaffoldSlot.body', 'element': 'LayoutId'},
{'element': 'CustomMultiChildLayout'},
{'element': 'Actions'},
{'element': 'AnimatedBuilder'},
{'element': 'DefaultTextStyle'}
],
'view.id': 'btn_1',
'view.class': 'MaterialButton',
}));
});
});

Expand All @@ -125,11 +136,25 @@ void main() {

await tapMe(tester, sut, 'btn_1');

Breadcrumb? crumb;
fixture.hub.configureScope((scope) {
crumb = scope.breadcrumbs.last;
});
expect(crumb?.data?['label'], 'Button 1');
expect(
fixture.getBreadcrumb().data?.replaceHashCodes(),
equals({
'path': [
{'name': 'btn_1', 'element': 'MaterialButton'},
{'element': 'Column'},
{'element': 'Center'},
{'name': '[GlobalKey#00000]', 'element': 'KeyedSubtree'},
{'element': 'MediaQuery'},
{'name': '_ScaffoldSlot.body', 'element': 'LayoutId'},
{'element': 'CustomMultiChildLayout'},
{'element': 'Actions'},
{'element': 'AnimatedBuilder'},
{'element': 'DefaultTextStyle'}
],
'label': 'Button 1',
'view.id': 'btn_1',
'view.class': 'MaterialButton'
}));
});
});

Expand All @@ -139,11 +164,25 @@ void main() {

await tapMe(tester, sut, 'btn_3');

Breadcrumb? crumb;
fixture.hub.configureScope((scope) {
crumb = scope.breadcrumbs.last;
});
expect(crumb?.data?['label'], 'My Icon');
expect(
fixture.getBreadcrumb().data?.replaceHashCodes(),
equals({
'path': [
{'name': 'btn_3', 'element': 'IconButton'},
{'element': 'Column'},
{'element': 'Center'},
{'name': '[GlobalKey#00000]', 'element': 'KeyedSubtree'},
{'element': 'MediaQuery'},
{'name': '_ScaffoldSlot.body', 'element': 'LayoutId'},
{'element': 'CustomMultiChildLayout'},
{'element': 'Actions'},
{'element': 'AnimatedBuilder'},
{'element': 'DefaultTextStyle'}
],
'label': 'My Icon',
'view.id': 'btn_3',
'view.class': 'IconButton'
}));
});
});

Expand All @@ -153,11 +192,25 @@ void main() {

await tapMe(tester, sut, 'btn_2');

Breadcrumb? crumb;
fixture.hub.configureScope((scope) {
crumb = scope.breadcrumbs.last;
});
expect(crumb?.data?['label'], 'Button 2');
expect(
fixture.getBreadcrumb().data?.replaceHashCodes(),
equals({
'path': [
{'name': 'btn_2', 'element': 'CupertinoButton'},
{'element': 'Column'},
{'element': 'Center'},
{'name': '[GlobalKey#00000]', 'element': 'KeyedSubtree'},
{'element': 'MediaQuery'},
{'name': '_ScaffoldSlot.body', 'element': 'LayoutId'},
{'element': 'CustomMultiChildLayout'},
{'element': 'Actions'},
{'element': 'AnimatedBuilder'},
{'element': 'DefaultTextStyle'}
],
'label': 'Button 2',
'view.id': 'btn_2',
'view.class': 'CupertinoButton'
}));
});
});

Expand All @@ -183,11 +236,25 @@ void main() {

await tapMe(tester, sut, 'btn_5');

Breadcrumb? crumb;
fixture.hub.configureScope((scope) {
crumb = scope.breadcrumbs.last;
});
expect(crumb?.data?['label'], 'Button 5');
expect(
fixture.getBreadcrumb().data?.replaceHashCodes(),
equals({
'path': [
{'name': 'btn_5', 'element': 'ButtonStyleButton'},
{'element': 'Stack'},
{'element': 'Listener'},
{'element': 'RawGestureDetector'},
{'name': 'btn_4', 'element': 'GestureDetector'},
{'element': 'Semantics'},
{'element': 'DefaultTextStyle'},
{'element': 'AnimatedDefaultTextStyle'},
{'element': 'NotificationListener<LayoutChangedNotification>'},
{'element': 'CustomPaint'}
],
'label': 'Button 5',
'view.id': 'btn_5',
'view.class': 'ButtonStyleButton'
}));
});
});

Expand All @@ -197,13 +264,24 @@ void main() {

await tapMe(tester, sut, 'popup_menu_button');

Breadcrumb? crumb;
fixture.hub.configureScope((scope) {
crumb = scope.breadcrumbs.last;
});
expect(crumb?.category, 'ui.click');
expect(crumb?.data?['view.id'], 'popup_menu_button');
expect(crumb?.data?['view.class'], 'PopupMenuButton');
expect(
fixture.getBreadcrumb().data?.replaceHashCodes(),
equals({
'path': [
{'name': 'popup_menu_button', 'element': 'PopupMenuButton'},
{'element': 'Column'},
{'element': 'Center'},
{'name': '[GlobalKey#00000]', 'element': 'KeyedSubtree'},
{'element': 'MediaQuery'},
{'name': '_ScaffoldSlot.body', 'element': 'LayoutId'},
{'element': 'CustomMultiChildLayout'},
{'element': 'Actions'},
{'element': 'AnimatedBuilder'},
{'element': 'DefaultTextStyle'}
],
'view.id': 'popup_menu_button',
'view.class': 'PopupMenuButton'
}));
});
});

Expand All @@ -217,13 +295,56 @@ void main() {

await tapMe(tester, sut, 'popup_menu_item_1');

Breadcrumb? crumb;
fixture.hub.configureScope((scope) {
crumb = scope.breadcrumbs.last;
});
expect(crumb?.category, 'ui.click');
expect(crumb?.data?['view.id'], 'popup_menu_item_1');
expect(crumb?.data?['view.class'], 'PopupMenuItem');
expect(
fixture.getBreadcrumb().data?.replaceHashCodes(),
equals({
'path': [
{'name': 'popup_menu_item_1', 'element': 'PopupMenuItem'},
{'name': '[GlobalKey#00000]', 'element': 'FadeTransition'},
{'element': 'ListBody'},
{'element': 'Padding'},
{'name': '[GlobalKey#00000]', 'element': 'IgnorePointer'},
{'element': 'Semantics'},
{'element': 'Listener'},
{
'name': '[LabeledGlobalKey<RawGestureDetectorState>#00000]',
'element': 'RawGestureDetector'
},
{'element': 'Listener'},
{'element': 'NotificationListener<ScrollMetricsNotification>'}
],
'view.id': 'popup_menu_item_1',
'view.class': 'PopupMenuItem'
}));
});
});

testWidgets('Add crumb for button with tooltip', (tester) async {
await tester.runAsync(() async {
final sut = fixture.getSut(sendDefaultPii: true);

// open the popup menu and wait for the animation to complete
await tapMe(tester, sut, 'tooltip_button');

expect(
fixture.getBreadcrumb().data?.replaceHashCodes(),
equals({
'path': [
{'name': 'tooltip_button', 'element': 'ButtonStyleButton'},
{'element': 'Semantics'},
{'element': 'Listener'},
{'element': 'OverlayPortal'},
{'element': 'Tooltip', 'label': 'Tooltip message.'},
{'element': 'Column'},
{'element': 'Center'},
{'name': '[GlobalKey#00000]', 'element': 'KeyedSubtree'},
{'element': 'MediaQuery'},
{'name': '_ScaffoldSlot.body', 'element': 'LayoutId'}
],
'label': 'Button text',
'view.id': 'tooltip_button',
'view.class': 'ButtonStyleButton'
}));
});
});
});
Expand Down Expand Up @@ -394,6 +515,14 @@ class Fixture {
child: MyApp(),
);
}

Breadcrumb getBreadcrumb() {
late final Breadcrumb crumb;
hub.configureScope((scope) {
crumb = scope.breadcrumbs.last;
});
return crumb;
}
}

class MyApp extends StatelessWidget {
Expand All @@ -420,23 +549,17 @@ class Page1 extends StatelessWidget {
children: [
MaterialButton(
key: Key('btn_1'),
onPressed: () {
// print('button pressed');
},
onPressed: () {},
child: const Text('Button 1'),
),
CupertinoButton(
key: Key('btn_2'),
onPressed: () {
// print('button pressed 2');
},
onPressed: () {},
child: const Text('Button 2'),
),
IconButton(
key: Key('btn_3'),
onPressed: () {
// print('button pressed 3');
},
onPressed: () {},
icon: Icon(
Icons.dark_mode,
semanticLabel: 'My Icon',
Expand All @@ -445,17 +568,13 @@ class Page1 extends StatelessWidget {
Card(
child: GestureDetector(
key: Key('btn_4'),
onTap: () => {
// print('button pressed 4'),
},
onTap: () => {},
child: Stack(
children: [
//fancy card layout
ElevatedButton(
key: Key('btn_5'),
onPressed: () => {
// print('button pressed 5'),
},
onPressed: () => {},
child: const Text('Button 5'),
),
],
Expand All @@ -478,6 +597,14 @@ class Page1 extends StatelessWidget {
),
],
),
Tooltip(
message: 'Tooltip message.',
child: ElevatedButton(
key: ValueKey('tooltip_button'),
onPressed: () {},
child: Text('Button text'),
),
)
],
),
),
Expand All @@ -496,9 +623,7 @@ class Page2 extends StatelessWidget {
children: [
MaterialButton(
key: Key('btn_page_2'),
onPressed: () {
// print('button page 2 pressed');
},
onPressed: () {},
child: const Text('Button Page 2'),
),
],
Expand All @@ -507,3 +632,34 @@ class Page2 extends StatelessWidget {
);
}
}

extension on String {
String replaceHashCodes() => replaceAll(RegExp(r'#[\da-fA-F]{5}'), '#00000');
}

extension on Map<dynamic, dynamic> {
Map<dynamic, dynamic> replaceHashCodes() => map((key, value) {
if (value is String) {
value = value.replaceHashCodes();
} else if (value is Map) {
value = value.replaceHashCodes();
} else if (value is List) {
value = value.replaceHashCodes();
}
return MapEntry(key, value);
});
}

extension on List<dynamic> {
Iterable<dynamic> replaceHashCodes() => map((value) {
if (value is String) {
return value.replaceHashCodes();
} else if (value is Map) {
return value.replaceHashCodes();
} else if (value is List) {
return value.replaceHashCodes();
} else {
return value;
}
});
}

0 comments on commit dd15eaa

Please sign in to comment.