annotated_region_test.dart 5.51 KB
Newer Older
1 2 3 4 5 6
// Copyright 2018 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.

import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
7
import '../flutter_test_alternative.dart';
8 9 10 11

void main() {
  group(AnnotatedRegion, () {
    test('finds the first value in a OffsetLayer when sized', () {
12
      final ContainerLayer containerLayer = ContainerLayer();
13
      final List<OffsetLayer> layers = <OffsetLayer>[
14 15 16
        OffsetLayer(offset: Offset.zero),
        OffsetLayer(offset: const Offset(0.0, 100.0)),
        OffsetLayer(offset: const Offset(0.0, 200.0)),
17 18 19
      ];
      int i = 0;
      for (OffsetLayer layer in layers) {
20
        layer.append(AnnotatedRegionLayer<int>(i, size: const Size(200.0, 100.0)));
21 22 23 24 25 26 27 28 29 30
        containerLayer.append(layer);
        i += 1;
      }

      expect(containerLayer.find<int>(const Offset(0.0, 1.0)), 0);
      expect(containerLayer.find<int>(const Offset(0.0, 101.0)), 1);
      expect(containerLayer.find<int>(const Offset(0.0, 201.0)), 2);
    });

    test('finds a value within the clip in a ClipRectLayer', () {
31
      final ContainerLayer containerLayer = ContainerLayer();
32
      final List<ClipRectLayer> layers = <ClipRectLayer>[
33 34 35
        ClipRectLayer(clipRect: Rect.fromLTRB(0.0, 0.0, 100.0, 100.0)),
        ClipRectLayer(clipRect: Rect.fromLTRB(0.0, 100.0, 100.0, 200.0)),
        ClipRectLayer(clipRect: Rect.fromLTRB(0.0, 200.0, 100.0, 300.0)),
36 37 38
      ];
      int i = 0;
      for (ClipRectLayer layer in layers) {
39
        layer.append(AnnotatedRegionLayer<int>(i));
40 41 42 43 44 45 46 47 48 49 50
        containerLayer.append(layer);
        i += 1;
      }

      expect(containerLayer.find<int>(const Offset(0.0, 1.0)), 0);
      expect(containerLayer.find<int>(const Offset(0.0, 101.0)), 1);
      expect(containerLayer.find<int>(const Offset(0.0, 201.0)), 2);
    });


    test('finds a value within the clip in a ClipRRectLayer', () {
51
      final ContainerLayer containerLayer = ContainerLayer();
52
      final List<ClipRRectLayer> layers = <ClipRRectLayer>[
53 54 55
        ClipRRectLayer(clipRRect: RRect.fromLTRBR(0.0, 0.0, 100.0, 100.0, const Radius.circular(4.0))),
        ClipRRectLayer(clipRRect: RRect.fromLTRBR(0.0, 100.0, 100.0, 200.0, const Radius.circular(4.0))),
        ClipRRectLayer(clipRRect: RRect.fromLTRBR(0.0, 200.0, 100.0, 300.0, const Radius.circular(4.0))),
56 57 58
      ];
      int i = 0;
      for (ClipRRectLayer layer in layers) {
59
        layer.append(AnnotatedRegionLayer<int>(i));
60 61 62 63 64 65 66 67 68 69
        containerLayer.append(layer);
        i += 1;
      }

      expect(containerLayer.find<int>(const Offset(5.0, 5.0)), 0);
      expect(containerLayer.find<int>(const Offset(5.0, 105.0)), 1);
      expect(containerLayer.find<int>(const Offset(5.0, 205.0)), 2);
    });

    test('finds a value under a TransformLayer', () {
70
      final Matrix4 transform = Matrix4(
71 72 73 74 75
        2.625, 0.0, 0.0, 0.0,
        0.0, 2.625, 0.0, 0.0,
        0.0, 0.0, 1.0, 0.0,
        0.0, 0.0, 0.0, 1.0,
      );
76
      final TransformLayer transformLayer = TransformLayer(transform: transform);
77
      final List<OffsetLayer> layers = <OffsetLayer>[
78 79 80
        OffsetLayer(),
        OffsetLayer(offset: const Offset(0.0, 100.0)),
        OffsetLayer(offset: const Offset(0.0, 200.0)),
81 82 83
      ];
      int i = 0;
      for (OffsetLayer layer in layers) {
84
        final AnnotatedRegionLayer<int> annotatedRegionLayer = AnnotatedRegionLayer<int>(i, size: const Size(100.0, 100.0));
85 86 87 88 89 90 91 92 93 94 95 96 97
        layer.append(annotatedRegionLayer);
        transformLayer.append(layer);
        i += 1;
      }

      expect(transformLayer.find<int>(const Offset(0.0, 100.0)), 0);
      expect(transformLayer.find<int>(const Offset(0.0, 200.0)), 0);
      expect(transformLayer.find<int>(const Offset(0.0, 270.0)), 1);
      expect(transformLayer.find<int>(const Offset(0.0, 400.0)), 1);
      expect(transformLayer.find<int>(const Offset(0.0, 530.0)), 2);
    });

    test('looks for child AnnotatedRegions before parents', () {
98 99 100
      final AnnotatedRegionLayer<int> parent = AnnotatedRegionLayer<int>(1);
      final AnnotatedRegionLayer<int> child = AnnotatedRegionLayer<int>(2);
      final ContainerLayer layer = ContainerLayer();
101 102 103 104 105 106 107
      parent.append(child);
      layer.append(parent);

      expect(parent.find<int>(Offset.zero), 2);
    });

    test('looks for correct type', () {
108 109 110
      final AnnotatedRegionLayer<int> child1 = AnnotatedRegionLayer<int>(1);
      final AnnotatedRegionLayer<String> child2 = AnnotatedRegionLayer<String>('hello');
      final ContainerLayer layer = ContainerLayer();
111 112 113 114 115 116 117
      layer.append(child2);
      layer.append(child1);

      expect(layer.find<String>(Offset.zero), 'hello');
    });

    test('does not clip Layer.find on an AnnotatedRegion with an unrelated type', () {
118 119 120
      final AnnotatedRegionLayer<int> child = AnnotatedRegionLayer<int>(1);
      final AnnotatedRegionLayer<String> parent = AnnotatedRegionLayer<String>('hello', size: const Size(10.0, 10.0));
      final ContainerLayer layer = ContainerLayer();
121 122 123 124 125
      parent.append(child);
      layer.append(parent);

      expect(layer.find<int>(const Offset(100.0, 100.0)), 1);
    });
126 127

    test('handles non-invertable transforms', () {
128 129
      final AnnotatedRegionLayer<int> child = AnnotatedRegionLayer<int>(1);
      final TransformLayer parent = TransformLayer(transform: Matrix4.diagonal3Values(0.0, 1.0, 1.0));
130 131 132 133
      parent.append(child);

      expect(parent.find<int>(const Offset(0.0, 0.0)), null);

134
      parent.transform = Matrix4.diagonal3Values(1.0, 1.0, 1.0);
135 136 137

      expect(parent.find<int>(const Offset(0.0, 0.0)), 1);
    });
138 139 140
  });
}