border_test.dart 9.11 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2015 The Chromium Authors. All rights reserved.
// 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() {
Ian Hickson's avatar
Ian Hickson committed
9
  test('Border constructor', () {
10 11 12 13
    expect(() => new Border(left: nonconst(null)), throwsAssertionError);
    expect(() => new Border(top: nonconst(null)), throwsAssertionError);
    expect(() => new Border(right: nonconst(null)), throwsAssertionError);
    expect(() => new Border(bottom: nonconst(null)), throwsAssertionError);
Ian Hickson's avatar
Ian Hickson committed
14 15
  });

16
  test('Border.merge', () {
17 18 19 20
    const BorderSide magenta3 = const BorderSide(color: const Color(0xFFFF00FF), width: 3.0);
    const BorderSide magenta6 = const BorderSide(color: const Color(0xFFFF00FF), width: 6.0);
    const BorderSide yellow2 = const BorderSide(color: const Color(0xFFFFFF00), width: 2.0);
    const BorderSide yellowNone0 = const BorderSide(color: const Color(0xFFFFFF00), width: 0.0, style: BorderStyle.none);
21 22
    expect(
      Border.merge(
23 24
        const Border(top: yellow2),
        const Border(right: magenta3),
25
      ),
26
      const Border(top: yellow2, right: magenta3),
27 28 29
    );
    expect(
      Border.merge(
30 31
        const Border(bottom: magenta3),
        const Border(bottom: magenta3),
32
      ),
33
      const Border(bottom: magenta6),
34 35 36
    );
    expect(
      Border.merge(
37 38
        const Border(left: magenta3, right: yellowNone0),
        const Border(right: yellow2),
39
      ),
40
      const Border(left: magenta3, right: yellow2),
41 42 43 44 45 46 47
    );
    expect(
      Border.merge(const Border(), const Border()),
      const Border(),
    );
    expect(
      () => Border.merge(
48 49
        const Border(left: magenta3),
        const Border(left: yellow2),
50 51 52 53 54 55
      ),
      throwsAssertionError,
    );
  });

  test('Border.add', () {
56 57 58 59
    const BorderSide magenta3 = const BorderSide(color: const Color(0xFFFF00FF), width: 3.0);
    const BorderSide magenta6 = const BorderSide(color: const Color(0xFFFF00FF), width: 6.0);
    const BorderSide yellow2 = const BorderSide(color: const Color(0xFFFFFF00), width: 2.0);
    const BorderSide yellowNone0 = const BorderSide(color: const Color(0xFFFFFF00), width: 0.0, style: BorderStyle.none);
60
    expect(
61 62
      const Border(top: yellow2) + const Border(right: magenta3),
      const Border(top: yellow2, right: magenta3),
63 64
    );
    expect(
65 66
      const Border(bottom: magenta3) + const Border(bottom: magenta3),
      const Border(bottom: magenta6),
67 68
    );
    expect(
69 70
      const Border(left: magenta3, right: yellowNone0) + const Border(right: yellow2),
      const Border(left: magenta3, right: yellow2),
71 72
    );
    expect(
Ian Hickson's avatar
Ian Hickson committed
73
      const Border() + const Border(),
74 75 76
      const Border(),
    );
    expect(
77
      const Border(left: magenta3) + const Border(left: yellow2),
Ian Hickson's avatar
Ian Hickson committed
78
      isNot(const isInstanceOf<Border>()), // see shape_border_test.dart for better tests of this case
79
    );
80 81
    const Border b3 = const Border(top: magenta3);
    const Border b6 = const Border(top: magenta6);
Ian Hickson's avatar
Ian Hickson committed
82
    expect(b3 + b3, b6);
83 84
    const Border b0 = const Border(top: yellowNone0);
    const Border bZ = const Border();
Ian Hickson's avatar
Ian Hickson committed
85 86 87 88
    expect(b0 + b0, bZ);
    expect(bZ + bZ, bZ);
    expect(b0 + bZ, bZ);
    expect(bZ + b0, bZ);
89 90 91
  });

  test('Border.scale', () {
92 93 94 95 96 97
    const BorderSide magenta3 = const BorderSide(color: const Color(0xFFFF00FF), width: 3.0);
    const BorderSide magenta6 = const BorderSide(color: const Color(0xFFFF00FF), width: 6.0);
    const BorderSide yellow2 = const BorderSide(color: const Color(0xFFFFFF00), width: 2.0);
    const BorderSide yellowNone0 = const BorderSide(color: const Color(0xFFFFFF00), width: 0.0, style: BorderStyle.none);
    const Border b3 = const Border(left: magenta3);
    const Border b6 = const Border(left: magenta6);
98
    expect(b3.scale(2.0), b6);
99
    const Border bY0 = const Border(top: yellowNone0);
100
    expect(bY0.scale(3.0), bY0);
101
    const Border bY2 = const Border(top: yellow2);
102 103
    expect(bY2.scale(0.0), bY0);
  });
