multichildobject_with_keys_test.dart 4.2 KB
Newer Older
1 2 3 4 5 6 7
// 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';
8
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
9 10

void main() {
11
  testWidgetsWithLeakTracking('Render and element tree stay in sync when keyed children move around', (WidgetTester tester) async {
12 13 14
    // Regression test for https://github.com/flutter/flutter/issues/48855.

    await tester.pumpWidget(
15
      const Directionality(
16 17
        textDirection: TextDirection.ltr,
        child: Column(
18
          children: <Widget>[
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
            Text('0', key: ValueKey<int>(0)),
            Text('1', key: ValueKey<int>(1)),
            Text('2', key: ValueKey<int>(2)),
            Text('3', key: ValueKey<int>(3)),
            Text('4', key: ValueKey<int>(4)),
            Text('5', key: ValueKey<int>(5)),
            Text('6', key: ValueKey<int>(6)),
            Text('7', key: ValueKey<int>(7)),
            Text('8', key: ValueKey<int>(8)),
          ],
        ),
      ),
    );

    expect(
      _getChildOrder(tester.renderObject<RenderFlex>(find.byType(Column))),
      <String>['0', '1', '2', '3', '4', '5', '6', '7', '8'],
    );

    await tester.pumpWidget(
39
      const Directionality(
40 41
        textDirection: TextDirection.ltr,
        child: Column(
42
          children: <Widget>[
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
            Text('0', key: ValueKey<int>(0)),
            Text('6', key: ValueKey<int>(6)),
            Text('7', key: ValueKey<int>(7)),
            Text('8', key: ValueKey<int>(8)),
            Text('1', key: ValueKey<int>(1)),
            Text('2', key: ValueKey<int>(2)),
            Text('3', key: ValueKey<int>(3)),
            Text('4', key: ValueKey<int>(4)),
            Text('5', key: ValueKey<int>(5)),
          ],
        ),
      ),
    );

    expect(
      _getChildOrder(tester.renderObject<RenderFlex>(find.byType(Column))),
      <String>['0', '6', '7', '8', '1', '2', '3', '4', '5'],
    );
  });
62

63
  testWidgetsWithLeakTracking('Building a new MultiChildRenderObjectElement with children having duplicated keys throws', (WidgetTester tester) async {
64 65
    const ValueKey<int> duplicatedKey = ValueKey<int>(1);

66 67
    await tester.pumpWidget(const Column(
      children: <Widget>[
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
        Text('Text 1', textDirection: TextDirection.ltr, key: duplicatedKey),
        Text('Text 2', textDirection: TextDirection.ltr, key: duplicatedKey),
      ],
    ));

    expect(
      tester.takeException(),
      isA<FlutterError>().having(
        (FlutterError error) => error.message,
        'error.message',
        startsWith('Duplicate keys found.'),
      ),
    );
  });

83
  testWidgetsWithLeakTracking('Updating a MultiChildRenderObjectElement to have children with duplicated keys throws', (WidgetTester tester) async {
84 85 86 87 88
    // Regression test for https://github.com/flutter/flutter/issues/81541

    const ValueKey<int> key1 = ValueKey<int>(1);
    const ValueKey<int> key2 = ValueKey<int>(2);

89
    Future<void> buildWithKey(Key key) {
90 91 92 93 94 95 96 97 98
      return tester.pumpWidget(Column(
        children: <Widget>[
          const Text('Text 1', textDirection: TextDirection.ltr, key: key1),
          Text('Text 2', textDirection: TextDirection.ltr, key: key),
        ],
      ));
    }

    // Initial build with two different keys.
99
    await buildWithKey(key2);
100 101 102
    expect(tester.takeException(), isNull);

    // Subsequent build with duplicated keys.
103
    await buildWithKey(key1);
104 105 106 107 108 109 110 111 112
    expect(
      tester.takeException(),
      isA<FlutterError>().having(
        (FlutterError error) => error.message,
        'error.message',
        startsWith('Duplicate keys found.'),
      ),
    );
  });
113 114 115 116 117 118 119 120
}

// 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<String> _getChildOrder(RenderFlex flex) {
  final List<String> childOrder = <String>[];
  flex.visitChildren((RenderObject child) {
121
    childOrder.add(((child as RenderParagraph).text as TextSpan).text!);
122 123 124
  });
  return childOrder;
}