key_test.dart 2.61 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
Ian Hickson's avatar
Ian Hickson committed
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
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
Ian Hickson's avatar
Ian Hickson committed
8 9

class TestValueKey<T> extends ValueKey<T> {
10
  const TestValueKey(super.value);
Ian Hickson's avatar
Ian Hickson committed
11 12
}

13
@immutable
Ian Hickson's avatar
Ian Hickson committed
14 15 16
class NotEquals {
  const NotEquals();
  @override
17
  bool operator ==(Object other) => false;
Ian Hickson's avatar
Ian Hickson committed
18 19 20 21 22
  @override
  int get hashCode => 0;
}

void main() {
23
  testWidgetsWithLeakTracking('Keys', (WidgetTester tester) async {
24 25 26
    expect(ValueKey<int>(nonconst(3)) == ValueKey<int>(nonconst(3)), isTrue);
    expect(ValueKey<num>(nonconst(3)) == ValueKey<int>(nonconst(3)), isFalse);
    expect(ValueKey<int>(nonconst(3)) == ValueKey<int>(nonconst(2)), isFalse);
27
    expect(const ValueKey<double>(double.nan) == const ValueKey<double>(double.nan), isFalse);
28

29 30 31 32
    expect(Key(nonconst('')) == ValueKey<String>(nonconst('')), isTrue);
    expect(ValueKey<String>(nonconst('')) == ValueKey<String>(nonconst('')), isTrue);
    expect(TestValueKey<String>(nonconst('')) == ValueKey<String>(nonconst('')), isFalse);
    expect(TestValueKey<String>(nonconst('')) == TestValueKey<String>(nonconst('')), isTrue);
33

34 35
    expect(ValueKey<String>(nonconst('')) == ValueKey<dynamic>(nonconst('')), isFalse);
    expect(TestValueKey<String>(nonconst('')) == TestValueKey<dynamic>(nonconst('')), isFalse);
36

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

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

46
    final NotEquals constNotEquals = nonconst(const NotEquals());
47 48
    expect(ValueKey<NotEquals>(constNotEquals) == ValueKey<NotEquals>(constNotEquals), isFalse);
    expect(ObjectKey(constNotEquals) == ObjectKey(constNotEquals), isTrue);
49

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

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