tooltip_test.dart 18.1 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 'package:flutter_test/flutter_test.dart';
import 'package:flutter/gestures.dart';
Hixie's avatar
Hixie committed
7 8 9 10
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';

11
import '../widgets/semantics_tester.dart';
Hixie's avatar
Hixie committed
12

Ian Hickson's avatar
Ian Hickson committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
// 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.

28 29
const String tooltipText = 'TIP';

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

66 67 68 69 70 71 72 73
    /********************* 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)
     *                   *
     *********************/
74

75
    RenderBox tip = tester.renderObject(find.text(tooltipText)).parent.parent.parent.parent.parent;
76

77 78 79 80
    Point tipInGlobal = tip.localToGlobal(tip.size.topCenter(Point.origin));
    // 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.x, 300.0);
81
    expect(tipInGlobal.y, 20.0);
82 83
  });

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

119 120 121 122 123 124 125 126
    /********************* 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
127

128
    RenderBox tip = tester.renderObject(find.text(tooltipText)).parent.parent.parent.parent.parent;
129 130
    expect(tip.size.height, equals(20.0)); // 10.0 height + 5.0 padding * 2 (top, bottom)
    expect(tip.localToGlobal(tip.size.topLeft(Point.origin)), equals(const Point(10.0, 20.0)));
Hixie's avatar
Hixie committed
131 132
  });

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

168
    /********************* 800x600 screen
169
     *        ___        * }- 10.0 margin
170 171 172 173 174 175 176
     *       |___|       * }-100.0 height
     *         |         * }-100.0 vertical offset
     *         o         * y=300.0
     *                   *
     *                   *
     *                   *
     *********************/
Hixie's avatar
Hixie committed
177

178
    RenderBox tip = tester.renderObject(find.text(tooltipText)).parent;
179 180 181
    expect(tip.size.height, equals(100.0));
    expect(tip.localToGlobal(tip.size.topLeft(Point.origin)).y, equals(100.0));
    expect(tip.localToGlobal(tip.size.bottomRight(Point.origin)).y, equals(200.0));
Hixie's avatar
Hixie committed
182 183
  });

184
  testWidgets('Does tooltip end up in the right place - center prefer above does not fit', (WidgetTester tester) async {
185
    GlobalKey key = new GlobalKey();
186
    await tester.pumpWidget(
187 188 189 190 191 192 193 194 195 196 197
      new Overlay(
        initialEntries: <OverlayEntry>[
          new OverlayEntry(
            builder: (BuildContext context) {
              return new Stack(
                children: <Widget>[
                  new Positioned(
                    left: 400.0,
                    top: 299.0,
                    child: new Tooltip(
                      key: key,
198
                      message: tooltipText,
199
                      height: 190.0,
200 201 202 203 204 205
                      padding: const EdgeInsets.all(0.0),
                      verticalOffset: 100.0,
                      preferBelow: false,
                      child: new Container(
                        width: 0.0,
                        height: 0.0
Hixie's avatar
Hixie committed
206
                      )
207 208 209 210 211 212 213 214 215
                    )
                  ),
                ]
              );
            }
          ),
        ]
      )
    );
216
    (key.currentState as dynamic).ensureTooltipVisible(); // before using "as dynamic" in your code, see note top of file
217
    await tester.pump(const Duration(seconds: 2)); // faded in, show timer started (and at 0.0)
Hixie's avatar
Hixie committed
218

219 220
    // we try to put it here but it doesn't fit:
    /********************* 800x600 screen
221 222
     *        ___        * }- 10.0 margin
     *       |___|       * }-190.0 height (starts at y=9.0)
223 224 225 226 227 228
     *         |         * }-100.0 vertical offset
     *         o         * y=299.0
     *                   *
     *                   *
     *                   *
     *********************/
Hixie's avatar
Hixie committed
229

230 231 232 233 234 235
    // so we put it here:
    /********************* 800x600 screen
     *                   *
     *                   *
     *         o         * y=299.0
     *        _|_        * }-100.0 vertical offset
236 237
     *       |___|       * }-190.0 height
     *                   * }- 10.0 margin
238
     *********************/
Hixie's avatar
Hixie committed
239

240
    RenderBox tip = tester.renderObject(find.text(tooltipText)).parent;
241
    expect(tip.size.height, equals(190.0));
242
    expect(tip.localToGlobal(tip.size.topLeft(Point.origin)).y, equals(399.0));
243
    expect(tip.localToGlobal(tip.size.bottomRight(Point.origin)).y, equals(589.0));
Hixie's avatar
Hixie committed
244 245
  });

