tooltip_test.dart 50.8 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
Hixie's avatar
Hixie committed
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';

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

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

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
void _ensureTooltipVisible(GlobalKey key) {
  // This function uses "as dynamic"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.
  // ignore: avoid_dynamic_calls
  (key.currentState as dynamic).ensureTooltipVisible();
}
Ian Hickson's avatar
Ian Hickson committed
33

34 35
const String tooltipText = 'TIP';

36 37 38 39 40 41 42
Finder _findTooltipContainer(String tooltipText) {
  return find.ancestor(
    of: find.text(tooltipText),
    matching: find.byType(Container),
  );
}

Hixie's avatar
Hixie committed
43
void main() {
44
  testWidgets('Does tooltip end up in the right place - center', (WidgetTester tester) async {
45
    final GlobalKey key = GlobalKey();
46
    await tester.pumpWidget(
47
      Directionality(
Ian Hickson's avatar
Ian Hickson committed
48
        textDirection: TextDirection.ltr,
49
        child: Overlay(
Ian Hickson's avatar
Ian Hickson committed
50
          initialEntries: <OverlayEntry>[
51
            OverlayEntry(
Ian Hickson's avatar
Ian Hickson committed
52
              builder: (BuildContext context) {
53
                return Stack(
Ian Hickson's avatar
Ian Hickson committed
54
                  children: <Widget>[
55
                    Positioned(
Ian Hickson's avatar
Ian Hickson committed
56 57
                      left: 300.0,
                      top: 0.0,
58
                      child: Tooltip(
Ian Hickson's avatar
Ian Hickson committed
59 60 61 62 63 64
                        key: key,
                        message: tooltipText,
                        height: 20.0,
                        padding: const EdgeInsets.all(5.0),
                        verticalOffset: 20.0,
                        preferBelow: false,
65
                        child: const SizedBox(
Ian Hickson's avatar
Ian Hickson committed
66 67 68 69 70 71 72 73 74 75 76 77
                          width: 0.0,
                          height: 0.0,
                        ),
                      ),
                    ),
                  ],
                );
              },
            ),
          ],
        ),
      ),
78
    );
79
    _ensureTooltipVisible(key);
80
    await tester.pump(const Duration(seconds: 2)); // faded in, show timer started (and at 0.0)
81

82 83 84 85 86 87 88 89
    /********************* 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)
     *                   *
     *********************/
90

91 92 93
    final RenderBox tip = tester.renderObject(
      _findTooltipContainer(tooltipText),
    );
94
    final Offset tipInGlobal = tip.localToGlobal(tip.size.topCenter(Offset.zero));
95 96
    // The exact position of the left side depends on the font the test framework
    // happens to pick, so we don't test that.
97 98
    expect(tipInGlobal.dx, 300.0);
    expect(tipInGlobal.dy, 20.0);
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 125 126 127 128 129 130 131 132 133 134 135 136 137 138
  testWidgets('Does tooltip end up in the right place - center with padding outside overlay', (WidgetTester tester) async {
    final GlobalKey key = GlobalKey();
    await tester.pumpWidget(
      Directionality(
        textDirection: TextDirection.ltr,
        child: Padding(
          padding: const EdgeInsets.all(20),
          child: Overlay(
            initialEntries: <OverlayEntry>[
              OverlayEntry(
                builder: (BuildContext context) {
                  return Stack(
                    children: <Widget>[
                      Positioned(
                        left: 300.0,
                        top: 0.0,
                        child: Tooltip(
                          key: key,
                          message: tooltipText,
                          height: 20.0,
                          padding: const EdgeInsets.all(5.0),
                          verticalOffset: 20.0,
                          preferBelow: false,
                          child: const SizedBox(
                            width: 0.0,
                            height: 0.0,
                          ),
                        ),
                      ),
                    ],
                  );
                },
              ),
            ],
          ),
        ),
      ),
    );
139
    _ensureTooltipVisible(key);
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
    await tester.pump(const Duration(seconds: 2)); // faded in, show timer started (and at 0.0)

    /************************ 800x600 screen
     *   ________________   * }- 20.0 padding outside overlay
     *  |    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)
     *  |________________|  *
     *                      * } - 20.0 padding outside overlay
     ************************/

    final RenderBox tip = tester.renderObject(
      _findTooltipContainer(tooltipText),
    );
    final Offset tipInGlobal = tip.localToGlobal(tip.size.topCenter(Offset.zero));
    // The exact position of the left side depends on the font the test framework
    // happens to pick, so we don't test that.
    expect(tipInGlobal.dx, 320.0);
    expect(tipInGlobal.dy, 40.0);
  });

163
  testWidgets('Does tooltip end up in the right place - top left', (WidgetTester tester) async {
164
    final GlobalKey key = GlobalKey();
165
    await tester.pumpWidget(
166
      Directionality(
Ian Hickson's avatar
Ian Hickson committed
167
        textDirection: TextDirection.ltr,
168
        child: Overlay(
Ian Hickson's avatar
Ian Hickson committed
169
          initialEntries: <OverlayEntry>[
170
            OverlayEntry(
Ian Hickson's avatar
Ian Hickson committed
171
              builder: (BuildContext context) {
172
                return Stack(
Ian Hickson's avatar
Ian Hickson committed
173
                  children: <Widget>[
174
                    Positioned(
Ian Hickson's avatar
Ian Hickson committed
175 176
                      left: 0.0,
                      top: 0.0,
177
                      child: Tooltip(
Ian Hickson's avatar
Ian Hickson committed
178 179 180 181 182 183
                        key: key,
                        message: tooltipText,
                        height: 20.0,
                        padding: const EdgeInsets.all(5.0),
                        verticalOffset: 20.0,
                        preferBelow: false,
184
                        child: const SizedBox(
Ian Hickson's avatar
Ian Hickson committed
185 186 187 188 189 190 191 192 193 194 195 196
                          width: 0.0,
                          height: 0.0,
                        ),
                      ),
                    ),
                  ],
                );
              },
            ),
          ],
        ),
      ),
197
    );
198
    _ensureTooltipVisible(key);
199
    await tester.pump(const Duration(seconds: 2)); // faded in, show timer started (and at 0.0)
Hixie's avatar
Hixie committed
200

201 202 203 204 205 206 207 208
    /********************* 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
209

210 211 212
    final RenderBox tip = tester.renderObject(
      _findTooltipContainer(tooltipText),
    );
213
    expect(tip.size.height, equals(24.0)); // 14.0 height + 5.0 padding * 2 (top, bottom)
214
    expect(tip.localToGlobal(tip.size.topLeft(Offset.zero)), equals(const Offset(10.0, 20.0)));
215
  });
Hixie's avatar
Hixie committed
216

217
  testWidgets('Does tooltip end up in the right place - center prefer above fits', (WidgetTester tester) async {
218
    final GlobalKey key = GlobalKey();
219
    await tester.pumpWidget(
220
      Directionality(
Ian Hickson's avatar
Ian Hickson committed
221
        textDirection: TextDirection.ltr,
222
        child: Overlay(
Ian Hickson's avatar
Ian Hickson committed
223
          initialEntries: <OverlayEntry>[
224
            OverlayEntry(
Ian Hickson's avatar
Ian Hickson committed
225
              builder: (BuildContext context) {
226
                return Stack(
Ian Hickson's avatar
Ian Hickson committed
227
                  children: <Widget>[
228
                    Positioned(
Ian Hickson's avatar
Ian Hickson committed
229 230
                      left: 400.0,
                      top: 300.0,
231
                      child: Tooltip(
Ian Hickson's avatar
Ian Hickson committed
232 233 234
                        key: key,
                        message: tooltipText,
                        height: 100.0,
235
                        padding: EdgeInsets.zero,
Ian Hickson's avatar
Ian Hickson committed
236 237
                        verticalOffset: 100.0,
                        preferBelow: false,
238
                        child: const SizedBox(
Ian Hickson's avatar
Ian Hickson committed
239 240 241 242 243 244 245 246 247 248 249 250
                          width: 0.0,
                          height: 0.0,
                        ),
                      ),
                    ),
                  ],
                );
              },
            ),
          ],
        ),
      ),
251
    );
252
    _ensureTooltipVisible(key);
253
    await tester.pump(const Duration(seconds: 2)); // faded in, show timer started (and at 0.0)
Hixie's avatar
Hixie committed
254

255
    /********************* 800x600 screen
256
     *        ___        * }- 10.0 margin
257 258 259 260 261 262 263
     *       |___|       * }-100.0 height
     *         |         * }-100.0 vertical offset
     *         o         * y=300.0
     *                   *
     *                   *
     *                   *
     *********************/
Hixie's avatar
Hixie committed
264

265 266 267
    final RenderBox tip = tester.renderObject(
      _findTooltipContainer(tooltipText),
    );
268
    expect(tip.size.height, equals(100.0));
269 270
    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
271 272
  });

273
  testWidgets('Does tooltip end up in the right place - center prefer above does not fit', (WidgetTester tester) async {
274
    final GlobalKey key = GlobalKey();
275
    await tester.pumpWidget(
276
      Directionality(
Ian Hickson's avatar
Ian Hickson committed
277
        textDirection: TextDirection.ltr,
278
        child: Overlay(
Ian Hickson's avatar
Ian Hickson committed
279
          initialEntries: <OverlayEntry>[
280
            OverlayEntry(
Ian Hickson's avatar
Ian Hickson committed
281
              builder: (BuildContext context) {
282
                return Stack(
Ian Hickson's avatar
Ian Hickson committed
283
                  children: <Widget>[
284
                    Positioned(
Ian Hickson's avatar
Ian Hickson committed
285 286
                      left: 400.0,
                      top: 299.0,
287
                      child: Tooltip(
Ian Hickson's avatar
Ian Hickson committed
288 289 290
                        key: key,
                        message: tooltipText,
                        height: 190.0,
291
                        padding: EdgeInsets.zero,
Ian Hickson's avatar
Ian Hickson committed
292 293
                        verticalOffset: 100.0,
                        preferBelow: false,
294
                        child: const SizedBox(
Ian Hickson's avatar
Ian Hickson committed
295 296 297 298 299 300 301 302 303 304 305 306
                          width: 0.0,
                          height: 0.0,
                        ),
                      ),
                    ),
                  ],
                );
              },
            ),
          ],
        ),
      ),
307
    );
308
    _ensureTooltipVisible(key);
309
    await tester.pump(const Duration(seconds: 2)); // faded in, show timer started (and at 0.0)
Hixie's avatar
Hixie committed
310

311 312
    // we try to put it here but it doesn't fit:
    /********************* 800x600 screen
313 314
     *        ___        * }- 10.0 margin
     *       |___|       * }-190.0 height (starts at y=9.0)
315 316 317 318 319 320
     *         |         * }-100.0 vertical offset
     *         o         * y=299.0
     *                   *
     *                   *
     *                   *
     *********************/
Hixie's avatar
Hixie committed
321

322 323 324 325 326 327
    // so we put it here:
    /********************* 800x600 screen
     *                   *
     *                   *
     *         o         * y=299.0
     *        _|_        * }-100.0 vertical offset
328 329
     *       |___|       * }-190.0 height
     *                   * }- 10.0 margin
330
     *********************/
Hixie's avatar
Hixie committed
331

332 333 334
    final RenderBox tip = tester.renderObject(
      _findTooltipContainer(tooltipText),
    );
335
    expect(tip.size.height, equals(190.0));
336 337
    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
338 339
  });

340
  testWidgets('Does tooltip end up in the right place - center prefer below fits', (WidgetTester tester) async {
341
    final GlobalKey key = GlobalKey();
342
    await tester.pumpWidget(
343
      Directionality(
Ian Hickson's avatar
Ian Hickson committed
344
        textDirection: TextDirection.ltr,
345
        child: Overlay(
Ian Hickson's avatar
Ian Hickson committed
346
          initialEntries: <OverlayEntry>[
347
            OverlayEntry(
Ian Hickson's avatar
Ian Hickson committed
348
              builder: (BuildContext context) {
349
                return Stack(
Ian Hickson's avatar
Ian Hickson committed
350
                  children: <Widget>[
351
                    Positioned(
Ian Hickson's avatar
Ian Hickson committed
352 353
                      left: 400.0,
                      top: 300.0,
354
                      child: Tooltip(
Ian Hickson's avatar
Ian Hickson committed
355 356 357
                        key: key,
                        message: tooltipText,
                        height: 190.0,
358
                        padding: EdgeInsets.zero,
Ian Hickson's avatar
Ian Hickson committed
359 360
                        verticalOffset: 100.0,
                        preferBelow: true,
361
                        child: const SizedBox(
Ian Hickson's avatar
Ian Hickson committed
362 363 364 365 366 367 368 369 370 371 372 373
                          width: 0.0,
                          height: 0.0,
                        ),
                      ),
                    ),
                  ],
                );
              },
            ),
          ],
        ),
      ),
374
    );
375
    _ensureTooltipVisible(key);
376
    await tester.pump(const Duration(seconds: 2)); // faded in, show timer started (and at 0.0)
Hixie's avatar
Hixie committed
377

378 379 380 381 382
    /********************* 800x600 screen
     *                   *
     *                   *
     *         o         * y=300.0
     *        _|_        * }-100.0 vertical offset
383 384
     *       |___|       * }-190.0 height
     *                   * }- 10.0 margin
385
     *********************/
Hixie's avatar
Hixie committed
386

387 388 389
    final RenderBox tip = tester.renderObject(
      _findTooltipContainer(tooltipText),
    );
390
    expect(tip.size.height, equals(190.0));
391 392
    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
393 394
  });

395
  testWidgets('Does tooltip end up in the right place - way off to the right', (WidgetTester tester) async {
396
    final GlobalKey key = GlobalKey();
397
    await tester.pumpWidget(
398
      Directionality(
Ian Hickson's avatar
Ian Hickson committed
399
        textDirection: TextDirection.ltr,
400
        child: Overlay(
Ian Hickson's avatar
Ian Hickson committed
401
          initialEntries: <OverlayEntry>[
402
            OverlayEntry(
Ian Hickson's avatar
Ian Hickson committed
403
              builder: (BuildContext context) {
404
                return Stack(
Ian Hickson's avatar
Ian Hickson committed
405
                  children: <Widget>[
406
                    Positioned(
Ian Hickson's avatar
Ian Hickson committed
407 408
                      left: 1600.0,
                      top: 300.0,
409
                      child: Tooltip(
Ian Hickson's avatar
Ian Hickson committed
410 411 412
                        key: key,
                        message: tooltipText,
                        height: 10.0,
413
                        padding: EdgeInsets.zero,
Ian Hickson's avatar
Ian Hickson committed
414 415
                        verticalOffset: 10.0,
                        preferBelow: true,
416
                        child: const SizedBox(
Ian Hickson's avatar
Ian Hickson committed
417 418 419 420 421 422 423 424 425 426 427 428
                          width: 0.0,
                          height: 0.0,
                        ),
                      ),
                    ),
                  ],
                );
              },
            ),
          ],
        ),
      ),
429
    );
430
    _ensureTooltipVisible(key);
431
    await tester.pump(const Duration(seconds: 2)); // faded in, show timer started (and at 0.0)
Hixie's avatar
Hixie committed
432

433 434 435 436 437 438 439 440 441
    /********************* 800x600 screen
     *                   *
     *                   *
     *                   * y=300.0;   target -->   o
     *              ___| * }-10.0 vertical offset
     *             |___| * }-10.0 height
     *                   *
     *                   * }-10.0 margin
     *********************/
Hixie's avatar
Hixie committed
442

443 444 445
    final RenderBox tip = tester.renderObject(
      _findTooltipContainer(tooltipText),
    );
446
    expect(tip.size.height, equals(14.0));
447 448
    expect(tip.localToGlobal(tip.size.topLeft(Offset.zero)).dy, equals(310.0));
    expect(tip.localToGlobal(tip.size.bottomRight(Offset.zero)).dx, equals(790.0));
449
    expect(tip.localToGlobal(tip.size.bottomRight(Offset.zero)).dy, equals(324.0));
450
  });
Hixie's avatar
Hixie committed
451

452
  testWidgets('Does tooltip end up in the right place - near the edge', (WidgetTester tester) async {
453
    final GlobalKey key = GlobalKey();
454
    await tester.pumpWidget(
455
      Directionality(
Ian Hickson's avatar
Ian Hickson committed
456
        textDirection: TextDirection.ltr,
457
        child: Overlay(
Ian Hickson's avatar
Ian Hickson committed
458
          initialEntries: <OverlayEntry>[
459
            OverlayEntry(
Ian Hickson's avatar
Ian Hickson committed
460
              builder: (BuildContext context) {
461
                return Stack(
Ian Hickson's avatar
Ian Hickson committed
462
                  children: <Widget>[
463
                    Positioned(
Ian Hickson's avatar
Ian Hickson committed
464 465
                      left: 780.0,
                      top: 300.0,
466
                      child: Tooltip(
Ian Hickson's avatar
Ian Hickson committed
467 468 469
                        key: key,
                        message: tooltipText,
                        height: 10.0,
470
                        padding: EdgeInsets.zero,
Ian Hickson's avatar
Ian Hickson committed
471 472
                        verticalOffset: 10.0,
                        preferBelow: true,
473
                        child: const SizedBox(
Ian Hickson's avatar
Ian Hickson committed
474 475 476 477 478 479 480 481 482 483 484 485
                          width: 0.0,
                          height: 0.0,
                        ),
                      ),
                    ),
                  ],
                );
              },
            ),
          ],
        ),
      ),
486
    );
487
    _ensureTooltipVisible(key);
488
    await tester.pump(const Duration(seconds: 2)); // faded in, show timer started (and at 0.0)
Hixie's avatar
Hixie committed
489

490 491 492 493 494 495 496 497 498
    /********************* 800x600 screen
     *                   *
     *                   *
     *                o  * y=300.0
     *              __|  * }-10.0 vertical offset
     *             |___| * }-10.0 height
     *                   *
     *                   * }-10.0 margin
     *********************/
Hixie's avatar
Hixie committed
499

500 501 502
    final RenderBox tip = tester.renderObject(
      _findTooltipContainer(tooltipText),
    );
503
    expect(tip.size.height, equals(14.0));
504 505
    expect(tip.localToGlobal(tip.size.topLeft(Offset.zero)).dy, equals(310.0));
    expect(tip.localToGlobal(tip.size.bottomRight(Offset.zero)).dx, equals(790.0));
506
    expect(tip.localToGlobal(tip.size.bottomRight(Offset.zero)).dy, equals(324.0));
507
  });
Hixie's avatar
Hixie committed
508

509 510 511 512 513 514 515 516 517 518 519 520 521
  testWidgets('Custom tooltip margin', (WidgetTester tester) async {
    const double _customMarginValue = 10.0;
    final GlobalKey key = GlobalKey();
    await tester.pumpWidget(
      Directionality(
        textDirection: TextDirection.ltr,
        child: Overlay(
          initialEntries: <OverlayEntry>[
            OverlayEntry(
              builder: (BuildContext context) {
                return Tooltip(
                  key: key,
                  message: tooltipText,
522
                  padding: EdgeInsets.zero,
523
                  margin: const EdgeInsets.all(_customMarginValue),
524
                  child: const SizedBox(
525 526 527 528 529 530 531 532 533 534
                    width: 0.0,
                    height: 0.0,
                  ),
                );
              },
            ),
          ],
        ),
      ),
    );
535
    _ensureTooltipVisible(key);
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
    await tester.pump(const Duration(seconds: 2)); // faded in, show timer started (and at 0.0)

    final Offset topLeftTipInGlobal = tester.getTopLeft(
      _findTooltipContainer(tooltipText),
    );
    final Offset topLeftTooltipContentInGlobal = tester.getTopLeft(find.text(tooltipText));
    expect(topLeftTooltipContentInGlobal.dx, topLeftTipInGlobal.dx + _customMarginValue);
    expect(topLeftTooltipContentInGlobal.dy, topLeftTipInGlobal.dy + _customMarginValue);

    final Offset topRightTipInGlobal = tester.getTopRight(
      _findTooltipContainer(tooltipText),
    );
    final Offset topRightTooltipContentInGlobal = tester.getTopRight(find.text(tooltipText));
    expect(topRightTooltipContentInGlobal.dx, topRightTipInGlobal.dx - _customMarginValue);
    expect(topRightTooltipContentInGlobal.dy, topRightTipInGlobal.dy + _customMarginValue);

    final Offset bottomLeftTipInGlobal = tester.getBottomLeft(
      _findTooltipContainer(tooltipText),
    );
    final Offset bottomLeftTooltipContentInGlobal = tester.getBottomLeft(find.text(tooltipText));
    expect(bottomLeftTooltipContentInGlobal.dx, bottomLeftTipInGlobal.dx + _customMarginValue);
    expect(bottomLeftTooltipContentInGlobal.dy, bottomLeftTipInGlobal.dy - _customMarginValue);

    final Offset bottomRightTipInGlobal = tester.getBottomRight(
      _findTooltipContainer(tooltipText),
    );
    final Offset bottomRightTooltipContentInGlobal = tester.getBottomRight(find.text(tooltipText));
    expect(bottomRightTooltipContentInGlobal.dx, bottomRightTipInGlobal.dx - _customMarginValue);
    expect(bottomRightTooltipContentInGlobal.dy, bottomRightTipInGlobal.dy - _customMarginValue);
  });

567 568 569 570 571 572 573 574 575 576 577 578 579
  testWidgets('Default tooltip message textStyle - light', (WidgetTester tester) async {
    final GlobalKey key = GlobalKey();
    await tester.pumpWidget(MaterialApp(
      home: Tooltip(
        key: key,
        message: tooltipText,
        child: Container(
          width: 100.0,
          height: 100.0,
          color: Colors.green[500],
        ),
      ),
    ));
580
    _ensureTooltipVisible(key);
581 582
    await tester.pump(const Duration(seconds: 2)); // faded in, show timer started (and at 0.0)

583
    final TextStyle textStyle = tester.widget<Text>(find.text(tooltipText)).style!;
584 585 586
    expect(textStyle.color, Colors.white);
    expect(textStyle.fontFamily, 'Roboto');
    expect(textStyle.decoration, TextDecoration.none);
587
    expect(textStyle.debugLabel, '((englishLike body1 2014).merge(blackMountainView bodyText2)).copyWith');
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
  });

  testWidgets('Default tooltip message textStyle - dark', (WidgetTester tester) async {
    final GlobalKey key = GlobalKey();
    await tester.pumpWidget(MaterialApp(
      theme: ThemeData(
        brightness: Brightness.dark,
      ),
      home: Tooltip(
        key: key,
        message: tooltipText,
        child: Container(
          width: 100.0,
          height: 100.0,
          color: Colors.green[500],
        ),
      ),
    ));
606
    _ensureTooltipVisible(key);
607 608
    await tester.pump(const Duration(seconds: 2)); // faded in, show timer started (and at 0.0)

609
    final TextStyle textStyle = tester.widget<Text>(find.text(tooltipText)).style!;
610 611 612
    expect(textStyle.color, Colors.black);
    expect(textStyle.fontFamily, 'Roboto');
    expect(textStyle.decoration, TextDecoration.none);
613
    expect(textStyle.debugLabel, '((englishLike body1 2014).merge(whiteMountainView bodyText2)).copyWith');
614 615 616 617 618 619 620 621 622
  });

  testWidgets('Custom tooltip message textStyle', (WidgetTester tester) async {
    final GlobalKey key = GlobalKey();
    await tester.pumpWidget(MaterialApp(
      home: Tooltip(
        key: key,
        textStyle: const TextStyle(
          color: Colors.orange,
623
          decoration: TextDecoration.underline,
624 625 626 627 628 629 630 631 632
        ),
        message: tooltipText,
        child: Container(
          width: 100.0,
          height: 100.0,
          color: Colors.green[500],
        ),
      ),
    ));
633
    _ensureTooltipVisible(key);
634 635
    await tester.pump(const Duration(seconds: 2)); // faded in, show timer started (and at 0.0)

636
    final TextStyle textStyle = tester.widget<Text>(find.text(tooltipText)).style!;
637 638 639 640 641
    expect(textStyle.color, Colors.orange);
    expect(textStyle.fontFamily, null);
    expect(textStyle.decoration, TextDecoration.underline);
  });

642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
  testWidgets('Tooltip overlay respects ambient Directionality', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/40702.
    Widget buildApp(String text, TextDirection textDirection) {
      return MaterialApp(
        home: Directionality(
          textDirection: textDirection,
          child: Center(
            child: Tooltip(
              message: text,
              child: Container(
                width: 100.0,
                height: 100.0,
                color: Colors.green[500],
              ),
            ),
          ),
        ),
      );
    }

    await tester.pumpWidget(buildApp(tooltipText, TextDirection.rtl));
    await tester.longPress(find.byType(Tooltip));
    expect(find.text(tooltipText), findsOneWidget);
    RenderParagraph tooltipRenderParagraph = tester.renderObject<RenderParagraph>(find.text(tooltipText));
    expect(tooltipRenderParagraph.textDirection, TextDirection.rtl);

    await tester.pumpWidget(buildApp(tooltipText, TextDirection.ltr));
    await tester.longPress(find.byType(Tooltip));
    expect(find.text(tooltipText), findsOneWidget);
    tooltipRenderParagraph = tester.renderObject<RenderParagraph>(find.text(tooltipText));
    expect(tooltipRenderParagraph.textDirection, TextDirection.ltr);
  });

675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
  testWidgets('Tooltip overlay wrapped with a non-fallback DefaultTextStyle widget', (WidgetTester tester) async {
    // A Material widget is needed as an ancestor of the Text widget.
    // It is invalid to have text in a Material application that
    // does not have a Material ancestor.
    final GlobalKey key = GlobalKey();
    await tester.pumpWidget(MaterialApp(
      home: Tooltip(
        key: key,
        message: tooltipText,
        child: Container(
          width: 100.0,
          height: 100.0,
          color: Colors.green[500],
        ),
      ),
    ));
691
    _ensureTooltipVisible(key);
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
    await tester.pump(const Duration(seconds: 2)); // faded in, show timer started (and at 0.0)

    final TextStyle textStyle = tester.widget<DefaultTextStyle>(
      find.ancestor(
        of: find.text(tooltipText),
        matching: find.byType(DefaultTextStyle),
      ).first,
    ).style;

    // The default fallback text style results in a text with a
    // double underline of Color(0xffffff00).
    expect(textStyle.decoration, isNot(TextDecoration.underline));
    expect(textStyle.decorationColor, isNot(const Color(0xffffff00)));
    expect(textStyle.decorationStyle, isNot(TextDecorationStyle.double));
  });

708 709 710 711 712 713 714 715 716 717 718 719
  testWidgets('Does tooltip end up with the right default size, shape, and color', (WidgetTester tester) async {
    final GlobalKey key = GlobalKey();
    await tester.pumpWidget(
      Directionality(
        textDirection: TextDirection.ltr,
        child: Overlay(
          initialEntries: <OverlayEntry>[
            OverlayEntry(
              builder: (BuildContext context) {
                return Tooltip(
                  key: key,
                  message: tooltipText,
720
                  child: const SizedBox(
721 722 723 724 725 726 727 728 729 730
                    width: 0.0,
                    height: 0.0,
                  ),
                );
              },
            ),
          ],
        ),
      ),
    );
731
    _ensureTooltipVisible(key);
732 733
    await tester.pump(const Duration(seconds: 2)); // faded in, show timer started (and at 0.0)

734 735 736
    final RenderBox tip = tester.renderObject(
      _findTooltipContainer(tooltipText),
    );
737 738 739 740 741 742
    expect(tip.size.height, equals(32.0));
    expect(tip.size.width, equals(74.0));
    expect(tip, paints..rrect(
      rrect: RRect.fromRectAndRadius(tip.paintBounds, const Radius.circular(4.0)),
      color: const Color(0xe6616161),
    ));
743
  });
744

745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
  testWidgets('Tooltip default size, shape, and color test for Desktop', (WidgetTester tester) async {
    // Regressing test for https://github.com/flutter/flutter/issues/68601
    final GlobalKey key = GlobalKey();
    await tester.pumpWidget(
      MaterialApp(
        home: Tooltip(
          key: key,
          message: tooltipText,
          child: const SizedBox(
            width: 0.0,
            height: 0.0,
          ),
        ),
      ),
    );
760
    // ignore: avoid_dynamic_calls
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
    (key.currentState as dynamic).ensureTooltipVisible();
    await tester.pump(const Duration(seconds: 2)); // faded in, show timer started (and at 0.0)

    final RenderParagraph tooltipRenderParagraph = tester.renderObject<RenderParagraph>(find.text(tooltipText));
    expect(tooltipRenderParagraph.textSize.height, equals(10.0));

    final RenderBox tip = tester.renderObject(
      _findTooltipContainer(tooltipText),
    );
    expect(tip.size.height, equals(24.0));
    expect(tip.size.width, equals(46.0));
    expect(tip, paints..rrect(
      rrect: RRect.fromRectAndRadius(tip.paintBounds, const Radius.circular(4.0)),
      color: const Color(0xe6616161),
    ));
  }, variant: const TargetPlatformVariant(<TargetPlatform>{TargetPlatform.macOS, TargetPlatform.linux, TargetPlatform.windows}));

778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
  testWidgets('Can tooltip decoration be customized', (WidgetTester tester) async {
    final GlobalKey key = GlobalKey();
    const Decoration customDecoration = ShapeDecoration(
      shape: StadiumBorder(),
      color: Color(0x80800000),
    );
    await tester.pumpWidget(
      Directionality(
        textDirection: TextDirection.ltr,
        child: Overlay(
          initialEntries: <OverlayEntry>[
            OverlayEntry(
              builder: (BuildContext context) {
                return Tooltip(
                  key: key,
                  decoration: customDecoration,
                  message: tooltipText,
795
                  child: const SizedBox(
796 797 798 799 800 801 802 803 804 805
                    width: 0.0,
                    height: 0.0,
                  ),
                );
              },
            ),
          ],
        ),
      ),
    );
806
    _ensureTooltipVisible(key);
807 808
    await tester.pump(const Duration(seconds: 2)); // faded in, show timer started (and at 0.0)

809 810 811
    final RenderBox tip = tester.renderObject(
      _findTooltipContainer(tooltipText),
    );
812 813 814 815 816
    expect(tip.size.height, equals(32.0));
    expect(tip.size.width, equals(74.0));
    expect(tip, paints..path(
      color: const Color(0x80800000),
    ));
817
  });
818

819
  testWidgets('Tooltip stays after long press', (WidgetTester tester) async {
820
    await tester.pumpWidget(
821 822 823
      MaterialApp(
        home: Center(
          child: Tooltip(
824
            message: tooltipText,
825
            child: Container(
826 827
              width: 100.0,
              height: 100.0,
828
              color: Colors.green[500],
829 830 831
            ),
          ),
        ),
832
      ),
833 834
    );

835
    final Finder tooltip = find.byType(Tooltip);
836
    TestGesture gesture = await tester.startGesture(tester.getCenter(tooltip));
837 838

    // long press reveals tooltip
839 840
    await tester.pump(kLongPressTimeout);
    await tester.pump(const Duration(milliseconds: 10));
841
    expect(find.text(tooltipText), findsOneWidget);
842 843 844 845
    await gesture.up();

    // tap (down, up) gesture hides tooltip, since its not
    // a long press
846 847
    await tester.tap(tooltip);
    await tester.pump(const Duration(milliseconds: 10));
848 849 850
    expect(find.text(tooltipText), findsNothing);

    // long press once more
851 852 853
    gesture = await tester.startGesture(tester.getCenter(tooltip));
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 300));
854
    expect(find.text(tooltipText), findsNothing);
855

856
    await tester.pump(kLongPressTimeout);
857
    await tester.pump(const Duration(milliseconds: 10));
858
    expect(find.text(tooltipText), findsOneWidget);
859 860

    // keep holding the long press, should still show tooltip
861
    await tester.pump(kLongPressTimeout);
862
    expect(find.text(tooltipText), findsOneWidget);
863
    await gesture.up();
864 865
  });

