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

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
9 10 11 12 13 14 15 16 17 18
  testWidgets('SemanticsDebugger will schedule a frame', (WidgetTester tester) async {
    await tester.pumpWidget(
      SemanticsDebugger(
        child: Container(),
      ),
    );

    expect(tester.binding.hasScheduledFrame, isTrue);
  });

19
  testWidgets('SemanticsDebugger smoke test', (WidgetTester tester) async {
Ian Hickson's avatar
Ian Hickson committed
20

21 22
    // This is a smoketest to verify that adding a debugger doesn't crash.
    await tester.pumpWidget(
23
      Directionality(
24
        textDirection: TextDirection.ltr,
25
        child: Stack(
26
          children: <Widget>[
27 28
            Semantics(),
            Semantics(
29
              container: true,
30
            ),
31
            Semantics(
32
              label: 'label',
Ian Hickson's avatar
Ian Hickson committed
33
              textDirection: TextDirection.ltr,
34
            ),
35 36 37
          ],
        ),
      ),
38 39
    );

40
    await tester.pumpWidget(
41
      Directionality(
42
        textDirection: TextDirection.ltr,
43 44
        child: SemanticsDebugger(
          child: Stack(
45
            children: <Widget>[
46 47
              Semantics(),
              Semantics(
48 49
                container: true,
              ),
50
              Semantics(
51 52 53 54 55 56 57 58 59
                label: 'label',
                textDirection: TextDirection.ltr,
              ),
            ],
          ),
        ),
      ),
    );

60 61 62
    expect(true, isTrue); // expect that we reach here without crashing
  });

63
  testWidgets('SemanticsDebugger reparents subtree', (WidgetTester tester) async {
64
    final GlobalKey key = GlobalKey();
65 66

    await tester.pumpWidget(
67
      Directionality(
68
        textDirection: TextDirection.ltr,
69 70
        child: SemanticsDebugger(
          child: Stack(
71
            children: <Widget>[
72 73
              Semantics(label: 'label1', textDirection: TextDirection.ltr),
              Positioned(
74 75 76 77 78
                key: key,
                left: 0.0,
                top: 0.0,
                width: 100.0,
                height: 100.0,
79
                child: Semantics(label: 'label2', textDirection: TextDirection.ltr),
80
              ),
81 82
            ],
          ),
83 84
        ),
      ),
85 86 87
    );

    await tester.pumpWidget(
88
      Directionality(
89
        textDirection: TextDirection.ltr,
90 91
        child: SemanticsDebugger(
          child: Stack(
92
            children: <Widget>[
93 94
              Semantics(label: 'label1', textDirection: TextDirection.ltr),
              Semantics(
95
                container: true,
96
                child: Stack(
97
                  children: <Widget>[
98
                    Positioned(
99 100 101 102 103
                      key: key,
                      left: 0.0,
                      top: 0.0,
                      width: 100.0,
                      height: 100.0,
104
                      child: Semantics(label: 'label2', textDirection: TextDirection.ltr),
105
                    ),
106
                    Semantics(label: 'label3', textDirection: TextDirection.ltr),
107 108
                  ],
                ),
109
              ),
110 111 112 113 114 115 116
            ],
          ),
        ),
      ),
    );

    await tester.pumpWidget(
117
      Directionality(
118
        textDirection: TextDirection.ltr,
119 120
        child: SemanticsDebugger(
          child: Stack(
121
            children: <Widget>[
122 123
              Semantics(label: 'label1', textDirection: TextDirection.ltr),
              Semantics(
124
                container: true,
125
                child: Stack(
126
                  children: <Widget>[
127
                    Positioned(
128 129 130 131 132 133 134
                      key: key,
                      left: 0.0,
                      top: 0.0,
                      width: 100.0,
                      height: 100.0,
                      child: Semantics(label: 'label2', textDirection: TextDirection.ltr),
                    ),
135 136
                    Semantics(label: 'label3', textDirection: TextDirection.ltr),
                    Semantics(label: 'label4', textDirection: TextDirection.ltr),
137 138 139 140 141
                  ],
                ),
              ),
            ],
          ),
142 143
        ),
      ),
144 145 146 147
    );

    expect(tester.takeException(), isNull);
  });
