visibility_test.dart 15.2 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({ super.key, required this.child, required this.log });
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
    await tester.pumpWidget(Center(
      child: Visibility(
        replacement: const Placeholder(),
        visible: false,
111
        child: testChild,
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
    await tester.pumpWidget(Center(
      child: Visibility(
        replacement: const Placeholder(),
127
        child: testChild,
128
      ),
129
    ));
130 131 132 133 134 135 136 137 138 139 140
    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();

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

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

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

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

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

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

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

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

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

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

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

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

    // Same but without maintainState.

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

377 378
    await tester.pumpWidget(Center(
      child: Visibility(
379
        child: testChild,
380
      ),
381
    ));
382 383 384 385 386 387 388 389 390 391 392
    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();

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

409 410
    await tester.pumpWidget(Center(
      child: Visibility(
411
        child: testChild,
412
      ),
413
    ));
414 415 416 417 418 419 420 421 422 423 424 425
    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();
426
  });
427
}