inherited_test.dart 2.06 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// 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/material.dart';
import 'package:test/test.dart';

class TestInherited extends InheritedWidget {
  TestInherited({ Key key, Widget child, this.shouldNotify: true })
    : super(key: key, child: child);

  final bool shouldNotify;

  @override
  bool updateShouldNotify(InheritedWidget oldWidget) {
    return shouldNotify;
  }
}

void main() {
22 23 24 25 26 27 28 29 30
  testWidgets('Inherited notifies dependents', (WidgetTester tester) {
    List<TestInherited> log = <TestInherited>[];

    Builder builder = new Builder(
      builder: (BuildContext context) {
        log.add(context.inheritFromWidgetOfExactType(TestInherited));
        return new Container();
      }
    );
31

32 33
    TestInherited first = new TestInherited(child: builder);
    tester.pumpWidget(first);
34

35
    expect(log, equals([first]));
36

37 38
    TestInherited second = new TestInherited(child: builder, shouldNotify: false);
    tester.pumpWidget(second);
39

40
    expect(log, equals([first]));
41

42 43
    TestInherited third = new TestInherited(child: builder, shouldNotify: true);
    tester.pumpWidget(third);
44

45
    expect(log, equals([first, third]));
46 47
  });

48 49 50 51 52 53 54 55 56 57 58 59 60 61
  testWidgets('Update inherited when reparenting state', (WidgetTester tester) {
    GlobalKey globalKey = new GlobalKey();
    List<TestInherited> log = <TestInherited>[];

    TestInherited build() {
      return new TestInherited(
        key: new UniqueKey(),
        child: new Container(
          key: globalKey,
          child: new Builder(
            builder: (BuildContext context) {
              log.add(context.inheritFromWidgetOfExactType(TestInherited));
              return new Container();
            }
62
          )
63 64 65
        )
      );
    }
66

67 68
    TestInherited first = build();
    tester.pumpWidget(first);
69

70
    expect(log, equals([first]));
71

72 73
    TestInherited second = build();
    tester.pumpWidget(second);
74

75
    expect(log, equals([first, second]));
76 77
  });
}