-
-
Notifications
You must be signed in to change notification settings - Fork 2.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
test: android test for MultimediaFragment intents #17286
base: main
Are you sure you want to change the base?
Conversation
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.
Check the android test failures they seem related to your changes.
9546eda
to
bb31a0d
Compare
bb31a0d
to
094b1cc
Compare
094b1cc
to
1e855ae
Compare
1e855ae
to
cf2ad33
Compare
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
@criticalAY with apologies for the delay on this one, is
Needs Author Reply
|
Up for review |
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.
Take it or leave it. I'm happy either way, and CI is happy. I'm more than happy with more tests 😁
Let me know if you want an explanation on anything here.
Index: AnkiDroid/src/androidTest/java/com/ichi2/anki/MultimediaTest.kt
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/AnkiDroid/src/androidTest/java/com/ichi2/anki/MultimediaTest.kt b/AnkiDroid/src/androidTest/java/com/ichi2/anki/MultimediaTest.kt
--- a/AnkiDroid/src/androidTest/java/com/ichi2/anki/MultimediaTest.kt (revision cf2ad33710b66061e3a276d57334cdaf3afeb135)
+++ b/AnkiDroid/src/androidTest/java/com/ichi2/anki/MultimediaTest.kt (date 1733658217414)
@@ -19,6 +19,7 @@
import android.content.Context
import android.content.Intent
+import androidx.fragment.app.Fragment
import androidx.test.core.app.ActivityScenario
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.assertion.ViewAssertions.matches
@@ -38,7 +39,6 @@
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
-import kotlin.test.DefaultAsserter.assertEquals
import kotlin.test.assertEquals
@RunWith(Parameterized::class)
@@ -50,15 +50,22 @@
@JvmField
@Parameterized.Parameter(1)
- var title: Int? = null
+ var expectedTitleRes: Int? = null
+
+ @Suppress("unused") // used by "{2}"
+ @JvmField
+ @Parameterized.Parameter(2)
+ var testName: String = ""
+
+ private val expectedTitle
+ get() = testContext.getString(expectedTitleRes!!)
@Test
fun testFragmentTitle() {
- ActivityScenario.launch<MultimediaActivity>(intentBuilder(testContext)).use { scenario ->
+ withMultimediaActivityScenario { scenario ->
scenario.onActivity { activity ->
- val fragment = activity.supportFragmentManager.findFragmentById(R.id.fragment_container) as MultimediaFragment
- val titleString = title?.let { testContext.getString(it) }
- assertEquals(titleString, fragment.title)
+ val actualFragmentTitle = (activity.fragmentContainer as MultimediaFragment).title
+ assertEquals(expectedTitle, actualFragmentTitle, message = "title")
}
}
}
@@ -66,7 +73,7 @@
@Test
fun testImageFragmentDiscardDialogShown() {
val validIntentBuilders = setOf(
- { context: Context -> getImageFragment(context) },
+ { context: Context -> getCameraFragment(context) },
{ context: Context -> getGalleryFragment(context) },
{ context: Context -> getDrawingFragment(context) }
)
@@ -75,11 +82,12 @@
return
}
- ActivityScenario.launch<MultimediaActivity>(intentBuilder(testContext)).use { scenario ->
+ withMultimediaActivityScenario { scenario ->
scenario.onActivity { activity ->
- val fragment = activity.supportFragmentManager.findFragmentById(R.id.fragment_container) as MultimediaImageFragment
- fragment.viewModel.updateCurrentMultimediaPath("test/path")
- fragment.requireActivity().onBackPressedDispatcher.onBackPressed()
+ (activity.fragmentContainer as MultimediaImageFragment).apply {
+ viewModel.updateCurrentMultimediaPath("test/path")
+ requireActivity().onBackPressedDispatcher.onBackPressed()
+ }
}
onView(withText(CollectionManager.TR.addingDiscardCurrentInput()))
@@ -94,23 +102,32 @@
}
}
+ /** Runs [ActivityScenario.launch] with the result of the [intentBuilder] */
+ private fun withMultimediaActivityScenario(block: (ActivityScenario<MultimediaActivity>) -> Unit) {
+ ActivityScenario.launch<MultimediaActivity>(intentBuilder(testContext)).use { block(it) }
+ }
+
+ private val MultimediaActivity.fragmentContainer: Fragment
+ get() = this.supportFragmentManager.findFragmentById(R.id.fragment_container)!!
+
+
companion object {
- @Parameterized.Parameters(name = "{index}: {1}")
+ @Parameterized.Parameters(name = "{2}")
@JvmStatic
fun initParameters(): Collection<Array<out Any>> {
return listOf(
- arrayOf({ context: Context -> getImageFragment(context) }, R.string.multimedia_editor_popup_image),
- arrayOf({ context: Context -> getGalleryFragment(context) }, R.string.multimedia_editor_popup_image),
- arrayOf({ context: Context -> getDrawingFragment(context) }, R.string.multimedia_editor_popup_image),
- arrayOf({ context: Context -> getAudioFragment(context) }, R.string.multimedia_editor_popup_audio_clip),
- arrayOf({ context: Context -> getVideoFragment(context) }, R.string.multimedia_editor_popup_video_clip),
- arrayOf({ context: Context -> getAudioRecordingFragment(context) }, R.string.multimedia_editor_field_editing_audio)
+ arrayOf({ context: Context -> getCameraFragment(context) }, R.string.multimedia_editor_popup_image, "Add image (Camera)"),
+ arrayOf({ context: Context -> getGalleryFragment(context) }, R.string.multimedia_editor_popup_image, "Add image (Gallery)"),
+ arrayOf({ context: Context -> getDrawingFragment(context) }, R.string.multimedia_editor_popup_image, "Add image (Drawing)"),
+ arrayOf({ context: Context -> getAudioFragment(context) }, R.string.multimedia_editor_popup_audio_clip, "Add audio clip"),
+ arrayOf({ context: Context -> getVideoFragment(context) }, R.string.multimedia_editor_popup_video_clip, "Add video clip"),
+ arrayOf({ context: Context -> getAudioRecordingFragment(context) }, R.string.multimedia_editor_field_editing_audio, "Record audio")
)
}
private val multimediaActivityExtra = MultimediaActivityExtra(0, ImageField(), getTestMultimediaNote())
- private fun getImageFragment(context: Context): Intent {
+ private fun getCameraFragment(context: Context): Intent {
return MultimediaImageFragment.getIntent(
context,
multimediaActivityExtra,
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.
Ok, I didn't had any issues with the code itself just with how useful I perceived it to be.
cf2ad33
to
f13818d
Compare
@david-allison thanks! applied the patch |
That's not a good sign - adding a flaky test is worse than adding no test, this needs real investigation + real fix so it isn't flaky |
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.
flaky
I am reverting the patch and will run 20 iterations lets see then |
@criticalAY to confirm: we have a GitHub action you can run for the 20x testing |
It is not poping up for emulated test but unit |
The Flake Hammer (tm) was implemented unit test only |
I have run emulated tests 16 times by manually triggering the CI, all green there |
Purpose / Description
Test for multimedia activity intents i.e. camera, gallery & drawing
Approach
Use
libs.androidx.espresso.intents
to check if the intents are there or not, this will be useful to test other components too I.e cropper, audio recorder, etcHow Has This Been Tested?
Tested locally using
./gradlew connectedplayDebugAndroidTest
Checklist
Please, go through these checks before submitting the PR.