22// Use of this source code is governed by a BSD-style license that can be
33// found in the LICENSE file.
44
5- @TestOn ('chrome' ) // Uses dart:html
6-
75import 'dart:convert' ;
86import 'dart:html' as html;
97import 'dart:typed_data' ;
108
119import 'package:flutter_test/flutter_test.dart' ;
1210import 'package:image_picker_for_web/image_picker_for_web.dart' ;
1311import 'package:image_picker_platform_interface/image_picker_platform_interface.dart' ;
12+ import 'package:integration_test/integration_test.dart' ;
1413
1514final String expectedStringContents = "Hello, world!" ;
1615final Uint8List bytes = utf8.encode (expectedStringContents) as Uint8List ;
1716final html.File textFile = html.File ([bytes], "hello.txt" );
1817
1918void main () {
19+ IntegrationTestWidgetsFlutterBinding .ensureInitialized ();
20+
2021 // Under test...
2122 late ImagePickerPlugin plugin;
2223
2324 setUp (() {
2425 plugin = ImagePickerPlugin ();
2526 });
2627
27- test ('Can select a file' , () async {
28+ testWidgets ('Can select a file (Deprecated) ' , (WidgetTester tester ) async {
2829 final mockInput = html.FileUploadInputElement ();
2930
3031 final overrides = ImagePickerPluginTestOverrides ()
@@ -45,9 +46,30 @@ void main() {
4546 expect ((await file).readAsBytes (), completion (isNotEmpty));
4647 });
4748
49+ testWidgets ('Can select a file' , (WidgetTester tester) async {
50+ final mockInput = html.FileUploadInputElement ();
51+
52+ final overrides = ImagePickerPluginTestOverrides ()
53+ ..createInputElement = ((_, __) => mockInput)
54+ ..getFileFromInput = ((_) => textFile);
55+
56+ final plugin = ImagePickerPlugin (overrides: overrides);
57+
58+ // Init the pick file dialog...
59+ final file = plugin.getFile ();
60+
61+ // Mock the browser behavior of selecting a file...
62+ mockInput.dispatchEvent (html.Event ('change' ));
63+
64+ // Now the file should be available
65+ expect (file, completes);
66+ // And readable
67+ expect ((await file).readAsBytes (), completion (isNotEmpty));
68+ });
69+
4870 // There's no good way of detecting when the user has "aborted" the selection.
4971
50- test ('computeCaptureAttribute' , () {
72+ testWidgets ('computeCaptureAttribute' , (WidgetTester tester) async {
5173 expect (
5274 plugin.computeCaptureAttribute (ImageSource .gallery, CameraDevice .front),
5375 isNull,
@@ -67,14 +89,14 @@ void main() {
6789 });
6890
6991 group ('createInputElement' , () {
70- test ('accept: any, capture: null' , () {
92+ testWidgets ('accept: any, capture: null' , (WidgetTester tester) async {
7193 html.Element input = plugin.createInputElement ('any' , null );
7294
7395 expect (input.attributes, containsPair ('accept' , 'any' ));
7496 expect (input.attributes, isNot (contains ('capture' )));
7597 });
7698
77- test ('accept: any, capture: something' , () {
99+ testWidgets ('accept: any, capture: something' , (WidgetTester tester) async {
78100 html.Element input = plugin.createInputElement ('any' , 'something' );
79101
80102 expect (input.attributes, containsPair ('accept' , 'any' ));
0 commit comments