diff --git a/examples/flutter_gallery/test/smoke_test.dart b/examples/flutter_gallery/test/smoke_test.dart index e1ab5cf413..26f6965c76 100644 --- a/examples/flutter_gallery/test/smoke_test.dart +++ b/examples/flutter_gallery/test/smoke_test.dart @@ -164,7 +164,7 @@ Future runSmokeTest(WidgetTester tester) async { void main() { testWidgets('Flutter Gallery app smoke test', runSmokeTest); - testWidgets('Flutter Gallery app smoke test', (WidgetTester tester) async { + testWidgets('Flutter Gallery app smoke test with semantics', (WidgetTester tester) async { RendererBinding.instance.setSemanticsEnabled(true); await runSmokeTest(tester); RendererBinding.instance.setSemanticsEnabled(false); diff --git a/packages/flutter/lib/src/rendering/object.dart b/packages/flutter/lib/src/rendering/object.dart index e76c471d0b..5458ad4e3d 100644 --- a/packages/flutter/lib/src/rendering/object.dart +++ b/packages/flutter/lib/src/rendering/object.dart @@ -1186,7 +1186,7 @@ class PipelineOwner { } bool _debugDoingSemantics = false; - final List _nodesNeedingSemantics = []; + final Set _nodesNeedingSemantics = new Set(); /// Update the semantics for render objects marked as needing a semantics /// update. @@ -1206,14 +1206,16 @@ class PipelineOwner { assert(_semanticsOwner != null); assert(() { _debugDoingSemantics = true; return true; }); try { - _nodesNeedingSemantics.sort((RenderObject a, RenderObject b) => a.depth - b.depth); - for (RenderObject node in _nodesNeedingSemantics) { + final List nodesToProcess = _nodesNeedingSemantics.toList() + ..sort((RenderObject a, RenderObject b) => a.depth - b.depth); + _nodesNeedingSemantics.clear(); + for (RenderObject node in nodesToProcess) { if (node._needsSemanticsUpdate && node.owner == this) node._updateSemantics(); } _semanticsOwner.sendSemanticsUpdate(); } finally { - _nodesNeedingSemantics.clear(); + assert(_nodesNeedingSemantics.isEmpty); assert(() { _debugDoingSemantics = false; return true; }); Timeline.finishSync(); } @@ -2580,6 +2582,16 @@ abstract class RenderObject extends AbstractNode with DiagnosticableTreeMixin im node = node.parent; } while (node._semantics == null); node._semantics?.reset(); + if (node != this && _semantics != null && _needsSemanticsUpdate) { + // If [this] node has already been added to [owner._nodesNeedingSemantics] + // remove it as it is no longer guaranteed that its semantics + // node will continue to be in the tree. If it still is in the tree, the + // ancestor [node] added to [owner._nodesNeedingSemantics] at the end of + // this block will ensure that the semantics of [this] node actually get + // updated. + // (See semantics_10_test.dart for an example why this is required). + owner._nodesNeedingSemantics.remove(this); + } if (!node._needsSemanticsUpdate) { node._needsSemanticsUpdate = true; if (owner != null) { diff --git a/packages/flutter/test/widgets/semantics_10_test.dart b/packages/flutter/test/widgets/semantics_10_test.dart new file mode 100644 index 0000000000..fea0e16015 --- /dev/null +++ b/packages/flutter/test/widgets/semantics_10_test.dart @@ -0,0 +1,120 @@ +// 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/widgets.dart'; +import 'package:flutter/rendering.dart'; +import 'package:flutter_test/flutter_test.dart'; + +import 'semantics_tester.dart'; + +List callLog = []; + +void main() { + testWidgets('can call markNeedsSemanticsUpdate(onlyChanges: true) followed by markNeedsSemanticsUpdate(onlyChanges: false)', (WidgetTester tester) async { + final SemanticsTester semantics = new SemanticsTester(tester); + + await tester.pumpWidget( + buildTestWidgets( + excludeSemantics: false, + label: 'label', + isSemanticsBoundary: true, + ) + ); + + callLog.clear(); + + // The following should not trigger an assert. + await tester.pumpWidget( + buildTestWidgets( + excludeSemantics: true, + label: 'label CHANGED', + isSemanticsBoundary: false, + ) + ); + + expect(callLog, ['markNeedsSemanticsUpdate(onlyChanges: true)', 'markNeedsSemanticsUpdate(onlyChanges: false)']); + + semantics.dispose(); + }); +} + +Widget buildTestWidgets({bool excludeSemantics, String label, bool isSemanticsBoundary}) { + return new Semantics( + label: 'container', + container: true, + child: new ExcludeSemantics( + excluding: excludeSemantics, + child: new TestWidget( + label: label, + isSemanticBoundary: isSemanticsBoundary, + child: new Column( + children: [ + new Semantics( + label: 'child1', + ), + new Semantics( + label: 'child2', + ), + ], + ), + ), + ), + ); +} + +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) { + return new RenderTest() + ..label = label + ..isSemanticBoundary = isSemanticBoundary; + } + + @override + void updateRenderObject(BuildContext context, RenderTest renderObject) { + renderObject + ..label = label + ..isSemanticBoundary = isSemanticBoundary; + } +} + +class RenderTest extends RenderProxyBox { + @override + SemanticsAnnotator get semanticsAnnotator => isSemanticBoundary ? _annotate : null; + + void _annotate(SemanticsNode node) { + node.label = _label; + } + + String _label; + set label(String value) { + if (value == _label) + return; + _label = value; + markNeedsSemanticsUpdate(onlyChanges: true); + callLog.add('markNeedsSemanticsUpdate(onlyChanges: true)'); + } + + @override + bool get isSemanticBoundary => _isSemanticBoundary; + bool _isSemanticBoundary; + set isSemanticBoundary(bool value) { + if (_isSemanticBoundary == value) + return; + _isSemanticBoundary = value; + markNeedsSemanticsUpdate(onlyChanges: false); + callLog.add('markNeedsSemanticsUpdate(onlyChanges: false)'); + } +}