notched_shapes_test.dart 3.87 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
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:math' as math;

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

void main() {
  group('CircularNotchedRectangle', () {
12
    test("guest and host don't overlap", () {
13
      const CircularNotchedRectangle shape = CircularNotchedRectangle();
Dan Field's avatar
Dan Field committed
14 15
      const Rect host = Rect.fromLTRB(0.0, 100.0, 300.0, 300.0);
      const Rect guest = Rect.fromLTWH(50.0, 50.0, 10.0, 10.0);
16 17

      final Path actualPath = shape.getOuterPath(host, guest);
18
      final Path expectedPath = Path()..addRect(host);
19 20 21 22 23 24 25

      expect(
        actualPath,
        coversSameAreaAs(
          expectedPath,
          areaToCompare: host.inflate(5.0),
          sampleSize: 40,
26
        ),
27 28 29 30
      );
    });

    test('guest center above host', () {
31
      const CircularNotchedRectangle shape = CircularNotchedRectangle();
Dan Field's avatar
Dan Field committed
32 33
      const Rect host = Rect.fromLTRB(0.0, 100.0, 300.0, 300.0);
      const Rect guest = Rect.fromLTRB(190.0, 85.0, 210.0, 105.0);
34 35 36 37 38 39 40

      final Path actualPath = shape.getOuterPath(host, guest);

      expect(pathDoesNotContainCircle(actualPath, guest), isTrue);
    });

    test('guest center below host', () {
41
      const CircularNotchedRectangle shape = CircularNotchedRectangle();
Dan Field's avatar
Dan Field committed
42 43
      const Rect host = Rect.fromLTRB(0.0, 100.0, 300.0, 300.0);
      const Rect guest = Rect.fromLTRB(190.0, 95.0, 210.0, 115.0);
44 45 46 47 48 49

      final Path actualPath = shape.getOuterPath(host, guest);

      expect(pathDoesNotContainCircle(actualPath, guest), isTrue);
    });

50
    test('no guest is ok', () {
Dan Field's avatar
Dan Field committed
51
      const Rect host = Rect.fromLTRB(0.0, 100.0, 300.0, 300.0);
52 53 54 55 56 57
      expect(
        const CircularNotchedRectangle().getOuterPath(host, null),
        coversSameAreaAs(
          Path()..addRect(host),
          areaToCompare: host.inflate(800.0),
          sampleSize: 100,
58
        ),
59 60 61 62 63 64 65 66 67
      );
    });

    test('AutomaticNotchedShape - with guest', () {
      expect(
        const AutomaticNotchedShape(
          RoundedRectangleBorder(),
          RoundedRectangleBorder(),
        ).getOuterPath(
Dan Field's avatar
Dan Field committed
68 69
          const Rect.fromLTWH(-200.0, -100.0, 50.0, 100.0),
          const Rect.fromLTWH(-175.0, -110.0, 100.0, 100.0),
70 71 72 73 74 75 76 77 78 79
        ),
        coversSameAreaAs(
          Path()
            ..moveTo(-200.0, -100.0)
            ..lineTo(-150.0, -100.0)
            ..lineTo(-150.0, -10.0)
            ..lineTo(-175.0, -10.0)
            ..lineTo(-175.0, 0.0)
            ..lineTo(-200.0, 0.0)
            ..close(),
Dan Field's avatar
Dan Field committed
80
          areaToCompare: const Rect.fromLTWH(-300.0, -300.0, 600.0, 600.0),
81
          sampleSize: 100,
82
        ),
83
      );
84
    }, skip: isBrowser); // https://github.com/flutter/flutter/issues/44572
85 86 87 88 89 90 91

    test('AutomaticNotchedShape - no guest', () {
      expect(
        const AutomaticNotchedShape(
          RoundedRectangleBorder(),
          RoundedRectangleBorder(),
        ).getOuterPath(
Dan Field's avatar
Dan Field committed
92
          const Rect.fromLTWH(-200.0, -100.0, 50.0, 100.0),
93 94 95 96 97 98 99 100 101
          null,
        ),
        coversSameAreaAs(
          Path()
            ..moveTo(-200.0, -100.0)
            ..lineTo(-150.0, -100.0)
            ..lineTo(-150.0, 0.0)
            ..lineTo(-200.0, 0.0)
            ..close(),
Dan Field's avatar
Dan Field committed
102
          areaToCompare: const Rect.fromLTWH(-300.0, -300.0, 600.0, 600.0),
103
          sampleSize: 100,
104
        ),
105 106
      );
    });
107 108 109 110 111 112 113 114 115 116 117
  });
}

bool pathDoesNotContainCircle(Path path, Rect circleBounds) {
  assert(circleBounds.width == circleBounds.height);
  final double radius = circleBounds.width / 2.0;

  for (double theta = 0.0; theta <= 2.0 * math.pi; theta += math.pi / 20.0) {
    for (double i = 0.0; i < 1; i += 0.01) {
      final double x = i * radius * math.cos(theta);
      final double y = i * radius * math.sin(theta);
118
      if (path.contains(Offset(x,y) + circleBounds.center)) {
119
        return false;
120
      }
121 122 123 124
    }
  }
  return true;
}