implicit_semantics_test.dart 6.84 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
// Copyright 2017 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/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 {
14
    final SemanticsTester semantics = SemanticsTester(tester);
15 16

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

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

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

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

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

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

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

    // SemanticsNode#0()
    //  └SemanticsNode#1(label: "Signed in as\nMichael Goderbauer\ngoderbauer@google.com", textDirection: ltr)
    expect(
      semantics,
      hasSemantics(
162
        TestSemantics.root(
163
          children: <TestSemantics>[
164
            TestSemantics.rootChild(
165 166 167 168 169 170 171 172 173 174 175 176 177 178
              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 {
179
    final SemanticsTester semantics = SemanticsTester(tester);
180 181

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

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

    semantics.dispose();
  });
}