Fix flaky tests due to shared system property

The system property is shared within JVM, change system property in a
test case might break test cases in another test class. To address the
issue, introduce SystemProperty helper class to back up and restore the
system properties in tests.

Bug: 373177618
Flag: EXEMPT Test only
Test: atest SettingsRoboTests
Change-Id: I15539ce5ac425f35571d796baa25f259df1b601f
This commit is contained in:
Jacky Wang
2024-10-15 16:43:05 +08:00
parent 8d30101228
commit 3ab6b197a9
4 changed files with 86 additions and 10 deletions

View File

@@ -21,9 +21,9 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.internal.widget.LockPatternUtils
import com.android.settings.flags.Flags
import com.android.settings.testutils.FakeFeatureFactory
import com.android.settings.testutils.SystemProperty
import com.android.settingslib.preference.CatalystScreenTestCase
import com.google.common.truth.Truth.assertThat
import org.junit.Ignore
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers.anyInt
@@ -66,16 +66,16 @@ class DisplayScreenTest : CatalystScreenTestCase() {
assertThat(preferenceScreenCreator.isAvailable(contextWrapper)).isFalse()
}
@Ignore("robolectric.createActivityContexts cause other test failure")
override fun migration() {
// avoid UnsupportedOperationException when getDisplay from context
System.setProperty("robolectric.createActivityContexts", "true")
SystemProperty("robolectric.createActivityContexts", "true").use {
val lockPatternUtils =
mock<LockPatternUtils> { on { isSecure(anyInt()) } doReturn true }
FakeFeatureFactory.setupForTest().securityFeatureProvider.stub {
on { getLockPatternUtils(any()) } doReturn lockPatternUtils
}
val lockPatternUtils = mock<LockPatternUtils> { on { isSecure(anyInt()) } doReturn true }
FakeFeatureFactory.setupForTest().securityFeatureProvider.stub {
on { getLockPatternUtils(any()) } doReturn lockPatternUtils
super.migration()
}
super.migration()
}
}