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

5 6
import 'dart:ui' show window;

7 8 9 10
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

249
    expect(tester.getTopLeft(find.byKey(childKey)).dy, equals(0.0));
250

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

254
    expect(tester.getTopLeft(find.byKey(childKey)).dy, equals(-480.0));
255

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

259
    expect(tester.getTopLeft(find.byKey(childKey)).dy, equals(-480.0));
260

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

264
    expect(tester.getTopLeft(find.byKey(childKey)).dy, equals(-480.0));
265

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

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

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

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

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

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

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

321 322 323 324 325
    // 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.
326
    await tester.fling(find.byType(Slider), const Offset(-100.0, 0.0), 2000.0, warnIfMissed: false); // hitting the debugger
327
    expect(value, equals(0.70));
328 329 330
  });

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

334
    bool? valueTop = false;
335 336

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

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

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

  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,
391
                    onChanged: (bool? _) { },
392 393 394 395 396 397 398
                  ),
                ),
                Semantics(
                  container: true,
                  key: checkboxUnchecked,
                  child: Checkbox(
                    value: false,
399
                    onChanged: (bool? _) { },
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 458 459
                  ),
                ),
                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,
            ),
          ),
        ),
      ),
    );

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

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

  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,
          ),
        ),
      ),
    );

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

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

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