Add empty check for path in UsageGraph

If path is empty, skip method since there is nothing to draw.

Bug: 132728523
Test: RunSettingsRoboTests
Change-Id: I6fdb7c2f69d3d658c47b58c7304cfeb7d4010a46
This commit is contained in:
Lei Yu
2019-05-15 11:14:14 -07:00
parent 2e26605065
commit 7db71f91bf
2 changed files with 17 additions and 1 deletions

View File

@@ -288,7 +288,11 @@ public class UsageGraph extends View {
canvas.drawPath(mPath, paint);
}
private void drawFilledPath(Canvas canvas, SparseIntArray localPaths, Paint paint) {
@VisibleForTesting
void drawFilledPath(Canvas canvas, SparseIntArray localPaths, Paint paint) {
if (localPaths.size() == 0) {
return;
}
mPath.reset();
float lastStartX = localPaths.keyAt(0);
mPath.moveTo(localPaths.keyAt(0), localPaths.valueAt(0));

View File

@@ -24,6 +24,8 @@ import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.SparseIntArray;
import com.android.settingslib.R;
@@ -177,4 +179,14 @@ public class UsageGraphTest {
assertThat(localPaths.keyAt(5)).isEqualTo(1001);
assertThat(localPaths.valueAt(5)).isEqualTo(-1);
}
@Test
public void drawFilledPath_emptyPath_shouldNotCrash() {
final Canvas canvas = new Canvas();
final SparseIntArray localPaths = new SparseIntArray();
final Paint paint = new Paint();
// Should not crash
mGraph.drawFilledPath(canvas, localPaths, paint);
}
}