hyperlink_test.dart 1.87 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
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
// @dart = 2.8

7 8 9 10 11
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/widgets.dart';

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

19
    bool didTapRight = false;
20
    final TapGestureRecognizer tapRight = TapGestureRecognizer()
21 22 23
      ..onTap = () {
        didTapRight = true;
      };
24

25
    const Key textKey = Key('text');
26

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

49
    final RenderBox box = tester.renderObject(find.byKey(textKey));
50

51 52
    expect(didTapLeft, isFalse);
    expect(didTapRight, isFalse);
53

54
    await tester.tapAt(box.localToGlobal(Offset.zero) + const Offset(2.0, 2.0));
55

56 57
    expect(didTapLeft, isTrue);
    expect(didTapRight, isFalse);
58

59
    didTapLeft = false;
60

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

63 64
    expect(didTapLeft, isTrue);
    expect(didTapRight, isFalse);
65

66
    didTapLeft = false;
67

68
    await tester.tapAt(box.localToGlobal(Offset(box.size.width, 0.0)) + const Offset(-2.0, 2.0));
69

70 71
    expect(didTapLeft, isFalse);
    expect(didTapRight, isTrue);
72 73
  });
}