1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.widget; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.Mockito.doReturn; 22 import static org.mockito.Mockito.spy; 23 import static org.mockito.Mockito.when; 24 25 import android.content.Context; 26 import android.content.res.Resources; 27 import android.graphics.Canvas; 28 import android.graphics.Paint; 29 import android.util.SparseIntArray; 30 31 import com.android.settingslib.R; 32 33 import org.junit.Before; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 import org.robolectric.RobolectricTestRunner; 37 import org.robolectric.RuntimeEnvironment; 38 39 @RunWith(RobolectricTestRunner.class) 40 public class UsageGraphTest { 41 42 private UsageGraph mGraph; 43 44 @Before setUp()45 public void setUp() { 46 // Set up a graph view of width 1000, height 200, and corner radius 5. 47 Context context = spy(RuntimeEnvironment.application); 48 Resources resources = spy(context.getResources()); 49 doReturn(resources).when(context).getResources(); 50 doReturn(5).when(resources).getDimensionPixelSize(R.dimen.usage_graph_line_corner_radius); 51 doReturn(1).when(resources).getDimensionPixelSize(R.dimen.usage_graph_line_width); 52 doReturn(1).when(resources).getDimensionPixelSize(R.dimen.usage_graph_dot_size); 53 doReturn(1).when(resources).getDimensionPixelSize(R.dimen.usage_graph_dot_interval); 54 doReturn(1).when(resources).getDimensionPixelSize(R.dimen.usage_graph_divider_size); 55 mGraph = spy(new UsageGraph(context, null)); 56 when(mGraph.getWidth()).thenReturn(1000); 57 when(mGraph.getHeight()).thenReturn(200); 58 59 // Set the conceptual size of the graph to 500ms x 100%. 60 mGraph.setMax(500, 100); 61 } 62 63 @Test calculateLocalPaths_singlePath()64 public void calculateLocalPaths_singlePath() { 65 SparseIntArray paths = new SparseIntArray(); 66 paths.append(0, 100); 67 paths.append(500, 50); 68 paths.append(501, -1); 69 70 SparseIntArray localPaths = new SparseIntArray(); 71 mGraph.calculateLocalPaths(paths, localPaths); 72 73 assertThat(localPaths.size()).isEqualTo(3); 74 assertThat(localPaths.keyAt(0)).isEqualTo(0); 75 assertThat(localPaths.valueAt(0)).isEqualTo(0); 76 assertThat(localPaths.keyAt(1)).isEqualTo(1000); 77 assertThat(localPaths.valueAt(1)).isEqualTo(100); 78 assertThat(localPaths.keyAt(2)).isEqualTo(1001); 79 assertThat(localPaths.valueAt(2)).isEqualTo(-1); 80 } 81 82 @Test calculateLocalPaths_multiplePaths()83 public void calculateLocalPaths_multiplePaths() { 84 SparseIntArray paths = new SparseIntArray(); 85 paths.append(0, 100); 86 paths.append(200, 75); 87 paths.append(201, -1); 88 89 paths.append(300, 50); 90 paths.append(500, 25); 91 paths.append(501, -1); 92 93 SparseIntArray localPaths = new SparseIntArray(); 94 mGraph.calculateLocalPaths(paths, localPaths); 95 96 assertThat(localPaths.size()).isEqualTo(6); 97 98 assertThat(localPaths.keyAt(0)).isEqualTo(0); 99 assertThat(localPaths.valueAt(0)).isEqualTo(0); 100 assertThat(localPaths.keyAt(1)).isEqualTo(400); 101 assertThat(localPaths.valueAt(1)).isEqualTo(50); 102 assertThat(localPaths.keyAt(2)).isEqualTo(401); 103 assertThat(localPaths.valueAt(2)).isEqualTo(-1); 104 105 assertThat(localPaths.keyAt(3)).isEqualTo(600); 106 assertThat(localPaths.valueAt(3)).isEqualTo(100); 107 assertThat(localPaths.keyAt(4)).isEqualTo(1000); 108 assertThat(localPaths.valueAt(4)).isEqualTo(150); 109 assertThat(localPaths.keyAt(5)).isEqualTo(1001); 110 assertThat(localPaths.valueAt(5)).isEqualTo(-1); 111 } 112 113 @Test calculateLocalPaths_similarPointMiddle()114 public void calculateLocalPaths_similarPointMiddle() { 115 SparseIntArray paths = new SparseIntArray(); 116 paths.append(0, 100); 117 paths.append(1, 99); // This point should be omitted. 118 paths.append(500, 50); 119 paths.append(501, -1); 120 121 SparseIntArray localPaths = new SparseIntArray(); 122 mGraph.calculateLocalPaths(paths, localPaths); 123 124 assertThat(localPaths.size()).isEqualTo(3); 125 assertThat(localPaths.keyAt(0)).isEqualTo(0); 126 assertThat(localPaths.valueAt(0)).isEqualTo(0); 127 assertThat(localPaths.keyAt(1)).isEqualTo(1000); 128 assertThat(localPaths.valueAt(1)).isEqualTo(100); 129 assertThat(localPaths.keyAt(2)).isEqualTo(1001); 130 assertThat(localPaths.valueAt(2)).isEqualTo(-1); 131 } 132 133 @Test calculateLocalPaths_similarPointEnd()134 public void calculateLocalPaths_similarPointEnd() { 135 SparseIntArray paths = new SparseIntArray(); 136 paths.append(0, 100); 137 paths.append(499, 51); 138 paths.append(500, 50); // This point should be kept: it's the last one. 139 paths.append(501, -1); 140 141 SparseIntArray localPaths = new SparseIntArray(); 142 mGraph.calculateLocalPaths(paths, localPaths); 143 144 assertThat(localPaths.size()).isEqualTo(4); 145 assertThat(localPaths.keyAt(0)).isEqualTo(0); 146 assertThat(localPaths.valueAt(0)).isEqualTo(0); 147 assertThat(localPaths.keyAt(1)).isEqualTo(998); 148 assertThat(localPaths.valueAt(1)).isEqualTo(98); 149 assertThat(localPaths.keyAt(2)).isEqualTo(1000); 150 assertThat(localPaths.valueAt(2)).isEqualTo(100); 151 assertThat(localPaths.keyAt(3)).isEqualTo(1001); 152 assertThat(localPaths.valueAt(3)).isEqualTo(-1); 153 } 154 155 @Test calculateLocalPaths_unavailableData_shouldInsertFlatPoint()156 public void calculateLocalPaths_unavailableData_shouldInsertFlatPoint() { 157 SparseIntArray paths = new SparseIntArray(); 158 paths.append(0, 0); 159 paths.append(199, -1); 160 paths.append(200, 25); 161 paths.append(300, 50); 162 paths.append(500, 75); 163 paths.append(501, -1); 164 165 SparseIntArray localPaths = new SparseIntArray(); 166 mGraph.calculateLocalPaths(paths, localPaths); 167 168 assertThat(localPaths.size()).isEqualTo(6); 169 assertThat(localPaths.keyAt(0)).isEqualTo(0); 170 assertThat(localPaths.valueAt(0)).isEqualTo(200); 171 assertThat(localPaths.keyAt(1)).isEqualTo(399); 172 assertThat(localPaths.valueAt(1)).isEqualTo(200); 173 assertThat(localPaths.keyAt(2)).isEqualTo(400); 174 assertThat(localPaths.valueAt(2)).isEqualTo(150); 175 assertThat(localPaths.keyAt(3)).isEqualTo(600); 176 assertThat(localPaths.valueAt(3)).isEqualTo(100); 177 assertThat(localPaths.keyAt(4)).isEqualTo(1000); 178 assertThat(localPaths.valueAt(4)).isEqualTo(50); 179 assertThat(localPaths.keyAt(5)).isEqualTo(1001); 180 assertThat(localPaths.valueAt(5)).isEqualTo(-1); 181 } 182 183 @Test drawFilledPath_emptyPath_shouldNotCrash()184 public void drawFilledPath_emptyPath_shouldNotCrash() { 185 final Canvas canvas = new Canvas(); 186 final SparseIntArray localPaths = new SparseIntArray(); 187 final Paint paint = new Paint(); 188 189 // Should not crash 190 mGraph.drawFilledPath(canvas, localPaths, paint); 191 } 192 } 193