Skip to content

Commit

Permalink
Make Locale-specific test more reliable by deactivating LeakCanary an…
Browse files Browse the repository at this point in the history
…d by granting CHANGE_CONFIGURATION permission before running LocaleTestRule
  • Loading branch information
mtotschnig committed Nov 18, 2023
1 parent 4f060bb commit 1bf680c
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 80 deletions.
6 changes: 4 additions & 2 deletions Screengrabfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# ./gradlew assembleExternDebug assembleAndroidTest
# ./gradlew assembleExternDebug assembleExternAndroidTest
# fastlane screengrab
app_package_name('org.totschnig.myexpenses.debug')

Expand Down Expand Up @@ -52,4 +52,6 @@ clear_previous_screenshots(false)

use_timestamp_suffix(false)

launch_arguments(["scenario 1"])
launch_arguments(["scenario 1", "screenshots true"])

# specific_device("emulator-5556")
4 changes: 3 additions & 1 deletion myExpenses/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,9 @@ tasks.withType(Test) {
}

dependencies {
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.10'
//LeakCanary leads to failure of Screengrab's LocaleUtil:
//https://github.com/fastlane/fastlane/issues/19521#issuecomment-1170017435
//debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.10'
implementation("androidx.datastore:datastore-preferences:1.0.0")
coreLibraryDesugaring "com.android.tools:desugar_jdk_libs:$desugarVersion"
implementation project(':transactionscontract')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,13 @@
package org.totschnig.myexpenses.test.screenshots

import android.Manifest
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.rule.GrantPermissionRule
import org.junit.Rule
import androidx.test.filters.LargeTest
import org.junit.Test
import tools.fastlane.screengrab.locale.LocaleTestRule

class ArabTest: TestMain() {

@get:Rule
val grantPermissionRule: GrantPermissionRule = GrantPermissionRule.grant(
Manifest.permission.WRITE_CALENDAR, Manifest.permission.READ_CALENDAR
)

@Rule
@JvmField
val localeTestRule = LocaleTestRule(locale)
@LargeTest
class ArabTest: TestMain("ar-SA") {

@Test
fun runArab() {
runScenario("1", locale)
}

companion object {
const val locale = "ar-SA"
runScenario("1")
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,14 @@
package org.totschnig.myexpenses.test.screenshots

import android.Manifest
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.rule.GrantPermissionRule
import org.junit.Rule
import androidx.test.filters.LargeTest
import org.junit.Test
import tools.fastlane.screengrab.locale.LocaleTestRule

class JapaneseTest: TestMain() {
@get:Rule
val grantPermissionRule: GrantPermissionRule = GrantPermissionRule.grant(
Manifest.permission.WRITE_CALENDAR, Manifest.permission.READ_CALENDAR
)

@Rule
@JvmField
val localeTestRule = LocaleTestRule(locale)
@LargeTest
class JapaneseTest: TestMain("ja-JP") {

@Test
fun runJapanese() {
runScenario("1", locale)
}

companion object {
const val locale = "ja-JP"
runScenario("1")
}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
package org.totschnig.myexpenses.test.screenshots

import android.Manifest
import androidx.test.filters.LargeTest
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.rule.GrantPermissionRule
import org.junit.Rule
import org.junit.Test
import tools.fastlane.screengrab.locale.LocaleTestRule
import tools.fastlane.screengrab.locale.LocaleUtil

/**
* When not run from ScreenGrab, it runs with device locale
*/
class ScreenGrabTest: TestMain() {

@get:Rule
val grantPermissionRule: GrantPermissionRule = GrantPermissionRule.grant(
Manifest.permission.WRITE_CALENDAR, Manifest.permission.READ_CALENDAR
)
@LargeTest
class ScreenGrabTest: TestMain(null) {

@Rule
@JvmField
val localeTestRule = LocaleTestRule()

override val shouldTakeScreenShot = true
override val shouldTakeScreenShot = getInstrumentationArgument("screenshots", "1") == "true"

@Test
fun mkScreenshots() {
val scenario = InstrumentationRegistry.getArguments().getString("scenario", "1")
runScenario(scenario, LocaleUtil.getTestLocale())
val scenario = getInstrumentationArgument("scenario", "1")
runScenario(scenario)
}

private fun getInstrumentationArgument(key: String, @Suppress("SameParameterValue") defaultValue: String) =
InstrumentationRegistry.getArguments().getString(key, defaultValue)
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package org.totschnig.myexpenses.test.screenshots

import android.content.res.Configuration
import android.content.res.Resources
import androidx.appcompat.app.AppCompatDelegate
import android.Manifest
import android.content.Context
import android.os.Build
import androidx.compose.ui.test.assertTextContains
import androidx.compose.ui.test.filter
import androidx.compose.ui.test.hasText
import androidx.compose.ui.test.onChildren
import androidx.compose.ui.test.onFirst
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.core.os.LocaleListCompat
import androidx.recyclerview.widget.RecyclerView
import androidx.test.espresso.Espresso.closeSoftKeyboard
import androidx.test.espresso.Espresso.onIdle
Expand All @@ -25,11 +24,16 @@ import androidx.test.espresso.contrib.RecyclerViewActions.actionOnItemAtPosition
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation
import androidx.test.rule.GrantPermissionRule
import androidx.test.uiautomator.UiDevice
import org.hamcrest.Matchers.containsString
import org.junit.After
import org.junit.AfterClass
import org.junit.Before
import org.junit.BeforeClass
import org.junit.Rule
import org.junit.rules.RuleChain
import org.junit.rules.TestRule
import org.totschnig.myexpenses.BuildConfig
import org.totschnig.myexpenses.R
import org.totschnig.myexpenses.preference.PrefKey
Expand All @@ -39,25 +43,50 @@ import org.totschnig.myexpenses.testutils.withPositionInParent
import org.totschnig.myexpenses.util.distrib.DistributionHelper.versionNumber
import tools.fastlane.screengrab.Screengrab
import tools.fastlane.screengrab.cleanstatusbar.CleanStatusBar
import tools.fastlane.screengrab.locale.LocaleTestRule
import tools.fastlane.screengrab.locale.LocaleUtil

abstract class TestMain : BaseMyExpensesTest() {
import java.util.Locale

abstract class TestMain(locale: String?) : BaseMyExpensesTest() {

@Rule
@JvmField
val chain: TestRule = RuleChain
.outerRule(
GrantPermissionRule.grant(
*buildList {
add(Manifest.permission.WRITE_CALENDAR)
add(Manifest.permission.READ_CALENDAR)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
add(Manifest.permission.POST_NOTIFICATIONS)
}
add(Manifest.permission.CHANGE_CONFIGURATION)
}.toTypedArray()
)
)
.around(
locale?.let { LocaleTestRule(it) } ?: LocaleTestRule()
)

open val shouldTakeScreenShot = false


@Before
fun configureLocale() {
LocaleUtil.localeFromString(LocaleUtil.getTestLocale())?.let {
//targetContext.updateWith(it)
testContext.updateWith(it)
}
}

@After
fun cleanUp() {
app.fixture.cleanup(contentResolver)
}

fun runScenario(scenario: String, locale: String?) {
loadFixture(scenario == "2", locale)
fun runScenario(scenario: String) {
loadFixture(scenario == "2")
scenario(scenario)
testScenario.onActivity {
it.runOnUiThread {
AppCompatDelegate.setApplicationLocales(LocaleListCompat.getEmptyLocaleList())
}
}
}

private fun drawerAction(action: ViewAction) {
Expand All @@ -82,6 +111,10 @@ abstract class TestMain : BaseMyExpensesTest() {
listNode.onChildren().onFirst()
.assertTextContains(getString(R.string.split_transaction), substring = true)
clickContextItem(R.string.details)
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.Q) {
//https://github.com/android/android-test/issues/444
Thread.sleep(500)
}
onView(withId(android.R.id.button1)).perform(click())
closeSoftKeyboard()
takeScreenshot("split")
Expand Down Expand Up @@ -118,7 +151,9 @@ abstract class TestMain : BaseMyExpensesTest() {
onView(withText(containsString("Drive"))).perform(click())
onView(withText(containsString("Dropbox"))).perform(click())
onView(withText(containsString("WebDAV"))).perform(scrollTo(), click())
Thread.sleep(5000)
if(shouldTakeScreenShot) {
Thread.sleep(5000)
}
takeScreenshot("sync")
}

Expand Down Expand Up @@ -150,27 +185,20 @@ abstract class TestMain : BaseMyExpensesTest() {
}
}

private fun loadFixture(withPicture: Boolean, locale: String?) {
//LocaleTestRule only configure for app context, fixture loads resources from instrumentation context
LocaleUtil.localeFromString(locale)?.let {
AppCompatDelegate.setApplicationLocales(LocaleListCompat.create(it))
val config = Configuration()
config.locale = it
testContext.resources.update(config)
targetContext.resources.update(config)
}
private fun loadFixture(withPicture: Boolean) {
unlock()
app.fixture.setup(withPicture, repository, app.appComponent.plannerUtils(), homeCurrency)
prefHandler.putInt(PrefKey.CURRENT_VERSION, versionNumber)
prefHandler.putInt(PrefKey.FIRST_INSTALL_VERSION, versionNumber)
launch(app.fixture.account1.id)
}

private fun Resources.update(configuration: Configuration) {
updateConfiguration(configuration, displayMetrics)
private fun Context.updateWith(locale: Locale) {
val config = resources.configuration
config.setLocale(locale)
resources.updateConfiguration(config, resources.displayMetrics)
}


private fun takeScreenshot(fileName: String) {
if (shouldTakeScreenShot) {
onIdle()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import androidx.core.database.getLongOrNull
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ProcessLifecycleOwner
import com.google.android.material.color.DynamicColors
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
Expand Down

0 comments on commit 1bf680c

Please sign in to comment.