semantics_test.dart 5.31 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2015 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_test/flutter_test.dart';

9
import 'semantics_tester.dart';
10 11 12

void main() {
  testWidgets('Semantics shutdown and restart', (WidgetTester tester) async {
13 14
    SemanticsTester semantics = new SemanticsTester(tester);

15
    final TestSemantics expectedSemantics = new TestSemantics.root(
16
      label: 'test1',
Ian Hickson's avatar
Ian Hickson committed
17
      textDirection: TextDirection.ltr,
18
    );
19 20 21 22 23

    await tester.pumpWidget(
      new Container(
        child: new Semantics(
          label: 'test1',
Ian Hickson's avatar
Ian Hickson committed
24
          textDirection: TextDirection.ltr,
25 26 27 28 29
          child: new Container()
        )
      )
    );

30
    expect(semantics, hasSemantics(expectedSemantics));
31

32 33
    semantics.dispose();
    semantics = null;
34 35

    expect(tester.binding.hasScheduledFrame, isFalse);
36
    semantics = new SemanticsTester(tester);
37 38 39
    expect(tester.binding.hasScheduledFrame, isTrue);
    await tester.pump();

40 41
    expect(semantics, hasSemantics(expectedSemantics));
    semantics.dispose();
42
  });
43 44

  testWidgets('Detach and reattach assert', (WidgetTester tester) async {
45 46
    final SemanticsTester semantics = new SemanticsTester(tester);
    final GlobalKey key = new GlobalKey();
47

Ian Hickson's avatar
Ian Hickson committed
48 49 50
    await tester.pumpWidget(new Directionality(
      textDirection: TextDirection.ltr,
      child: new Container(
51 52 53 54 55 56 57 58 59 60
        child: new Semantics(
          label: 'test1',
          child: new Semantics(
            key: key,
            container: true,
            label: 'test2a',
            child: new Container()
          )
        )
      )
Ian Hickson's avatar
Ian Hickson committed
61
    ));
62

63
    expect(semantics, hasSemantics(
64
      new TestSemantics.root(
65 66
        label: 'test1',
        children: <TestSemantics>[
67
          new TestSemantics.rootChild(
68 69
            id: 1,
            label: 'test2a',
70
            rect: TestSemantics.fullScreen,
71 72 73 74
          )
        ]
      )
    ));
75

Ian Hickson's avatar
Ian Hickson committed
76 77 78
    await tester.pumpWidget(new Directionality(
      textDirection: TextDirection.ltr,
      child: new Container(
79 80 81 82 83 84 85 86 87 88 89 90 91 92
        child: new Semantics(
          label: 'test1',
          child: new Semantics(
            container: true,
            label: 'middle',
            child: new Semantics(
              key: key,
              container: true,
              label: 'test2b',
              child: new Container()
            )
          )
        )
      )
Ian Hickson's avatar
Ian Hickson committed
93
    ));
94

95
    expect(semantics, hasSemantics(
96
      new TestSemantics.root(
97 98
        label: 'test1',
        children: <TestSemantics>[
99
          new TestSemantics.rootChild(
100 101
            id: 2,
            label: 'middle',
102
            rect: TestSemantics.fullScreen,
103 104 105 106
            children: <TestSemantics>[
              new TestSemantics(
                id: 1,
                label: 'test2b',
107
                rect: TestSemantics.fullScreen,
108 109 110 111 112 113
              )
            ]
          )
        ]
      )
    ));
114

115
    semantics.dispose();
116
  });
Ian Hickson's avatar
Ian Hickson committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202

  testWidgets('Semantics and Directionality - RTL', (WidgetTester tester) async {
    final SemanticsTester semantics = new SemanticsTester(tester);

    final TestSemantics expectedSemantics = new TestSemantics.root(
      label: 'test1',
      textDirection: TextDirection.rtl,
    );

    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.rtl,
        child: new Semantics(
          label: 'test1',
          child: new Container(),
        ),
      ),
    );

    expect(semantics, hasSemantics(expectedSemantics));
  });

  testWidgets('Semantics and Directionality - LTR', (WidgetTester tester) async {
    final SemanticsTester semantics = new SemanticsTester(tester);

    final TestSemantics expectedSemantics = new TestSemantics.root(
      label: 'test1',
      textDirection: TextDirection.ltr,
    );

    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Semantics(
          label: 'test1',
          child: new Container(),
        ),
      ),
    );

    expect(semantics, hasSemantics(expectedSemantics));
  });

  testWidgets('Semantics and Directionality - overriding RTL with LTR', (WidgetTester tester) async {
    final SemanticsTester semantics = new SemanticsTester(tester);

    final TestSemantics expectedSemantics = new TestSemantics.root(
      label: 'test1',
      textDirection: TextDirection.ltr,
    );

    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.rtl,
        child: new Semantics(
          label: 'test1',
          textDirection: TextDirection.ltr,
          child: new Container(),
        ),
      ),
    );

    expect(semantics, hasSemantics(expectedSemantics));
  });

  testWidgets('Semantics and Directionality - overriding LTR with RTL', (WidgetTester tester) async {
    final SemanticsTester semantics = new SemanticsTester(tester);

    final TestSemantics expectedSemantics = new TestSemantics.root(
      label: 'test1',
      textDirection: TextDirection.rtl,
    );

    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Semantics(
          label: 'test1',
          textDirection: TextDirection.rtl,
          child: new Container(),
        ),
      ),
    );

    expect(semantics, hasSemantics(expectedSemantics));
  });
203
}