246
  testWidgets('Does tooltip end up in the right place - center prefer below fits', (WidgetTester tester) async {
247
    GlobalKey key = new GlobalKey();
248
    await tester.pumpWidget(
249 250 251 252 253 254 255 256 257 258 259
      new Overlay(
        initialEntries: <OverlayEntry>[
          new OverlayEntry(
            builder: (BuildContext context) {
              return new Stack(
                children: <Widget>[
                  new Positioned(
                    left: 400.0,
                    top: 300.0,
                    child: new Tooltip(
                      key: key,
260
                      message: tooltipText,
261
                      height: 190.0,
262 263 264 265 266 267
                      padding: const EdgeInsets.all(0.0),
                      verticalOffset: 100.0,
                      preferBelow: true,
                      child: new Container(
                        width: 0.0,
                        height: 0.0
Hixie's avatar
Hixie committed
268
                      )
269 270 271 272 273 274 275 276 277
                    )
                  ),
                ]
              );
            }
          ),
        ]
      )
    );
278
    (key.currentState as dynamic).ensureTooltipVisible(); // before using "as dynamic" in your code, see note top of file
279
    await tester.pump(const Duration(seconds: 2)); // faded in, show timer started (and at 0.0)
Hixie's avatar
Hixie committed
280

281 282 283 284 285
    /********************* 800x600 screen
     *                   *
     *                   *
     *         o         * y=300.0
     *        _|_        * }-100.0 vertical offset
286 287
     *       |___|       * }-190.0 height
     *                   * }- 10.0 margin
288
     *********************/
Hixie's avatar
Hixie committed
289

290
    RenderBox tip = tester.renderObject(find.text(tooltipText)).parent;
291
    expect(tip.size.height, equals(190.0));
292
    expect(tip.localToGlobal(tip.size.topLeft(Point.origin)).y, equals(400.0));
293
    expect(tip.localToGlobal(tip.size.bottomRight(Point.origin)).y, equals(590.0));
Hixie's avatar
Hixie committed
294 295
  });

296
  testWidgets('Does tooltip end up in the right place - way off to the right', (WidgetTester tester) async {
297
    GlobalKey key = new GlobalKey();
298
    await tester.pumpWidget(
299 300 301 302 303 304 305 306 307 308 309
      new Overlay(
        initialEntries: <OverlayEntry>[
          new OverlayEntry(
            builder: (BuildContext context) {
              return new Stack(
                children: <Widget>[
                  new Positioned(
                    left: 1600.0,
                    top: 300.0,
                    child: new Tooltip(
                      key: key,
310
                      message: tooltipText,
311 312 313 314 315 316 317
                      height: 10.0,
                      padding: const EdgeInsets.all(0.0),
                      verticalOffset: 10.0,
                      preferBelow: true,
                      child: new Container(
                        width: 0.0,
                        height: 0.0
Hixie's avatar
Hixie committed
318
                      )
319 320 321 322 323 324 325 326 327
                    )
                  ),
                ]
              );
            }
          ),
        ]
      )
    );
328
    (key.currentState as dynamic).ensureTooltipVisible(); // before using "as dynamic" in your code, see note top of file
329
    await tester.pump(const Duration(seconds: 2)); // faded in, show timer started (and at 0.0)
Hixie's avatar
Hixie committed
330

331 332 333 334 335 336 337 338 339
    /********************* 800x600 screen
     *                   *
     *                   *
     *                   * y=300.0;   target -->   o
     *              ___| * }-10.0 vertical offset
     *             |___| * }-10.0 height
     *                   *
     *                   * }-10.0 margin
     *********************/
Hixie's avatar
Hixie committed
340

341
    RenderBox tip = tester.renderObject(find.text(tooltipText)).parent;
342 343 344 345
    expect(tip.size.height, equals(10.0));
    expect(tip.localToGlobal(tip.size.topLeft(Point.origin)).y, equals(310.0));
    expect(tip.localToGlobal(tip.size.bottomRight(Point.origin)).x, equals(790.0));
    expect(tip.localToGlobal(tip.size.bottomRight(Point.origin)).y, equals(320.0));
Hixie's avatar
Hixie committed
346 347
  });

