-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
ShadowStatsLogTest.java
185 lines (137 loc) · 6.11 KB
/
ShadowStatsLogTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package org.robolectric.shadows;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import static org.junit.Assert.assertEquals;
import android.os.Build;
import android.os.SystemClock;
import android.util.StatsEvent;
import android.util.StatsLog;
import com.google.common.collect.Range;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
/** Tests for {@link ShadowStatsLog} */
@RunWith(RobolectricTestRunner.class)
@Config(minSdk = Build.VERSION_CODES.R)
public final class ShadowStatsLogTest {
@Test
public void testNoFields() {
final StatsEvent statsEvent = StatsEvent.newBuilder().usePooledBuffer().build();
long minTimestamp = SystemClock.elapsedRealtimeNanos();
StatsLog.write(statsEvent);
long maxTimestamp = SystemClock.elapsedRealtimeNanos();
final int expectedAtomId = 0;
assertEquals(1, ShadowStatsLog.getStatsLogs().size());
assertEquals((int) expectedAtomId, (int) ShadowStatsLog.getStatsLogs().get(0).atomId());
final ByteBuffer buffer =
ByteBuffer.wrap(ShadowStatsLog.getStatsLogs().get(0).bytes())
.order(ByteOrder.LITTLE_ENDIAN);
assertWithMessage("Root element in buffer is not TYPE_OBJECT")
.that(buffer.get())
.isEqualTo(StatsEvent.TYPE_OBJECT);
assertWithMessage("Incorrect number of elements in root object")
.that(buffer.get())
.isEqualTo(3);
assertWithMessage("First element is not timestamp")
.that(buffer.get())
.isEqualTo(StatsEvent.TYPE_LONG);
assertWithMessage("Incorrect timestamp")
.that(buffer.getLong())
.isIn(Range.closed(minTimestamp, maxTimestamp));
assertWithMessage("Second element is not atom id")
.that(buffer.get())
.isEqualTo(StatsEvent.TYPE_INT);
assertWithMessage("Incorrect atom id").that(buffer.getInt()).isEqualTo(expectedAtomId);
assertWithMessage("Third element is not errors type")
.that(buffer.get())
.isEqualTo(StatsEvent.TYPE_ERRORS);
final int errorMask = buffer.getInt();
assertWithMessage("ERROR_NO_ATOM_ID should be the only error in the error mask")
.that(errorMask)
.isEqualTo(StatsEvent.ERROR_NO_ATOM_ID);
assertThat(statsEvent.getNumBytes()).isEqualTo(buffer.position());
statsEvent.release();
}
@Test
public void testOnlyAtomId() {
final int expectedAtomId = 109;
final StatsEvent statsEvent =
StatsEvent.newBuilder().setAtomId(expectedAtomId).usePooledBuffer().build();
long minTimestamp = SystemClock.elapsedRealtimeNanos();
StatsLog.write(statsEvent);
long maxTimestamp = SystemClock.elapsedRealtimeNanos();
assertEquals(1, ShadowStatsLog.getStatsLogs().size());
assertEquals((int) expectedAtomId, (int) ShadowStatsLog.getStatsLogs().get(0).atomId());
final ByteBuffer buffer =
ByteBuffer.wrap(ShadowStatsLog.getStatsLogs().get(0).bytes())
.order(ByteOrder.LITTLE_ENDIAN);
assertWithMessage("Root element in buffer is not TYPE_OBJECT")
.that(buffer.get())
.isEqualTo(StatsEvent.TYPE_OBJECT);
assertWithMessage("Incorrect number of elements in root object")
.that(buffer.get())
.isEqualTo(2);
assertWithMessage("First element is not timestamp")
.that(buffer.get())
.isEqualTo(StatsEvent.TYPE_LONG);
assertWithMessage("Incorrect timestamp")
.that(buffer.getLong())
.isIn(Range.closed(minTimestamp, maxTimestamp));
assertWithMessage("Second element is not atom id")
.that(buffer.get())
.isEqualTo(StatsEvent.TYPE_INT);
assertWithMessage("Incorrect atom id").that(buffer.getInt()).isEqualTo(expectedAtomId);
assertThat(statsEvent.getNumBytes()).isEqualTo(buffer.position());
statsEvent.release();
}
@Test
public void testIntIntInt() {
final int expectedAtomId = 109;
final int field1 = 1;
final int field2 = 2;
final int field3 = 3;
final StatsEvent statsEvent =
StatsEvent.newBuilder()
.setAtomId(expectedAtomId)
.writeInt(field1)
.writeInt(field2)
.writeInt(field3)
.usePooledBuffer()
.build();
long minTimestamp = SystemClock.elapsedRealtimeNanos();
StatsLog.write(statsEvent);
long maxTimestamp = SystemClock.elapsedRealtimeNanos();
assertEquals(1, ShadowStatsLog.getStatsLogs().size());
assertEquals((int) expectedAtomId, (int) ShadowStatsLog.getStatsLogs().get(0).atomId());
final ByteBuffer buffer =
ByteBuffer.wrap(ShadowStatsLog.getStatsLogs().get(0).bytes())
.order(ByteOrder.LITTLE_ENDIAN);
assertWithMessage("Root element in buffer is not TYPE_OBJECT")
.that(buffer.get())
.isEqualTo(StatsEvent.TYPE_OBJECT);
assertWithMessage("Incorrect number of elements in root object")
.that(buffer.get())
.isEqualTo(5);
assertWithMessage("First element is not timestamp")
.that(buffer.get())
.isEqualTo(StatsEvent.TYPE_LONG);
assertWithMessage("Incorrect timestamp")
.that(buffer.getLong())
.isIn(Range.closed(minTimestamp, maxTimestamp));
assertWithMessage("Second element is not atom id")
.that(buffer.get())
.isEqualTo(StatsEvent.TYPE_INT);
assertWithMessage("Incorrect atom id").that(buffer.getInt()).isEqualTo(expectedAtomId);
assertWithMessage("First field is not Int").that(buffer.get()).isEqualTo(StatsEvent.TYPE_INT);
assertWithMessage("Incorrect field 1").that(buffer.getInt()).isEqualTo(field1);
assertWithMessage("Third field is not Int").that(buffer.get()).isEqualTo(StatsEvent.TYPE_INT);
assertWithMessage("Incorrect field 2").that(buffer.getInt()).isEqualTo(field2);
assertWithMessage("Fourth field is not Int").that(buffer.get()).isEqualTo(StatsEvent.TYPE_INT);
assertWithMessage("Incorrect field 3").that(buffer.getInt()).isEqualTo(field3);
assertThat(statsEvent.getNumBytes()).isEqualTo(buffer.position());
statsEvent.release();
}
}