tooltip_test.dart 25.1 KB
Newer Older
1 2
import 'dart:ui';

Hixie's avatar
Hixie committed
3 4 5 6
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

7
import 'package:flutter/services.dart';
8 9
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/gestures.dart';
Hixie's avatar
Hixie committed
10 11 12 13
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';

14
import '../widgets/semantics_tester.dart';
15
import 'feedback_tester.dart';
Hixie's avatar
Hixie committed
16

Ian Hickson's avatar
Ian Hickson committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
// This file uses "as dynamic" in a few places to defeat the static
// analysis. In general you want to avoid using this style in your
// code, as it will cause the analyzer to be unable to help you catch
// errors.
//
// In this case, we do it because we are trying to call internal
// methods of the tooltip code in order to test it. Normally, the
// state of a tooltip is a private class, but by using a GlobalKey we
// can get a handle to that object and by using "as dynamic" we can
// bypass the analyzer's type checks and call methods that we aren't
// supposed to be able to know about.
//
// It's ok to do this in tests, but you really don't want to do it in
// production code.

32 33
const String tooltipText = 'TIP';

Hixie's avatar
Hixie committed
34
void main() {
35
  testWidgets('Does tooltip end up in the right place - center', (WidgetTester tester) async {
36
    final GlobalKey key = new GlobalKey();
37
    await tester.pumpWidget(
Ian Hickson's avatar
Ian Hickson committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Overlay(
          initialEntries: <OverlayEntry>[
            new OverlayEntry(
              builder: (BuildContext context) {
                return new Stack(
                  children: <Widget>[
                    new Positioned(
                      left: 300.0,
                      top: 0.0,
                      child: new Tooltip(
                        key: key,
                        message: tooltipText,
                        height: 20.0,
                        padding: const EdgeInsets.all(5.0),
                        verticalOffset: 20.0,
                        preferBelow: false,
                        child: new Container(
                          width: 0.0,
                          height: 0.0,
                        ),
                      ),
                    ),
                  ],
                );
              },
            ),
          ],
        ),
      ),
69
    );
70
    (key.currentState as dynamic).ensureTooltipVisible(); // before using "as dynamic" in your code, see note top of file
71
    await tester.pump(const Duration(seconds: 2)); // faded in, show timer started (and at 0.0)
72

73 74 75 76 77 78 79 80
    /********************* 800x600 screen
     *      o            * y=0
     *      |            * }- 20.0 vertical offset, of which 10.0 is in the screen edge margin
     *   +----+          * \- (5.0 padding in height)
     *   |    |          * |- 20 height
     *   +----+          * /- (5.0 padding in height)
     *                   *
     *********************/
81

82
    final RenderBox tip = tester.renderObject(find.text(tooltipText)).parent.parent.parent.parent.parent;
83

84
    final Offset tipInGlobal = tip.localToGlobal(tip.size.topCenter(Offset.zero));
85 86
    // The exact position of the left side depends on the font the test framework
    // happens to pick, so we don't test that.
87 88
    expect(tipInGlobal.dx, 300.0);
    expect(tipInGlobal.dy, 20.0);
89 90
  });

91
  testWidgets('Does tooltip end up in the right place - top left', (WidgetTester tester) async {
92
    final GlobalKey key = new GlobalKey();
93
    await tester.pumpWidget(
Ian Hickson's avatar
Ian Hickson committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Overlay(
          initialEntries: <OverlayEntry>[
            new OverlayEntry(
              builder: (BuildContext context) {
                return new Stack(
                  children: <Widget>[
                    new Positioned(
                      left: 0.0,
                      top: 0.0,
                      child: new Tooltip(
                        key: key,
                        message: tooltipText,
                        height: 20.0,
                        padding: const EdgeInsets.all(5.0),
                        verticalOffset: 20.0,
                        preferBelow: false,
                        child: new Container(
                          width: 0.0,
                          height: 0.0,
                        ),
                      ),
                    ),
                  ],
                );
              },
            ),
          ],
        ),
      ),
125
    );
126
    (key.currentState as dynamic).ensureTooltipVisible(); // before using "as dynamic" in your code, see note top of file
127
    await tester.pump(const Duration(seconds: 2)); // faded in, show timer started (and at 0.0)
Hixie's avatar
Hixie committed
128

129 130 131 132 133 134 135 136
    /********************* 800x600 screen
     *o                  * y=0
     *|                  * }- 20.0 vertical offset, of which 10.0 is in the screen edge margin
     *+----+             * \- (5.0 padding in height)
     *|    |             * |- 20 height
     *+----+             * /- (5.0 padding in height)
     *                   *
     *********************/
Hixie's avatar
Hixie committed
137

138
    final RenderBox tip = tester.renderObject(find.text(tooltipText)).parent.parent.parent.parent.parent;
139
    expect(tip.size.height, equals(24.0)); // 14.0 height + 5.0 padding * 2 (top, bottom)
140
    expect(tip.localToGlobal(tip.size.topLeft(Offset.zero)), equals(const Offset(10.0, 20.0)));
Hixie's avatar
Hixie committed
141 142
  });