866
  testWidgets('Tooltip shows/hides when hovered', (WidgetTester tester) async {
867
    const Duration waitDuration = Duration.zero;
868
    TestGesture? gesture = await tester.createGesture(kind: PointerDeviceKind.mouse);
869 870 871 872
    addTearDown(() async {
      if (gesture != null)
        return gesture.removePointer();
    });
Michael Goderbauer's avatar
Michael Goderbauer committed
873
    await gesture.addPointer();
874 875 876 877 878
    await gesture.moveTo(const Offset(1.0, 1.0));
    await tester.pump();
    await gesture.moveTo(Offset.zero);

    await tester.pumpWidget(
879
      const MaterialApp(
880 881 882 883
        home: Center(
          child: Tooltip(
            message: tooltipText,
            waitDuration: waitDuration,
884
            child: SizedBox(
885 886 887 888 889
              width: 100.0,
              height: 100.0,
            ),
          ),
        ),
890
      ),
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
    );

    final Finder tooltip = find.byType(Tooltip);
    await gesture.moveTo(Offset.zero);
    await tester.pump();
    await gesture.moveTo(tester.getCenter(tooltip));
    await tester.pump();
    // Wait for it to appear.
    await tester.pump(waitDuration);
    expect(find.text(tooltipText), findsOneWidget);

    // Wait a looong time to make sure that it doesn't go away if the mouse is
    // still over the widget.
    await tester.pump(const Duration(days: 1));
    await tester.pumpAndSettle();
    expect(find.text(tooltipText), findsOneWidget);

    await gesture.moveTo(Offset.zero);
    await tester.pump();

    // Wait for it to disappear.
    await tester.pumpAndSettle();
    await gesture.removePointer();
914
    gesture = null;
915 916 917
    expect(find.text(tooltipText), findsNothing);
  });

