0

I am trying the solution for e2e test for image_picker from this link How to test ImagePicker in Flutter Driver?

 void main() {
      enableFlutterDriverExtension();
    
      const MethodChannel channel =
          MethodChannel('plugins.flutter.io/image_picker');
    
      setUp(() {
        channel.setMockMethodCallHandler((MethodCall methodCall) async {
          ByteData data = await rootBundle.load('images/sample.png');
          Uint8List bytes = data.buffer.asUint8List();
          Directory tempDir = await getTemporaryDirectory();
          File file = await File(
            '${tempDir.path}/tmp.tmp',
          ).writeAsBytes(bytes);
          print(file.path);
          return file.path;
        });
      });
      app.main();
    }

My main file for test is exactly the same. The issue is that when I am using enableFlutterDriverExtension(); the test right after start, finished with the info that all test passed, without emulating steps on emulator and print all the info to console after every step. And in the console I am getting

[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: 'package:flutter_driver/src/extension/extension.dart': Failed assertion: line 222 pos 10:
'WidgetsBinding.instance == null': is not true.
#0      _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:47:61)
#1      _AssertionError._throwNew (dart:core-patch/errors_patch.dart:36:5)
#2      enableFlutterDriverExtension (package:flutter_driver/src/extension/extension.dart:222:10)
#3      main 

Without enableFlutterDriverExtension(); test fail when I am trying to call image_picker. With error

flutter:   'package:flutter_test/src/binding.dart': Failed assertion: line 775 pos 14: '_pendingExceptionDetails != null': A test overrode FlutterError.onError
but either failed to return it to its original state, or had unexpected additional errors that it could not handle. Typically, this is caused by using expect()
before restoring FlutterError.onError.
flutter:   dart:core-patch/errors_patch.dart 47:61       _AssertionError._doThrowNew

What exactly the enableFlutterDriverExtension(); do? Without testing image_picker and this enableFlutterDriverExtension() tests are working correctly. Is there any other solution for testing image_picker?

Martyna
  • 1
  • 1

2 Answers2

1

In onder to have it working on android emulator, I did this in my widget test:

const MethodChannel channel =
    MethodChannel('plugins.flutter.io/image_picker_android');
handler(MethodCall methodCall) async {
  ByteData data = await rootBundle.load(imagePath);
  Uint8List bytes = data.buffer.asUint8List();
  Directory tempDir = await getTemporaryDirectory();
  File file = await File(
    '${tempDir.path}/tmp.tmp',
  ).writeAsBytes(bytes);
  return file.path;
}

TestWidgetsFlutterBinding.ensureInitialized();
TestDefaultBinaryMessengerBinding.instance?.defaultBinaryMessenger
    .setMockMethodCallHandler(channel, handler);
Joris.B
  • 101
  • 6
0

The question How to test ImagePicker in Flutter Driver? is over 2 years old so it's not more actuel.


bevore you test it you need to implament it for a platform. you implement it for android like this: Starting with version 0.8.1 the Android implementation support to pick (multiple) images on Android 4.3 or higher.

No configuration required - the plugin should work out of the box. It is however highly recommended to prepare for Android killing the application when low on memory. How to prepare for this is discussed in the Handling MainActivity destruction on Android section.

It is no longer required to add android:requestLegacyExternalStorage="true" as an attribute to the tag in AndroidManifest.xml, as image_picker has been updated to make use of scoped storage.

Note: Images and videos picked using the camera are saved to your application's local cache, and should therefore be expected to only be around temporarily. If you require your picked image to be stored permanently, it is your responsibility to move it to a more permanent location.


then you can test it by checking if it works by doing this:

// it pick a image from gallery but you can change that to camera
final XFile? image = await _picker.pickImage(source: ImageSource.gallery);

you need to know that it gives you a XFile and not a normal file but you can convert it to an file like this:

final xFile = await ImagePicker().pickImage(source: ImageSource.gallery);

final String? path = xFile!.path;

//that's the File
final bytes = await File(path!).readAsBytes();
liam spiegel
  • 520
  • 5
  • 18