viewport_caching_test.dart 5.43 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// This file is separate from viewport_test.dart because we can't use both
// testWidgets and rendering_tester in the same file - testWidgets will
// initialize a binding, which rendering_tester will attempt to re-initialize
// (or vice versa).

import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';

import 'rendering_tester.dart';

void main() {
16 17
  TestRenderingFlutterBinding.ensureInitialized();

18 19 20
  const double width = 800;
  const double height = 600;
  Rect rectExpandedOnAxis(double value) => Rect.fromLTRB(0.0, 0.0 - value, width, height + value);
21
  late List<RenderSliver> children;
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 62 63 64 65 66

  setUp(() {
    children = <RenderSliver>[
      RenderSliverToBoxAdapter(
        child: RenderSizedBox(const Size(800, 400)),
      ),
    ];
  });

  test('Cache extent - null, pixels', () async {
    final RenderViewport renderViewport = RenderViewport(
      crossAxisDirection: AxisDirection.left,
      offset: ViewportOffset.zero(),
      children: children,
    );
    layout(renderViewport, phase: EnginePhase.flushSemantics);
    expect(
      renderViewport.describeSemanticsClip(null),
      rectExpandedOnAxis(RenderAbstractViewport.defaultCacheExtent),
    );
  });

  test('Cache extent - 0, pixels', () async {
    final RenderViewport renderViewport = RenderViewport(
      crossAxisDirection: AxisDirection.left,
      offset: ViewportOffset.zero(),
      cacheExtent: 0.0,
      children: children,
    );
    layout(renderViewport, phase: EnginePhase.flushSemantics);
    expect(renderViewport.describeSemanticsClip(null), rectExpandedOnAxis(0.0));
  });

  test('Cache extent - 500, pixels', () async {
    final RenderViewport renderViewport = RenderViewport(
      crossAxisDirection: AxisDirection.left,
      offset: ViewportOffset.zero(),
      cacheExtent: 500.0,
      children: children,
    );
    layout(renderViewport, phase: EnginePhase.flushSemantics);
    expect(renderViewport.describeSemanticsClip(null), rectExpandedOnAxis(500.0));
  });

  test('Cache extent - nullx viewport', () async {
67 68
    await expectLater(
      () => RenderViewport(
69 70 71 72 73
        crossAxisDirection: AxisDirection.left,
        offset: ViewportOffset.zero(),
        cacheExtentStyle: CacheExtentStyle.viewport,
        children: children,
      ),
74
      throwsAssertionError,
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
    );
  });

  test('Cache extent - 0x viewport', () async {
    final RenderViewport renderViewport = RenderViewport(
      crossAxisDirection: AxisDirection.left,
      offset: ViewportOffset.zero(),
      cacheExtent: 0.0,
      cacheExtentStyle: CacheExtentStyle.viewport,
      children: children,
    );

    layout(renderViewport);
    expect(renderViewport.describeSemanticsClip(null), rectExpandedOnAxis(0));
  });

  test('Cache extent - .5x viewport', () async {
    final RenderViewport renderViewport = RenderViewport(
      crossAxisDirection: AxisDirection.left,
      offset: ViewportOffset.zero(),
      cacheExtent: .5,
      cacheExtentStyle: CacheExtentStyle.viewport,
      children: children,
    );

    layout(renderViewport);
    expect(renderViewport.describeSemanticsClip(null), rectExpandedOnAxis(height  / 2));
  });

  test('Cache extent - 1x viewport', () async {
    final RenderViewport renderViewport = RenderViewport(
      crossAxisDirection: AxisDirection.left,
      offset: ViewportOffset.zero(),
      cacheExtent: 1.0,
      cacheExtentStyle: CacheExtentStyle.viewport,
      children: children,
    );

    layout(renderViewport);
    expect(renderViewport.describeSemanticsClip(null), rectExpandedOnAxis(height));
  });

  test('Cache extent - 2.5x viewport', () async {
    final RenderViewport renderViewport = RenderViewport(
      crossAxisDirection: AxisDirection.left,
      offset: ViewportOffset.zero(),
      cacheExtent: 2.5,
      cacheExtentStyle: CacheExtentStyle.viewport,
      children: children,
    );

    layout(renderViewport);
    expect(renderViewport.describeSemanticsClip(null), rectExpandedOnAxis(height * 2.5));
  });
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163

  test('RenderShrinkWrappingViewport describeApproximatePaintClip with infinite viewportMainAxisExtent returns finite rect', () {
    final RenderSliver child = CustomConstraintsRenderSliver(const SliverConstraints(
      axisDirection: AxisDirection.down,
      cacheOrigin: 0.0,
      crossAxisDirection: AxisDirection.left,
      crossAxisExtent: 400.0,
      growthDirection: GrowthDirection.forward,
      overlap: 1.0,                                // must not equal 0 for this test
      precedingScrollExtent: 0.0,
      remainingPaintExtent: double.infinity,
      remainingCacheExtent: 0.0,
      scrollOffset: 0.0,
      userScrollDirection: ScrollDirection.idle,
      viewportMainAxisExtent: double.infinity,     // must == infinity
    ));

    final RenderShrinkWrappingViewport viewport = RenderShrinkWrappingViewport(
      crossAxisDirection: AxisDirection.left,
      offset: ViewportOffset.zero(),
      children: <RenderSliver>[ child ],
    );

    layout(viewport);
    expect(
      viewport.describeApproximatePaintClip(child),
      const Rect.fromLTRB(0.0, 0.0, 800.0, 600.0),
    );
  });
}

class CustomConstraintsRenderSliver extends RenderSliver {
  CustomConstraintsRenderSliver(this.constraints);

  @override
164
  SliverGeometry get geometry => SliverGeometry.zero;
165 166 167 168 169 170 171

  @override
  final SliverConstraints constraints;

  @override
  void performLayout() {}

172
}