implicit_semantics_test.dart 6.87 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
// @dart = 2.8

7 8 9 10 11 12 13 14 15
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';

import 'semantics_tester.dart';

void main() {
  testWidgets('Implicit Semantics merge behavior', (WidgetTester tester) async {
16
    final SemanticsTester semantics = SemanticsTester(tester);
17 18

    await tester.pumpWidget(
19
      Directionality(
20
        textDirection: TextDirection.ltr,
21
        child: Semantics(
22 23
          container: true,
          explicitChildNodes: false,
24
          child: Column(
25
            children: const <Widget>[
26 27
              Text('Michael Goderbauer'),
              Text('goderbauer@google.com'),
28 29 30 31 32 33 34 35 36 37 38
            ],
          ),
        ),
      ),
    );

    // SemanticsNode#0()
    //  └SemanticsNode#1(label: "Michael Goderbauer\ngoderbauer@google.com", textDirection: ltr)
    expect(
      semantics,
      hasSemantics(
39
        TestSemantics.root(
40
          children: <TestSemantics>[
41
            TestSemantics.rootChild(
42 43 44 45 46 47 48 49 50 51 52
              id: 1,
              label: 'Michael Goderbauer\ngoderbauer@google.com',
            ),
          ],
        ),
        ignoreRect: true,
        ignoreTransform: true,
      ),
    );

    await tester.pumpWidget(
53
      Directionality(
54
        textDirection: TextDirection.ltr,
55
        child: Semantics(
56 57
          container: true,
          explicitChildNodes: true,
58
          child: Column(
59
            children: const <Widget>[
60 61
              Text('Michael Goderbauer'),
              Text('goderbauer@google.com'),
62 63 64 65 66 67 68 69 70 71 72 73 74
            ],
          ),
        ),
      ),
    );

    // SemanticsNode#0()
    //  └SemanticsNode#1()
    //    ├SemanticsNode#2(label: "Michael Goderbauer", textDirection: ltr)
    //    └SemanticsNode#3(label: "goderbauer@google.com", textDirection: ltr)
    expect(
      semantics,
      hasSemantics(
75
        TestSemantics.root(
76
          children: <TestSemantics>[
77
            TestSemantics.rootChild(
78 79
              id: 1,
              children: <TestSemantics>[
80
                TestSemantics(
81 82 83
                  id: 2,
                  label: 'Michael Goderbauer',
                ),
84
                TestSemantics(
85 86 87 88 89 90 91 92 93 94 95 96 97
                  id: 3,
                  label: 'goderbauer@google.com',
                ),
              ],
            ),
          ],
        ),
        ignoreRect: true,
        ignoreTransform: true,
      ),
    );

    await tester.pumpWidget(
98
      Directionality(
99
        textDirection: TextDirection.ltr,
100
        child: Semantics(
101 102
          container: true,
          explicitChildNodes: true,
103
          child: Semantics(
104
            label: 'Signed in as',
105
            child: Column(
106
              children: const <Widget>[
107 108
                Text('Michael Goderbauer'),
                Text('goderbauer@google.com'),
109 110 111 112 113 114 115 116 117 118 119 120 121
              ],
            ),
          ),
        ),
      ),
    );

    // SemanticsNode#0()
    //  └SemanticsNode#1()
    //    └SemanticsNode#4(label: "Signed in as\nMichael Goderbauer\ngoderbauer@google.com", textDirection: ltr)
    expect(
      semantics,
      hasSemantics(
122
        TestSemantics.root(
123
          children: <TestSemantics>[
124
            TestSemantics.rootChild(
125 126
              id: 1,
              children: <TestSemantics>[
127
                TestSemantics(
128 129 130 131 132 133 134 135 136 137 138 139 140
                  id: 4,
                  label: 'Signed in as\nMichael Goderbauer\ngoderbauer@google.com',
                ),
              ],
            ),
          ],
        ),
        ignoreRect: true,
        ignoreTransform: true,
      ),
    );

    await tester.pumpWidget(
141
      Directionality(
142
        textDirection: TextDirection.ltr,
143
        child: Semantics(
144 145
          container: true,
          explicitChildNodes: false,
146
          child: Semantics(
147
            label: 'Signed in as',
148
            child: Column(
149
              children: const <Widget>[
150 151
                Text('Michael Goderbauer'),
                Text('goderbauer@google.com'),
152 153 154 155 156 157 158 159 160 161 162 163
              ],
            ),
          ),
        ),
      ),
    );

    // SemanticsNode#0()
    //  └SemanticsNode#1(label: "Signed in as\nMichael Goderbauer\ngoderbauer@google.com", textDirection: ltr)
    expect(
      semantics,
      hasSemantics(
164
        TestSemantics.root(
165
          children: <TestSemantics>[
166
            TestSemantics.rootChild(
167 168 169 170 171 172 173 174 175 176 177 178 179 180
              id: 1,
              label: 'Signed in as\nMichael Goderbauer\ngoderbauer@google.com',
            ),
          ],
        ),
        ignoreRect: true,
        ignoreTransform: true,
      ),
    );

    semantics.dispose();
  });

  testWidgets('Do not merge with conflicts', (WidgetTester tester) async {
181
    final SemanticsTester semantics = SemanticsTester(tester);
182 183

    await tester.pumpWidget(
184
      Directionality(
185
        textDirection: TextDirection.ltr,
186
        child: Semantics(
187 188
          container: true,
          explicitChildNodes: false,
189
          child: Column(
190
            children: <Widget>[
191
              Semantics(
192 193
                label: 'node 1',
                selected: true,
194
                child: const SizedBox(
195 196 197 198
                  width: 10.0,
                  height: 10.0,
                ),
              ),
199
              Semantics(
200 201
                label: 'node 2',
                selected: true,
202
                child: const SizedBox(
203 204 205 206
                  width: 10.0,
                  height: 10.0,
                ),
              ),
207
              Semantics(
208 209
                label: 'node 3',
                selected: true,
210
                child: const SizedBox(
211 212 213 214 215 216 217 218 219 220 221
                  width: 10.0,
                  height: 10.0,
                ),
              ),
            ],
          ),
        ),
      ),
    );

    // SemanticsNode#0()
222 223 224 225
    //  └SemanticsNode#1()
    //   ├SemanticsNode#2(selected, label: "node 1", textDirection: ltr)
    //   ├SemanticsNode#3(selected, label: "node 2", textDirection: ltr)
    //   └SemanticsNode#4(selected, label: "node 3", textDirection: ltr)
226 227 228
    expect(
      semantics,
      hasSemantics(
229
        TestSemantics.root(
230
          children: <TestSemantics>[
231
            TestSemantics.rootChild(
232
              id: 1,
233
              children: <TestSemantics>[
234
                TestSemantics(
235
                  id: 2,
236
                  flags: SemanticsFlag.isSelected.index,
237 238
                  label: 'node 1',
                ),
239
                TestSemantics(
240
                  id: 3,
241
                  flags: SemanticsFlag.isSelected.index,
242 243
                  label: 'node 2',
                ),
244
                TestSemantics(
245
                  id: 4,
246
                  flags: SemanticsFlag.isSelected.index,
247 248 249 250 251 252 253 254 255 256 257 258 259 260
                  label: 'node 3',
                ),
              ],
            ),
          ],
        ),
        ignoreRect: true,
        ignoreTransform: true,
      ),
    );

    semantics.dispose();
  });
}