semantics_debugger_test.dart 15 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
import 'dart:ui' show window;

9 10
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
Ian Hickson's avatar
Ian Hickson committed
11
import 'package:flutter/rendering.dart';
12 13 14
import 'package:flutter_test/flutter_test.dart';

void main() {
15 16 17 18 19 20 21 22 23 24
  testWidgets('SemanticsDebugger will schedule a frame', (WidgetTester tester) async {
    await tester.pumpWidget(
      SemanticsDebugger(
        child: Container(),
      ),
    );

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

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

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

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

66 67 68
    expect(true, isTrue); // expect that we reach here without crashing
  });

69
  testWidgets('SemanticsDebugger reparents subtree', (WidgetTester tester) async {
70
    final GlobalKey key = GlobalKey();
71 72

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

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

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

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

154
  testWidgets('SemanticsDebugger interaction test', (WidgetTester tester) async {
155 156 157
    final List<String> log = <String>[];

    await tester.pumpWidget(
158
      Directionality(
159
        textDirection: TextDirection.ltr,
160 161 162
        child: SemanticsDebugger(
          child: Material(
            child: ListView(
163
              children: <Widget>[
164
                ElevatedButton(
165 166 167 168 169
                  onPressed: () {
                    log.add('top');
                  },
                  child: const Text('TOP'),
                ),
170
                ElevatedButton(
171 172 173 174 175 176 177
                  onPressed: () {
                    log.add('bottom');
                  },
                  child: const Text('BOTTOM'),
                ),
              ],
            ),
178
          ),
179 180 181 182 183 184 185 186 187 188 189 190 191
        ),
      ),
    );

    await tester.tap(find.text('TOP'));
    expect(log, equals(<String>['top']));
    log.clear();

    await tester.tap(find.text('BOTTOM'));
    expect(log, equals(<String>['bottom']));
    log.clear();
  });

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

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

    await tester.tap(find.text('TOP'));
    expect(log, equals(<String>['top']));
    log.clear();

    await tester.tap(find.text('BOTTOM'));
    expect(log, equals(<String>[]));
    log.clear();
  });

232
  testWidgets('SemanticsDebugger scroll test', (WidgetTester tester) async {
233
    final Key childKey = UniqueKey();
234 235

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

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

254
    await tester.fling(find.byType(ListView), const Offset(0.0, -200.0), 200.0);
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);
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(-200.0, 0.0), 200.0);
265 266
    await tester.pump();

267
    expect(tester.getTopLeft(find.byKey(childKey)).dy, equals(-480.0));
268

269
    await tester.fling(find.byType(ListView), const Offset(0.0, 200.0), 200.0);
270 271
    await tester.pump();

272
    expect(tester.getTopLeft(find.byKey(childKey)).dy, equals(0.0));
273 274 275 276 277 278
  });

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

    await tester.pumpWidget(
279
      Directionality(
280
        textDirection: TextDirection.ltr,
281 282
        child: SemanticsDebugger(
          child: GestureDetector(
283 284 285 286 287 288
            onLongPress: () {
              expect(didLongPress, isFalse);
              didLongPress = true;
            },
            child: const Text('target', textDirection: TextDirection.ltr),
          ),
289 290 291 292 293 294 295 296 297 298 299 300
        ),
      ),
    );

    await tester.longPress(find.text('target'));
    expect(didLongPress, isTrue);
  });

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

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

324 325 326 327 328 329
    // 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.
    await tester.fling(find.byType(Slider), const Offset(-100.0, 0.0), 2000.0);
330
    expect(value, equals(0.70));
331 332 333
  });

  testWidgets('SemanticsDebugger checkbox', (WidgetTester tester) async {
334 335
    final Key keyTop = UniqueKey();
    final Key keyBottom = UniqueKey();
336 337

    bool valueTop = false;
338
    const bool valueBottom = true;
339 340

    await tester.pumpWidget(
341
      Directionality(
342
        textDirection: TextDirection.ltr,
343 344 345
        child: SemanticsDebugger(
          child: Material(
            child: ListView(
346
              children: <Widget>[
347
                Checkbox(
348 349 350 351 352 353
                  key: keyTop,
                  value: valueTop,
                  onChanged: (bool newValue) {
                    valueTop = newValue;
                  },
                ),
354
                Checkbox(
355 356 357 358 359 360
                  key: keyBottom,
                  value: valueBottom,
                  onChanged: null,
                ),
              ],
            ),
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
          ),
        ),
      ),
    );

    await tester.tap(find.byKey(keyTop));

    expect(valueTop, isTrue);
    valueTop = false;
    expect(valueTop, isFalse);

    await tester.tap(find.byKey(keyBottom));

    expect(valueTop, isFalse);
    expect(valueTop, isFalse);
  });
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 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 458 459 460 461 462 463 464 465 466

  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,
                    onChanged: (bool _) { },
                  ),
                ),
                Semantics(
                  container: true,
                  key: checkboxUnchecked,
                  child: Checkbox(
                    value: false,
                    onChanged: (bool _) { },
                  ),
                ),
                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,
            ),
          ),
        ),
      ),
    );

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

470
    expect(
471
      semanticsDebuggerPainter.getMessage(renderTextfield.debugSemantics),
472 473 474
      'textfield',
    );
  });
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494

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

    expect(_getSemanticsDebuggerPainter(debuggerKey: debugger, tester: tester).labelStyle, labelStyle);
  });
495 496 497 498 499 500
}

String _getMessageShownInSemanticsDebugger({
  @required Key widgetKey,
  @required Key debuggerKey,
  @required WidgetTester tester,
501 502
}) {
  final dynamic semanticsDebuggerPainter = _getSemanticsDebuggerPainter(debuggerKey: debuggerKey, tester: tester);
503
  return semanticsDebuggerPainter.getMessage(tester.renderObject(find.byKey(widgetKey)).debugSemantics) as String;
504 505 506 507 508
}

dynamic _getSemanticsDebuggerPainter({
  @required Key debuggerKey,
  @required WidgetTester tester,
509 510 511 512
}) {
  final CustomPaint customPaint = tester.widgetList(find.descendant(
    of: find.byKey(debuggerKey),
    matching: find.byType(CustomPaint),
513
  )).first as CustomPaint;
514 515
  final dynamic semanticsDebuggerPainter = customPaint.foregroundPainter;
  expect(semanticsDebuggerPainter.runtimeType.toString(), '_SemanticsDebuggerPainter');
516
  return semanticsDebuggerPainter;
517
}