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

import '../rendering/mock_canvas.dart';
import 'semantics_tester.dart';

class TestState extends StatefulWidget {
12
  const TestState({ Key? key, required this.child, required this.log }) : super(key: key);
13 14 15
  final Widget child;
  final List<String> log;
  @override
16
  State<TestState> createState() => _TestStateState();
17 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() {
  testWidgets('Visibility', (WidgetTester tester) async {
33
    final SemanticsTester semantics = SemanticsTester(tester);
34 35
    final List<String> log = <String>[];

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

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

64
    final Matcher expectedSemanticsWhenAbsent = hasSemantics(TestSemantics.root());
65 66

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

70
    await tester.pumpWidget(Visibility(child: testChild));
71 72 73 74 75 76 77 78 79 80
    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();

81
    await tester.pumpWidget(Visibility(visible: false, child: testChild));
82 83 84 85 86
    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>[]);
87
    await tester.tap(find.byType(Visibility), warnIfMissed: false);
88 89 90
    expect(log, <String>[]);
    log.clear();

91 92 93
    await tester.pumpWidget(Center(
      child: Visibility(
        visible: false,
94
        child: testChild,
95
      ),
96
    ));
97 98 99 100 101 102
    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>[]);
103
    await tester.tap(find.byType(Visibility), warnIfMissed: false);
104 105 106
    expect(log, <String>[]);
    log.clear();

107 108 109 110 111
    await tester.pumpWidget(Center(
      child: Visibility(
        child: testChild,
        replacement: const Placeholder(),
        visible: false,
112
      ),
113
    ));
114 115 116 117 118 119
    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>[]);
120
    await tester.tap(find.byType(Visibility), warnIfMissed: false);
121 122 123
    expect(log, <String>[]);
    log.clear();

124 125 126 127 128
    await tester.pumpWidget(Center(
      child: Visibility(
        child: testChild,
        replacement: const Placeholder(),
        visible: true,
129
      ),
130
    ));
131 132 133 134 135 136 137 138 139 140 141
    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();

142 143 144 145 146 147 148 149
    await tester.pumpWidget(Center(
      child: Visibility(
        visible: true,
        maintainState: true,
        maintainAnimation: true,
        maintainSize: true,
        maintainInteractivity: true,
        maintainSemantics: true,
150
        child: testChild,
151
      ),
152
    ));
153 154 155 156 157 158 159 160 161 162 163
    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();

164 165 166 167 168 169 170 171
    await tester.pumpWidget(Center(
      child: Visibility(
        visible: false,
        maintainState: true,
        maintainAnimation: true,
        maintainSize: true,
        maintainInteractivity: true,
        maintainSemantics: true,
172
        child: testChild,
173
      ),
174
    ));
175 176 177 178 179 180 181 182 183 184 185
    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();

186 187 188 189 190 191 192
    await tester.pumpWidget(Center(
      child: Visibility(
        visible: false,
        maintainState: true,
        maintainAnimation: true,
        maintainSize: true,
        maintainInteractivity: true,
193
        child: testChild,
194
      ),
195
    ));
196 197 198 199 200 201 202 203 204 205 206
    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();

207 208 209 210 211 212 213
    await tester.pumpWidget(Center(
      child: Visibility(
        visible: false,
        maintainState: true,
        maintainAnimation: true,
        maintainSize: true,
        maintainSemantics: true,
214
        child: testChild,
215
      ),
216
    ));
217 218 219 220 221 222 223
    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>['created new state']);
224
    await tester.tap(find.byType(Visibility), warnIfMissed: false);
225 226 227
    expect(log, <String>['created new state']);
    log.clear();

228 229 230 231 232 233
    await tester.pumpWidget(Center(
      child: Visibility(
        visible: false,
        maintainState: true,
        maintainAnimation: true,
        maintainSize: true,
234
        child: testChild,
235
      ),
236
    ));
237 238 239 240 241 242 243
    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>[]);
244
    await tester.tap(find.byType(Visibility), warnIfMissed: false);