143
  testWidgets('Does tooltip end up in the right place - center prefer above fits', (WidgetTester tester) async {
144
    final GlobalKey key = new GlobalKey();
145
    await tester.pumpWidget(
Ian Hickson's avatar
Ian Hickson committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Overlay(
          initialEntries: <OverlayEntry>[
            new OverlayEntry(
              builder: (BuildContext context) {
                return new Stack(
                  children: <Widget>[
                    new Positioned(
                      left: 400.0,
                      top: 300.0,
                      child: new Tooltip(
                        key: key,
                        message: tooltipText,
                        height: 100.0,
                        padding: const EdgeInsets.all(0.0),
                        verticalOffset: 100.0,
                        preferBelow: false,
                        child: new Container(
                          width: 0.0,
                          height: 0.0,
                        ),
                      ),
                    ),
                  ],
                );
              },
            ),
          ],
        ),
      ),
177
    );
178
    (key.currentState as dynamic).ensureTooltipVisible(); // before using "as dynamic" in your code, see note top of file
179
    await tester.pump(const Duration(seconds: 2)); // faded in, show timer started (and at 0.0)
Hixie's avatar
Hixie committed
180

181
    /********************* 800x600 screen
182
     *        ___        * }- 10.0 margin
183 184 185 186 187 188 189
     *       |___|       * }-100.0 height
     *         |         * }-100.0 vertical offset
     *         o         * y=300.0
     *                   *
     *                   *
     *                   *
     *********************/
Hixie's avatar
Hixie committed
190

191
    final RenderBox tip = tester.renderObject(find.text(tooltipText)).parent;
192
    expect(tip.size.height, equals(100.0));
193 194
    expect(tip.localToGlobal(tip.size.topLeft(Offset.zero)).dy, equals(100.0));
    expect(tip.localToGlobal(tip.size.bottomRight(Offset.zero)).dy, equals(200.0));
Hixie's avatar
Hixie committed
195 196
  });

197
  testWidgets('Does tooltip end up in the right place - center prefer above does not fit', (WidgetTester tester) async {
198
    final GlobalKey key = new GlobalKey();
199
    await tester.pumpWidget(
Ian Hickson's avatar
Ian Hickson committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Overlay(
          initialEntries: <OverlayEntry>[
            new OverlayEntry(
              builder: (BuildContext context) {
                return new Stack(
                  children: <Widget>[
                    new Positioned(
                      left: 400.0,
                      top: 299.0,
                      child: new Tooltip(
                        key: key,
                        message: tooltipText,
                        height: 190.0,
                        padding: const EdgeInsets.all(0.0),
                        verticalOffset: 100.0,
                        preferBelow: false,
                        child: new Container(
                          width: 0.0,
                          height: 0.0,
                        ),
                      ),
                    ),
                  ],
                );
              },
            ),
          ],
        ),
      ),
231
    );
232
    (key.currentState as dynamic).ensureTooltipVisible(); // before using "as dynamic" in your code, see note top of file
233
    await tester.pump(const Duration(seconds: 2)); // faded in, show timer started (and at 0.0)
Hixie's avatar
Hixie committed
234

235 236
    // we try to put it here but it doesn't fit:
    /********************* 800x600 screen
237 238
     *        ___        * }- 10.0 margin
     *       |___|       * }-190.0 height (starts at y=9.0)
239 240 241 242 243 244
     *         |         * }-100.0 vertical offset
     *         o         * y=299.0
     *                   *
     *                   *
     *                   *
     *********************/
Hixie's avatar
Hixie committed
245

246 247 248 249 250 251
    // so we put it here:
    /********************* 800x600 screen
     *                   *
     *                   *
     *         o         * y=299.0
     *        _|_        * }-100.0 vertical offset
252 253
     *       |___|       * }-190.0 height
     *                   * }- 10.0 margin
254
     *********************/
Hixie's avatar
Hixie committed
255

256
    final RenderBox tip = tester.renderObject(find.text(tooltipText)).parent;
257
    expect(tip.size.height, equals(190.0));
258 259
    expect(tip.localToGlobal(tip.size.topLeft(Offset.zero)).dy, equals(399.0));
    expect(tip.localToGlobal(tip.size.bottomRight(Offset.zero)).dy, equals(589.0));
Hixie's avatar
Hixie committed
260 261
  });

