Skip to content

Commit

Permalink
upgrade image_cropper from 5.0.1 to 8.0.2 with necessary adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
Satyam Jha committed Jan 7, 2025
1 parent b5b49cb commit 7ab5174
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 52 deletions.
12 changes: 8 additions & 4 deletions lib/services/image_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ class ImageService {
try {
final CroppedFile? croppedImage = await _imageCropper.cropImage(
sourcePath: imageFile.path,
aspectRatioPresets: [
CropAspectRatioPreset.square,
CropAspectRatioPreset.original,
],
uiSettings: [
AndroidUiSettings(
toolbarTitle: 'Crop Image',
Expand All @@ -42,9 +38,17 @@ class ImageService {
cropGridColor: Colors.white,
initAspectRatio: CropAspectRatioPreset.original,
lockAspectRatio: false,
aspectRatioPresets: [
CropAspectRatioPreset.square,
CropAspectRatioPreset.original,
],
),
IOSUiSettings(
minimumAspectRatio: 1.0,
aspectRatioPresets: [
CropAspectRatioPreset.square,
CropAspectRatioPreset.original,
],
),
],
);
Expand Down
12 changes: 6 additions & 6 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -753,26 +753,26 @@ packages:
dependency: "direct main"
description:
name: image_cropper
sha256: f4bad5ed2dfff5a7ce0dfbad545b46a945c702bb6182a921488ef01ba7693111
sha256: "266760ed426d7121f0ada02c672bfe5c1b5c714e908328716aee756f045709dc"
url: "https://pub.dev"
source: hosted
version: "5.0.1"
version: "8.1.0"
image_cropper_for_web:
dependency: transitive
description:
name: image_cropper_for_web
sha256: "865d798b5c9d826f1185b32e5d0018c4183ddb77b7b82a931e1a06aa3b74974e"
sha256: "34256c8fb7fcb233251787c876bb37271744459b593a948a2db73caa323034d0"
url: "https://pub.dev"
source: hosted
version: "3.0.0"
version: "6.0.2"
image_cropper_platform_interface:
dependency: transitive
description:
name: image_cropper_platform_interface
sha256: ee160d686422272aa306125f3b6fb1c1894d9b87a5e20ed33fa008e7285da11e
sha256: e8e9d2ca36360387aee39295ce49029362ae4df3071f23e8e71f2b81e40b7531
url: "https://pub.dev"
source: hosted
version: "5.0.0"
version: "7.0.0"
image_picker:
dependency: "direct main"
description:
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ dependencies:
graphql_flutter: ^5.1.2
hive: ^2.2.3
http: ^1.2.2
image_cropper: ^5.0.1
image_cropper: ^8.0.2
image_picker: ^1.1.2
intl: ^0.19.0
json_annotation: ^4.7.0
Expand Down
10 changes: 0 additions & 10 deletions test/helpers/test_helpers.mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4525,14 +4525,6 @@ class MockImageCropper extends _i2.Mock implements _i41.ImageCropper {
int? maxWidth,
int? maxHeight,
_i42.CropAspectRatio? aspectRatio,
List<_i42.CropAspectRatioPreset>? aspectRatioPresets = const [
_i42.CropAspectRatioPreset.original,
_i42.CropAspectRatioPreset.square,
_i42.CropAspectRatioPreset.ratio3x2,
_i42.CropAspectRatioPreset.ratio4x3,
_i42.CropAspectRatioPreset.ratio16x9,
],
_i42.CropStyle? cropStyle = _i42.CropStyle.rectangle,
_i42.ImageCompressFormat? compressFormat = _i42.ImageCompressFormat.jpg,
int? compressQuality = 90,
List<_i42.PlatformUiSettings>? uiSettings,
Expand All @@ -4546,8 +4538,6 @@ class MockImageCropper extends _i2.Mock implements _i41.ImageCropper {
#maxWidth: maxWidth,
#maxHeight: maxHeight,
#aspectRatio: aspectRatio,
#aspectRatioPresets: aspectRatioPresets,
#cropStyle: cropStyle,
#compressFormat: compressFormat,
#compressQuality: compressQuality,
#uiSettings: uiSettings,
Expand Down
82 changes: 65 additions & 17 deletions test/service_tests/image_service_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,27 @@ class MockImageService extends Mock implements ImageService {
}

void main() {
late ImageCropper mockImageCropper;

setUpAll(() {
TestWidgetsFlutterBinding.ensureInitialized();
testSetupLocator();
registerServices();
});
setUp(() {
mockImageCropper = locator<ImageCropper>();
reset(mockImageCropper); // Reset mock before each test
});

group('Tests for Crop Image', () {
test("test no image provided for the image cropper", () async {
const path = 'test';
final file = await imageService.cropImage(imageFile: File(path));
expect(file?.path, null);
});

test("crop image method", () async {
final mockImageCropper = imageCropper;

const path = "test";
final fakefile = File(path);
final croppedFile = CroppedFile("fakeCropped");

when(
mockImageCropper.cropImage(
sourcePath: "test",
aspectRatioPresets: [
CropAspectRatioPreset.square,
CropAspectRatioPreset.original,
],
uiSettings: anyNamed('uiSettings'),
),
).thenAnswer((realInvocation) async => croppedFile);
Expand All @@ -58,17 +52,71 @@ void main() {
expect(file?.path, croppedFile.path);
});

test('cropImage handles null CroppedFile', () async {
final testFile = File('test.png');

when(
mockImageCropper.cropImage(
sourcePath: 'test',
uiSettings: anyNamed('uiSettings'),
),
).thenAnswer((_) async => null);

final result = await imageService.cropImage(imageFile: testFile);

expect(result, isNull);
});

test('cropImage uses correct aspectRatioPresets', () async {
const path = "test";

final testFile = File(path);
final croppedFile = CroppedFile('cropped_test.png');

when(
mockImageCropper.cropImage(
sourcePath: 'test',
uiSettings: captureAnyNamed('uiSettings'),
),
).thenAnswer((_) async => croppedFile);

await imageService.cropImage(imageFile: testFile);

final capturedUiSettings = verify(
mockImageCropper.cropImage(
sourcePath: 'test',
uiSettings: captureAnyNamed('uiSettings'),
),
).captured.single as List<PlatformUiSettings>;

final androidSettings =
capturedUiSettings.whereType<AndroidUiSettings>().single;
final iosSettings = capturedUiSettings.whereType<IOSUiSettings>().single;

expect(
androidSettings.aspectRatioPresets,
contains(CropAspectRatioPreset.square),
);
expect(
androidSettings.aspectRatioPresets,
contains(CropAspectRatioPreset.original),
);
expect(
iosSettings.aspectRatioPresets,
contains(CropAspectRatioPreset.square),
);
expect(
iosSettings.aspectRatioPresets,
contains(CropAspectRatioPreset.original),
);
});

test("error in crop image", () async {
final mockImageCropper = locator<ImageCropper>();
const path = "test";
final fakefile = File(path);
when(
mockImageCropper.cropImage(
sourcePath: "test",
aspectRatioPresets: [
CropAspectRatioPreset.square,
CropAspectRatioPreset.original,
],
uiSettings: anyNamed('uiSettings'),
),
).thenThrow(Exception());
Expand Down
66 changes: 52 additions & 14 deletions test/service_tests/multi_media_pick_service_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ import '../helpers/test_helpers.dart';
import '../helpers/test_locator.dart';

void main() {
late ImageCropper mockImageCropper;

setUpAll(() {
TestWidgetsFlutterBinding.ensureInitialized();
testSetupLocator();
registerServices();
});
setUp(() {
registerServices();
mockImageCropper = locator<ImageCropper>();
reset(mockImageCropper); // Reset mock before each test
SizeConfig().test();
});
tearDown(() {
unregisterServices();
});
group('MultiMediaPickerService test', () {
test("test get photo from gallery method if camera option is false",
() async {
final mockImageCropper = locator<ImageCropper>();
final mockPicker = imagePicker;
final model = MultiMediaPickerService();
const path = 'test';
Expand All @@ -41,10 +41,6 @@ void main() {
when(
mockImageCropper.cropImage(
sourcePath: "test",
aspectRatioPresets: [
CropAspectRatioPreset.square,
CropAspectRatioPreset.original,
],
uiSettings: anyNamed('uiSettings'),
),
).thenAnswer((realInvocation) async => CroppedFile(path));
Expand All @@ -55,7 +51,6 @@ void main() {
});
test("test get photo from gallery method if camera option is true",
() async {
final mockImageCropper = locator<ImageCropper>();
final mockPicker = locator<ImagePicker>();
final model = MultiMediaPickerService();
const path = 'test';
Expand All @@ -65,10 +60,6 @@ void main() {
when(
mockImageCropper.cropImage(
sourcePath: "test",
aspectRatioPresets: [
CropAspectRatioPreset.square,
CropAspectRatioPreset.original,
],
uiSettings: anyNamed('uiSettings'),
),
).thenAnswer((realInvocation) async => CroppedFile(path));
Expand All @@ -77,6 +68,53 @@ void main() {

expect(result?.path, path);
});
test(
"test get photo from gallery method with aspectRatioPresets in uiSettings",
() async {
final mockPicker = locator<ImagePicker>();
final model = MultiMediaPickerService();
const path = 'test';
final image = XFile(path);

when(mockPicker.pickImage(source: ImageSource.gallery))
.thenAnswer((_) async => image);
when(
mockImageCropper.cropImage(
sourcePath: "test",
uiSettings: anyNamed('uiSettings'),
),
).thenAnswer((_) async => CroppedFile(path));

await model.getPhotoFromGallery(camera: false);

final capturedUiSettings = verify(
mockImageCropper.cropImage(
sourcePath: "test",
uiSettings: captureAnyNamed('uiSettings'),
),
).captured.single as List<PlatformUiSettings>;

final androidSettings =
capturedUiSettings.whereType<AndroidUiSettings>().single;
final iosSettings = capturedUiSettings.whereType<IOSUiSettings>().single;

expect(
androidSettings.aspectRatioPresets,
contains(CropAspectRatioPreset.square),
);
expect(
androidSettings.aspectRatioPresets,
contains(CropAspectRatioPreset.original),
);
expect(
iosSettings.aspectRatioPresets,
contains(CropAspectRatioPreset.square),
);
expect(
iosSettings.aspectRatioPresets,
contains(CropAspectRatioPreset.original),
);
});
test("test no photo provided for the pick image", () async {
final model = MultiMediaPickerService();
final file = await model.getPhotoFromGallery(camera: false);
Expand Down

0 comments on commit 7ab5174

Please sign in to comment.