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

10
class TestIcon extends StatefulWidget {
11
  const TestIcon({super.key});
12 13 14 15 16 17

  @override
  TestIconState createState() => TestIconState();
}

class TestIconState extends State<TestIcon> {
18
  late IconThemeData iconTheme;
19 20 21 22 23 24 25 26 27

  @override
  Widget build(BuildContext context) {
    iconTheme = IconTheme.of(context);
    return const Icon(Icons.expand_more);
  }
}

class TestText extends StatefulWidget {
28
  const TestText(this.text, {super.key});
29 30 31 32 33 34 35 36

  final String text;

  @override
  TestTextState createState() => TestTextState();
}

class TestTextState extends State<TestText> {
37
  late TextStyle textStyle;
38 39 40 41 42 43 44 45

  @override
  Widget build(BuildContext context) {
    textStyle = DefaultTextStyle.of(context).style;
    return Text(widget.text);
  }
}

46
void main() {
47 48 49 50
  const Color dividerColor = Color(0x1f333333);
  const Color foregroundColor = Colors.blueAccent;
  const Color unselectedWidgetColor = Colors.black54;
  const Color headerColor = Colors.black45;
51

52
  testWidgetsWithLeakTracking('ExpansionTile initial state', (WidgetTester tester) async {
53
    final Key topKey = UniqueKey();
54 55 56
    const Key expandedKey = PageStorageKey<String>('expanded');
    const Key collapsedKey = PageStorageKey<String>('collapsed');
    const Key defaultKey = PageStorageKey<String>('default');
57

58
    final Key tileKey = UniqueKey();
59
    const Clip clipBehavior = Clip.antiAlias;
60

61 62
    await tester.pumpWidget(MaterialApp(
      theme: ThemeData(
63
        dividerColor: dividerColor,
64
      ),
65 66 67
      home: Material(
        child: SingleChildScrollView(
          child: Column(
68
            children: <Widget>[
69 70
              ListTile(title: const Text('Top'), key: topKey),
              ExpansionTile(
71 72 73
                key: expandedKey,
                initiallyExpanded: true,
                title: const Text('Expanded'),
74
                backgroundColor: Colors.red,
75
                clipBehavior: clipBehavior,
76
                children: <Widget>[
77
                  ListTile(
78
                    key: tileKey,
79 80 81
                    title: const Text('0'),
                  ),
                ],
82
              ),
83
              ExpansionTile(
84 85 86
                key: collapsedKey,
                title: const Text('Collapsed'),
                children: <Widget>[
87
                  ListTile(
88
                    key: tileKey,
89 90 91
                    title: const Text('0'),
                  ),
                ],
92
              ),
93
              const ExpansionTile(
94
                key: defaultKey,
95 96 97
                title: Text('Default'),
                children: <Widget>[
                  ListTile(title: Text('0')),
98 99 100 101 102 103
                ],
              ),
            ],
          ),
        ),
      ),
104 105 106
    ));

    double getHeight(Key key) => tester.getSize(find.byKey(key)).height;
107 108 109 110
    Container getContainer(Key key) => tester.firstWidget(find.descendant(
      of: find.byKey(key),
      matching: find.byType(Container),
    ));
111 112 113 114 115

    expect(getHeight(topKey), getHeight(expandedKey) - getHeight(tileKey) - 2.0);
    expect(getHeight(topKey), getHeight(collapsedKey) - 2.0);
    expect(getHeight(topKey), getHeight(defaultKey) - 2.0);

116 117 118 119
    // expansionTile should have Clip.antiAlias as clipBehavior
    expect(getContainer(expandedKey).clipBehavior, clipBehavior);

    ShapeDecoration expandedContainerDecoration = getContainer(expandedKey).decoration! as ShapeDecoration;
120
    expect(expandedContainerDecoration.color, Colors.red);
121 122
    expect((expandedContainerDecoration.shape as Border).top.color, dividerColor);
    expect((expandedContainerDecoration.shape as Border).bottom.color, dividerColor);
123

124
    ShapeDecoration collapsedContainerDecoration = getContainer(collapsedKey).decoration! as ShapeDecoration;
125
    expect(collapsedContainerDecoration.color, Colors.transparent);
126 127
    expect((collapsedContainerDecoration.shape as Border).top.color, Colors.transparent);
    expect((collapsedContainerDecoration.shape as Border).bottom.color, Colors.transparent);
128

129 130 131 132 133
    await tester.tap(find.text('Expanded'));
    await tester.tap(find.text('Collapsed'));
    await tester.tap(find.text('Default'));

    await tester.pump();
134 135 136

    // Pump to the middle of the animation for expansion.
    await tester.pump(const Duration(milliseconds: 100));
137
    final ShapeDecoration collapsingContainerDecoration = getContainer(collapsedKey).decoration! as ShapeDecoration;
138
    expect(collapsingContainerDecoration.color, Colors.transparent);
139 140
    expect((collapsingContainerDecoration.shape as Border).top.color, const Color(0x15222222));
    expect((collapsingContainerDecoration.shape as Border).bottom.color, const Color(0x15222222));
141 142

    // Pump all the way to the end now.
143 144 145 146 147
    await tester.pump(const Duration(seconds: 1));

    expect(getHeight(topKey), getHeight(expandedKey) - 2.0);
    expect(getHeight(topKey), getHeight(collapsedKey) - getHeight(tileKey) - 2.0);
    expect(getHeight(topKey), getHeight(defaultKey) - getHeight(tileKey) - 2.0);
148 149

    // Expanded should be collapsed now.
150
    expandedContainerDecoration = getContainer(expandedKey).decoration! as ShapeDecoration;
151
    expect(expandedContainerDecoration.color, Colors.transparent);
152 153
    expect((expandedContainerDecoration.shape as Border).top.color, Colors.transparent);
    expect((expandedContainerDecoration.shape as Border).bottom.color, Colors.transparent);
154 155

    // Collapsed should be expanded now.
156
    collapsedContainerDecoration = getContainer(collapsedKey).decoration! as ShapeDecoration;
157
    expect(collapsedContainerDecoration.color, Colors.transparent);
158 159
    expect((collapsedContainerDecoration.shape as Border).top.color, dividerColor);
    expect((collapsedContainerDecoration.shape as Border).bottom.color, dividerColor);
Dan Field's avatar
Dan Field committed
160
  }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS,  TargetPlatform.macOS }));
