3
3
// found in the LICENSE file.
4
4
5
5
import 'dart:convert' ;
6
- import 'dart:html' as html ;
6
+ import 'dart:js_interop' ;
7
7
import 'dart:typed_data' ;
8
8
9
9
import 'package:flutter_test/flutter_test.dart' ;
10
10
import 'package:image_picker_for_web/image_picker_for_web.dart' ;
11
11
import 'package:image_picker_platform_interface/image_picker_platform_interface.dart' ;
12
12
import 'package:integration_test/integration_test.dart' ;
13
+ import 'package:web/web.dart' as web;
13
14
14
15
const String expectedStringContents = 'Hello, world!' ;
15
16
const String otherStringContents = 'Hello again, world!' ;
16
17
final Uint8List bytes = const Utf8Encoder ().convert (expectedStringContents);
17
18
final Uint8List otherBytes = const Utf8Encoder ().convert (otherStringContents);
18
- final Map <String , dynamic > options = < String , dynamic > {
19
- 'type' : 'text/plain' ,
20
- 'lastModified' : DateTime .utc (2017 , 12 , 13 ).millisecondsSinceEpoch,
21
- };
22
- final html.File textFile = html.File (< Uint8List > [bytes], 'hello.txt' , options);
23
- final html.File secondTextFile =
24
- html.File (< Uint8List > [otherBytes], 'secondFile.txt' );
19
+ // TODO(dit): When web:0.6.0 lands, move `type` to the [web.FilePropertyBag] constructor.
20
+ // See: https://github.com/dart-lang/web/pull/197
21
+ final web.FilePropertyBag options = web.FilePropertyBag (
22
+ lastModified: DateTime .utc (2017 , 12 , 13 ).millisecondsSinceEpoch,
23
+ )..type = 'text/plain' ;
24
+
25
+ final web.File textFile =
26
+ web.File (< JSUint8Array > [bytes.toJS].toJS, 'hello.txt' , options);
27
+ final web.File secondTextFile =
28
+ web.File (< JSUint8Array > [otherBytes.toJS].toJS, 'secondFile.txt' );
25
29
26
30
void main () {
27
31
IntegrationTestWidgetsFlutterBinding .ensureInitialized ();
@@ -36,12 +40,12 @@ void main() {
36
40
testWidgets ('getImageFromSource can select a file' , (
37
41
WidgetTester _,
38
42
) async {
39
- final html. FileUploadInputElement mockInput = html. FileUploadInputElement ();
40
-
43
+ final web. HTMLInputElement mockInput = web. HTMLInputElement ()
44
+ ..type = 'file' ;
41
45
final ImagePickerPluginTestOverrides overrides =
42
46
ImagePickerPluginTestOverrides ()
43
47
..createInputElement = ((_, __) => mockInput)
44
- ..getMultipleFilesFromInput = ((_) => < html .File > [textFile]);
48
+ ..getMultipleFilesFromInput = ((_) => < web .File > [textFile]);
45
49
46
50
final ImagePickerPlugin plugin = ImagePickerPlugin (overrides: overrides);
47
51
@@ -50,11 +54,12 @@ void main() {
50
54
source: ImageSource .camera,
51
55
);
52
56
53
- expect (html.querySelector ('flt-image-picker-inputs' )? .children.isEmpty,
54
- isFalse);
57
+ expect (
58
+ web.document.querySelector ('flt-image-picker-inputs' )? .children.length,
59
+ isNonZero);
55
60
56
61
// Mock the browser behavior of selecting a file...
57
- mockInput.dispatchEvent (html .Event ('change' ));
62
+ mockInput.dispatchEvent (web .Event ('change' ));
58
63
59
64
// Now the file should be available
60
65
expect (image, completes);
@@ -69,30 +74,32 @@ void main() {
69
74
expect (
70
75
file.lastModified (),
71
76
completion (
72
- DateTime .fromMillisecondsSinceEpoch (textFile.lastModified! ),
77
+ DateTime .fromMillisecondsSinceEpoch (textFile.lastModified),
73
78
));
74
- expect (html.querySelector ('flt-image-picker-inputs' )? .children.isEmpty,
75
- isTrue);
79
+ expect (
80
+ web.document.querySelector ('flt-image-picker-inputs' )? .children.length,
81
+ isZero);
76
82
});
77
83
78
84
testWidgets ('getMultiImageWithOptions can select multiple files' , (
79
85
WidgetTester _,
80
86
) async {
81
- final html.FileUploadInputElement mockInput = html.FileUploadInputElement ();
87
+ final web.HTMLInputElement mockInput = web.HTMLInputElement ()
88
+ ..type = 'file' ;
82
89
83
90
final ImagePickerPluginTestOverrides overrides =
84
91
ImagePickerPluginTestOverrides ()
85
92
..createInputElement = ((_, __) => mockInput)
86
93
..getMultipleFilesFromInput =
87
- ((_) => < html .File > [textFile, secondTextFile]);
94
+ ((_) => < web .File > [textFile, secondTextFile]);
88
95
89
96
final ImagePickerPlugin plugin = ImagePickerPlugin (overrides: overrides);
90
97
91
98
// Init the pick file dialog...
92
99
final Future <List <XFile >> files = plugin.getMultiImageWithOptions ();
93
100
94
101
// Mock the browser behavior of selecting a file...
95
- mockInput.dispatchEvent (html .Event ('change' ));
102
+ mockInput.dispatchEvent (web .Event ('change' ));
96
103
97
104
// Now the file should be available
98
105
expect (files, completes);
@@ -108,13 +115,14 @@ void main() {
108
115
});
109
116
110
117
testWidgets ('getMedia can select multiple files' , (WidgetTester _) async {
111
- final html.FileUploadInputElement mockInput = html.FileUploadInputElement ();
118
+ final web.HTMLInputElement mockInput = web.HTMLInputElement ()
119
+ ..type = 'file' ;
112
120
113
121
final ImagePickerPluginTestOverrides overrides =
114
122
ImagePickerPluginTestOverrides ()
115
123
..createInputElement = ((_, __) => mockInput)
116
124
..getMultipleFilesFromInput =
117
- ((_) => < html .File > [textFile, secondTextFile]);
125
+ ((_) => < web .File > [textFile, secondTextFile]);
118
126
119
127
final ImagePickerPlugin plugin = ImagePickerPlugin (overrides: overrides);
120
128
@@ -123,7 +131,7 @@ void main() {
123
131
plugin.getMedia (options: const MediaOptions (allowMultiple: true ));
124
132
125
133
// Mock the browser behavior of selecting a file...
126
- mockInput.dispatchEvent (html .Event ('change' ));
134
+ mockInput.dispatchEvent (web .Event ('change' ));
127
135
128
136
// Now the file should be available
129
137
expect (files, completes);
@@ -139,20 +147,20 @@ void main() {
139
147
});
140
148
141
149
group ('cancel event' , () {
142
- late html. FileUploadInputElement mockInput;
150
+ late web. HTMLInputElement mockInput;
143
151
late ImagePickerPluginTestOverrides overrides;
144
152
late ImagePickerPlugin plugin;
145
153
146
154
setUp (() {
147
- mockInput = html. FileUploadInputElement () ;
155
+ mockInput = web. HTMLInputElement ()..type = 'file' ;
148
156
overrides = ImagePickerPluginTestOverrides ()
149
157
..createInputElement = ((_, __) => mockInput)
150
- ..getMultipleFilesFromInput = ((_) => < html .File > [textFile]);
158
+ ..getMultipleFilesFromInput = ((_) => < web .File > [textFile]);
151
159
plugin = ImagePickerPlugin (overrides: overrides);
152
160
});
153
161
154
162
void mockCancel () {
155
- mockInput.dispatchEvent (html .Event ('cancel' ));
163
+ mockInput.dispatchEvent (web .Event ('cancel' ));
156
164
}
157
165
158
166
testWidgets ('getFiles - returns empty list' , (WidgetTester _) async {
@@ -226,61 +234,61 @@ void main() {
226
234
227
235
group ('createInputElement' , () {
228
236
testWidgets ('accept: any, capture: null' , (WidgetTester tester) async {
229
- final html .Element input = plugin.createInputElement ('any' , null );
237
+ final web .Element input = plugin.createInputElement ('any' , null );
230
238
231
- expect (input.attributes, containsPair ('accept' , 'any' ) );
232
- expect (input.attributes, isNot ( contains ( 'capture' )) );
233
- expect (input.attributes, isNot ( contains ( 'multiple' )) );
239
+ expect (input.getAttribute ('accept' ) , 'any' );
240
+ expect (input.hasAttribute ( 'capture' ), false );
241
+ expect (input.hasAttribute ( 'multiple' ), false );
234
242
});
235
243
236
244
testWidgets ('accept: any, capture: something' , (WidgetTester tester) async {
237
- final html .Element input = plugin.createInputElement ('any' , 'something' );
245
+ final web .Element input = plugin.createInputElement ('any' , 'something' );
238
246
239
- expect (input.attributes, containsPair ('accept' , 'any' ) );
240
- expect (input.attributes, containsPair ('capture' , 'something' ) );
241
- expect (input.attributes, isNot ( contains ( 'multiple' )) );
247
+ expect (input.getAttribute ('accept' ) , 'any' );
248
+ expect (input.getAttribute ('capture' ) , 'something' );
249
+ expect (input.hasAttribute ( 'multiple' ), false );
242
250
});
243
251
244
252
testWidgets ('accept: any, capture: null, multi: true' ,
245
253
(WidgetTester tester) async {
246
- final html .Element input =
254
+ final web .Element input =
247
255
plugin.createInputElement ('any' , null , multiple: true );
248
256
249
- expect (input.attributes, containsPair ('accept' , 'any' ) );
250
- expect (input.attributes, isNot ( contains ( 'capture' )) );
251
- expect (input.attributes, contains ('multiple' ));
257
+ expect (input.getAttribute ('accept' ) , 'any' );
258
+ expect (input.hasAttribute ( 'capture' ), false );
259
+ expect (input.hasAttribute ('multiple' ), true );
252
260
});
253
261
254
262
testWidgets ('accept: any, capture: something, multi: true' ,
255
263
(WidgetTester tester) async {
256
- final html .Element input =
264
+ final web .Element input =
257
265
plugin.createInputElement ('any' , 'something' , multiple: true );
258
266
259
- expect (input.attributes, containsPair ('accept' , 'any' ) );
260
- expect (input.attributes, containsPair ('capture' , 'something' ) );
261
- expect (input.attributes, contains ('multiple' ));
267
+ expect (input.getAttribute ('accept' ) , 'any' );
268
+ expect (input.getAttribute ('capture' ) , 'something' );
269
+ expect (input.hasAttribute ('multiple' ), true );
262
270
});
263
271
});
264
272
265
273
group ('Deprecated methods' , () {
266
- late html. FileUploadInputElement mockInput;
274
+ late web. HTMLInputElement mockInput;
267
275
late ImagePickerPluginTestOverrides overrides;
268
276
late ImagePickerPlugin plugin;
269
277
270
278
setUp (() {
271
- mockInput = html. FileUploadInputElement () ;
279
+ mockInput = web. HTMLInputElement ()..type = 'file' ;
272
280
overrides = ImagePickerPluginTestOverrides ()
273
281
..createInputElement = ((_, __) => mockInput)
274
- ..getMultipleFilesFromInput = ((_) => < html .File > [textFile]);
282
+ ..getMultipleFilesFromInput = ((_) => < web .File > [textFile]);
275
283
plugin = ImagePickerPlugin (overrides: overrides);
276
284
});
277
285
278
286
void mockCancel () {
279
- mockInput.dispatchEvent (html .Event ('cancel' ));
287
+ mockInput.dispatchEvent (web .Event ('cancel' ));
280
288
}
281
289
282
290
void mockChange () {
283
- mockInput.dispatchEvent (html .Event ('change' ));
291
+ mockInput.dispatchEvent (web .Event ('change' ));
284
292
}
285
293
286
294
group ('getImage' , () {
@@ -306,7 +314,7 @@ void main() {
306
314
expect (
307
315
file.lastModified (),
308
316
completion (
309
- DateTime .fromMillisecondsSinceEpoch (textFile.lastModified! ),
317
+ DateTime .fromMillisecondsSinceEpoch (textFile.lastModified),
310
318
));
311
319
});
312
320
@@ -326,7 +334,7 @@ void main() {
326
334
testWidgets ('can select multiple files' , (WidgetTester _) async {
327
335
// Override the returned files...
328
336
overrides.getMultipleFilesFromInput =
329
- (_) => < html .File > [textFile, secondTextFile];
337
+ (_) => < web .File > [textFile, secondTextFile];
330
338
331
339
// ignore: deprecated_member_use
332
340
final Future <List <XFile >> files = plugin.getMultiImage ();
0 commit comments