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

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

class MyNotification extends Notification { }

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

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

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

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

80
  testWidgets('Notification basics - listener null return value', (WidgetTester tester) async {
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    await tester.pumpWidget(const Placeholder());
    final ScrollMetricsNotification n1 = ScrollMetricsNotification(
      metrics: FixedScrollMetrics(
        minScrollExtent: 1.0,
        maxScrollExtent: 2.0,
        pixels: 3.0,
        viewportDimension: 4.0,
        axisDirection: AxisDirection.down,
        devicePixelRatio: 5.0,
      ),
      context: tester.allElements.first,
    );
    expect(n1.metrics.pixels, 3.0);
    final ScrollUpdateNotification n2 = n1.asScrollUpdate();
    expect(n2.metrics.pixels, 3.0);
  });
97
}