framework_test.dart 4.86 KB
Newer Older
1 2 3 4 5
// Copyright 2015 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_test/flutter_test.dart';
6
import 'package:flutter/foundation.dart';
7 8
import 'package:flutter/widgets.dart';

9 10 11 12 13
class TestState extends State<StatefulWidget> {
  @override
  Widget build(BuildContext context) => null;
}

14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
void main() {
  testWidgets('UniqueKey control test', (WidgetTester tester) async {
    Key key = new UniqueKey();
    expect(key, hasOneLineDescription);
    expect(key, isNot(equals(new UniqueKey())));
  });

  testWidgets('ObjectKey control test', (WidgetTester tester) async {
    Object a = new Object();
    Object b = new Object();
    Key keyA = new ObjectKey(a);
    Key keyA2 = new ObjectKey(a);
    Key keyB = new ObjectKey(b);

    expect(keyA, hasOneLineDescription);
    expect(keyA, equals(keyA2));
    expect(keyA.hashCode, equals(keyA2.hashCode));
    expect(keyA, isNot(equals(keyB)));
  });

34 35 36 37 38 39 40 41 42 43 44 45 46
  testWidgets('GlobalObjectKey control test', (WidgetTester tester) async {
    Object a = new Object();
    Object b = new Object();
    Key keyA = new GlobalObjectKey(a);
    Key keyA2 = new GlobalObjectKey(a);
    Key keyB = new GlobalObjectKey(b);

    expect(keyA, hasOneLineDescription);
    expect(keyA, equals(keyA2));
    expect(keyA.hashCode, equals(keyA2.hashCode));
    expect(keyA, isNot(equals(keyB)));
  });

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
  testWidgets('GlobalKey duplication', (WidgetTester tester) async {
    Key key = new GlobalKey(debugLabel: 'problematic');

    await tester.pumpWidget(new Stack(
      children: <Widget>[
        new Container(
          key: const ValueKey<int>(1),
        ),
        new Container(
          key: const ValueKey<int>(2),
        ),
        new Container(
          key: key
        ),
      ],
    ));

    await tester.pumpWidget(new Stack(
      children: <Widget>[
        new Container(
          key: const ValueKey<int>(1),
          child: new SizedBox(key: key),
        ),
        new Container(
          key: const ValueKey<int>(2),
          child: new Placeholder(key: key),
        ),
      ],
    ));

77
    expect(tester.takeException(), isFlutterError);
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
  });

  testWidgets('GlobalKey notification exception handling', (WidgetTester tester) async {
    GlobalKey key = new GlobalKey();

    await tester.pumpWidget(new Container(key: key));

    GlobalKey.registerRemoveListener(key, (GlobalKey key) {
      throw new Exception('Misbehaving listener');
    });

    bool didReceiveCallback = false;
    GlobalKey.registerRemoveListener(key, (GlobalKey key) {
      expect(didReceiveCallback, isFalse);
      didReceiveCallback = true;
    });

    await tester.pumpWidget(new Placeholder());

    expect(tester.takeException(), isNotNull);
    expect(didReceiveCallback, isTrue);
  });
100

101

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
  testWidgets('Defunct setState throws exception', (WidgetTester tester) async {
    StateSetter setState;

    await tester.pumpWidget(new StatefulBuilder(
      builder: (BuildContext context, StateSetter setter) {
        setState = setter;
        return new Container();
      },
    ));

    // Control check that setState doesn't throw an exception.
    setState(() { });

    await tester.pumpWidget(new Container());

    expect(() { setState(() { }); }, throwsFlutterError);
  });

  testWidgets('State toString', (WidgetTester tester) async {
    TestState state = new TestState();
    expect(state.toString(), contains('no config'));
  });

  testWidgets('debugPrintGlobalKeyedWidgetLifecycle control test', (WidgetTester tester) async {
    expect(debugPrintGlobalKeyedWidgetLifecycle, isFalse);

    final DebugPrintCallback oldCallback = debugPrint;
    debugPrintGlobalKeyedWidgetLifecycle = true;

    List<String> log = <String>[];
    debugPrint = (String message, { int wrapWidth }) {
      log.add(message);
    };

    GlobalKey key = new GlobalKey();
    await tester.pumpWidget(new Container(key: key));
    expect(log, isEmpty);
    await tester.pumpWidget(new Placeholder());
    debugPrint = oldCallback;
    debugPrintGlobalKeyedWidgetLifecycle = false;

    expect(log.length, equals(2));
    expect(log[0], matches('Deactivated'));
    expect(log[1], matches('Discarding .+ from inactive elements list.'));
  });
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

  testWidgets('MultiChildRenderObjectElement.children', (WidgetTester tester) async {
    GlobalKey key0, key1, key2;
    await tester.pumpWidget(new Column(
      key: key0 = new GlobalKey(),
      children: <Widget>[
        new Container(),
        new Container(key: key1 = new GlobalKey()),
        new Container(child: new Container()),
        new Container(key: key2 = new GlobalKey()),
        new Container(),
      ],
    ));
    MultiChildRenderObjectElement element = key0.currentContext;
    expect(
      element.children.map((Element element) => element.widget.key), // ignore: INVALID_USE_OF_PROTECTED_MEMBER
      <Key>[null, key1, null, key2, null],
    );
  });
166
}