918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
  testWidgets('Tooltip text is also hoverable', (WidgetTester tester) async {
    const Duration waitDuration = Duration.zero;
    TestGesture? gesture = await tester.createGesture(kind: PointerDeviceKind.mouse);
    addTearDown(() async {
      if (gesture != null)
        return gesture.removePointer();
    });
    await gesture.addPointer();
    await gesture.moveTo(const Offset(1.0, 1.0));
    await tester.pump();
    await gesture.moveTo(Offset.zero);

    await tester.pumpWidget(
      const MaterialApp(
        home: Center(
          child: Tooltip(
            message: tooltipText,
            waitDuration: waitDuration,
            child: Text('I am tool tip'),
          ),
        ),
      ),
    );

    final Finder tooltip = find.byType(Tooltip);
    await gesture.moveTo(Offset.zero);
    await tester.pump();
    await gesture.moveTo(tester.getCenter(tooltip));
    await tester.pump();
    // Wait for it to appear.
    await tester.pump(waitDuration);
    expect(find.text(tooltipText), findsOneWidget);

    // Wait a looong time to make sure that it doesn't go away if the mouse is
    // still over the widget.
    await tester.pump(const Duration(days: 1));
    await tester.pumpAndSettle();
    expect(find.text(tooltipText), findsOneWidget);

    // Hover to the tool tip text and verify the tooltip doesn't go away.
    await gesture.moveTo(tester.getTopLeft(find.text(tooltipText)));
    await tester.pump(const Duration(days: 1));
    await tester.pumpAndSettle();
    expect(find.text(tooltipText), findsOneWidget);

    await gesture.moveTo(Offset.zero);
    await tester.pump();

    // Wait for it to disappear.
    await tester.pumpAndSettle();
    await gesture.removePointer();
    gesture = null;
    expect(find.text(tooltipText), findsNothing);
  });

  testWidgets('Tooltip can be dismissed by escape key', (WidgetTester tester) async {
    const Duration waitDuration = Duration.zero;
    TestGesture? gesture = await tester.createGesture(kind: PointerDeviceKind.mouse);
    addTearDown(() async {
      if (gesture != null)
        return gesture.removePointer();
    });
    await gesture.addPointer();
    await gesture.moveTo(const Offset(1.0, 1.0));
    await tester.pump();
    await gesture.moveTo(Offset.zero);

    await tester.pumpWidget(
      const MaterialApp(
        home: Center(
          child: Tooltip(
            message: tooltipText,
            waitDuration: waitDuration,
            child: Text('I am tool tip'),
          ),
        ),
      ),
    );

    final Finder tooltip = find.byType(Tooltip);
    await gesture.moveTo(Offset.zero);
    await tester.pump();
    await gesture.moveTo(tester.getCenter(tooltip));
    await tester.pump();
    // Wait for it to appear.
    await tester.pump(waitDuration);
    expect(find.text(tooltipText), findsOneWidget);

    // Try to dismiss the tooltip with the shortcut key
    await tester.sendKeyEvent(LogicalKeyboardKey.escape);
    await tester.pumpAndSettle();
    expect(find.text(tooltipText), findsNothing);

    await gesture.moveTo(Offset.zero);
    await tester.pumpAndSettle();
    await gesture.removePointer();
    gesture = null;
  });

  testWidgets('Multiple Tooltips are dismissed by escape key', (WidgetTester tester) async {
    const Duration waitDuration = Duration.zero;
    TestGesture? gesture = await tester.createGesture(kind: PointerDeviceKind.mouse);
    addTearDown(() async {
      if (gesture != null)
        return gesture.removePointer();
    });
    await gesture.addPointer();
    await gesture.moveTo(const Offset(1.0, 1.0));
    await tester.pump();
    await gesture.moveTo(Offset.zero);

    await tester.pumpWidget(
      MaterialApp(
        home: Center(
          child: Column(
            children: const <Widget>[
              Tooltip(
                message: 'message1',
                waitDuration: waitDuration,
                showDuration: Duration(days: 1),
                child: Text('tooltip1'),
              ),
              Spacer(flex: 2),
              Tooltip(
                message: 'message2',
                waitDuration: waitDuration,
                showDuration: Duration(days: 1),
                child: Text('tooltip2'),
              )
            ],
          ),
        ),
      ),
    );

    final Finder tooltip = find.text('tooltip1');
    await gesture.moveTo(Offset.zero);
    await tester.pump();
    await gesture.moveTo(tester.getCenter(tooltip));
    await tester.pump();
    await tester.pump(waitDuration);
    expect(find.text('message1'), findsOneWidget);

    final Finder secondTooltip = find.text('tooltip2');
    await gesture.moveTo(Offset.zero);
    await tester.pump();
    await gesture.moveTo(tester.getCenter(secondTooltip));
    await tester.pump();
    await tester.pump(waitDuration);
    // Make sure both messages are on the screen.
    expect(find.text('message1'), findsOneWidget);
    expect(find.text('message2'), findsOneWidget);

    // Try to dismiss the tooltip with the shortcut key
    await tester.sendKeyEvent(LogicalKeyboardKey.escape);
    await tester.pumpAndSettle();
    expect(find.text('message1'), findsNothing);
    expect(find.text('message2'), findsNothing);

    await gesture.moveTo(Offset.zero);
    await tester.pumpAndSettle();
    await gesture.removePointer();
    gesture = null;
  });

