Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ebf03681 authored by andrewxu's avatar andrewxu Committed by Andrew Xu
Browse files

Avoid reusing the same battery view model instance in Kosmos

This change ensures a new model instance is always created via
factory.create() instead of reusing an existing one. Reusing the
same instance caused an error when attempting to activate an
already-activated model, which was discovered in the new test at
ag/35064871.

Bug:None
Flag:TEST_ONLY
Change-Id: Ib3f449796f04fc2d6b9daa04c5388ddc69c8276e
parent 798e5cd0
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -39,7 +39,7 @@ import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class BatteryViewModelAlwaysShowPercentTest : SysuiTestCase() {
    val kosmos = testKosmos()
    val Kosmos.underTest by Kosmos.Fixture { batteryViewModelAlwaysShowPercent }
    val Kosmos.underTest by Kosmos.Fixture { batteryViewModelAlwaysShowPercentFactory.create() }

    @Before
    fun setUp() {
+1 −1
Original line number Diff line number Diff line
@@ -41,7 +41,7 @@ import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class BatteryViewModelBasedOnSettingTest : SysuiTestCase() {
    val kosmos = testKosmos()
    val Kosmos.underTest by Kosmos.Fixture { batteryViewModelBasedOnSetting }
    val Kosmos.underTest by Kosmos.Fixture { batteryViewModelBasedOnSettingFactory.create() }

    @Before
    fun setUp() {
+3 −1
Original line number Diff line number Diff line
@@ -39,7 +39,9 @@ import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class BatteryViewModelShowWhenChargingTest : SysuiTestCase() {
    val kosmos = testKosmos()
    val Kosmos.underTest by Kosmos.Fixture { batteryViewModelShowWhenChargingOrSetting }
    val Kosmos.underTest by Kosmos.Fixture {
        batteryViewModelShowWhenChargingOrSettingFactory.create()
    }

    @Before
    fun setUp() {
+15 −18
Original line number Diff line number Diff line
@@ -20,40 +20,37 @@ import android.content.testableContext
import com.android.systemui.kosmos.Kosmos
import com.android.systemui.statusbar.pipeline.battery.domain.interactor.batteryInteractor

val Kosmos.batteryViewModelBasedOnSetting by
    Kosmos.Fixture { BatteryViewModel.BasedOnUserSetting(batteryInteractor, testableContext) }

val Kosmos.batteryViewModelBasedOnSettingFactory by
    Kosmos.Fixture {
        BatteryViewModel.BasedOnUserSetting.Factory { batteryViewModelBasedOnSetting }
        BatteryViewModel.BasedOnUserSetting.Factory {
            BatteryViewModel.BasedOnUserSetting(
                batteryInteractor,
                testableContext
            )
        }

val Kosmos.batteryViewModelShowWhenChargingOrSetting by
    Kosmos.Fixture {
        BatteryViewModel.ShowPercentWhenChargingOrSetting(batteryInteractor, testableContext)
    }

val Kosmos.batteryViewModelShowWhenChargingOrSettingFactory by
    Kosmos.Fixture {
        BatteryViewModel.ShowPercentWhenChargingOrSetting.Factory {
            batteryViewModelShowWhenChargingOrSetting
            BatteryViewModel.ShowPercentWhenChargingOrSetting(batteryInteractor, testableContext)
        }
    }

val Kosmos.batteryViewModelAlwaysShowPercent by
    Kosmos.Fixture { BatteryViewModel.AlwaysShowPercent(batteryInteractor, testableContext) }

val Kosmos.batteryViewModelAlwaysShowPercentFactory by
    Kosmos.Fixture {
        BatteryViewModel.AlwaysShowPercent.Factory { batteryViewModelAlwaysShowPercent }
        BatteryViewModel.AlwaysShowPercent.Factory {
            BatteryViewModel.AlwaysShowPercent(
                batteryInteractor,
                testableContext
            )
        }
    }

val Kosmos.batteryWithPercentViewModel by
    Kosmos.Fixture { BatteryNextToPercentViewModel(batteryInteractor, testableContext) }

val Kosmos.batteryWithPercentViewModelFactory by
    Kosmos.Fixture {
        object : BatteryNextToPercentViewModel.Factory {
            override fun create(): BatteryNextToPercentViewModel = batteryWithPercentViewModel
            override fun create(): BatteryNextToPercentViewModel =
                BatteryNextToPercentViewModel(batteryInteractor, testableContext)
        }
    }