visibility_test.dart 20.6 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'package:flutter/rendering.dart';
6 7
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
8
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
9 10 11 12

import 'semantics_tester.dart';

class TestState extends StatefulWidget {
13
  const TestState({ super.key, required this.child, required this.log });
14 15 16
  final Widget child;
  final List<String> log;
  @override
17
  State<TestState> createState() => _TestStateState();
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
}

class _TestStateState extends State<TestState> {
  @override
  void initState() {
    super.initState();
    widget.log.add('created new state');
  }
  @override
  Widget build(BuildContext context) {
    return widget.child;
  }
}

void main() {
33
  testWidgetsWithLeakTracking('Visibility', (WidgetTester tester) async {
34
    final SemanticsTester semantics = SemanticsTester(tester);
35 36
    final List<String> log = <String>[];

37
    final Widget testChild = GestureDetector(
38
      onTap: () { log.add('tap'); },
39
      child: Builder(
40 41
        builder: (BuildContext context) {
          final bool animating = TickerMode.of(context);
42
          return TestState(
43
            log: log,
44
            child: Text('a $animating', textDirection: TextDirection.rtl),
45 46 47 48 49 50
          );
        },
      ),
    );

    final Matcher expectedSemanticsWhenPresent = hasSemantics(
51
      TestSemantics.root(
52
        children: <TestSemantics>[
53
          TestSemantics.rootChild(
54 55 56
            label: 'a true',
            textDirection: TextDirection.rtl,
            actions: <SemanticsAction>[SemanticsAction.tap],
57
          ),
58 59 60 61 62 63 64
        ],
      ),
      ignoreId: true,
      ignoreRect: true,
      ignoreTransform: true,
    );

65 66 67 68 69 70 71 72 73 74 75 76 77 78
    final Matcher expectedSemanticsWhenPresentWithIgnorePointer = hasSemantics(
      TestSemantics.root(
        children: <TestSemantics>[
          TestSemantics.rootChild(
            label: 'a true',
            textDirection: TextDirection.rtl,
          ),
        ],
      ),
      ignoreId: true,
      ignoreRect: true,
      ignoreTransform: true,
    );

79
    final Matcher expectedSemanticsWhenAbsent = hasSemantics(TestSemantics.root());
80 81

    // We now run a sequence of pumpWidget calls one after the other. In
82
    // addition to verifying that the right behavior is seen in each case, this
83 84
    // also verifies that the widget can dynamically change from state to state.

85
    await tester.pumpWidget(Visibility(child: testChild));
86 87 88 89 90 91 92 93 94 95
    expect(find.byType(Text, skipOffstage: false), findsOneWidget);
    expect(find.text('a true', skipOffstage: false), findsOneWidget);
    expect(find.byType(Visibility), paints..paragraph());
    expect(tester.getSize(find.byType(Visibility)), const Size(800.0, 600.0));
    expect(semantics, expectedSemanticsWhenPresent);
    expect(log, <String>['created new state']);
    await tester.tap(find.byType(Visibility));
    expect(log, <String>['created new state', 'tap']);
    log.clear();

96
    await tester.pumpWidget(Visibility(visible: false, child: testChild));
97 98 99 100 101
    expect(find.byType(Text, skipOffstage: false), findsNothing);
    expect(find.byType(Visibility), paintsNothing);
    expect(tester.getSize(find.byType(Visibility)), const Size(800.0, 600.0));
    expect(semantics, expectedSemanticsWhenAbsent);
    expect(log, <String>[]);
102
    await tester.tap(find.byType(Visibility), warnIfMissed: false);
103 104 105
    expect(log, <String>[]);
    log.clear();

106 107 108
    await tester.pumpWidget(Center(
      child: Visibility(
        visible: false,
109
        child: testChild,
110
      ),
111
    ));
112 113 114 115 116 117
    expect(find.byType(Text, skipOffstage: false), findsNothing);
    expect(find.byType(Placeholder), findsNothing);
    expect(find.byType(Visibility), paintsNothing);
    expect(tester.getSize(find.byType(Visibility)), Size.zero);
    expect(semantics, expectedSemanticsWhenAbsent);
    expect(log, <String>[]);
118
    await tester.tap(find.byType(Visibility), warnIfMissed: false);
119 120 121
    expect(log, <String>[]);
    log.clear();

122 123 124 125
    await tester.pumpWidget(Center(
      child: Visibility(
        replacement: const Placeholder(),
        visible: false,
126
        child: testChild,
127
      ),
128
    ));
129 130 131 132 133 134
    expect(find.byType(Text, skipOffstage: false), findsNothing);
    expect(find.byType(Placeholder), findsOneWidget);
    expect(find.byType(Visibility), paints..path());
    expect(tester.getSize(find.byType(Visibility)), const Size(800.0, 600.0));
    expect(semantics, expectedSemanticsWhenAbsent);
    expect(log, <String>[]);
135
    await tester.tap(find.byType(Visibility), warnIfMissed: false);
136 137 138
    expect(log, <String>[]);
    log.clear();

139 140 141
    await tester.pumpWidget(Center(
      child: Visibility(
        replacement: const Placeholder(),
142
        child: testChild,
143
      ),
144
    ));
145 146 147 148 149 150 151 152 153 154 155
    expect(find.byType(Text, skipOffstage: false), findsOneWidget);
    expect(find.text('a true', skipOffstage: false), findsOneWidget);
    expect(find.byType(Placeholder), findsNothing);
    expect(find.byType(Visibility), paints..paragraph());
    expect(tester.getSize(find.byType(Visibility)), const Size(84.0, 14.0));
    expect(semantics, expectedSemanticsWhenPresent);
    expect(log, <String>['created new state']);
    await tester.tap(find.byType(Visibility));
    expect(log, <String>['created new state', 'tap']);
    log.clear();

156 157 158 159 160 161 162
    await tester.pumpWidget(Center(
      child: Visibility(
        maintainState: true,
        maintainAnimation: true,
        maintainSize: true,
        maintainInteractivity: true,
        maintainSemantics: true,
163
        child: testChild,
164
      ),
165
    ));
166 167 168 169 170 171 172 173 174 175 176
    expect(find.byType(Text, skipOffstage: false), findsOneWidget);
    expect(find.text('a true', skipOffstage: false), findsOneWidget);
    expect(find.byType(Placeholder), findsNothing);
    expect(find.byType(Visibility), paints..paragraph());
    expect(tester.getSize(find.byType(Visibility)), const Size(84.0, 14.0));
    expect(semantics, expectedSemanticsWhenPresent);
    expect(log, <String>['created new state']);
    await tester.tap(find.byType(Visibility));
    expect(log, <String>['created new state', 'tap']);
    log.clear();

177 178 179 180 181 182 183 184
    await tester.pumpWidget(Center(
      child: Visibility(
        visible: false,
        maintainState: true,
        maintainAnimation: true,
        maintainSize: true,
        maintainInteractivity: true,
        maintainSemantics: true,
185
        child: testChild,
186
      ),
187
    ));
188 189 190 191 192 193 194 195 196 197 198
    expect(find.byType(Text, skipOffstage: false), findsOneWidget);
    expect(find.text('a true', skipOffstage: false), findsOneWidget);
    expect(find.byType(Placeholder), findsNothing);
    expect(find.byType(Visibility), paintsNothing);
    expect(tester.getSize(find.byType(Visibility)), const Size(84.0, 14.0));
    expect(semantics, expectedSemanticsWhenPresent);
    expect(log, <String>[]);
    await tester.tap(find.byType(Visibility));
    expect(log, <String>['tap']);
    log.clear();

199 200 201 202 203 204 205
    await tester.pumpWidget(Center(
      child: Visibility(
        visible: false,
        maintainState: true,
        maintainAnimation: true,
        maintainSize: true,
        maintainInteractivity: true,
206
        child: testChild,
207
      ),
208
    ));
209 210 211 212 213 214 215 216 217 218 219
    expect(find.byType(Text, skipOffstage: false), findsOneWidget);
    expect(find.text('a true', skipOffstage: false), findsOneWidget);
    expect(find.byType(Placeholder), findsNothing);
    expect(find.byType(Visibility), paintsNothing);
    expect(tester.getSize(find.byType(Visibility)), const Size(84.0, 14.0));
    expect(semantics, expectedSemanticsWhenAbsent);
    expect(log, <String>[]);
    await tester.tap(find.byType(Visibility));
    expect(log, <String>['tap']);
    log.clear();

220 221 222 223 224 225 226
    await tester.pumpWidget(Center(
      child: Visibility(
        visible: false,
        maintainState: true,
        maintainAnimation: true,
        maintainSize: true,
        maintainSemantics: true,
227
        child: testChild,
228
      ),
229
    ));
230 231 232 233 234
    expect(find.byType(Text, skipOffstage: false), findsOneWidget);
    expect(find.text('a true', skipOffstage: false), findsOneWidget);
    expect(find.byType(Placeholder), findsNothing);
    expect(find.byType(Visibility), paintsNothing);
    expect(tester.getSize(find.byType(Visibility)), const Size(84.0, 14.0));
235 236
    expect(semantics, expectedSemanticsWhenPresentWithIgnorePointer);
    expect(log, <String>[]);
237
    await tester.tap(find.byType(Visibility), warnIfMissed: false);
238
    expect(log, <String>[]);
239 240
    log.clear();

241 242 243 244 245 246
    await tester.pumpWidget(Center(
      child: Visibility(
        visible: false,
        maintainState: true,
        maintainAnimation: true,
        maintainSize: true,
247
        child: testChild,
248
      ),
249
    ));
250 251 252 253 254 255 256
    expect(find.byType(Text, skipOffstage: false), findsOneWidget);
    expect(find.text('a true', skipOffstage: false), findsOneWidget);
    expect(find.byType(Placeholder), findsNothing);
    expect(find.byType(Visibility), paintsNothing);
    expect(tester.getSize(find.byType(Visibility)), const Size(84.0, 14.0));
    expect(semantics, expectedSemanticsWhenAbsent);
    expect(log, <String>[]);
257
    await tester.tap(find.byType(Visibility), warnIfMissed: false);
258 259 260
    expect(log, <String>[]);
    log.clear();

261 262 263 264 265
    await tester.pumpWidget(Center(
      child: Visibility(
        visible: false,
        maintainState: true,
        maintainAnimation: true,
266
        child: testChild,
267
      ),
268
    ));
269
    expect(find.byType(Text, skipOffstage: false), findsOneWidget);
270
    expect(find.byType(Text), findsNothing);
271 272 273 274 275 276
    expect(find.text('a true', skipOffstage: false), findsOneWidget);
    expect(find.byType(Placeholder), findsNothing);
    expect(find.byType(Visibility), paintsNothing);
    expect(tester.getSize(find.byType(Visibility)), Size.zero);
    expect(semantics, expectedSemanticsWhenAbsent);
    expect(log, <String>['created new state']);
277
    await tester.tap(find.byType(Visibility), warnIfMissed: false);
278 279 280
    expect(log, <String>['created new state']);
    log.clear();

281 282 283 284
    await tester.pumpWidget(Center(
      child: Visibility(
        visible: false,
        maintainState: true,
285
        child: testChild,
286
      ),
287
    ));
288
    expect(find.byType(Text, skipOffstage: false), findsOneWidget);
289
    expect(find.byType(Text), findsNothing);
290 291 292 293 294 295
    expect(find.text('a false', skipOffstage: false), findsOneWidget);
    expect(find.byType(Placeholder), findsNothing);
    expect(find.byType(Visibility), paintsNothing);
    expect(tester.getSize(find.byType(Visibility)), Size.zero);
    expect(semantics, expectedSemanticsWhenAbsent);
    expect(log, <String>['created new state']);
296
    await tester.tap(find.byType(Visibility), warnIfMissed: false);
297 298 299 300 301
    expect(log, <String>['created new state']);
    log.clear();

    // Now we toggle the visibility off and on a few times to make sure that works.

302 303 304
    await tester.pumpWidget(Center(
      child: Visibility(
        maintainState: true,
305
        child: testChild,
306
      ),
307
    ));
308 309 310 311 312 313 314
    expect(find.byType(Text), findsOneWidget);
    expect(find.text('a true', skipOffstage: false), findsOneWidget);
    expect(find.byType(Placeholder), findsNothing);
    expect(find.byType(Visibility), paints..paragraph());
    expect(tester.getSize(find.byType(Visibility)), const Size(84.0, 14.0));
    expect(semantics, expectedSemanticsWhenPresent);
    expect(log, <String>[]);
315
    await tester.tap(find.byType(Visibility), warnIfMissed: false);
316 317 318
    expect(log, <String>['tap']);
    log.clear();

319 320 321 322
    await tester.pumpWidget(Center(
      child: Visibility(
        visible: false,
        maintainState: true,
323
        child: testChild,
324
      ),
325
    ));
326
    expect(find.byType(Text, skipOffstage: false), findsOneWidget);
327
    expect(find.byType(Text), findsNothing);
328 329 330 331 332 333
    expect(find.text('a false', skipOffstage: false), findsOneWidget);
    expect(find.byType(Placeholder), findsNothing);
    expect(find.byType(Visibility), paintsNothing);
    expect(tester.getSize(find.byType(Visibility)), Size.zero);
    expect(semantics, expectedSemanticsWhenAbsent);
    expect(log, <String>[]);
334
    await tester.tap(find.byType(Visibility), warnIfMissed: false);
335 336 337
    expect(log, <String>[]);
    log.clear();

338 339 340
    await tester.pumpWidget(Center(
      child: Visibility(
        maintainState: true,
341
        child: testChild,
342
      ),
343
    ));
344 345 346 347 348 349 350 351 352 353 354
    expect(find.byType(Text), findsOneWidget);
    expect(find.text('a true', skipOffstage: false), findsOneWidget);
    expect(find.byType(Placeholder), findsNothing);
    expect(find.byType(Visibility), paints..paragraph());
    expect(tester.getSize(find.byType(Visibility)), const Size(84.0, 14.0));
    expect(semantics, expectedSemanticsWhenPresent);
    expect(log, <String>[]);
    await tester.tap(find.byType(Visibility));
    expect(log, <String>['tap']);
    log.clear();

355 356 357 358
    await tester.pumpWidget(Center(
      child: Visibility(
        visible: false,
        maintainState: true,
359
        child: testChild,
360
      ),
361
    ));
362
    expect(find.byType(Text, skipOffstage: false), findsOneWidget);
363
    expect(find.byType(Text), findsNothing);
364 365 366 367 368 369
    expect(find.text('a false', skipOffstage: false), findsOneWidget);
    expect(find.byType(Placeholder), findsNothing);
    expect(find.byType(Visibility), paintsNothing);
    expect(tester.getSize(find.byType(Visibility)), Size.zero);
    expect(semantics, expectedSemanticsWhenAbsent);
    expect(log, <String>[]);
370
    await tester.tap(find.byType(Visibility), warnIfMissed: false);
371 372 373 374 375
    expect(log, <String>[]);
    log.clear();

    // Same but without maintainState.

376 377 378
    await tester.pumpWidget(Center(
      child: Visibility(
        visible: false,
379
        child: testChild,
380
      ),
381
    ));
382 383 384 385 386 387
    expect(find.byType(Text, skipOffstage: false), findsNothing);
    expect(find.byType(Placeholder), findsNothing);
    expect(find.byType(Visibility), paintsNothing);
    expect(tester.getSize(find.byType(Visibility)), Size.zero);
    expect(semantics, expectedSemanticsWhenAbsent);
    expect(log, <String>[]);
388
    await tester.tap(find.byType(Visibility), warnIfMissed: false);
389 390 391
    expect(log, <String>[]);
    log.clear();

392 393
    await tester.pumpWidget(Center(
      child: Visibility(
394
        child: testChild,
395
      ),
396
    ));
397 398 399 400 401 402 403 404 405 406 407
    expect(find.byType(Text), findsOneWidget);
    expect(find.text('a true', skipOffstage: false), findsOneWidget);
    expect(find.byType(Placeholder), findsNothing);
    expect(find.byType(Visibility), paints..paragraph());
    expect(tester.getSize(find.byType(Visibility)), const Size(84.0, 14.0));
    expect(semantics, expectedSemanticsWhenPresent);
    expect(log, <String>['created new state']);
    await tester.tap(find.byType(Visibility));
    expect(log, <String>['created new state', 'tap']);
    log.clear();

408 409 410
    await tester.pumpWidget(Center(
      child: Visibility(
        visible: false,
411
        child: testChild,
412
      ),
413
    ));
414 415 416 417 418 419
    expect(find.byType(Text, skipOffstage: false), findsNothing);
    expect(find.byType(Placeholder), findsNothing);
    expect(find.byType(Visibility), paintsNothing);
    expect(tester.getSize(find.byType(Visibility)), Size.zero);
    expect(semantics, expectedSemanticsWhenAbsent);
    expect(log, <String>[]);
420
    await tester.tap(find.byType(Visibility), warnIfMissed: false);
421 422 423
    expect(log, <String>[]);
    log.clear();

424 425
    await tester.pumpWidget(Center(
      child: Visibility(
426
        child: testChild,
427
      ),
428
    ));
429 430 431 432 433 434 435 436 437 438 439 440
    expect(find.byType(Text), findsOneWidget);
    expect(find.text('a true', skipOffstage: false), findsOneWidget);
    expect(find.byType(Placeholder), findsNothing);
    expect(find.byType(Visibility), paints..paragraph());
    expect(tester.getSize(find.byType(Visibility)), const Size(84.0, 14.0));
    expect(semantics, expectedSemanticsWhenPresent);
    expect(log, <String>['created new state']);
    await tester.tap(find.byType(Visibility));
    expect(log, <String>['created new state', 'tap']);
    log.clear();

    semantics.dispose();
441
  });
442

443
  testWidgetsWithLeakTracking('Visibility does not force compositing when visible and maintain*', (WidgetTester tester) async {
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
    await tester.pumpWidget(
      const Visibility(
        maintainSize: true,
        maintainAnimation: true,
        maintainState: true,
        child: Text('hello', textDirection: TextDirection.ltr),
      ),
    );

    // Root transform from the tester and then the picture created by the text.
    expect(tester.layers, hasLength(2));
    expect(tester.layers, isNot(contains(isA<OpacityLayer>())));
    expect(tester.layers.last, isA<PictureLayer>());
  });

459
  testWidgetsWithLeakTracking('SliverVisibility does not force compositing when visible and maintain*', (WidgetTester tester) async {
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.ltr,
        child: CustomScrollView(
          slivers: <Widget>[
            SliverVisibility(
              maintainSize: true,
              maintainAnimation: true,
              maintainState: true,
              sliver: SliverList(
              delegate: SliverChildListDelegate.fixed(
                addRepaintBoundaries: false,
                <Widget>[
                  Text('hello'),
                ],
              ),
            ))
          ]
        ),
      ),
    );

    // This requires a lot more layers due to including sliver lists which do manage additional
    // offset layers. Just trust me this is one fewer layers than before...
    expect(tester.layers, hasLength(6));
    expect(tester.layers, isNot(contains(isA<OpacityLayer>())));
    expect(tester.layers.last, isA<PictureLayer>());
  });
