-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_sharing_logic.dart
More file actions
161 lines (140 loc) · 4.51 KB
/
test_sharing_logic.dart
File metadata and controls
161 lines (140 loc) · 4.51 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
void main() {
testLogic("Hello world", "Text only");
testLogic("https://google.com", "Pure URL");
testLogic("Check this https://google.com out", "Text with URL");
testLogic("Check this www.google.com out", "Text with www URL");
testLogic(" https://google.com ", "URL with spaces");
}
void testLogic(String content, String scenario) {
print("\n--- Testing: $scenario ---");
print("Input: '$content'");
final trimmedContent = content.trim();
String? urlToLoad;
// Logic from UnifiedSharingService.handleSharedContent
if (isUrl(trimmedContent)) {
print("MATCH: isUrl() returned true. Opening directly.");
return;
}
if (isPotentialUrl(trimmedContent)) {
print("MATCH: isPotentialUrl() returned true.");
// Logic from lines 178-185
final uri = Uri.tryParse(trimmedContent);
if (uri != null && (uri.scheme == 'http' || uri.scheme == 'https')) {
urlToLoad = trimmedContent;
print(" -> Valid URI with scheme. urlToLoad = $urlToLoad");
} else {
urlToLoad = trimmedContent;
print(" -> Potential URL without scheme. urlToLoad = $urlToLoad");
}
} else {
print("NO MATCH: isPotentialUrl() returned false.");
// Check if there's a URL inside the text
final urlPattern = RegExp(r'https?:\/\/[^\s]+');
final match = urlPattern.firstMatch(content);
if (match != null) {
urlToLoad = match.group(0);
print("MATCH: Regex found URL: $urlToLoad");
} else {
print("NO MATCH: Regex found nothing.");
}
}
if (urlToLoad != null) {
print("RESULT: Should show Dialog with URL: $urlToLoad");
} else {
print("RESULT: Should load as Text File.");
}
}
// Logic copied from UnifiedSharingService
bool isUrl(String text) {
final trimmedText = text.trim();
if (trimmedText.isEmpty) {
return false;
}
// Remove quotes if present
final cleanText = trimmedText.startsWith('"') && trimmedText.endsWith('"')
? trimmedText.substring(1, trimmedText.length - 1)
: (trimmedText.startsWith("'") && trimmedText.endsWith("'")
? trimmedText.substring(1, trimmedText.length - 1)
: trimmedText);
// First, check if this is explicitly a file URL (file:// protocol)
if (cleanText.startsWith('file://') || cleanText.startsWith('file///')) {
return false;
}
// Check if this is an Android content:// URI - these should be treated as file shares
if (cleanText.startsWith('content://')) {
return false;
}
// Check if this is likely a file path (starts with /) - do this early to avoid false positives
if (cleanText.startsWith('/')) {
return false;
}
// Try to parse as URI - check if it's a valid HTTP/HTTPS URL first
try {
final uri = Uri.tryParse(cleanText);
if (uri != null && (uri.scheme == 'http' || uri.scheme == 'https')) {
return true;
}
} catch (e) {
// If parsing fails, continue with other checks
}
return false;
}
bool isPotentialUrl(String text) {
if (text.isEmpty) return false;
final trimmed = text.trim();
// Remove common URL wrappers
final cleanText = trimmed
.replaceAll('<', '')
.replaceAll('>', '')
.replaceAll('"', '')
.replaceAll("'", '');
// Check for common URL patterns
try {
final uri = Uri.tryParse(cleanText);
if (uri != null) {
// Valid URL with http/https scheme
if (uri.scheme == 'http' || uri.scheme == 'https') {
return true;
}
// Valid URL that might be missing scheme (www.example.com)
if (uri.host.isNotEmpty && !uri.host.contains(' ')) {
return true;
}
}
} catch (e) {
// Parsing failed, try simpler patterns
}
// Check for common URL patterns without scheme
final urlPatterns = [
r'www\.',
r'http://',
r'https://',
r'\.com',
r'\.org',
r'\.net',
r'\.io',
r'\.co',
r'\.app',
r'\.dev',
];
for (final pattern in urlPatterns) {
if (cleanText.contains(RegExp(pattern, caseSensitive: false))) {
// Additional checks to avoid false positives
if (cleanText.contains(' ') && !cleanText.startsWith('http')) {
// Contains spaces but doesn't start with http - might not be a URL
continue;
}
return true;
}
}
// Check for common URL structures
if ((cleanText.contains('.') && cleanText.contains('/')) ||
(cleanText.contains('.') && cleanText.length > 10)) {
// Might be a URL, but do additional checks
final hasInvalidChars = RegExp(r'[\s\n\r\t]').hasMatch(cleanText);
if (!hasInvalidChars) {
return true;
}
}
return false;
}