fast_reassemble_test.dart 2.85 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
// 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/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  testWidgets('reassemble with a className only marks subtrees from the first matching element as dirty', (WidgetTester tester) async {
    await tester.pumpWidget(
      const Foo(Bar(Fizz(SizedBox())))
    );

    expect(Foo.count, 0);
    expect(Bar.count, 0);
    expect(Fizz.count, 0);

    DebugReassembleConfig config = DebugReassembleConfig(widgetName: 'Bar');
    WidgetsBinding.instance.buildOwner!.reassemble(WidgetsBinding.instance.renderViewElement!, config);

    expect(Foo.count, 0);
    expect(Bar.count, 1);
    expect(Fizz.count, 1);

    config = DebugReassembleConfig(widgetName: 'Fizz');
    WidgetsBinding.instance.buildOwner!.reassemble(WidgetsBinding.instance.renderViewElement!, config);

    expect(Foo.count, 0);
    expect(Bar.count, 1);
    expect(Fizz.count, 2);

    config = DebugReassembleConfig(widgetName: 'NoMatch');
    WidgetsBinding.instance.buildOwner!.reassemble(WidgetsBinding.instance.renderViewElement!, config);

    expect(Foo.count, 0);
    expect(Bar.count, 1);
    expect(Fizz.count, 2);

    config = DebugReassembleConfig();
    WidgetsBinding.instance.buildOwner!.reassemble(WidgetsBinding.instance.renderViewElement!, config);

    expect(Foo.count, 1);
    expect(Bar.count, 2);
    expect(Fizz.count, 3);

    WidgetsBinding.instance.buildOwner!.reassemble(WidgetsBinding.instance.renderViewElement!, null);

    expect(Foo.count, 2);
    expect(Bar.count, 3);
    expect(Fizz.count, 4);
  });
}

class Foo extends StatefulWidget {
  const Foo(this.child, {super.key});

  final Widget child;
  static int count = 0;

  @override
  State<Foo> createState() => _FooState();
}

class _FooState extends State<Foo> {
  @override
  void reassemble() {
    Foo.count += 1;
    super.reassemble();
  }

  @override
  Widget build(BuildContext context) {
    return widget.child;
  }
}


class Bar extends StatefulWidget {
  const Bar(this.child, {super.key});

  final Widget child;
  static int count = 0;

  @override
  State<Bar> createState() => _BarState();
}

class _BarState extends State<Bar> {
  @override
  void reassemble() {
    Bar.count += 1;
    super.reassemble();
  }

  @override
  Widget build(BuildContext context) {
    return widget.child;
  }
}

class Fizz extends StatefulWidget {
  const Fizz(this.child, {super.key});

  final Widget child;
  static int count = 0;

  @override
  State<Fizz> createState() => _FizzState();
}

class _FizzState extends State<Fizz> {
  @override
  void reassemble() {
    Fizz.count += 1;
    super.reassemble();
  }

  @override
  Widget build(BuildContext context) {
    return widget.child;
  }
}