semantics_10_test.dart 2.9 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/rendering.dart';
import 'package:flutter_test/flutter_test.dart';

import 'semantics_tester.dart';

void main() {
13
  testWidgets('can cease to be semantics boundary after markNeedsSemanticsUpdate() has already been called once', (WidgetTester tester) async {
14
    final SemanticsTester semantics = SemanticsTester(tester);
15 16

    await tester.pumpWidget(
Ian Hickson's avatar
Ian Hickson committed
17 18 19 20 21
      buildTestWidgets(
        excludeSemantics: false,
        label: 'label',
        isSemanticsBoundary: true,
      ),
22 23 24 25
    );

    // The following should not trigger an assert.
    await tester.pumpWidget(
Ian Hickson's avatar
Ian Hickson committed
26 27 28 29 30
      buildTestWidgets(
        excludeSemantics: true,
        label: 'label CHANGED',
        isSemanticsBoundary: false,
      ),
31 32 33 34 35 36
    );

    semantics.dispose();
  });
}

37
Widget buildTestWidgets({ bool excludeSemantics, String label, bool isSemanticsBoundary }) {
38
  return Directionality(
Ian Hickson's avatar
Ian Hickson committed
39
    textDirection: TextDirection.ltr,
40
    child: Semantics(
Ian Hickson's avatar
Ian Hickson committed
41 42
      label: 'container',
      container: true,
43
      child: ExcludeSemantics(
Ian Hickson's avatar
Ian Hickson committed
44
        excluding: excludeSemantics,
45
        child: TestWidget(
Ian Hickson's avatar
Ian Hickson committed
46 47
          label: label,
          isSemanticBoundary: isSemanticsBoundary,
48
          child: Column(
Ian Hickson's avatar
Ian Hickson committed
49
            children: <Widget>[
50
              Semantics(
Ian Hickson's avatar
Ian Hickson committed
51 52
                label: 'child1',
              ),
53
              Semantics(
Ian Hickson's avatar
Ian Hickson committed
54 55 56 57
                label: 'child2',
              ),
            ],
          ),
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
        ),
      ),
    ),
  );
}

class TestWidget extends SingleChildRenderObjectWidget {
  const TestWidget({
    Key key,
    Widget child,
    this.label,
    this.isSemanticBoundary,
  }) : super(key: key, child: child);

  final String label;
  final bool isSemanticBoundary;

  @override
  RenderTest createRenderObject(BuildContext context) {
77
    return RenderTest()
78 79 80 81 82 83 84 85 86 87 88 89 90
      ..label = label
      ..isSemanticBoundary = isSemanticBoundary;
  }

  @override
  void updateRenderObject(BuildContext context, RenderTest renderObject) {
    renderObject
      ..label = label
      ..isSemanticBoundary = isSemanticBoundary;
  }
}

class RenderTest extends RenderProxyBox {
91

92
  @override
93 94 95 96 97 98 99 100 101 102
  void describeSemanticsConfiguration(SemanticsConfiguration config) {
    super.describeSemanticsConfiguration(config);

    if (!_isSemanticBoundary)
      return;

    config
      ..isSemanticBoundary = _isSemanticBoundary
      ..label = _label
      ..textDirection = TextDirection.ltr;
103 104 105

  }

106
  String _label = '<>';
107 108 109 110
  set label(String value) {
    if (value == _label)
      return;
    _label = value;
111
    markNeedsSemanticsUpdate();
112 113
  }

114

115
  bool _isSemanticBoundary = false;
116 117 118 119
  set isSemanticBoundary(bool value) {
    if (_isSemanticBoundary == value)
      return;
    _isSemanticBoundary = value;
120
    markNeedsSemanticsUpdate();
121 122
  }
}