hyperlink_test.dart 1.82 KB
Newer Older
1 2 3 4 5 6 7 8 9
// 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.

import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/widgets.dart';

void main() {
10
  testWidgets('Can tap a hyperlink', (WidgetTester tester) async {
11 12 13 14 15
    bool didTapLeft = false;
    TapGestureRecognizer tapLeft = new TapGestureRecognizer()
      ..onTap = () {
        didTapLeft = true;
      };
16

17 18 19 20 21
    bool didTapRight = false;
    TapGestureRecognizer tapRight = new TapGestureRecognizer()
      ..onTap = () {
        didTapRight = true;
      };
22

23
    Key textKey = new Key('text');
24

25
    await tester.pumpWidget(
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
      new Center(
        child: new RichText(
          key: textKey,
          text: new TextSpan(
            children: <TextSpan>[
              new TextSpan(
                text: 'xxxxxxxx',
                recognizer: tapLeft
              ),
              new TextSpan(text: 'yyyyyyyy'),
              new TextSpan(
                text: 'zzzzzzzzz',
                recognizer: tapRight
              ),
            ]
41 42
          )
        )
43 44
      )
    );
45

46
    RenderBox box = tester.renderObject(find.byKey(textKey));
47

48 49
    expect(didTapLeft, isFalse);
    expect(didTapRight, isFalse);
50

51
    await tester.tapAt(box.localToGlobal(Point.origin) + new Offset(2.0, 2.0));
52

53 54
    expect(didTapLeft, isTrue);
    expect(didTapRight, isFalse);
55

56
    didTapLeft = false;
57

58
    await tester.tapAt(box.localToGlobal(Point.origin) + new Offset(30.0, 2.0));
59

60 61
    expect(didTapLeft, isTrue);
    expect(didTapRight, isFalse);
62

63
    didTapLeft = false;
64

65
    await tester.tapAt(box.localToGlobal(new Point(box.size.width, 0.0)) + new Offset(-2.0, 2.0));
66

67 68
    expect(didTapLeft, isFalse);
    expect(didTapRight, isTrue);
69 70
  });
}