stack_test.dart 2.79 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1 2 3 4
// Copyright 2015 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
import 'package:flutter/rendering.dart';
6
import '../flutter_test_alternative.dart';
7

8
import 'rendering_tester.dart';
9 10 11

void main() {
  test('Stack can layout with top, right, bottom, left 0.0', () {
12 13
    final RenderBox size = RenderConstrainedBox(
      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 21
      child: size
    );
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;
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

  group('RenderIndexedStack', () {
    test('visitChildrenForSemantics only visits displayed child', () {
66 67
      final RenderBox child1 = RenderConstrainedBox(
          additionalConstraints: BoxConstraints.tight(const Size(100.0, 100.0))
68
      );
69 70
      final RenderBox child2 = RenderConstrainedBox(
          additionalConstraints: BoxConstraints.tight(const Size(100.0, 100.0))
71
      );
72 73
      final RenderBox child3 = RenderConstrainedBox(
          additionalConstraints: BoxConstraints.tight(const Size(100.0, 100.0))
74
      );
75
      final RenderBox stack = RenderIndexedStack(
76
          index: 1,
77 78
          textDirection: TextDirection.ltr,
          children: <RenderBox>[child1, child2, child3],
79 80
      );

Josh Soref's avatar
Josh Soref committed
81
      final List<RenderObject> visitedChildren = <RenderObject>[];
82
      final RenderObjectVisitor visitor = (RenderObject child) {
Josh Soref's avatar
Josh Soref committed
83
        visitedChildren.add(child);
84 85 86 87
      };

      stack.visitChildrenForSemantics(visitor);

Josh Soref's avatar
Josh Soref committed
88 89
      expect(visitedChildren, hasLength(1));
      expect(visitedChildren.first, child2);
90 91 92 93
    });

  });

94
  // More tests in ../widgets/stack_test.dart
95
}