blob: 097badadcea95df0bfbe7a64a3ca82203432f170 [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();
47 final StatsEvent statsEvent = StatsEvent.newBuilder().build();
48 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)
102 .build();
103 final long maxTimestamp = SystemClock.elapsedRealtimeNanos();
104
105 assertThat(statsEvent.getAtomId()).isEqualTo(expectedAtomId);
106
107 final ByteBuffer buffer =
108 ByteBuffer.wrap(statsEvent.getBytes()).order(ByteOrder.LITTLE_ENDIAN);
109
110 assertWithMessage("Root element in buffer is not TYPE_OBJECT")
111 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_OBJECT);
112
113 assertWithMessage("Incorrect number of elements in root object")
114 .that(buffer.get()).isEqualTo(6);
115
116 assertWithMessage("First element is not timestamp")
117 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_LONG);
118
119 assertWithMessage("Incorrect timestamp")
120 .that(buffer.getLong()).isIn(Range.closed(minTimestamp, maxTimestamp));
121
122 assertWithMessage("Second element is not atom id")
123 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_INT);
124
125 assertWithMessage("Incorrect atom id")
126 .that(buffer.getInt()).isEqualTo(expectedAtomId);
127
128 assertWithMessage("First field is not Int")
129 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_INT);
130
131 assertWithMessage("Incorrect field 1")
132 .that(buffer.getInt()).isEqualTo(field1);
133
134 assertWithMessage("Second field is not Boolean")
135 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_BOOLEAN);
136
137 assertWithMessage("Incorrect field 2")
138 .that(buffer.get()).isEqualTo(1);
139
140 assertWithMessage("Third field is not Int")
141 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_INT);
142
143 assertWithMessage("Incorrect field 3")
144 .that(buffer.getInt()).isEqualTo(field3);
145
146 assertWithMessage("Fourth field is not Int")
147 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_INT);
148
149 assertWithMessage("Incorrect field 4")
150 .that(buffer.getInt()).isEqualTo(field4);
151
152 assertThat(statsEvent.getNumBytes()).isEqualTo(buffer.position());
153
154 statsEvent.release();
155 }
156
157 @Test
158 public void testStringFloatByteArray() {
159 final int expectedAtomId = 109;
160 final String field1 = "Str 1";
161 final float field2 = 9.334f;
162 final byte[] field3 = new byte[] { 56, 23, 89, -120 };
163
164 final long minTimestamp = SystemClock.elapsedRealtimeNanos();
165 final StatsEvent statsEvent = StatsEvent.newBuilder()
166 .setAtomId(expectedAtomId)
167 .writeString(field1)
168 .writeFloat(field2)
169 .writeByteArray(field3)
170 .build();
171 final long maxTimestamp = SystemClock.elapsedRealtimeNanos();
172
173 assertThat(statsEvent.getAtomId()).isEqualTo(expectedAtomId);
174
175 final ByteBuffer buffer =
176 ByteBuffer.wrap(statsEvent.getBytes()).order(ByteOrder.LITTLE_ENDIAN);
177
178 assertWithMessage("Root element in buffer is not TYPE_OBJECT")
179 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_OBJECT);
180
181 assertWithMessage("Incorrect number of elements in root object")
182 .that(buffer.get()).isEqualTo(5);
183
184 assertWithMessage("First element is not timestamp")
185 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_LONG);
186
187 assertWithMessage("Incorrect timestamp")
188 .that(buffer.getLong()).isIn(Range.closed(minTimestamp, maxTimestamp));
189
190 assertWithMessage("Second element is not atom id")
191 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_INT);
192
193 assertWithMessage("Incorrect atom id")
194 .that(buffer.getInt()).isEqualTo(expectedAtomId);
195
196 assertWithMessage("First field is not String")
197 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_STRING);
198
199 final String field1Actual = getStringFromByteBuffer(buffer);
200 assertWithMessage("Incorrect field 1")
201 .that(field1Actual).isEqualTo(field1);
202
203 assertWithMessage("Second field is not Float")
204 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_FLOAT);
205
206 assertWithMessage("Incorrect field 2")
207 .that(buffer.getFloat()).isEqualTo(field2);
208
209 assertWithMessage("Third field is not byte array")
210 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_BYTE_ARRAY);
211
212 final byte[] field3Actual = getByteArrayFromByteBuffer(buffer);
213 assertWithMessage("Incorrect field 3")
214 .that(field3Actual).isEqualTo(field3);
215
216 assertThat(statsEvent.getNumBytes()).isEqualTo(buffer.position());
217
218 statsEvent.release();
219 }
220
221 @Test
222 public void testAttributionChainLong() {
223 final int expectedAtomId = 109;
224 final int[] uids = new int[] { 1, 2, 3, 4, 5 };
225 final String[] tags = new String[] { "1", "2", "3", "4", "5" };
226 final long field2 = -230909823L;
227
228 final long minTimestamp = SystemClock.elapsedRealtimeNanos();
229 final StatsEvent statsEvent = StatsEvent.newBuilder()
230 .setAtomId(expectedAtomId)
231 .writeAttributionChain(uids, tags)
232 .writeLong(field2)
233 .build();
234 final long maxTimestamp = SystemClock.elapsedRealtimeNanos();
235
236 assertThat(statsEvent.getAtomId()).isEqualTo(expectedAtomId);
237
238 final ByteBuffer buffer =
239 ByteBuffer.wrap(statsEvent.getBytes()).order(ByteOrder.LITTLE_ENDIAN);
240
241 assertWithMessage("Root element in buffer is not TYPE_OBJECT")
242 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_OBJECT);
243
244 assertWithMessage("Incorrect number of elements in root object")
245 .that(buffer.get()).isEqualTo(4);
246
247 assertWithMessage("First element is not timestamp")
248 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_LONG);
249
250 assertWithMessage("Incorrect timestamp")
251 .that(buffer.getLong()).isIn(Range.closed(minTimestamp, maxTimestamp));
252
253 assertWithMessage("Second element is not atom id")
254 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_INT);
255
256 assertWithMessage("Incorrect atom id")
257 .that(buffer.getInt()).isEqualTo(expectedAtomId);
258
259 assertWithMessage("First field is not Attribution Chain")
260 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_ATTRIBUTION_CHAIN);
261
262 assertWithMessage("Incorrect number of attribution nodes")
263 .that(buffer.get()).isEqualTo((byte) uids.length);
264
265 for (int i = 0; i < tags.length; i++) {
266 assertWithMessage("Incorrect uid in Attribution Chain")
267 .that(buffer.getInt()).isEqualTo(uids[i]);
268
269 final String tag = getStringFromByteBuffer(buffer);
270 assertWithMessage("Incorrect tag in Attribution Chain")
271 .that(tag).isEqualTo(tags[i]);
272 }
273
274 assertWithMessage("Second field is not Long")
275 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_LONG);
276
277 assertWithMessage("Incorrect field 2")
278 .that(buffer.getLong()).isEqualTo(field2);
279
280 assertThat(statsEvent.getNumBytes()).isEqualTo(buffer.position());
281
282 statsEvent.release();
283 }
284
285 @Test
286 public void testKeyValuePairs() {
287 final int expectedAtomId = 109;
288 final SparseIntArray intMap = new SparseIntArray();
289 final SparseLongArray longMap = new SparseLongArray();
290 final SparseArray<String> stringMap = new SparseArray<>();
291 final SparseArray<Float> floatMap = new SparseArray<>();
292 intMap.put(1, -1);
293 intMap.put(2, -2);
294 stringMap.put(3, "abc");
295 stringMap.put(4, "2h");
296 floatMap.put(9, -234.344f);
297
298 final long minTimestamp = SystemClock.elapsedRealtimeNanos();
299 final StatsEvent statsEvent = StatsEvent.newBuilder()
300 .setAtomId(expectedAtomId)
301 .writeKeyValuePairs(intMap, longMap, stringMap, floatMap)
302 .build();
303 final long maxTimestamp = SystemClock.elapsedRealtimeNanos();
304
305 assertThat(statsEvent.getAtomId()).isEqualTo(expectedAtomId);
306
307 final ByteBuffer buffer =
308 ByteBuffer.wrap(statsEvent.getBytes()).order(ByteOrder.LITTLE_ENDIAN);
309
310 assertWithMessage("Root element in buffer is not TYPE_OBJECT")
311 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_OBJECT);
312
313 assertWithMessage("Incorrect number of elements in root object")
314 .that(buffer.get()).isEqualTo(3);
315
316 assertWithMessage("First element is not timestamp")
317 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_LONG);
318
319 assertWithMessage("Incorrect timestamp")
320 .that(buffer.getLong()).isIn(Range.closed(minTimestamp, maxTimestamp));
321
322 assertWithMessage("Second element is not atom id")
323 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_INT);
324
325 assertWithMessage("Incorrect atom id")
326 .that(buffer.getInt()).isEqualTo(expectedAtomId);
327
328 assertWithMessage("First field is not KeyValuePairs")
329 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_KEY_VALUE_PAIRS);
330
331 assertWithMessage("Incorrect number of key value pairs")
332 .that(buffer.get()).isEqualTo(
333 (byte) (intMap.size() + longMap.size() + stringMap.size()
334 + floatMap.size()));
335
336 for (int i = 0; i < intMap.size(); i++) {
337 assertWithMessage("Incorrect key in intMap")
338 .that(buffer.getInt()).isEqualTo(intMap.keyAt(i));
339 assertWithMessage("The type id of the value should be TYPE_INT in intMap")
340 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_INT);
341 assertWithMessage("Incorrect value in intMap")
342 .that(buffer.getInt()).isEqualTo(intMap.valueAt(i));
343 }
344
345 for (int i = 0; i < longMap.size(); i++) {
346 assertWithMessage("Incorrect key in longMap")
347 .that(buffer.getInt()).isEqualTo(longMap.keyAt(i));
348 assertWithMessage("The type id of the value should be TYPE_LONG in longMap")
349 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_LONG);
350 assertWithMessage("Incorrect value in longMap")
351 .that(buffer.getLong()).isEqualTo(longMap.valueAt(i));
352 }
353
354 for (int i = 0; i < stringMap.size(); i++) {
355 assertWithMessage("Incorrect key in stringMap")
356 .that(buffer.getInt()).isEqualTo(stringMap.keyAt(i));
357 assertWithMessage("The type id of the value should be TYPE_STRING in stringMap")
358 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_STRING);
359 final String value = getStringFromByteBuffer(buffer);
360 assertWithMessage("Incorrect value in stringMap")
361 .that(value).isEqualTo(stringMap.valueAt(i));
362 }
363
364 for (int i = 0; i < floatMap.size(); i++) {
365 assertWithMessage("Incorrect key in floatMap")
366 .that(buffer.getInt()).isEqualTo(floatMap.keyAt(i));
367 assertWithMessage("The type id of the value should be TYPE_FLOAT in floatMap")
368 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_FLOAT);
369 assertWithMessage("Incorrect value in floatMap")
370 .that(buffer.getFloat()).isEqualTo(floatMap.valueAt(i));
371 }
372
373 assertThat(statsEvent.getNumBytes()).isEqualTo(buffer.position());
374
375 statsEvent.release();
376 }
377
378 @Test
379 public void testSingleAnnotations() {
380 final int expectedAtomId = 109;
381 final int field1 = 1;
382 final byte field1AnnotationId = 45;
383 final boolean field1AnnotationValue = false;
384 final boolean field2 = true;
385 final byte field2AnnotationId = 1;
386 final int field2AnnotationValue = 23;
387
388 final long minTimestamp = SystemClock.elapsedRealtimeNanos();
389 final StatsEvent statsEvent = StatsEvent.newBuilder()
390 .setAtomId(expectedAtomId)
391 .writeInt(field1)
392 .addBooleanAnnotation(field1AnnotationId, field1AnnotationValue)
393 .writeBoolean(field2)
394 .addIntAnnotation(field2AnnotationId, field2AnnotationValue)
395 .build();
396 final long maxTimestamp = SystemClock.elapsedRealtimeNanos();
397
398 assertThat(statsEvent.getAtomId()).isEqualTo(expectedAtomId);
399
400 final ByteBuffer buffer =
401 ByteBuffer.wrap(statsEvent.getBytes()).order(ByteOrder.LITTLE_ENDIAN);
402
403 assertWithMessage("Root element in buffer is not TYPE_OBJECT")
404 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_OBJECT);
405
406 assertWithMessage("Incorrect number of elements in root object")
407 .that(buffer.get()).isEqualTo(4);
408
409 assertWithMessage("First element is not timestamp")
410 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_LONG);
411
412 assertWithMessage("Incorrect timestamp")
413 .that(buffer.getLong()).isIn(Range.closed(minTimestamp, maxTimestamp));
414
415 assertWithMessage("Second element is not atom id")
416 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_INT);
417
418 assertWithMessage("Incorrect atom id")
419 .that(buffer.getInt()).isEqualTo(expectedAtomId);
420
421 final byte field1Header = buffer.get();
422 final int field1AnnotationValueCount = field1Header >> 4;
423 final byte field1Type = (byte) (field1Header & 0x0F);
424 assertWithMessage("First field is not Int")
425 .that(field1Type).isEqualTo(StatsEvent.TYPE_INT);
426 assertWithMessage("First field annotation count is wrong")
427 .that(field1AnnotationValueCount).isEqualTo(1);
428 assertWithMessage("Incorrect field 1")
429 .that(buffer.getInt()).isEqualTo(field1);
430 assertWithMessage("First field's annotation id is wrong")
431 .that(buffer.get()).isEqualTo(field1AnnotationId);
432 assertWithMessage("First field's annotation type is wrong")
433 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_BOOLEAN);
434 assertWithMessage("First field's annotation value is wrong")
435 .that(buffer.get()).isEqualTo(field1AnnotationValue ? 1 : 0);
436
437 final byte field2Header = buffer.get();
438 final int field2AnnotationValueCount = field2Header >> 4;
439 final byte field2Type = (byte) (field2Header & 0x0F);
440 assertWithMessage("Second field is not boolean")
441 .that(field2Type).isEqualTo(StatsEvent.TYPE_BOOLEAN);
442 assertWithMessage("Second field annotation count is wrong")
443 .that(field2AnnotationValueCount).isEqualTo(1);
444 assertWithMessage("Incorrect field 2")
445 .that(buffer.get()).isEqualTo(field2 ? 1 : 0);
446 assertWithMessage("Second field's annotation id is wrong")
447 .that(buffer.get()).isEqualTo(field2AnnotationId);
448 assertWithMessage("Second field's annotation type is wrong")
449 .that(buffer.get()).isEqualTo(StatsEvent.TYPE_INT);
450 assertWithMessage("Second field's annotation value is wrong")
451 .that(buffer.getInt()).isEqualTo(field2AnnotationValue);
452
453 assertThat(statsEvent.getNumBytes()).isEqualTo(buffer.position());
454
455 statsEvent.release();
456 }
457
458 private static byte[] getByteArrayFromByteBuffer(final ByteBuffer buffer) {
459 final int numBytes = buffer.getInt();
460 byte[] bytes = new byte[numBytes];
461 buffer.get(bytes);
462 return bytes;
463 }
464
465 private static String getStringFromByteBuffer(final ByteBuffer buffer) {
466 final byte[] bytes = getByteArrayFromByteBuffer(buffer);
467 return new String(bytes, UTF_8);
468 }
469}