semantics_merge_test.dart 4.01 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
// 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/rendering.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';

import 'semantics_tester.dart';

void main() {
13 14 15 16
  setUp(() {
    debugResetSemanticsIdCounter();
  });

17
  testWidgets('MergeSemantics', (WidgetTester tester) async {
18
    final SemanticsTester semantics = SemanticsTester(tester);
19 20 21

    // not merged
    await tester.pumpWidget(
22
      Directionality(
23
        textDirection: TextDirection.ltr,
24
        child: Row(
25
          children: <Widget>[
26
            Semantics(
27 28 29
              container: true,
              child: const Text('test1'),
            ),
30
            Semantics(
31 32 33
              container: true,
              child: const Text('test2'),
            ),
34 35 36 37 38 39
          ],
        ),
      ),
    );

    expect(semantics, hasSemantics(
40
      TestSemantics.root(
41
        children: <TestSemantics>[
42
          TestSemantics.rootChild(
43 44 45
            id: 1,
            label: 'test1',
          ),
46
          TestSemantics.rootChild(
47 48 49
            id: 2,
            label: 'test2',
          ),
50 51 52 53 54 55 56 57
        ],
      ),
      ignoreRect: true,
      ignoreTransform: true,
    ));

    // merged
    await tester.pumpWidget(
58
      Directionality(
59
        textDirection: TextDirection.ltr,
60 61
        child: MergeSemantics(
          child: Row(
62
            children: <Widget>[
63
              Semantics(
64 65 66
                container: true,
                child: const Text('test1'),
              ),
67
              Semantics(
68 69 70
                container: true,
                child: const Text('test2'),
              ),
71 72 73 74 75 76 77
            ],
          ),
        ),
      ),
    );

    expect(semantics, hasSemantics(
78
      TestSemantics.root(
79
        children: <TestSemantics>[
80
          TestSemantics.rootChild(
81 82 83 84 85
            id: 3,
            label: 'test1\ntest2',
          ),
        ]
      ),
86 87 88 89 90 91
      ignoreRect: true,
      ignoreTransform: true,
    ));

    // not merged
    await tester.pumpWidget(
92
      Directionality(
93
        textDirection: TextDirection.ltr,
94
        child: Row(
95
          children: <Widget>[
96
            Semantics(
97 98 99
              container: true,
              child: const Text('test1'),
            ),
100
            Semantics(
101 102 103
              container: true,
              child: const Text('test2'),
            ),
104 105 106 107 108 109
          ],
        ),
      ),
    );

    expect(semantics, hasSemantics(
110
      TestSemantics.root(
111
        children: <TestSemantics>[
112 113
          TestSemantics.rootChild(id: 6, label: 'test1'),
          TestSemantics.rootChild(id: 7, label: 'test2'),
114 115 116 117 118 119 120 121
        ],
      ),
      ignoreRect: true,
      ignoreTransform: true,
    ));

    semantics.dispose();
  });
122 123

  testWidgets('MergeSemantics works if other nodes are implicitly merged into its node', (WidgetTester tester) async {
124
    final SemanticsTester semantics = SemanticsTester(tester);
125 126

    await tester.pumpWidget(
127
      Directionality(
128
        textDirection: TextDirection.ltr,
129 130
        child: MergeSemantics(
          child: Semantics(
131
            selected: true, // this is implicitly merged into the MergeSemantics node
132
            child: Row(
133
              children: <Widget>[
134
                Semantics(
135 136 137
                  container: true,
                  child: const Text('test1'),
                ),
138
                Semantics(
139 140 141 142 143 144 145 146 147 148 149
                  container: true,
                  child: const Text('test2'),
                ),
              ],
            ),
          ),
        ),
      ),
    );

    expect(semantics, hasSemantics(
150
      TestSemantics.root(
151
          children: <TestSemantics>[
152
            TestSemantics.rootChild(
153 154 155 156 157 158 159 160 161 162 163 164 165 166
              id: 1,
              flags: <SemanticsFlag>[
                SemanticsFlag.isSelected,
              ],
              label: 'test1\ntest2',
            ),
          ]
      ),
      ignoreRect: true,
      ignoreTransform: true,
    ));

    semantics.dispose();
  });
167
}