161

162
  testWidgetsWithLeakTracking('ExpansionTile Theme dependencies', (WidgetTester tester) async {
163 164 165 166 167 168 169 170
    final Key expandedTitleKey = UniqueKey();
    final Key collapsedTitleKey = UniqueKey();
    final Key expandedIconKey = UniqueKey();
    final Key collapsedIconKey = UniqueKey();

    await tester.pumpWidget(
      MaterialApp(
        theme: ThemeData(
171
          useMaterial3: false,
172 173
          colorScheme: ColorScheme.fromSwatch().copyWith(primary: foregroundColor),
          unselectedWidgetColor: unselectedWidgetColor,
174
          textTheme: const TextTheme(titleMedium: TextStyle(color: headerColor)),
175 176 177 178 179 180 181 182 183 184 185
        ),
        home: Material(
          child: SingleChildScrollView(
            child: Column(
              children: <Widget>[
                const ListTile(title: Text('Top')),
                ExpansionTile(
                  initiallyExpanded: true,
                  title: TestText('Expanded', key: expandedTitleKey),
                  backgroundColor: Colors.red,
                  trailing: TestIcon(key: expandedIconKey),
186
                  children: const <Widget>[ListTile(title: Text('0'))],
187 188 189 190
                ),
                ExpansionTile(
                  title: TestText('Collapsed', key: collapsedTitleKey),
                  trailing: TestIcon(key: collapsedIconKey),
191
                  children: const <Widget>[ListTile(title: Text('0'))],
192 193 194 195 196 197 198 199
                ),
              ],
            ),
          ),
        ),
      ),
    );

200 201
    Color iconColor(Key key) => tester.state<TestIconState>(find.byKey(key)).iconTheme.color!;
    Color textColor(Key key) => tester.state<TestTextState>(find.byKey(key)).textStyle.color!;
202

203 204 205 206
    expect(textColor(expandedTitleKey), foregroundColor);
    expect(textColor(collapsedTitleKey), headerColor);
    expect(iconColor(expandedIconKey), foregroundColor);
    expect(iconColor(collapsedIconKey), unselectedWidgetColor);
207 208 209 210 211 212 213 214

    // Tap both tiles to change their state: collapse and extend respectively
    await tester.tap(find.text('Expanded'));
    await tester.tap(find.text('Collapsed'));
    await tester.pump();
    await tester.pump(const Duration(seconds: 1));
    await tester.pump(const Duration(seconds: 1));

215 216 217 218
    expect(textColor(expandedTitleKey), headerColor);
    expect(textColor(collapsedTitleKey), foregroundColor);
    expect(iconColor(expandedIconKey), unselectedWidgetColor);
    expect(iconColor(collapsedIconKey), foregroundColor);
Dan Field's avatar
Dan Field committed
219
  }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS,  TargetPlatform.macOS }));
220

221
  testWidgetsWithLeakTracking('ExpansionTile subtitle', (WidgetTester tester) async {
222 223 224 225 226 227 228 229 230 231 232 233 234 235
    await tester.pumpWidget(
      const MaterialApp(
        home: Scaffold(
          body: ExpansionTile(
            title: Text('Title'),
            subtitle: Text('Subtitle'),
            children: <Widget>[ListTile(title: Text('0'))],
          ),
        ),
      ),
    );

    expect(find.text('Subtitle'), findsOneWidget);
  });
236

237
  testWidgetsWithLeakTracking('ExpansionTile maintainState', (WidgetTester tester) async {
238 239 240 241
    await tester.pumpWidget(
      MaterialApp(
        theme: ThemeData(
          platform: TargetPlatform.iOS,
242
          dividerColor: dividerColor,
243
        ),
244
        home: const Material(
245 246
          child: SingleChildScrollView(
            child: Column(
247
              children: <Widget>[
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
                ExpansionTile(
                  title: Text('Tile 1'),
                  maintainState: true,
                  children: <Widget>[
                    Text('Maintaining State'),
                  ],
                ),
                ExpansionTile(
                  title: Text('Title 2'),
                  children: <Widget>[
                    Text('Discarding State'),
                  ],
                ),
              ],
            ),
          ),
        ),
      ),
    );
267 268 269 270 271 272 273 274

     // This text should be offstage while ExpansionTile collapsed
     expect(find.text('Maintaining State', skipOffstage: false), findsOneWidget);
     expect(find.text('Maintaining State'), findsNothing);
     // This text shouldn't be there while ExpansionTile collapsed
     expect(find.text('Discarding State'), findsNothing);
   });

