Skip to content

Commit 0e6eed6

Browse files
authored
Merge pull request #17302 from wordpress-mobile/fix/rounding-issues-on-stats
Fix rounding issues on stats
2 parents d469524 + 19bd270 commit 0e6eed6

6 files changed

Lines changed: 50 additions & 20 deletions

File tree

WordPress/src/main/java/org/wordpress/android/ui/stats/refresh/lists/detail/PostDayViewsMapper.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import org.wordpress.android.ui.stats.refresh.utils.StatsDateFormatter
1313
import org.wordpress.android.ui.stats.refresh.utils.StatsUtils
1414
import org.wordpress.android.util.text.PercentFormatter
1515
import org.wordpress.android.viewmodel.ResourceProvider
16+
import java.math.RoundingMode.HALF_UP
1617
import javax.inject.Inject
1718

1819
class PostDayViewsMapper
@@ -67,7 +68,7 @@ class PostDayViewsMapper
6768
0 -> ""
6869
else -> {
6970
val percentageValue = difference.toFloat() / previousValue
70-
percentFormatter.format(percentageValue)
71+
percentFormatter.format(value = percentageValue, rounding = HALF_UP)
7172
}
7273
}
7374
val formattedDifference = mapIntToString(difference, isFormattedNumber)

WordPress/src/main/java/org/wordpress/android/ui/stats/refresh/lists/sections/insights/usecases/MostPopularInsightsUseCase.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import org.wordpress.android.ui.stats.refresh.utils.ItemPopupMenuHandler
2424
import org.wordpress.android.ui.stats.refresh.utils.StatsSiteProvider
2525
import org.wordpress.android.util.text.PercentFormatter
2626
import org.wordpress.android.viewmodel.ResourceProvider
27+
import java.math.RoundingMode
2728
import javax.inject.Inject
2829
import javax.inject.Named
2930
import kotlin.math.roundToInt
@@ -75,11 +76,17 @@ class MostPopularInsightsUseCase
7576
} else {
7677
val highestDayPercent = resourceProvider.getString(
7778
R.string.stats_most_popular_percent_views,
78-
percentFormatter.format(domainModel.highestDayPercent.roundToInt())
79+
percentFormatter.format(
80+
value = domainModel.highestDayPercent.roundToInt(),
81+
rounding = RoundingMode.HALF_UP
82+
)
7983
)
8084
val highestHourPercent = resourceProvider.getString(
8185
R.string.stats_most_popular_percent_views,
82-
percentFormatter.format(domainModel.highestHourPercent.roundToInt())
86+
percentFormatter.format(
87+
value = domainModel.highestHourPercent.roundToInt(),
88+
rounding = RoundingMode.HALF_UP
89+
)
8390
)
8491
items.add(
8592
QuickScanItem(

WordPress/src/main/java/org/wordpress/android/ui/stats/refresh/utils/StatsUtils.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.LineC
77
import org.wordpress.android.util.LocaleManagerWrapper
88
import org.wordpress.android.util.text.PercentFormatter
99
import org.wordpress.android.viewmodel.ResourceProvider
10+
import java.math.RoundingMode.HALF_UP
1011
import java.text.DecimalFormat
1112
import java.util.TreeMap
1213
import javax.inject.Inject
@@ -197,7 +198,7 @@ class StatsUtils @Inject constructor(
197198
0L -> ""
198199
else -> {
199200
val percentageValue = difference.toFloat() / previousValue
200-
percentFormatter.format(percentageValue)
201+
percentFormatter.format(value = percentageValue, rounding = HALF_UP)
201202
}
202203
}
203204
val formattedDifference = mapLongToString(difference, isFormattedNumber)

WordPress/src/test/java/org/wordpress/android/ui/stats/refresh/lists/detail/PostDayViewsMapperTest.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import org.wordpress.android.ui.stats.refresh.utils.StatsDateFormatter
1717
import org.wordpress.android.ui.stats.refresh.utils.StatsUtils
1818
import org.wordpress.android.util.text.PercentFormatter
1919
import org.wordpress.android.viewmodel.ResourceProvider
20+
import java.math.RoundingMode
21+
import java.math.RoundingMode.HALF_UP
2022

2123
class PostDayViewsMapperTest : BaseUnitTest() {
2224
@Mock lateinit var statsDateFormatter: StatsDateFormatter
@@ -57,7 +59,7 @@ class PostDayViewsMapperTest : BaseUnitTest() {
5759

5860
@Test
5961
fun `builds title with positive difference`() {
60-
whenever(percentFormatter.format(3.0F)).thenReturn("300")
62+
whenever(percentFormatter.format(value = 3.0F, rounding = RoundingMode.HALF_UP)).thenReturn("300")
6163
val previousCount = 5
6264
val previousItem = selectedItem.copy(count = previousCount)
6365
val positiveLabel = "+15 (300%)"
@@ -92,7 +94,7 @@ class PostDayViewsMapperTest : BaseUnitTest() {
9294

9395
@Test
9496
fun `builds title with negative difference`() {
95-
whenever(percentFormatter.format(-0.33333334F)).thenReturn("-33")
97+
whenever(percentFormatter.format(value = -0.33333334F, rounding = HALF_UP)).thenReturn("-33")
9698
val previousCount = 30
9799
val previousItem = selectedItem.copy(count = previousCount)
98100
val negativeLabel = "-10 (-33%)"
@@ -109,7 +111,7 @@ class PostDayViewsMapperTest : BaseUnitTest() {
109111

110112
@Test
111113
fun `builds title with max negative difference`() {
112-
whenever(percentFormatter.format(-1F)).thenReturn("-100")
114+
whenever(percentFormatter.format(value = -1F, rounding = HALF_UP)).thenReturn("-100")
113115
val newCount = 0
114116
val newItem = selectedItem.copy(count = newCount)
115117
val negativeLabel = "-20 (-100%)"
@@ -150,7 +152,7 @@ class PostDayViewsMapperTest : BaseUnitTest() {
150152

151153
@Test
152154
fun `builds title with negative difference for the last item`() {
153-
whenever(percentFormatter.format(-0.33333334F)).thenReturn("-33")
155+
whenever(percentFormatter.format(value = -0.33333334F, rounding = HALF_UP)).thenReturn("-33")
154156
val previousCount = 30
155157
val previousItem = selectedItem.copy(count = previousCount)
156158
val negativeLabel = "-10 (-33%)"
@@ -167,12 +169,12 @@ class PostDayViewsMapperTest : BaseUnitTest() {
167169

168170
@Test
169171
fun `should call PercentFormatter when builds title`() {
170-
whenever(percentFormatter.format(3.0F)).thenReturn("3%")
172+
whenever(percentFormatter.format(value = 3.0F, rounding = HALF_UP)).thenReturn("3%")
171173
val previousCount = 5
172174
val previousItem = selectedItem.copy(count = previousCount)
173175
mapper.buildTitle(selectedItem, previousItem, false)
174176

175177
// buildChange is called twice: for change and unformattedChange
176-
verify(percentFormatter, times(2)).format(3.0F)
178+
verify(percentFormatter, times(2)).format(value = 3.0F, rounding = HALF_UP)
177179
}
178180
}

WordPress/src/test/java/org/wordpress/android/ui/stats/refresh/lists/sections/insights/usecases/MostPopularInsightsUseCaseTest.kt

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ import org.wordpress.android.ui.stats.refresh.utils.ItemPopupMenuHandler
3232
import org.wordpress.android.ui.stats.refresh.utils.StatsSiteProvider
3333
import org.wordpress.android.util.text.PercentFormatter
3434
import org.wordpress.android.viewmodel.ResourceProvider
35+
import java.math.RoundingMode
36+
import java.math.RoundingMode.HALF_UP
3537
import kotlin.math.roundToInt
3638

3739
class MostPopularInsightsUseCaseTest : BaseUnitTest() {
@@ -66,8 +68,18 @@ class MostPopularInsightsUseCaseTest : BaseUnitTest() {
6668
actionCardHandler,
6769
percentFormatter
6870
)
69-
whenever(percentFormatter.format(highestDayPercent.roundToInt())).thenReturn("10%")
70-
whenever(percentFormatter.format(highestHourPercent.roundToInt())).thenReturn("20%")
71+
whenever(
72+
percentFormatter.format(
73+
value = highestDayPercent.roundToInt(),
74+
rounding = RoundingMode.HALF_UP
75+
)
76+
).thenReturn("10%")
77+
whenever(
78+
percentFormatter.format(
79+
value = highestHourPercent.roundToInt(),
80+
rounding = RoundingMode.HALF_UP
81+
)
82+
).thenReturn("20%")
7183
whenever(statsSiteProvider.siteModel).thenReturn(site)
7284
whenever(dateUtils.getWeekDay(day)).thenReturn(dayString)
7385

@@ -127,8 +139,14 @@ class MostPopularInsightsUseCaseTest : BaseUnitTest() {
127139
@Test
128140
fun `when buildUiModel is called, should call PercentFormatter`() = test {
129141
useCase.buildUiModel(InsightsMostPopularModel(0, day, hour, highestDayPercent, highestHourPercent))
130-
verify(percentFormatter).format(highestDayPercent.roundToInt())
131-
verify(percentFormatter).format(highestHourPercent.roundToInt())
142+
verify(percentFormatter).format(
143+
value = highestDayPercent.roundToInt(),
144+
rounding = HALF_UP
145+
)
146+
verify(percentFormatter).format(
147+
value = highestHourPercent.roundToInt(),
148+
rounding = HALF_UP
149+
)
132150
}
133151

134152
private fun assertTitle(item: BlockListItem) {

WordPress/src/test/java/org/wordpress/android/ui/stats/refresh/utils/StatsUtilsTest.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import org.wordpress.android.R.string
1515
import org.wordpress.android.util.LocaleManagerWrapper
1616
import org.wordpress.android.util.text.PercentFormatter
1717
import org.wordpress.android.viewmodel.ResourceProvider
18+
import java.math.RoundingMode.HALF_UP
1819
import java.util.Locale
1920

2021
@RunWith(MockitoJUnitRunner::class)
@@ -258,7 +259,7 @@ class StatsUtilsTest {
258259

259260
@Test
260261
fun `build change with positive difference`() {
261-
whenever(percentFormatter.format(3.0F)).thenReturn("300")
262+
whenever(percentFormatter.format(value = 3.0F, rounding = HALF_UP)).thenReturn("300")
262263
val previousValue = 5L
263264
val value = 20L
264265
val positive = true
@@ -273,7 +274,7 @@ class StatsUtilsTest {
273274

274275
@Test
275276
fun `build change with infinite positive difference`() {
276-
whenever(percentFormatter.format(3.0F)).thenReturn("")
277+
whenever(percentFormatter.format(value = 3.0F, rounding = HALF_UP)).thenReturn("")
277278
val previousValue = 0L
278279
val value = 20L
279280
val positive = true
@@ -288,7 +289,7 @@ class StatsUtilsTest {
288289

289290
@Test
290291
fun `build change with negative difference`() {
291-
whenever(percentFormatter.format(-0.33333334F)).thenReturn("-33")
292+
whenever(percentFormatter.format(value = -0.33333334F, rounding = HALF_UP)).thenReturn("-33")
292293
val previousValue = 30L
293294
val value = 20L
294295
val positive = false
@@ -304,7 +305,7 @@ class StatsUtilsTest {
304305
@Test
305306
fun `build change with max negative difference`() {
306307
val previousValue = 20L
307-
whenever(percentFormatter.format(-1F)).thenReturn("-100")
308+
whenever(percentFormatter.format(value = -1F, rounding = HALF_UP)).thenReturn("-100")
308309
val value = 0L
309310
val positive = false
310311
val expectedChange = "-20 (-100%)"
@@ -332,8 +333,8 @@ class StatsUtilsTest {
332333

333334
@Test
334335
fun `when buildChange, should call PercentFormatter`() {
335-
whenever(percentFormatter.format(3.0F)).thenReturn("3%")
336+
whenever(percentFormatter.format(value = 3.0F, rounding = HALF_UP)).thenReturn("3%")
336337
statsUtils.buildChange(5L, 20L, true, isFormattedNumber = true)
337-
verify(percentFormatter).format(3.0F)
338+
verify(percentFormatter).format(value = 3.0F, rounding = HALF_UP)
338339
}
339340
}

0 commit comments

Comments
 (0)