key_test.dart 3.1 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
    final int int3 = 3; // we want these instances to be separate instances so that we're not just checking with a single object
23 24
    expect(new ValueKey<int>(int3) == new ValueKey<int>(int3), isTrue);
    expect(new ValueKey<num>(int3) == new ValueKey<int>(int3), isFalse);
25
    final int int2 = 2; // we want these instances to be separate instances so that we're not just checking with a single object
26
    expect(new ValueKey<int>(int3) == new ValueKey<int>(int2), isFalse);
Ian Hickson's avatar
Ian Hickson committed
27
    expect(const ValueKey<double>(double.NAN) == const ValueKey<double>(double.NAN), isFalse);
28

29
    final String empty = ''; // we want these instances to be separate instances so that we're not just checking with a single object
30 31 32 33 34 35 36 37
    expect(new Key(empty) == new ValueKey<String>(empty), isTrue);
    expect(new ValueKey<String>(empty) == new ValueKey<String>(empty), isTrue);
    expect(new TestValueKey<String>(empty) == new ValueKey<String>(empty), isFalse);
    expect(new TestValueKey<String>(empty) == new TestValueKey<String>(empty), isTrue);

    expect(new ValueKey<String>(empty) == new ValueKey<dynamic>(empty), isFalse);
    expect(new TestValueKey<String>(empty) == new TestValueKey<dynamic>(empty), isFalse);

Ian Hickson's avatar
Ian Hickson committed
38
    expect(new UniqueKey() == new UniqueKey(), isFalse);
39
    final LocalKey k = new UniqueKey();
Ian Hickson's avatar
Ian Hickson committed
40 41
    expect(new UniqueKey() == new UniqueKey(), isFalse);
    expect(k == k, isTrue);
42

Ian Hickson's avatar
Ian Hickson committed
43 44 45 46
    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);

47
    final NotEquals constNotEquals = const NotEquals(); // we want these instances to be separate instances so that we're not just checking with a single object
48 49 50
    expect(new ValueKey<NotEquals>(constNotEquals) == new ValueKey<NotEquals>(constNotEquals), isFalse);
    expect(new ObjectKey(constNotEquals) == new ObjectKey(constNotEquals), isTrue);

51
    final Object constObject = const Object(); // we want these instances to be separate instances so that we're not just checking with a single object
52
    expect(new ObjectKey(constObject) == new ObjectKey(constObject), isTrue);
Ian Hickson's avatar
Ian Hickson committed
53 54
    expect(new ObjectKey(new Object()) == new ObjectKey(new Object()), isFalse);

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