275
  testWidgetsWithLeakTracking('ExpansionTile padding test', (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
    await tester.pumpWidget(const MaterialApp(
      home: Material(
        child: Center(
          child: ExpansionTile(
            title: Text('Hello'),
            tilePadding: EdgeInsets.fromLTRB(8, 12, 4, 10),
          ),
        ),
      ),
    ));

    final Rect titleRect = tester.getRect(find.text('Hello'));
    final Rect trailingRect = tester.getRect(find.byIcon(Icons.expand_more));
    final Rect listTileRect = tester.getRect(find.byType(ListTile));
    final Rect tallerWidget = titleRect.height > trailingRect.height ? titleRect : trailingRect;

    // Check the positions of title and trailing Widgets, after padding is applied.
    expect(listTileRect.left, titleRect.left - 8);
    expect(listTileRect.right, trailingRect.right + 4);

    // Calculate the remaining height of ListTile from the default height.
    final double remainingHeight = 56 - tallerWidget.height;
    expect(listTileRect.top, tallerWidget.top - remainingHeight / 2 - 12);
    expect(listTileRect.bottom, tallerWidget.bottom + remainingHeight / 2 + 10);
  });
301

302
  testWidgetsWithLeakTracking('ExpansionTile expandedAlignment test', (WidgetTester tester) async {
303
    await tester.pumpWidget(const MaterialApp(
304 305 306
      home: Material(
        child: Center(
          child: ExpansionTile(
307
            title: Text('title'),
308 309
            expandedAlignment: Alignment.centerLeft,
            children: <Widget>[
310 311
              SizedBox(height: 100, width: 100),
              SizedBox(height: 100, width: 80),
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
            ],
          ),
        ),
      ),
    ));

    await tester.tap(find.text('title'));
    await tester.pumpAndSettle();

    final Rect columnRect = tester.getRect(find.byType(Column).last);

    // The expandedAlignment is used to define the alignment of the Column widget in
    // expanded tile, not the alignment of the children inside the Column.
    expect(columnRect.left, 0.0);
    // The width of the Column is the width of the largest child. The largest width
    // being 100.0, the offset of the right edge of Column from X-axis should be 100.0.
    expect(columnRect.right, 100.0);
  });

331
  testWidgetsWithLeakTracking('ExpansionTile expandedCrossAxisAlignment test', (WidgetTester tester) async {
332 333 334
    const Key child0Key = Key('child0');
    const Key child1Key = Key('child1');

335 336 337 338 339
    await tester.pumpWidget(const MaterialApp(
      home: Material(
        child: Center(
          child: ExpansionTile(
            title: Text('title'),
340 341 342 343 344
            // Set the column's alignment to Alignment.centerRight to test CrossAxisAlignment
            // of children widgets. This helps distinguish the effect of expandedAlignment
            // and expandedCrossAxisAlignment later in the test.
            expandedAlignment: Alignment.centerRight,
            expandedCrossAxisAlignment: CrossAxisAlignment.start,
345
            children: <Widget>[
346 347
              SizedBox(height: 100, width: 100, key: child0Key),
              SizedBox(height: 100, width: 80, key: child1Key),
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
            ],
          ),
        ),
      ),
    ));

    await tester.tap(find.text('title'));
    await tester.pumpAndSettle();

    final Rect columnRect = tester.getRect(find.byType(Column).last);
    final Rect child0Rect = tester.getRect(find.byKey(child0Key));
    final Rect child1Rect = tester.getRect(find.byKey(child1Key));

    // Since expandedAlignment is set to Alignment.centerRight, the column of children
    // should be aligned to the center right of the expanded tile. This provides confirmation
    // that the expandedCrossAxisAlignment.start is 700.0, where columnRect.left is.
    expect(columnRect.right, 800.0);
    // The width of the Column is the width of the largest child. The largest width
    // being 100.0, the offset of the left edge of Column from X-axis should be 700.0.
    expect(columnRect.left, 700.0);

    // Considering the value of expandedCrossAxisAlignment is CrossAxisAlignment.start,
    // the offset of the left edge of both the children from X-axis should be 700.0.
    expect(child0Rect.left, 700.0);
    expect(child1Rect.left, 700.0);
  });

375
  testWidgetsWithLeakTracking('CrossAxisAlignment.baseline is not allowed', (WidgetTester tester) async {
376 377 378 379 380 381 382 383 384
    expect(
      () {
        MaterialApp(
          home: Material(
            child: ExpansionTile(
              initiallyExpanded: true,
              title: const Text('title'),
              expandedCrossAxisAlignment: CrossAxisAlignment.baseline,
            ),
385
          ),
386 387 388
        );
      },
      throwsA(isA<AssertionError>().having((AssertionError error) => error.toString(), '.toString()', contains(
389 390
        'CrossAxisAlignment.baseline is not supported since the expanded'
        ' children are aligned in a column, not a row. Try to use another constant.',
391 392
      ))),
    );
393 394
  });

395
  testWidgetsWithLeakTracking('expandedCrossAxisAlignment and expandedAlignment default values', (WidgetTester tester) async {
396 397
    const Key child1Key = Key('child1');

398
    await tester.pumpWidget(const MaterialApp(
399 400 401
      home: Material(
        child: Center(
          child: ExpansionTile(
402
            title: Text('title'),
403
            children: <Widget>[
404 405
              SizedBox(height: 100, width: 100),
              SizedBox(height: 100, width: 80, key: child1Key),
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
            ],
          ),
        ),
      ),
    ));


    await tester.tap(find.text('title'));
    await tester.pumpAndSettle();

    final Rect columnRect = tester.getRect(find.byType(Column).last);
    final Rect child1Rect = tester.getRect(find.byKey(child1Key));

    // The default viewport size is Size(800, 600).
    // By default the value of extendedAlignment is Alignment.center, hence the offset
    // of left and right edges from x axis should be equal.
    expect(columnRect.left, 800 - columnRect.right);

    // By default the value of extendedCrossAxisAlignment is CrossAxisAlignment.center, hence
    // the offset of left and right edges from Column should be equal.
    expect(child1Rect.left - columnRect.left, columnRect.right - child1Rect.right);

  });