148

149
  testWidgets('SemanticsDebugger interaction test', (WidgetTester tester) async {
150 151 152
    final List<String> log = <String>[];

    await tester.pumpWidget(
153
      Directionality(
154
        textDirection: TextDirection.ltr,
155 156 157
        child: SemanticsDebugger(
          child: Material(
            child: ListView(
158
              children: <Widget>[
159
                ElevatedButton(
160 161 162 163 164
                  onPressed: () {
                    log.add('top');
                  },
                  child: const Text('TOP'),
                ),
165
                ElevatedButton(
166 167 168 169 170 171 172
                  onPressed: () {
                    log.add('bottom');
                  },
                  child: const Text('BOTTOM'),
                ),
              ],
            ),
173
          ),
174 175 176 177
        ),
      ),
    );

178
    await tester.tap(find.text('TOP'), warnIfMissed: false); // hitting the debugger
179 180 181
    expect(log, equals(<String>['top']));
    log.clear();

182
    await tester.tap(find.text('BOTTOM'), warnIfMissed: false); // hitting the debugger
183 184 185 186
    expect(log, equals(<String>['bottom']));
    log.clear();
  });

187
  testWidgets('SemanticsDebugger interaction test - negative', (WidgetTester tester) async {
Ian Hickson's avatar
Ian Hickson committed
188 189 190
    final List<String> log = <String>[];

    await tester.pumpWidget(
191
      Directionality(
Ian Hickson's avatar
Ian Hickson committed
192
        textDirection: TextDirection.ltr,
193 194 195
        child: SemanticsDebugger(
          child: Material(
            child: ListView(
Ian Hickson's avatar
Ian Hickson committed
196
              children: <Widget>[
197
                ElevatedButton(
Ian Hickson's avatar
Ian Hickson committed
198 199 200 201 202
                  onPressed: () {
                    log.add('top');
                  },
                  child: const Text('TOP', textDirection: TextDirection.ltr),
                ),
203
                ExcludeSemantics(
204
                  child: ElevatedButton(
Ian Hickson's avatar
Ian Hickson committed
205 206 207 208 209 210 211 212 213 214 215 216 217
                    onPressed: () {
                      log.add('bottom');
                    },
                    child: const Text('BOTTOM', textDirection: TextDirection.ltr),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );

218
    await tester.tap(find.text('TOP'), warnIfMissed: false); // hitting the debugger
Ian Hickson's avatar
Ian Hickson committed
219 220 221
    expect(log, equals(<String>['top']));
    log.clear();

222
    await tester.tap(find.text('BOTTOM'), warnIfMissed: false); // hitting the debugger
Ian Hickson's avatar
Ian Hickson committed
223 224 225 226
    expect(log, equals(<String>[]));
    log.clear();
  });

227
  testWidgets('SemanticsDebugger scroll test', (WidgetTester tester) async {
228
    final Key childKey = UniqueKey();
229 230

    await tester.pumpWidget(
231
      Directionality(
232
        textDirection: TextDirection.ltr,
233 234
        child: SemanticsDebugger(
          child: ListView(
235
            children: <Widget>[
236
              Container(
237 238 239 240 241 242
                key: childKey,
                height: 5000.0,
                color: Colors.green[500],
              ),
            ],
          ),
243 244 245 246
        ),
      ),
    );

247
    expect(tester.getTopLeft(find.byKey(childKey)).dy, equals(0.0));
248

249
    await tester.fling(find.byType(ListView), const Offset(0.0, -200.0), 200.0, warnIfMissed: false); // hitting the debugger);
250 251
    await tester.pump();

252
    expect(tester.getTopLeft(find.byKey(childKey)).dy, equals(-480.0));
253

254
    await tester.fling(find.byType(ListView), const Offset(200.0, 0.0), 200.0, warnIfMissed: false); // hitting the debugger);
255 256
    await tester.pump();

257
    expect(tester.getTopLeft(find.byKey(childKey)).dy, equals(-480.0));
258

259
    await tester.fling(find.byType(ListView), const Offset(-200.0, 0.0), 200.0, warnIfMissed: false); // hitting the debugger);
260 261
    await tester.pump();

262
    expect(tester.getTopLeft(find.byKey(childKey)).dy, equals(-480.0));
263

264
    await tester.fling(find.byType(ListView), const Offset(0.0, 200.0), 200.0, warnIfMissed: false); // hitting the debugger);
265 266
    await tester.pump();

267
    expect(tester.getTopLeft(find.byKey(childKey)).dy, equals(0.0));
268 269 270 271 272 273
  });

  testWidgets('SemanticsDebugger long press', (WidgetTester tester) async {
    bool didLongPress = false;

    await tester.pumpWidget(
274
      Directionality(
275
        textDirection: TextDirection.ltr,
276 277
        child: SemanticsDebugger(
          child: GestureDetector(
278 279 280 281 282 283
            onLongPress: () {
              expect(didLongPress, isFalse);
              didLongPress = true;
            },
            child: const Text('target', textDirection: TextDirection.ltr),
          ),
284 285 286 287
        ),
      ),
    );

288
    await tester.longPress(find.text('target'), warnIfMissed: false); // hitting the debugger
289 290 291 292 293 294 295
    expect(didLongPress, isTrue);
  });

  testWidgets('SemanticsDebugger slider', (WidgetTester tester) async {
    double value = 0.75;

    await tester.pumpWidget(
296
      Directionality(
297
        textDirection: TextDirection.ltr,
298 299
        child: SemanticsDebugger(
          child: Directionality(
300
            textDirection: TextDirection.ltr,
301
            child: MediaQuery(
302
              data: MediaQueryData.fromWindow(WidgetsBinding.instance.window),
303 304 305
              child: Material(
                child: Center(
                  child: Slider(
306 307 308 309 310
                    value: value,
                    onChanged: (double newValue) {
                      value = newValue;
                    },
                  ),
311
                ),
312
              ),
313 314 315 316 317 318
            ),
          ),
        ),
      ),
    );

319 320 321 322 323
    // The fling below must be such that the velocity estimation examines an
    // offset greater than the kTouchSlop. Too slow or too short a distance, and
    // it won't trigger. The actual distance moved doesn't matter since this is
    // interpreted as a gesture by the semantics debugger and sent to the widget
    // as a semantic action that always moves by 10% of the complete track.
324
    await tester.fling(find.byType(Slider), const Offset(-100.0, 0.0), 2000.0, warnIfMissed: false); // hitting the debugger
325
    expect(value, equals(0.70));
326 327 328
  });

  testWidgets('SemanticsDebugger checkbox', (WidgetTester tester) async {
329 330
    final Key keyTop = UniqueKey();
    final Key keyBottom = UniqueKey();
331

332
    bool? valueTop = false;
333 334

    await tester.pumpWidget(
335
      Directionality(
336
        textDirection: TextDirection.ltr,
337 338 339
        child: SemanticsDebugger(
          child: Material(
            child: ListView(
340
              children: <Widget>[
341
                Checkbox(
342 343
                  key: keyTop,
                  value: valueTop,
344
                  onChanged: (bool? newValue) {
345 346 347
                    valueTop = newValue;
                  },
                ),
348
                Checkbox(
349
                  key: keyBottom,
350
                  value: false,
351 352 353 354
                  onChanged: null,
                ),
              ],
            ),
355 356 357 358 359
          ),
        ),
      ),
    );

360
    await tester.tap(find.byKey(keyTop), warnIfMissed: false); // hitting the debugger
361 362 363 364
    expect(valueTop, isTrue);
    valueTop = false;
    expect(valueTop, isFalse);

365
    await tester.tap(find.byKey(keyBottom), warnIfMissed: false); // hitting the debugger
366 367
    expect(valueTop, isFalse);
  });
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388

  testWidgets('SemanticsDebugger checkbox message', (WidgetTester tester) async {
    final Key checkbox = UniqueKey();
    final Key checkboxUnchecked = UniqueKey();
    final Key checkboxDisabled = UniqueKey();
    final Key checkboxDisabledUnchecked = UniqueKey();
    final Key debugger = UniqueKey();

    await tester.pumpWidget(
      Directionality(
        textDirection: TextDirection.ltr,
        child: SemanticsDebugger(
          key: debugger,
          child: Material(
            child: ListView(
              children: <Widget>[
                Semantics(
                  container: true,
                  key: checkbox,
                  child: Checkbox(
                    value: true,
389
                    onChanged: (bool? _) { },
390 391 392 393 394 395 396
                  ),
                ),
                Semantics(
                  container: true,
                  key: checkboxUnchecked,
                  child: Checkbox(
                    value: false,
397
                    onChanged: (bool? _) { },
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
                  ),
                ),
                Semantics(
                  container: true,
                  key: checkboxDisabled,
                  child: const Checkbox(
                    value: true,
                    onChanged: null,
                  ),
                ),
                Semantics(
                  container: true,
                  key: checkboxDisabledUnchecked,
                  child: const Checkbox(
                    value: false,
                    onChanged: null,
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );

    expect(
      _getMessageShownInSemanticsDebugger(widgetKey: checkbox, debuggerKey: debugger, tester: tester),
      'checked',
    );
    expect(
      _getMessageShownInSemanticsDebugger(widgetKey: checkboxUnchecked, debuggerKey: debugger, tester: tester),
      'unchecked',
    );
    expect(
      _getMessageShownInSemanticsDebugger(widgetKey: checkboxDisabled, debuggerKey: debugger, tester: tester),
      'checked; disabled',
    );
    expect(
      _getMessageShownInSemanticsDebugger(widgetKey: checkboxDisabledUnchecked, debuggerKey: debugger, tester: tester),
      'unchecked; disabled',
    );
  });

  testWidgets('SemanticsDebugger textfield', (WidgetTester tester) async {
    final UniqueKey textField = UniqueKey();
    final UniqueKey debugger = UniqueKey();

    await tester.pumpWidget(
      MaterialApp(
        home: SemanticsDebugger(
          key: debugger,
          child: Material(
            child: TextField(
              key: textField,
            ),
          ),
        ),
      ),
    );

458 459 460
    final dynamic semanticsDebuggerPainter = _getSemanticsDebuggerPainter(debuggerKey: debugger, tester: tester);
    final RenderObject renderTextfield = tester.renderObject(find.descendant(of: find.byKey(textField), matching: find.byType(Semantics)).first);

461
    expect(
462
      // ignore: avoid_dynamic_calls
463
      semanticsDebuggerPainter.getMessage(renderTextfield.debugSemantics),
464 465 466
      'textfield',
    );
  });
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484

  testWidgets('SemanticsDebugger label style is used in the painter.', (WidgetTester tester) async {
    final UniqueKey debugger = UniqueKey();
    const TextStyle labelStyle = TextStyle(color: Colors.amber);
    await tester.pumpWidget(
      Directionality(
        textDirection: TextDirection.ltr,
        child: SemanticsDebugger(
          key: debugger,
          labelStyle: labelStyle,
          child: Semantics(
            label: 'label',
            textDirection: TextDirection.ltr,
          ),
        ),
      ),
    );

485
    // ignore: avoid_dynamic_calls
486 487
    expect(_getSemanticsDebuggerPainter(debuggerKey: debugger, tester: tester).labelStyle, labelStyle);
  });
488 489 490
}

String _getMessageShownInSemanticsDebugger({
491 492 493
  required Key widgetKey,
  required Key debuggerKey,
  required WidgetTester tester,
494 495
}) {
  final dynamic semanticsDebuggerPainter = _getSemanticsDebuggerPainter(debuggerKey: debuggerKey, tester: tester);
496
  // ignore: avoid_dynamic_calls
497
  return semanticsDebuggerPainter.getMessage(tester.renderObject(find.byKey(widgetKey)).debugSemantics) as String;
498 499 500
}

dynamic _getSemanticsDebuggerPainter({
501 502
  required Key debuggerKey,
  required WidgetTester tester,
503 504 505 506
}) {
  final CustomPaint customPaint = tester.widgetList(find.descendant(
    of: find.byKey(debuggerKey),
    matching: find.byType(CustomPaint),
507
  )).first as CustomPaint;
508 509
  final dynamic semanticsDebuggerPainter = customPaint.foregroundPainter;
  expect(semanticsDebuggerPainter.runtimeType.toString(), '_SemanticsDebuggerPainter');
510
  return semanticsDebuggerPainter;
511
}