expand_icon_test.dart 9.32 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6
// 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';
7
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
8

9
Widget wrap({ required Widget child, ThemeData? theme }) {
10 11 12 13 14 15 16 17
  return MaterialApp(
    theme: theme,
    home: Center(
      child: Material(child: child),
    ),
  );
}

18
void main() {
19
  testWidgetsWithLeakTracking('ExpandIcon test', (WidgetTester tester) async {
20
    bool expanded = false;
21 22 23 24 25 26 27
    IconTheme iconTheme;

    // Light mode tests
    await tester.pumpWidget(wrap(
      child: ExpandIcon(
        onPressed: (bool isExpanded) {
          expanded = !expanded;
28
        },
29 30 31 32 33 34 35 36 37 38 39 40 41
      ),
    ));
    await tester.pumpAndSettle();

    expect(expanded, isFalse);
    iconTheme = tester.firstWidget(find.byType(IconTheme).last);
    expect(iconTheme.data.color, equals(Colors.black54));

    await tester.tap(find.byType(ExpandIcon));
    await tester.pumpAndSettle();
    expect(expanded, isTrue);
    iconTheme = tester.firstWidget(find.byType(IconTheme).last);
    expect(iconTheme.data.color, equals(Colors.black54));
42

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    await tester.tap(find.byType(ExpandIcon));
    await tester.pumpAndSettle();
    expect(expanded, isFalse);
    iconTheme = tester.firstWidget(find.byType(IconTheme).last);
    expect(iconTheme.data.color, equals(Colors.black54));

    // Dark mode tests
    await tester.pumpWidget(wrap(
      child: ExpandIcon(
        onPressed: (bool isExpanded) {
          expanded = !expanded;
        },
      ),
      theme: ThemeData(brightness: Brightness.dark),
    ));
    await tester.pumpAndSettle();
59 60

    expect(expanded, isFalse);
61 62 63
    iconTheme = tester.firstWidget(find.byType(IconTheme).last);
    expect(iconTheme.data.color, equals(Colors.white60));

64
    await tester.tap(find.byType(ExpandIcon));
65
    await tester.pumpAndSettle();
66
    expect(expanded, isTrue);
67 68 69
    iconTheme = tester.firstWidget(find.byType(IconTheme).last);
    expect(iconTheme.data.color, equals(Colors.white60));

70
    await tester.tap(find.byType(ExpandIcon));
71
    await tester.pumpAndSettle();
72
    expect(expanded, isFalse);
73 74
    iconTheme = tester.firstWidget(find.byType(IconTheme).last);
    expect(iconTheme.data.color, equals(Colors.white60));
75 76
  });

77
  testWidgetsWithLeakTracking('ExpandIcon disabled', (WidgetTester tester) async {
78 79 80
    IconTheme iconTheme;
    // Light mode test
    await tester.pumpWidget(wrap(
81
      theme: ThemeData(useMaterial3: false),
82 83 84 85 86
      child: const ExpandIcon(onPressed: null),
    ));
    await tester.pumpAndSettle();

    iconTheme = tester.firstWidget(find.byType(IconTheme).last);
87
    expect(iconTheme.data.color, equals(Colors.black38));
88 89 90 91

    // Dark mode test
    await tester.pumpWidget(wrap(
      child: const ExpandIcon(onPressed: null),
92
      theme: ThemeData(useMaterial3: false, brightness: Brightness.dark),
93 94 95 96
    ));
    await tester.pumpAndSettle();

    iconTheme = tester.firstWidget(find.byType(IconTheme).last);
97
    expect(iconTheme.data.color, equals(Colors.white38));
98
  });
99

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

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    await tester.pumpWidget(wrap(
      child: ExpandIcon(
        onPressed: (bool isExpanded) {
          expanded = !expanded;
        },
      ),
    ));

    await tester.pumpWidget(wrap(
      child: ExpandIcon(
        isExpanded: true,
        onPressed: (bool isExpanded) {
          expanded = !expanded;
        },
      ),
    ));
119 120 121

    expect(expanded, isFalse);
  });
122

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

126 127 128 129 130 131 132 133
    await tester.pumpWidget(wrap(
      child: ExpandIcon(
        isExpanded: expanded,
        onPressed: (bool isExpanded) {
          expanded = !isExpanded;
        },
      ),
    ));
134 135 136
    final RotationTransition rotation = tester.firstWidget(find.byType(RotationTransition));
    expect(rotation.turns.value, 0.5);
  });
137

138
  testWidgetsWithLeakTracking('ExpandIcon default size is 24', (WidgetTester tester) async {
139 140 141 142 143
    final ExpandIcon expandIcon =  ExpandIcon(
      onPressed: (bool isExpanded) {},
    );

    await tester.pumpWidget(wrap(
144
      child: expandIcon,
145 146 147 148 149 150
    ));

    final ExpandIcon icon = tester.firstWidget(find.byWidget(expandIcon));
    expect(icon.size, 24);
  });

151
  testWidgetsWithLeakTracking('ExpandIcon has the correct given size', (WidgetTester tester) async {
152 153 154 155 156 157
    ExpandIcon expandIcon =  ExpandIcon(
      size: 36,
      onPressed: (bool isExpanded) {},
    );

    await tester.pumpWidget(wrap(
158
      child: expandIcon,
159 160 161 162 163 164 165 166 167 168 169
    ));

    ExpandIcon icon = tester.firstWidget(find.byWidget(expandIcon));
    expect(icon.size, 36);

    expandIcon =  ExpandIcon(
      size: 48,
      onPressed: (bool isExpanded) {},
    );

    await tester.pumpWidget(wrap(
170
      child: expandIcon,
171 172 173 174 175 176
    ));

    icon = tester.firstWidget(find.byWidget(expandIcon));
    expect(icon.size, 48);
  });

