notification_test.dart 2.7 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
// @dart = 2.8

7 8 9 10 11 12 13
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/widgets.dart';

class MyNotification extends Notification { }

void main() {
  testWidgets('Notification basics - toString', (WidgetTester tester) async {
14
    expect(MyNotification(), hasOneLineDescription);
15 16 17
  });

  testWidgets('Notification basics - dispatch', (WidgetTester tester) async {
18
    final List<dynamic> log = <dynamic>[];
19 20
    final GlobalKey key = GlobalKey();
    await tester.pumpWidget(NotificationListener<MyNotification>(
21 22 23 24 25
      onNotification: (MyNotification value) {
        log.add('a');
        log.add(value);
        return true;
      },
26
      child: NotificationListener<MyNotification>(
27 28 29 30 31
        onNotification: (MyNotification value) {
          log.add('b');
          log.add(value);
          return false;
        },
32
        child: Container(key: key),
33 34 35
      ),
    ));
    expect(log, isEmpty);
36
    final Notification notification = MyNotification();
37 38 39 40 41
    expect(() { notification.dispatch(key.currentContext); }, isNot(throwsException));
    expect(log, <dynamic>['b', notification, 'a', notification]);
  });

  testWidgets('Notification basics - cancel', (WidgetTester tester) async {
42
    final List<dynamic> log = <dynamic>[];
43 44
    final GlobalKey key = GlobalKey();
    await tester.pumpWidget(NotificationListener<MyNotification>(
45 46 47 48 49
      onNotification: (MyNotification value) {
        log.add('a - error');
        log.add(value);
        return true;
      },
50
      child: NotificationListener<MyNotification>(
51 52 53 54 55
        onNotification: (MyNotification value) {
          log.add('b');
          log.add(value);
          return true;
        },
56
        child: Container(key: key),
57 58 59
      ),
    ));
    expect(log, isEmpty);
60
    final Notification notification = MyNotification();
61 62 63 64 65
    expect(() { notification.dispatch(key.currentContext); }, isNot(throwsException));
    expect(log, <dynamic>['b', notification]);
  });

  testWidgets('Notification basics - listener null return value', (WidgetTester tester) async {
66
    final List<Type> log = <Type>[];
67 68
    final GlobalKey key = GlobalKey();
    await tester.pumpWidget(NotificationListener<MyNotification>(
69 70
      onNotification: (MyNotification value) {
        log.add(value.runtimeType);
71
        return false;
72
      },
73
      child: NotificationListener<MyNotification>(
74
        onNotification: (MyNotification value) => false,
75
        child: Container(key: key),
76
      ),
77
    ));
78
    expect(() { MyNotification().dispatch(key.currentContext); }, isNot(throwsException));
79
    expect(log, <Type>[MyNotification]);
80 81
  });
}