262
  testWidgets('Does tooltip end up in the right place - center prefer below fits', (WidgetTester tester) async {
263
    final GlobalKey key = new GlobalKey();
264
    await tester.pumpWidget(
Ian Hickson's avatar
Ian Hickson committed
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Overlay(
          initialEntries: <OverlayEntry>[
            new OverlayEntry(
              builder: (BuildContext context) {
                return new Stack(
                  children: <Widget>[
                    new Positioned(
                      left: 400.0,
                      top: 300.0,
                      child: new Tooltip(
                        key: key,
                        message: tooltipText,
                        height: 190.0,
                        padding: const EdgeInsets.all(0.0),
                        verticalOffset: 100.0,
                        preferBelow: true,
                        child: new Container(
                          width: 0.0,
                          height: 0.0,
                        ),
                      ),
                    ),
                  ],
                );
              },
            ),
          ],
        ),
      ),
296
    );
297
    (key.currentState as dynamic).ensureTooltipVisible(); // before using "as dynamic" in your code, see note top of file
298
    await tester.pump(const Duration(seconds: 2)); // faded in, show timer started (and at 0.0)
Hixie's avatar
Hixie committed
299

300 301 302 303 304
    /********************* 800x600 screen
     *                   *
     *                   *
     *         o         * y=300.0
     *        _|_        * }-100.0 vertical offset
305 306
     *       |___|       * }-190.0 height
     *                   * }- 10.0 margin
307
     *********************/
Hixie's avatar
Hixie committed
308

309
    final RenderBox tip = tester.renderObject(find.text(tooltipText)).parent;
310
    expect(tip.size.height, equals(190.0));
311 312
    expect(tip.localToGlobal(tip.size.topLeft(Offset.zero)).dy, equals(400.0));
    expect(tip.localToGlobal(tip.size.bottomRight(Offset.zero)).dy, equals(590.0));
Hixie's avatar
Hixie committed
313 314
  });

315
  testWidgets('Does tooltip end up in the right place - way off to the right', (WidgetTester tester) async {
316
    final GlobalKey key = new GlobalKey();
317
    await tester.pumpWidget(
Ian Hickson's avatar
Ian Hickson committed
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Overlay(
          initialEntries: <OverlayEntry>[
            new OverlayEntry(
              builder: (BuildContext context) {
                return new Stack(
                  children: <Widget>[
                    new Positioned(
                      left: 1600.0,
                      top: 300.0,
                      child: new Tooltip(
                        key: key,
                        message: tooltipText,
                        height: 10.0,
                        padding: const EdgeInsets.all(0.0),
                        verticalOffset: 10.0,
                        preferBelow: true,
                        child: new Container(
                          width: 0.0,
                          height: 0.0,
                        ),
                      ),
                    ),
                  ],
                );
              },
            ),
          ],
        ),
      ),
349
    );
350
    (key.currentState as dynamic).ensureTooltipVisible(); // before using "as dynamic" in your code, see note top of file
351
    await tester.pump(const Duration(seconds: 2)); // faded in, show timer started (and at 0.0)
Hixie's avatar
Hixie committed
352

353 354 355 356 357 358 359 360 361
    /********************* 800x600 screen
     *                   *
     *                   *
     *                   * y=300.0;   target -->   o
     *              ___| * }-10.0 vertical offset
     *             |___| * }-10.0 height
     *                   *
     *                   * }-10.0 margin
     *********************/
Hixie's avatar
Hixie committed
362

363
    final RenderBox tip = tester.renderObject(find.text(tooltipText)).parent;
364
    expect(tip.size.height, equals(14.0));
365 366
    expect(tip.localToGlobal(tip.size.topLeft(Offset.zero)).dy, equals(310.0));
    expect(tip.localToGlobal(tip.size.bottomRight(Offset.zero)).dx, equals(790.0));
367
    expect(tip.localToGlobal(tip.size.bottomRight(Offset.zero)).dy, equals(324.0));
Hixie's avatar
Hixie committed
368 369
  });

