key_test.dart 2.66 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// Copyright 2016 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 TestValueKey<T> extends ValueKey<T> {
  const TestValueKey(T value) : super(value);
}

class NotEquals {
  const NotEquals();
  @override
  bool operator ==(dynamic other) => false;
  @override
  int get hashCode => 0;
}

void main() {
  testWidgets('Keys', (WidgetTester tester) async {
22 23 24
    expect(new ValueKey<int>(nonconst(3)) == new ValueKey<int>(nonconst(3)), isTrue);
    expect(new ValueKey<num>(nonconst(3)) == new ValueKey<int>(nonconst(3)), isFalse);
    expect(new ValueKey<int>(nonconst(3)) == new ValueKey<int>(nonconst(2)), isFalse);
Ian Hickson's avatar
Ian Hickson committed
25
    expect(const ValueKey<double>(double.NAN) == const ValueKey<double>(double.NAN), isFalse);
26

27 28 29 30
    expect(new Key(nonconst('')) == new ValueKey<String>(nonconst('')), isTrue);
    expect(new ValueKey<String>(nonconst('')) == new ValueKey<String>(nonconst('')), isTrue);
    expect(new TestValueKey<String>(nonconst('')) == new ValueKey<String>(nonconst('')), isFalse);
    expect(new TestValueKey<String>(nonconst('')) == new TestValueKey<String>(nonconst('')), isTrue);
31

32 33
    expect(new ValueKey<String>(nonconst('')) == new ValueKey<dynamic>(nonconst('')), isFalse);
    expect(new TestValueKey<String>(nonconst('')) == new TestValueKey<dynamic>(nonconst('')), isFalse);
34

Ian Hickson's avatar
Ian Hickson committed
35
    expect(new UniqueKey() == new UniqueKey(), isFalse);
36
    final LocalKey k = new UniqueKey();
Ian Hickson's avatar
Ian Hickson committed
37 38
    expect(new UniqueKey() == new UniqueKey(), isFalse);
    expect(k == k, isTrue);
39

Ian Hickson's avatar
Ian Hickson committed
40 41 42 43
    expect(new ValueKey<LocalKey>(k) == new ValueKey<LocalKey>(k), isTrue);
    expect(new ValueKey<LocalKey>(k) == new ValueKey<UniqueKey>(k), isFalse);
    expect(new ObjectKey(k) == new ObjectKey(k), isTrue);

44
    final NotEquals constNotEquals = nonconst(const NotEquals());
45 46 47
    expect(new ValueKey<NotEquals>(constNotEquals) == new ValueKey<NotEquals>(constNotEquals), isFalse);
    expect(new ObjectKey(constNotEquals) == new ObjectKey(constNotEquals), isTrue);

48
    final Object constObject = nonconst(const Object());
49
    expect(new ObjectKey(constObject) == new ObjectKey(constObject), isTrue);
Ian Hickson's avatar
Ian Hickson committed
50 51
    expect(new ObjectKey(new Object()) == new ObjectKey(new Object()), isFalse);

52
    expect(const ValueKey<bool>(true), hasOneLineDescription);
Ian Hickson's avatar
Ian Hickson committed
53
    expect(new UniqueKey(), hasOneLineDescription);
54
    expect(const ObjectKey(true), hasOneLineDescription);
Ian Hickson's avatar
Ian Hickson committed
55 56
    expect(new GlobalKey(), hasOneLineDescription);
    expect(new GlobalKey(debugLabel: 'hello'), hasOneLineDescription);
57
    expect(const GlobalObjectKey(true), hasOneLineDescription);
Ian Hickson's avatar
Ian Hickson committed
58 59
  });
}