inherited_tree_cache_test.dart 2.76 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 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 77 78 79
// Copyright 2014 The Flutter 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/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  testWidgets('InheritedTreeCache returns null if element is not found', (WidgetTester tester) async {
    final InheritedTreeCache parent = InheritedTreeCache();

    expect(parent[InheritedElementA], isNull);
  });

  testWidgets('InheritedTreeCache can look up element from parent', (WidgetTester tester) async {
    final InheritedTreeCache parent = InheritedTreeCache();
    final InheritedTreeCache child = InheritedTreeCache(parent);
    final InheritedElementA elementA = InheritedElementA(const InheritedWidgetA());

    parent[InheritedElementA] = elementA;

    expect(child[InheritedElementA], elementA);
  });

  testWidgets('InheritedTreeCache can look up multiple elements from parent', (WidgetTester tester) async {
    final InheritedTreeCache parent = InheritedTreeCache();
    final InheritedTreeCache child = InheritedTreeCache(parent);
    final InheritedElementA elementA = InheritedElementA(const InheritedWidgetA());
    final InheritedElementA elementB = InheritedElementA(const InheritedWidgetB());

    parent[InheritedElementA] = elementA;
    parent[InheritedElementB] = elementB;

    expect(child[InheritedElementA], elementA);
    expect(child[InheritedElementB], elementB);
  });

  testWidgets('InheritedTreeCache does not cache nulls', (WidgetTester tester) async {
    final InheritedTreeCache parent = InheritedTreeCache();
    final InheritedTreeCache child = InheritedTreeCache(parent);
    final InheritedElementA elementA = InheritedElementA(const InheritedWidgetA());

    // First look up element that has not been cached.
    expect(child[InheritedElementA], null);

    // Then manually add element to parent.
    parent[InheritedElementA] = elementA;

    // Then the child should be able to find it.
    expect(child[InheritedElementA], elementA);
  });
}

class InheritedElementA extends InheritedElement {
  InheritedElementA(InheritedWidget widget) : super(widget);
}

class InheritedWidgetA extends InheritedWidget {
  const InheritedWidgetA({ Key? key }) : super(child: const SizedBox(), key: key);

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

class InheritedElementB extends InheritedElement {
  InheritedElementB(InheritedWidget widget) : super(widget);
}

class InheritedWidgetB extends InheritedWidget {
  const InheritedWidgetB({ Key? key }) : super(child: const SizedBox(), key: key);

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