Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
Add pick file unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
ditman committed May 20, 2020
1 parent 2e323c1 commit e426c9a
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ final String _kAcceptVideoMimeType = 'video/*';
///
/// This class implements the `package:image_picker` functionality for the web.
class ImagePickerPlugin extends ImagePickerPlatform {
final Function _overrideCreateInput;
bool get _shouldOverrideInput => _overrideCreateInput != null;

html.Element _target;

/// A constructor that allows tests to override the window object used by the plugin.
ImagePickerPlugin({@visibleForTesting html.Element target})
: _target = target {
if (_target == null) {
_target = _initTarget(_kImagePickerInputsDomId);
}
/// A constructor that allows tests to override the function that creates file inputs.
ImagePickerPlugin({@visibleForTesting Function overrideCreateInput})
: _overrideCreateInput = overrideCreateInput {
_target = _initTarget(_kImagePickerInputsDomId);
}

/// Registers this class as the default instance of [ImagePickerPlatform].
Expand Down Expand Up @@ -122,6 +123,10 @@ class ImagePickerPlugin extends ImagePickerPlatform {
html.Element _createInputElement(String accept, String capture) {
html.Element element;

if (_shouldOverrideInput) {
return _overrideCreateInput(accept, capture);
}

if (capture != null) {
// Capture is not supported by dart:html :/
element = html.Element.html(
Expand All @@ -137,8 +142,10 @@ class ImagePickerPlugin extends ImagePickerPlatform {

/// Injects the file input element, and clicks on it
void _injectAndActivate(html.Element element) {
_target.children.clear();
_target.children.add(element);
if (!_shouldOverrideInput) {
_target.children.clear();
_target.children.add(element);
}
element.click();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,74 @@

@TestOn('chrome') // Uses web-only Flutter SDK

import 'dart:async';
import 'dart:convert';
import 'dart:html' as html;
import 'dart:typed_data';

import 'package:flutter_test/flutter_test.dart';
import 'package:image_picker_for_web/image_picker_for_web.dart';
import 'package:image_picker_platform_interface/image_picker_platform_interface.dart';
import 'package:mockito/mockito.dart';

class MockWindow extends Mock implements html.Window {}
final String expectedStringContents = "Hello, world!";
final Uint8List bytes = utf8.encode(expectedStringContents);
final html.File textFile = html.File([bytes], "hello.txt");

class MockFileInput extends Mock implements html.FileUploadInputElement {}

class MockOnChangeEvent extends Mock implements html.Event {
@override
MockFileInput target;
}

class MockElementStream<T extends html.Event> extends Mock
implements html.ElementStream<T> {
final StreamController<T> controller = StreamController<T>();
@override
StreamSubscription<T> listen(void onData(T event),
{Function onError, void onDone(), bool cancelOnError}) {
return controller.stream.listen(onData,
onError: onError, onDone: onDone, cancelOnError: cancelOnError);
}
}

void main() {
MockFileInput mockInput = MockFileInput();
MockElementStream mockStream = MockElementStream<html.Event>();
MockElementStream mockErrorStream = MockElementStream<html.Event>();
MockOnChangeEvent mockEvent = MockOnChangeEvent()..target = mockInput;

// Under test...
ImagePickerPlugin plugin =
ImagePickerPlugin(overrideCreateInput: (_, __) => mockInput);

setUp(() {
// Make the mockInput behave like a proper input...
when(mockInput.onChange).thenAnswer((_) => mockStream);
when(mockInput.onError).thenAnswer((_) => mockErrorStream);
});

tearDown(() {
reset(mockInput);
});

// Pick a file...
test('Can select a file, happy case', () async {
// Init the pick file dialog...
final file = plugin.pickImage(
source: ImageSource.gallery,
);

// Mock the browser behavior of selecting a file...
when(mockInput.files).thenReturn([textFile]);
mockStream.controller.add(mockEvent);

// Now the file should be selected
expect(file, completes);
// And readable
expect((await file).readAsString(), completion(expectedStringContents));
});

// Creates the correct DOM for the input...
}

0 comments on commit e426c9a

Please sign in to comment.