1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
  testWidgets('Tooltip does not attempt to show after unmount', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/54096.
    const Duration waitDuration = Duration(seconds: 1);
    final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse);
    addTearDown(() async {
      if (gesture != null)
        return gesture.removePointer();
    });
    await gesture.addPointer();
    await gesture.moveTo(const Offset(1.0, 1.0));
    await tester.pump();
    await gesture.moveTo(Offset.zero);

    await tester.pumpWidget(
      const MaterialApp(
        home: Center(
          child: Tooltip(
            message: tooltipText,
            waitDuration: waitDuration,
            child: SizedBox(
              width: 100.0,
              height: 100.0,
            ),
          ),
        ),
      ),
    );

    final Finder tooltip = find.byType(Tooltip);
    await gesture.moveTo(Offset.zero);
    await tester.pump();
    await gesture.moveTo(tester.getCenter(tooltip));
    await tester.pump();

    // Pump another random widget to unmount the Tooltip widget.
    await tester.pumpWidget(
        const MaterialApp(
          home: Center(
            child: SizedBox(),
        ),
      ),
    );

    // If the issue regresses, an exception will be thrown while we are waiting.
    await tester.pump(waitDuration);
  });

1130
  testWidgets('Does tooltip contribute semantics', (WidgetTester tester) async {
1131
    final SemanticsTester semantics = SemanticsTester(tester);
1132

1133
    final GlobalKey key = GlobalKey();
1134
    await tester.pumpWidget(
1135
      Directionality(
Ian Hickson's avatar
Ian Hickson committed
1136
        textDirection: TextDirection.ltr,
1137
        child: Overlay(
Ian Hickson's avatar
Ian Hickson committed
1138
          initialEntries: <OverlayEntry>[
1139
            OverlayEntry(
Ian Hickson's avatar
Ian Hickson committed
1140
              builder: (BuildContext context) {
1141
                return Stack(
Ian Hickson's avatar
Ian Hickson committed
1142
                  children: <Widget>[
1143
                    Positioned(
Ian Hickson's avatar
Ian Hickson committed
1144 1145
                      left: 780.0,
                      top: 300.0,
1146
                      child: Tooltip(
Ian Hickson's avatar
Ian Hickson committed
1147 1148
                        key: key,
                        message: tooltipText,
1149
                        child: const SizedBox(width: 10.0, height: 10.0),
Ian Hickson's avatar
Ian Hickson committed
1150 1151 1152 1153 1154 1155 1156 1157 1158
                      ),
                    ),
                  ],
                );
              },
            ),
          ],
        ),
      ),
1159
    );
1160

1161
    final TestSemantics expected = TestSemantics.root(
1162
      children: <TestSemantics>[
1163
        TestSemantics.rootChild(
1164 1165 1166 1167
          id: 1,
          label: 'TIP',
          textDirection: TextDirection.ltr,
        ),
1168
      ],
1169 1170 1171
    );

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

1173 1174
    // This triggers a rebuild of the semantics because the tree changes.
    _ensureTooltipVisible(key);
Hixie's avatar
Hixie committed
1175

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

1178
    expect(semantics, hasSemantics(expected, ignoreTransform: true, ignoreRect: true));
1179 1180

    semantics.dispose();
Hixie's avatar
Hixie committed
1181
  });
