semantics_10_test.dart 3.04 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6
// 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';
7
import 'package:flutter/widgets.dart';
8 9 10 11 12
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 38 39 40 41
Widget buildTestWidgets({
  required bool excludeSemantics,
  required String label,
  required bool isSemanticsBoundary,
}) {
42
  return Directionality(
Ian Hickson's avatar
Ian Hickson committed
43
    textDirection: TextDirection.ltr,
44
    child: Semantics(
Ian Hickson's avatar
Ian Hickson committed
45 46
      label: 'container',
      container: true,
47
      child: ExcludeSemantics(
Ian Hickson's avatar
Ian Hickson committed
48
        excluding: excludeSemantics,
49
        child: TestWidget(
Ian Hickson's avatar
Ian Hickson committed
50 51
          label: label,
          isSemanticBoundary: isSemanticsBoundary,
52
          child: Column(
Ian Hickson's avatar
Ian Hickson committed
53
            children: <Widget>[
54
              Semantics(
Ian Hickson's avatar
Ian Hickson committed
55 56
                label: 'child1',
              ),
57
              Semantics(
Ian Hickson's avatar
Ian Hickson committed
58 59 60 61
                label: 'child2',
              ),
            ],
          ),
62 63 64 65 66 67 68 69
        ),
      ),
    ),
  );
}

class TestWidget extends SingleChildRenderObjectWidget {
  const TestWidget({
70 71
    super.key,
    required Widget super.child,
72 73
    required this.label,
    required this.isSemanticBoundary,
74
  });
75 76 77 78 79 80

  final String label;
  final bool isSemanticBoundary;

  @override
  RenderTest createRenderObject(BuildContext context) {
81
    return RenderTest()
82 83 84 85 86 87 88 89 90 91 92 93 94 95
      ..label = label
      ..isSemanticBoundary = isSemanticBoundary;
  }

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

class RenderTest extends RenderProxyBox {
  @override
96 97 98
  void describeSemanticsConfiguration(SemanticsConfiguration config) {
    super.describeSemanticsConfiguration(config);

99
    if (!_isSemanticBoundary) {
100
      return;
101
    }
102 103 104 105 106

    config
      ..isSemanticBoundary = _isSemanticBoundary
      ..label = _label
      ..textDirection = TextDirection.ltr;
107 108 109

  }

110
  String get label => _label;
111
  String _label = '<>';
112
  set label(String value) {
113
    if (value == _label) {
114
      return;
115
    }
116
    _label = value;
117
    markNeedsSemanticsUpdate();
118 119
  }

120

121
  bool get isSemanticBoundary => _isSemanticBoundary;
122
  bool _isSemanticBoundary = false;
123
  set isSemanticBoundary(bool value) {
124
    if (_isSemanticBoundary == value) {
125
      return;
126
    }
127
    _isSemanticBoundary = value;
128
    markNeedsSemanticsUpdate();
129 130
  }
}