blob: ac25e2734ac9bf64de3d83b10df7d8731cf16a49 [file] [log] [blame]
Muhammad Qureshi557859e2019-11-14 17:41:02 -08001/*
2 * Copyright (C) 2019 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 android.util;
18
19import static com.google.common.truth.Truth.assertThat;
20import static com.google.common.truth.Truth.assertWithMessage;
21
22import static java.nio.charset.StandardCharsets.UTF_8;
23
24import android.os.SystemClock;
25
26import androidx.test.filters.SmallTest;
27import androidx.test.runner.AndroidJUnit4;
28
29import com.google.common.collect.Range;
30
31import org.junit.Test;
32import org.junit.runner.RunWith;
33
34import java.nio.ByteBuffer;
35import java.nio.ByteOrder;
36
37/**
38 * Internal tests for {@link StatsEvent}.
39 */
40@SmallTest
41@RunWith(AndroidJUnit4.class)
42public class StatsEventTest {
43
44 @Test
45 public void testNoFields() {
46 final long minTimestamp = SystemClock.elapsedRealtimeNanos();
Muhammad Qureshi9e829802019-12-04 15:38:20 -080047 final StatsEvent statsEvent = StatsEvent.newBuilder().usePooledBuffer().build();
Muhammad Qureshi557859e2019-11-14 17:41:02 -080048 final long maxTimestamp = SystemClock.elapsedRealtimeNanos();
49
50 final int expectedAtomId = 0;
51 assertThat(statsEvent.getAtomId()).isEqualTo(expectedAtomId);
52
53 final ByteBuffer buffer =
54 ByteBuffer.wrap(statsEvent.getBytes()).order(ByteOrder.LITTLE_ENDIAN);
55
Muhammad Qureshid2650882019-12-05 18:07:12 -080056 assertWithMessage("Root element in buffer is not TYPE_OBJECT")
57 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_OBJECT);
Muhammad Qureshi557859e2019-11-14 17:41:02 -080058
59 assertWithMessage("Incorrect number of elements in root object")
60 .that(buffer.get()).isEqualTo(3);
61
62 assertWithMessage("First element is not timestamp")
63 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_LONG);
64
65 assertWithMessage("Incorrect timestamp")
66 .that(buffer.getLong()).isIn(Range.closed(minTimestamp, maxTimestamp));
67
68 assertWithMessage("Second element is not atom id")
69 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_INT);
70
71 assertWithMessage("Incorrect atom id")
72 .that(buffer.getInt()).isEqualTo(expectedAtomId);
73
Muhammad Qureshid2650882019-12-05 18:07:12 -080074 assertWithMessage("Third element is not errors type")
75 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_ERRORS);
76
Muhammad Qureshi557859e2019-11-14 17:41:02 -080077 final int errorMask = buffer.getInt();
78
79 assertWithMessage("ERROR_NO_ATOM_ID should be the only error in the error mask")
80 .that(errorMask).isEqualTo(StatsEvent.ERROR_NO_ATOM_ID);
81
82 assertThat(statsEvent.getNumBytes()).isEqualTo(buffer.position());
83
84 statsEvent.release();
85 }
86
87 @Test
88 public void testIntBooleanIntInt() {
89 final int expectedAtomId = 109;
90 final int field1 = 1;
91 final boolean field2 = true;
92 final int field3 = 3;
93 final int field4 = 4;
94
95 final long minTimestamp = SystemClock.elapsedRealtimeNanos();
96 final StatsEvent statsEvent = StatsEvent.newBuilder()
97 .setAtomId(expectedAtomId)
98 .writeInt(field1)
99 .writeBoolean(field2)
100 .writeInt(field3)
101 .writeInt(field4)
Muhammad Qureshi9e829802019-12-04 15:38:20 -0800102 .usePooledBuffer()
Muhammad Qureshi557859e2019-11-14 17:41:02 -0800103 .build();
104 final long maxTimestamp = SystemClock.elapsedRealtimeNanos();
105
106 assertThat(statsEvent.getAtomId()).isEqualTo(expectedAtomId);
107
108 final ByteBuffer buffer =
109 ByteBuffer.wrap(statsEvent.getBytes()).order(ByteOrder.LITTLE_ENDIAN);
110
111 assertWithMessage("Root element in buffer is not TYPE_OBJECT")
112 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_OBJECT);
113
114 assertWithMessage("Incorrect number of elements in root object")
115 .that(buffer.get()).isEqualTo(6);
116
117 assertWithMessage("First element is not timestamp")
118 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_LONG);
119
120 assertWithMessage("Incorrect timestamp")
121 .that(buffer.getLong()).isIn(Range.closed(minTimestamp, maxTimestamp));
122
123 assertWithMessage("Second element is not atom id")
124 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_INT);
125
126 assertWithMessage("Incorrect atom id")
127 .that(buffer.getInt()).isEqualTo(expectedAtomId);
128
129 assertWithMessage("First field is not Int")
130 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_INT);
131
132 assertWithMessage("Incorrect field 1")
133 .that(buffer.getInt()).isEqualTo(field1);
134
135 assertWithMessage("Second field is not Boolean")
136 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_BOOLEAN);
137
138 assertWithMessage("Incorrect field 2")
139 .that(buffer.get()).isEqualTo(1);
140
141 assertWithMessage("Third field is not Int")
142 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_INT);
143
144 assertWithMessage("Incorrect field 3")
145 .that(buffer.getInt()).isEqualTo(field3);
146
147 assertWithMessage("Fourth field is not Int")
148 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_INT);
149
150 assertWithMessage("Incorrect field 4")
151 .that(buffer.getInt()).isEqualTo(field4);
152
153 assertThat(statsEvent.getNumBytes()).isEqualTo(buffer.position());
154
155 statsEvent.release();
156 }
157
158 @Test
159 public void testStringFloatByteArray() {
160 final int expectedAtomId = 109;
161 final String field1 = "Str 1";
162 final float field2 = 9.334f;
163 final byte[] field3 = new byte[] { 56, 23, 89, -120 };
164
165 final long minTimestamp = SystemClock.elapsedRealtimeNanos();
166 final StatsEvent statsEvent = StatsEvent.newBuilder()
167 .setAtomId(expectedAtomId)
168 .writeString(field1)
169 .writeFloat(field2)
170 .writeByteArray(field3)
Muhammad Qureshi9e829802019-12-04 15:38:20 -0800171 .usePooledBuffer()
Muhammad Qureshi557859e2019-11-14 17:41:02 -0800172 .build();
173 final long maxTimestamp = SystemClock.elapsedRealtimeNanos();
174
175 assertThat(statsEvent.getAtomId()).isEqualTo(expectedAtomId);
176
177 final ByteBuffer buffer =
178 ByteBuffer.wrap(statsEvent.getBytes()).order(ByteOrder.LITTLE_ENDIAN);
179
180 assertWithMessage("Root element in buffer is not TYPE_OBJECT")
181 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_OBJECT);
182
183 assertWithMessage("Incorrect number of elements in root object")
184 .that(buffer.get()).isEqualTo(5);
185
186 assertWithMessage("First element is not timestamp")
187 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_LONG);
188
189 assertWithMessage("Incorrect timestamp")
190 .that(buffer.getLong()).isIn(Range.closed(minTimestamp, maxTimestamp));
191
192 assertWithMessage("Second element is not atom id")
193 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_INT);
194
195 assertWithMessage("Incorrect atom id")
196 .that(buffer.getInt()).isEqualTo(expectedAtomId);
197
198 assertWithMessage("First field is not String")
199 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_STRING);
200
201 final String field1Actual = getStringFromByteBuffer(buffer);
202 assertWithMessage("Incorrect field 1")
203 .that(field1Actual).isEqualTo(field1);
204
205 assertWithMessage("Second field is not Float")
206 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_FLOAT);
207
208 assertWithMessage("Incorrect field 2")
209 .that(buffer.getFloat()).isEqualTo(field2);
210
211 assertWithMessage("Third field is not byte array")
212 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_BYTE_ARRAY);
213
214 final byte[] field3Actual = getByteArrayFromByteBuffer(buffer);
215 assertWithMessage("Incorrect field 3")
216 .that(field3Actual).isEqualTo(field3);
217
218 assertThat(statsEvent.getNumBytes()).isEqualTo(buffer.position());
219
220 statsEvent.release();
221 }
222
223 @Test
224 public void testAttributionChainLong() {
225 final int expectedAtomId = 109;
226 final int[] uids = new int[] { 1, 2, 3, 4, 5 };
227 final String[] tags = new String[] { "1", "2", "3", "4", "5" };
228 final long field2 = -230909823L;
229
230 final long minTimestamp = SystemClock.elapsedRealtimeNanos();
231 final StatsEvent statsEvent = StatsEvent.newBuilder()
232 .setAtomId(expectedAtomId)
233 .writeAttributionChain(uids, tags)
234 .writeLong(field2)
Muhammad Qureshi9e829802019-12-04 15:38:20 -0800235 .usePooledBuffer()
Muhammad Qureshi557859e2019-11-14 17:41:02 -0800236 .build();
237 final long maxTimestamp = SystemClock.elapsedRealtimeNanos();
238
239 assertThat(statsEvent.getAtomId()).isEqualTo(expectedAtomId);
240
241 final ByteBuffer buffer =
242 ByteBuffer.wrap(statsEvent.getBytes()).order(ByteOrder.LITTLE_ENDIAN);
243
244 assertWithMessage("Root element in buffer is not TYPE_OBJECT")
245 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_OBJECT);
246
247 assertWithMessage("Incorrect number of elements in root object")
248 .that(buffer.get()).isEqualTo(4);
249
250 assertWithMessage("First element is not timestamp")
251 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_LONG);
252
253 assertWithMessage("Incorrect timestamp")
254 .that(buffer.getLong()).isIn(Range.closed(minTimestamp, maxTimestamp));
255
256 assertWithMessage("Second element is not atom id")
257 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_INT);
258
259 assertWithMessage("Incorrect atom id")
260 .that(buffer.getInt()).isEqualTo(expectedAtomId);
261
262 assertWithMessage("First field is not Attribution Chain")
263 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_ATTRIBUTION_CHAIN);
264
265 assertWithMessage("Incorrect number of attribution nodes")
266 .that(buffer.get()).isEqualTo((byte) uids.length);
267
268 for (int i = 0; i < tags.length; i++) {
269 assertWithMessage("Incorrect uid in Attribution Chain")
270 .that(buffer.getInt()).isEqualTo(uids[i]);
271
272 final String tag = getStringFromByteBuffer(buffer);
273 assertWithMessage("Incorrect tag in Attribution Chain")
274 .that(tag).isEqualTo(tags[i]);
275 }
276
277 assertWithMessage("Second field is not Long")
278 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_LONG);
279
280 assertWithMessage("Incorrect field 2")
281 .that(buffer.getLong()).isEqualTo(field2);
282
283 assertThat(statsEvent.getNumBytes()).isEqualTo(buffer.position());
284
285 statsEvent.release();
286 }
287
288 @Test
289 public void testKeyValuePairs() {
290 final int expectedAtomId = 109;
291 final SparseIntArray intMap = new SparseIntArray();
292 final SparseLongArray longMap = new SparseLongArray();
293 final SparseArray<String> stringMap = new SparseArray<>();
294 final SparseArray<Float> floatMap = new SparseArray<>();
295 intMap.put(1, -1);
296 intMap.put(2, -2);
297 stringMap.put(3, "abc");
298 stringMap.put(4, "2h");
299 floatMap.put(9, -234.344f);
300
301 final long minTimestamp = SystemClock.elapsedRealtimeNanos();
302 final StatsEvent statsEvent = StatsEvent.newBuilder()
303 .setAtomId(expectedAtomId)
304 .writeKeyValuePairs(intMap, longMap, stringMap, floatMap)
Muhammad Qureshi9e829802019-12-04 15:38:20 -0800305 .usePooledBuffer()
Muhammad Qureshi557859e2019-11-14 17:41:02 -0800306 .build();
307 final long maxTimestamp = SystemClock.elapsedRealtimeNanos();
308
309 assertThat(statsEvent.getAtomId()).isEqualTo(expectedAtomId);
310
311 final ByteBuffer buffer =
312 ByteBuffer.wrap(statsEvent.getBytes()).order(ByteOrder.LITTLE_ENDIAN);
313
314 assertWithMessage("Root element in buffer is not TYPE_OBJECT")
315 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_OBJECT);
316
317 assertWithMessage("Incorrect number of elements in root object")
318 .that(buffer.get()).isEqualTo(3);
319
320 assertWithMessage("First element is not timestamp")
321 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_LONG);
322
323 assertWithMessage("Incorrect timestamp")
324 .that(buffer.getLong()).isIn(Range.closed(minTimestamp, maxTimestamp));
325
326 assertWithMessage("Second element is not atom id")
327 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_INT);
328
329 assertWithMessage("Incorrect atom id")
330 .that(buffer.getInt()).isEqualTo(expectedAtomId);
331
332 assertWithMessage("First field is not KeyValuePairs")
333 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_KEY_VALUE_PAIRS);
334
335 assertWithMessage("Incorrect number of key value pairs")
336 .that(buffer.get()).isEqualTo(
337 (byte) (intMap.size() + longMap.size() + stringMap.size()
338 + floatMap.size()));
339
340 for (int i = 0; i < intMap.size(); i++) {
341 assertWithMessage("Incorrect key in intMap")
342 .that(buffer.getInt()).isEqualTo(intMap.keyAt(i));
343 assertWithMessage("The type id of the value should be TYPE_INT in intMap")
344 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_INT);
345 assertWithMessage("Incorrect value in intMap")
346 .that(buffer.getInt()).isEqualTo(intMap.valueAt(i));
347 }
348
349 for (int i = 0; i < longMap.size(); i++) {
350 assertWithMessage("Incorrect key in longMap")
351 .that(buffer.getInt()).isEqualTo(longMap.keyAt(i));
352 assertWithMessage("The type id of the value should be TYPE_LONG in longMap")
353 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_LONG);
354 assertWithMessage("Incorrect value in longMap")
355 .that(buffer.getLong()).isEqualTo(longMap.valueAt(i));
356 }
357
358 for (int i = 0; i < stringMap.size(); i++) {
359 assertWithMessage("Incorrect key in stringMap")
360 .that(buffer.getInt()).isEqualTo(stringMap.keyAt(i));
361 assertWithMessage("The type id of the value should be TYPE_STRING in stringMap")
362 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_STRING);
363 final String value = getStringFromByteBuffer(buffer);
364 assertWithMessage("Incorrect value in stringMap")
365 .that(value).isEqualTo(stringMap.valueAt(i));
366 }
367
368 for (int i = 0; i < floatMap.size(); i++) {
369 assertWithMessage("Incorrect key in floatMap")
370 .that(buffer.getInt()).isEqualTo(floatMap.keyAt(i));
371 assertWithMessage("The type id of the value should be TYPE_FLOAT in floatMap")
372 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_FLOAT);
373 assertWithMessage("Incorrect value in floatMap")
374 .that(buffer.getFloat()).isEqualTo(floatMap.valueAt(i));
375 }
376
377 assertThat(statsEvent.getNumBytes()).isEqualTo(buffer.position());
378
379 statsEvent.release();
380 }
381
382 @Test
383 public void testSingleAnnotations() {
384 final int expectedAtomId = 109;
385 final int field1 = 1;
386 final byte field1AnnotationId = 45;
387 final boolean field1AnnotationValue = false;
388 final boolean field2 = true;
389 final byte field2AnnotationId = 1;
390 final int field2AnnotationValue = 23;
391
392 final long minTimestamp = SystemClock.elapsedRealtimeNanos();
393 final StatsEvent statsEvent = StatsEvent.newBuilder()
394 .setAtomId(expectedAtomId)
395 .writeInt(field1)
396 .addBooleanAnnotation(field1AnnotationId, field1AnnotationValue)
397 .writeBoolean(field2)
398 .addIntAnnotation(field2AnnotationId, field2AnnotationValue)
Muhammad Qureshi9e829802019-12-04 15:38:20 -0800399 .usePooledBuffer()
Muhammad Qureshi557859e2019-11-14 17:41:02 -0800400 .build();
401 final long maxTimestamp = SystemClock.elapsedRealtimeNanos();
402
403 assertThat(statsEvent.getAtomId()).isEqualTo(expectedAtomId);
404
405 final ByteBuffer buffer =
406 ByteBuffer.wrap(statsEvent.getBytes()).order(ByteOrder.LITTLE_ENDIAN);
407
408 assertWithMessage("Root element in buffer is not TYPE_OBJECT")
409 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_OBJECT);
410
411 assertWithMessage("Incorrect number of elements in root object")
412 .that(buffer.get()).isEqualTo(4);
413
414 assertWithMessage("First element is not timestamp")
415 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_LONG);
416
417 assertWithMessage("Incorrect timestamp")
418 .that(buffer.getLong()).isIn(Range.closed(minTimestamp, maxTimestamp));
419
420 assertWithMessage("Second element is not atom id")
421 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_INT);
422
423 assertWithMessage("Incorrect atom id")
424 .that(buffer.getInt()).isEqualTo(expectedAtomId);
425
426 final byte field1Header = buffer.get();
427 final int field1AnnotationValueCount = field1Header >> 4;
428 final byte field1Type = (byte) (field1Header & 0x0F);
429 assertWithMessage("First field is not Int")
430 .that(field1Type).isEqualTo(StatsEvent.TYPE_INT);
431 assertWithMessage("First field annotation count is wrong")
432 .that(field1AnnotationValueCount).isEqualTo(1);
433 assertWithMessage("Incorrect field 1")
434 .that(buffer.getInt()).isEqualTo(field1);
435 assertWithMessage("First field's annotation id is wrong")
436 .that(buffer.get()).isEqualTo(field1AnnotationId);
437 assertWithMessage("First field's annotation type is wrong")
438 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_BOOLEAN);
439 assertWithMessage("First field's annotation value is wrong")
440 .that(buffer.get()).isEqualTo(field1AnnotationValue ? 1 : 0);
441
442 final byte field2Header = buffer.get();
443 final int field2AnnotationValueCount = field2Header >> 4;
444 final byte field2Type = (byte) (field2Header & 0x0F);
445 assertWithMessage("Second field is not boolean")
446 .that(field2Type).isEqualTo(StatsEvent.TYPE_BOOLEAN);
447 assertWithMessage("Second field annotation count is wrong")
448 .that(field2AnnotationValueCount).isEqualTo(1);
449 assertWithMessage("Incorrect field 2")
450 .that(buffer.get()).isEqualTo(field2 ? 1 : 0);
451 assertWithMessage("Second field's annotation id is wrong")
452 .that(buffer.get()).isEqualTo(field2AnnotationId);
453 assertWithMessage("Second field's annotation type is wrong")
454 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_INT);
455 assertWithMessage("Second field's annotation value is wrong")
456 .that(buffer.getInt()).isEqualTo(field2AnnotationValue);
457
458 assertThat(statsEvent.getNumBytes()).isEqualTo(buffer.position());
459
460 statsEvent.release();
461 }
462
463 private static byte[] getByteArrayFromByteBuffer(final ByteBuffer buffer) {
464 final int numBytes = buffer.getInt();
465 byte[] bytes = new byte[numBytes];
466 buffer.get(bytes);
467 return bytes;
468 }
469
470 private static String getStringFromByteBuffer(final ByteBuffer buffer) {
471 final byte[] bytes = getByteArrayFromByteBuffer(buffer);
472 return new String(bytes, UTF_8);
473 }
474}