sliver_fill_viewport_test.dart 8.43 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'package:flutter/material.dart';
6
import 'package:flutter/rendering.dart';
7 8 9
import 'package:flutter_test/flutter_test.dart';

void main() {
10
  testWidgets('SliverFillViewport control test', (WidgetTester tester) async {
11
    final List<Widget> children = List<Widget>.generate(20, (int i) {
12
      return Container(color: Colors.green, child: Text('$i', textDirection: TextDirection.ltr));
13 14 15
    });

    await tester.pumpWidget(
16
      Directionality(
17
        textDirection: TextDirection.ltr,
18
        child: CustomScrollView(
19
          slivers: <Widget>[
20
            SliverFillViewport(
21
              delegate: SliverChildListDelegate(children, addAutomaticKeepAlives: false, addSemanticIndexes: false),
22 23 24
            ),
          ],
        ),
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
      ),
    );

    final RenderBox box = tester.renderObject<RenderBox>(find.byType(Container).first);
    expect(box.size.height, equals(600.0));

    expect(find.text('0'), findsOneWidget);
    expect(find.text('1'), findsNothing);
    expect(find.text('2'), findsNothing);

    await tester.drag(find.byType(Scrollable), const Offset(0.0, -700.0));
    await tester.pump();

    expect(find.text('0'), findsNothing);
    expect(find.text('1'), findsOneWidget);
    expect(find.text('2'), findsOneWidget);
    expect(find.text('3'), findsNothing);
    expect(find.text('4'), findsNothing);

    await tester.drag(find.byType(Scrollable), const Offset(0.0, 200.0));
    await tester.pump();

    expect(find.text('0'), findsOneWidget);
    expect(find.text('1'), findsOneWidget);
    expect(find.text('2'), findsNothing);
    expect(find.text('3'), findsNothing);

    await tester.drag(find.byType(Scrollable), const Offset(0.0, 700.0));
    await tester.pump();

    final RenderBox box2 = tester.renderObject<RenderBox>(find.byType(Container).first);
    expect(box2.size.height, equals(600.0));

    expect(find.text('0'), findsOneWidget);
    expect(find.text('1'), findsNothing);
    expect(find.text('2'), findsNothing);
    expect(find.text('3'), findsNothing);
62 63

    final RenderObject viewport = tester.renderObject<RenderObject>(find.byType(SliverFillViewport).first);
64
    expect(viewport, hasAGoodToStringDeep);
65
    expect(
66
      viewport.toStringDeep(minLevel: DiagnosticLevel.info),
67
      equalsIgnoringHashCodes(
68
        '_RenderSliverFractionalPadding#00000 relayoutBoundary=up1\n'
69
        ' │ needs compositing\n'
70 71 72 73
        ' │ parentData: paintOffset=Offset(0.0, 0.0) (can use size)\n'
        ' │ constraints: SliverConstraints(AxisDirection.down,\n'
        ' │   GrowthDirection.forward, ScrollDirection.idle, scrollOffset:\n'
        ' │   0.0, remainingPaintExtent: 600.0, crossAxisExtent: 800.0,\n'
74
        ' │   crossAxisDirection: AxisDirection.right,\n'
75 76
        ' │   viewportMainAxisExtent: 600.0, remainingCacheExtent: 850.0,\n'
        ' │   cacheOrigin: 0.0)\n'
77
        ' │ geometry: SliverGeometry(scrollExtent: 12000.0, paintExtent:\n'
78 79
        ' │   600.0, maxPaintExtent: 12000.0, hasVisualOverflow: true,\n'
        ' │   cacheExtent: 850.0)\n'
80
        ' │\n'
81
        ' └─child: RenderSliverFillViewport#00000 relayoutBoundary=up2\n'
82
        '   │ needs compositing\n'
83 84 85 86 87
        '   │ parentData: paintOffset=Offset(0.0, 0.0) (can use size)\n'
        '   │ constraints: SliverConstraints(AxisDirection.down,\n'
        '   │   GrowthDirection.forward, ScrollDirection.idle, scrollOffset:\n'
        '   │   0.0, remainingPaintExtent: 600.0, crossAxisExtent: 800.0,\n'
        '   │   crossAxisDirection: AxisDirection.right,\n'
88 89
        '   │   viewportMainAxisExtent: 600.0, remainingCacheExtent: 850.0,\n'
        '   │   cacheOrigin: 0.0)\n'
90 91 92 93
        '   │ geometry: SliverGeometry(scrollExtent: 12000.0, paintExtent:\n'
        '   │   600.0, maxPaintExtent: 12000.0, hasVisualOverflow: true,\n'
        '   │   cacheExtent: 850.0)\n'
        '   │ currently live children: 0 to 1\n'
94
        '   │\n'
95 96 97 98 99 100 101 102 103 104
        '   ├─child with index 0: RenderRepaintBoundary#00000\n'
        '   │ │ needs compositing\n'
        '   │ │ parentData: index=0; layoutOffset=0.0\n'
        '   │ │ constraints: BoxConstraints(w=800.0, h=600.0)\n'
        '   │ │ layer: OffsetLayer#00000\n'
        '   │ │ size: Size(800.0, 600.0)\n'
        '   │ │ metrics: 66.7% useful (1 bad vs 2 good)\n'
        '   │ │ diagnosis: insufficient data to draw conclusion (less than five\n'
        '   │ │   repaints)\n'
        '   │ │\n'
105
        '   │ └─child: _RenderColoredBox#00000\n'
106 107 108
        '   │   │ parentData: <none> (can use size)\n'
        '   │   │ constraints: BoxConstraints(w=800.0, h=600.0)\n'
        '   │   │ size: Size(800.0, 600.0)\n'
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
        '   │   │ behavior: opaque\n'
        '   │   │\n'
        '   │   └─child: RenderParagraph#00000\n'
        '   │     │ parentData: <none> (can use size)\n'
        '   │     │ constraints: BoxConstraints(w=800.0, h=600.0)\n'
        '   │     │ semantics node: SemanticsNode#2\n'
        '   │     │ size: Size(800.0, 600.0)\n'
        '   │     │ textAlign: start\n'
        '   │     │ textDirection: ltr\n'
        '   │     │ softWrap: wrapping at box width\n'
        '   │     │ overflow: clip\n'
        '   │     │ maxLines: unlimited\n'
        '   │     ╘═╦══ text ═══\n'
        '   │       ║ TextSpan:\n'
        '   │       ║   <all styles inherited>\n'
        '   │       ║   "0"\n'
        '   │       ╚═══════════\n'
126 127 128
        '   └─child with index 1: RenderRepaintBoundary#00000\n'
        '     │ needs compositing\n'
        '     │ parentData: index=1; layoutOffset=600.0\n'
129
        '     │ constraints: BoxConstraints(w=800.0, h=600.0)\n'
130
        '     │ layer: OffsetLayer#00000 DETACHED\n'
131
        '     │ size: Size(800.0, 600.0)\n'
132 133 134 135
        '     │ metrics: 50.0% useful (1 bad vs 1 good)\n'
        '     │ diagnosis: insufficient data to draw conclusion (less than five\n'
        '     │   repaints)\n'
        '     │\n'
136
        '     └─child: _RenderColoredBox#00000\n'
137 138 139
        '       │ parentData: <none> (can use size)\n'
        '       │ constraints: BoxConstraints(w=800.0, h=600.0)\n'
        '       │ size: Size(800.0, 600.0)\n'
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
        '       │ behavior: opaque\n'
        '       │\n'
        '       └─child: RenderParagraph#00000\n'
        '         │ parentData: <none> (can use size)\n'
        '         │ constraints: BoxConstraints(w=800.0, h=600.0)\n'
        '         │ semantics node: SemanticsNode#3\n'
        '         │ size: Size(800.0, 600.0)\n'
        '         │ textAlign: start\n'
        '         │ textDirection: ltr\n'
        '         │ softWrap: wrapping at box width\n'
        '         │ overflow: clip\n'
        '         │ maxLines: unlimited\n'
        '         ╘═╦══ text ═══\n'
        '           ║ TextSpan:\n'
        '           ║   <all styles inherited>\n'
        '           ║   "1"\n'
156
        '           ╚═══════════\n',
157 158
      ),
    );
159
  });
160 161 162 163

  testWidgets('SliverFillViewport padding test', (WidgetTester tester) async {
    final SliverChildListDelegate delegate = SliverChildListDelegate(
      <Widget>[
164
        const Text('0'),
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
      ],
      addAutomaticKeepAlives: false,
      addSemanticIndexes: false,
    );

    await tester.pumpWidget(
      Directionality(
        textDirection: TextDirection.ltr,
        child: CustomScrollView(
          slivers: <Widget>[
            SliverFillViewport(
              viewportFraction: 0.5,
              delegate: delegate,
            ),
          ],
        ),
      ),
    );

    final RenderSliver boxWithPadding = tester.renderObject<RenderSliver>(find.byType(SliverFillViewport));
185
    expect(boxWithPadding.geometry!.paintExtent, equals(600.0));
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202

    await tester.pumpWidget(
      Directionality(
        textDirection: TextDirection.ltr,
        child: CustomScrollView(
          slivers: <Widget>[
            SliverFillViewport(
              padEnds: false,
              viewportFraction: 0.5,
              delegate: delegate,
            ),
          ],
        ),
      ),
    );

    final RenderSliver boxWithoutPadding = tester.renderObject<RenderSliver>(find.byType(SliverFillViewport));
203
    expect(boxWithoutPadding.geometry!.paintExtent, equals(300.0));
204
  });
205
}