-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[image_picker_android] Adjust file extension in FileUtils when it does not match its mime type #3409
Merged
auto-submit
merged 10 commits into
flutter:main
from
Utopia-USS:image_picker_android_fix_mismatched_extension
Mar 17, 2023
Merged
[image_picker_android] Adjust file extension in FileUtils when it does not match its mime type #3409
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0e80b62
[image_picker_android] Adjust file extension in FileUtils when it doe…
033fec4
Merge branch 'main' into image_picker_android_fix_mismatched_extension
SynSzakala a76ca85
[image_picker_android] Fixed formatting
40c0605
Merge remote-tracking branch 'origin/image_picker_android_fix_mismatc…
637d240
[in_app_purchases_android_platform] Add test case for unknown mime ty…
b9bf893
Merge remote-tracking branch 'upstream/main' into image_picker_androi…
4aa46c3
Merge branch 'main' into image_picker_android_fix_mismatched_extension
SynSzakala 31cb6b6
[in_app_purchases_android_platform] Fix formatting in FileUtilTest
9318db9
Merge branch 'main' into image_picker_android_fix_mismatched_extension
SynSzakala 71af4a2
Merge branch 'main' into image_picker_android_fix_mismatched_extension
SynSzakala File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
import android.database.MatrixCursor; | ||
import android.net.Uri; | ||
import android.provider.MediaStore; | ||
import android.webkit.MimeTypeMap; | ||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
import androidx.test.core.app.ApplicationProvider; | ||
|
@@ -29,6 +30,7 @@ | |
import org.robolectric.Robolectric; | ||
import org.robolectric.RobolectricTestRunner; | ||
import org.robolectric.shadows.ShadowContentResolver; | ||
import org.robolectric.shadows.ShadowMimeTypeMap; | ||
|
||
@RunWith(RobolectricTestRunner.class) | ||
public class FileUtilTest { | ||
|
@@ -42,6 +44,10 @@ public void before() { | |
context = ApplicationProvider.getApplicationContext(); | ||
shadowContentResolver = shadowOf(context.getContentResolver()); | ||
fileUtils = new FileUtils(); | ||
ShadowMimeTypeMap mimeTypeMap = shadowOf(MimeTypeMap.getSingleton()); | ||
mimeTypeMap.addExtensionMimeTypMapping("jpg", "image/jpeg"); | ||
mimeTypeMap.addExtensionMimeTypMapping("png", "image/png"); | ||
mimeTypeMap.addExtensionMimeTypMapping("webp", "image/webp"); | ||
} | ||
|
||
@Test | ||
|
@@ -74,15 +80,38 @@ public void FileUtil_getImageExtension() throws IOException { | |
|
||
@Test | ||
public void FileUtil_getImageName() throws IOException { | ||
Uri uri = Uri.parse("content://dummy/dummy.png"); | ||
Uri uri = MockContentProvider.PNG_URI; | ||
Robolectric.buildContentProvider(MockContentProvider.class).create("dummy"); | ||
shadowContentResolver.registerInputStream( | ||
uri, new ByteArrayInputStream("imageStream".getBytes(UTF_8))); | ||
String path = fileUtils.getPathFromUri(context, uri); | ||
assertTrue(path.endsWith("a.b.png")); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You created a new codepath in this PR for the case where the filename is non-null but the extension is null; that case needs a test as well. |
||
@Test | ||
public void FileUtil_getImageName_mismatchedType() throws IOException { | ||
Uri uri = MockContentProvider.WEBP_URI; | ||
Robolectric.buildContentProvider(MockContentProvider.class).create("dummy"); | ||
shadowContentResolver.registerInputStream( | ||
uri, new ByteArrayInputStream("imageStream".getBytes(UTF_8))); | ||
String path = fileUtils.getPathFromUri(context, uri); | ||
assertTrue(path.endsWith("dummy.png")); | ||
assertTrue(path.endsWith("c.d.webp")); | ||
} | ||
|
||
@Test | ||
public void FileUtil_getImageName_unknownType() throws IOException { | ||
Uri uri = MockContentProvider.UNKNOWN_URI; | ||
Robolectric.buildContentProvider(MockContentProvider.class).create("dummy"); | ||
shadowContentResolver.registerInputStream( | ||
uri, new ByteArrayInputStream("imageStream".getBytes(UTF_8))); | ||
String path = fileUtils.getPathFromUri(context, uri); | ||
assertTrue(path.endsWith("e.f.g")); | ||
} | ||
|
||
private static class MockContentProvider extends ContentProvider { | ||
public static final Uri PNG_URI = Uri.parse("content://dummy/a.b.png"); | ||
public static final Uri WEBP_URI = Uri.parse("content://dummy/c.d.png"); | ||
public static final Uri UNKNOWN_URI = Uri.parse("content://dummy/e.f.g"); | ||
|
||
@Override | ||
public boolean onCreate() { | ||
|
@@ -98,14 +127,16 @@ public Cursor query( | |
@Nullable String[] selectionArgs, | ||
@Nullable String sortOrder) { | ||
MatrixCursor cursor = new MatrixCursor(new String[] {MediaStore.MediaColumns.DISPLAY_NAME}); | ||
cursor.addRow(new Object[] {"dummy.png"}); | ||
cursor.addRow(new Object[] {uri.getLastPathSegment()}); | ||
return cursor; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public String getType(@NonNull Uri uri) { | ||
return "image/png"; | ||
if (uri.equals(PNG_URI)) return "image/png"; | ||
if (uri.equals(WEBP_URI)) return "image/webp"; | ||
return null; | ||
} | ||
|
||
@Nullable | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add another test for the case where a file's basename contains
.
s.