1182 1183 1184

  testWidgets('Tooltip overlay does not update', (WidgetTester tester) async {
    Widget buildApp(String text) {
1185 1186 1187
      return MaterialApp(
        home: Center(
          child: Tooltip(
1188
            message: text,
1189
            child: Container(
1190 1191
              width: 100.0,
              height: 100.0,
1192
              color: Colors.green[500],
1193 1194 1195
            ),
          ),
        ),
1196 1197 1198 1199 1200 1201 1202 1203
      );
    }

    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);
1204
    await tester.tapAt(const Offset(5.0, 5.0));
1205 1206 1207 1208 1209 1210 1211
    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);
  });

1212
  testWidgets('Tooltip text scales with textScaleFactor', (WidgetTester tester) async {
1213
    Widget buildApp(String text, { required double textScaleFactor }) {
1214 1215 1216
      return MediaQuery(
        data: MediaQueryData(textScaleFactor: textScaleFactor),
        child: Directionality(
1217
          textDirection: TextDirection.ltr,
1218
          child: Navigator(
1219
            onGenerateRoute: (RouteSettings settings) {
1220
              return MaterialPageRoute<void>(
1221
                builder: (BuildContext context) {
1222 1223
                  return Center(
                    child: Tooltip(
1224
                      message: text,
1225
                      child: Container(
1226 1227 1228 1229 1230 1231
                        width: 100.0,
                        height: 100.0,
                        color: Colors.green[500],
                      ),
                    ),
                  );
1232
                },
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
              );
            },
          ),
        ),
      );
    }

    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)));