370
  testWidgets('Does tooltip end up in the right place - near the edge', (WidgetTester tester) async {
371
    final GlobalKey key = new GlobalKey();
372
    await tester.pumpWidget(
Ian Hickson's avatar
Ian Hickson committed
373 374 375 376 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
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Overlay(
          initialEntries: <OverlayEntry>[
            new OverlayEntry(
              builder: (BuildContext context) {
                return new Stack(
                  children: <Widget>[
                    new Positioned(
                      left: 780.0,
                      top: 300.0,
                      child: new Tooltip(
                        key: key,
                        message: tooltipText,
                        height: 10.0,
                        padding: const EdgeInsets.all(0.0),
                        verticalOffset: 10.0,
                        preferBelow: true,
                        child: new Container(
                          width: 0.0,
                          height: 0.0,
                        ),
                      ),
                    ),
                  ],
                );
              },
            ),
          ],
        ),
      ),
404
    );
405
    (key.currentState as dynamic).ensureTooltipVisible(); // before using "as dynamic" in your code, see note top of file
406
    await tester.pump(const Duration(seconds: 2)); // faded in, show timer started (and at 0.0)
Hixie's avatar
Hixie committed
407

408 409 410 411 412 413 414 415 416
    /********************* 800x600 screen
     *                   *
     *                   *
     *                o  * y=300.0
     *              __|  * }-10.0 vertical offset
     *             |___| * }-10.0 height
     *                   *
     *                   * }-10.0 margin
     *********************/
Hixie's avatar
Hixie committed
417

418
    final RenderBox tip = tester.renderObject(find.text(tooltipText)).parent;
419
    expect(tip.size.height, equals(14.0));
420 421
    expect(tip.localToGlobal(tip.size.topLeft(Offset.zero)).dy, equals(310.0));
    expect(tip.localToGlobal(tip.size.bottomRight(Offset.zero)).dx, equals(790.0));
422
    expect(tip.localToGlobal(tip.size.bottomRight(Offset.zero)).dy, equals(324.0));
Hixie's avatar
Hixie committed
423
  });
Hixie's avatar
Hixie committed
424

425 426 427 428 429
  testWidgets('Tooltip stays around', (WidgetTester tester) async {
    await tester.pumpWidget(
      new MaterialApp(
        home: new Center(
          child: new Tooltip(
430
            message: tooltipText,
431 432 433
            child: new Container(
              width: 100.0,
              height: 100.0,
434
              color: Colors.green[500],
435 436 437 438 439 440
            )
          )
        )
      )
    );

441
    final Finder tooltip = find.byType(Tooltip);
442 443 444 445
    TestGesture gesture = await tester.startGesture(tester.getCenter(tooltip));
    await tester.pump(kLongPressTimeout);
    await tester.pump(const Duration(milliseconds: 10));
    await gesture.up();
446
    expect(find.text(tooltipText), findsOneWidget);
447 448 449 450 451
    await tester.tap(tooltip);
    await tester.pump(const Duration(milliseconds: 10));
    gesture = await tester.startGesture(tester.getCenter(tooltip));
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 300));
452
    expect(find.text(tooltipText), findsNothing);
453
    await tester.pump(kLongPressTimeout);
454
    expect(find.text(tooltipText), findsOneWidget);
455
    await tester.pump(kLongPressTimeout);
456
    expect(find.text(tooltipText), findsOneWidget);
457 458 459
    gesture.up();
  });

