forked from zulip/zulip-flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent_test.dart
More file actions
162 lines (134 loc) · 6.64 KB
/
Copy pathcontent_test.dart
File metadata and controls
162 lines (134 loc) · 6.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import 'dart:io';
import 'package:checks/checks.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:zulip/api/core.dart';
import 'package:zulip/model/content.dart';
import 'package:zulip/widgets/content.dart';
import 'package:zulip/widgets/store.dart';
import '../example_data.dart' as eg;
import '../model/binding.dart';
import '../test_images.dart';
import 'dialog_checks.dart';
void main() {
TestZulipBinding.ensureInitialized();
group('LinkNode interactions', () {
const expectedModeAndroid = LaunchMode.externalApplication;
// The Flutter test font uses square glyphs, so width equals height:
// https://github.com/flutter/flutter/wiki/Flutter-Test-Fonts
const fontSize = 48.0;
Future<void> prepareContent(WidgetTester tester, String html) async {
await testBinding.globalStore.add(eg.selfAccount, eg.initialSnapshot());
addTearDown(testBinding.reset);
await tester.pumpWidget(GlobalStoreWidget(child: MaterialApp(
home: PerAccountStoreWidget(accountId: eg.selfAccount.id,
child: BlockContentList(
nodes: parseContent(html).nodes)))));
await tester.pump();
await tester.pump();
}
testWidgets('can tap a link to open URL', (tester) async {
await prepareContent(tester,
'<p><a href="https://example/">hello</a></p>');
await tester.tap(find.text('hello'));
final expectedMode = defaultTargetPlatform == TargetPlatform.android ?
LaunchMode.externalApplication : LaunchMode.platformDefault;
check(testBinding.takeLaunchUrlCalls())
.single.equals((url: Uri.parse('https://example/'), mode: expectedMode));
}, variant: const TargetPlatformVariant({TargetPlatform.android, TargetPlatform.iOS}));
testWidgets('multiple links in paragraph', (tester) async {
await prepareContent(tester,
'<p><a href="https://a/">foo</a> bar <a href="https://b/">baz</a></p>');
final base = tester.getTopLeft(find.text('foo bar baz'))
.translate(fontSize/2, fontSize/2); // middle of first letter
await tester.tapAt(base.translate(5*fontSize, 0)); // "foo bXr baz"
check(testBinding.takeLaunchUrlCalls()).isEmpty();
await tester.tapAt(base.translate(1*fontSize, 0)); // "fXo bar baz"
check(testBinding.takeLaunchUrlCalls())
.single.equals((url: Uri.parse('https://a/'), mode: expectedModeAndroid));
await tester.tapAt(base.translate(9*fontSize, 0)); // "foo bar bXz"
check(testBinding.takeLaunchUrlCalls())
.single.equals((url: Uri.parse('https://b/'), mode: expectedModeAndroid));
});
testWidgets('link nested in other spans', (tester) async {
await prepareContent(tester,
'<p><strong><em><a href="https://a/">word</a></em></strong></p>');
await tester.tap(find.text('word'));
check(testBinding.takeLaunchUrlCalls())
.single.equals((url: Uri.parse('https://a/'), mode: expectedModeAndroid));
});
testWidgets('link containing other spans', (tester) async {
await prepareContent(tester,
'<p><a href="https://a/">two <strong><em><code>words</code></em></strong></a></p>');
final base = tester.getTopLeft(find.text('two words'))
.translate(fontSize/2, fontSize/2); // middle of first letter
await tester.tapAt(base.translate(1*fontSize, 0)); // "tXo words"
check(testBinding.takeLaunchUrlCalls())
.single.equals((url: Uri.parse('https://a/'), mode: expectedModeAndroid));
await tester.tapAt(base.translate(6*fontSize, 0)); // "two woXds"
check(testBinding.takeLaunchUrlCalls())
.single.equals((url: Uri.parse('https://a/'), mode: expectedModeAndroid));
});
testWidgets('relative links are resolved', (tester) async {
await prepareContent(tester,
'<p><a href="/a/b?c#d">word</a></p>');
await tester.tap(find.text('word'));
check(testBinding.takeLaunchUrlCalls())
.single.equals((url: Uri.parse('${eg.realmUrl}a/b?c#d'), mode: expectedModeAndroid));
});
testWidgets('link inside HeadingNode', (tester) async {
await prepareContent(tester,
'<h6><a href="https://a/">word</a></h6>');
await tester.tap(find.text('word'));
check(testBinding.takeLaunchUrlCalls())
.single.equals((url: Uri.parse('https://a/'), mode: expectedModeAndroid));
});
testWidgets('error dialog if invalid link', (tester) async {
await prepareContent(tester,
'<p><a href="file:///etc/bad">word</a></p>');
testBinding.launchUrlResult = false;
await tester.tap(find.text('word'));
await tester.pump();
check(testBinding.takeLaunchUrlCalls())
.single.equals((url: Uri.parse('file:///etc/bad'), mode: expectedModeAndroid));
checkErrorDialog(tester, expectedTitle: 'Unable to open link');
});
});
group('RealmContentNetworkImage', () {
final authHeaders = authHeader(email: eg.selfAccount.email, apiKey: eg.selfAccount.apiKey);
Future<String?> actualAuthHeader(WidgetTester tester, Uri src) async {
await testBinding.globalStore.add(eg.selfAccount, eg.initialSnapshot());
addTearDown(testBinding.reset);
final httpClient = FakeImageHttpClient();
debugNetworkImageHttpClientProvider = () => httpClient;
httpClient.request.response
..statusCode = HttpStatus.ok
..content = kSolidBlueAvatar;
await tester.pumpWidget(GlobalStoreWidget(
child: PerAccountStoreWidget(accountId: eg.selfAccount.id,
child: RealmContentNetworkImage(src))));
await tester.pump();
await tester.pump();
final headers = httpClient.request.headers.values;
check(authHeaders.keys).deepEquals(['Authorization']);
return headers['Authorization']?.single;
}
testWidgets('includes auth header if `src` on-realm', (tester) async {
check(await actualAuthHeader(tester, Uri.parse('https://chat.example/image.png')))
.isNotNull().equals(authHeaders['Authorization']!);
debugNetworkImageHttpClientProvider = null;
});
testWidgets('excludes auth header if `src` off-realm', (tester) async {
check(await actualAuthHeader(tester, Uri.parse('https://other.example/image.png')))
.isNull();
debugNetworkImageHttpClientProvider = null;
});
testWidgets('throws if no `PerAccountStoreWidget` ancestor', (WidgetTester tester) async {
await tester.pumpWidget(
RealmContentNetworkImage(Uri.parse('https://zulip.invalid/path/to/image.png'), filterQuality: FilterQuality.medium));
check(tester.takeException()).isA<AssertionError>();
});
});
}