Skip to content

Commit

Permalink
Sort diagnostics node dependencies so that order is stable (#105319)
Browse files Browse the repository at this point in the history
* Sort diagnostics node dependencies so that order is stable

* use toDescription

* use toStringShort when possible

* sort InheritedElements
  • Loading branch information
kenzieschmoll authored Jun 6, 2022
1 parent 95eb7a3 commit 1b2ee41
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
8 changes: 6 additions & 2 deletions packages/flutter/lib/src/widgets/framework.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4452,8 +4452,12 @@ abstract class Element extends DiagnosticableTree implements BuildContext {
properties.add(DiagnosticsProperty<Key>('key', _widget?.key, showName: false, defaultValue: null, level: DiagnosticLevel.hidden));
_widget?.debugFillProperties(properties);
properties.add(FlagProperty('dirty', value: dirty, ifTrue: 'dirty'));
if (_dependencies != null && _dependencies!.isNotEmpty) {
final List<DiagnosticsNode> diagnosticsDependencies = _dependencies!
final Set<InheritedElement>? deps = _dependencies;
if (deps != null && deps.isNotEmpty) {
final List<InheritedElement> sortedDependencies = deps.toList()
..sort((InheritedElement a, InheritedElement b) =>
a.toStringShort().compareTo(b.toStringShort()));
final List<DiagnosticsNode> diagnosticsDependencies = sortedDependencies
.map((InheritedElement element) => element.widget.toDiagnosticsNode(style: DiagnosticsTreeStyle.sparse))
.toList();
properties.add(DiagnosticsProperty<List<DiagnosticsNode>>('dependencies', diagnosticsDependencies));
Expand Down
44 changes: 44 additions & 0 deletions packages/flutter/test/widgets/framework_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1558,6 +1558,50 @@ void main() {
expect(builder.properties.any((DiagnosticsNode property) => property.name == 'renderObject' && property.value == null), isTrue);
});

testWidgets('debugFillProperties sorts dependencies in alphabetical order', (WidgetTester tester) async {
final DiagnosticPropertiesBuilder builder = DiagnosticPropertiesBuilder();
final TestRenderObjectElement element = TestRenderObjectElement();

final _TestInheritedElement focusTraversalOrder =
_TestInheritedElement(const FocusTraversalOrder(
order: LexicalFocusOrder(''),
child: Placeholder(),
));
final _TestInheritedElement directionality =
_TestInheritedElement(const Directionality(
textDirection: TextDirection.ltr,
child: Placeholder(),
));
final _TestInheritedElement buttonBarTheme =
_TestInheritedElement(const ButtonBarTheme(
data: ButtonBarThemeData(
alignment: MainAxisAlignment.center,
),
child: Placeholder(),
));

// Dependencies are added out of alphabetical order.
element
..dependOnInheritedElement(focusTraversalOrder)
..dependOnInheritedElement(directionality)
..dependOnInheritedElement(buttonBarTheme);

// Dependencies will be sorted by [debugFillProperties].
element.debugFillProperties(builder);

expect(
builder.properties.any((DiagnosticsNode property) => property.name == 'dependencies' && property.value != null),
isTrue,
);
final DiagnosticsProperty<List<DiagnosticsNode>> dependenciesProperty =
builder.properties.firstWhere((DiagnosticsNode property) => property.name == 'dependencies') as DiagnosticsProperty<List<DiagnosticsNode>>;
expect(dependenciesProperty, isNotNull);

final List<DiagnosticsNode> dependencies = dependenciesProperty.value!;
expect(dependencies.length, equals(3));
expect(dependencies.toString(), '[ButtonBarTheme, Directionality, FocusTraversalOrder]');
});

testWidgets('BuildOwner.globalKeyCount keeps track of in-use global keys', (WidgetTester tester) async {
final int initialCount = tester.binding.buildOwner!.globalKeyCount;
final GlobalKey key1 = GlobalKey();
Expand Down

0 comments on commit 1b2ee41

Please sign in to comment.