From bb31a0d47b6da986ea872f26a2e15978e3888ed8 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] test: android test for MultimediaFragment intents --- AnkiDroid/build.gradle | 1 + AnkiDroid/proguard-rules.pro | 2 + .../java/com/ichi2/anki/MultimediaTest.kt | 120 ++++++++++++++++++ gradle/libs.versions.toml | 2 + 4 files changed, 125 insertions(+) create mode 100644 AnkiDroid/src/androidTest/java/com/ichi2/anki/MultimediaTest.kt diff --git a/AnkiDroid/build.gradle b/AnkiDroid/build.gradle index 05e25219ef0e..7cc0ff46bc5e 100644 --- a/AnkiDroid/build.gradle +++ b/AnkiDroid/build.gradle @@ -317,6 +317,7 @@ dependencies { } api project(":api") implementation libs.androidx.work.runtime + implementation libs.androidx.espresso.intents lintChecks project(":lint-rules") coreLibraryDesugaring libs.desugar.jdk.libs.nio diff --git a/AnkiDroid/proguard-rules.pro b/AnkiDroid/proguard-rules.pro index 4c3fa9bcccc3..b9bc908535eb 100644 --- a/AnkiDroid/proguard-rules.pro +++ b/AnkiDroid/proguard-rules.pro @@ -28,6 +28,8 @@ -keep class * extends com.google.protobuf.GeneratedMessageLite { *; } -keep class androidx.core.app.ActivityCompat$* { *; } -keep class androidx.concurrent.futures.** { *; } +-keep class * extends androidx.work.InputMerger + # Ignore unused packages -dontwarn javax.naming.** 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..72b744dc504a --- /dev/null +++ b/AnkiDroid/src/androidTest/java/com/ichi2/anki/MultimediaTest.kt @@ -0,0 +1,120 @@ +/* + * 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 android.provider.MediaStore +import androidx.test.core.app.ActivityScenario +import androidx.test.espresso.intent.Intents +import androidx.test.espresso.intent.Intents.intended +import androidx.test.espresso.intent.matcher.IntentMatchers.hasAction +import androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent +import com.ichi2.anki.multimedia.MultimediaActivity +import com.ichi2.anki.multimedia.MultimediaActivityExtra +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 + +@RunWith(Parameterized::class) +class MultimediaTest : InstrumentedTest() { + + @JvmField + @Parameterized.Parameter(0) + var intentBuilder: (Context) -> Intent? = { null } + + @JvmField + @Parameterized.Parameter(1) + var expectedAction: String? = null + + @JvmField + @Parameterized.Parameter(2) + var isComponentCheck: Boolean = false + + @Test + fun testIntentFired() { + Intents.init() + + try { + ActivityScenario.launch(intentBuilder(testContext)).use { scenario -> + scenario.onActivity { activity -> + activity.supportFragmentManager.findFragmentById(R.id.fragment_container) as MultimediaImageFragment + } + + if (isComponentCheck) { + intended(hasComponent(expectedAction!!)) + } else { + intended(hasAction(expectedAction)) + } + } + } finally { + Intents.release() + } + } + + companion object { + @Parameterized.Parameters(name = "{index}: {1}") + @JvmStatic + fun initParameters(): Collection> { + return listOf( + arrayOf({ context: Context -> getImageFragment(context) }, MediaStore.ACTION_IMAGE_CAPTURE, false), + arrayOf({ context: Context -> getGalleryFragment(context) }, Intent.ACTION_PICK, false), + arrayOf({ context: Context -> getDrawingFragment(context) }, DrawingActivity::class.java.name, true) + ) + } + + private val multimediaActivityExtra = MultimediaActivityExtra(0, ImageField(), getTestMultimediaNote()) + + private fun getImageFragment(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 getTestMultimediaNote(): MultimediaEditableNote { + val note = MultimediaEditableNote() + note.setNumFields(1) + note.setField(0, TextField()) + note.freezeInitialFieldValues() + return note + } + } +} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 2fef10b66282..069d938cc98b 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -103,6 +103,7 @@ timber = "5.0.1" # In the past, releases have been published before the changelog triplet = "3.11.0" turbine = "1.1.0" +espressoIntents = "3.6.1" [libraries] acra-limiter = { module = "ch.acra:acra-limiter", version.ref = "acra" } @@ -165,6 +166,7 @@ androidx-work-runtime = { group = "androidx.work", name = "work-runtime-ktx", ve #testing libs androidx-espresso-contrib = { module = "androidx.test.espresso:espresso-contrib", version.ref = "espresso" } +androidx-espresso-intents = { module = "androidx.test.espresso:espresso-intents", version.ref = "espressoIntents" } androidx-espresso-core = { module = "androidx.test.espresso:espresso-core", version.ref = "espresso" } androidx-fragment-testing = { module = "androidx.fragment:fragment-testing", version.ref = "androidxFragmentKtx" } androidx-fragment-testing-manifest = { module = "androidx.fragment:fragment-testing-manifest", version.ref = "androidxFragmentKtx" }