488

489
  testWidgetsWithLeakTracking('Visibility.of returns correct value', (WidgetTester tester) async {
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.ltr,
        child: _ShowVisibility(),
      ),
    );
    expect(find.text('is visible ? true', skipOffstage: false), findsOneWidget);

    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.ltr,
        child: Visibility(
          maintainState: true,
          child: _ShowVisibility(),
        ),
      ),
    );
    expect(find.text('is visible ? true', skipOffstage: false), findsOneWidget);

    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.ltr,
        child: Visibility(
          visible: false,
          maintainState: true,
          child: _ShowVisibility(),
        ),
      ),
    );
    expect(find.text('is visible ? false', skipOffstage: false), findsOneWidget);
  });

522
  testWidgetsWithLeakTracking('Visibility.of works when multiple Visibility widgets are in hierarchy', (WidgetTester tester) async {
523 524 525 526 527 528 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 557 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
    bool didChangeDependencies = false;
    void handleDidChangeDependencies() {
      didChangeDependencies = true;
    }

    Widget newWidget({required bool ancestorIsVisible, required bool descendantIsVisible}) {
      return Directionality(
        textDirection: TextDirection.ltr,
        child: Visibility(
          visible: ancestorIsVisible,
          maintainState: true,
          child: Center(
            child: Visibility(
              visible: descendantIsVisible,
              maintainState: true,
              child: _ShowVisibility(
                onDidChangeDependencies: handleDidChangeDependencies,
              ),
            ),
          ),
        ),
      );
    }

    await tester.pumpWidget(newWidget(ancestorIsVisible: true, descendantIsVisible: true));
    expect(didChangeDependencies, isTrue);
    expect(find.text('is visible ? true', skipOffstage: false), findsOneWidget);
    didChangeDependencies = false;

    await tester.pumpWidget(newWidget(ancestorIsVisible: true, descendantIsVisible: false));
    expect(didChangeDependencies, isTrue);
    expect(find.text('is visible ? false', skipOffstage: false), findsOneWidget);
    didChangeDependencies = false;

    await tester.pumpWidget(newWidget(ancestorIsVisible: true, descendantIsVisible: false));
    expect(didChangeDependencies, isFalse);

    await tester.pumpWidget(newWidget(ancestorIsVisible: false, descendantIsVisible: false));
    expect(didChangeDependencies, isTrue);
    didChangeDependencies = false;

    await tester.pumpWidget(newWidget(ancestorIsVisible: false, descendantIsVisible: true));
    expect(didChangeDependencies, isTrue);
    expect(find.text('is visible ? false', skipOffstage: false), findsOneWidget);
  });
}

class _ShowVisibility extends StatefulWidget {
  const _ShowVisibility({this.onDidChangeDependencies});

  final VoidCallback? onDidChangeDependencies;

  @override
  State<_ShowVisibility> createState() => _ShowVisibilityState();
}

class _ShowVisibilityState extends State<_ShowVisibility> {
  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    if (widget.onDidChangeDependencies != null) {
      widget.onDidChangeDependencies!();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Text('is visible ? ${Visibility.of(context)}');
  }
592
}