hyperlink_test.dart 1.86 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

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

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

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

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

25
    await tester.pumpWidget(
26 27
      Center(
        child: RichText(
28
          key: textKey,
Ian Hickson's avatar
Ian Hickson committed
29
          textDirection: TextDirection.ltr,
30
          text: TextSpan(
31
            children: <TextSpan>[
32
              TextSpan(
33
                text: 'xxxxxxxx',
34
                recognizer: tapLeft,
35
              ),
36
              const TextSpan(text: 'yyyyyyyy'),
37
              TextSpan(
38
                text: 'zzzzzzzzz',
39
                recognizer: tapRight,
40
              ),
41
            ],
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(Offset(box.size.width, 0.0)) + const Offset(-2.0, 2.0));
67

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