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.internal.os;
18 
19 import static android.os.BatteryStats.STATS_SINCE_CHARGED;
20 
21 import static com.android.internal.os.BatteryStatsImpl.LongSamplingCounterArray;
22 import static com.android.internal.os.BatteryStatsImpl.TimeBase;
23 
24 import static org.junit.Assert.assertArrayEquals;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.verifyNoMoreInteractions;
27 import static org.mockito.Mockito.verifyZeroInteractions;
28 import static org.mockito.Mockito.when;
29 
30 import android.os.Parcel;
31 
32 import androidx.test.filters.SmallTest;
33 import androidx.test.runner.AndroidJUnit4;
34 
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.Mock;
39 import org.mockito.Mockito;
40 import org.mockito.MockitoAnnotations;
41 
42 /**
43  * Test class for {@link BatteryStatsImpl.LongSamplingCounterArray}.
44  *
45  * To run the tests, use
46  *
47  * runtest -c com.android.internal.os.LongSamplingCounterArrayTest frameworks-core
48  *
49  * or the following steps:
50  *
51  * Build: m FrameworksCoreTests
52  * Install: adb install -r \
53  *     ${ANDROID_PRODUCT_OUT}/data/app/FrameworksCoreTests/FrameworksCoreTests.apk
54  * Run: adb shell am instrument -e class com.android.internal.os.LongSamplingCounterArrayTest -w \
55  *     com.android.frameworks.coretests/androidx.test.runner.AndroidJUnitRunner
56  *
57  * or just do
58  * atest frameworks/base/core/tests/coretests/src/com/android/internal/os/LongSamplingCounterArrayTest.java
59  */
60 @SmallTest
61 @RunWith(AndroidJUnit4.class)
62 public class LongSamplingCounterArrayTest {
63 
64     private static final long[] COUNTS = {1111, 2222, 3333, 4444};
65     private static final long[] ZEROES = {0, 0, 0, 0};
66 
67     @Mock private TimeBase mTimeBase;
68     private LongSamplingCounterArray mCounterArray;
69 
70     @Before
setUp()71     public void setUp() {
72         MockitoAnnotations.initMocks(this);
73         mCounterArray = new LongSamplingCounterArray(mTimeBase);
74         Mockito.reset(mTimeBase);
75     }
76 
77     @Test
testReadWriteParcel()78     public void testReadWriteParcel() {
79         final Parcel parcel = Parcel.obtain();
80         updateCounts(COUNTS);
81         LongSamplingCounterArray.writeToParcel(parcel, mCounterArray);
82         parcel.setDataPosition(0);
83 
84         // Now clear counterArray and verify values are read from parcel correctly.
85         updateCounts(null);
86         mCounterArray = LongSamplingCounterArray.readFromParcel(parcel, mTimeBase);
87         assertArrayEquals(COUNTS, mCounterArray.mCounts);
88         parcel.recycle();
89     }
90 
91     @Test
testReadWriteSummaryParcel()92     public void testReadWriteSummaryParcel() {
93         final Parcel parcel = Parcel.obtain();
94         updateCounts(COUNTS);
95         LongSamplingCounterArray.writeSummaryToParcelLocked(parcel, mCounterArray);
96         parcel.setDataPosition(0);
97 
98         // Now clear counterArray and verify values are read from parcel correctly.
99         updateCounts(null);
100         mCounterArray = LongSamplingCounterArray.readSummaryFromParcelLocked(parcel, mTimeBase);
101         assertArrayEquals(COUNTS, mCounterArray.mCounts);
102         parcel.recycle();
103     }
104 
105     @Test
testOnTimeStarted()106     public void testOnTimeStarted() {
107         updateCounts(COUNTS);
108         mCounterArray.onTimeStarted(0, 0, 0);
109         assertArrayEquals(COUNTS, mCounterArray.mCounts);
110     }
111 
112     @Test
testOnTimeStopped()113     public void testOnTimeStopped() {
114         updateCounts(COUNTS);
115         mCounterArray.onTimeStopped(0, 0, 0);
116         assertArrayEquals(COUNTS, mCounterArray.mCounts);
117     }
118 
119     @Test
testGetCountsLocked()120     public void testGetCountsLocked() {
121         updateCounts(COUNTS);
122 
123         when(mTimeBase.isRunning()).thenReturn(false);
124         assertArrayEquals(COUNTS, mCounterArray.getCountsLocked(STATS_SINCE_CHARGED));
125 
126         when(mTimeBase.isRunning()).thenReturn(true);
127         assertArrayEquals(COUNTS, mCounterArray.getCountsLocked(STATS_SINCE_CHARGED));
128     }
129 
subtract(long[] val, long[] toSubtract)130     private long[] subtract(long[] val, long[] toSubtract) {
131         final long[] result = val.clone();
132         if (toSubtract != null) {
133             for (int i = val.length - 1; i >= 0; --i) {
134                 result[i] -= toSubtract[i];
135             }
136         }
137         return result;
138     }
139 
140     @Test
testAddCountLocked()141     public void testAddCountLocked() {
142         updateCounts(null);
143         final long[] deltas = {123, 234, 345, 456};
144         when(mTimeBase.isRunning()).thenReturn(true);
145         mCounterArray.addCountLocked(deltas);
146         assertArrayEquals(deltas, mCounterArray.mCounts);
147 
148         updateCounts(null);
149         mCounterArray.addCountLocked(deltas, false);
150         assertArrayEquals(null, mCounterArray.mCounts);
151         mCounterArray.addCountLocked(deltas, true);
152         assertArrayEquals(deltas, mCounterArray.mCounts);
153 
154         updateCounts(COUNTS);
155         final long[] newCounts = new long[deltas.length];
156         for (int i = 0; i < deltas.length; ++i) {
157             newCounts[i] = COUNTS[i] + deltas[i];
158         }
159         mCounterArray.addCountLocked(deltas);
160         assertArrayEquals(newCounts, mCounterArray.mCounts);
161 
162         updateCounts(COUNTS);
163         mCounterArray.addCountLocked(deltas, false);
164         assertArrayEquals(COUNTS, mCounterArray.mCounts);
165         mCounterArray.addCountLocked(deltas, true);
166         assertArrayEquals(newCounts, mCounterArray.mCounts);
167     }
168 
169     @Test
testReset()170     public void testReset() {
171         updateCounts(COUNTS);
172         // Test with detachIfReset=false
173         mCounterArray.reset(false /* detachIfReset */);
174         assertArrayEquals(ZEROES, mCounterArray.mCounts);
175         verifyZeroInteractions(mTimeBase);
176 
177         updateCounts(COUNTS);
178         // Test with detachIfReset=true
179         mCounterArray.reset(true /* detachIfReset */);
180         assertArrayEquals(ZEROES, mCounterArray.mCounts);
181         verify(mTimeBase).remove(mCounterArray);
182         verifyNoMoreInteractions(mTimeBase);
183     }
184 
185     @Test
testDetach()186     public void testDetach() {
187         mCounterArray.detach();
188         verify(mTimeBase).remove(mCounterArray);
189         verifyNoMoreInteractions(mTimeBase);
190     }
191 
updateCounts(long[] counts)192     private void updateCounts(long[] counts) {
193         mCounterArray.mCounts = counts == null ? null : counts.clone();
194     }
195 }
196