implicit_semantics_test.dart 6.81 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 20
          container: true,
          explicitChildNodes: false,
21
          child: Column(
22
            children: const <Widget>[
23 24
              Text('Michael Goderbauer'),
              Text('goderbauer@google.com'),
25 26 27 28 29 30 31 32 33 34 35
            ],
          ),
        ),
      ),
    );

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

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

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

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

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

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

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

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

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

    semantics.dispose();
  });
}