slivers_helpers_test.dart 4.44 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
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

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

void main() {
9
  test('applyGrowthDirectionToAxisDirection produces expected AxisDirection', () {
10
    expect(AxisDirection.values.length, 4);
11
    for (final AxisDirection axisDirection in AxisDirection.values) {
12
      expect(applyGrowthDirectionToAxisDirection(axisDirection, GrowthDirection.forward), axisDirection);
13
    }
14 15 16 17 18 19
    expect(applyGrowthDirectionToAxisDirection(AxisDirection.up, GrowthDirection.reverse), AxisDirection.down);
    expect(applyGrowthDirectionToAxisDirection(AxisDirection.down, GrowthDirection.reverse), AxisDirection.up);
    expect(applyGrowthDirectionToAxisDirection(AxisDirection.left, GrowthDirection.reverse), AxisDirection.right);
    expect(applyGrowthDirectionToAxisDirection(AxisDirection.right, GrowthDirection.reverse), AxisDirection.left);
  });

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
  test('SliverConstraints are the same when copied', () {
    const SliverConstraints original = SliverConstraints(
      axisDirection: AxisDirection.down,
      growthDirection: GrowthDirection.forward,
      userScrollDirection: ScrollDirection.idle,
      scrollOffset: 0.0,
      precedingScrollExtent: 0.0,
      overlap: 0.0,
      remainingPaintExtent: 0.0,
      crossAxisExtent: 0.0,
      crossAxisDirection: AxisDirection.right,
      viewportMainAxisExtent: 0.0,
      cacheOrigin: 0.0,
      remainingCacheExtent: 0.0,
    );
    final SliverConstraints copy = original.copyWith();
    expect(original, equals(copy));
    expect(original.hashCode, equals(copy.hashCode));
    expect(original.toString(), equals(copy.toString()));
    expect(original, hasOneLineDescription);
    expect(original.normalizedGrowthDirection, equals(GrowthDirection.forward));
  });

  test('SliverConstraints normalizedGrowthDirection is inferred from AxisDirection and GrowthDirection', () {
44
    const SliverConstraints a = SliverConstraints(
45 46 47 48
      axisDirection: AxisDirection.down,
      growthDirection: GrowthDirection.forward,
      userScrollDirection: ScrollDirection.idle,
      scrollOffset: 0.0,
49
      precedingScrollExtent: 0.0,
50 51 52
      overlap: 0.0,
      remainingPaintExtent: 0.0,
      crossAxisExtent: 0.0,
53
      crossAxisDirection: AxisDirection.right,
54
      viewportMainAxisExtent: 0.0,
55 56
      cacheOrigin: 0.0,
      remainingCacheExtent: 0.0,
57 58
    );

59
    final SliverConstraints c = a.copyWith(
60 61 62 63 64 65 66
      axisDirection: AxisDirection.up,
      growthDirection: GrowthDirection.reverse,
      userScrollDirection: ScrollDirection.forward,
      scrollOffset: 10.0,
      overlap: 20.0,
      remainingPaintExtent: 30.0,
      crossAxisExtent: 40.0,
67
      viewportMainAxisExtent: 30.0,
68
    );
69
    const SliverConstraints d = SliverConstraints(
70 71 72 73
      axisDirection: AxisDirection.up,
      growthDirection: GrowthDirection.reverse,
      userScrollDirection: ScrollDirection.forward,
      scrollOffset: 10.0,
74
      precedingScrollExtent: 0.0,
75 76 77
      overlap: 20.0,
      remainingPaintExtent: 30.0,
      crossAxisExtent: 40.0,
78
      crossAxisDirection: AxisDirection.right,
79
      viewportMainAxisExtent: 30.0,
80 81
      cacheOrigin: 0.0,
      remainingCacheExtent: 0.0,
82 83 84 85 86
    );
    expect(c, equals(d));
    expect(c.normalizedGrowthDirection, equals(GrowthDirection.forward));
    expect(d.normalizedGrowthDirection, equals(GrowthDirection.forward));

87
    final SliverConstraints e = d.copyWith(axisDirection: AxisDirection.right);
88 89
    expect(e.normalizedGrowthDirection, equals(GrowthDirection.reverse));

90
    final SliverConstraints f = d.copyWith(axisDirection: AxisDirection.left);
91 92
    expect(f.normalizedGrowthDirection, equals(GrowthDirection.forward));

93
    final SliverConstraints g = d.copyWith(growthDirection: GrowthDirection.forward);
94 95 96
    expect(g.normalizedGrowthDirection, equals(GrowthDirection.reverse));
  });

97
  test('SliverGeometry with no arguments is valid', () {
98
    expect(SliverGeometry.zero.debugAssertIsValid(), isTrue);
99 100 101
  });

  test('SliverGeometry throws error when layoutExtent exceeds paintExtent', () {
102
    expect(() {
103
      const SliverGeometry(layoutExtent: 10.0, paintExtent: 9.0).debugAssertIsValid();
104
    }, throwsFlutterError);
105 106 107
  });

  test('SliverGeometry throws error when maxPaintExtent is less than paintExtent', () {
108
    expect(() {
109
      const SliverGeometry(paintExtent: 9.0, maxPaintExtent: 8.0).debugAssertIsValid();
110 111 112
    }, throwsFlutterError);
  });
}