box_decoration_test.dart 3.58 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
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_test/flutter_test.dart';
import 'package:flutter/painting.dart';

import '../rendering/mock_canvas.dart';

void main() {
  test('BoxDecoration with BorderRadiusDirectional', () {
12 13 14
    const BoxDecoration decoration = BoxDecoration(
      color: Color(0xFF000000),
      borderRadius: BorderRadiusDirectional.only(topStart: Radius.circular(100.0)),
15 16
    );
    final BoxPainter painter = decoration.createBoxPainter();
17
    const Size size = Size(1000.0, 1000.0);
18 19 20 21
    expect(
      (Canvas canvas) {
        painter.paint(
          canvas,
22
          Offset.zero,
23 24 25 26
          const ImageConfiguration(size: size, textDirection: TextDirection.rtl),
        );
      },
      paints
27
        ..rrect(rrect: RRect.fromRectAndCorners(Offset.zero & size, topRight: const Radius.circular(100.0))),
28 29 30 31 32 33 34
    );
    expect(decoration.hitTest(size, const Offset(10.0, 10.0), textDirection: TextDirection.rtl), isTrue);
    expect(decoration.hitTest(size, const Offset(990.0, 10.0), textDirection: TextDirection.rtl), isFalse);
    expect(
      (Canvas canvas) {
        painter.paint(
          canvas,
35
          Offset.zero,
36 37 38 39
          const ImageConfiguration(size: size, textDirection: TextDirection.ltr),
        );
      },
      paints
40
        ..rrect(rrect: RRect.fromRectAndCorners(Offset.zero & size, topLeft: const Radius.circular(100.0))),
41 42 43 44
    );
    expect(decoration.hitTest(size, const Offset(10.0, 10.0), textDirection: TextDirection.ltr), isFalse);
    expect(decoration.hitTest(size, const Offset(990.0, 10.0), textDirection: TextDirection.ltr), isTrue);
  });
45 46

  test('BoxDecoration with LinearGradient using AlignmentDirectional', () {
47 48 49
    const BoxDecoration decoration = BoxDecoration(
      color: Color(0xFF000000),
      gradient: LinearGradient(
50 51
        begin: AlignmentDirectional.centerStart,
        end: AlignmentDirectional.bottomEnd,
52 53 54
        colors: <Color>[
          Color(0xFF000000),
          Color(0xFFFFFFFF),
55 56 57 58
        ],
      ),
    );
    final BoxPainter painter = decoration.createBoxPainter();
59
    const Size size = Size(1000.0, 1000.0);
60
    expect(
61
      (Canvas canvas) {
62 63
        painter.paint(
          canvas,
64
          Offset.zero,
65 66 67 68 69 70
          const ImageConfiguration(size: size, textDirection: TextDirection.rtl),
        );
      },
      paints..rect(rect: Offset.zero & size),
    );
  });
71

72
  test('BoxDecoration.getClipPath with borderRadius', () {
73 74 75 76 77 78 79 80 81 82 83 84
    const double radius = 10;
    final BoxDecoration decoration = BoxDecoration(
      borderRadius: BorderRadius.circular(radius),
    );
    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(30.0, 10.0), Offset(50.0, 10.0), ],
      excludes: const <Offset>[ Offset(1.0, 1.0), Offset(99.0, 19.0), ],
    );
    expect(clipPath, isLookLikeExpectedPath);
  });
85 86 87 88 89 90 91 92 93 94 95 96 97

  test('BoxDecoration.getClipPath with shape BoxShape.circle', () {
    const BoxDecoration decoration = BoxDecoration(
      shape: BoxShape.circle,
    );
    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, 0.0), Offset(40.0, 10.0), ],
      excludes: const <Offset>[ Offset(40.0, 0.0), Offset(10.0, 10.0), ],
    );
    expect(clipPath, isLookLikeExpectedPath);
  });
98
}