430
  testWidgetsWithLeakTracking('childrenPadding default value', (WidgetTester tester) async {
431 432 433 434 435 436 437 438 439 440
    await tester.pumpWidget(
      const MaterialApp(
        home: Material(
          child: Center(
            child: ExpansionTile(
              title: Text('title'),
              children: <Widget>[
                SizedBox(height: 100, width: 100),
              ],
            ),
441 442 443
          ),
        ),
      ),
444
    );
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459

    await tester.tap(find.text('title'));
    await tester.pumpAndSettle();

    final Rect columnRect = tester.getRect(find.byType(Column).last);
    final Rect paddingRect = tester.getRect(find.byType(Padding).last);

    // By default, the value of childrenPadding is EdgeInsets.zero, hence offset
    // of all the edges from x-axis and y-axis should be equal for Padding and Column.
    expect(columnRect.top, paddingRect.top);
    expect(columnRect.left, paddingRect.left);
    expect(columnRect.right, paddingRect.right);
    expect(columnRect.bottom, paddingRect.bottom);
  });

460
  testWidgetsWithLeakTracking('ExpansionTile childrenPadding test', (WidgetTester tester) async {
461 462 463 464 465 466 467 468 469 470 471
    await tester.pumpWidget(
      const MaterialApp(
        home: Material(
          child: Center(
            child: ExpansionTile(
              title: Text('title'),
              childrenPadding: EdgeInsets.fromLTRB(10, 8, 12, 4),
              children: <Widget>[
                SizedBox(height: 100, width: 100),
              ],
            ),
472 473 474
          ),
        ),
      ),
475
    );
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490

    await tester.tap(find.text('title'));
    await tester.pumpAndSettle();

    final Rect columnRect = tester.getRect(find.byType(Column).last);
    final Rect paddingRect = tester.getRect(find.byType(Padding).last);

    // Check the offset of all the edges from x-axis and y-axis after childrenPadding
    // is applied.
    expect(columnRect.left, paddingRect.left + 10);
    expect(columnRect.top, paddingRect.top + 8);
    expect(columnRect.right, paddingRect.right - 12);
    expect(columnRect.bottom, paddingRect.bottom - 4);
  });

491
  testWidgetsWithLeakTracking('ExpansionTile.collapsedBackgroundColor', (WidgetTester tester) async {
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
    const Key expansionTileKey = Key('expansionTileKey');
    const Color backgroundColor = Colors.red;
    const Color collapsedBackgroundColor = Colors.brown;

    await tester.pumpWidget(const MaterialApp(
      home: Material(
        child: ExpansionTile(
          key: expansionTileKey,
          title: Text('Title'),
          backgroundColor: backgroundColor,
          collapsedBackgroundColor: collapsedBackgroundColor,
          children: <Widget>[
            SizedBox(height: 100, width: 100),
          ],
        ),
      ),
    ));

510
    ShapeDecoration shapeDecoration =  tester.firstWidget<Container>(find.descendant(
511 512
      of: find.byKey(expansionTileKey),
      matching: find.byType(Container),
513
    )).decoration! as ShapeDecoration;
514

515
    expect(shapeDecoration.color, collapsedBackgroundColor);
516 517 518 519

    await tester.tap(find.text('Title'));
    await tester.pumpAndSettle();

520
    shapeDecoration =  tester.firstWidget<Container>(find.descendant(
521 522
      of: find.byKey(expansionTileKey),
      matching: find.byType(Container),
523
    )).decoration! as ShapeDecoration;
524

525
    expect(shapeDecoration.color, backgroundColor);
526
  });
527

528
  testWidgetsWithLeakTracking('ExpansionTile default iconColor, textColor', (WidgetTester tester) async {
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
    final ThemeData theme = ThemeData(useMaterial3: true);

    await tester.pumpWidget(MaterialApp(
      theme: theme,
      home: const Material(
        child: ExpansionTile(
          title: TestText('title'),
          trailing: TestIcon(),
          children: <Widget>[
            SizedBox(height: 100, width: 100),
          ],
        ),
      ),
    ));

    Color getIconColor() => tester.state<TestIconState>(find.byType(TestIcon)).iconTheme.color!;
    Color getTextColor() => tester.state<TestTextState>(find.byType(TestText)).textStyle.color!;

    expect(getIconColor(), theme.colorScheme.onSurfaceVariant);
    expect(getTextColor(), theme.colorScheme.onSurface);

    await tester.tap(find.text('title'));
    await tester.pumpAndSettle();

    expect(getIconColor(), theme.colorScheme.primary);
    expect(getTextColor(), theme.colorScheme.onSurface);
  });