177
  testWidgetsWithLeakTracking('ExpandIcon has correct semantic hints', (WidgetTester tester) async {
178 179 180
    final SemanticsHandle handle = tester.ensureSemantics();
    const DefaultMaterialLocalizations localizations = DefaultMaterialLocalizations();
    await tester.pumpWidget(wrap(
181
      theme: ThemeData(useMaterial3: false),
182 183 184 185
      child: ExpandIcon(
        isExpanded: true,
        onPressed: (bool _) { },
      ),
186 187
    ));

188
    expect(tester.getSemantics(find.byType(ExpandIcon)), matchesSemantics(
189 190 191
      hasTapAction: true,
      hasEnabledState: true,
      isEnabled: true,
192
      isFocusable: true,
193 194 195 196 197
      isButton: true,
      onTapHint: localizations.expandedIconTapHint,
    ));

    await tester.pumpWidget(wrap(
198
      child: ExpandIcon(
199
        onPressed: (bool _) { },
200
      ),
201 202
    ));

203
    expect(tester.getSemantics(find.byType(ExpandIcon)), matchesSemantics(
204 205 206
      hasTapAction: true,
      hasEnabledState: true,
      isEnabled: true,
207
      isFocusable: true,
208 209 210 211 212
      isButton: true,
      onTapHint: localizations.collapsedIconTapHint,
    ));
    handle.dispose();
  });
Ian Hickson's avatar
Ian Hickson committed
213

214
  testWidgetsWithLeakTracking('ExpandIcon uses custom icon color and expanded icon color', (WidgetTester tester) async {
215 216 217 218 219 220
    bool expanded = false;
    IconTheme iconTheme;

    await tester.pumpWidget(wrap(
      child: StatefulBuilder(
        builder: (BuildContext context, StateSetter setState) {
221 222 223 224 225 226 227 228 229 230 231
          return ExpandIcon(
            isExpanded: expanded,
            onPressed: (bool isExpanded) {
              setState(() {
                expanded = !isExpanded;
              });
            },
            color: Colors.indigo,
          );
        },
      ),
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
    ));
    await tester.pumpAndSettle();
    iconTheme = tester.firstWidget(find.byType(IconTheme).last);
    expect(iconTheme.data.color, equals(Colors.indigo));

    await tester.tap(find.byType(ExpandIcon));
    await tester.pumpAndSettle();
    iconTheme = tester.firstWidget(find.byType(IconTheme).last);
    expect(iconTheme.data.color, equals(Colors.indigo));

    expanded = false;

    await tester.pumpWidget(wrap(
      child: StatefulBuilder(
        builder: (BuildContext context, StateSetter setState) {
247 248 249 250 251 252 253 254 255 256 257 258
          return ExpandIcon(
            isExpanded: expanded,
            onPressed: (bool isExpanded) {
              setState(() {
                expanded = !isExpanded;
              });
            },
            color: Colors.indigo,
            expandedColor: Colors.teal,
          );
        },
      ),
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
    ));
    await tester.pumpAndSettle();
    iconTheme = tester.firstWidget(find.byType(IconTheme).last);
    expect(iconTheme.data.color, equals(Colors.indigo));

    await tester.tap(find.byType(ExpandIcon));
    await tester.pumpAndSettle();
    iconTheme = tester.firstWidget(find.byType(IconTheme).last);
    expect(iconTheme.data.color, equals(Colors.teal));

    await tester.tap(find.byType(ExpandIcon));
    await tester.pumpAndSettle();
    iconTheme = tester.firstWidget(find.byType(IconTheme).last);
    expect(iconTheme.data.color, equals(Colors.indigo));
  });

275
  testWidgetsWithLeakTracking('ExpandIcon uses custom disabled icon color', (WidgetTester tester) async {
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
    IconTheme iconTheme;

    await tester.pumpWidget(wrap(
      child: const ExpandIcon(
        onPressed: null,
        disabledColor: Colors.cyan,
      ),
    ));
    await tester.pumpAndSettle();
    iconTheme = tester.firstWidget(find.byType(IconTheme).last);
    expect(iconTheme.data.color, equals(Colors.cyan));

    await tester.pumpWidget(wrap(
      child: const ExpandIcon(
        onPressed: null,
        color: Colors.indigo,
        disabledColor: Colors.cyan,
      ),
    ));
    await tester.pumpAndSettle();
    iconTheme = tester.firstWidget(find.byType(IconTheme).last);
    expect(iconTheme.data.color, equals(Colors.cyan));

    await tester.pumpWidget(wrap(
      child: const ExpandIcon(
        isExpanded: true,
        onPressed: null,
        disabledColor: Colors.cyan,
      ),
    ));
    await tester.pumpWidget(wrap(
      child: const ExpandIcon(
        isExpanded: true,
        onPressed: null,
        expandedColor: Colors.teal,
        disabledColor: Colors.cyan,
      ),
    ));
    await tester.pumpAndSettle();
    iconTheme = tester.firstWidget(find.byType(IconTheme).last);
    expect(iconTheme.data.color, equals(Colors.cyan));
  });
Ian Hickson's avatar
Ian Hickson committed
318
}