Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test ImagePicker in Flutter Driver?

Tags:

flutter-test

In Flutter integration testing, how can we handle ImagePicker? as well as other platform related plugins?

like image 774
Frank Yan Avatar asked Jun 13 '19 21:06

Frank Yan


People also ask

Which Dev dependency is required to perform an integration test in flutter?

Add the integration_test dependency Next, use the integration_test and flutter_test packages to write integration tests. Add these dependencies to the dev_dependencies section of the app's pubspec. yaml file, specifying the Flutter SDK as the location of the package.


1 Answers

Finally, I got a solution for this question. this is the code in app.dart:

prepare an image file in assets, for example: images/sample.png.

import 'dart:io';
import 'dart:typed_data';
import 'package:path_provider/path_provider.dart';

import 'package:image_picker_test/main.dart' as app;
import 'package:flutter_driver/driver_extension.dart';
import 'package:flutter/services.dart';
void main() {
  // This line enables the extension.
  enableFlutterDriverExtension();

  const MethodChannel channel =
  MethodChannel('plugins.flutter.io/image_picker');

  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();
}
like image 74
Frank Yan Avatar answered Sep 27 '22 17:09

Frank Yan