348
  testWidgets('Does tooltip end up in the right place - near the edge', (WidgetTester tester) async {
349
    GlobalKey key = new GlobalKey();
350
    await tester.pumpWidget(
351 352 353 354 355 356 357 358 359 360 361
      new Overlay(
        initialEntries: <OverlayEntry>[
          new OverlayEntry(
            builder: (BuildContext context) {
              return new Stack(
                children: <Widget>[
                  new Positioned(
                    left: 780.0,
                    top: 300.0,
                    child: new Tooltip(
                      key: key,
362
                      message: tooltipText,
363 364 365 366 367 368 369
                      height: 10.0,
                      padding: const EdgeInsets.all(0.0),
                      verticalOffset: 10.0,
                      preferBelow: true,
                      child: new Container(
                        width: 0.0,
                        height: 0.0
Hixie's avatar
Hixie committed
370
                      )
371 372 373 374 375 376 377 378 379
                    )
                  ),
                ]
              );
            }
          ),
        ]
      )
    );
380
    (key.currentState as dynamic).ensureTooltipVisible(); // before using "as dynamic" in your code, see note top of file
381
    await tester.pump(const Duration(seconds: 2)); // faded in, show timer started (and at 0.0)
Hixie's avatar
Hixie committed
382

383 384 385 386 387 388 389 390 391
    /********************* 800x600 screen
     *                   *
     *                   *
     *                o  * y=300.0
     *              __|  * }-10.0 vertical offset
     *             |___| * }-10.0 height
     *                   *
     *                   * }-10.0 margin
     *********************/
Hixie's avatar
Hixie committed
392

393
    RenderBox tip = tester.renderObject(find.text(tooltipText)).parent;
394 395 396 397
    expect(tip.size.height, equals(10.0));
    expect(tip.localToGlobal(tip.size.topLeft(Point.origin)).y, equals(310.0));
    expect(tip.localToGlobal(tip.size.bottomRight(Point.origin)).x, equals(790.0));
    expect(tip.localToGlobal(tip.size.bottomRight(Point.origin)).y, equals(320.0));
Hixie's avatar
Hixie committed
398
  });
Hixie's avatar
Hixie committed
399

400 401 402 403 404
  testWidgets('Tooltip stays around', (WidgetTester tester) async {
    await tester.pumpWidget(
      new MaterialApp(
        home: new Center(
          child: new Tooltip(
405
            message: tooltipText,
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
            child: new Container(
              width: 100.0,
              height: 100.0,
              decoration: new BoxDecoration(
                backgroundColor: Colors.green[500]
              )
            )
          )
        )
      )
    );

    Finder tooltip = find.byType(Tooltip);
    TestGesture gesture = await tester.startGesture(tester.getCenter(tooltip));
    await tester.pump(kLongPressTimeout);
    await tester.pump(const Duration(milliseconds: 10));
    await gesture.up();
423
    expect(find.text(tooltipText), findsOneWidget);
424 425 426 427 428
    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));
429
    expect(find.text(tooltipText), findsNothing);
430
    await tester.pump(kLongPressTimeout);
431
    expect(find.text(tooltipText), findsOneWidget);
432
    await tester.pump(kLongPressTimeout);
433
    expect(find.text(tooltipText), findsOneWidget);
434 435 436
    gesture.up();
  });

437
  testWidgets('Does tooltip contribute semantics', (WidgetTester tester) async {
438 439
    SemanticsTester semantics = new SemanticsTester(tester);

440
    GlobalKey key = new GlobalKey();
441
    await tester.pumpWidget(
442 443 444 445 446 447 448 449 450 451 452
      new Overlay(
        initialEntries: <OverlayEntry>[
          new OverlayEntry(
            builder: (BuildContext context) {
              return new Stack(
                children: <Widget>[
                  new Positioned(
                    left: 780.0,
                    top: 300.0,
                    child: new Tooltip(
                      key: key,
453
                      message: tooltipText,
454 455 456 457 458 459 460 461 462 463
                      child: new Container(width: 0.0, height: 0.0)
                    )
                  ),
                ]
              );
            }
          ),
        ]
      )
    );
464

465
    expect(semantics, hasSemantics(new TestSemantics(id: 0, label: tooltipText)));
Hixie's avatar
Hixie committed
466

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

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

472
    expect(semantics, hasSemantics(new TestSemantics(id: 0, label: tooltipText)));
473 474

    semantics.dispose();
Hixie's avatar
Hixie committed
475
  });
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507

  testWidgets('Tooltip overlay does not update', (WidgetTester tester) async {
    Widget buildApp(String text) {
      return new MaterialApp(
        home: new Center(
          child: new Tooltip(
            message: text,
            child: new Container(
              width: 100.0,
              height: 100.0,
              decoration: new BoxDecoration(
                backgroundColor: Colors.green[500]
              )
            )
          )
        )
      );
    }

    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);
    await tester.tapAt(const Point(5.0, 5.0));
    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);
  });

Hixie's avatar
Hixie committed
508
}