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

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

import '../rendering/mock_canvas.dart';
import 'common_matchers.dart';

void main() {
12 13 14 15 16 17 18 19 20 21 22 23
  test('StadiumBorder defaults', () {
    const StadiumBorder border = StadiumBorder();
    expect(border.side, BorderSide.none);
  });

  test('StadiumBorder copyWith, ==, hashCode', () {
    expect(const StadiumBorder(), const StadiumBorder().copyWith());
    expect(const StadiumBorder().hashCode, const StadiumBorder().copyWith().hashCode);
    const BorderSide side = BorderSide(width: 10.0, color: Color(0xff123456));
    expect(const StadiumBorder().copyWith(side: side), const StadiumBorder(side: side));
  });

24
  test('StadiumBorder', () {
25 26 27
    const StadiumBorder c10 = StadiumBorder(side: BorderSide(width: 10.0));
    const StadiumBorder c15 = StadiumBorder(side: BorderSide(width: 15.0));
    const StadiumBorder c20 = StadiumBorder(side: BorderSide(width: 20.0));
28 29 30 31 32 33 34
    expect(c10.dimensions, const EdgeInsets.all(10.0));
    expect(c10.scale(2.0), c20);
    expect(c20.scale(0.5), c10);
    expect(ShapeBorder.lerp(c10, c20, 0.0), c10);
    expect(ShapeBorder.lerp(c10, c20, 0.5), c15);
    expect(ShapeBorder.lerp(c10, c20, 1.0), c20);

35
    const StadiumBorder c1 = StadiumBorder(side: BorderSide(width: 1.0));
36
    expect(c1.getOuterPath(Rect.fromCircle(center: Offset.zero, radius: 1.0)), isUnitCircle);
37
    const StadiumBorder c2 = StadiumBorder(side: BorderSide(width: 1.0));
38
    expect(c2.getInnerPath(Rect.fromCircle(center: Offset.zero, radius: 2.0)), isUnitCircle);
Dan Field's avatar
Dan Field committed
39
    const Rect rect = Rect.fromLTRB(10.0, 20.0, 100.0, 200.0);
40 41 42 43
    expect(
            (Canvas canvas) => c10.paint(canvas, rect),
        paints
          ..rrect(
44
            rrect: RRect.fromRectAndRadius(rect.deflate(5.0), Radius.circular(rect.shortestSide / 2.0 - 5.0)),
45
            strokeWidth: 10.0,
46
          ),
47 48 49 50
    );
  });

  test('StadiumBorder and CircleBorder', () {
51 52
    const StadiumBorder stadium = StadiumBorder(side: BorderSide.none);
    const CircleBorder circle = CircleBorder(side: BorderSide.none);
Dan Field's avatar
Dan Field committed
53
    const Rect rect = Rect.fromLTWH(0.0, 0.0, 100.0, 20.0);
54
    final Matcher looksLikeS = isPathThat(
55 56
      includes: const <Offset>[ Offset(30.0, 10.0), Offset(50.0, 10.0), ],
      excludes: const <Offset>[ Offset(1.0, 1.0), Offset(99.0, 19.0), ],
57 58
    );
    final Matcher looksLikeC = isPathThat(
59 60
      includes: const <Offset>[ Offset(50.0, 10.0), ],
      excludes: const <Offset>[ Offset(1.0, 1.0), Offset(30.0, 10.0), Offset(99.0, 19.0), ],
61 62 63
    );
    expect(stadium.getOuterPath(rect), looksLikeS);
    expect(circle.getOuterPath(rect), looksLikeC);
64 65 66 67 68 69 70 71 72 73 74 75
    expect(ShapeBorder.lerp(stadium, circle, 0.1)!.getOuterPath(rect), looksLikeS);
    expect(ShapeBorder.lerp(stadium, circle, 0.9)!.getOuterPath(rect), looksLikeC);
    expect(ShapeBorder.lerp(ShapeBorder.lerp(stadium, circle, 0.9), stadium, 0.1)!.getOuterPath(rect), looksLikeC);
    expect(ShapeBorder.lerp(ShapeBorder.lerp(stadium, circle, 0.9), stadium, 0.9)!.getOuterPath(rect), looksLikeS);
    expect(ShapeBorder.lerp(ShapeBorder.lerp(stadium, circle, 0.1), circle, 0.1)!.getOuterPath(rect), looksLikeS);
    expect(ShapeBorder.lerp(ShapeBorder.lerp(stadium, circle, 0.1), circle, 0.9)!.getOuterPath(rect), looksLikeC);
    expect(ShapeBorder.lerp(ShapeBorder.lerp(stadium, circle, 0.1), ShapeBorder.lerp(stadium, circle, 0.9), 0.1)!.getOuterPath(rect), looksLikeS);
    expect(ShapeBorder.lerp(ShapeBorder.lerp(stadium, circle, 0.1), ShapeBorder.lerp(stadium, circle, 0.9), 0.9)!.getOuterPath(rect), looksLikeC);
    expect(ShapeBorder.lerp(stadium, ShapeBorder.lerp(stadium, circle, 0.9), 0.1)!.getOuterPath(rect), looksLikeS);
    expect(ShapeBorder.lerp(stadium, ShapeBorder.lerp(stadium, circle, 0.9), 0.9)!.getOuterPath(rect), looksLikeC);
    expect(ShapeBorder.lerp(circle, ShapeBorder.lerp(stadium, circle, 0.1), 0.1)!.getOuterPath(rect), looksLikeC);
    expect(ShapeBorder.lerp(circle, ShapeBorder.lerp(stadium, circle, 0.1), 0.9)!.getOuterPath(rect), looksLikeS);
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

    expect(ShapeBorder.lerp(stadium, circle, 0.1).toString(),
        'StadiumBorder(BorderSide(Color(0xff000000), 0.0, BorderStyle.none), 10.0% of the way to being a CircleBorder)');
    expect(ShapeBorder.lerp(stadium, circle, 0.2).toString(),
        'StadiumBorder(BorderSide(Color(0xff000000), 0.0, BorderStyle.none), 20.0% of the way to being a CircleBorder)');
    expect(ShapeBorder.lerp(ShapeBorder.lerp(stadium, circle, 0.1), ShapeBorder.lerp(stadium, circle, 0.9), 0.9).toString(),
        'StadiumBorder(BorderSide(Color(0xff000000), 0.0, BorderStyle.none), 82.0% of the way to being a CircleBorder)');

    expect(ShapeBorder.lerp(circle, stadium, 0.9).toString(),
        'StadiumBorder(BorderSide(Color(0xff000000), 0.0, BorderStyle.none), 10.0% of the way to being a CircleBorder)');
    expect(ShapeBorder.lerp(circle, stadium, 0.8).toString(),
        'StadiumBorder(BorderSide(Color(0xff000000), 0.0, BorderStyle.none), 20.0% of the way to being a CircleBorder)');
    expect(ShapeBorder.lerp(ShapeBorder.lerp(stadium, circle, 0.9), ShapeBorder.lerp(stadium, circle, 0.1), 0.1).toString(),
        'StadiumBorder(BorderSide(Color(0xff000000), 0.0, BorderStyle.none), 82.0% of the way to being a CircleBorder)');

    expect(ShapeBorder.lerp(stadium, circle, 0.1), ShapeBorder.lerp(stadium, circle, 0.1));
    expect(ShapeBorder.lerp(stadium, circle, 0.1).hashCode, ShapeBorder.lerp(stadium, circle, 0.1).hashCode);

94 95
    final ShapeBorder direct50 = ShapeBorder.lerp(stadium, circle, 0.5)!;
    final ShapeBorder indirect50 = ShapeBorder.lerp(ShapeBorder.lerp(circle, stadium, 0.1), ShapeBorder.lerp(circle, stadium, 0.9), 0.5)!;
96 97 98
    expect(direct50, indirect50);
    expect(direct50.hashCode, indirect50.hashCode);
    expect(direct50.toString(), indirect50.toString());
99
  });
100 101

  test('StadiumBorder and RoundedRectBorder', () {
102 103
    const StadiumBorder stadium = StadiumBorder(side: BorderSide.none);
    const RoundedRectangleBorder rrect = RoundedRectangleBorder(side: BorderSide.none);
Dan Field's avatar
Dan Field committed
104
    const Rect rect = Rect.fromLTWH(0.0, 0.0, 100.0, 50.0);
105 106
    final Matcher looksLikeS = isPathThat(
      includes: const <Offset>[
107 108 109
        Offset(25.0, 25.0),
        Offset(50.0, 25.0),
        Offset(7.33, 7.33),
110 111
      ],
      excludes: const <Offset>[
112 113 114 115
        Offset(0.001, 0.001),
        Offset(99.999, 0.001),
        Offset(99.999, 49.999),
        Offset(0.001, 49.999),
116 117 118 119
      ],
    );
    final Matcher looksLikeR = isPathThat(
      includes: const <Offset>[
120 121 122 123 124 125 126
        Offset(25.0, 25.0),
        Offset(50.0, 25.0),
        Offset(7.33, 7.33),
        Offset(4.0, 4.0),
        Offset(96.0, 4.0),
        Offset(96.0, 46.0),
        Offset(4.0, 46.0),
127 128 129 130
      ],
    );
    expect(stadium.getOuterPath(rect), looksLikeS);
    expect(rrect.getOuterPath(rect), looksLikeR);
131 132 133 134 135 136 137 138 139 140 141 142
    expect(ShapeBorder.lerp(stadium, rrect, 0.1)!.getOuterPath(rect), looksLikeS);
    expect(ShapeBorder.lerp(stadium, rrect, 0.9)!.getOuterPath(rect), looksLikeR);
    expect(ShapeBorder.lerp(ShapeBorder.lerp(stadium, rrect, 0.9), stadium, 0.1)!.getOuterPath(rect), looksLikeR);
    expect(ShapeBorder.lerp(ShapeBorder.lerp(stadium, rrect, 0.9), stadium, 0.9)!.getOuterPath(rect), looksLikeS);
    expect(ShapeBorder.lerp(ShapeBorder.lerp(stadium, rrect, 0.1), rrect, 0.1)!.getOuterPath(rect), looksLikeS);
    expect(ShapeBorder.lerp(ShapeBorder.lerp(stadium, rrect, 0.1), rrect, 0.9)!.getOuterPath(rect), looksLikeS);
    expect(ShapeBorder.lerp(ShapeBorder.lerp(stadium, rrect, 0.1), ShapeBorder.lerp(stadium, rrect, 0.9), 0.1)!.getOuterPath(rect), looksLikeS);
    expect(ShapeBorder.lerp(ShapeBorder.lerp(stadium, rrect, 0.1), ShapeBorder.lerp(stadium, rrect, 0.9), 0.9)!.getOuterPath(rect), looksLikeR);
    expect(ShapeBorder.lerp(stadium, ShapeBorder.lerp(stadium, rrect, 0.9), 0.1)!.getOuterPath(rect), looksLikeS);
    expect(ShapeBorder.lerp(stadium, ShapeBorder.lerp(stadium, rrect, 0.9), 0.9)!.getOuterPath(rect), looksLikeR);
    expect(ShapeBorder.lerp(rrect, ShapeBorder.lerp(stadium, rrect, 0.1), 0.1)!.getOuterPath(rect), looksLikeS);
    expect(ShapeBorder.lerp(rrect, ShapeBorder.lerp(stadium, rrect, 0.1), 0.9)!.getOuterPath(rect), looksLikeS);
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166

    expect(ShapeBorder.lerp(stadium, rrect, 0.1).toString(),
        'StadiumBorder(BorderSide(Color(0xff000000), 0.0, BorderStyle.none), '
        'BorderRadius.zero, 10.0% of the way to being a RoundedRectangleBorder)');
    expect(ShapeBorder.lerp(stadium, rrect, 0.2).toString(),
        'StadiumBorder(BorderSide(Color(0xff000000), 0.0, BorderStyle.none), '
        'BorderRadius.zero, 20.0% of the way to being a RoundedRectangleBorder)');
    expect(ShapeBorder.lerp(ShapeBorder.lerp(stadium, rrect, 0.1), ShapeBorder.lerp(stadium, rrect, 0.9), 0.9).toString(),
        'StadiumBorder(BorderSide(Color(0xff000000), 0.0, BorderStyle.none), '
        'BorderRadius.zero, 82.0% of the way to being a RoundedRectangleBorder)');

    expect(ShapeBorder.lerp(rrect, stadium, 0.9).toString(),
        'StadiumBorder(BorderSide(Color(0xff000000), 0.0, BorderStyle.none), '
        'BorderRadius.zero, 10.0% of the way to being a RoundedRectangleBorder)');
    expect(ShapeBorder.lerp(rrect, stadium, 0.8).toString(),
        'StadiumBorder(BorderSide(Color(0xff000000), 0.0, BorderStyle.none), '
        'BorderRadius.zero, 20.0% of the way to being a RoundedRectangleBorder)');
    expect(ShapeBorder.lerp(ShapeBorder.lerp(stadium, rrect, 0.9), ShapeBorder.lerp(stadium, rrect, 0.1), 0.1).toString(),
        'StadiumBorder(BorderSide(Color(0xff000000), 0.0, BorderStyle.none), '
        'BorderRadius.zero, 82.0% of the way to being a RoundedRectangleBorder)');

    expect(ShapeBorder.lerp(stadium, rrect, 0.1), ShapeBorder.lerp(stadium, rrect, 0.1));
    expect(ShapeBorder.lerp(stadium, rrect, 0.1).hashCode, ShapeBorder.lerp(stadium, rrect, 0.1).hashCode);

167 168
    final ShapeBorder direct50 = ShapeBorder.lerp(stadium, rrect, 0.5)!;
    final ShapeBorder indirect50 = ShapeBorder.lerp(ShapeBorder.lerp(rrect, stadium, 0.1), ShapeBorder.lerp(rrect, stadium, 0.9), 0.5)!;
169 170 171 172 173
    expect(direct50, indirect50);
    expect(direct50.hashCode, indirect50.hashCode);
    expect(direct50.toString(), indirect50.toString());
  });
}