-
-
Notifications
You must be signed in to change notification settings - Fork 62
ADFA-5535: stop the memory chart's axis printing duplicate and negative labels #1802
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+175
−8
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
app/src/main/java/com/itsaky/androidide/ui/MemoryChartAxis.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /* | ||
| * This file is part of AndroidIDE. | ||
| * | ||
| * AndroidIDE is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * AndroidIDE is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| package com.itsaky.androidide.ui | ||
|
|
||
| import com.github.mikephil.charting.components.AxisBase | ||
| import com.github.mikephil.charting.components.YAxis | ||
| import com.github.mikephil.charting.formatter.IAxisValueFormatter | ||
| import kotlin.math.roundToLong | ||
|
|
||
| /** | ||
| * The memory chart's value axis: whole megabytes, never negative, never repeated (ADFA-5535). | ||
| * | ||
| * Extracted from the chart setup so the labels it produces can be tested. They could not be checked | ||
| * any other way: what MPAndroidChart prints depends on the range it picks for itself, which is only | ||
| * decided during a layout and a draw. | ||
| */ | ||
| object MemoryChartAxis { | ||
| /** | ||
| * Formats whole megabytes. | ||
| * | ||
| * The rounding is why [configure] has to bound the interval. Left to itself, MPAndroidChart | ||
| * picks a label interval from the data, and an all-zero chart -- every editor open, before the | ||
| * first sample lands -- gave it a range of about -1MB to 1MB with six labels. Rounded to whole | ||
| * megabytes those collapse onto three strings, each printed twice: "-1MB, -1MB, 0MB, 0MB, 1MB, | ||
| * 1MB". | ||
| */ | ||
| private val FORMATTER = | ||
| object : IAxisValueFormatter { | ||
| override fun getFormattedValue( | ||
| value: Float, | ||
| axis: AxisBase?, | ||
| ): String = "%dMB".format(value.roundToLong()) | ||
| } | ||
|
|
||
| /** | ||
| * A megabyte, the smallest interval the label text can tell apart. | ||
| * | ||
| * Granularity is a floor on the interval, not the interval itself, so this costs nothing on a | ||
| * chart with real data -- a memory axis spanning hundreds of megabytes was never going to want | ||
| * gridlines a megabyte apart. It only bites in the degenerate case it exists for. | ||
| */ | ||
| private const val MINIMUM_INTERVAL_MEGABYTES = 1f | ||
|
|
||
| fun configure(axis: YAxis) { | ||
| axis.valueFormatter = FORMATTER | ||
| // Memory is never negative, so the axis has no business going below zero -- which is also | ||
| // half of what made the duplicates readable as "-1MB" twice. | ||
| axis.axisMinimum = 0f | ||
| axis.granularity = MINIMUM_INTERVAL_MEGABYTES | ||
| axis.isGranularityEnabled = true | ||
| } | ||
| } |
106 changes: 106 additions & 0 deletions
106
app/src/test/java/com/itsaky/androidide/ui/MemoryChartAxisTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| /* | ||
| * This file is part of AndroidIDE. | ||
| * | ||
| * AndroidIDE is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * AndroidIDE is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| package com.itsaky.androidide.ui | ||
|
|
||
| import android.content.Context | ||
| import android.graphics.Bitmap | ||
| import android.graphics.Canvas | ||
| import android.view.View | ||
| import androidx.test.core.app.ApplicationProvider | ||
| import com.github.mikephil.charting.data.Entry | ||
| import com.github.mikephil.charting.data.LineData | ||
| import com.github.mikephil.charting.data.LineDataSet | ||
| import com.google.common.truth.Truth.assertThat | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.robolectric.RobolectricTestRunner | ||
|
|
||
| /** | ||
| * The labels the memory chart's value axis actually prints (ADFA-5535). | ||
| * | ||
| * Asserted on what MPAndroidChart computes rather than on the formatter alone, because the bug was | ||
| * never in the formatter: it was in the interval the library chose to hand it. That interval is | ||
| * decided during a layout and a draw, so the chart has to be laid out and drawn here. | ||
| */ | ||
| @RunWith(RobolectricTestRunner::class) | ||
| class MemoryChartAxisTest { | ||
| private val context = ApplicationProvider.getApplicationContext<Context>() | ||
|
|
||
| private fun chartWith(megabytes: List<Float>): SafeLineChart { | ||
| val chart = SafeLineChart(context) | ||
| MemoryChartAxis.configure(chart.axisRight) | ||
| chart.axisLeft.isEnabled = false | ||
| chart.data = | ||
| LineData(LineDataSet(megabytes.mapIndexed { i, mb -> Entry(i.toFloat(), mb) }, "IDE")) | ||
| chart.notifyDataSetChanged() | ||
|
|
||
| chart.measure( | ||
| View.MeasureSpec.makeMeasureSpec(WIDTH, View.MeasureSpec.EXACTLY), | ||
| View.MeasureSpec.makeMeasureSpec(HEIGHT, View.MeasureSpec.EXACTLY), | ||
| ) | ||
| chart.layout(0, 0, WIDTH, HEIGHT) | ||
| chart.draw(Canvas(Bitmap.createBitmap(WIDTH, HEIGHT, Bitmap.Config.ARGB_8888))) | ||
| return chart | ||
| } | ||
|
|
||
| private fun labelsOf(chart: SafeLineChart): List<String> { | ||
| val axis = chart.axisRight | ||
| return (0 until axis.mEntryCount).map { i -> axis.getFormattedLabel(i) } | ||
| } | ||
|
|
||
| @Test | ||
| fun `an all-zero chart prints no duplicate labels`() { | ||
| // Every editor open looks like this until the first sample lands, and so does the moment | ||
| // after the sampling rate changes, which clears the buffers. | ||
| val labels = labelsOf(chartWith(List(60) { 0f })) | ||
|
|
||
| assertThat(labels).isNotEmpty() | ||
| assertThat(labels).containsNoDuplicates() | ||
| } | ||
|
|
||
| @Test | ||
| fun `an all-zero chart prints no negative megabytes`() { | ||
| // The reported symptom was "-1MB, -1MB, 0MB, 0MB, 1MB, 1MB". Memory is never negative, so | ||
| // half of that was nonsense before it was even repeated. | ||
| val labels = labelsOf(chartWith(List(60) { 0f })) | ||
|
|
||
| labels.forEach { label -> assertThat(label).doesNotContain("-") } | ||
| } | ||
|
|
||
| @Test | ||
| fun `a chart with real readings still labels distinctly`() { | ||
| // Granularity is a floor on the interval, not the interval, so a chart spanning hundreds of | ||
| // megabytes is unaffected by it. If this ever fails, the floor has started to bite. | ||
| val labels = labelsOf(chartWith(listOf(120f, 480f, 733f, 906f, 512f))) | ||
|
|
||
| assertThat(labels).containsNoDuplicates() | ||
| assertThat(labels.size).isAtLeast(3) | ||
| } | ||
|
|
||
| @Test | ||
| fun `the labels are whole megabytes`() { | ||
| val labels = labelsOf(chartWith(listOf(120f, 480f, 733f))) | ||
|
|
||
| labels.forEach { label -> assertThat(label).matches("\\d+MB") } | ||
| } | ||
|
|
||
| private companion object { | ||
| const val WIDTH = 720 | ||
| const val HEIGHT = 400 | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🤖 get_repo_knowledge executed:
get_repo_knowledge appdevforall/CodeOnTheGo /tmp/coderabbit-repo-knowledge/appdevforall-codeonthego-5165177f/architecture /tmp/coderabbit-repo-knowledge/appdevforall-codeonthego-5165177f/conventions /tmp/coderabbit-repo-knowledge/appdevforall-codeonthego-5165177f/learningsLength of output: 47698
🏁 Script executed:
Repository: appdevforall/CodeOnTheGo
Length of output: 4847
🏁 Script executed:
Repository: appdevforall/CodeOnTheGo
Length of output: 35492
Record font-scale verification for the changed memory chart.
MemoryChartAxis.configure(axisRight)changes the chart's value-axis labels and range. Verify the editor screen at font scales1.0and2.0. Add screenshots or a PR note covering clipping, overflow, inaccessible actions, and overlapping content.🤖 Prompt for AI Agents
Source: Coding guidelines