hyperlink_test.dart 1.95 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
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
9 10

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

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

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

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

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

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

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

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

58
    didTapLeft = false;
59

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

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

65
    didTapLeft = false;
66

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

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