// Copyright 2014 The Flutter 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/rendering.dart'; import 'package:flutter/widgets.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart'; void main() { testWidgets('Render and element tree stay in sync when keyed children move around', ( WidgetTester tester, ) async { // Regression test for https://github.com/flutter/flutter/issues/48855. await tester.pumpWidget( const Directionality( textDirection: TextDirection.ltr, child: Column( children: [ Text('0', key: ValueKey(0)), Text('1', key: ValueKey(1)), Text('2', key: ValueKey(2)), Text('3', key: ValueKey(3)), Text('4', key: ValueKey(4)), Text('5', key: ValueKey(5)), Text('6', key: ValueKey(6)), Text('7', key: ValueKey(7)), Text('8', key: ValueKey(8)), ], ), ), ); expect(_getChildOrder(tester.renderObject(find.byType(Column))), [ '0', '1', '2', '3', '4', '5', '6', '7', '8', ]); await tester.pumpWidget( const Directionality( textDirection: TextDirection.ltr, child: Column( children: [ Text('0', key: ValueKey(0)), Text('6', key: ValueKey(6)), Text('7', key: ValueKey(7)), Text('8', key: ValueKey(8)), Text('1', key: ValueKey(1)), Text('2', key: ValueKey(2)), Text('3', key: ValueKey(3)), Text('4', key: ValueKey(4)), Text('5', key: ValueKey(5)), ], ), ), ); expect(_getChildOrder(tester.renderObject(find.byType(Column))), [ '0', '6', '7', '8', '1', '2', '3', '4', '5', ]); }); testWidgets( 'Building a new MultiChildRenderObjectElement with children having duplicated keys throws', (WidgetTester tester) async { const duplicatedKey = ValueKey(1); await tester.pumpWidget( const Column( children: [ Text('Text 1', textDirection: TextDirection.ltr, key: duplicatedKey), Text('Text 2', textDirection: TextDirection.ltr, key: duplicatedKey), ], ), ); expect( tester.takeException(), isA().having( (FlutterError error) => error.message, 'error.message', startsWith('Duplicate keys found.'), ), ); }, ); testWidgets( 'Updating a MultiChildRenderObjectElement to have children with duplicated keys throws', experimentalLeakTesting: LeakTesting.settings .withIgnoredAll(), // leaking by design because of exception (WidgetTester tester) async { // Regression test for https://github.com/flutter/flutter/issues/81541 const key1 = ValueKey(1); const key2 = ValueKey(2); Future buildWithKey(Key key) { return tester.pumpWidget( Column( children: [ const Text('Text 1', textDirection: TextDirection.ltr, key: key1), Text('Text 2', textDirection: TextDirection.ltr, key: key), ], ), ); } // Initial build with two different keys. await buildWithKey(key2); expect(tester.takeException(), isNull); // Subsequent build with duplicated keys. await buildWithKey(key1); expect( tester.takeException(), isA().having( (FlutterError error) => error.message, 'error.message', startsWith('Duplicate keys found.'), ), ); }, ); } // Do not use tester.renderObjectList(find.byType(RenderParagraph). That returns // the RenderObjects in the order of their associated RenderObjectWidgets. The // point of this test is to assert the children order in the render tree, though. List _getChildOrder(RenderFlex flex) { final childOrder = []; flex.visitChildren((RenderObject child) { childOrder.add(((child as RenderParagraph).text as TextSpan).text!); }); return childOrder; }