shape_decoration_test.dart 6.36 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 'dart:ui' as ui;
6

7 8
import 'package:flutter/foundation.dart';
import 'package:flutter/painting.dart';
9 10
import 'package:flutter_test/flutter_test.dart';

11
import '../rendering/mock_canvas.dart';
12
import '../rendering/rendering_tester.dart';
13

14
void main() {
15
  TestRenderingFlutterBinding.ensureInitialized();
16

17
  test('ShapeDecoration constructor', () {
18 19 20 21
    const Color colorR = Color(0xffff0000);
    const Color colorG = Color(0xff00ff00);
    const Gradient gradient = LinearGradient(colors: <Color>[colorR, colorG]);
    expect(const ShapeDecoration(shape: Border()), const ShapeDecoration(shape: Border()));
22
    expect(() => ShapeDecoration(color: colorR, gradient: nonconst(gradient), shape: const Border()), throwsAssertionError);
23
    expect(
24
      ShapeDecoration.fromBoxDecoration(const BoxDecoration(shape: BoxShape.circle)),
25
      const ShapeDecoration(shape: CircleBorder()),
26 27
    );
    expect(
28
      ShapeDecoration.fromBoxDecoration(BoxDecoration(borderRadius: BorderRadiusDirectional.circular(100.0))),
29
      ShapeDecoration(shape: RoundedRectangleBorder(borderRadius: BorderRadiusDirectional.circular(100.0))),
30 31
    );
    expect(
32
      ShapeDecoration.fromBoxDecoration(BoxDecoration(shape: BoxShape.circle, border: Border.all(color: colorG))),
33
      const ShapeDecoration(shape: CircleBorder(side: BorderSide(color: colorG))),
34 35
    );
    expect(
36
      ShapeDecoration.fromBoxDecoration(BoxDecoration(border: Border.all(color: colorR))),
37
      ShapeDecoration(shape: Border.all(color: colorR)),
38 39
    );
    expect(
40
      ShapeDecoration.fromBoxDecoration(const BoxDecoration(border: BorderDirectional(start: BorderSide()))),
41
      const ShapeDecoration(shape: BorderDirectional(start: BorderSide())),
42 43 44 45
    );
  });

  test('ShapeDecoration.lerp and hit test', () {
46 47
    const Decoration a = ShapeDecoration(shape: CircleBorder());
    const Decoration b = ShapeDecoration(shape: RoundedRectangleBorder());
48
    const Decoration c = ShapeDecoration(shape: OvalBorder());
49 50
    expect(Decoration.lerp(a, b, 0.0), a);
    expect(Decoration.lerp(a, b, 1.0), b);
51 52 53 54
    expect(Decoration.lerp(a, c, 0.0), a);
    expect(Decoration.lerp(a, c, 1.0), c);
    expect(Decoration.lerp(b, c, 0.0), b);
    expect(Decoration.lerp(b, c, 1.0), c);
55
    const Size size = Size(200.0, 100.0); // at t=0.5, width will be 150 (x=25 to x=175).
56
    expect(a.hitTest(size, const Offset(20.0, 50.0)), isFalse);
57 58
    expect(c.hitTest(size, const Offset(50, 5.0)), isFalse);
    expect(c.hitTest(size, const Offset(5, 30.0)), isFalse);
59 60 61
    expect(Decoration.lerp(a, b, 0.1)!.hitTest(size, const Offset(20.0, 50.0)), isFalse);
    expect(Decoration.lerp(a, b, 0.5)!.hitTest(size, const Offset(20.0, 50.0)), isFalse);
    expect(Decoration.lerp(a, b, 0.9)!.hitTest(size, const Offset(20.0, 50.0)), isTrue);
62 63 64 65 66 67
    expect(Decoration.lerp(a, c, 0.1)!.hitTest(size, const Offset(30.0, 50.0)), isFalse);
    expect(Decoration.lerp(a, c, 0.5)!.hitTest(size, const Offset(30.0, 50.0)), isTrue);
    expect(Decoration.lerp(a, c, 0.9)!.hitTest(size, const Offset(30.0, 50.0)), isTrue);
    expect(Decoration.lerp(b, c, 0.1)!.hitTest(size, const Offset(45.0, 10.0)), isTrue);
    expect(Decoration.lerp(b, c, 0.5)!.hitTest(size, const Offset(30.0, 10.0)), isTrue);
    expect(Decoration.lerp(b, c, 0.9)!.hitTest(size, const Offset(10.0, 30.0)), isTrue);
68 69
    expect(b.hitTest(size, const Offset(20.0, 50.0)), isTrue);
  });
70

71 72
  test('ShapeDecoration.image RTL test', () async {
    final ui.Image image = await createTestImage(width: 100, height: 200);
73
    final List<int> log = <int>[];
74
    final ShapeDecoration decoration = ShapeDecoration(
75
      shape: const CircleBorder(),
76
      image: DecorationImage(
77
        image: TestImageProvider(image),
78 79 80 81
        alignment: AlignmentDirectional.bottomEnd,
      ),
    );
    final BoxPainter painter = decoration.createBoxPainter(() { log.add(0); });
82
    expect((Canvas canvas) => painter.paint(canvas, Offset.zero, const ImageConfiguration(size: Size(100.0, 100.0))), paintsAssertion);
83 84 85 86 87 88
    expect(
      (Canvas canvas) {
        return painter.paint(
          canvas,
          const Offset(20.0, -40.0),
          const ImageConfiguration(
89
            size: Size(1000.0, 1000.0),
90 91 92 93 94
            textDirection: TextDirection.rtl,
          ),
        );
      },
      paints
Dan Field's avatar
Dan Field committed
95
        ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 100.0, 200.0), destination: const Rect.fromLTRB(20.0, 1000.0 - 40.0 - 200.0, 20.0 + 100.0, 1000.0 - 40.0)),
96 97 98 99 100 101 102
    );
    expect(
      (Canvas canvas) {
        return painter.paint(
          canvas,
          Offset.zero,
          const ImageConfiguration(
103
            size: Size(100.0, 200.0),
104 105 106 107
            textDirection: TextDirection.ltr,
          ),
        );
      },
108
      isNot(paints..image()), // we always use drawImageRect
109 110 111
    );
    expect(log, isEmpty);
  });