460
  testWidgets('Does tooltip contribute semantics', (WidgetTester tester) async {
461
    final SemanticsTester semantics = new SemanticsTester(tester);
462

463
    final GlobalKey key = new GlobalKey();
464
    await tester.pumpWidget(
Ian Hickson's avatar
Ian Hickson committed
465 466 467 468 469 470 471 472 473 474 475 476 477 478
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Overlay(
          initialEntries: <OverlayEntry>[
            new OverlayEntry(
              builder: (BuildContext context) {
                return new Stack(
                  children: <Widget>[
                    new Positioned(
                      left: 780.0,
                      top: 300.0,
                      child: new Tooltip(
                        key: key,
                        message: tooltipText,
479
                        child: new Container(width: 10.0, height: 10.0),
Ian Hickson's avatar
Ian Hickson committed
480 481 482 483 484 485 486 487 488
                      ),
                    ),
                  ],
                );
              },
            ),
          ],
        ),
      ),
489
    );
490

491
    final TestSemantics expected = new TestSemantics.root(
492 493 494 495 496 497 498
      children: <TestSemantics>[
        new TestSemantics.rootChild(
          id: 1,
          label: 'TIP',
          textDirection: TextDirection.ltr,
        ),
      ]
499 500 501
    );

    expect(semantics, hasSemantics(expected, ignoreTransform: true, ignoreRect: true));
Hixie's avatar
Hixie committed
502

503
    // before using "as dynamic" in your code, see note top of file
504
    (key.currentState as dynamic).ensureTooltipVisible(); // this triggers a rebuild of the semantics because the tree changes
Hixie's avatar
Hixie committed
505

506
    await tester.pump(const Duration(seconds: 2)); // faded in, show timer started (and at 0.0)
507

508
    expect(semantics, hasSemantics(expected, ignoreTransform: true, ignoreRect: true));
509 510

    semantics.dispose();
Hixie's avatar
Hixie committed
511
  });
512 513 514 515 516 517 518 519 520 521

  testWidgets('Tooltip overlay does not update', (WidgetTester tester) async {
    Widget buildApp(String text) {
      return new MaterialApp(
        home: new Center(
          child: new Tooltip(
            message: text,
            child: new Container(
              width: 100.0,
              height: 100.0,
522
              color: Colors.green[500],
523 524 525 526 527 528 529 530 531 532 533
            )
          )
        )
      );
    }

    await tester.pumpWidget(buildApp(tooltipText));
    await tester.longPress(find.byType(Tooltip));
    expect(find.text(tooltipText), findsOneWidget);
    await tester.pumpWidget(buildApp('NEW'));
    expect(find.text(tooltipText), findsOneWidget);
534
    await tester.tapAt(const Offset(5.0, 5.0));
535 536 537 538 539 540 541
    await tester.pump();
    await tester.pump(const Duration(seconds: 1));
    expect(find.text(tooltipText), findsNothing);
    await tester.longPress(find.byType(Tooltip));
    expect(find.text(tooltipText), findsNothing);
  });

542 543 544 545 546 547 548 549
  testWidgets('Tooltip text scales with textScaleFactor', (WidgetTester tester) async {
    Widget buildApp(String text, { double textScaleFactor }) {
      return new MediaQuery(
        data: new MediaQueryData(textScaleFactor: textScaleFactor),
        child: new Directionality(
          textDirection: TextDirection.ltr,
          child: new Navigator(
            onGenerateRoute: (RouteSettings settings) {
550
              return new MaterialPageRoute<void>(
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
                builder: (BuildContext context) {
                  return new Center(
                    child: new Tooltip(
                      message: text,
                      child: new Container(
                        width: 100.0,
                        height: 100.0,
                        color: Colors.green[500],
                      ),
                    ),
                  );
                }
              );
            },
          ),
        ),
      );
    }

    await tester.pumpWidget(buildApp(tooltipText, textScaleFactor: 1.0));
    await tester.longPress(find.byType(Tooltip));
    expect(find.text(tooltipText), findsOneWidget);
    expect(tester.getSize(find.text(tooltipText)), equals(const Size(42.0, 14.0)));
    RenderBox tip = tester.renderObject(find.text(tooltipText)).parent;
    expect(tip.size.height, equals(32.0));

    await tester.pumpWidget(buildApp(tooltipText, textScaleFactor: 4.0));
    await tester.longPress(find.byType(Tooltip));
    expect(find.text(tooltipText), findsOneWidget);
    expect(tester.getSize(find.text(tooltipText)), equals(const Size(168.0, 56.0)));
    tip = tester.renderObject(find.text(tooltipText)).parent;
    expect(tip.size.height, equals(56.0));
  });