557
  testWidgetsWithLeakTracking('ExpansionTile iconColor, textColor', (WidgetTester tester) async {
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
    // Regression test for https://github.com/flutter/flutter/pull/78281

    const Color iconColor = Color(0xff00ff00);
    const Color collapsedIconColor = Color(0xff0000ff);
    const Color textColor = Color(0xff00ffff);
    const Color collapsedTextColor = Color(0xffff00ff);

    await tester.pumpWidget(const MaterialApp(
      home: Material(
        child: ExpansionTile(
          iconColor: iconColor,
          collapsedIconColor: collapsedIconColor,
          textColor: textColor,
          collapsedTextColor: collapsedTextColor,
          title: TestText('title'),
          trailing: TestIcon(),
          children: <Widget>[
            SizedBox(height: 100, width: 100),
          ],
        ),
      ),
    ));

    Color getIconColor() => tester.state<TestIconState>(find.byType(TestIcon)).iconTheme.color!;
    Color getTextColor() => tester.state<TestTextState>(find.byType(TestText)).textStyle.color!;

    expect(getIconColor(), collapsedIconColor);
    expect(getTextColor(), collapsedTextColor);

    await tester.tap(find.text('title'));
    await tester.pumpAndSettle();

    expect(getIconColor(), iconColor);
    expect(getTextColor(), textColor);
  });
593

