notification_test.dart 2.72 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// 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';
import 'package:flutter/widgets.dart';

class MyNotification extends Notification { }

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

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

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

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