forked from miguelpruivo/flutter_file_picker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_picker_windows_test.dart
174 lines (153 loc) · 6.53 KB
/
file_picker_windows_test.dart
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
163
164
165
166
167
168
169
170
171
172
173
174
@TestOn('windows')
import 'dart:ffi';
import 'package:ffi/ffi.dart';
import 'package:file_picker/src/exceptions.dart';
import 'package:file_picker/src/file_picker.dart';
import 'package:file_picker/src/windows/file_picker_windows.dart';
import 'package:file_picker/src/windows/file_picker_windows_ffi_types.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
group('fileTypeToFileFilter()', () {
test('should return the file filter', () {
final picker = FilePickerWindows();
expect(
picker.fileTypeToFileFilter(FileType.any, null),
equals('All Files (*.*)\x00*.*\x00\x00'),
);
expect(
picker.fileTypeToFileFilter(FileType.audio, null),
equals(
'Audios (*.aac,*.midi,*.mp3,*.ogg,*.wav)\x00*.aac;*.midi;*.mp3;*.ogg;*.wav\x00\x00'),
);
expect(
picker.fileTypeToFileFilter(FileType.image, null),
equals(
'Images (*.bmp,*.gif,*.jpeg,*.jpg,*.png)\x00*.bmp;*.gif;*.jpeg;*.jpg;*.png\x00\x00',
),
);
expect(
picker.fileTypeToFileFilter(FileType.media, null),
equals(
'Videos (*.avi,*.flv,*.mkv,*.mov,*.mp4,*.mpeg,*.webm,*.wmv)\x00*.avi;*.flv;*.mkv;*.mov;*.mp4;*.mpeg;*.webm;*.wmv\x00Images (*.bmp,*.gif,*.jpeg,*.jpg,*.png)\x00*.bmp;*.gif;*.jpeg;*.jpg;*.png\x00\x00',
),
);
expect(
picker.fileTypeToFileFilter(FileType.video, null),
equals(
'Videos (*.avi,*.flv,*.mkv,*.mov,*.mp4,*.mpeg,*.webm,*.wmv)\x00*.avi;*.flv;*.mkv;*.mov;*.mp4;*.mpeg;*.webm;*.wmv\x00\x00',
),
);
});
test(
'should return the file filter when given a list of custom file extensions',
() {
final picker = FilePickerWindows();
expect(
picker.fileTypeToFileFilter(FileType.custom, ['dart']),
equals('Files (*.dart)\x00*.dart\x00\x00'),
);
expect(
picker.fileTypeToFileFilter(FileType.custom, ['dart', 'html']),
equals('Files (*.dart,*.html)\x00*.dart;*.html\x00\x00'),
);
});
});
group('validateFileName()', () {
test('should throw an exception if the file name contains a < (less than)',
() {
expect(() => FilePickerWindows().validateFileName('file with < .txt'),
throwsA(TypeMatcher<IllegalCharacterInFileNameException>()));
});
test(
'should throw an exception if the file name contains a > (greater than)',
() {
expect(() => FilePickerWindows().validateFileName('file>.csv'),
throwsA(TypeMatcher<IllegalCharacterInFileNameException>()));
});
test('should throw an exception if the file name contains a : (colon)', () {
expect(() => FilePickerWindows().validateFileName('fi:le.csv'),
throwsA(TypeMatcher<IllegalCharacterInFileNameException>()));
});
test(
'should throw an exception if the file name contains a " (double quote)',
() {
expect(() => FilePickerWindows().validateFileName('"output.csv'),
throwsA(TypeMatcher<IllegalCharacterInFileNameException>()));
});
test(
'should throw an exception if the file name contains a / (forward slash)',
() {
expect(() => FilePickerWindows().validateFileName('my-output/-file.csv'),
throwsA(TypeMatcher<IllegalCharacterInFileNameException>()));
});
test('should throw an exception if the file name contains a \\ (backslash)',
() {
expect(
() =>
FilePickerWindows().validateFileName('invalid-\\-file-name.csv'),
throwsA(TypeMatcher<IllegalCharacterInFileNameException>()));
});
test('should throw an exception if the file name contains a | (pipe)', () {
expect(() => FilePickerWindows().validateFileName('download|.pdf'),
throwsA(TypeMatcher<IllegalCharacterInFileNameException>()));
});
test(
'should throw an exception if the file name contains a ? (question mark)',
() {
expect(() => FilePickerWindows().validateFileName('bill?-2021-12-18.pdf'),
throwsA(TypeMatcher<IllegalCharacterInFileNameException>()));
});
test('should throw an exception if the file name contains a * (asterisk)',
() {
expect(() => FilePickerWindows().validateFileName('*.txt'),
throwsA(TypeMatcher<IllegalCharacterInFileNameException>()));
});
test('should return normally given a valid file name', () {
expect(
() => FilePickerWindows()
.validateFileName('0123456789,;.-_+#\'äöüß!§\$%&(){}[]=`´.txt'),
returnsNormally);
});
});
group('extractSelectedFilesFromOpenFileNameW', () {
test('should parse the result of picking a single file', () {
final Pointer<OPENFILENAMEW> openFileNameW = calloc<OPENFILENAMEW>();
openFileNameW.ref.lpstrFile =
"C:\\Program Files\\Sublime Text 3\\changelog.txt\x00\x00"
.toNativeUtf16();
final filePaths = FilePickerWindows()
.extractSelectedFilesFromOpenFileNameW(openFileNameW.ref);
expect(filePaths, hasLength(1));
expect(filePaths[0],
equals("C:\\Program Files\\Sublime Text 3\\changelog.txt"));
});
test('should parse the result of picking multiple files', () {
final Pointer<OPENFILENAMEW> openFileNameW = calloc<OPENFILENAMEW>();
openFileNameW.ref.lpstrFile =
"C:\\Users\\Jane\x00file1.jpg\x00file2.pdf\x00file3.docx\x00\x00"
.toNativeUtf16();
final filePaths = FilePickerWindows()
.extractSelectedFilesFromOpenFileNameW(openFileNameW.ref);
expect(filePaths, hasLength(3));
expect(filePaths[0], equals("C:\\Users\\Jane\\file1.jpg"));
expect(filePaths[1], equals("C:\\Users\\Jane\\file2.pdf"));
expect(filePaths[2], equals("C:\\Users\\Jane\\file3.docx"));
});
});
/** See https://github.com/miguelpruivo/flutter_file_picker/issues/1257 for reconstructing this special case. */
test(
'should parse the result of the save-file dialog when the dialog was instantiated with a long '
'default file name that is contained - in parts - in the result separated by only one null character',
() {
final Pointer<OPENFILENAMEW> x = calloc<OPENFILENAMEW>();
x.ref.lpstrFile =
"C:\\Users\\John\\Desktop\\test.txt\x00qrstuvwxyz0123456789.png\x00\x00"
.toNativeUtf16();
final filePaths = FilePickerWindows().extractSelectedFilesFromOpenFileNameW(
x.ref,
isResultFromSaveFileDialog: true,
);
expect(filePaths, hasLength(1));
expect(filePaths[0], equals("C:\\Users\\John\\Desktop\\test.txt"));
});
}