shape_decoration_test.dart 5.08 KB
Newer Older
1 2 3 4
// Copyright 2017 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.

5 6
import 'dart:async';
import 'dart:typed_data';
7
import 'dart:ui' as ui;
8

9 10
import 'package:flutter/foundation.dart';
import 'package:flutter/painting.dart';
11 12
import 'package:flutter_test/flutter_test.dart';

13
import '../rendering/mock_canvas.dart';
14
import '../rendering/rendering_tester.dart';
15

16
void main() {
17 18
  new TestRenderingFlutterBinding(); // initializes the imageCache

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

  test('ShapeDecoration.lerp and hit test', () {
49 50
    const Decoration a = const ShapeDecoration(shape: const CircleBorder());
    const Decoration b = const ShapeDecoration(shape: const RoundedRectangleBorder());
51 52
    expect(Decoration.lerp(a, b, 0.0), a);
    expect(Decoration.lerp(a, b, 1.0), b);
53
    const Size size = const Size(200.0, 100.0); // at t=0.5, width will be 150 (x=25 to x=175).
54 55 56 57 58 59
    expect(a.hitTest(size, const Offset(20.0, 50.0)), isFalse);
    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);
    expect(b.hitTest(size, const Offset(20.0, 50.0)), isTrue);
  });
60 61 62 63 64 65 66 67 68 69 70

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

class TestImageProvider extends ImageProvider<TestImageProvider> {
  @override
  Future<TestImageProvider> obtainKey(ImageConfiguration configuration) {
    return new SynchronousFuture<TestImageProvider>(this);
  }

  @override
  ImageStreamCompleter load(TestImageProvider key) {
    return new OneFrameImageStreamCompleter(
      new SynchronousFuture<ImageInfo>(new ImageInfo(image: new TestImage(), scale: 1.0)),
    );
  }
}

117
class TestImage implements ui.Image {
118 119 120 121 122 123 124 125
  @override
  int get width => 100;

  @override
  int get height => 200;

  @override
  void dispose() { }
126 127

  @override
128
  Future<ByteData> toByteData({ui.ImageByteFormat format}) async {
129 130
    throw new UnsupportedError('Cannot encode test image');
  }
131
}