From f13818deb41629539c76b737312bc85fce7b9e1b Mon Sep 17 00:00:00 2001 From: Ashish Yadav <48384865+criticalAY@users.noreply.github.com> Date: Sun, 20 Oct 2024 15:55:07 +0530 Subject: [PATCH 1/4] test: instrumented test to test multimedia titles --- .../java/com/ichi2/anki/MultimediaTest.kt | 184 ++++++++++++++++++ 1 file changed, 184 insertions(+) create mode 100644 AnkiDroid/src/androidTest/java/com/ichi2/anki/MultimediaTest.kt diff --git a/AnkiDroid/src/androidTest/java/com/ichi2/anki/MultimediaTest.kt b/AnkiDroid/src/androidTest/java/com/ichi2/anki/MultimediaTest.kt new file mode 100644 index 000000000000..817b1fa141c6 --- /dev/null +++ b/AnkiDroid/src/androidTest/java/com/ichi2/anki/MultimediaTest.kt @@ -0,0 +1,184 @@ +/* + * Copyright (c) 2024 Ashish Yadav + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation; either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +package com.ichi2.anki + +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 +import androidx.test.espresso.matcher.RootMatchers.isDialog +import androidx.test.espresso.matcher.ViewMatchers.isDisplayed +import androidx.test.espresso.matcher.ViewMatchers.withText +import com.ichi2.anki.multimedia.AudioRecordingFragment +import com.ichi2.anki.multimedia.AudioVideoFragment +import com.ichi2.anki.multimedia.MultimediaActivity +import com.ichi2.anki.multimedia.MultimediaActivityExtra +import com.ichi2.anki.multimedia.MultimediaFragment +import com.ichi2.anki.multimedia.MultimediaImageFragment +import com.ichi2.anki.multimediacard.fields.ImageField +import com.ichi2.anki.multimediacard.fields.TextField +import com.ichi2.anki.multimediacard.impl.MultimediaEditableNote +import com.ichi2.anki.tests.InstrumentedTest +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.Parameterized +import kotlin.test.assertEquals + +@RunWith(Parameterized::class) +class MultimediaTest : InstrumentedTest() { + + @JvmField + @Parameterized.Parameter(0) + var intentBuilder: (Context) -> Intent? = { null } + + @JvmField + @Parameterized.Parameter(1) + 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() { + withMultimediaActivityScenario { scenario -> + scenario.onActivity { activity -> + val actualFragmentTitle = (activity.fragmentContainer as MultimediaFragment).title + assertEquals(expectedTitle, actualFragmentTitle, message = "title") + } + } + } + + @Test + fun testImageFragmentDiscardDialogShown() { + val validIntentBuilders = setOf( + { context: Context -> getCameraFragment(context) }, + { context: Context -> getGalleryFragment(context) }, + { context: Context -> getDrawingFragment(context) } + ) + + if (intentBuilder !in validIntentBuilders) { + return + } + + withMultimediaActivityScenario { scenario -> + scenario.onActivity { activity -> + (activity.fragmentContainer as MultimediaImageFragment).apply { + viewModel.updateCurrentMultimediaPath("test/path") + requireActivity().onBackPressedDispatcher.onBackPressed() + } + } + + onView(withText(CollectionManager.TR.addingDiscardCurrentInput())) + .inRoot(isDialog()) + .check(matches(isDisplayed())) + + onView(withText(R.string.discard)) + .check(matches(isDisplayed())) + + onView(withText(CollectionManager.TR.addingKeepEditing())) + .check(matches(isDisplayed())) + } + } + + /** Runs [ActivityScenario.launch] with the result of the [intentBuilder] */ + private fun withMultimediaActivityScenario(block: (ActivityScenario) -> Unit) { + ActivityScenario.launch(intentBuilder(testContext)).use { block(it) } + } + + private val MultimediaActivity.fragmentContainer: Fragment + get() = this.supportFragmentManager.findFragmentById(R.id.fragment_container)!! + + companion object { + @Parameterized.Parameters(name = "{2}") + @JvmStatic + fun initParameters(): Collection> { + return listOf( + 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 getCameraFragment(context: Context): Intent { + return MultimediaImageFragment.getIntent( + context, + multimediaActivityExtra, + MultimediaImageFragment.ImageOptions.CAMERA + ) + } + + private fun getGalleryFragment(context: Context): Intent { + return MultimediaImageFragment.getIntent( + context, + multimediaActivityExtra, + MultimediaImageFragment.ImageOptions.GALLERY + ) + } + + private fun getDrawingFragment(context: Context): Intent { + return MultimediaImageFragment.getIntent( + context, + multimediaActivityExtra, + MultimediaImageFragment.ImageOptions.DRAWING + ) + } + + private fun getVideoFragment(context: Context): Intent { + return AudioVideoFragment.getIntent( + context, + multimediaActivityExtra, + AudioVideoFragment.MediaOption.VIDEO_CLIP + ) + } + + private fun getAudioFragment(context: Context): Intent { + return AudioVideoFragment.getIntent( + context, + multimediaActivityExtra, + AudioVideoFragment.MediaOption.AUDIO_CLIP + ) + } + + private fun getAudioRecordingFragment(context: Context): Intent { + return AudioRecordingFragment.getIntent( + context, + multimediaActivityExtra + ) + } + + private fun getTestMultimediaNote(): MultimediaEditableNote { + val note = MultimediaEditableNote() + note.setNumFields(1) + note.setField(0, TextField()) + note.freezeInitialFieldValues() + return note + } + } +} From 9140852c8cf8d667a83b792f9d38ba3b4252039d Mon Sep 17 00:00:00 2001 From: Ashish Yadav <48384865+criticalAY@users.noreply.github.com> Date: Mon, 9 Dec 2024 22:29:34 +0530 Subject: [PATCH 2/4] refactor: title test --- .../java/com/ichi2/anki/MultimediaTest.kt | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/AnkiDroid/src/androidTest/java/com/ichi2/anki/MultimediaTest.kt b/AnkiDroid/src/androidTest/java/com/ichi2/anki/MultimediaTest.kt index 817b1fa141c6..679b851083b8 100644 --- a/AnkiDroid/src/androidTest/java/com/ichi2/anki/MultimediaTest.kt +++ b/AnkiDroid/src/androidTest/java/com/ichi2/anki/MultimediaTest.kt @@ -48,24 +48,15 @@ class MultimediaTest : InstrumentedTest() { @Parameterized.Parameter(0) var intentBuilder: (Context) -> Intent? = { null } - @JvmField - @Parameterized.Parameter(1) - var expectedTitleRes: Int? = null - - @Suppress("unused") // used by "{2}" - @JvmField - @Parameterized.Parameter(2) - var testName: String = "" - - private val expectedTitle - get() = testContext.getString(expectedTitleRes!!) + private var title: Int? = null @Test fun testFragmentTitle() { - withMultimediaActivityScenario { scenario -> + ActivityScenario.launch(intentBuilder(testContext)).use { scenario -> scenario.onActivity { activity -> - val actualFragmentTitle = (activity.fragmentContainer as MultimediaFragment).title - assertEquals(expectedTitle, actualFragmentTitle, message = "title") + val fragment = activity.supportFragmentManager.findFragmentById(R.id.fragment_container) as MultimediaFragment + val titleString = title?.let { testContext.getString(it) } + assertEquals(titleString, fragment.title) } } } From 201191eae6ce63c77b2cfbb18f9aadf5ac91c0f2 Mon Sep 17 00:00:00 2001 From: Ashish Yadav <48384865+criticalAY@users.noreply.github.com> Date: Mon, 9 Dec 2024 22:43:31 +0530 Subject: [PATCH 3/4] refactor: title test --- .../java/com/ichi2/anki/MultimediaTest.kt | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/AnkiDroid/src/androidTest/java/com/ichi2/anki/MultimediaTest.kt b/AnkiDroid/src/androidTest/java/com/ichi2/anki/MultimediaTest.kt index 679b851083b8..0b38c70f66ce 100644 --- a/AnkiDroid/src/androidTest/java/com/ichi2/anki/MultimediaTest.kt +++ b/AnkiDroid/src/androidTest/java/com/ichi2/anki/MultimediaTest.kt @@ -52,7 +52,7 @@ class MultimediaTest : InstrumentedTest() { @Test fun testFragmentTitle() { - ActivityScenario.launch(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) } @@ -102,16 +102,16 @@ class MultimediaTest : InstrumentedTest() { get() = this.supportFragmentManager.findFragmentById(R.id.fragment_container)!! companion object { - @Parameterized.Parameters(name = "{2}") + @Parameterized.Parameters(name = "{index}: {1}") @JvmStatic fun initParameters(): Collection> { return listOf( - 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") + arrayOf({ context: Context -> getCameraFragment(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) ) } From 65a0fea5fa61617dae742fbc84c8b7fc854bd11e Mon Sep 17 00:00:00 2001 From: Ashish Yadav <48384865+criticalAY@users.noreply.github.com> Date: Mon, 9 Dec 2024 22:56:36 +0530 Subject: [PATCH 4/4] refactor: title test --- .../src/androidTest/java/com/ichi2/anki/MultimediaTest.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/AnkiDroid/src/androidTest/java/com/ichi2/anki/MultimediaTest.kt b/AnkiDroid/src/androidTest/java/com/ichi2/anki/MultimediaTest.kt index 0b38c70f66ce..a4a77ab62b70 100644 --- a/AnkiDroid/src/androidTest/java/com/ichi2/anki/MultimediaTest.kt +++ b/AnkiDroid/src/androidTest/java/com/ichi2/anki/MultimediaTest.kt @@ -48,7 +48,9 @@ class MultimediaTest : InstrumentedTest() { @Parameterized.Parameter(0) var intentBuilder: (Context) -> Intent? = { null } - private var title: Int? = null + @JvmField + @Parameterized.Parameter(1) + var title: Int? = null @Test fun testFragmentTitle() {