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

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';

import 'semantics_tester.dart';

void main() {
  testWidgets('SemanticNode.rect is clipped', (WidgetTester tester) async {
13
    final SemanticsTester semantics = SemanticsTester(tester);
14

15
    await tester.pumpWidget(Directionality(
16
      textDirection: TextDirection.ltr,
17
      child: Center(
18
        child: SizedBox(
19
          width: 100.0,
20
          child: Flex(
21
            clipBehavior: Clip.hardEdge,
22
            direction: Axis.horizontal,
23 24
            children: const <Widget>[
              SizedBox(
25
                width: 75.0,
26
                child: Text('1'),
27
              ),
28
              SizedBox(
29
                width: 75.0,
30
                child: Text('2'),
31
              ),
32
              SizedBox(
33
                width: 75.0,
34
                child: Text('3'),
35 36 37 38 39 40 41
              ),
            ],
          ),
        ),
      ),
    ));

42
    final dynamic exception = tester.takeException();
Dan Field's avatar
Dan Field committed
43
    expect(exception, isFlutterError);
44
    // ignore: avoid_dynamic_calls
45
    expect(exception.diagnostics.first.level, DiagnosticLevel.summary);
46
    // ignore: avoid_dynamic_calls
47
    expect(exception.diagnostics.first.toString(), contains('overflowed'));
48

49
    expect(semantics, hasSemantics(
50
      TestSemantics.root(
51
        children: <TestSemantics>[
52
          TestSemantics(
53
            label: '1',
Dan Field's avatar
Dan Field committed
54
            rect: const Rect.fromLTRB(0.0, 0.0, 75.0, 14.0),
55
          ),
56
          TestSemantics(
57
            label: '2',
Dan Field's avatar
Dan Field committed
58
            rect: const Rect.fromLTRB(0.0, 0.0, 25.0, 14.0), // clipped form original 75.0 to 25.0
59 60 61 62 63
          ),
          // node with Text 3 not present.
        ],
      ),
      ignoreTransform: true,
64
      ignoreId: true,
65 66 67
    ));

    semantics.dispose();
68
  });
69 70

  testWidgets('SemanticsNode is not removed if out of bounds and merged into something within bounds', (WidgetTester tester) async {
71
    final SemanticsTester semantics = SemanticsTester(tester);
72

73
    await tester.pumpWidget(Directionality(
74
      textDirection: TextDirection.ltr,
75
      child: Center(
76
        child: SizedBox(
77
          width: 100.0,
78
          child: Flex(
79
            clipBehavior: Clip.hardEdge,
80 81
            direction: Axis.horizontal,
            children: <Widget>[
82
              const SizedBox(
83
                width: 75.0,
84
                child: Text('1'),
85
              ),
86 87
              MergeSemantics(
                child: Flex(
88
                  direction: Axis.horizontal,
89 90
                  children: const <Widget>[
                    SizedBox(
91
                      width: 75.0,
92
                      child: Text('2'),
93
                    ),
94
                    SizedBox(
95
                      width: 75.0,
96
                      child: Text('3'),
97
                    ),
98 99
                  ],
                ),
100 101 102 103 104 105 106
              ),
            ],
          ),
        ),
      ),
    ));

107
    final dynamic exception = tester.takeException();
Dan Field's avatar
Dan Field committed
108
    expect(exception, isFlutterError);
109
    // ignore: avoid_dynamic_calls
110
    expect(exception.diagnostics.first.level, DiagnosticLevel.summary);
111
    // ignore: avoid_dynamic_calls
112
    expect(exception.diagnostics.first.toString(), contains('overflowed'));
113

114
    expect(semantics, hasSemantics(
115
      TestSemantics.root(
116
        children: <TestSemantics>[
117
          TestSemantics(
118
            label: '1',
Dan Field's avatar
Dan Field committed
119
            rect: const Rect.fromLTRB(0.0, 0.0, 75.0, 14.0),
120
          ),
121
          TestSemantics(
122
            label: '2\n3',
Dan Field's avatar
Dan Field committed
123
            rect: const Rect.fromLTRB(0.0, 0.0, 25.0, 14.0), // clipped form original 75.0 to 25.0
124 125 126 127
          ),
        ],
      ),
      ignoreTransform: true,
128
      ignoreId: true,
129 130 131
    ));

    semantics.dispose();
132
  });
133
}