594
  testWidgetsWithLeakTracking('ExpansionTile Border', (WidgetTester tester) async {
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
    const Key expansionTileKey = PageStorageKey<String>('expansionTile');

    const Border collapsedShape = Border(
      top: BorderSide(color: Colors.blue),
      bottom: BorderSide(color: Colors.green)
    );
    final Border shape = Border.all(color: Colors.red);

    await tester.pumpWidget(MaterialApp(
      home: Material(
        child: SingleChildScrollView(
          child: Column(
            children: <Widget>[
              ExpansionTile(
                key: expansionTileKey,
                title: const Text('ExpansionTile'),
                collapsedShape: collapsedShape,
                shape: shape,
                children: const <Widget>[
                  ListTile(
                    title: Text('0'),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    ));

    Container getContainer(Key key) => tester.firstWidget(find.descendant(
      of: find.byKey(key),
      matching: find.byType(Container),
    ));

    // expansionTile should be Collapsed now.
    ShapeDecoration expandedContainerDecoration = getContainer(expansionTileKey).decoration! as ShapeDecoration;
    expect(expandedContainerDecoration.shape, collapsedShape);

    await tester.tap(find.text('ExpansionTile'));
    await tester.pumpAndSettle();

    // expansionTile should be Expanded now.
    expandedContainerDecoration = getContainer(expansionTileKey).decoration! as ShapeDecoration;
    expect(expandedContainerDecoration.shape, shape);
  });

642
  testWidgetsWithLeakTracking('ExpansionTile platform controlAffinity test', (WidgetTester tester) async {
643 644 645 646 647 648 649 650 651 652 653 654 655
    await tester.pumpWidget(const MaterialApp(
      home: Material(
        child: ExpansionTile(
          title: Text('Title'),
        ),
      ),
    ));

    final ListTile listTile = tester.widget(find.byType(ListTile));
    expect(listTile.leading, isNull);
    expect(listTile.trailing.runtimeType, RotationTransition);
  });

656
  testWidgetsWithLeakTracking('ExpansionTile trailing controlAffinity test', (WidgetTester tester) async {
657 658 659 660 661 662 663 664 665 666 667 668 669 670
    await tester.pumpWidget(const MaterialApp(
      home: Material(
        child: ExpansionTile(
          title: Text('Title'),
          controlAffinity: ListTileControlAffinity.trailing,
        ),
      ),
    ));

    final ListTile listTile = tester.widget(find.byType(ListTile));
    expect(listTile.leading, isNull);
    expect(listTile.trailing.runtimeType, RotationTransition);
  });

671
  testWidgetsWithLeakTracking('ExpansionTile leading controlAffinity test', (WidgetTester tester) async {
672 673 674 675 676 677 678 679 680 681 682 683 684 685
    await tester.pumpWidget(const MaterialApp(
      home: Material(
        child: ExpansionTile(
          title: Text('Title'),
          controlAffinity: ListTileControlAffinity.leading,
        ),
      ),
    ));

    final ListTile listTile = tester.widget(find.byType(ListTile));
    expect(listTile.leading.runtimeType, RotationTransition);
    expect(listTile.trailing, isNull);
  });

686
  testWidgetsWithLeakTracking('ExpansionTile override rotating icon test', (WidgetTester tester) async {
687 688 689 690 691 692 693 694 695 696 697 698 699 700
    await tester.pumpWidget(const MaterialApp(
      home: Material(
        child: ExpansionTile(
          title: Text('Title'),
          leading: Icon(Icons.info),
          controlAffinity: ListTileControlAffinity.leading,
        ),
      ),
    ));

    final ListTile listTile = tester.widget(find.byType(ListTile));
    expect(listTile.leading.runtimeType, Icon);
    expect(listTile.trailing, isNull);
  });
701

702
  testWidgetsWithLeakTracking('Nested ListTile Semantics', (WidgetTester tester) async {
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
    tester.binding.focusManager.highlightStrategy = FocusHighlightStrategy.alwaysTraditional;
    final SemanticsHandle handle = tester.ensureSemantics();

    await tester.pumpWidget(const MaterialApp(
      home: Material(
        child: Column(
          children: <Widget>[
            ExpansionTile(
              title: Text('First Expansion Tile'),
            ),
            ExpansionTile(
              initiallyExpanded: true,
              title: Text('Second Expansion Tile'),
            ),
          ],
        ),
      ),
    ));

    await tester.pumpAndSettle();

    // Focus the first ExpansionTile.
    tester.binding.focusManager.primaryFocus?.nextFocus();
    await tester.pumpAndSettle();

    // The first list tile is focused.
    expect(
      tester.getSemantics(find.byType(ListTile).first),
      matchesSemantics(
        hasTapAction: true,
        hasEnabledState: true,
        isEnabled: true,
        isFocused: true,
        isFocusable: true,
        label: 'First Expansion Tile',
        textDirection: TextDirection.ltr,
      ),
    );

    // The first list tile is not focused.
    expect(
      tester.getSemantics(find.byType(ListTile).last),
      matchesSemantics(
        hasTapAction: true,
        hasEnabledState: true,
        isEnabled: true,
        isFocusable: true,
        label: 'Second Expansion Tile',
        textDirection: TextDirection.ltr,
      ),
    );
    handle.dispose();
  });

757
  testWidgetsWithLeakTracking('ExpansionTile Semantics announcement', (WidgetTester tester) async {
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
    final SemanticsHandle handle = tester.ensureSemantics();
    const DefaultMaterialLocalizations localizations = DefaultMaterialLocalizations();
    await tester.pumpWidget(
      const MaterialApp(
        home: Material(
          child: ExpansionTile(
            title: Text('Title'),
            children: <Widget>[
              SizedBox(height: 100, width: 100),
            ],
          ),
        ),
      ),
    );

    // There is no semantics announcement without tap action.
    expect(tester.takeAnnouncements(), isEmpty);

    // Tap the title to expand ExpansionTile.
    await tester.tap(find.text('Title'));
    await tester.pumpAndSettle();

    // The announcement should be the opposite of the current state.
    // The ExpansionTile is expanded, so the announcement should be
    // "Expanded".
    expect(tester.takeAnnouncements().first.message, localizations.collapsedHint);

    // Tap the title to collapse ExpansionTile.
    await tester.tap(find.text('Title'));
    await tester.pumpAndSettle();

    // The announcement should be the opposite of the current state.
    // The ExpansionTile is collapsed, so the announcement should be
    // "Collapsed".
    expect(tester.takeAnnouncements().first.message, localizations.expandedHint);
    handle.dispose();
  });

796
  testWidgetsWithLeakTracking('Semantics with the onTapHint is an ancestor of ListTile', (WidgetTester tester) async {
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
    // This is a regression test for https://github.com/flutter/flutter/pull/121624
    final SemanticsHandle handle = tester.ensureSemantics();
    const DefaultMaterialLocalizations localizations = DefaultMaterialLocalizations();

    await tester.pumpWidget(const MaterialApp(
      home: Material(
        child: Column(
          children: <Widget>[
            ExpansionTile(
              title: Text('First Expansion Tile'),
            ),
            ExpansionTile(
              initiallyExpanded: true,
              title: Text('Second Expansion Tile'),
            ),
          ],
        ),
      ),
    ));

    SemanticsNode semantics = tester.getSemantics(
      find.ancestor(
        of: find.byType(ListTile).first,
        matching: find.byType(Semantics),
      ).first,
    );
    expect(semantics, isNotNull);
    // The onTapHint is passed to semantics properties's hintOverrides.
    expect(semantics.hintOverrides, isNotNull);
    // The hint should be the opposite of the current state.
    // The first ExpansionTile is collapsed, so the hint should be
    // "double tap to expand".
    expect(semantics.hintOverrides!.onTapHint, localizations.expansionTileCollapsedTapHint);

    semantics = tester.getSemantics(
      find.ancestor(
        of: find.byType(ListTile).last,
        matching: find.byType(Semantics),
      ).first,
    );

    expect(semantics, isNotNull);
    // The onTapHint is passed to semantics properties's hintOverrides.
    expect(semantics.hintOverrides, isNotNull);
    // The hint should be the opposite of the current state.
    // The second ExpansionTile is expanded, so the hint should be
    // "double tap to collapse".
    expect(semantics.hintOverrides!.onTapHint, localizations.expansionTileExpandedTapHint);
    handle.dispose();
  });

848
  testWidgetsWithLeakTracking('Semantics hint for iOS and macOS', (WidgetTester tester) async {
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
    final SemanticsHandle handle = tester.ensureSemantics();
    const DefaultMaterialLocalizations localizations = DefaultMaterialLocalizations();

    await tester.pumpWidget(const MaterialApp(
      home: Material(
        child: Column(
          children: <Widget>[
            ExpansionTile(
              title: Text('First Expansion Tile'),
            ),
            ExpansionTile(
              initiallyExpanded: true,
              title: Text('Second Expansion Tile'),
            ),
          ],
        ),
      ),
    ));

    SemanticsNode semantics = tester.getSemantics(
      find.ancestor(
        of: find.byType(ListTile).first,
        matching: find.byType(Semantics),
      ).first,
    );

    expect(semantics, isNotNull);
    expect(
      semantics.hint,
      '${localizations.expandedHint}\n ${localizations.expansionTileCollapsedHint}',
    );

    semantics = tester.getSemantics(
      find.ancestor(
        of: find.byType(ListTile).last,
        matching: find.byType(Semantics),
      ).first,
    );

    expect(semantics, isNotNull);
    expect(
      semantics.hint,
      '${localizations.collapsedHint}\n ${localizations.expansionTileExpandedHint}',
    );
    handle.dispose();
  }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS, TargetPlatform.macOS }));

896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
  testWidgetsWithLeakTracking('Collapsed ExpansionTile properties can be updated with setState', (WidgetTester tester) async {
    const Key expansionTileKey = Key('expansionTileKey');
    ShapeBorder collapsedShape = const RoundedRectangleBorder(
      borderRadius: BorderRadius.all(Radius.circular(4)),
    );
    Color collapsedTextColor = const Color(0xffffffff);
    Color collapsedBackgroundColor = const Color(0xffff0000);
    Color collapsedIconColor = const Color(0xffffffff);

    await tester.pumpWidget(MaterialApp(
      home: Material(
        child: StatefulBuilder(
          builder: (BuildContext context, StateSetter setState) {
            return Column(
              children: <Widget>[
                ExpansionTile(
                  key: expansionTileKey,
                  collapsedShape: collapsedShape,
                  collapsedTextColor: collapsedTextColor,
                  collapsedBackgroundColor: collapsedBackgroundColor,
                  collapsedIconColor: collapsedIconColor,
                  title: const TestText('title'),
                  trailing: const TestIcon(),
                  children: const <Widget>[
                    SizedBox(height: 100, width: 100),
                  ],
                ),
                // This button is used to update the ExpansionTile properties.
                FilledButton(
                  onPressed: () {
                    setState(() {
                      collapsedShape = const RoundedRectangleBorder(
                        borderRadius: BorderRadius.all(Radius.circular(16)),
                      );
                      collapsedTextColor = const Color(0xff000000);
                      collapsedBackgroundColor = const Color(0xffffff00);
                      collapsedIconColor = const Color(0xff000000);
                    });
                  },
                  child: const Text('Update collapsed properties'),
                ),
              ],
            );
          }
        ),
      ),
    ));

    ShapeDecoration shapeDecoration =  tester.firstWidget<Container>(find.descendant(
      of: find.byKey(expansionTileKey),
      matching: find.byType(Container),
    )).decoration! as ShapeDecoration;

    // Test initial ExpansionTile properties.
    expect(shapeDecoration.shape, const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(4))));
    expect(shapeDecoration.color, const Color(0xffff0000));
    expect(tester.state<TestIconState>(find.byType(TestIcon)).iconTheme.color, const Color(0xffffffff));
    expect(tester.state<TestTextState>(find.byType(TestText)).textStyle.color, const Color(0xffffffff));

    // Tap the button to update the ExpansionTile properties.
    await tester.tap(find.text('Update collapsed properties'));
    await tester.pumpAndSettle();

    shapeDecoration =  tester.firstWidget<Container>(find.descendant(
      of: find.byKey(expansionTileKey),
      matching: find.byType(Container),
    )).decoration! as ShapeDecoration;

    // Test updated ExpansionTile properties.
    expect(shapeDecoration.shape, const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(16))));
    expect(shapeDecoration.color, const Color(0xffffff00));
    expect(tester.state<TestIconState>(find.byType(TestIcon)).iconTheme.color, const Color(0xff000000));
    expect(tester.state<TestTextState>(find.byType(TestText)).textStyle.color, const Color(0xff000000));
  });

  testWidgetsWithLeakTracking('Expanded ExpansionTile properties can be updated with setState', (WidgetTester tester) async {
    const Key expansionTileKey = Key('expansionTileKey');
    ShapeBorder shape = const RoundedRectangleBorder(
      borderRadius: BorderRadius.all(Radius.circular(12)),
    );
    Color textColor = const Color(0xff00ffff);
    Color backgroundColor = const Color(0xff0000ff);
    Color iconColor = const Color(0xff00ffff);

    await tester.pumpWidget(MaterialApp(
      home: Material(
        child: StatefulBuilder(
          builder: (BuildContext context, StateSetter setState) {
            return Column(
              children: <Widget>[
                ExpansionTile(
                  key: expansionTileKey,
                  shape: shape,
                  textColor: textColor,
                  backgroundColor: backgroundColor,
                  iconColor: iconColor,
                  title: const TestText('title'),
                  trailing: const TestIcon(),
                  children: const <Widget>[
                    SizedBox(height: 100, width: 100),
                  ],
                ),
                // This button is used to update the ExpansionTile properties.
                FilledButton(
                  onPressed: () {
                    setState(() {
                      shape = const RoundedRectangleBorder(
                        borderRadius: BorderRadius.all(Radius.circular(6)),
                      );
                      textColor = const Color(0xffffffff);
                      backgroundColor = const Color(0xff123456);
                      iconColor = const Color(0xffffffff);
                    });
                  },
                  child: const Text('Update collapsed properties'),
                ),
              ],
            );
          }
        ),
      ),
    ));

    // Tap to expand the ExpansionTile.
    await tester.tap(find.text('title'));
    await tester.pumpAndSettle();

    ShapeDecoration shapeDecoration =  tester.firstWidget<Container>(find.descendant(
      of: find.byKey(expansionTileKey),
      matching: find.byType(Container),
    )).decoration! as ShapeDecoration;

    // Test initial ExpansionTile properties.
    expect(shapeDecoration.shape, const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(12))));
    expect(shapeDecoration.color, const Color(0xff0000ff));
    expect(tester.state<TestIconState>(find.byType(TestIcon)).iconTheme.color, const Color(0xff00ffff));
    expect(tester.state<TestTextState>(find.byType(TestText)).textStyle.color, const Color(0xff00ffff));

    // Tap the button to update the ExpansionTile properties.
    await tester.tap(find.text('Update collapsed properties'));
    await tester.pumpAndSettle();

    shapeDecoration =  tester.firstWidget<Container>(find.descendant(
      of: find.byKey(expansionTileKey),
      matching: find.byType(Container),
    )).decoration! as ShapeDecoration;
    iconColor = tester.state<TestIconState>(find.byType(TestIcon)).iconTheme.color!;
    textColor = tester.state<TestTextState>(find.byType(TestText)).textStyle.color!;

    // Test updated ExpansionTile properties.
    expect(shapeDecoration.shape, const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(6))));
    expect(shapeDecoration.color, const Color(0xff123456));
    expect(tester.state<TestIconState>(find.byType(TestIcon)).iconTheme.color, const Color(0xffffffff));
    expect(tester.state<TestTextState>(find.byType(TestText)).textStyle.color, const Color(0xffffffff));
  });

