Skip to content

Commit

Permalink
fix: Image picking tests fix. #275
Browse files Browse the repository at this point in the history
  • Loading branch information
LuchoTurtle committed Oct 4, 2023
1 parent e2f2c59 commit bef0bf5
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ packages:
source: hosted
version: "0.2.1+1"
image_picker_platform_interface:
dependency: transitive
dependency: "direct dev"
description:
name: image_picker_platform_interface
sha256: ed9b00e63977c93b0d2d2b343685bed9c324534ba5abafbb3dfbd6a780b1b514
Expand Down
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ dev_dependencies:

cross_file: ^0.3.3+5
path_provider_platform_interface: ^2.1.1
image_picker_platform_interface: ^2.9.1

flutter:
uses-material-design: true

assets:
- assets/icon/
- assets/
81 changes: 76 additions & 5 deletions test/integration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,49 @@ import 'package:dwyl_app/presentation/views/views.dart';
import 'package:dwyl_app/presentation/widgets/editor/emoji_picker.dart';
import 'package:dwyl_app/presentation/widgets/widgets.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_quill/flutter_quill.dart' hide Text;
import 'package:flutter_quill/flutter_quill_test.dart';
import 'package:flutter_quill_extensions/embeds/toolbar/image_button.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:dwyl_app/main.dart';
import 'package:image_picker_platform_interface/image_picker_platform_interface.dart';
import 'package:path_provider/path_provider.dart';
import 'package:universal_io/io.dart';

class FakeImagePicker extends ImagePickerPlatform {
@override
Future<XFile?> getImageFromSource({
required ImageSource source,
ImagePickerOptions options = const ImagePickerOptions(),
}) async {
final data = await rootBundle.load('assets/sample.jpeg');
final bytes = data.buffer.asUint8List();
final tempDir = await getTemporaryDirectory();
final file = await File(
'${tempDir.path}/doc.png',
).writeAsBytes(bytes);

return XFile(file.path);
}

@override
Future<List<XFile>> getMultiImageWithOptions({
MultiImagePickerOptions options = const MultiImagePickerOptions(),
}) async {
final data = await rootBundle.load('assets/sample.jpeg');
final bytes = data.buffer.asUint8List();
final tempDir = await getTemporaryDirectory();
final file = await File(
'${tempDir.path}/sample.png',
).writeAsBytes(bytes);
return <XFile>[
XFile(
file.path,
),
];
}
}

void main() {
/// Bootstraps a sample main application, whether it [isWeb] or not.
Expand All @@ -21,8 +59,10 @@ void main() {
);
}

// See https://stackoverflow.com/questions/76586920/mocking-imagepicker-in-flutter-integration-tests-not-working for context.
setUpAll(() {
TestWidgetsFlutterBinding.ensureInitialized();
ImagePickerPlatform.instance = FakeImagePicker();
});

group('Normal build', () {
Expand Down Expand Up @@ -403,9 +443,6 @@ void main() {

group('Image picker', () {
testWidgets('should show and select image', (WidgetTester tester) async {
// Mock image
// mockImagePicker(tester);

// Build our app and trigger a frame.
final app = initializeMainApp(isWeb: false);
await tester.pumpWidget(app);
Expand All @@ -426,12 +463,46 @@ void main() {
await tester.pump(const Duration(minutes: 1));

// Press image button
// Because of the override, should embed image.
final imageButton = find.byType(ImageButton);
await tester.tap(imageButton);
await tester.pumpAndSettle();

// Image correctly added, editor should be visible again.
expect(editor.hitTestable(), findsOneWidget);
});


// TODO does not work because inside ImageButton, there is a `kIsWeb`.
// This can't be mockable, unless we use a custom button to recreate this behaviour.
testWidgets('should show and select image (on the web - uploads image to the web)', (WidgetTester tester) async {
// Build our app and trigger a frame.
final app = initializeMainApp(isWeb: true);
await tester.pumpWidget(app);
await tester.pumpAndSettle();

// Tap textfield to open new page to create item
await tester.tap(find.byKey(textfieldKey));
await tester.pumpAndSettle(const Duration(seconds: 2));

// Should show editor and toolbar
final editor = find.byType(QuillEditor);
final toolbar = find.byType(QuillToolbar);
expect(editor.hitTestable(), findsOneWidget);
expect(toolbar.hitTestable(), findsOneWidget);

// Drag toolbar to the right
await tester.drag(toolbar, const Offset(-500, 0));
await tester.pump(const Duration(minutes: 1));

// Press image button
// Because of the override, should embed image.
final imageButton = find.byType(ImageButton);
await tester.tap(imageButton);
await tester.pumpAndSettle();

// TODO can't choose image.
// Check https://github.com/singerdmx/flutter-quill/issues/1389.
// Image correctly added, editor should be visible again.
expect(editor.hitTestable(), findsOneWidget);
});
});

Expand Down
6 changes: 3 additions & 3 deletions test/unit/image_callbacks_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class FakePathProviderPlatform extends PathProviderPlatform implements Fake {
@override
Future<String?> getApplicationDocumentsPath() async {
// Make sure is the same folder used in the tests.
return 'test';
return 'assets';
}
}

Expand Down Expand Up @@ -45,11 +45,11 @@ void main() {
});

test('`onImagePickCallback` should return path of a given file.', () async {
final file = File('test/sample.jpeg');
final file = File('assets/sample.jpeg');
final path = await onImagePickCallback(file);

// Some time must have passed
expect(path == 'test/sample.jpeg', true);
expect(path == 'assets/sample.jpeg', true);
});

testWidgets('`webImagePickImpl` should return the URL of the uploaded image on success (200).', (WidgetTester tester) async {
Expand Down

0 comments on commit bef0bf5

Please sign in to comment.