blob: 09b75e71d946d60d78433a6163ad1f947b6ae320 [file] [log] [blame]
Nataniel Borges5a38bad2019-01-03 10:32:03 -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
Adam Pardyl0f1b3d42019-08-19 15:24:11 +020017package com.android.server.utils;
Nataniel Borges5a38bad2019-01-03 10:32:03 -080018
19import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
20
21import static com.android.server.wm.WindowManagerTraceFileProto.MAGIC_NUMBER;
Nataniel Borges5a38bad2019-01-03 10:32:03 -080022
23import static org.junit.Assert.assertEquals;
24import static org.junit.Assert.assertTrue;
25
26import android.content.Context;
27import android.platform.test.annotations.Presubmit;
28import android.util.proto.ProtoOutputStream;
29
30import androidx.test.filters.SmallTest;
31
Nataniel Borges98d92aa2019-01-03 14:22:44 -080032import com.android.internal.util.Preconditions;
33
Nataniel Borges5a38bad2019-01-03 10:32:03 -080034import org.junit.After;
35import org.junit.Before;
36import org.junit.Test;
37
38import java.io.File;
Nataniel Borges5a38bad2019-01-03 10:32:03 -080039
40
41/**
Adam Pardyl0f1b3d42019-08-19 15:24:11 +020042 * Test class for {@link TraceBuffer}.
Nataniel Borges5a38bad2019-01-03 10:32:03 -080043 *
44 * Build/Install/Run:
Adam Pardyl0f1b3d42019-08-19 15:24:11 +020045 * atest WmTests:TraceBufferTest
Nataniel Borges5a38bad2019-01-03 10:32:03 -080046 */
47@SmallTest
48@Presubmit
Adam Pardyl0f1b3d42019-08-19 15:24:11 +020049public class TraceBufferTest {
Nataniel Borges5a38bad2019-01-03 10:32:03 -080050 private File mFile;
Adam Pardyl0f1b3d42019-08-19 15:24:11 +020051 private TraceBuffer mBuffer;
Nataniel Borges5a38bad2019-01-03 10:32:03 -080052
53 @Before
54 public void setUp() throws Exception {
55 final Context testContext = getInstrumentation().getContext();
56 mFile = testContext.getFileStreamPath("tracing_test.dat");
57 mFile.delete();
Nataniel Borges7d37ce22019-02-05 10:07:02 -080058
Adam Pardyl0f1b3d42019-08-19 15:24:11 +020059 mBuffer = new TraceBuffer(10);
Nataniel Borges5a38bad2019-01-03 10:32:03 -080060 }
61
62 @After
63 public void tearDown() throws Exception {
64 mFile.delete();
65 }
66
67 @Test
Nataniel Borges7d37ce22019-02-05 10:07:02 -080068 public void test_addItem() {
Nataniel Borges98d92aa2019-01-03 14:22:44 -080069 ProtoOutputStream toWrite = getDummy(1);
Nataniel Borges023ecb52019-01-16 14:15:43 -080070 final int objectSize = toWrite.getRawSize();
Nataniel Borges7d37ce22019-02-05 10:07:02 -080071 mBuffer.setCapacity(objectSize);
72 mBuffer.resetBuffer();
Nataniel Borges98d92aa2019-01-03 14:22:44 -080073
Nataniel Borges7d37ce22019-02-05 10:07:02 -080074 Preconditions.checkArgument(mBuffer.size() == 0);
Nataniel Borges98d92aa2019-01-03 14:22:44 -080075
Nataniel Borges7d37ce22019-02-05 10:07:02 -080076 mBuffer.add(toWrite);
Nataniel Borges98d92aa2019-01-03 14:22:44 -080077
Nataniel Borges7d37ce22019-02-05 10:07:02 -080078 assertEquals("Item was not added to the buffer", 1, mBuffer.size());
Nataniel Borges98d92aa2019-01-03 14:22:44 -080079 assertEquals("Total buffer getSize differs from inserted object",
Nataniel Borges7d37ce22019-02-05 10:07:02 -080080 mBuffer.getBufferSize(), objectSize);
81 assertEquals("Available buffer space does not match used one", 0,
82 mBuffer.getAvailableSpace());
Nataniel Borges98d92aa2019-01-03 14:22:44 -080083 }
84
85 @Test
Nataniel Borges7d37ce22019-02-05 10:07:02 -080086 public void test_addItemMustOverwriteOne() {
Nataniel Borges98d92aa2019-01-03 14:22:44 -080087 ProtoOutputStream toWrite1 = getDummy(1);
88 ProtoOutputStream toWrite2 = getDummy(2);
89 ProtoOutputStream toWrite3 = getDummy(3);
Nataniel Borges023ecb52019-01-16 14:15:43 -080090 final int objectSize = toWrite1.getRawSize();
Nataniel Borges98d92aa2019-01-03 14:22:44 -080091 final int bufferCapacity = objectSize * 2 + 1;
Nataniel Borges7d37ce22019-02-05 10:07:02 -080092 mBuffer.setCapacity(bufferCapacity);
93 mBuffer.resetBuffer();
Nataniel Borges98d92aa2019-01-03 14:22:44 -080094
Nataniel Borges7d37ce22019-02-05 10:07:02 -080095 mBuffer.add(toWrite1);
Nataniel Borges023ecb52019-01-16 14:15:43 -080096 byte[] toWrite1Bytes = toWrite1.getBytes();
Nataniel Borges98d92aa2019-01-03 14:22:44 -080097 assertTrue("First element should be in the list",
Nataniel Borges7d37ce22019-02-05 10:07:02 -080098 mBuffer.contains(toWrite1Bytes));
Nataniel Borges98d92aa2019-01-03 14:22:44 -080099
Nataniel Borges7d37ce22019-02-05 10:07:02 -0800100 mBuffer.add(toWrite2);
Nataniel Borges023ecb52019-01-16 14:15:43 -0800101 byte[] toWrite2Bytes = toWrite2.getBytes();
Nataniel Borges98d92aa2019-01-03 14:22:44 -0800102 assertTrue("First element should be in the list",
Nataniel Borges7d37ce22019-02-05 10:07:02 -0800103 mBuffer.contains(toWrite1Bytes));
Nataniel Borges98d92aa2019-01-03 14:22:44 -0800104 assertTrue("Second element should be in the list",
Nataniel Borges7d37ce22019-02-05 10:07:02 -0800105 mBuffer.contains(toWrite2Bytes));
Nataniel Borges98d92aa2019-01-03 14:22:44 -0800106
Nataniel Borges7d37ce22019-02-05 10:07:02 -0800107 mBuffer.add(toWrite3);
Nataniel Borges023ecb52019-01-16 14:15:43 -0800108 byte[] toWrite3Bytes = toWrite3.getBytes();
Nataniel Borges98d92aa2019-01-03 14:22:44 -0800109 assertTrue("First element should not be in the list",
Nataniel Borges7d37ce22019-02-05 10:07:02 -0800110 !mBuffer.contains(toWrite1Bytes));
Nataniel Borges98d92aa2019-01-03 14:22:44 -0800111 assertTrue("Second element should be in the list",
Nataniel Borges7d37ce22019-02-05 10:07:02 -0800112 mBuffer.contains(toWrite2Bytes));
Nataniel Borges98d92aa2019-01-03 14:22:44 -0800113 assertTrue("Third element should be in the list",
Nataniel Borges7d37ce22019-02-05 10:07:02 -0800114 mBuffer.contains(toWrite3Bytes));
115 assertEquals("Buffer should have 2 elements", 2, mBuffer.size());
Nataniel Borges98d92aa2019-01-03 14:22:44 -0800116 assertEquals(String.format("Buffer is full, used space should be %d", bufferCapacity),
Nataniel Borges7d37ce22019-02-05 10:07:02 -0800117 mBuffer.getBufferSize(), bufferCapacity - 1);
118 assertEquals(" Buffer is full, available space should be 0", 1,
119 mBuffer.getAvailableSpace());
Nataniel Borges98d92aa2019-01-03 14:22:44 -0800120 }
121
122 @Test
Nataniel Borges7d37ce22019-02-05 10:07:02 -0800123 public void test_addItemMustOverwriteMultiple() {
Nataniel Borges98d92aa2019-01-03 14:22:44 -0800124 ProtoOutputStream toWriteSmall1 = getDummy(1);
125 ProtoOutputStream toWriteSmall2 = getDummy(2);
Nataniel Borges023ecb52019-01-16 14:15:43 -0800126 final int objectSize = toWriteSmall1.getRawSize();
Nataniel Borges98d92aa2019-01-03 14:22:44 -0800127 final int bufferCapacity = objectSize * 2;
Nataniel Borges7d37ce22019-02-05 10:07:02 -0800128 mBuffer.setCapacity(bufferCapacity);
129 mBuffer.resetBuffer();
Nataniel Borges98d92aa2019-01-03 14:22:44 -0800130
131 ProtoOutputStream toWriteBig = new ProtoOutputStream();
132 toWriteBig.write(MAGIC_NUMBER, 1);
133 toWriteBig.write(MAGIC_NUMBER, 2);
Nataniel Borges98d92aa2019-01-03 14:22:44 -0800134
Nataniel Borges7d37ce22019-02-05 10:07:02 -0800135 mBuffer.add(toWriteSmall1);
Nataniel Borges023ecb52019-01-16 14:15:43 -0800136 byte[] toWriteSmall1Bytes = toWriteSmall1.getBytes();
Nataniel Borges98d92aa2019-01-03 14:22:44 -0800137 assertTrue("First element should be in the list",
Nataniel Borges7d37ce22019-02-05 10:07:02 -0800138 mBuffer.contains(toWriteSmall1Bytes));
Nataniel Borges98d92aa2019-01-03 14:22:44 -0800139
Nataniel Borges7d37ce22019-02-05 10:07:02 -0800140 mBuffer.add(toWriteSmall2);
Nataniel Borges023ecb52019-01-16 14:15:43 -0800141 byte[] toWriteSmall2Bytes = toWriteSmall2.getBytes();
Nataniel Borges98d92aa2019-01-03 14:22:44 -0800142 assertTrue("First element should be in the list",
Nataniel Borges7d37ce22019-02-05 10:07:02 -0800143 mBuffer.contains(toWriteSmall1Bytes));
Nataniel Borges98d92aa2019-01-03 14:22:44 -0800144 assertTrue("Second element should be in the list",
Nataniel Borges7d37ce22019-02-05 10:07:02 -0800145 mBuffer.contains(toWriteSmall2Bytes));
Nataniel Borges98d92aa2019-01-03 14:22:44 -0800146
Nataniel Borges7d37ce22019-02-05 10:07:02 -0800147 mBuffer.add(toWriteBig);
Nataniel Borges023ecb52019-01-16 14:15:43 -0800148 byte[] toWriteBigBytes = toWriteBig.getBytes();
Nataniel Borges98d92aa2019-01-03 14:22:44 -0800149 assertTrue("Third element should overwrite all others",
Nataniel Borges7d37ce22019-02-05 10:07:02 -0800150 !mBuffer.contains(toWriteSmall1Bytes));
Nataniel Borges98d92aa2019-01-03 14:22:44 -0800151 assertTrue("Third element should overwrite all others",
Nataniel Borges7d37ce22019-02-05 10:07:02 -0800152 !mBuffer.contains(toWriteSmall2Bytes));
Nataniel Borges98d92aa2019-01-03 14:22:44 -0800153 assertTrue("Third element should overwrite all others",
Nataniel Borges7d37ce22019-02-05 10:07:02 -0800154 mBuffer.contains(toWriteBigBytes));
Nataniel Borges98d92aa2019-01-03 14:22:44 -0800155
Nataniel Borges7d37ce22019-02-05 10:07:02 -0800156 assertEquals(" Buffer should have only 1 big element", 1, mBuffer.size());
Nataniel Borges98d92aa2019-01-03 14:22:44 -0800157 assertEquals(String.format(" Buffer is full, used space should be %d", bufferCapacity),
Nataniel Borges7d37ce22019-02-05 10:07:02 -0800158 mBuffer.getBufferSize(), bufferCapacity);
159 assertEquals(" Buffer is full, available space should be 0", 0,
160 mBuffer.getAvailableSpace());
Nataniel Borges98d92aa2019-01-03 14:22:44 -0800161 }
162
Nataniel Borges7d37ce22019-02-05 10:07:02 -0800163 @Test
164 public void test_startResetsBuffer() {
165 ProtoOutputStream toWrite = getDummy(1);
166 mBuffer.resetBuffer();
167 Preconditions.checkArgument(mBuffer.size() == 0);
168
169 mBuffer.add(toWrite);
170 assertEquals("Item was not added to the buffer", 1, mBuffer.size());
171 mBuffer.resetBuffer();
172 assertEquals("Buffer should be empty after reset", 0, mBuffer.size());
173 assertEquals("Buffer size should be 0 after reset", 0, mBuffer.getBufferSize());
Nataniel Borges98d92aa2019-01-03 14:22:44 -0800174 }
175
Nataniel Borges5a38bad2019-01-03 10:32:03 -0800176 private ProtoOutputStream getDummy(int value) {
177 ProtoOutputStream toWrite = new ProtoOutputStream();
178 toWrite.write(MAGIC_NUMBER, value);
179 toWrite.flush();
180
181 return toWrite;
182 }
183
Nataniel Borges5a38bad2019-01-03 10:32:03 -0800184}