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({ 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(child: testChild, visible: false));
82 83 84 85 86 87 88 89 90
    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();

91 92 93 94 95 96
    await tester.pumpWidget(Center(
      child: Visibility(
        child: testChild,
        visible: false,
      )
    ));
97 98 99 100 101 102 103 104 105 106
    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();

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

124 125 126 127 128 129 130
    await tester.pumpWidget(Center(
      child: Visibility(
        child: testChild,
        replacement: const Placeholder(),
        visible: true,
      )
    ));
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 150 151 152
    await tester.pumpWidget(Center(
      child: Visibility(
        child: testChild,
        visible: true,
        maintainState: true,
        maintainAnimation: true,
        maintainSize: true,
        maintainInteractivity: true,
        maintainSemantics: true,
      )
    ));
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 172 173 174
    await tester.pumpWidget(Center(
      child: Visibility(
        child: testChild,
        visible: false,
        maintainState: true,
        maintainAnimation: true,
        maintainSize: true,
        maintainInteractivity: true,
        maintainSemantics: true,
      )
    ));
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 193 194 195
    await tester.pumpWidget(Center(
      child: Visibility(
        child: testChild,
        visible: false,
        maintainState: true,
        maintainAnimation: true,
        maintainSize: true,
        maintainInteractivity: true,
      )
    ));
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 214 215 216
    await tester.pumpWidget(Center(
      child: Visibility(
        child: testChild,
        visible: false,
        maintainState: true,
        maintainAnimation: true,
        maintainSize: true,
        maintainSemantics: true,
      )
    ));
217 218 219 220 221 222 223 224 225 226 227
    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();

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

248 249 250 251 252 253 254 255
    await tester.pumpWidget(Center(
      child: Visibility(
        child: testChild,
        visible: false,
        maintainState: true,
        maintainAnimation: true,
      )
    ));
256 257 258 259 260 261 262 263 264 265 266 267
    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();

268 269 270 271 272 273 274
    await tester.pumpWidget(Center(
      child: Visibility(
        child: testChild,
        visible: false,
        maintainState: true,
      )
    ));
275 276 277 278 279 280 281 282 283 284 285 286 287 288
    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.

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

307 308 309 310 311 312 313
    await tester.pumpWidget(Center(
      child: Visibility(
        child: testChild,
        visible: false,
        maintainState: true,
      )
    ));
314 315 316 317 318 319 320 321 322 323 324 325
    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();

326 327 328 329 330 331 332
    await tester.pumpWidget(Center(
      child: Visibility(
        child: testChild,
        visible: true,
        maintainState: true,
      )
    ));
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 348 349 350
    await tester.pumpWidget(Center(
      child: Visibility(
        child: testChild,
        visible: false,
        maintainState: true,
      )
    ));
351 352 353 354 355 356 357 358 359 360 361 362 363 364
    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.

365 366 367 368 369 370
    await tester.pumpWidget(Center(
      child: Visibility(
        child: testChild,
        visible: false,
      )
    ));
371 372 373 374 375 376 377 378 379 380
    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();

381 382 383 384 385 386
    await tester.pumpWidget(Center(
      child: Visibility(
        child: testChild,
        visible: true,
      )
    ));
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 401 402 403
    await tester.pumpWidget(Center(
      child: Visibility(
        child: testChild,
        visible: false,
      )
    ));
404 405 406 407 408 409 410 411 412 413
    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();

414 415 416 417 418 419
    await tester.pumpWidget(Center(
      child: Visibility(
        child: testChild,
        visible: true,
      )
    ));
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
}