|
| 1 | +// Copyright 2019 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:html' as html; |
| 6 | +import 'dart:js_util'; |
| 7 | +import 'package:flutter/widgets.dart'; |
| 8 | +import 'package:flutter_test/flutter_test.dart'; |
| 9 | +import 'package:url_launcher_platform_interface/link.dart'; |
| 10 | +import 'package:url_launcher_web/src/link.dart'; |
| 11 | +import 'package:integration_test/integration_test.dart'; |
| 12 | + |
| 13 | +void main() { |
| 14 | + IntegrationTestWidgetsFlutterBinding.ensureInitialized(); |
| 15 | + |
| 16 | + group('Link Widget', () { |
| 17 | + testWidgets('creates anchor with correct attributes', |
| 18 | + (WidgetTester tester) async { |
| 19 | + final Uri uri = Uri.parse('http://foobar/example?q=1'); |
| 20 | + await tester.pumpWidget(Directionality( |
| 21 | + textDirection: TextDirection.ltr, |
| 22 | + child: WebLinkDelegate(TestLinkInfo( |
| 23 | + uri: uri, |
| 24 | + target: LinkTarget.blank, |
| 25 | + builder: (BuildContext context, FollowLink? followLink) { |
| 26 | + return Container(width: 100, height: 100); |
| 27 | + }, |
| 28 | + )), |
| 29 | + )); |
| 30 | + // Platform view creation happens asynchronously. |
| 31 | + await tester.pumpAndSettle(); |
| 32 | + |
| 33 | + final html.Element anchor = _findSingleAnchor(); |
| 34 | + expect(anchor.getAttribute('href'), uri.toString()); |
| 35 | + expect(anchor.getAttribute('target'), '_blank'); |
| 36 | + |
| 37 | + final Uri uri2 = Uri.parse('http://foobar2/example?q=2'); |
| 38 | + await tester.pumpWidget(Directionality( |
| 39 | + textDirection: TextDirection.ltr, |
| 40 | + child: WebLinkDelegate(TestLinkInfo( |
| 41 | + uri: uri2, |
| 42 | + target: LinkTarget.self, |
| 43 | + builder: (BuildContext context, FollowLink? followLink) { |
| 44 | + return Container(width: 100, height: 100); |
| 45 | + }, |
| 46 | + )), |
| 47 | + )); |
| 48 | + await tester.pumpAndSettle(); |
| 49 | + |
| 50 | + // Check that the same anchor has been updated. |
| 51 | + expect(anchor.getAttribute('href'), uri2.toString()); |
| 52 | + expect(anchor.getAttribute('target'), '_self'); |
| 53 | + }); |
| 54 | + |
| 55 | + testWidgets('sizes itself correctly', (WidgetTester tester) async { |
| 56 | + final Key containerKey = GlobalKey(); |
| 57 | + final Uri uri = Uri.parse('http://foobar'); |
| 58 | + await tester.pumpWidget(Directionality( |
| 59 | + textDirection: TextDirection.ltr, |
| 60 | + child: Center( |
| 61 | + child: ConstrainedBox( |
| 62 | + constraints: BoxConstraints.tight(Size(100.0, 100.0)), |
| 63 | + child: WebLinkDelegate(TestLinkInfo( |
| 64 | + uri: uri, |
| 65 | + target: LinkTarget.blank, |
| 66 | + builder: (BuildContext context, FollowLink? followLink) { |
| 67 | + return Container( |
| 68 | + key: containerKey, |
| 69 | + child: SizedBox(width: 50.0, height: 50.0), |
| 70 | + ); |
| 71 | + }, |
| 72 | + )), |
| 73 | + ), |
| 74 | + ), |
| 75 | + )); |
| 76 | + await tester.pumpAndSettle(); |
| 77 | + |
| 78 | + final Size containerSize = tester.getSize(find.byKey(containerKey)); |
| 79 | + // The Stack widget inserted by the `WebLinkDelegate` shouldn't loosen the |
| 80 | + // constraints before passing them to the inner container. So the inner |
| 81 | + // container should respect the tight constraints given by the ancestor |
| 82 | + // `ConstrainedBox` widget. |
| 83 | + expect(containerSize.width, 100.0); |
| 84 | + expect(containerSize.height, 100.0); |
| 85 | + }); |
| 86 | + |
| 87 | + // See: https://github.com/flutter/plugins/pull/3522#discussion_r574703724 |
| 88 | + testWidgets('uri can be null', (WidgetTester tester) async { |
| 89 | + await tester.pumpWidget(Directionality( |
| 90 | + textDirection: TextDirection.ltr, |
| 91 | + child: WebLinkDelegate(TestLinkInfo( |
| 92 | + uri: null, |
| 93 | + target: LinkTarget.defaultTarget, |
| 94 | + builder: (BuildContext context, FollowLink? followLink) { |
| 95 | + return Container(width: 100, height: 100); |
| 96 | + }, |
| 97 | + )), |
| 98 | + )); |
| 99 | + // Platform view creation happens asynchronously. |
| 100 | + await tester.pumpAndSettle(); |
| 101 | + |
| 102 | + final html.Element anchor = _findSingleAnchor(); |
| 103 | + expect(anchor.hasAttribute('href'), false); |
| 104 | + }); |
| 105 | + }); |
| 106 | +} |
| 107 | + |
| 108 | +html.Element _findSingleAnchor() { |
| 109 | + final List<html.Element> foundAnchors = <html.Element>[]; |
| 110 | + for (final html.Element anchor in html.document.querySelectorAll('a')) { |
| 111 | + if (hasProperty(anchor, linkViewIdProperty)) { |
| 112 | + foundAnchors.add(anchor); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + // Search inside platform views with shadow roots as well. |
| 117 | + for (final html.Element platformView |
| 118 | + in html.document.querySelectorAll('flt-platform-view')) { |
| 119 | + final html.ShadowRoot shadowRoot = platformView.shadowRoot!; |
| 120 | + if (shadowRoot != null) { |
| 121 | + for (final html.Element anchor in shadowRoot.querySelectorAll('a')) { |
| 122 | + if (hasProperty(anchor, linkViewIdProperty)) { |
| 123 | + foundAnchors.add(anchor); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + return foundAnchors.single; |
| 130 | +} |
| 131 | + |
| 132 | +class TestLinkInfo extends LinkInfo { |
| 133 | + @override |
| 134 | + final LinkWidgetBuilder builder; |
| 135 | + |
| 136 | + @override |
| 137 | + final Uri? uri; |
| 138 | + |
| 139 | + @override |
| 140 | + final LinkTarget target; |
| 141 | + |
| 142 | + @override |
| 143 | + bool get isDisabled => uri == null; |
| 144 | + |
| 145 | + TestLinkInfo({ |
| 146 | + required this.uri, |
| 147 | + required this.target, |
| 148 | + required this.builder, |
| 149 | + }); |
| 150 | +} |
0 commit comments