-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Fix max-pixels compression not applying to ImageWidget
#7284
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
Open
grzesiek2010
wants to merge
7
commits into
getodk:master
Choose a base branch
from
grzesiek2010:COLLECT-7277
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+219
−30
Open
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
99afb0f
Fix max-pixels compression not applying to ImageWidge
grzesiek2010 c06981f
Add tests
grzesiek2010 a78cf3a
Decouple ImageCompressionController from QuestionWidget
grzesiek2010 c5497f1
Gate image compression on control type instead of widget class
grzesiek2010 ba5676f
Make image compression test sizes explicit at call sites
grzesiek2010 ec22f26
Verify compressed image via submitted media file
grzesiek2010 469967d
Suppress WrongThread lint warning
grzesiek2010 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
113 changes: 113 additions & 0 deletions
113
...pp/src/androidTest/java/org/odk/collect/android/feature/formentry/ImageCompressionTest.kt
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,113 @@ | ||
| package org.odk.collect.android.feature.formentry | ||
|
|
||
| import android.app.Activity | ||
| import android.app.Instrumentation | ||
| import android.content.Intent | ||
| import android.graphics.Bitmap | ||
| import android.graphics.BitmapFactory | ||
| import android.provider.MediaStore | ||
| import androidx.test.espresso.intent.Intents | ||
| import androidx.test.espresso.intent.matcher.IntentMatchers.hasAction | ||
| import androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent | ||
| import org.hamcrest.Matcher | ||
| import org.hamcrest.MatcherAssert.assertThat | ||
| import org.hamcrest.Matchers.equalTo | ||
| import org.junit.Rule | ||
| import org.junit.Test | ||
| import org.junit.rules.RuleChain | ||
| import org.odk.collect.android.storage.StoragePathProvider | ||
| import org.odk.collect.android.support.StorageUtils | ||
| import org.odk.collect.android.support.rules.FormEntryActivityTestRule | ||
| import org.odk.collect.android.support.rules.TestRuleChain.chain | ||
| import org.odk.collect.androidtest.RecordedIntentsRule | ||
| import org.odk.collect.draw.DrawActivity | ||
| import org.odk.collect.testshared.WaitFor.waitFor | ||
| import org.odk.collect.strings.R.string | ||
| import org.odk.collect.testshared.AssertionFramework | ||
| import java.io.File | ||
| import java.io.FileOutputStream | ||
|
|
||
| class ImageCompressionTest { | ||
| private val rule = FormEntryActivityTestRule() | ||
|
|
||
| @get:Rule | ||
| var chain: RuleChain = chain() | ||
| .around(RecordedIntentsRule()) | ||
| .around(rule) | ||
|
|
||
| @Test | ||
| fun imageWidget_scalesCapturedImageDownToMaxPixels() { | ||
| rule | ||
| .setUpProjectAndCopyForm("image-compression.xml") | ||
| .fillNewForm("image-compression.xml", "image-compression") | ||
| .also { | ||
| stubCaptureReturningImage(hasAction(MediaStore.ACTION_IMAGE_CAPTURE)) | ||
| }.clickOnString(string.capture_image, AssertionFramework.COMPOSE) | ||
|
|
||
| assertSavedImageWasScaledDownToMaxPixels() | ||
| } | ||
|
|
||
| @Test | ||
| fun annotateWidget_scalesCapturedImageDownToMaxPixels() { | ||
| rule | ||
| .setUpProjectAndCopyForm("image-compression.xml") | ||
| .fillNewForm("image-compression.xml", "image-compression") | ||
| .swipeToNextQuestion("Annotate") | ||
| .also { | ||
| stubCaptureReturningImage(hasAction(MediaStore.ACTION_IMAGE_CAPTURE)) | ||
| }.clickOnString(string.capture_image) | ||
|
|
||
| assertSavedImageWasScaledDownToMaxPixels() | ||
| } | ||
|
|
||
| @Test | ||
| fun drawWidget_scalesCapturedImageDownToMaxPixels() { | ||
| rule | ||
| .setUpProjectAndCopyForm("image-compression.xml") | ||
| .fillNewForm("image-compression.xml", "image-compression") | ||
| .swipeToNextQuestion("Annotate") | ||
| .swipeToNextQuestion("Draw") | ||
| .also { | ||
| stubCaptureReturningImage(hasComponent(DrawActivity::class.java.name)) | ||
| }.clickOnString(string.draw_image) | ||
|
|
||
| assertSavedImageWasScaledDownToMaxPixels() | ||
| } | ||
|
|
||
| @Test | ||
| fun signatureWidget_scalesCapturedImageDownToMaxPixels() { | ||
| rule | ||
| .setUpProjectAndCopyForm("image-compression.xml") | ||
| .fillNewForm("image-compression.xml", "image-compression") | ||
| .swipeToNextQuestion("Annotate") | ||
| .swipeToNextQuestion("Draw") | ||
| .swipeToNextQuestion("Signature") | ||
| .also { | ||
| stubCaptureReturningImage(hasComponent(DrawActivity::class.java.name)) | ||
| }.clickOnString(string.sign_button) | ||
|
|
||
| assertSavedImageWasScaledDownToMaxPixels() | ||
| } | ||
|
|
||
| private fun stubCaptureReturningImage(captureIntent: Matcher<Intent>) { | ||
| // Long edge (2000) is twice the form's max image size, so it should be scaled down to 1000. | ||
| val bitmap = Bitmap.createBitmap(2000, 1000, Bitmap.Config.ARGB_8888) | ||
|
grzesiek2010 marked this conversation as resolved.
Outdated
|
||
| val tmpImage = File(StoragePathProvider().getTmpImageFilePath()) | ||
| FileOutputStream(tmpImage).use { bitmap.compress(Bitmap.CompressFormat.JPEG, 90, it) } | ||
|
|
||
| Intents.intending(captureIntent).respondWith( | ||
| Instrumentation.ActivityResult(Activity.RESULT_OK, null) | ||
| ) | ||
| } | ||
|
|
||
| private fun assertSavedImageWasScaledDownToMaxPixels() { | ||
| waitFor { | ||
| val instanceDir = File(StorageUtils.getInstancesDirPath()).listFiles()!!.single() | ||
| val image = instanceDir.listFiles()!!.single { it.extension.equals("jpg", ignoreCase = true) } | ||
|
|
||
| val options = BitmapFactory.Options().apply { inJustDecodeBounds = true } | ||
| BitmapFactory.decodeFile(image.absolutePath, options) | ||
| assertThat(maxOf(options.outWidth, options.outHeight), equalTo(1000)) | ||
| } | ||
| } | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| <?xml version="1.0"?> | ||
| <h:html | ||
| xmlns="http://www.w3.org/2002/xforms" | ||
| xmlns:h="http://www.w3.org/1999/xhtml" | ||
| xmlns:ev="http://www.w3.org/2001/xml-events" | ||
| xmlns:xsd="http://www.w3.org/2001/XMLSchema" | ||
| xmlns:jr="http://openrosa.org/javarosa" | ||
| xmlns:orx="http://openrosa.org/xforms" | ||
| xmlns:odk="http://www.opendatakit.org/xforms"> | ||
| <h:head> | ||
| <h:title>image-compression</h:title> | ||
| <model odk:xforms-version="1.0.0"> | ||
| <instance> | ||
| <data id="image-compression"> | ||
| <image/> | ||
| <annotate/> | ||
| <draw/> | ||
| <signature/> | ||
| <meta> | ||
| <instanceID/> | ||
| </meta> | ||
| </data> | ||
| </instance> | ||
| <bind nodeset="/data/image" type="binary" orx:max-pixels="1000"/> | ||
| <bind nodeset="/data/annotate" type="binary" orx:max-pixels="1000"/> | ||
| <bind nodeset="/data/draw" type="binary" orx:max-pixels="1000"/> | ||
| <bind nodeset="/data/signature" type="binary" orx:max-pixels="1000"/> | ||
| <bind nodeset="/data/meta/instanceID" type="string" readonly="true()" jr:preload="uid"/> | ||
| </model> | ||
| </h:head> | ||
| <h:body> | ||
| <upload mediatype="image/*" ref="/data/image"> | ||
| <label>Image</label> | ||
| </upload> | ||
| <upload appearance="annotate" mediatype="image/*" ref="/data/annotate"> | ||
| <label>Annotate</label> | ||
| </upload> | ||
| <upload appearance="draw" mediatype="image/*" ref="/data/draw"> | ||
| <label>Draw</label> | ||
| </upload> | ||
| <upload appearance="signature" mediatype="image/*" ref="/data/signature"> | ||
| <label>Signature</label> | ||
| </upload> | ||
| </h:body> | ||
| </h:html> |
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.
Uh oh!
There was an error while loading. Please reload this page.