hyperlink_test.dart 1.89 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
    bool didTapLeft = false;
12
    final TapGestureRecognizer tapLeft = new TapGestureRecognizer()
13 14 15
      ..onTap = () {
        didTapLeft = true;
      };
16

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

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

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

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

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

52
    await tester.tapAt(box.localToGlobal(Offset.zero) + const Offset(2.0, 2.0));
53

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

57
    didTapLeft = false;
58

59
    await tester.tapAt(box.localToGlobal(Offset.zero) + const Offset(30.0, 2.0));
60

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

64
    didTapLeft = false;
65

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

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