245 246 247
    expect(log, <String>[]);
    log.clear();

248 249 250 251 252
    await tester.pumpWidget(Center(
      child: Visibility(
        visible: false,
        maintainState: true,
        maintainAnimation: true,
253
        child: testChild,
254
      ),
255
    ));
256 257 258 259 260 261 262 263
    expect(find.byType(Text, skipOffstage: false), findsOneWidget);
    expect(find.byType(Text, skipOffstage: true), findsNothing);
    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']);
264
    await tester.tap(find.byType(Visibility), warnIfMissed: false);
265 266 267
    expect(log, <String>['created new state']);
    log.clear();

268 269 270 271
    await tester.pumpWidget(Center(
      child: Visibility(
        visible: false,
        maintainState: true,
272
        child: testChild,
273
      ),
274
    ));
275 276 277 278 279 280 281 282
    expect(find.byType(Text, skipOffstage: false), findsOneWidget);
    expect(find.byType(Text, skipOffstage: true), findsNothing);
    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']);
283
    await tester.tap(find.byType(Visibility), warnIfMissed: false);
284 285 286 287 288
    expect(log, <String>['created new state']);
    log.clear();

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

289 290 291 292
    await tester.pumpWidget(Center(
      child: Visibility(
        visible: true,
        maintainState: true,
293
        child: testChild,
294
      ),
295
    ));
296 297 298 299 300 301 302
    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>[]);
303
    await tester.tap(find.byType(Visibility), warnIfMissed: false);
304 305 306
    expect(log, <String>['tap']);
    log.clear();

307 308 309 310
    await tester.pumpWidget(Center(
      child: Visibility(
        visible: false,
        maintainState: true,
311
        child: testChild,
312
      ),
313
    ));
314 315 316 317 318 319 320 321
    expect(find.byType(Text, skipOffstage: false), findsOneWidget);
    expect(find.byType(Text, skipOffstage: true), findsNothing);
    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>[]);
322
    await tester.tap(find.byType(Visibility), warnIfMissed: false);
323 324 325
    expect(log, <String>[]);
    log.clear();

326 327 328 329
    await tester.pumpWidget(Center(
      child: Visibility(
        visible: true,
        maintainState: true,
330
        child: testChild,
331
      ),
332
    ));
333 334 335 336 337 338 339 340 341 342 343
    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();

344 345 346 347
    await tester.pumpWidget(Center(
      child: Visibility(
        visible: false,
        maintainState: true,
348
        child: testChild,
349
      ),
350
    ));
351 352 353 354 355 356 357 358
    expect(find.byType(Text, skipOffstage: false), findsOneWidget);
    expect(find.byType(Text, skipOffstage: true), findsNothing);
    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>[]);
359
    await tester.tap(find.byType(Visibility), warnIfMissed: false);
360 361 362 363 364
    expect(log, <String>[]);
    log.clear();

    // Same but without maintainState.

365 366 367
    await tester.pumpWidget(Center(
      child: Visibility(
        visible: false,
368
        child: testChild,
369
      ),
370
    ));
371 372 373 374 375 376
    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>[]);
377
    await tester.tap(find.byType(Visibility), warnIfMissed: false);
378 379 380
    expect(log, <String>[]);
    log.clear();

381 382 383
    await tester.pumpWidget(Center(
      child: Visibility(
        visible: true,
384
        child: testChild,
385
      ),
386
    ));
387 388 389 390 391 392 393 394 395 396 397
    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();

398 399 400
    await tester.pumpWidget(Center(
      child: Visibility(
        visible: false,
401
        child: testChild,
402
      ),
403
    ));
404 405 406 407 408 409
    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>[]);
410
    await tester.tap(find.byType(Visibility), warnIfMissed: false);
411 412 413
    expect(log, <String>[]);
    log.clear();

414 415 416
    await tester.pumpWidget(Center(
      child: Visibility(
        visible: true,
417
        child: testChild,
418
      ),
419
    ));
420 421 422 423 424 425 426 427 428 429 430 431
    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();
432
  });
433
}