border_test.dart 8.87 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/painting.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
9 10
  test('Border.uniform constructor', () {
    const BorderSide side = BorderSide();
11
    const Border border = Border.fromBorderSide(side);
12 13 14 15 16 17
    expect(border.left, same(side));
    expect(border.top, same(side));
    expect(border.right, same(side));
    expect(border.bottom, same(side));
  });

18 19 20 21
  test('Border.symmetric constructor', () {
    const BorderSide side1 = BorderSide(color: Color(0xFFFFFFFF));
    const BorderSide side2 = BorderSide(color: Color(0xFF000000));
    const Border border = Border.symmetric(vertical: side1, horizontal: side2);
22 23 24 25
    expect(border.left, same(side1));
    expect(border.top, same(side2));
    expect(border.right, same(side1));
    expect(border.bottom, same(side2));
26 27
  });

28
  test('Border.merge', () {
29 30 31 32
    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);
33 34
    expect(
      Border.merge(
35 36
        const Border(top: yellow2),
        const Border(right: magenta3),
37
      ),
38
      const Border(top: yellow2, right: magenta3),
39 40 41
    );
    expect(
      Border.merge(
42 43
        const Border(bottom: magenta3),
        const Border(bottom: magenta3),
44
      ),
45
      const Border(bottom: magenta6),
46 47 48
    );
    expect(
      Border.merge(
49 50
        const Border(left: magenta3, right: yellowNone0),
        const Border(right: yellow2),
51
      ),
52
      const Border(left: magenta3, right: yellow2),
53 54 55 56 57 58 59
    );
    expect(
      Border.merge(const Border(), const Border()),
      const Border(),
    );
    expect(
      () => Border.merge(
60 61
        const Border(left: magenta3),
        const Border(left: yellow2),
62 63 64 65 66 67
      ),
      throwsAssertionError,
    );
  });

  test('Border.add', () {
68 69 70 71
    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);
72
    expect(
73 74
      const Border(top: yellow2) + const Border(right: magenta3),
      const Border(top: yellow2, right: magenta3),
75 76
    );
    expect(
77 78
      const Border(bottom: magenta3) + const Border(bottom: magenta3),
      const Border(bottom: magenta6),
79 80
    );
    expect(
81 82
      const Border(left: magenta3, right: yellowNone0) + const Border(right: yellow2),
      const Border(left: magenta3, right: yellow2),
83 84
    );
    expect(
Ian Hickson's avatar
Ian Hickson committed
85
      const Border() + const Border(),
86 87 88
      const Border(),
    );
    expect(
89
      const Border(left: magenta3) + const Border(left: yellow2),
Dan Field's avatar
Dan Field committed
90
      isNot(isA<Border>()), // see shape_border_test.dart for better tests of this case
91
    );
92 93
    const Border b3 = Border(top: magenta3);
    const Border b6 = Border(top: magenta6);
Ian Hickson's avatar
Ian Hickson committed
94
    expect(b3 + b3, b6);
95 96
    const Border b0 = Border(top: yellowNone0);
    const Border bZ = Border();
Ian Hickson's avatar
Ian Hickson committed
97 98 99 100
    expect(b0 + b0, bZ);
    expect(bZ + bZ, bZ);
    expect(b0 + bZ, bZ);
    expect(bZ + b0, bZ);
101 102 103
  });

  test('Border.scale', () {
104 105 106 107 108 109
    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);
110
    expect(b3.scale(2.0), b6);
111
    const Border bY0 = Border(top: yellowNone0);
112
    expect(bY0.scale(3.0), bY0);
113
    const Border bY2 = Border(top: yellow2);
114 115
    expect(bY2.scale(0.0), bY0);
  });
Ian Hickson's avatar
Ian Hickson committed
116 117 118 119

  test('Border.dimensions', () {
    expect(
      const Border(
120 121 122 123
        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
124 125 126 127 128 129 130 131
      ).dimensions,
      const EdgeInsets.fromLTRB(2.0, 3.0, 7.0, 5.0),
    );
  });

  test('Border.isUniform', () {
    expect(
      const Border(
132 133 134 135
        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
136 137 138 139 140
      ).isUniform,
      false,
    );
    expect(
      const Border(
141 142 143 144
        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
145 146 147 148 149
      ).isUniform,
      true,
    );
    expect(
      const Border(
150 151 152 153
        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
154 155 156 157 158
      ).isUniform,
      false,
    );
    expect(
      const Border(
159 160 161 162
        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
163 164 165 166 167
      ).isUniform,
      true,
    );
    expect(
      const Border(
168 169 170 171
        left: BorderSide(style: BorderStyle.none),
        top: BorderSide(style: BorderStyle.none),
        right: BorderSide(style: BorderStyle.none),
        bottom: BorderSide(style: BorderStyle.solid, width: 0.0),
Ian Hickson's avatar
Ian Hickson committed
172 173 174 175 176
      ).isUniform,
      false,
    );
    expect(
      const Border(
177 178 179 180
        left: BorderSide(style: BorderStyle.none),
        top: BorderSide(style: BorderStyle.none),
        right: BorderSide(style: BorderStyle.none),
        bottom: BorderSide(style: BorderStyle.solid, width: 0.0),
Ian Hickson's avatar
Ian Hickson committed
181 182 183 184 185
      ).isUniform,
      false,
    );
    expect(
      const Border(
186 187 188
        left: BorderSide(style: BorderStyle.none),
        top: BorderSide(style: BorderStyle.none),
        right: BorderSide(style: BorderStyle.none),
Ian Hickson's avatar
Ian Hickson committed
189 190 191 192 193 194
        bottom: BorderSide.none,
      ).isUniform,
      false,
    );
    expect(
      const Border(
195 196 197
        left: BorderSide(style: BorderStyle.none, width: 0.0),
        top: BorderSide(style: BorderStyle.none, width: 0.0),
        right: BorderSide(style: BorderStyle.none, width: 0.0),
Ian Hickson's avatar
Ian Hickson committed
198 199 200 201 202 203 204 205 206 207 208
        bottom: BorderSide.none,
      ).isUniform,
      true,
    );
    expect(
      const Border().isUniform,
      true,
    );
  });

  test('Border.lerp', () {
209 210 211 212 213 214 215
    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
216 217

    expect(Border.lerp(null, null, -1.0), null);
218
    expect(Border.lerp(visualWithTop10, null, -1.0), const Border(top: BorderSide(width: 20.0)));
Ian Hickson's avatar
Ian Hickson committed
219 220 221 222
    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);
223
    expect(Border.lerp(visualWithTop10, null, 0.0), const Border(top: BorderSide(width: 10.0)));
Ian Hickson's avatar
Ian Hickson committed
224 225 226 227
    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);
228 229
    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
230 231 232
    expect(Border.lerp(at0, at100, 0.25), at25);

    expect(Border.lerp(null, null, 0.75), null);
233 234
    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
235 236 237 238
    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());
239
    expect(Border.lerp(null, visualWithTop10, 1.0), const Border(top: BorderSide(width: 10.0)));
Ian Hickson's avatar
Ian Hickson committed
240 241 242 243
    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());
244
    expect(Border.lerp(null, visualWithTop10, 2.0), const Border(top: BorderSide(width: 20.0)));
Ian Hickson's avatar
Ian Hickson committed
245 246
    expect(Border.lerp(at0, at100, 2.0), at200);
  });
247
}