border_test.dart 11.1 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/foundation.dart' show FlutterError;
6 7 8
import 'package:flutter/painting.dart';
import 'package:flutter_test/flutter_test.dart';

9 10 11 12 13 14 15 16 17
class TestCanvas implements Canvas {
  final List<Invocation> invocations = <Invocation>[];

  @override
  void noSuchMethod(Invocation invocation) {
    invocations.add(invocation);
  }
}

18
void main() {
19 20
  test('Border.uniform constructor', () {
    const BorderSide side = BorderSide();
21
    const Border border = Border.fromBorderSide(side);
22 23 24 25 26 27
    expect(border.left, same(side));
    expect(border.top, same(side));
    expect(border.right, same(side));
    expect(border.bottom, same(side));
  });

28 29
  test('Border.symmetric constructor', () {
    const BorderSide side1 = BorderSide(color: Color(0xFFFFFFFF));
30
    const BorderSide side2 = BorderSide();
31
    const Border border = Border.symmetric(vertical: side1, horizontal: side2);
32 33 34 35
    expect(border.left, same(side1));
    expect(border.top, same(side2));
    expect(border.right, same(side1));
    expect(border.bottom, same(side2));
36 37
  });

38
  test('Border.merge', () {
39 40 41 42
    const BorderSide magenta3 = BorderSide(color: Color(0xFFFF00FF), width: 3.0);
    const BorderSide magenta6 = BorderSide(color: Color(0xFFFF00FF), width: 6.0);
    const BorderSide yellow2 = BorderSide(color: Color(0xFFFFFF00), width: 2.0);
    const BorderSide yellowNone0 = BorderSide(color: Color(0xFFFFFF00), width: 0.0, style: BorderStyle.none);
43 44
    expect(
      Border.merge(
45 46
        const Border(top: yellow2),
        const Border(right: magenta3),
47
      ),
48
      const Border(top: yellow2, right: magenta3),
49 50 51
    );
    expect(
      Border.merge(
52 53
        const Border(bottom: magenta3),
        const Border(bottom: magenta3),
54
      ),
55
      const Border(bottom: magenta6),
56 57 58
    );
    expect(
      Border.merge(
59 60
        const Border(left: magenta3, right: yellowNone0),
        const Border(right: yellow2),
61
      ),
62
      const Border(left: magenta3, right: yellow2),
63 64 65 66 67 68 69
    );
    expect(
      Border.merge(const Border(), const Border()),
      const Border(),
    );
    expect(
      () => Border.merge(
70 71
        const Border(left: magenta3),
        const Border(left: yellow2),
72 73 74 75 76 77
      ),
      throwsAssertionError,
    );
  });

  test('Border.add', () {
78 79 80 81
    const BorderSide magenta3 = BorderSide(color: Color(0xFFFF00FF), width: 3.0);
    const BorderSide magenta6 = BorderSide(color: Color(0xFFFF00FF), width: 6.0);
    const BorderSide yellow2 = BorderSide(color: Color(0xFFFFFF00), width: 2.0);
    const BorderSide yellowNone0 = BorderSide(color: Color(0xFFFFFF00), width: 0.0, style: BorderStyle.none);
82
    expect(
83 84
      const Border(top: yellow2) + const Border(right: magenta3),
      const Border(top: yellow2, right: magenta3),
85 86
    );
    expect(
87 88
      const Border(bottom: magenta3) + const Border(bottom: magenta3),
      const Border(bottom: magenta6),
89 90
    );
    expect(
91 92
      const Border(left: magenta3, right: yellowNone0) + const Border(right: yellow2),
      const Border(left: magenta3, right: yellow2),
93 94
    );
    expect(
Ian Hickson's avatar
Ian Hickson committed
95
      const Border() + const Border(),
96 97 98
      const Border(),
    );
    expect(
99
      const Border(left: magenta3) + const Border(left: yellow2),
Dan Field's avatar
Dan Field committed
100
      isNot(isA<Border>()), // see shape_border_test.dart for better tests of this case
101
    );
102 103
    const Border b3 = Border(top: magenta3);
    const Border b6 = Border(top: magenta6);
Ian Hickson's avatar
Ian Hickson committed
104
    expect(b3 + b3, b6);
105 106
    const Border b0 = Border(top: yellowNone0);
    const Border bZ = Border();
Ian Hickson's avatar
Ian Hickson committed
107 108 109 110
    expect(b0 + b0, bZ);
    expect(bZ + bZ, bZ);
    expect(b0 + bZ, bZ);
    expect(bZ + b0, bZ);
111 112 113
  });

  test('Border.scale', () {
114 115 116 117 118 119
    const BorderSide magenta3 = BorderSide(color: Color(0xFFFF00FF), width: 3.0);
    const BorderSide magenta6 = BorderSide(color: Color(0xFFFF00FF), width: 6.0);
    const BorderSide yellow2 = BorderSide(color: Color(0xFFFFFF00), width: 2.0);
    const BorderSide yellowNone0 = BorderSide(color: Color(0xFFFFFF00), width: 0.0, style: BorderStyle.none);
    const Border b3 = Border(left: magenta3);
    const Border b6 = Border(left: magenta6);
120
    expect(b3.scale(2.0), b6);
121
    const Border bY0 = Border(top: yellowNone0);
122
    expect(bY0.scale(3.0), bY0);
123
    const Border bY2 = Border(top: yellow2);
124 125
    expect(bY2.scale(0.0), bY0);
  });
Ian Hickson's avatar
Ian Hickson committed
126 127 128 129

  test('Border.dimensions', () {
    expect(
      const Border(
130 131 132 133
        left: BorderSide(width: 2.0),
        top: BorderSide(width: 3.0),
        bottom: BorderSide(width: 5.0),
        right: BorderSide(width: 7.0),
Ian Hickson's avatar
Ian Hickson committed
134 135 136 137 138 139 140 141
      ).dimensions,
      const EdgeInsets.fromLTRB(2.0, 3.0, 7.0, 5.0),
    );
  });

  test('Border.isUniform', () {
    expect(
      const Border(
142 143 144 145
        left: BorderSide(width: 3.0),
        top: BorderSide(width: 3.0),
        right: BorderSide(width: 3.0),
        bottom: BorderSide(width: 3.1),
Ian Hickson's avatar
Ian Hickson committed
146 147 148 149 150
      ).isUniform,
      false,
    );
    expect(
      const Border(
151 152 153 154
        left: BorderSide(width: 3.0),
        top: BorderSide(width: 3.0),
        right: BorderSide(width: 3.0),
        bottom: BorderSide(width: 3.0),
Ian Hickson's avatar
Ian Hickson committed
155 156 157 158 159
      ).isUniform,
      true,
    );
    expect(
      const Border(
160 161 162 163
        left: BorderSide(color: Color(0xFFFFFFFE)),
        top: BorderSide(color: Color(0xFFFFFFFF)),
        right: BorderSide(color: Color(0xFFFFFFFF)),
        bottom: BorderSide(color: Color(0xFFFFFFFF)),
Ian Hickson's avatar
Ian Hickson committed
164 165 166 167 168
      ).isUniform,
      false,
    );
    expect(
      const Border(
169 170 171 172
        left: BorderSide(color: Color(0xFFFFFFFF)),
        top: BorderSide(color: Color(0xFFFFFFFF)),
        right: BorderSide(color: Color(0xFFFFFFFF)),
        bottom: BorderSide(color: Color(0xFFFFFFFF)),
Ian Hickson's avatar
Ian Hickson committed
173 174 175 176 177
      ).isUniform,
      true,
    );
    expect(
      const Border(
178 179 180
        left: BorderSide(style: BorderStyle.none),
        top: BorderSide(style: BorderStyle.none),
        right: BorderSide(style: BorderStyle.none),
181
        bottom: BorderSide(width: 0.0),
Ian Hickson's avatar
Ian Hickson committed
182 183 184 185 186
      ).isUniform,
      false,
    );
    expect(
      const Border(
187 188 189
        left: BorderSide(style: BorderStyle.none),
        top: BorderSide(style: BorderStyle.none),
        right: BorderSide(style: BorderStyle.none),
190
        bottom: BorderSide(width: 0.0),
Ian Hickson's avatar
Ian Hickson committed
191 192 193 194 195
      ).isUniform,
      false,
    );
    expect(
      const Border(
196 197 198
        left: BorderSide(style: BorderStyle.none),
        top: BorderSide(style: BorderStyle.none),
        right: BorderSide(style: BorderStyle.none),
Ian Hickson's avatar
Ian Hickson committed
199 200 201
      ).isUniform,
      false,
    );
202 203 204 205 206 207 208 209
    expect(
      const Border(
        left: BorderSide(),
        top: BorderSide(strokeAlign: StrokeAlign.center),
        right: BorderSide(strokeAlign: StrokeAlign.outside),
      ).isUniform,
      false,
    );
Ian Hickson's avatar
Ian Hickson committed
210
    expect(
211
      const Border().isUniform,
Ian Hickson's avatar
Ian Hickson committed
212 213 214 215 216 217 218 219 220
      true,
    );
    expect(
      const Border().isUniform,
      true,
    );
  });

  test('Border.lerp', () {
221 222 223 224 225 226 227
    const Border visualWithTop10 = Border(top: BorderSide(width: 10.0));
    const Border atMinus100 = Border(left: BorderSide(width: 0.0), right: BorderSide(width: 300.0));
    const Border at0 = Border(left: BorderSide(width: 100.0), right: BorderSide(width: 200.0));
    const Border at25 = Border(left: BorderSide(width: 125.0), right: BorderSide(width: 175.0));
    const Border at75 = Border(left: BorderSide(width: 175.0), right: BorderSide(width: 125.0));
    const Border at100 = Border(left: BorderSide(width: 200.0), right: BorderSide(width: 100.0));
    const Border at200 = Border(left: BorderSide(width: 300.0), right: BorderSide(width: 0.0));
Ian Hickson's avatar
Ian Hickson committed
228 229

    expect(Border.lerp(null, null, -1.0), null);
230
    expect(Border.lerp(visualWithTop10, null, -1.0), const Border(top: BorderSide(width: 20.0)));
Ian Hickson's avatar
Ian Hickson committed
231 232 233 234
    expect(Border.lerp(null, visualWithTop10, -1.0), const Border());
    expect(Border.lerp(at0, at100, -1.0), atMinus100);

    expect(Border.lerp(null, null, 0.0), null);
235
    expect(Border.lerp(visualWithTop10, null, 0.0), const Border(top: BorderSide(width: 10.0)));
Ian Hickson's avatar
Ian Hickson committed
236 237 238 239
    expect(Border.lerp(null, visualWithTop10, 0.0), const Border());
    expect(Border.lerp(at0, at100, 0.0), at0);

    expect(Border.lerp(null, null, 0.25), null);
240 241
    expect(Border.lerp(visualWithTop10, null, 0.25), const Border(top: BorderSide(width: 7.5)));
    expect(Border.lerp(null, visualWithTop10, 0.25), const Border(top: BorderSide(width: 2.5)));
Ian Hickson's avatar
Ian Hickson committed
242 243 244
    expect(Border.lerp(at0, at100, 0.25), at25);

    expect(Border.lerp(null, null, 0.75), null);
245 246
    expect(Border.lerp(visualWithTop10, null, 0.75), const Border(top: BorderSide(width: 2.5)));
    expect(Border.lerp(null, visualWithTop10, 0.75), const Border(top: BorderSide(width: 7.5)));
Ian Hickson's avatar
Ian Hickson committed
247 248 249 250
    expect(Border.lerp(at0, at100, 0.75), at75);

    expect(Border.lerp(null, null, 1.0), null);
    expect(Border.lerp(visualWithTop10, null, 1.0), const Border());
251
    expect(Border.lerp(null, visualWithTop10, 1.0), const Border(top: BorderSide(width: 10.0)));
Ian Hickson's avatar
Ian Hickson committed
252 253 254 255
    expect(Border.lerp(at0, at100, 1.0), at100);

    expect(Border.lerp(null, null, 2.0), null);
    expect(Border.lerp(visualWithTop10, null, 2.0), const Border());
256
    expect(Border.lerp(null, visualWithTop10, 2.0), const Border(top: BorderSide(width: 20.0)));
Ian Hickson's avatar
Ian Hickson committed
257 258
    expect(Border.lerp(at0, at100, 2.0), at200);
  });
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302

  test('Border - throws correct exception with strokeAlign', () {
    late FlutterError error;
    try {
      final TestCanvas canvas = TestCanvas();
      // Border.all supports all StrokeAlign values.
      // Border() supports StrokeAlign.inside only.
      const Border(
        left: BorderSide(strokeAlign: StrokeAlign.center),
        right: BorderSide(strokeAlign: StrokeAlign.outside),
      ).paint(canvas, const Rect.fromLTWH(10.0, 20.0, 30.0, 40.0));
    } on FlutterError catch (e) {
      error = e;
    }
    expect(error, isNotNull);
    expect(error.diagnostics.length, 1);
    expect(
      error.diagnostics[0].toStringDeep(),
      'A Border can only draw strokeAlign different than\nStrokeAlign.inside on uniform borders.\n',
    );
  });

  test('Border.dimension', () {
    final Border insideBorder = Border.all(width: 10);
    expect(insideBorder.dimensions, const EdgeInsets.all(10));

    final Border centerBorder = Border.all(width: 10, strokeAlign: StrokeAlign.center);
    expect(centerBorder.dimensions, const EdgeInsets.all(5));

    final Border outsideBorder = Border.all(width: 10, strokeAlign: StrokeAlign.outside);
    expect(outsideBorder.dimensions, EdgeInsets.zero);

    const BorderSide insideSide = BorderSide(width: 10);
    const BorderDirectional insideBorderDirectional = BorderDirectional(top: insideSide, bottom: insideSide, start: insideSide, end: insideSide);
    expect(insideBorderDirectional.dimensions, const EdgeInsetsDirectional.all(10));

    const BorderSide centerSide = BorderSide(width: 10, strokeAlign: StrokeAlign.center);
    const BorderDirectional centerBorderDirectional = BorderDirectional(top: centerSide, bottom: centerSide, start: centerSide, end: centerSide);
    expect(centerBorderDirectional.dimensions, const EdgeInsetsDirectional.all(5));

    const BorderSide outsideSide = BorderSide(width: 10, strokeAlign: StrokeAlign.outside);
    const BorderDirectional outsideBorderDirectional = BorderDirectional(top: outsideSide, bottom: outsideSide, start: outsideSide, end: outsideSide);
    expect(outsideBorderDirectional.dimensions, EdgeInsetsDirectional.zero);
  });
303
}