From 0962dd6a9433ba934f57a7097e66506032358200 Mon Sep 17 00:00:00 2001 From: Ian Hickson Date: Fri, 17 Feb 2017 12:56:25 -0800 Subject: [PATCH] More better toStrings and more vigorous smoke testing of gallery (#8237) * More better toStrings and more vigorous smoke testing of gallery * Update scroll_controller.dart * Update sliver.dart --- examples/flutter_gallery/test/smoke_test.dart | 72 +++++++++++++++++-- .../flutter/lib/src/material/app_bar.dart | 5 ++ .../flutter/lib/src/rendering/binding.dart | 2 +- .../flutter/lib/src/rendering/editable.dart | 2 +- packages/flutter/lib/src/rendering/layer.dart | 10 ++- .../flutter/lib/src/rendering/object.dart | 31 ++++++-- .../flutter/lib/src/rendering/paragraph.dart | 2 +- .../rendering/sliver_persistent_header.dart | 4 +- .../lib/src/widgets/scroll_controller.dart | 17 +++++ packages/flutter/lib/src/widgets/sliver.dart | 18 +++++ .../foundation/service_extensions_test.dart | 1 - 11 files changed, 147 insertions(+), 17 deletions(-) diff --git a/examples/flutter_gallery/test/smoke_test.dart b/examples/flutter_gallery/test/smoke_test.dart index 47d44c1d4d..e7ea527faa 100644 --- a/examples/flutter_gallery/test/smoke_test.dart +++ b/examples/flutter_gallery/test/smoke_test.dart @@ -3,6 +3,7 @@ // found in the LICENSE file. import 'dart:collection' show LinkedHashSet; +import 'dart:math' as math; import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; @@ -25,6 +26,36 @@ Finder findGalleryItemByRouteName(WidgetTester tester, String routeName) { }); } +int errors = 0; + +void reportToStringError(String name, String route, int lineNumber, List lines, String message) { + // If you're on line 12, then it has index 11. + // If you want 1 line before and 1 line after, then you want lines with index 10, 11, and 12. + // That's (lineNumber-1)-margin .. (lineNumber-1)+margin, or lineNumber-(margin+1) .. lineNumber+(margin-1) + int margin = 5; + int firstLine = math.max(0, lineNumber - margin); + int lastLine = math.min(lines.length, lineNumber + margin); + print('$name : $route : line $lineNumber of ${lines.length} : $message; nearby lines were:\n ${lines.sublist(firstLine, lastLine).join("\n ")}'); + errors += 1; +} + +void verifyToStringOutput(String name, String route, String testString) { + int lineNumber = 0; + List lines = testString.split('\n'); + if (!testString.endsWith('\n')) + reportToStringError(name, route, lines.length, lines, 'does not end with a line feed'); + for (String line in lines) { + lineNumber += 1; + if (line == '' && lineNumber != lines.length) { + reportToStringError(name, route, lineNumber, lines, 'found empty line'); + } else if (line.contains('Instance of ')) { + reportToStringError(name, route, lineNumber, lines, 'found a class that does not have its own toString'); + } else if (line.endsWith(' ')) { + reportToStringError(name, route, lineNumber, lines, 'found a line with trailing whitespace'); + } + } +} + // Start a gallery demo and then go back. This function assumes that the // we're starting on the home route and that the submenu that contains // the item for a demo that pushes route 'routeName' is already open. @@ -33,12 +64,43 @@ Future smokeDemo(WidgetTester tester, String routeName) async { final Finder menuItem = findGalleryItemByRouteName(tester, routeName); expect(menuItem, findsOneWidget); + // Don't use pumpUntilNoTransientCallbacks in this function, because some of + // the smoketests have infinitely-running animations (e.g. the progress + // indicators demo). + await tester.tap(menuItem); await tester.pump(); // Launch the demo. - await tester.pump(const Duration(seconds: 1)); // Wait until the demo has opened. - + await tester.pump(const Duration(milliseconds: 400)); // Wait until the demo has opened. expect(find.text(kCaption), findsNothing); - await tester.pump(const Duration(seconds: 1)); // Leave the demo on the screen briefly for manual testing. + + // Leave the demo on the screen briefly for manual testing. + await tester.pump(const Duration(milliseconds: 400)); + + // Scroll the demo around a bit. + await tester.flingFrom(const Point(400.0, 300.0), const Offset(-100.0, 0.0), 500.0); + await tester.flingFrom(const Point(400.0, 300.0), const Offset(0.0, -100.0), 500.0); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 50)); + await tester.pump(const Duration(milliseconds: 200)); + await tester.pump(const Duration(milliseconds: 400)); + + // Verify that the dumps are pretty. + verifyToStringOutput('debugDumpApp', routeName, WidgetsBinding.instance.renderViewElement.toStringDeep()); + verifyToStringOutput('debugDumpRenderTree', routeName, RendererBinding.instance?.renderView?.toStringDeep()); + verifyToStringOutput('debugDumpLayerTree', routeName, RendererBinding.instance?.renderView?.debugLayer?.toStringDeep()); + + // Scroll the demo around a bit more. + await tester.flingFrom(const Point(400.0, 300.0), const Offset(-200.0, 0.0), 500.0); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 50)); + await tester.pump(const Duration(milliseconds: 200)); + await tester.pump(const Duration(milliseconds: 400)); + await tester.flingFrom(const Point(400.0, 300.0), const Offset(100.0, 0.0), 500.0); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 400)); + await tester.flingFrom(const Point(400.0, 300.0), const Offset(0.0, 400.0), 1000.0); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 400)); // Go back Finder backButton = find.byTooltip('Back'); @@ -46,7 +108,7 @@ Future smokeDemo(WidgetTester tester, String routeName) async { await tester.tap(backButton); await tester.pump(); // Start the pop "back" operation. await tester.pump(); // Complete the willPop() Future. - await tester.pump(const Duration(seconds: 1)); // Wait until it has finished. + await tester.pump(const Duration(milliseconds: 400)); // Wait until it has finished. return null; } @@ -71,6 +133,8 @@ Future runSmokeTest(WidgetTester tester) async { tester.binding.debugAssertNoTransientCallbacks('A transient callback was still active after leaving route $routeName'); } + expect(errors, 0); + Finder navigationMenuButton = find.byTooltip('Open navigation menu'); expect(navigationMenuButton, findsOneWidget); await tester.tap(navigationMenuButton); diff --git a/packages/flutter/lib/src/material/app_bar.dart b/packages/flutter/lib/src/material/app_bar.dart index 2b13d847ab..4eb35030fa 100644 --- a/packages/flutter/lib/src/material/app_bar.dart +++ b/packages/flutter/lib/src/material/app_bar.dart @@ -555,6 +555,11 @@ class _SliverAppBarDelegate extends SliverPersistentHeaderDelegate { || expandedHeight != oldDelegate.expandedHeight || topPadding != oldDelegate.topPadding; } + + @override + String toString() { + return '$runtimeType#$hashCode(topPadding: ${topPadding.toStringAsFixed(1)}, bottomHeight: ${_bottomHeight.toStringAsFixed(1)}, ...)'; + } } class SliverAppBar extends StatelessWidget { diff --git a/packages/flutter/lib/src/rendering/binding.dart b/packages/flutter/lib/src/rendering/binding.dart index 05ddda40cf..0a586a1fe2 100644 --- a/packages/flutter/lib/src/rendering/binding.dart +++ b/packages/flutter/lib/src/rendering/binding.dart @@ -266,7 +266,7 @@ void debugDumpRenderTree() { /// Prints a textual representation of the entire layer tree. void debugDumpLayerTree() { - debugPrint(RendererBinding.instance?.renderView?.layer?.toStringDeep()); + debugPrint(RendererBinding.instance?.renderView?.debugLayer?.toStringDeep()); } /// Prints a textual representation of the entire semantics tree. diff --git a/packages/flutter/lib/src/rendering/editable.dart b/packages/flutter/lib/src/rendering/editable.dart index c74ee054a6..184ef091c6 100644 --- a/packages/flutter/lib/src/rendering/editable.dart +++ b/packages/flutter/lib/src/rendering/editable.dart @@ -403,6 +403,6 @@ class RenderEditable extends RenderBox { return '$prefix \u2558\u2550\u2566\u2550\u2550 text \u2550\u2550\u2550\n' '${text.toString("$prefix \u2551 ")}' // TextSpan includes a newline '$prefix \u255A\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\n' - '$prefix\n'; + '${prefix.trimRight()}\n'; } } diff --git a/packages/flutter/lib/src/rendering/layer.dart b/packages/flutter/lib/src/rendering/layer.dart index d6fa24ac63..d115fca4da 100644 --- a/packages/flutter/lib/src/rendering/layer.dart +++ b/packages/flutter/lib/src/rendering/layer.dart @@ -87,9 +87,13 @@ abstract class Layer { List description = []; debugFillDescription(description); result += description.map((String description) => "$descriptionPrefix$description\n").join(); - if (childrenDescription == '') - result += '$prefixOtherLines\n'; - result += childrenDescription; + if (childrenDescription == '') { + String prefix = prefixOtherLines.trimRight(); + if (prefix != '') + result += '$prefix\n'; + } else { + result += childrenDescription; + } return result; } diff --git a/packages/flutter/lib/src/rendering/object.dart b/packages/flutter/lib/src/rendering/object.dart index f0f2d8031f..d1d7281004 100644 --- a/packages/flutter/lib/src/rendering/object.dart +++ b/packages/flutter/lib/src/rendering/object.dart @@ -1903,12 +1903,31 @@ abstract class RenderObject extends AbstractNode implements HitTestTarget { OffsetLayer _layer; /// The compositing layer that this render object uses to repaint. /// - /// Call only when [isRepaintBoundary] is true. + /// Call only when [isRepaintBoundary] is true and the render object has + /// already painted. + /// + /// To access the layer in debug code, even when it might be inappropriate to + /// access it (e.g. because it is dirty), consider [debugLayer]. OffsetLayer get layer { assert(isRepaintBoundary); assert(!_needsPaint); return _layer; } + /// In debug mode, the compositing layer that this render object uses to repaint. + /// + /// This getter is intended for debugging purposes only. In release builds, it + /// always returns null. In debug builds, it returns the layer even if the layer + /// is dirty. + /// + /// For production code, consider [layer]. + OffsetLayer get debugLayer { + OffsetLayer result; + assert(() { + result = _layer; + return true; + }); + return result; + } bool _needsCompositingBitsUpdate = false; // set to true when a child is added /// Mark the compositing state for this render object as dirty. @@ -2422,9 +2441,13 @@ abstract class RenderObject extends AbstractNode implements HitTestTarget { .expand((String description) => debugWordWrap(description, 65, wrapIndent: ' ')) .map((String line) => "$descriptionPrefix$line\n") .join(); - if (childrenDescription == '') - result += '$prefixOtherLines\n'; - result += childrenDescription; + if (childrenDescription == '') { + String prefix = prefixOtherLines.trimRight(); + if (prefix != '') + result += '$prefix\n'; + } else { + result += childrenDescription; + } _debugActiveLayout = debugPreviousActiveLayout; return result; } diff --git a/packages/flutter/lib/src/rendering/paragraph.dart b/packages/flutter/lib/src/rendering/paragraph.dart index 645aeb7861..fb1c83ca2c 100644 --- a/packages/flutter/lib/src/rendering/paragraph.dart +++ b/packages/flutter/lib/src/rendering/paragraph.dart @@ -342,6 +342,6 @@ class RenderParagraph extends RenderBox { return '$prefix \u2558\u2550\u2566\u2550\u2550 text \u2550\u2550\u2550\n' '${text.toString("$prefix \u2551 ")}' // TextSpan includes a newline '$prefix \u255A\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\n' - '$prefix\n'; + '${prefix.trimRight()}\n'; } } diff --git a/packages/flutter/lib/src/rendering/sliver_persistent_header.dart b/packages/flutter/lib/src/rendering/sliver_persistent_header.dart index cdf10cb2a8..85d39010e4 100644 --- a/packages/flutter/lib/src/rendering/sliver_persistent_header.dart +++ b/packages/flutter/lib/src/rendering/sliver_persistent_header.dart @@ -178,12 +178,12 @@ abstract class RenderSliverPersistentHeader extends RenderSliver with RenderObje try { description.add('maxExtent: ${maxExtent.toStringAsFixed(1)}'); } catch (e) { - description.add('maxExtent: EXCEPTION (${e.runtimeType}) WHILE COMPUTING MAX EXTENT'); + description.add('maxExtent: EXCEPTION (${e.runtimeType})'); } try { description.add('child position: ${childMainAxisPosition(child).toStringAsFixed(1)}'); } catch (e) { - description.add('child position: EXCEPTION (${e.runtimeType}) WHILE COMPUTING CHILD POSITION'); + description.add('child position: EXCEPTION (${e.runtimeType})'); } } } diff --git a/packages/flutter/lib/src/widgets/scroll_controller.dart b/packages/flutter/lib/src/widgets/scroll_controller.dart index ab00d815c6..dfd287f633 100644 --- a/packages/flutter/lib/src/widgets/scroll_controller.dart +++ b/packages/flutter/lib/src/widgets/scroll_controller.dart @@ -118,4 +118,21 @@ class ScrollController { oldPosition: oldPosition, ); } + + @override + String toString() { + final StringBuffer result = new StringBuffer(); + result.write('$runtimeType#$hashCode('); + if (initialScrollOffset != 0.0) + result.write('initialScrollOffset: ${initialScrollOffset.toStringAsFixed(1)}, '); + if (_positions.isEmpty) { + result.write('no clients'); + } else if (_positions.length == 1) { + result.write('one client, offset $offset'); + } else { + result.write('${_positions.length} clients'); + } + result.write(')'); + return result.toString(); + } } diff --git a/packages/flutter/lib/src/widgets/sliver.dart b/packages/flutter/lib/src/widgets/sliver.dart index c0f3aaad65..f59b436993 100644 --- a/packages/flutter/lib/src/widgets/sliver.dart +++ b/packages/flutter/lib/src/widgets/sliver.dart @@ -39,6 +39,24 @@ abstract class SliverChildDelegate { ) => null; bool shouldRebuild(@checked SliverChildDelegate oldDelegate); + + @override + String toString() { + List description = []; + debugFillDescription(description); + return '$runtimeType#$hashCode(${description.join(", ")})'; + } + + @protected + void debugFillDescription(List description) { + try { + final int children = estimatedChildCount; + if (children != null) + description.add('estimated child count: $children'); + } catch (e) { + description.add('estimated child count: EXCEPTION (${e.runtimeType})'); + } + } } class SliverChildBuilderDelegate extends SliverChildDelegate { diff --git a/packages/flutter/test/foundation/service_extensions_test.dart b/packages/flutter/test/foundation/service_extensions_test.dart index f905997d27..3d02b09376 100644 --- a/packages/flutter/test/foundation/service_extensions_test.dart +++ b/packages/flutter/test/foundation/service_extensions_test.dart @@ -130,7 +130,6 @@ void main() { r' window size: Size\(800\.0, 600\.0\) \(in physical pixels\)\n' r' device pixel ratio: 1\.0 \(physical pixels per logical pixel\)\n' r' configuration: Size\(800\.0, 600\.0\) at 1\.0x \(in logical pixels\)\n' - r'\n' ), ]); console.clear();