async_lifecycle_test.dart 1.82 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6 7 8
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';

class InvalidOnInitLifecycleWidget extends StatefulWidget {
9
  const InvalidOnInitLifecycleWidget({super.key});
10 11

  @override
12
  InvalidOnInitLifecycleWidgetState createState() => InvalidOnInitLifecycleWidgetState();
13 14 15 16
}

class InvalidOnInitLifecycleWidgetState extends State<InvalidOnInitLifecycleWidget> {
  @override
17
  Future<void> initState() async {
18 19 20 21 22
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
23
    return Container();
24 25 26 27
  }
}

class InvalidDidUpdateWidgetLifecycleWidget extends StatefulWidget {
28
  const InvalidDidUpdateWidgetLifecycleWidget({super.key, required this.id});
29 30 31 32

  final int id;

  @override
33
  InvalidDidUpdateWidgetLifecycleWidgetState createState() => InvalidDidUpdateWidgetLifecycleWidgetState();
34 35 36 37
}

class InvalidDidUpdateWidgetLifecycleWidgetState extends State<InvalidDidUpdateWidgetLifecycleWidget> {
  @override
38
  Future<void> didUpdateWidget(InvalidDidUpdateWidgetLifecycleWidget oldWidget) async {
39 40 41 42 43
    super.didUpdateWidget(oldWidget);
  }

  @override
  Widget build(BuildContext context) {
44
    return Container();
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
  }
}

void main() {
  testWidgets('async onInit throws FlutterError', (WidgetTester tester) async {
    await tester.pumpWidget(const InvalidOnInitLifecycleWidget());

    expect(tester.takeException(), isFlutterError);
  });

  testWidgets('async didUpdateWidget throws FlutterError', (WidgetTester tester) async {
    await tester.pumpWidget(const InvalidDidUpdateWidgetLifecycleWidget(id: 1));
    await tester.pumpWidget(const InvalidDidUpdateWidgetLifecycleWidget(id: 2));

    expect(tester.takeException(), isFlutterError);
  });
}