Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/image_picker/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.6.1+4

* Android: Fix a regression where the `retrieveLostImage` does not work anymore.
* Set up Android unit test to test `ImagePickerCache` and added image quality caching tests.

## 0.6.1+3

* Bugfix iOS: Fix orientation of the picked image after scaling.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.content.SharedPreferences;
import android.net.Uri;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import io.flutter.plugin.common.MethodCall;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -27,14 +28,20 @@ class ImagePickerCache {
private static final String SHARED_PREFERENCE_ERROR_CODE_KEY = "flutter_image_picker_error_code";
private static final String SHARED_PREFERENCE_ERROR_MESSAGE_KEY =
"flutter_image_picker_error_message";

private static final String SHARED_PREFERENCE_MAX_WIDTH_KEY = "flutter_image_picker_max_width";

private static final String SHARED_PREFERENCE_MAX_HEIGHT_KEY = "flutter_image_picker_max_height";

private static final String SHARED_PREFERENCE_IMAGE_QUALITY_KEY =
"flutter_image_picker_image_quality";

private static final String SHARED_PREFERENCE_TYPE_KEY = "flutter_image_picker_type";
private static final String SHARED_PREFERENCE_PENDING_IMAGE_URI_PATH_KEY =
"flutter_image_picker_pending_image_uri";
private static final String SHARED_PREFERENCES_NAME = "flutter_image_picker_shared_preference";

@VisibleForTesting
static final String SHARED_PREFERENCES_NAME = "flutter_image_picker_shared_preference";

private SharedPreferences prefs;

Expand Down Expand Up @@ -147,9 +154,9 @@ Map<String, Object> getCacheMap() {
}
if (prefs.contains(SHARED_PREFERENCE_IMAGE_QUALITY_KEY)) {
final int imageQuality = prefs.getInt(SHARED_PREFERENCE_IMAGE_QUALITY_KEY, 100);
resultMap.put(MAP_KEY_MAX_HEIGHT, imageQuality);
resultMap.put(MAP_KEY_IMAGE_QUALITY, imageQuality);
} else {
resultMap.put(MAP_KEY_MAX_HEIGHT, 100);
resultMap.put(MAP_KEY_IMAGE_QUALITY, 100);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Copyright 2019 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package io.flutter.plugins.imagepicker;

import static io.flutter.plugins.imagepicker.ImagePickerCache.MAP_KEY_IMAGE_QUALITY;
import static io.flutter.plugins.imagepicker.ImagePickerCache.SHARED_PREFERENCES_NAME;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import io.flutter.plugin.common.MethodCall;
import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

public class ImagePickerCacheTest {
private static final double WIDTH = 10.0;
private static final double HEIGHT = 10.0;
private static final int IMAGE_QUALITY = 90;
private static final String PATH = "a_mock_path";

@Mock Activity mockActivity;
@Mock SharedPreferences mockPreference;
@Mock SharedPreferences.Editor mockEditor;
@Mock MethodCall mockMethodCall;

static Map<String, Object> preferenceStorage;

@Before
public void setUp() {
MockitoAnnotations.initMocks(this);

preferenceStorage = new HashMap();
when(mockActivity.getPackageName()).thenReturn("com.example.test");
when(mockActivity.getPackageManager()).thenReturn(mock(PackageManager.class));
when(mockActivity.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE))
.thenReturn(mockPreference);
when(mockPreference.edit()).thenReturn(mockEditor);
when(mockEditor.putInt(any(String.class), any(int.class)))
.then(
i -> {
preferenceStorage.put(i.getArgument(0), i.getArgument(1));
return mockEditor;
});
when(mockEditor.putLong(any(String.class), any(long.class)))
.then(
i -> {
preferenceStorage.put(i.getArgument(0), i.getArgument(1));
return mockEditor;
});
when(mockEditor.putString(any(String.class), any(String.class)))
.then(
i -> {
preferenceStorage.put(i.getArgument(0), i.getArgument(1));
return mockEditor;
});

when(mockPreference.getInt(any(String.class), any(int.class)))
.then(
i -> {
int result =
(int)
((preferenceStorage.get(i.getArgument(0)) != null)
? preferenceStorage.get(i.getArgument(0))
: i.getArgument(1));
return result;
});
when(mockPreference.getLong(any(String.class), any(long.class)))
.then(
i -> {
long result =
(long)
((preferenceStorage.get(i.getArgument(0)) != null)
? preferenceStorage.get(i.getArgument(0))
: i.getArgument(1));
return result;
});
when(mockPreference.getString(any(String.class), any(String.class)))
.then(
i -> {
String result =
(String)
((preferenceStorage.get(i.getArgument(0)) != null)
? preferenceStorage.get(i.getArgument(0))
: i.getArgument(1));
return result;
});

when(mockPreference.contains(any(String.class))).thenReturn(true);
}

@Test
public void ImageCache_ShouldBeAbleToSetAndGetQuality() {
when(mockMethodCall.argument(MAP_KEY_IMAGE_QUALITY)).thenReturn(IMAGE_QUALITY);
ImagePickerCache cache = new ImagePickerCache(mockActivity);
cache.saveDimensionWithMethodCall(mockMethodCall);
Map<String, Object> resultMap = cache.getCacheMap();
int imageQuality = (int) resultMap.get(cache.MAP_KEY_IMAGE_QUALITY);
assertThat(imageQuality, equalTo(IMAGE_QUALITY));

when(mockMethodCall.argument(MAP_KEY_IMAGE_QUALITY)).thenReturn(null);
cache.saveDimensionWithMethodCall(mockMethodCall);
Map<String, Object> resultMapWithDefaultQuality = cache.getCacheMap();
int defaultImageQuality = (int) resultMapWithDefaultQuality.get(cache.MAP_KEY_IMAGE_QUALITY);
assertThat(defaultImageQuality, equalTo(100));
}
}
2 changes: 1 addition & 1 deletion packages/image_picker/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors:
- Flutter Team <[email protected]>
- Rhodes Davis Jr. <[email protected]>
homepage: https://github.com/flutter/plugins/tree/master/packages/image_picker
version: 0.6.1+3
version: 0.6.1+4

flutter:
plugin:
Expand Down