tooltip_test.dart 24.7 KB
Newer Older
Hixie's avatar
Hixie committed
1 2 3 4
// 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.

5 6
import 'dart:ui';

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 = GlobalKey();
37
    await tester.pumpWidget(
38
      Directionality(
Ian Hickson's avatar
Ian Hickson committed
39
        textDirection: TextDirection.ltr,
40
        child: Overlay(
Ian Hickson's avatar
Ian Hickson committed
41
          initialEntries: <OverlayEntry>[
42
            OverlayEntry(
Ian Hickson's avatar
Ian Hickson committed
43
              builder: (BuildContext context) {
44
                return Stack(
Ian Hickson's avatar
Ian Hickson committed
45
                  children: <Widget>[
46
                    Positioned(
Ian Hickson's avatar
Ian Hickson committed
47 48
                      left: 300.0,
                      top: 0.0,
49
                      child: Tooltip(
Ian Hickson's avatar
Ian Hickson committed
50 51 52 53 54 55
                        key: key,
                        message: tooltipText,
                        height: 20.0,
                        padding: const EdgeInsets.all(5.0),
                        verticalOffset: 20.0,
                        preferBelow: false,
56
                        child: Container(
Ian Hickson's avatar
Ian Hickson committed
57 58 59 60 61 62 63 64 65 66 67 68
                          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 = GlobalKey();
93
    await tester.pumpWidget(
94
      Directionality(
Ian Hickson's avatar
Ian Hickson committed
95
        textDirection: TextDirection.ltr,
96
        child: Overlay(
Ian Hickson's avatar
Ian Hickson committed
97
          initialEntries: <OverlayEntry>[
98
            OverlayEntry(
Ian Hickson's avatar
Ian Hickson committed
99
              builder: (BuildContext context) {
100
                return Stack(
Ian Hickson's avatar
Ian Hickson committed
101
                  children: <Widget>[
102
                    Positioned(
Ian Hickson's avatar
Ian Hickson committed
103 104
                      left: 0.0,
                      top: 0.0,
105
                      child: Tooltip(
Ian Hickson's avatar
Ian Hickson committed
106 107 108 109 110 111
                        key: key,
                        message: tooltipText,
                        height: 20.0,
                        padding: const EdgeInsets.all(5.0),
                        verticalOffset: 20.0,
                        preferBelow: false,
112
                        child: Container(
Ian Hickson's avatar
Ian Hickson committed
113 114 115 116 117 118 119 120 121 122 123 124
                          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 = GlobalKey();
145
    await tester.pumpWidget(
146
      Directionality(
Ian Hickson's avatar
Ian Hickson committed
147
        textDirection: TextDirection.ltr,
148
        child: Overlay(
Ian Hickson's avatar
Ian Hickson committed
149
          initialEntries: <OverlayEntry>[
150
            OverlayEntry(
Ian Hickson's avatar
Ian Hickson committed
151
              builder: (BuildContext context) {
152
                return Stack(
Ian Hickson's avatar
Ian Hickson committed
153
                  children: <Widget>[
154
                    Positioned(
Ian Hickson's avatar
Ian Hickson committed
155 156
                      left: 400.0,
                      top: 300.0,
157
                      child: Tooltip(
Ian Hickson's avatar
Ian Hickson committed
158 159 160 161 162 163
                        key: key,
                        message: tooltipText,
                        height: 100.0,
                        padding: const EdgeInsets.all(0.0),
                        verticalOffset: 100.0,
                        preferBelow: false,
164
                        child: Container(
Ian Hickson's avatar
Ian Hickson committed
165 166 167 168 169 170 171 172 173 174 175 176
                          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 = GlobalKey();
199
    await tester.pumpWidget(
200
      Directionality(
Ian Hickson's avatar
Ian Hickson committed
201
        textDirection: TextDirection.ltr,
202
        child: Overlay(
Ian Hickson's avatar
Ian Hickson committed
203
          initialEntries: <OverlayEntry>[
204
            OverlayEntry(
Ian Hickson's avatar
Ian Hickson committed
205
              builder: (BuildContext context) {
206
                return Stack(
Ian Hickson's avatar
Ian Hickson committed
207
                  children: <Widget>[
208
                    Positioned(
Ian Hickson's avatar
Ian Hickson committed
209 210
                      left: 400.0,
                      top: 299.0,
211
                      child: Tooltip(
Ian Hickson's avatar
Ian Hickson committed
212 213 214 215 216 217
                        key: key,
                        message: tooltipText,
                        height: 190.0,
                        padding: const EdgeInsets.all(0.0),
                        verticalOffset: 100.0,
                        preferBelow: false,
218
                        child: Container(
Ian Hickson's avatar
Ian Hickson committed
219 220 221 222 223 224 225 226 227 228 229 230
                          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 = GlobalKey();
264
    await tester.pumpWidget(
265
      Directionality(
Ian Hickson's avatar
Ian Hickson committed
266
        textDirection: TextDirection.ltr,
267
        child: Overlay(
Ian Hickson's avatar
Ian Hickson committed
268
          initialEntries: <OverlayEntry>[
269
            OverlayEntry(
Ian Hickson's avatar
Ian Hickson committed
270
              builder: (BuildContext context) {
271
                return Stack(
Ian Hickson's avatar
Ian Hickson committed
272
                  children: <Widget>[
273
                    Positioned(
Ian Hickson's avatar
Ian Hickson committed
274 275
                      left: 400.0,
                      top: 300.0,
276
                      child: Tooltip(
Ian Hickson's avatar
Ian Hickson committed
277 278 279 280 281 282
                        key: key,
                        message: tooltipText,
                        height: 190.0,
                        padding: const EdgeInsets.all(0.0),
                        verticalOffset: 100.0,
                        preferBelow: true,
283
                        child: Container(
Ian Hickson's avatar
Ian Hickson committed
284 285 286 287 288 289 290 291 292 293 294 295
                          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 = GlobalKey();
317
    await tester.pumpWidget(
318
      Directionality(
Ian Hickson's avatar
Ian Hickson committed
319
        textDirection: TextDirection.ltr,
320
        child: Overlay(
Ian Hickson's avatar
Ian Hickson committed
321
          initialEntries: <OverlayEntry>[
322
            OverlayEntry(
Ian Hickson's avatar
Ian Hickson committed
323
              builder: (BuildContext context) {
324
                return Stack(
Ian Hickson's avatar
Ian Hickson committed
325
                  children: <Widget>[
326
                    Positioned(
Ian Hickson's avatar
Ian Hickson committed
327 328
                      left: 1600.0,
                      top: 300.0,
329
                      child: Tooltip(
Ian Hickson's avatar
Ian Hickson committed
330 331 332 333 334 335
                        key: key,
                        message: tooltipText,
                        height: 10.0,
                        padding: const EdgeInsets.all(0.0),
                        verticalOffset: 10.0,
                        preferBelow: true,
336
                        child: Container(
Ian Hickson's avatar
Ian Hickson committed
337 338 339 340 341 342 343 344 345 346 347 348
                          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 = GlobalKey();
372
    await tester.pumpWidget(
373
      Directionality(
Ian Hickson's avatar
Ian Hickson committed
374
        textDirection: TextDirection.ltr,
375
        child: Overlay(
Ian Hickson's avatar
Ian Hickson committed
376
          initialEntries: <OverlayEntry>[
377
            OverlayEntry(
Ian Hickson's avatar
Ian Hickson committed
378
              builder: (BuildContext context) {
379
                return Stack(
Ian Hickson's avatar
Ian Hickson committed
380
                  children: <Widget>[
381
                    Positioned(
Ian Hickson's avatar
Ian Hickson committed
382 383
                      left: 780.0,
                      top: 300.0,
384
                      child: Tooltip(
Ian Hickson's avatar
Ian Hickson committed
385 386 387 388 389 390
                        key: key,
                        message: tooltipText,
                        height: 10.0,
                        padding: const EdgeInsets.all(0.0),
                        verticalOffset: 10.0,
                        preferBelow: true,
391
                        child: Container(
Ian Hickson's avatar
Ian Hickson committed
392 393 394 395 396 397 398 399 400 401 402 403
                          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
  testWidgets('Tooltip stays around', (WidgetTester tester) async {
    await tester.pumpWidget(
427 428 429
      MaterialApp(
        home: Center(
          child: Tooltip(
430
            message: tooltipText,
431
            child: Container(
432 433
              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 = SemanticsTester(tester);
462

463
    final GlobalKey key = GlobalKey();
464
    await tester.pumpWidget(
465
      Directionality(
Ian Hickson's avatar
Ian Hickson committed
466
        textDirection: TextDirection.ltr,
467
        child: Overlay(
Ian Hickson's avatar
Ian Hickson committed
468
          initialEntries: <OverlayEntry>[
469
            OverlayEntry(
Ian Hickson's avatar
Ian Hickson committed
470
              builder: (BuildContext context) {
471
                return Stack(
Ian Hickson's avatar
Ian Hickson committed
472
                  children: <Widget>[
473
                    Positioned(
Ian Hickson's avatar
Ian Hickson committed
474 475
                      left: 780.0,
                      top: 300.0,
476
                      child: Tooltip(
Ian Hickson's avatar
Ian Hickson committed
477 478
                        key: key,
                        message: tooltipText,
479
                        child: 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 = TestSemantics.root(
492
      children: <TestSemantics>[
493
        TestSemantics.rootChild(
494 495 496 497 498
          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

  testWidgets('Tooltip overlay does not update', (WidgetTester tester) async {
    Widget buildApp(String text) {
515 516 517
      return MaterialApp(
        home: Center(
          child: Tooltip(
518
            message: text,
519
            child: Container(
520 521
              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
  testWidgets('Tooltip text scales with textScaleFactor', (WidgetTester tester) async {
    Widget buildApp(String text, { double textScaleFactor }) {
544 545 546
      return MediaQuery(
        data: MediaQueryData(textScaleFactor: textScaleFactor),
        child: Directionality(
547
          textDirection: TextDirection.ltr,
548
          child: Navigator(
549
            onGenerateRoute: (RouteSettings settings) {
550
              return MaterialPageRoute<void>(
551
                builder: (BuildContext context) {
552 553
                  return Center(
                    child: Tooltip(
554
                      message: text,
555
                      child: Container(
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
                        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
  testWidgets('Haptic feedback', (WidgetTester tester) async {
586
    final FeedbackTester feedback = FeedbackTester();
Ian Hickson's avatar
Ian Hickson committed
587
    await tester.pumpWidget(
588 589 590
      MaterialApp(
        home: Center(
          child: Tooltip(
Ian Hickson's avatar
Ian Hickson committed
591
            message: 'Foo',
592
            child: Container(
Ian Hickson's avatar
Ian Hickson committed
593 594 595 596 597 598 599
              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
  testWidgets('Semantics included', (WidgetTester tester) async {
610
    final SemanticsTester semantics = SemanticsTester(tester);
611 612

    await tester.pumpWidget(
613 614
      const MaterialApp(
        home: Center(
615
          child: Tooltip(
616
            message: 'Foo',
617
            child: Text('Bar'),
618 619 620 621 622
          ),
        ),
      ),
    );

623
    expect(semantics, hasSemantics(TestSemantics.root(
624
      children: <TestSemantics>[
625
        TestSemantics.rootChild(
626
          children: <TestSemantics>[
627
            TestSemantics(
628 629
              flags: <SemanticsFlag>[SemanticsFlag.scopesRoute],
              children: <TestSemantics>[
630
                TestSemantics(
631 632
                  label: 'Foo\nBar',
                  textDirection: TextDirection.ltr,
633
                ),
634 635
              ],
            ),
636
          ],
637 638 639 640 641 642 643 644
        ),
      ],
    ), ignoreRect: true, ignoreId: true, ignoreTransform: true));

    semantics.dispose();
  });

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

    await tester.pumpWidget(
648 649
      const MaterialApp(
        home: Center(
650
          child: Tooltip(
651
            message: 'Foo',
652
            child: Text('Bar'),
653 654 655 656 657 658
            excludeFromSemantics: true,
          ),
        ),
      ),
    );

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

    semantics.dispose();
  });

680 681
  testWidgets('has semantic events', (WidgetTester tester) async {
    final List<dynamic> semanticEvents = <dynamic>[];
682
    SystemChannels.accessibility.setMockMessageHandler((dynamic message) async {
683 684
      semanticEvents.add(message);
    });
685
    final SemanticsTester semantics = SemanticsTester(tester);
686 687

    await tester.pumpWidget(
688 689 690
      MaterialApp(
        home: Center(
          child: Tooltip(
691
            message: 'Foo',
692
            child: Container(
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
              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
}