implicit_semantics_test.dart 6.7 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('Implicit Semantics merge behavior', (WidgetTester tester) async {
13
    final SemanticsTester semantics = SemanticsTester(tester);
14 15

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

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

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

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

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

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

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

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

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

    // SemanticsNode#0()
216 217 218 219
    //  └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)
220 221 222
    expect(
      semantics,
      hasSemantics(
223
        TestSemantics.root(
224
          children: <TestSemantics>[
225
            TestSemantics.rootChild(
226
              id: 1,
227
              children: <TestSemantics>[
228
                TestSemantics(
229
                  id: 2,
230
                  flags: SemanticsFlag.isSelected.index,
231 232
                  label: 'node 1',
                ),
233
                TestSemantics(
234
                  id: 3,
235
                  flags: SemanticsFlag.isSelected.index,
236 237
                  label: 'node 2',
                ),
238
                TestSemantics(
239
                  id: 4,
240
                  flags: SemanticsFlag.isSelected.index,
241 242 243 244 245 246 247 248 249 250 251 252 253 254
                  label: 'node 3',
                ),
              ],
            ),
          ],
        ),
        ignoreRect: true,
        ignoreTransform: true,
      ),
    );

    semantics.dispose();
  });
}