1244 1245 1246
    RenderBox tip = tester.renderObject(
      _findTooltipContainer(tooltipText),
    );
1247 1248 1249 1250 1251 1252
    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)));
1253 1254 1255
    tip = tester.renderObject(
      _findTooltipContainer(tooltipText),
    );
1256
    expect(tip.size.height, equals(56.0));
1257
  });
1258

1259
  testWidgets('Haptic feedback', (WidgetTester tester) async {
1260
    final FeedbackTester feedback = FeedbackTester();
Ian Hickson's avatar
Ian Hickson committed
1261
    await tester.pumpWidget(
1262 1263 1264
      MaterialApp(
        home: Center(
          child: Tooltip(
Ian Hickson's avatar
Ian Hickson committed
1265
            message: 'Foo',
1266
            child: Container(
Ian Hickson's avatar
Ian Hickson committed
1267 1268 1269 1270 1271 1272 1273
              width: 100.0,
              height: 100.0,
              color: Colors.green[500],
            ),
          ),
        ),
      ),
1274 1275 1276 1277 1278 1279 1280 1281 1282
    );

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

    feedback.dispose();
  });

1283
  testWidgets('Semantics included', (WidgetTester tester) async {
1284
    final SemanticsTester semantics = SemanticsTester(tester);
1285 1286

    await tester.pumpWidget(
1287 1288
      const MaterialApp(
        home: Center(
1289
          child: Tooltip(
1290
            message: 'Foo',
1291
            child: Text('Bar'),
1292 1293 1294 1295 1296
          ),
        ),
      ),
    );

1297
    expect(semantics, hasSemantics(TestSemantics.root(
1298
      children: <TestSemantics>[
1299
        TestSemantics.rootChild(
1300
          children: <TestSemantics>[
1301
            TestSemantics(
1302
              children: <TestSemantics>[
1303
                TestSemantics(
1304 1305 1306 1307 1308 1309 1310
                  flags: <SemanticsFlag>[SemanticsFlag.scopesRoute],
                  children: <TestSemantics>[
                    TestSemantics(
                      label: 'Foo\nBar',
                      textDirection: TextDirection.ltr,
                    ),
                  ],
1311
                ),
1312 1313
              ],
            ),
1314
          ],
1315 1316 1317 1318 1319 1320 1321 1322
        ),
      ],
    ), ignoreRect: true, ignoreId: true, ignoreTransform: true));

    semantics.dispose();
  });

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

    await tester.pumpWidget(
1326 1327
      const MaterialApp(
        home: Center(
1328
          child: Tooltip(
1329 1330
            message: 'Foo',
            excludeFromSemantics: true,
1331
            child: Text('Bar'),
1332 1333 1334 1335 1336
          ),
        ),
      ),
    );

