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

8
import 'rendering_tester.dart';
9 10 11

void main() {
  test('Stack can layout with top, right, bottom, left 0.0', () {
12
    final RenderBox size = RenderConstrainedBox(
13
      additionalConstraints: BoxConstraints.tight(const Size(100.0, 100.0)),
14
    );
15

16
    final RenderBox red = RenderDecoratedBox(
17
      decoration: const BoxDecoration(
18
        color: Color(0xFFFF0000),
19
      ),
20
      child: size,
21
    );
22

23
    final RenderBox green = RenderDecoratedBox(
24
      decoration: const BoxDecoration(
25
        color: Color(0xFFFF0000),
26
      ),
27
    );
28

29
    final RenderBox stack = RenderStack(
30 31 32
      textDirection: TextDirection.ltr,
      children: <RenderBox>[red, green],
    );
33
    final StackParentData greenParentData = green.parentData! as StackParentData;
Hixie's avatar
Hixie committed
34
    greenParentData
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
      ..top = 0.0
      ..right = 0.0
      ..bottom = 0.0
      ..left = 0.0;

    layout(stack, constraints: const BoxConstraints());

    expect(stack.size.width, equals(100.0));
    expect(stack.size.height, equals(100.0));

    expect(red.size.width, equals(100.0));
    expect(red.size.height, equals(100.0));

    expect(green.size.width, equals(100.0));
    expect(green.size.height, equals(100.0));
  });
51

52
  test('Stack can layout with no children', () {
53
    final RenderBox stack = RenderStack(
54 55 56 57
      textDirection: TextDirection.ltr,
      children: <RenderBox>[],
    );

58
    layout(stack, constraints: BoxConstraints.tight(const Size(100.0, 100.0)));
59 60 61 62

    expect(stack.size.width, equals(100.0));
    expect(stack.size.height, equals(100.0));
  });
63

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
  test('Stack respects clipBehavior', () {
    const BoxConstraints viewport = BoxConstraints(maxHeight: 100.0, maxWidth: 100.0);
    final TestClipPaintingContext context = TestClipPaintingContext();

    // By default, clipBehavior should be Clip.none
    final RenderStack defaultStack = RenderStack(textDirection: TextDirection.ltr, children: <RenderBox>[box200x200]);
    layout(defaultStack, constraints: viewport, phase: EnginePhase.composite, onErrors: expectOverflowedErrors);
    defaultStack.paint(context, Offset.zero);
    expect(context.clipBehavior, equals(Clip.none));

    for (final Clip clip in Clip.values) {
      final RenderBox child = box200x200;
      final RenderStack stack = RenderStack(
          textDirection: TextDirection.ltr,
          children: <RenderBox>[child],
          clipBehavior: clip,
      );
      { // Make sure that the child is positioned so the stack will consider it as overflowed.
82
        final StackParentData parentData = child.parentData! as StackParentData;
83 84 85
        parentData.left = parentData.right = 0;
      }
      layout(stack, constraints: viewport, phase: EnginePhase.composite, onErrors: expectOverflowedErrors);
86
      context.paintChild(stack, Offset.zero);
87 88 89 90
      expect(context.clipBehavior, equals(clip));
    }
  });

91 92
  group('RenderIndexedStack', () {
    test('visitChildrenForSemantics only visits displayed child', () {
93
      final RenderBox child1 = RenderConstrainedBox(
94
        additionalConstraints: BoxConstraints.tight(const Size(100.0, 100.0)),
95
      );
96
      final RenderBox child2 = RenderConstrainedBox(
97
        additionalConstraints: BoxConstraints.tight(const Size(100.0, 100.0)),
98
      );
99
      final RenderBox child3 = RenderConstrainedBox(
100
        additionalConstraints: BoxConstraints.tight(const Size(100.0, 100.0)),
101
      );
102
      final RenderBox stack = RenderIndexedStack(
103 104 105
        index: 1,
        textDirection: TextDirection.ltr,
        children: <RenderBox>[child1, child2, child3],
106 107
      );

Josh Soref's avatar
Josh Soref committed
108
      final List<RenderObject> visitedChildren = <RenderObject>[];
109
      void visitor(RenderObject child) {
Josh Soref's avatar
Josh Soref committed
110
        visitedChildren.add(child);
111
      }
112 113 114

      stack.visitChildrenForSemantics(visitor);

Josh Soref's avatar
Josh Soref committed
115 116
      expect(visitedChildren, hasLength(1));
      expect(visitedChildren.first, child2);
117 118 119 120
    });

  });

121
  // More tests in ../widgets/stack_test.dart
122
}