shape_border_test.dart 8.56 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
Ian Hickson's avatar
Ian Hickson committed
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';

void main() {
  test('Compound borders', () {
12 13
    final Border b1 = Border.all(color: const Color(0xFF00FF00));
    final Border b2 = Border.all(color: const Color(0xFF0000FF));
Ian Hickson's avatar
Ian Hickson committed
14 15
    expect(
      (b1 + b2).toString(),
16 17
      'Border.all(BorderSide(color: Color(0xff00ff00))) + '
      'Border.all(BorderSide(color: Color(0xff0000ff)))',
Ian Hickson's avatar
Ian Hickson committed
18 19 20
    );
    expect(
      (b1 + (b2 + b2)).toString(),
21 22
      'Border.all(BorderSide(color: Color(0xff00ff00))) + '
      'Border.all(BorderSide(color: Color(0xff0000ff), width: 2.0))',
Ian Hickson's avatar
Ian Hickson committed
23 24 25
    );
    expect(
      ((b1 + b2) + b2).toString(),
26 27
      'Border.all(BorderSide(color: Color(0xff00ff00))) + '
      'Border.all(BorderSide(color: Color(0xff0000ff), width: 2.0))',
Ian Hickson's avatar
Ian Hickson committed
28 29 30 31
    );
    expect((b1 + b2) + b2, b1 + (b2 + b2));
    expect(
      (b1 + b2).scale(3.0).toString(),
32 33
      'Border.all(BorderSide(color: Color(0xff00ff00), width: 3.0)) + '
      'Border.all(BorderSide(color: Color(0xff0000ff), width: 3.0))',
Ian Hickson's avatar
Ian Hickson committed
34 35 36
    );
    expect(
      (b1 + b2).scale(0.0).toString(),
37 38
      'Border.all(BorderSide(color: Color(0xff00ff00), width: 0.0, style: none)) + '
      'Border.all(BorderSide(color: Color(0xff0000ff), width: 0.0, style: none))',
Ian Hickson's avatar
Ian Hickson committed
39 40 41
    );
    expect(
      ShapeBorder.lerp(b2 + b1, b1 + b2, 0.0).toString(),
42 43
      'Border.all(BorderSide(color: Color(0xff0000ff))) + '
      'Border.all(BorderSide(color: Color(0xff00ff00)))',
Ian Hickson's avatar
Ian Hickson committed
44 45 46
    );
    expect(
      ShapeBorder.lerp(b2 + b1, b1 + b2, 0.25).toString(),
47 48
      'Border.all(BorderSide(color: Color(0xff003fbf))) + '
      'Border.all(BorderSide(color: Color(0xff00bf3f)))',
Ian Hickson's avatar
Ian Hickson committed
49 50 51
    );
    expect(
      ShapeBorder.lerp(b2 + b1, b1 + b2, 0.5).toString(),
52 53
      'Border.all(BorderSide(color: Color(0xff007f7f))) + '
      'Border.all(BorderSide(color: Color(0xff007f7f)))',
Ian Hickson's avatar
Ian Hickson committed
54 55 56
    );
    expect(
      ShapeBorder.lerp(b2 + b1, b1 + b2, 1.0).toString(),
57 58
      'Border.all(BorderSide(color: Color(0xff00ff00))) + '
      'Border.all(BorderSide(color: Color(0xff0000ff)))',
Ian Hickson's avatar
Ian Hickson committed
59 60
    );
    expect((b1 + b2).dimensions, const EdgeInsets.all(2.0));
Dan Field's avatar
Dan Field committed
61
    const Rect rect = Rect.fromLTRB(11.0, 15.0, 299.0, 175.0);
Ian Hickson's avatar
Ian Hickson committed
62 63
    expect((Canvas canvas) => (b1 + b2).paint(canvas, rect), paints
      ..rect(rect: rect.deflate(0.5), color: b2.top.color)
64
      ..rect(rect: rect.deflate(1.5), color: b1.top.color),
Ian Hickson's avatar
Ian Hickson committed
65 66 67 68 69
    );
    expect((b1 + b2 + b1).dimensions, const EdgeInsets.all(3.0));
    expect((Canvas canvas) => (b1 + b2 + b1).paint(canvas, rect), paints
      ..rect(rect: rect.deflate(0.5), color: b1.top.color)
      ..rect(rect: rect.deflate(1.5), color: b2.top.color)
70
      ..rect(rect: rect.deflate(2.5), color: b1.top.color),
Ian Hickson's avatar
Ian Hickson committed
71 72
    );
  });
Ian Hickson's avatar
Ian Hickson committed
73 74

  test('Compound borders', () {
75 76 77 78
    const BorderSide side1 = BorderSide(color: Color(0xFF00FF00));
    const BorderSide side2 = BorderSide(color: Color(0xFF0000FF));
    const BorderDirectional b1 = BorderDirectional(top: side1, start: side1, end: side1, bottom: side1);
    const BorderDirectional b2 = BorderDirectional(top: side2, start: side2, end: side2, bottom: side2);
Ian Hickson's avatar
Ian Hickson committed
79 80
    expect(
      (b1 + b2).toString(),
81 82
      'BorderDirectional(top: BorderSide(color: Color(0xff00ff00)), start: BorderSide(color: Color(0xff00ff00)), end: BorderSide(color: Color(0xff00ff00)), bottom: BorderSide(color: Color(0xff00ff00))) + '
      'BorderDirectional(top: BorderSide(color: Color(0xff0000ff)), start: BorderSide(color: Color(0xff0000ff)), end: BorderSide(color: Color(0xff0000ff)), bottom: BorderSide(color: Color(0xff0000ff)))',
Ian Hickson's avatar
Ian Hickson committed
83 84 85
    );
    expect(
      (b1 + (b2 + b2)).toString(),
86 87
      'BorderDirectional(top: BorderSide(color: Color(0xff00ff00)), start: BorderSide(color: Color(0xff00ff00)), end: BorderSide(color: Color(0xff00ff00)), bottom: BorderSide(color: Color(0xff00ff00))) + '
      'BorderDirectional(top: BorderSide(color: Color(0xff0000ff), width: 2.0), start: BorderSide(color: Color(0xff0000ff), width: 2.0), end: BorderSide(color: Color(0xff0000ff), width: 2.0), bottom: BorderSide(color: Color(0xff0000ff), width: 2.0))',
Ian Hickson's avatar
Ian Hickson committed
88 89 90
    );
    expect(
      ((b1 + b2) + b2).toString(),
91 92
      'BorderDirectional(top: BorderSide(color: Color(0xff00ff00)), start: BorderSide(color: Color(0xff00ff00)), end: BorderSide(color: Color(0xff00ff00)), bottom: BorderSide(color: Color(0xff00ff00))) + '
      'BorderDirectional(top: BorderSide(color: Color(0xff0000ff), width: 2.0), start: BorderSide(color: Color(0xff0000ff), width: 2.0), end: BorderSide(color: Color(0xff0000ff), width: 2.0), bottom: BorderSide(color: Color(0xff0000ff), width: 2.0))',
Ian Hickson's avatar
Ian Hickson committed
93 94 95 96
    );
    expect((b1 + b2) + b2, b1 + (b2 + b2));
    expect(
      (b1 + b2).scale(3.0).toString(),
97 98
      'BorderDirectional(top: BorderSide(color: Color(0xff00ff00), width: 3.0), start: BorderSide(color: Color(0xff00ff00), width: 3.0), end: BorderSide(color: Color(0xff00ff00), width: 3.0), bottom: BorderSide(color: Color(0xff00ff00), width: 3.0)) + '
      'BorderDirectional(top: BorderSide(color: Color(0xff0000ff), width: 3.0), start: BorderSide(color: Color(0xff0000ff), width: 3.0), end: BorderSide(color: Color(0xff0000ff), width: 3.0), bottom: BorderSide(color: Color(0xff0000ff), width: 3.0))',
Ian Hickson's avatar
Ian Hickson committed
99 100 101
    );
    expect(
      (b1 + b2).scale(0.0).toString(),
102 103
      'BorderDirectional(top: BorderSide(color: Color(0xff00ff00), width: 0.0, style: none), start: BorderSide(color: Color(0xff00ff00), width: 0.0, style: none), end: BorderSide(color: Color(0xff00ff00), width: 0.0, style: none), bottom: BorderSide(color: Color(0xff00ff00), width: 0.0, style: none)) + '
      'BorderDirectional(top: BorderSide(color: Color(0xff0000ff), width: 0.0, style: none), start: BorderSide(color: Color(0xff0000ff), width: 0.0, style: none), end: BorderSide(color: Color(0xff0000ff), width: 0.0, style: none), bottom: BorderSide(color: Color(0xff0000ff), width: 0.0, style: none))',
Ian Hickson's avatar
Ian Hickson committed
104 105 106
    );
    expect(
      ShapeBorder.lerp(b2 + b1, b1 + b2, 0.0).toString(),
107 108
      'BorderDirectional(top: BorderSide(color: Color(0xff0000ff)), start: BorderSide(color: Color(0xff0000ff)), end: BorderSide(color: Color(0xff0000ff)), bottom: BorderSide(color: Color(0xff0000ff))) + '
      'BorderDirectional(top: BorderSide(color: Color(0xff00ff00)), start: BorderSide(color: Color(0xff00ff00)), end: BorderSide(color: Color(0xff00ff00)), bottom: BorderSide(color: Color(0xff00ff00)))',
Ian Hickson's avatar
Ian Hickson committed
109 110 111
    );
    expect(
      ShapeBorder.lerp(b2 + b1, b1 + b2, 0.25).toString(),
112 113
      'BorderDirectional(top: BorderSide(color: Color(0xff003fbf)), start: BorderSide(color: Color(0xff003fbf)), end: BorderSide(color: Color(0xff003fbf)), bottom: BorderSide(color: Color(0xff003fbf))) + '
      'BorderDirectional(top: BorderSide(color: Color(0xff00bf3f)), start: BorderSide(color: Color(0xff00bf3f)), end: BorderSide(color: Color(0xff00bf3f)), bottom: BorderSide(color: Color(0xff00bf3f)))',
Ian Hickson's avatar
Ian Hickson committed
114 115 116
    );
    expect(
      ShapeBorder.lerp(b2 + b1, b1 + b2, 0.5).toString(),
117 118
      'BorderDirectional(top: BorderSide(color: Color(0xff007f7f)), start: BorderSide(color: Color(0xff007f7f)), end: BorderSide(color: Color(0xff007f7f)), bottom: BorderSide(color: Color(0xff007f7f))) + '
      'BorderDirectional(top: BorderSide(color: Color(0xff007f7f)), start: BorderSide(color: Color(0xff007f7f)), end: BorderSide(color: Color(0xff007f7f)), bottom: BorderSide(color: Color(0xff007f7f)))',
Ian Hickson's avatar
Ian Hickson committed
119 120 121
    );
    expect(
      ShapeBorder.lerp(b2 + b1, b1 + b2, 1.0).toString(),
122 123
      'BorderDirectional(top: BorderSide(color: Color(0xff00ff00)), start: BorderSide(color: Color(0xff00ff00)), end: BorderSide(color: Color(0xff00ff00)), bottom: BorderSide(color: Color(0xff00ff00))) + '
      'BorderDirectional(top: BorderSide(color: Color(0xff0000ff)), start: BorderSide(color: Color(0xff0000ff)), end: BorderSide(color: Color(0xff0000ff)), bottom: BorderSide(color: Color(0xff0000ff)))',
Ian Hickson's avatar
Ian Hickson committed
124 125
    );
    expect((b1 + b2).dimensions, const EdgeInsetsDirectional.fromSTEB(2.0, 2.0, 2.0, 2.0));
Dan Field's avatar
Dan Field committed
126
    const Rect rect = Rect.fromLTRB(11.0, 15.0, 299.0, 175.0);
Ian Hickson's avatar
Ian Hickson committed
127 128
    expect((Canvas canvas) => (b1 + b2).paint(canvas, rect, textDirection: TextDirection.rtl), paints
      ..rect(rect: rect.deflate(0.5), color: b2.top.color)
129
      ..rect(rect: rect.deflate(1.5), color: b1.top.color),
Ian Hickson's avatar
Ian Hickson committed
130 131 132 133 134
    );
    expect((b1 + b2 + b1).dimensions, const EdgeInsetsDirectional.fromSTEB(3.0, 3.0, 3.0, 3.0));
    expect((Canvas canvas) => (b1 + b2 + b1).paint(canvas, rect, textDirection: TextDirection.rtl), paints
      ..rect(rect: rect.deflate(0.5), color: b1.top.color)
      ..rect(rect: rect.deflate(1.5), color: b2.top.color)
135
      ..rect(rect: rect.deflate(2.5), color: b1.top.color),
Ian Hickson's avatar
Ian Hickson committed
136 137
    );
  });
Ian Hickson's avatar
Ian Hickson committed
138
}