1/* 2 * Copyright (C) 2018 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 17package com.android.internal.os; 18 19import static android.os.BatteryStats.STATS_SINCE_CHARGED; 20 21import static com.android.internal.os.BatteryStatsImpl.LongSamplingCounter; 22import static com.android.internal.os.BatteryStatsImpl.TimeBase; 23 24import static org.junit.Assert.assertEquals; 25import static org.mockito.Mockito.verify; 26import static org.mockito.Mockito.verifyNoMoreInteractions; 27import static org.mockito.Mockito.verifyZeroInteractions; 28import static org.mockito.Mockito.when; 29 30import android.os.Parcel; 31import android.support.test.filters.SmallTest; 32import android.support.test.runner.AndroidJUnit4; 33 34import org.junit.Before; 35import org.junit.Test; 36import org.junit.runner.RunWith; 37import org.mockito.Mock; 38import org.mockito.Mockito; 39import org.mockito.MockitoAnnotations; 40 41/** 42 * Test class for {@link LongSamplingCounter}. 43 * 44 * To run the tests, use 45 * 46 * bit FrameworksCoreTests:com.android.internal.os.LongSamplingCounterTest 47 */ 48@SmallTest 49@RunWith(AndroidJUnit4.class) 50public class LongSamplingCounterTest { 51 52 private static final long COUNT = 1111; 53 private static final long CURRENT_COUNT = 5555; 54 55 @Mock 56 private TimeBase mTimeBase; 57 private LongSamplingCounter mCounter; 58 59 @Before 60 public void setUp() { 61 MockitoAnnotations.initMocks(this); 62 mCounter = new LongSamplingCounter(mTimeBase); 63 Mockito.reset(mTimeBase); 64 } 65 66 @Test 67 public void testReadWriteParcel() { 68 final Parcel parcel = Parcel.obtain(); 69 updateCounts(COUNT, CURRENT_COUNT); 70 mCounter.writeToParcel(parcel); 71 parcel.setDataPosition(0); 72 73 // Now clear counterArray and verify values are read from parcel correctly. 74 updateCounts(0, 0); 75 76 mCounter = new LongSamplingCounter(mTimeBase, parcel); 77 assertEquals(COUNT, mCounter.mCount); 78 assertEquals(CURRENT_COUNT, mCounter.mCurrentCount); 79 parcel.recycle(); 80 } 81 82 @Test 83 public void testReadWriteSummaryParcel() { 84 final Parcel parcel = Parcel.obtain(); 85 updateCounts(COUNT, CURRENT_COUNT); 86 mCounter.writeSummaryFromParcelLocked(parcel); 87 parcel.setDataPosition(0); 88 89 // Now clear counterArray and verify values are read from parcel correctly. 90 updateCounts(0, 0); 91 92 mCounter.readSummaryFromParcelLocked(parcel); 93 assertEquals(COUNT, mCounter.mCount); 94 parcel.recycle(); 95 } 96 97 @Test 98 public void testOnTimeStarted() { 99 updateCounts(COUNT, CURRENT_COUNT); 100 mCounter.onTimeStarted(0, 0, 0); 101 assertEquals(COUNT, mCounter.mCount); 102 assertEquals(COUNT, mCounter.mUnpluggedCount); 103 } 104 105 @Test 106 public void testOnTimeStopped() { 107 updateCounts(COUNT, CURRENT_COUNT); 108 mCounter.onTimeStopped(0, 0, 0); 109 assertEquals(COUNT, mCounter.mCount); 110 } 111 112 @Test 113 public void testAddCountLocked() { 114 updateCounts(0, 0); 115 assertEquals(0, mCounter.getCountLocked(0)); 116 when(mTimeBase.isRunning()).thenReturn(true); 117 mCounter.addCountLocked(111); 118 assertEquals(111, mCounter.getCountLocked(STATS_SINCE_CHARGED)); 119 assertEquals(111, mCounter.mCurrentCount); 120 mCounter.addCountLocked(222); 121 assertEquals(333, mCounter.getCountLocked(STATS_SINCE_CHARGED)); 122 assertEquals(333, mCounter.mCurrentCount); 123 124 when(mTimeBase.isRunning()).thenReturn(false); 125 mCounter.addCountLocked(456); 126 assertEquals(333, mCounter.getCountLocked(STATS_SINCE_CHARGED)); 127 assertEquals(789, mCounter.mCurrentCount); 128 129 mCounter.addCountLocked(444, true); 130 assertEquals(777, mCounter.getCountLocked(STATS_SINCE_CHARGED)); 131 assertEquals(1233, mCounter.mCurrentCount); 132 mCounter.addCountLocked(567, false); 133 assertEquals(777, mCounter.getCountLocked(STATS_SINCE_CHARGED)); 134 assertEquals(1800, mCounter.mCurrentCount); 135 } 136 137 @Test 138 public void testUpdate() { 139 updateCounts(0, 0); 140 assertEquals(0, mCounter.getCountLocked(0)); 141 when(mTimeBase.isRunning()).thenReturn(true); 142 mCounter.update(111); 143 assertEquals(111, mCounter.getCountLocked(STATS_SINCE_CHARGED)); 144 assertEquals(111, mCounter.mCurrentCount); 145 mCounter.update(333); 146 assertEquals(333, mCounter.getCountLocked(STATS_SINCE_CHARGED)); 147 assertEquals(333, mCounter.mCurrentCount); 148 149 when(mTimeBase.isRunning()).thenReturn(false); 150 mCounter.update(789); 151 assertEquals(333, mCounter.getCountLocked(STATS_SINCE_CHARGED)); 152 assertEquals(789, mCounter.mCurrentCount); 153 mCounter.update(100); 154 assertEquals(333, mCounter.getCountLocked(STATS_SINCE_CHARGED)); 155 assertEquals(100, mCounter.mCurrentCount); 156 157 mCounter.update(544, true); 158 assertEquals(777, mCounter.getCountLocked(STATS_SINCE_CHARGED)); 159 assertEquals(544, mCounter.mCurrentCount); 160 mCounter.update(1544, false); 161 assertEquals(777, mCounter.getCountLocked(STATS_SINCE_CHARGED)); 162 assertEquals(1544, mCounter.mCurrentCount); 163 } 164 165 @Test 166 public void testReset() { 167 updateCounts(COUNT, CURRENT_COUNT); 168 // Test with detachIfReset=false 169 mCounter.reset(false /* detachIfReset */); 170 assertEquals(0, mCounter.mCount); 171 assertEquals(CURRENT_COUNT, mCounter.mCurrentCount); 172 verifyZeroInteractions(mTimeBase); 173 174 updateCounts(COUNT, CURRENT_COUNT); 175 // Test with detachIfReset=true 176 mCounter.reset(true /* detachIfReset */); 177 assertEquals(0, mCounter.mCount); 178 assertEquals(CURRENT_COUNT, mCounter.mCurrentCount); 179 verify(mTimeBase).remove(mCounter); 180 verifyNoMoreInteractions(mTimeBase); 181 } 182 183 @Test 184 public void testDetach() { 185 mCounter.detach(); 186 verify(mTimeBase).remove(mCounter); 187 verifyNoMoreInteractions(mTimeBase); 188 } 189 190 private void updateCounts(long total, long current) { 191 mCounter.mCount = total; 192 mCounter.mCurrentCount = current; 193 } 194} 195