1052
  group('Material 2', () {
1053 1054 1055
    // These tests are only relevant for Material 2. Once Material 2
    // support is deprecated and the APIs are removed, these tests
    // can be deleted.
1056

1057
    testWidgetsWithLeakTracking('ExpansionTile default iconColor, textColor', (WidgetTester tester) async {
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
      final ThemeData theme = ThemeData(useMaterial3: false);

      await tester.pumpWidget(MaterialApp(
        theme: theme,
        home: const Material(
          child: ExpansionTile(
            title: TestText('title'),
            trailing: TestIcon(),
            children: <Widget>[
              SizedBox(height: 100, width: 100),
            ],
          ),
        ),
      ));

      Color getIconColor() => tester.state<TestIconState>(find.byType(TestIcon)).iconTheme.color!;
      Color getTextColor() => tester.state<TestTextState>(find.byType(TestText)).textStyle.color!;

      expect(getIconColor(), theme.unselectedWidgetColor);
      expect(getTextColor(), theme.textTheme.titleMedium!.color);

      await tester.tap(find.text('title'));
      await tester.pumpAndSettle();

      expect(getIconColor(), theme.colorScheme.primary);
      expect(getTextColor(), theme.colorScheme.primary);
    });
  });
1086

1087
  testWidgetsWithLeakTracking('ExpansionTileController isExpanded, expand() and collapse()', (WidgetTester tester) async {
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
    final ExpansionTileController controller = ExpansionTileController();

    await tester.pumpWidget(MaterialApp(
      home: Material(
        child: ExpansionTile(
          controller: controller,
          title: const Text('Title'),
          children: const <Widget>[
            Text('Child 0'),
          ],
        ),
      ),
    ));

    expect(find.text('Child 0'), findsNothing);
    expect(controller.isExpanded, isFalse);
    controller.expand();
    expect(controller.isExpanded, isTrue);
    await tester.pumpAndSettle();
    expect(find.text('Child 0'), findsOneWidget);
    expect(controller.isExpanded, isTrue);
    controller.collapse();
    expect(controller.isExpanded, isFalse);
    await tester.pumpAndSettle();
    expect(find.text('Child 0'), findsNothing);
  });