112 113

  test('ShapeDecoration.getClipPath', () {
114
    const ShapeDecoration decoration = ShapeDecoration(shape: CircleBorder());
115 116 117 118 119 120 121 122
    const Rect rect = Rect.fromLTWH(0.0, 0.0, 100.0, 20.0);
    final Path clipPath = decoration.getClipPath(rect, TextDirection.ltr);
    final Matcher isLookLikeExpectedPath = isPathThat(
      includes: const <Offset>[ Offset(50.0, 10.0), ],
      excludes: const <Offset>[ Offset(1.0, 1.0), Offset(30.0, 10.0), Offset(99.0, 19.0), ],
    );
    expect(clipPath, isLookLikeExpectedPath);
  });
123 124 125 126 127 128 129 130 131 132
  test('ShapeDecoration.getClipPath for oval', () {
    const ShapeDecoration decoration = ShapeDecoration(shape: OvalBorder());
    const Rect rect = Rect.fromLTWH(0.0, 0.0, 100.0, 50.0);
    final Path clipPath = decoration.getClipPath(rect, TextDirection.ltr);
    final Matcher isLookLikeExpectedPath = isPathThat(
      includes: const <Offset>[ Offset(50.0, 10.0), ],
      excludes: const <Offset>[ Offset(1.0, 1.0), Offset(15.0, 1.0), Offset(99.0, 19.0), ],
    );
    expect(clipPath, isLookLikeExpectedPath);
  });
133 134 135
}

class TestImageProvider extends ImageProvider<TestImageProvider> {
136 137 138 139
  TestImageProvider(this.image);

  final ui.Image image;

140 141
  @override
  Future<TestImageProvider> obtainKey(ImageConfiguration configuration) {
142
    return SynchronousFuture<TestImageProvider>(this);
143 144 145
  }

  @override
146
  ImageStreamCompleter load(TestImageProvider key, DecoderCallback decode) {
147
    return OneFrameImageStreamCompleter(
148
      SynchronousFuture<ImageInfo>(ImageInfo(image: image)),
149 150 151
    );
  }
}