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
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
// @dart = 2.8

7 8 9 10 11 12 13 14 15 16 17
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 {
  const TestState({ Key key, this.child, this.log }) : super(key: key);
  final Widget child;
  final List<String> log;
  @override
18
  State<TestState> createState() => _TestStateState();
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
}

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 {
35
    final SemanticsTester semantics = SemanticsTester(tester);
36 37
    final List<String> log = <String>[];

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

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

66
    final Matcher expectedSemanticsWhenAbsent = hasSemantics(TestSemantics.root());
67 68

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

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

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

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

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

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

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

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

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

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

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

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

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

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

291 292 293 294 295 296 297
    await tester.pumpWidget(Center(
      child: Visibility(
        child: testChild,
        visible: true,
        maintainState: true,
      )
    ));
298 299 300 301 302 303 304 305 306 307 308
    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();

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

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

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

    // Same but without maintainState.

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

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

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

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