585 586
  testWidgets('Haptic feedback', (WidgetTester tester) async {
    final FeedbackTester feedback = new FeedbackTester();
Ian Hickson's avatar
Ian Hickson committed
587 588
    await tester.pumpWidget(
      new MaterialApp(
589
        home: new Center(
Ian Hickson's avatar
Ian Hickson committed
590 591 592 593 594 595 596 597 598 599
          child: new Tooltip(
            message: 'Foo',
            child: new Container(
              width: 100.0,
              height: 100.0,
              color: Colors.green[500],
            ),
          ),
        ),
      ),
600 601 602 603 604 605 606 607 608
    );

    await tester.longPress(find.byType(Tooltip));
    await tester.pumpAndSettle(const Duration(seconds: 1));
    expect(feedback.hapticCount, 1);

    feedback.dispose();
  });

609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
  testWidgets('Semantics included', (WidgetTester tester) async {
    final SemanticsTester semantics = new SemanticsTester(tester);

    await tester.pumpWidget(
      new MaterialApp(
        home: const Center(
          child: const Tooltip(
            message: 'Foo',
            child: const Text('Bar'),
          ),
        ),
      ),
    );

    expect(semantics, hasSemantics(new TestSemantics.root(
      children: <TestSemantics>[
        new TestSemantics.rootChild(
626 627
          children: <TestSemantics>[
            new TestSemantics(
628 629 630 631 632 633 634 635
              flags: <SemanticsFlag>[SemanticsFlag.scopesRoute],
              children: <TestSemantics>[
                new TestSemantics(
                  label: 'Foo\nBar',
                  textDirection: TextDirection.ltr,
                )
              ],
            ),
636
          ],
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
        ),
      ],
    ), ignoreRect: true, ignoreId: true, ignoreTransform: true));

    semantics.dispose();
  });

  testWidgets('Semantics excluded', (WidgetTester tester) async {
    final SemanticsTester semantics = new SemanticsTester(tester);

    await tester.pumpWidget(
      new MaterialApp(
        home: const Center(
          child: const Tooltip(
            message: 'Foo',
            child: const Text('Bar'),
            excludeFromSemantics: true,
          ),
        ),
      ),
    );

    expect(semantics, hasSemantics(new TestSemantics.root(
      children: <TestSemantics>[
        new TestSemantics.rootChild(
662 663
          children: <TestSemantics>[
            new TestSemantics(
664 665 666 667 668 669 670 671
              flags: <SemanticsFlag>[SemanticsFlag.scopesRoute],
              children: <TestSemantics>[
                new TestSemantics(
                  label: 'Bar',
                  textDirection: TextDirection.ltr,
                )
              ],
            ),
672
          ],
673 674 675 676 677 678 679
        ),
      ],
    ), ignoreRect: true, ignoreId: true, ignoreTransform: true));

    semantics.dispose();
  });

680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
  testWidgets('has semantic events', (WidgetTester tester) async {
    final List<dynamic> semanticEvents = <dynamic>[];
    SystemChannels.accessibility.setMockMessageHandler((dynamic message) {
      semanticEvents.add(message);
    });
    final SemanticsTester semantics = new SemanticsTester(tester);

    await tester.pumpWidget(
      new MaterialApp(
        home: new Center(
          child: new Tooltip(
            message: 'Foo',
            child: new Container(
              width: 100.0,
              height: 100.0,
              color: Colors.green[500],
            ),
          ),
        ),
      ),
    );

    await tester.longPress(find.byType(Tooltip));
    final RenderObject object = tester.firstRenderObject(find.byType(Tooltip));

    expect(semanticEvents, unorderedEquals(<dynamic>[
      <String, dynamic>{
        'type': 'longPress',
        'nodeId': findDebugSemantics(object).id,
        'data': <String, dynamic>{},
      },
      <String, dynamic>{
        'type': 'tooltip',
        'data': <String, dynamic>{
          'message': 'Foo',
        },
      },
    ]));
    semantics.dispose();
    SystemChannels.accessibility.setMockMessageHandler(null);
  });
}

SemanticsNode findDebugSemantics(RenderObject object) {
  if (object.debugSemantics != null)
    return object.debugSemantics;
  return findDebugSemantics(object.parent);
Hixie's avatar
Hixie committed
727
}