1337
    expect(semantics, hasSemantics(TestSemantics.root(
1338
      children: <TestSemantics>[
1339
        TestSemantics.rootChild(
1340
          children: <TestSemantics>[
1341
            TestSemantics(
1342
              children: <TestSemantics>[
1343
                TestSemantics(
1344 1345 1346 1347 1348 1349 1350
                  flags: <SemanticsFlag>[SemanticsFlag.scopesRoute],
                  children: <TestSemantics>[
                    TestSemantics(
                      label: 'Bar',
                      textDirection: TextDirection.ltr,
                    ),
                  ],
1351
                ),
1352 1353
              ],
            ),
1354
          ],
1355 1356 1357 1358 1359 1360 1361
        ),
      ],
    ), ignoreRect: true, ignoreId: true, ignoreTransform: true));

    semantics.dispose();
  });

1362 1363
  testWidgets('has semantic events', (WidgetTester tester) async {
    final List<dynamic> semanticEvents = <dynamic>[];
1364
    tester.binding.defaultBinaryMessenger.setMockDecodedMessageHandler<dynamic>(SystemChannels.accessibility, (dynamic message) async {
1365 1366
      semanticEvents.add(message);
    });
1367
    final SemanticsTester semantics = SemanticsTester(tester);
1368 1369

    await tester.pumpWidget(
1370 1371 1372
      MaterialApp(
        home: Center(
          child: Tooltip(
1373
            message: 'Foo',
1374
            child: Container(
1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400
              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();
1401
    tester.binding.defaultBinaryMessenger.setMockDecodedMessageHandler<dynamic>(SystemChannels.accessibility, null);
1402
  });
1403 1404 1405
  testWidgets('default Tooltip debugFillProperties', (WidgetTester tester) async {
    final DiagnosticPropertiesBuilder builder = DiagnosticPropertiesBuilder();

1406
    const Tooltip(message: 'message').debugFillProperties(builder);
1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426

    final List<String> description = builder.properties
      .where((DiagnosticsNode node) => !node.isFiltered(DiagnosticLevel.info))
      .map((DiagnosticsNode node) => node.toString()).toList();

    expect(description, <String>[
      '"message"',
    ]);
  });
  testWidgets('Tooltip implements debugFillProperties', (WidgetTester tester) async {
    final DiagnosticPropertiesBuilder builder = DiagnosticPropertiesBuilder();

    // Not checking controller, inputFormatters, focusNode
    const Tooltip(
      key: ValueKey<String>('foo'),
      message: 'message',
      decoration: BoxDecoration(),
      waitDuration: Duration(seconds: 1),
      showDuration: Duration(seconds: 2),
      padding: EdgeInsets.zero,
1427
      margin: EdgeInsets.all(5.0),
1428 1429 1430 1431
      height: 100.0,
      excludeFromSemantics: true,
      preferBelow: false,
      verticalOffset: 50.0,
1432 1433
      triggerMode: TooltipTriggerMode.manual,
      enableFeedback: true,
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443
    ).debugFillProperties(builder);

    final List<String> description = builder.properties
      .where((DiagnosticsNode node) => !node.isFiltered(DiagnosticLevel.info))
      .map((DiagnosticsNode node) => node.toString()).toList();

    expect(description, <String>[
      '"message"',
      'height: 100.0',
      'padding: EdgeInsets.zero',
1444
      'margin: EdgeInsets.all(5.0)',
1445 1446 1447 1448 1449
      'vertical offset: 50.0',
      'position: above',
      'semantics: excluded',
      'wait duration: 0:00:01.000000',
      'show duration: 0:00:02.000000',
1450 1451
      'triggerMode: TooltipTriggerMode.manual',
      'enableFeedback: true',
1452 1453
    ]);
  });
1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524

  testWidgets('Tooltip triggers on tap when trigger mode is tap', (WidgetTester tester) async {
    await setWidgetForTooltipMode(tester, TooltipTriggerMode.tap);

    final Finder tooltip = find.byType(Tooltip);
    expect(find.text(tooltipText), findsNothing);

    await testGestureTap(tester, tooltip);
    expect(find.text(tooltipText), findsOneWidget);
  });

  testWidgets('Tooltip triggers on long press when mode is long press', (WidgetTester tester) async {
    await setWidgetForTooltipMode(tester, TooltipTriggerMode.longPress);

    final Finder tooltip = find.byType(Tooltip);
    expect(find.text(tooltipText), findsNothing);

    await testGestureTap(tester, tooltip);
    expect(find.text(tooltipText), findsNothing);

    await testGestureLongPress(tester, tooltip);
    expect(find.text(tooltipText), findsOneWidget);
  });

  testWidgets('Tooltip does not trigger on tap when trigger mode is longPress', (WidgetTester tester) async {
    await setWidgetForTooltipMode(tester, TooltipTriggerMode.longPress);

    final Finder tooltip = find.byType(Tooltip);
    expect(find.text(tooltipText), findsNothing);

    await testGestureTap(tester, tooltip);
    expect(find.text(tooltipText), findsNothing);
  });

  testWidgets('Tooltip does not trigger when trigger mode is manual', (WidgetTester tester) async {
    await setWidgetForTooltipMode(tester, TooltipTriggerMode.manual);

    final Finder tooltip = find.byType(Tooltip);
    expect(find.text(tooltipText), findsNothing);

    await testGestureTap(tester, tooltip);
    expect(find.text(tooltipText), findsNothing);

    await testGestureLongPress(tester, tooltip);
    expect(find.text(tooltipText), findsNothing);
  });
}

Future<void> setWidgetForTooltipMode(WidgetTester tester, TooltipTriggerMode triggerMode) async {
  await tester.pumpWidget(
    MaterialApp(
      home: Tooltip(
        message: tooltipText,
        triggerMode: triggerMode,
        child: const SizedBox(width: 100.0, height: 100.0),
      ),
    ),
  );
}

Future<void> testGestureLongPress(WidgetTester tester, Finder tooltip) async {
  final TestGesture gestureLongPress = await tester.startGesture(tester.getCenter(tooltip));
  await tester.pump();
  await tester.pump(kLongPressTimeout);
  await gestureLongPress.up();
  await tester.pump();
}

Future<void> testGestureTap(WidgetTester tester, Finder tooltip) async {
  await tester.tap(tooltip);
  await tester.pump(const Duration(milliseconds: 10));
1525 1526 1527 1528
}

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