1115
  testWidgetsWithLeakTracking('Calling ExpansionTileController.expand/collapsed has no effect if it is already expanded/collapsed', (WidgetTester tester) async {
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
    final ExpansionTileController controller = ExpansionTileController();

    await tester.pumpWidget(MaterialApp(
      home: Material(
        child: ExpansionTile(
          controller: controller,
          title: const Text('Title'),
          initiallyExpanded: true,
          children: const <Widget>[
            Text('Child 0'),
          ],
        ),
      ),
    ));

    expect(find.text('Child 0'), findsOneWidget);
    expect(controller.isExpanded, isTrue);
    controller.expand();
    expect(controller.isExpanded, isTrue);
    await tester.pump();
    expect(tester.hasRunningAnimations, isFalse);
    expect(find.text('Child 0'), findsOneWidget);
    controller.collapse();
    expect(controller.isExpanded, isFalse);
    await tester.pump();
    expect(tester.hasRunningAnimations, isTrue);
    await tester.pumpAndSettle();
    expect(controller.isExpanded, isFalse);
    expect(find.text('Child 0'), findsNothing);
    controller.collapse();
    expect(controller.isExpanded, isFalse);
    await tester.pump();
    expect(tester.hasRunningAnimations, isFalse);
  });

1151
  testWidgetsWithLeakTracking('Call to ExpansionTileController.of()', (WidgetTester tester) async {
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
    final GlobalKey titleKey = GlobalKey();
    final GlobalKey childKey = GlobalKey();
    await tester.pumpWidget(MaterialApp(
      home: Material(
        child: ExpansionTile(
          initiallyExpanded: true,
          title: Text('Title', key: titleKey),
          children: <Widget>[
            Text('Child 0', key: childKey),
          ],
        ),
      ),
    ));

    final ExpansionTileController controller1 = ExpansionTileController.of(childKey.currentContext!);
    expect(controller1.isExpanded, isTrue);

    final ExpansionTileController controller2 = ExpansionTileController.of(titleKey.currentContext!);
    expect(controller2.isExpanded, isTrue);

    expect(controller1, controller2);
  });

1175
  testWidgetsWithLeakTracking('Call to ExpansionTile.maybeOf()', (WidgetTester tester) async {
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
    final GlobalKey titleKey = GlobalKey();
    final GlobalKey nonDescendantKey = GlobalKey();
    await tester.pumpWidget(MaterialApp(
      home: Material(
        child: Column(
          children: <Widget>[
            ExpansionTile(
              title: Text('Title', key: titleKey),
              children: const <Widget>[
                Text('Child 0'),
              ],
            ),
            Text('Non descendant', key: nonDescendantKey),
          ],
        ),
      ),
    ));

    final ExpansionTileController? controller1 = ExpansionTileController.maybeOf(titleKey.currentContext!);
    expect(controller1, isNotNull);
    expect(controller1?.isExpanded, isFalse);

    final ExpansionTileController? controller2 = ExpansionTileController.maybeOf(nonDescendantKey.currentContext!);
    expect(controller2, isNull);
  });
1201
}