-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Crash on Previewing Existing Image
* Fix #5856 - Image Preview Crash When loading an existing image, we performed the image load before we had created all elements of the UI. This caused issues. We fix this by splitting UI creation and initialisation logic * Fix #5856 - Fix API Errors createUI has `@SuppressLint("NewApi")` due to camera permissioning, but it masked a bug in calling setBackground with lower API levels * Added Unit Tests Very basic right now, but there's a lot of value in ensuring that a screen can be created with no errors.
- Loading branch information
1 parent
a2f8c09
commit a9c689b
Showing
4 changed files
with
144 additions
and
29 deletions.
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
53 changes: 53 additions & 0 deletions
53
...test/java/com/ichi2/anki/multimediacard/activity/MultimediaEditFieldActivityTestBase.java
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.ichi2.anki.multimediacard.activity; | ||
|
||
import android.Manifest; | ||
import android.app.Application; | ||
import android.content.Intent; | ||
|
||
import com.ichi2.anki.RobolectricTest; | ||
import com.ichi2.anki.multimediacard.IMultimediaEditableNote; | ||
import com.ichi2.anki.multimediacard.fields.IField; | ||
import com.ichi2.anki.multimediacard.fields.IFieldController; | ||
import com.ichi2.anki.multimediacard.impl.MultimediaEditableNote; | ||
|
||
import org.robolectric.Robolectric; | ||
import org.robolectric.Shadows; | ||
import org.robolectric.android.controller.ActivityController; | ||
import org.robolectric.shadows.ShadowApplication; | ||
|
||
import androidx.test.core.app.ApplicationProvider; | ||
|
||
public abstract class MultimediaEditFieldActivityTestBase extends RobolectricTest { | ||
|
||
protected void grantCameraPermission() { | ||
Application application = ApplicationProvider.getApplicationContext(); | ||
ShadowApplication app = Shadows.shadowOf(application); | ||
app.grantPermissions(Manifest.permission.CAMERA); | ||
} | ||
|
||
protected IFieldController getControllerForField(IField field, IMultimediaEditableNote note, int fieldIndex) { | ||
Intent intent = new Intent(Intent.ACTION_VIEW); | ||
intent.putExtra(MultimediaEditFieldActivity.EXTRA_FIELD_INDEX, fieldIndex); | ||
intent.putExtra(MultimediaEditFieldActivity.EXTRA_FIELD, field); | ||
intent.putExtra(MultimediaEditFieldActivity.EXTRA_WHOLE_NOTE, note); | ||
return getControllerForIntent(intent); | ||
} | ||
|
||
protected IMultimediaEditableNote getEmptyNote() { | ||
MultimediaEditableNote note = new MultimediaEditableNote(); | ||
note.setNumFields(1); | ||
return note; | ||
} | ||
|
||
protected IFieldController getControllerForIntent(Intent intent) { | ||
|
||
|
||
ActivityController multimediaController = Robolectric.buildActivity(MultimediaEditFieldActivity.class, intent) | ||
.create().start().resume().visible(); | ||
MultimediaEditFieldActivity testCardTemplatePreviewer = (MultimediaEditFieldActivity) multimediaController.get(); | ||
|
||
|
||
|
||
return testCardTemplatePreviewer.getFieldController(); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...oid/src/test/java/com/ichi2/anki/multimediacard/fields/BasicImageFieldControllerTest.java
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.ichi2.anki.multimediacard.fields; | ||
|
||
import com.ichi2.anki.multimediacard.activity.MultimediaEditFieldActivityTestBase; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.robolectric.RobolectricTestRunner; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.instanceOf; | ||
|
||
|
||
@RunWith(RobolectricTestRunner.class) | ||
public class BasicImageFieldControllerTest extends MultimediaEditFieldActivityTestBase { | ||
|
||
@Test | ||
public void constructionWithoutDataGivesNoError() { | ||
grantCameraPermission(); | ||
|
||
IFieldController controller = getControllerForField(emptyImageField(), getEmptyNote(), 0); | ||
|
||
assertThat(controller, instanceOf(BasicImageFieldController.class)); | ||
} | ||
|
||
@Test | ||
public void constructionWithDataSucceeds() { | ||
grantCameraPermission(); | ||
|
||
IFieldController controller = getControllerForField(imageFieldWithData(), getEmptyNote(), 0); | ||
|
||
assertThat(controller, instanceOf(BasicImageFieldController.class)); | ||
} | ||
|
||
private static IField emptyImageField() { | ||
return new ImageField(); | ||
} | ||
|
||
private static IField imageFieldWithData() { | ||
IField field = emptyImageField(); | ||
field.setImagePath("test"); | ||
return field; | ||
} | ||
} |