Show "0 B used" when no data usage this cycle

When the latest cycle has no data usage, currently it shows all time
usage without cycle info, which could confuse user.

Change to "0 B used used xxx - xxx" to fix this issue.

Fix: 292346951
Test: manual - on Mobile Settings
Test: unit tests
Change-Id: Ic06fd63a3bc049d70538d0a3cd1fa3d62dbd71d7
This commit is contained in:
Chaohui Wang
2024-01-06 00:00:30 +08:00
parent b236498939
commit f28d119560
2 changed files with 12 additions and 13 deletions

View File

@@ -107,7 +107,7 @@ class DataUsagePreferenceController(context: Context, key: String) :
private fun getDataUsageSummary(): String? {
val repository = createNetworkCycleDataRepository() ?: return null
repository.loadFirstCycle()?.takeIf { it.usage > 0 }?.let { usageData ->
repository.loadFirstCycle()?.let { usageData ->
return mContext.getString(
R.string.data_usage_template,
usageData.formatUsage(mContext),

View File

@@ -22,7 +22,6 @@ import android.net.NetworkTemplate
import android.provider.Settings
import android.telephony.SubscriptionManager
import android.util.DataUnit
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.testing.TestLifecycleOwner
import androidx.preference.Preference
import androidx.preference.PreferenceManager
@@ -78,13 +77,11 @@ class DataUsagePreferenceControllerTest {
fun setUp() {
mockSession = ExtendedMockito.mockitoSession()
.initMocks(this)
.mockStatic(SubscriptionManager::class.java)
.spyStatic(DataUsageUtils::class.java)
.spyStatic(DataUsageLib::class.java)
.strictness(Strictness.LENIENT)
.startMocking()
whenever(SubscriptionManager.isValidSubscriptionId(SUB_ID)).thenReturn(true)
ExtendedMockito.doReturn(true).`when` { DataUsageUtils.hasMobileData(context) }
ExtendedMockito.doReturn(networkTemplate).`when` {
DataUsageLib.getMobileTemplate(context, SUB_ID)
@@ -109,9 +106,10 @@ class DataUsagePreferenceControllerTest {
@Test
fun getAvailabilityStatus_invalidSubId_returnUnsearchable() {
controller.init(SubscriptionManager.INVALID_SUBSCRIPTION_ID)
val availabilityStatus =
controller.getAvailabilityStatus(SubscriptionManager.INVALID_SUBSCRIPTION_ID)
assertThat(controller.availabilityStatus).isEqualTo(AVAILABLE_UNSEARCHABLE)
assertThat(availabilityStatus).isEqualTo(AVAILABLE_UNSEARCHABLE)
}
@Test
@@ -120,7 +118,7 @@ class DataUsagePreferenceControllerTest {
repository.stub {
on { loadFirstCycle() } doReturn usageData
}
controller.onViewCreated(TestLifecycleOwner(initialState = Lifecycle.State.STARTED))
controller.onViewCreated(TestLifecycleOwner())
waitUntil { preference.summary != null }
controller.handlePreferenceTreeClick(preference)
@@ -136,21 +134,22 @@ class DataUsagePreferenceControllerTest {
fun updateState_invalidSubId_disabled() = runBlocking {
controller.init(SubscriptionManager.INVALID_SUBSCRIPTION_ID)
controller.onViewCreated(TestLifecycleOwner(initialState = Lifecycle.State.STARTED))
controller.onViewCreated(TestLifecycleOwner())
waitUntil { !preference.isEnabled }
}
@Test
fun updateState_noUsageData_shouldDisablePreference() = runBlocking {
fun updateState_noUsageData_shouldEnablePreference() = runBlocking {
val usageData = NetworkUsageData(START_TIME, END_TIME, 0L)
repository.stub {
on { loadFirstCycle() } doReturn usageData
}
controller.onViewCreated(TestLifecycleOwner(initialState = Lifecycle.State.STARTED))
controller.onViewCreated(TestLifecycleOwner())
waitUntil { !preference.isEnabled }
waitUntil { preference.isEnabled }
waitUntil { preference.summary?.contains("0 B used") == true }
}
@Test
@@ -160,9 +159,9 @@ class DataUsagePreferenceControllerTest {
on { loadFirstCycle() } doReturn usageData
}
controller.onViewCreated(TestLifecycleOwner(initialState = Lifecycle.State.STARTED))
controller.onViewCreated(TestLifecycleOwner())
waitUntil { preference.summary?.contains("1.00 MB") == true }
waitUntil { preference.summary?.contains("1.00 MB used") == true }
}
private companion object {