expand_icon_test.dart 3.34 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
// 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/material.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  testWidgets('ExpandIcon test', (WidgetTester tester) async {
    bool expanded = false;

    await tester.pumpWidget(
Ian Hickson's avatar
Ian Hickson committed
13
      wrap(
14
          child: ExpandIcon(
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
            onPressed: (bool isExpanded) {
              expanded = !expanded;
            }
          )
      )
    );

    expect(expanded, isFalse);
    await tester.tap(find.byType(ExpandIcon));
    expect(expanded, isTrue);
    await tester.pump(const Duration(milliseconds: 100));
    await tester.tap(find.byType(ExpandIcon));
    expect(expanded, isFalse);
  });

  testWidgets('ExpandIcon disabled', (WidgetTester tester) async {
    await tester.pumpWidget(
Ian Hickson's avatar
Ian Hickson committed
32
      wrap(
33
          child: const ExpandIcon(
34 35 36 37 38
            onPressed: null
          )
      )
    );

39
    final IconTheme iconTheme = tester.firstWidget(find.byType(IconTheme).last);
40
    expect(iconTheme.data.color, equals(Colors.black38));
41
  });
42 43 44 45 46

  testWidgets('ExpandIcon test isExpanded does not trigger callback', (WidgetTester tester) async {
    bool expanded = false;

    await tester.pumpWidget(
Ian Hickson's avatar
Ian Hickson committed
47
      wrap(
48
          child: ExpandIcon(
49 50 51 52 53 54 55 56 57
            isExpanded: false,
            onPressed: (bool isExpanded) {
              expanded = !expanded;
            }
          )
      )
    );

    await tester.pumpWidget(
Ian Hickson's avatar
Ian Hickson committed
58
      wrap(
59
          child: ExpandIcon(
60 61 62 63 64 65 66 67 68 69
            isExpanded: true,
            onPressed: (bool isExpanded) {
              expanded = !expanded;
            }
        )
      )
    );

    expect(expanded, isFalse);
  });
70 71 72 73 74 75

  testWidgets('ExpandIcon is rotated initially if isExpanded is true on first build', (WidgetTester tester) async {
    bool expanded = true;

    await tester.pumpWidget(
        wrap(
76
            child: ExpandIcon(
77 78 79 80 81 82 83 84 85 86
              isExpanded: expanded,
              onPressed: (bool isExpanded) {
                expanded = !isExpanded;
              },
            )
        )
    );
    final RotationTransition rotation = tester.firstWidget(find.byType(RotationTransition));
    expect(rotation.turns.value, 0.5);
  });
87 88 89 90 91

  testWidgets('ExpandIcon has correct semantic hints', (WidgetTester tester) async {
    final SemanticsHandle handle = tester.ensureSemantics();
    const DefaultMaterialLocalizations localizations = DefaultMaterialLocalizations();
    await tester.pumpWidget(wrap(
92
        child: ExpandIcon(
93 94 95 96 97
          isExpanded: true,
          onPressed: (bool _) {},
        )
    ));

98
    expect(tester.getSemantics(find.byType(ExpandIcon)), matchesSemantics(
99 100 101 102 103 104 105 106
      hasTapAction: true,
      hasEnabledState: true,
      isEnabled: true,
      isButton: true,
      onTapHint: localizations.expandedIconTapHint,
    ));

    await tester.pumpWidget(wrap(
107
      child: ExpandIcon(
108 109 110 111 112
        isExpanded: false,
        onPressed: (bool _) {},
      )
    ));

113
    expect(tester.getSemantics(find.byType(ExpandIcon)), matchesSemantics(
114 115 116 117 118 119 120 121
      hasTapAction: true,
      hasEnabledState: true,
      isEnabled: true,
      isButton: true,
      onTapHint: localizations.collapsedIconTapHint,
    ));
    handle.dispose();
  });
122
}
Ian Hickson's avatar
Ian Hickson committed
123 124

Widget wrap({ Widget child }) {
125 126 127
  return MaterialApp(
    home: Center(
      child: Material(child: child),
Ian Hickson's avatar
Ian Hickson committed
128 129 130
    ),
  );
}