1 /*
2  * Copyright (C) 2016 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 android.os.health.cts;
18 
19 import android.os.Parcel;
20 import android.os.health.HealthKeys;
21 import android.os.health.HealthStats;
22 import android.os.health.HealthStatsParceler;
23 import android.os.health.HealthStatsWriter;
24 import android.os.health.TimerStat;
25 import android.test.suitebuilder.annotation.SmallTest;
26 
27 import junit.framework.Assert;
28 import junit.framework.TestCase;
29 
30 import java.util.Map;
31 
32 /**
33  * Provides test cases for android.os.health.HealthStats and HealthStatsWriter.
34  */
35 public class HealthStatsTest extends TestCase {
36     @HealthKeys.Constant(type=HealthKeys.TYPE_TIMER)
37     public static final int TIMER_0 = 10;
38 
39     @HealthKeys.Constant(type=HealthKeys.TYPE_TIMER)
40     public static final int TIMER_1 = 11;
41 
42     @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
43     public static final int MEASUREMENT_0 = 20;
44 
45     @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
46     public static final int MEASUREMENT_1 = 21;
47 
48     @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
49     public static final int MEASUREMENT_2 = 21;
50 
51     @HealthKeys.Constant(type=HealthKeys.TYPE_STATS)
52     public static final int STATS_0 = 30;
53 
54     @HealthKeys.Constant(type=HealthKeys.TYPE_TIMERS)
55     public static final int TIMERS_0 = 30;
56 
57     @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENTS)
58     public static final int MEASUREMENTS_0 = 40;
59 
60     @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENTS)
61     public static final int MEASUREMENTS_1 = 41;
62 
63 
64     public static final HealthKeys.Constants CONSTANTS
65             = new HealthKeys.Constants(HealthStatsTest.class);
66 
67     /**
68      * Check all the fields.
69      */
checkHealthStats(HealthStats readback)70     private void checkHealthStats(HealthStats readback) {
71         // Header fields
72         Assert.assertEquals("HealthStatsTest", readback.getDataType());
73 
74 
75         // TimerStat fields
76         Assert.assertEquals(2, readback.getTimerKeyCount());
77 
78         Assert.assertEquals(TIMER_0, readback.getTimerKeyAt(0));
79         Assert.assertTrue(readback.hasTimer(TIMER_0));
80         Assert.assertEquals(1, readback.getTimerCount(TIMER_0));
81         Assert.assertEquals(100, readback.getTimerTime(TIMER_0));
82 
83         Assert.assertEquals(TIMER_1, readback.getTimerKeyAt(1));
84         Assert.assertTrue(readback.hasTimer(TIMER_1));
85         Assert.assertEquals(Integer.MAX_VALUE, readback.getTimerCount(TIMER_1));
86         Assert.assertEquals(Long.MAX_VALUE, readback.getTimerTime(TIMER_1));
87 
88         Assert.assertEquals(Integer.MAX_VALUE, readback.getTimer(TIMER_1).getCount());
89         Assert.assertEquals(Long.MAX_VALUE, readback.getTimer(TIMER_1).getTime());
90 
91 
92         // Measurement fields
93         Assert.assertEquals(2, readback.getMeasurementKeyCount());
94 
95         Assert.assertEquals(MEASUREMENT_0, readback.getMeasurementKeyAt(0));
96         Assert.assertTrue(readback.hasMeasurement(MEASUREMENT_0));
97         Assert.assertEquals(300, readback.getMeasurement(MEASUREMENT_0));
98 
99         Assert.assertEquals(MEASUREMENT_1, readback.getMeasurementKeyAt(1));
100         Assert.assertTrue(readback.hasMeasurement(MEASUREMENT_1));
101         Assert.assertEquals(Long.MAX_VALUE, readback.getMeasurement(MEASUREMENT_1));
102 
103 
104         // Stats fields
105         Assert.assertEquals(1, readback.getStatsKeyCount());
106 
107         Assert.assertEquals(STATS_0, readback.getStatsKeyAt(0));
108         Assert.assertTrue(readback.hasStats(STATS_0));
109 
110         final Map<String,HealthStats> stats = readback.getStats(STATS_0);
111         Assert.assertEquals(1, stats.size());
112         final HealthStats child = stats.get("a");
113         Assert.assertEquals(0, child.getTimerKeyCount());
114         Assert.assertEquals(1, child.getMeasurementKeyCount());
115         Assert.assertEquals(Long.MIN_VALUE, child.getMeasurement(MEASUREMENT_2));
116         Assert.assertEquals(0, child.getStatsKeyCount());
117         Assert.assertEquals(0, child.getTimersKeyCount());
118 
119 
120         // Timers fields
121         Assert.assertEquals(1, readback.getTimersKeyCount());
122 
123         Assert.assertEquals(TIMERS_0, readback.getTimersKeyAt(0));
124         Assert.assertTrue(readback.hasTimers(TIMERS_0));
125         final Map<String,TimerStat> timers = readback.getTimers(TIMERS_0);
126         Assert.assertEquals(1, timers.size());
127         Assert.assertEquals(200, timers.get("b").getCount());
128         Assert.assertEquals(400, timers.get("b").getTime());
129 
130 
131         // Measurements fields
132         Map<String,Long> measurements;
133         Assert.assertEquals(2, readback.getMeasurementsKeyCount());
134 
135         Assert.assertEquals(MEASUREMENTS_0, readback.getMeasurementsKeyAt(0));
136         Assert.assertTrue(readback.hasMeasurements(MEASUREMENTS_0));
137         measurements = readback.getMeasurements(MEASUREMENTS_0);
138         Assert.assertEquals(1, measurements.size());
139         Assert.assertEquals(800L, measurements.get("Z").longValue());
140 
141         Assert.assertEquals(MEASUREMENTS_1, readback.getMeasurementsKeyAt(1));
142         measurements = readback.getMeasurements(MEASUREMENTS_1);
143         Assert.assertTrue(readback.hasMeasurements(MEASUREMENTS_1));
144         Assert.assertEquals(2, measurements.size());
145         Assert.assertEquals(900L, measurements.get("Y").longValue());
146         Assert.assertEquals(901L, measurements.get("X").longValue());
147     }
148 
149     /**
150      * Tests parceling empty HealthStats.
151      */
152     @SmallTest
testParcelEmpty()153     public void testParcelEmpty() throws Exception {
154         final HealthStatsWriter writer = new HealthStatsWriter(CONSTANTS);
155 
156         Parcel parcel = Parcel.obtain();
157         writer.flattenToParcel(parcel);
158 
159         parcel.setDataPosition(0);
160 
161         HealthStats readback = new HealthStats(parcel);
162 
163         // Check that there is no more data in the parcel
164         Assert.assertEquals(0, parcel.dataAvail());
165         parcel.recycle();
166 
167         Assert.assertEquals(0, readback.getTimerKeyCount());
168         Assert.assertEquals(0, readback.getMeasurementKeyCount());
169         Assert.assertEquals(0, readback.getStatsKeyCount());
170         Assert.assertEquals(0, readback.getTimersKeyCount());
171         Assert.assertEquals(0, readback.getMeasurementsKeyCount());
172     }
173 
174 
175     /**
176      * Tests parceling HealthStats.
177      */
178     @SmallTest
testParcelling()179     public void testParcelling() throws Exception {
180         final HealthStatsWriter writer = new HealthStatsWriter(CONSTANTS);
181 
182         // TimerStat
183         writer.addTimer(TIMER_0, 1, 100);
184         writer.addTimer(TIMER_1, Integer.MAX_VALUE, Long.MAX_VALUE);
185 
186         // Measurement
187         writer.addMeasurement(MEASUREMENT_0, 300);
188         writer.addMeasurement(MEASUREMENT_1, Long.MAX_VALUE);
189 
190         // Stats
191         HealthStatsWriter writer2 = new HealthStatsWriter(CONSTANTS);
192         writer2.addMeasurement(MEASUREMENT_2, Long.MIN_VALUE);
193         writer.addStats(STATS_0, "a", writer2);
194 
195         // Timers
196         writer.addTimers(TIMERS_0, "b", new TimerStat(200, 400));
197 
198         // Measurements
199         writer.addMeasurements(MEASUREMENTS_0, "Z", 800);
200         writer.addMeasurements(MEASUREMENTS_1, "Y", 900);
201         writer.addMeasurements(MEASUREMENTS_1, "X", 901);
202 
203 
204         // Parcel and unparcel it via HealthStatsWriter.writeToParcel.
205         Parcel parcel = Parcel.obtain();
206         writer.flattenToParcel(parcel);
207         parcel.setDataPosition(0);
208         HealthStats readback = new HealthStats(parcel);
209 
210         // Check that there is no more data in the parcel
211         Assert.assertEquals(0, parcel.dataAvail());
212         parcel.recycle();
213 
214         checkHealthStats(readback);
215     }
216 
217     /**
218      * Tests the HealthStatsParceler.
219      */
220     @SmallTest
testParceler()221     public void testParceler() throws Exception {
222         final HealthStatsWriter writer = new HealthStatsWriter(CONSTANTS);
223         writer.addMeasurement(MEASUREMENT_0, 300);
224 
225         final HealthStatsParceler parceler = new HealthStatsParceler(writer);
226 
227         final Parcel parcel = Parcel.obtain();
228         parceler.writeToParcel(parcel, 0);
229         parcel.setDataPosition(0);
230 
231         final HealthStatsParceler parceler2 = new HealthStatsParceler(parcel);
232 
233         // Check that there is no more data in the parcel
234         Assert.assertEquals(0, parcel.dataAvail());
235         parcel.recycle();
236 
237         final HealthStats readback = parceler2.getHealthStats();
238 
239         Assert.assertEquals(300, readback.getMeasurement(MEASUREMENT_0));
240 
241         // Should fail
242         try {
243             final Parcel parcel2 = Parcel.obtain();
244             parceler2.writeToParcel(parcel2, 0);
245             parcel2.recycle();
246             throw new Exception("Expected IndexOutOfBoundsException");
247         } catch (RuntimeException ex) {
248             // expected
249         }
250     }
251 }
252 
253