This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[image_picker]fix a crash when a non-image file is picked. #2293
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
65 changes: 65 additions & 0 deletions
65
...er/example/android/app/src/test/java/io/flutter/plugins/imagepicker/ImageResizerTest.java
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| // 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 org.hamcrest.core.IsEqual.equalTo; | ||
| import static org.junit.Assert.assertThat; | ||
|
|
||
| import android.graphics.Bitmap; | ||
| import android.graphics.BitmapFactory; | ||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.junit.rules.TemporaryFolder; | ||
| import org.junit.runner.RunWith; | ||
| import org.mockito.MockitoAnnotations; | ||
| import org.robolectric.RobolectricTestRunner; | ||
|
|
||
| // RobolectricTestRunner always creates a default mock bitmap when reading from file. So we cannot actually test the scaling. | ||
| // But we can still test whether the original or scaled file is created. | ||
| @RunWith(RobolectricTestRunner.class) | ||
| public class ImageResizerTest { | ||
|
|
||
| ImageResizer resizer; | ||
| File imageFile; | ||
| File externalDirectory; | ||
| Bitmap originalImageBitmap; | ||
|
|
||
| @Before | ||
| public void setUp() throws IOException { | ||
| MockitoAnnotations.initMocks(this); | ||
| imageFile = new File(getClass().getClassLoader().getResource("pngImage.png").getFile()); | ||
| originalImageBitmap = BitmapFactory.decodeFile(imageFile.getPath()); | ||
| TemporaryFolder temporaryFolder = new TemporaryFolder(); | ||
| temporaryFolder.create(); | ||
| externalDirectory = temporaryFolder.newFolder("image_picker_testing_path"); | ||
| resizer = new ImageResizer(externalDirectory, new ExifDataCopier()); | ||
| } | ||
|
|
||
| @Test | ||
| public void onResizeImageIfNeeded_WhenQualityIsNull_ShoultNotResize_ReturnTheUnscaledFile() { | ||
| String outoutFile = resizer.resizeImageIfNeeded(imageFile.getPath(), null, null, null); | ||
| assertThat(outoutFile, equalTo(externalDirectory.getPath() + "/pngImage.png")); | ||
| } | ||
|
|
||
| @Test | ||
| public void onResizeImageIfNeeded_WhenQualityIsNotNull_ShoulResize_ReturnResizedFile() { | ||
| String outoutFile = resizer.resizeImageIfNeeded(imageFile.getPath(), null, null, 50); | ||
| assertThat(outoutFile, equalTo(externalDirectory.getPath() + "/scaled_pngImage.png")); | ||
| } | ||
|
|
||
| @Test | ||
| public void onResizeImageIfNeeded_WhenWidthIsNotNull_ShoulResize_ReturnResizedFile() { | ||
| String outoutFile = resizer.resizeImageIfNeeded(imageFile.getPath(), 50.0, null, null); | ||
| assertThat(outoutFile, equalTo(externalDirectory.getPath() + "/scaled_pngImage.png")); | ||
| } | ||
|
|
||
| @Test | ||
| public void onResizeImageIfNeeded_WhenHeightIsNotNull_ShoulResize_ReturnResizedFile() { | ||
| String outoutFile = resizer.resizeImageIfNeeded(imageFile.getPath(), null, 50.0, null); | ||
| assertThat(outoutFile, equalTo(externalDirectory.getPath() + "/scaled_pngImage.png")); | ||
| } | ||
| } | ||
Binary file added
BIN
+629 Bytes
packages/image_picker/example/android/app/src/test/resources/pngImage.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or 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.
If we have some time to improve this, it would be better to change the style of testing here so that it's really testing the behavior of
ImageResizer's public API instead of the interactions of a bunch of its private methods. The way this test is written right now it's really just verifying that when certain methods are called, other methods also get triggered. This isn't really testing the logic of the class, and it could be broken by trivial method renames. It also exposes a bunch of methods with@VisibleForTestingthat would be better kept private.https://testing.googleblog.com/2013/08/testing-on-toilet-test-behavior-not.html
Here's the approach I would probably take:
ImageResizerasexternalFilesDirectoryas its constructed in the tests.TemporaryFolder.ImageResizer.resizeImageIfNeededwith a combination of settings. Verify that the bitmap at the returned path exists and has been resized correctly for each one.What do you think, does that sound workable to you? The catch here is I'm not sure how creating a Bitmap in a test goes, and I'm not sure if it's easy to check a Bitmap's image quality or not. I don't remember that API off the top of my head.
This is still an improvement over what we have so I am happy to merge this if needed, thank you for writing tests.
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.
Will take a look