Ian Hickson's avatar
Ian Hickson committed
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 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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196

  test('Border.dimensions', () {
    expect(
      const Border(
        left: const BorderSide(width: 2.0),
        top: const BorderSide(width: 3.0),
        bottom: const BorderSide(width: 5.0),
        right: const BorderSide(width: 7.0),
      ).dimensions,
      const EdgeInsets.fromLTRB(2.0, 3.0, 7.0, 5.0),
    );
  });

  test('Border.isUniform', () {
    expect(
      const Border(
        left: const BorderSide(width: 3.0),
        top: const BorderSide(width: 3.0),
        right: const BorderSide(width: 3.0),
        bottom: const BorderSide(width: 3.1),
      ).isUniform,
      false,
    );
    expect(
      const Border(
        left: const BorderSide(width: 3.0),
        top: const BorderSide(width: 3.0),
        right: const BorderSide(width: 3.0),
        bottom: const BorderSide(width: 3.0),
      ).isUniform,
      true,
    );
    expect(
      const Border(
        left: const BorderSide(color: const Color(0xFFFFFFFE)),
        top: const BorderSide(color: const Color(0xFFFFFFFF)),
        right: const BorderSide(color: const Color(0xFFFFFFFF)),
        bottom: const BorderSide(color: const Color(0xFFFFFFFF)),
      ).isUniform,
      false,
    );
    expect(
      const Border(
        left: const BorderSide(color: const Color(0xFFFFFFFF)),
        top: const BorderSide(color: const Color(0xFFFFFFFF)),
        right: const BorderSide(color: const Color(0xFFFFFFFF)),
        bottom: const BorderSide(color: const Color(0xFFFFFFFF)),
      ).isUniform,
      true,
    );
    expect(
      const Border(
        left: const BorderSide(style: BorderStyle.none),
        top: const BorderSide(style: BorderStyle.none),
        right: const BorderSide(style: BorderStyle.none),
        bottom: const BorderSide(style: BorderStyle.solid, width: 0.0),
      ).isUniform,
      false,
    );
    expect(
      const Border(
        left: const BorderSide(style: BorderStyle.none),
        top: const BorderSide(style: BorderStyle.none),
        right: const BorderSide(style: BorderStyle.none),
        bottom: const BorderSide(style: BorderStyle.solid, width: 0.0),
      ).isUniform,
      false,
    );
    expect(
      const Border(
        left: const BorderSide(style: BorderStyle.none),
        top: const BorderSide(style: BorderStyle.none),
        right: const BorderSide(style: BorderStyle.none),
        bottom: BorderSide.none,
      ).isUniform,
      false,
    );
    expect(
      const Border(
        left: const BorderSide(style: BorderStyle.none, width: 0.0),
        top: const BorderSide(style: BorderStyle.none, width: 0.0),
        right: const BorderSide(style: BorderStyle.none, width: 0.0),
        bottom: BorderSide.none,
      ).isUniform,
      true,
    );
    expect(
      const Border().isUniform,
      true,
    );
  });

  test('Border.lerp', () {
197 198 199 200 201 202 203
    const Border visualWithTop10 = const Border(top: const BorderSide(width: 10.0));
    const Border atMinus100 = const Border(left: const BorderSide(width: 0.0), right: const BorderSide(width: 300.0));
    const Border at0 = const Border(left: const BorderSide(width: 100.0), right: const BorderSide(width: 200.0));
    const Border at25 = const Border(left: const BorderSide(width: 125.0), right: const BorderSide(width: 175.0));
    const Border at75 = const Border(left: const BorderSide(width: 175.0), right: const BorderSide(width: 125.0));
    const Border at100 = const Border(left: const BorderSide(width: 200.0), right: const BorderSide(width: 100.0));
    const Border at200 = const Border(left: const BorderSide(width: 300.0), right: const BorderSide(width: 0.0));
Ian Hickson's avatar
Ian Hickson committed
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234

    expect(Border.lerp(null, null, -1.0), null);
    expect(Border.lerp(visualWithTop10, null, -1.0), const Border(top: const BorderSide(width: 20.0)));
    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);
    expect(Border.lerp(visualWithTop10, null, 0.0), const Border(top: const BorderSide(width: 10.0)));
    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);
    expect(Border.lerp(visualWithTop10, null, 0.25), const Border(top: const BorderSide(width: 7.5)));
    expect(Border.lerp(null, visualWithTop10, 0.25), const Border(top: const BorderSide(width: 2.5)));
    expect(Border.lerp(at0, at100, 0.25), at25);

    expect(Border.lerp(null, null, 0.75), null);
    expect(Border.lerp(visualWithTop10, null, 0.75), const Border(top: const BorderSide(width: 2.5)));
    expect(Border.lerp(null, visualWithTop10, 0.75), const Border(top: const BorderSide(width: 7.5)));
    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());
    expect(Border.lerp(null, visualWithTop10, 1.0), const Border(top: const BorderSide(width: 10.0)));
    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());
    expect(Border.lerp(null, visualWithTop10, 2.0), const Border(top: const BorderSide(width: 20.0)));
    expect(Border.lerp(at0, at100, 2.0), at200);
  });
235
}