1 /*
2  * Copyright (C) 2023 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.util;
18 
19 import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify;
20 import static com.android.os.AtomsProto.Atom.TOMB_STONE_OCCURRED_FIELD_NUMBER;
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import android.util.StatsLog;
24 import com.android.modules.utils.testing.ExtendedMockitoRule;
25 import com.android.os.AtomsProto.Atom;
26 import com.android.os.AtomsProto.TestAtomReported;
27 import com.android.os.AtomsProto.TrainExperimentIds;
28 import org.junit.Ignore;
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.mockito.ArgumentCaptor;
32 import org.mockito.Captor;
33 
34 /**
35  * Internal tests for {@link StatsEventTestUtils}.
36  */
37 public final class StatsEventTestUtilsTest {
38     @Rule
39     public final ExtendedMockitoRule mExtendedMockitoRule =
40             new ExtendedMockitoRule.Builder(this).mockStatic(StatsLog.class).build();
41 
42     @Captor ArgumentCaptor<StatsEvent> mStatsEventCaptor;
43 
44     @Ignore("b/287773614")
45     @Test
testOnlyAtomId()46     public void testOnlyAtomId() throws Exception {
47         Atom atom = StatsEventTestUtils.convertToAtom(
48                 StatsEvent.newBuilder()
49                         .setAtomId(TOMB_STONE_OCCURRED_FIELD_NUMBER)
50                         .usePooledBuffer()
51                         .build());
52 
53         assertThat(atom.hasTombStoneOccurred()).isTrue();
54     }
55 
56     @Test
testOneField()57     public void testOneField() throws Exception {
58         StatsdTestStatsLog.write(StatsdTestStatsLog.BATTERY_LEVEL_CHANGED, 3);
59         verify(() -> StatsLog.write(mStatsEventCaptor.capture()));
60         Atom atom = StatsEventTestUtils.convertToAtom(mStatsEventCaptor.getValue());
61 
62         assertThat(atom.hasBatteryLevelChanged()).isTrue();
63         assertThat(atom.getBatteryLevelChanged().getBatteryLevel()).isEqualTo(3);
64     }
65 
66     @Test
testTestAtomReported()67     public void testTestAtomReported() throws Exception {
68         TrainExperimentIds trainExpIds =
69                 TrainExperimentIds.newBuilder().addExperimentId(10L).addExperimentId(20L).build();
70 
71         StatsdTestStatsLog.write(StatsdTestStatsLog.TEST_ATOM_REPORTED,
72                 /* uid */ new int[] {1000},
73                 /* tag */ new String[] {"tag"},
74                 /* int_field */ 1,
75                 /* long_field */ 2L,
76                 /* float_field */ 3.5f,
77                 /* string_field */ "abc",
78                 /* boolean_field */ true,
79                 /* state */ StatsdTestStatsLog.TEST_ATOM_REPORTED__STATE__ON,
80                 /* bytes_field */ trainExpIds.toByteArray(),
81                 /* repeated_int_field */ new int[] {4, 5, 6},
82                 /* repeated_long_field */ new long[] {7L, 8L},
83                 /* repeated_float_field */ new float[] {},
84                 /* repeated_string_field */ new String[] {"xyz"},
85                 /* repeated_boolean_field */ new boolean[] {false, false},
86                 /* repeated_enum_field */
87                 new int[] {StatsdTestStatsLog.TEST_ATOM_REPORTED__STATE__OFF});
88         verify(() -> StatsLog.write(mStatsEventCaptor.capture()));
89         Atom atom = StatsEventTestUtils.convertToAtom(mStatsEventCaptor.getValue());
90 
91         assertThat(atom.hasTestAtomReported()).isTrue();
92         TestAtomReported tar = atom.getTestAtomReported();
93         assertThat(tar.getAttributionNodeCount()).isEqualTo(1);
94         assertThat(tar.getAttributionNode(0).getUid()).isEqualTo(1000);
95         assertThat(tar.getAttributionNode(0).getTag()).isEqualTo("tag");
96         assertThat(tar.getIntField()).isEqualTo(1);
97         assertThat(tar.getLongField()).isEqualTo(2L);
98         assertThat(tar.getFloatField()).isEqualTo(3.5f);
99         assertThat(tar.getStringField()).isEqualTo("abc");
100         assertThat(tar.getBooleanField()).isEqualTo(true);
101         assertThat(tar.getState()).isEqualTo(TestAtomReported.State.ON);
102         assertThat(tar.getBytesField().getExperimentIdList()).containsExactly(10L, 20L);
103         assertThat(tar.getRepeatedIntFieldList()).containsExactly(4, 5, 6);
104         assertThat(tar.getRepeatedLongFieldList()).containsExactly(7L, 8L);
105         assertThat(tar.getRepeatedFloatFieldCount()).isEqualTo(0);
106         assertThat(tar.getRepeatedStringFieldList()).containsExactly("xyz");
107         assertThat(tar.getRepeatedBooleanFieldList()).containsExactly(false, false);
108         assertThat(tar.getRepeatedEnumFieldList()).containsExactly(TestAtomReported.State.OFF);
109     }
110 }
111