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