box_painter_test.dart 4.06 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5
// 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';
6
import 'package:flutter_test/flutter_test.dart';
7 8

void main() {
xster's avatar
xster committed
9
  test('BorderSide control test', () {
10
    const BorderSide side1 = BorderSide();
11
    final BorderSide side2 = side1.copyWith(
12 13 14 15 16 17 18 19 20 21 22 23 24 25
      color: const Color(0xFF00FFFF),
      width: 2.0,
      style: BorderStyle.solid,
    );

    expect(side1, hasOneLineDescription);
    expect(side1.hashCode, isNot(equals(side2.hashCode)));

    expect(side2.color, equals(const Color(0xFF00FFFF)));
    expect(side2.width, equals(2.0));
    expect(side2.style, equals(BorderStyle.solid));

    expect(BorderSide.lerp(side1, side2, 0.0), equals(side1));
    expect(BorderSide.lerp(side1, side2, 1.0), equals(side2));
26
    expect(BorderSide.lerp(side1, side2, 0.5), equals(BorderSide(
27
      color: Color.lerp(const Color(0xFF000000), const Color(0xFF00FFFF), 0.5)!,
28 29 30 31
      width: 1.5,
      style: BorderStyle.solid,
    )));

32
    final BorderSide side3 = side2.copyWith(style: BorderStyle.none);
33 34 35 36 37 38 39 40 41
    BorderSide interpolated = BorderSide.lerp(side2, side3, 0.2);
    expect(interpolated.style, equals(BorderStyle.solid));
    expect(interpolated.color, equals(side2.color.withOpacity(0.8)));

    interpolated = BorderSide.lerp(side3, side2, 0.2);
    expect(interpolated.style, equals(BorderStyle.solid));
    expect(interpolated.color, equals(side2.color.withOpacity(0.2)));
  });

42
  test('BorderSide toString test', () {
43
    const BorderSide side1 = BorderSide();
44 45 46 47 48 49 50 51 52 53
    final BorderSide side2 = side1.copyWith(
      color: const Color(0xFF00FFFF),
      width: 2.0,
      style: BorderStyle.solid,
    );

    expect(side1.toString(), equals('BorderSide(Color(0xff000000), 1.0, BorderStyle.solid)'));
    expect(side2.toString(), equals('BorderSide(Color(0xff00ffff), 2.0, BorderStyle.solid)'));
  });

xster's avatar
xster committed
54
  test('Border control test', () {
55
    final Border border1 = Border.all(width: 4.0);
56 57
    final Border border2 = Border.lerp(null, border1, 0.25)!;
    final Border border3 = Border.lerp(border1, null, 0.25)!;
58 59 60 61 62 63 64

    expect(border1, hasOneLineDescription);
    expect(border1.hashCode, isNot(equals(border2.hashCode)));

    expect(border2.top.width, equals(1.0));
    expect(border3.bottom.width, equals(3.0));

65
    final Border border4 = Border.lerp(border2, border3, 0.5)!;
66 67 68
    expect(border4.left.width, equals(2.0));
  });

69 70
  test('Border toString test', () {
    expect(
71
      Border.all(width: 4.0).toString(),
72
      equals(
73 74 75 76 77
        'Border.all(BorderSide(Color(0xff000000), 4.0, BorderStyle.solid))',
      ),
    );
    expect(
      const Border(
78 79 80 81
        top: BorderSide(width: 3.0),
        right: BorderSide(width: 3.0),
        bottom: BorderSide(width: 3.0),
        left: BorderSide(width: 3.0),
82 83 84
      ).toString(),
      equals(
        'Border.all(BorderSide(Color(0xff000000), 3.0, BorderStyle.solid))',
85 86 87 88
      ),
    );
  });

xster's avatar
xster committed
89
  test('BoxShadow control test', () {
90
    const BoxShadow shadow1 = BoxShadow(blurRadius: 4.0);
91 92
    final BoxShadow shadow2 = BoxShadow.lerp(null, shadow1, 0.25)!;
    final BoxShadow shadow3 = BoxShadow.lerp(shadow1, null, 0.25)!;
93 94 95

    expect(shadow1, hasOneLineDescription);
    expect(shadow1.hashCode, isNot(equals(shadow2.hashCode)));
96
    expect(shadow1, equals(const BoxShadow(blurRadius: 4.0)));
97 98 99 100

    expect(shadow2.blurRadius, equals(1.0));
    expect(shadow3.blurRadius, equals(3.0));

101
    final BoxShadow shadow4 = BoxShadow.lerp(shadow2, shadow3, 0.5)!;
102 103 104
    expect(shadow4.blurRadius, equals(2.0));

    List<BoxShadow> shadowList = BoxShadow.lerpList(
105
        <BoxShadow>[shadow2, shadow1], <BoxShadow>[shadow3], 0.5)!;
106 107
    expect(shadowList, equals(<BoxShadow>[shadow4, shadow1.scale(0.5)]));
    shadowList = BoxShadow.lerpList(
108
        <BoxShadow>[shadow2], <BoxShadow>[shadow3, shadow1], 0.5)!;
109 110
    expect(shadowList, equals(<BoxShadow>[shadow4, shadow1.scale(0.5)]));
  });
xster's avatar
xster committed
111

112 113 114
  test('BoxShadow toString test', () {
    expect(const BoxShadow(blurRadius: 4.0).toString(), equals('BoxShadow(Color(0xff000000), Offset(0.0, 0.0), 4.0, 0.0)'));
  });
115
}