Nataniel Borges | 5a38bad | 2019-01-03 10:32:03 -0800 | [diff] [blame] | 1 | /* |
| 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 Pardyl | 0f1b3d4 | 2019-08-19 15:24:11 +0200 | [diff] [blame] | 17 | package com.android.server.utils; |
Nataniel Borges | 5a38bad | 2019-01-03 10:32:03 -0800 | [diff] [blame] | 18 | |
| 19 | import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation; |
| 20 | |
| 21 | import static com.android.server.wm.WindowManagerTraceFileProto.MAGIC_NUMBER; |
Nataniel Borges | 5a38bad | 2019-01-03 10:32:03 -0800 | [diff] [blame] | 22 | |
| 23 | import static org.junit.Assert.assertEquals; |
| 24 | import static org.junit.Assert.assertTrue; |
| 25 | |
| 26 | import android.content.Context; |
| 27 | import android.platform.test.annotations.Presubmit; |
| 28 | import android.util.proto.ProtoOutputStream; |
| 29 | |
| 30 | import androidx.test.filters.SmallTest; |
| 31 | |
Nataniel Borges | 98d92aa | 2019-01-03 14:22:44 -0800 | [diff] [blame] | 32 | import com.android.internal.util.Preconditions; |
| 33 | |
Nataniel Borges | 5a38bad | 2019-01-03 10:32:03 -0800 | [diff] [blame] | 34 | import org.junit.After; |
| 35 | import org.junit.Before; |
| 36 | import org.junit.Test; |
| 37 | |
| 38 | import java.io.File; |
Nataniel Borges | 5a38bad | 2019-01-03 10:32:03 -0800 | [diff] [blame] | 39 | |
| 40 | |
| 41 | /** |
Adam Pardyl | 0f1b3d4 | 2019-08-19 15:24:11 +0200 | [diff] [blame] | 42 | * Test class for {@link TraceBuffer}. |
Nataniel Borges | 5a38bad | 2019-01-03 10:32:03 -0800 | [diff] [blame] | 43 | * |
| 44 | * Build/Install/Run: |
Adam Pardyl | 0f1b3d4 | 2019-08-19 15:24:11 +0200 | [diff] [blame] | 45 | * atest WmTests:TraceBufferTest |
Nataniel Borges | 5a38bad | 2019-01-03 10:32:03 -0800 | [diff] [blame] | 46 | */ |
| 47 | @SmallTest |
| 48 | @Presubmit |
Adam Pardyl | 0f1b3d4 | 2019-08-19 15:24:11 +0200 | [diff] [blame] | 49 | public class TraceBufferTest { |
Nataniel Borges | 5a38bad | 2019-01-03 10:32:03 -0800 | [diff] [blame] | 50 | private File mFile; |
Adam Pardyl | 0f1b3d4 | 2019-08-19 15:24:11 +0200 | [diff] [blame] | 51 | private TraceBuffer mBuffer; |
Nataniel Borges | 5a38bad | 2019-01-03 10:32:03 -0800 | [diff] [blame] | 52 | |
| 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 Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 58 | |
Adam Pardyl | 0f1b3d4 | 2019-08-19 15:24:11 +0200 | [diff] [blame] | 59 | mBuffer = new TraceBuffer(10); |
Nataniel Borges | 5a38bad | 2019-01-03 10:32:03 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @After |
| 63 | public void tearDown() throws Exception { |
| 64 | mFile.delete(); |
| 65 | } |
| 66 | |
| 67 | @Test |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 68 | public void test_addItem() { |
Nataniel Borges | 98d92aa | 2019-01-03 14:22:44 -0800 | [diff] [blame] | 69 | ProtoOutputStream toWrite = getDummy(1); |
Nataniel Borges | 023ecb5 | 2019-01-16 14:15:43 -0800 | [diff] [blame] | 70 | final int objectSize = toWrite.getRawSize(); |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 71 | mBuffer.setCapacity(objectSize); |
| 72 | mBuffer.resetBuffer(); |
Nataniel Borges | 98d92aa | 2019-01-03 14:22:44 -0800 | [diff] [blame] | 73 | |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 74 | Preconditions.checkArgument(mBuffer.size() == 0); |
Nataniel Borges | 98d92aa | 2019-01-03 14:22:44 -0800 | [diff] [blame] | 75 | |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 76 | mBuffer.add(toWrite); |
Nataniel Borges | 98d92aa | 2019-01-03 14:22:44 -0800 | [diff] [blame] | 77 | |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 78 | assertEquals("Item was not added to the buffer", 1, mBuffer.size()); |
Nataniel Borges | 98d92aa | 2019-01-03 14:22:44 -0800 | [diff] [blame] | 79 | assertEquals("Total buffer getSize differs from inserted object", |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 80 | mBuffer.getBufferSize(), objectSize); |
| 81 | assertEquals("Available buffer space does not match used one", 0, |
| 82 | mBuffer.getAvailableSpace()); |
Nataniel Borges | 98d92aa | 2019-01-03 14:22:44 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | @Test |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 86 | public void test_addItemMustOverwriteOne() { |
Nataniel Borges | 98d92aa | 2019-01-03 14:22:44 -0800 | [diff] [blame] | 87 | ProtoOutputStream toWrite1 = getDummy(1); |
| 88 | ProtoOutputStream toWrite2 = getDummy(2); |
| 89 | ProtoOutputStream toWrite3 = getDummy(3); |
Nataniel Borges | 023ecb5 | 2019-01-16 14:15:43 -0800 | [diff] [blame] | 90 | final int objectSize = toWrite1.getRawSize(); |
Nataniel Borges | 98d92aa | 2019-01-03 14:22:44 -0800 | [diff] [blame] | 91 | final int bufferCapacity = objectSize * 2 + 1; |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 92 | mBuffer.setCapacity(bufferCapacity); |
| 93 | mBuffer.resetBuffer(); |
Nataniel Borges | 98d92aa | 2019-01-03 14:22:44 -0800 | [diff] [blame] | 94 | |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 95 | mBuffer.add(toWrite1); |
Nataniel Borges | 023ecb5 | 2019-01-16 14:15:43 -0800 | [diff] [blame] | 96 | byte[] toWrite1Bytes = toWrite1.getBytes(); |
Nataniel Borges | 98d92aa | 2019-01-03 14:22:44 -0800 | [diff] [blame] | 97 | assertTrue("First element should be in the list", |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 98 | mBuffer.contains(toWrite1Bytes)); |
Nataniel Borges | 98d92aa | 2019-01-03 14:22:44 -0800 | [diff] [blame] | 99 | |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 100 | mBuffer.add(toWrite2); |
Nataniel Borges | 023ecb5 | 2019-01-16 14:15:43 -0800 | [diff] [blame] | 101 | byte[] toWrite2Bytes = toWrite2.getBytes(); |
Nataniel Borges | 98d92aa | 2019-01-03 14:22:44 -0800 | [diff] [blame] | 102 | assertTrue("First element should be in the list", |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 103 | mBuffer.contains(toWrite1Bytes)); |
Nataniel Borges | 98d92aa | 2019-01-03 14:22:44 -0800 | [diff] [blame] | 104 | assertTrue("Second element should be in the list", |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 105 | mBuffer.contains(toWrite2Bytes)); |
Nataniel Borges | 98d92aa | 2019-01-03 14:22:44 -0800 | [diff] [blame] | 106 | |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 107 | mBuffer.add(toWrite3); |
Nataniel Borges | 023ecb5 | 2019-01-16 14:15:43 -0800 | [diff] [blame] | 108 | byte[] toWrite3Bytes = toWrite3.getBytes(); |
Nataniel Borges | 98d92aa | 2019-01-03 14:22:44 -0800 | [diff] [blame] | 109 | assertTrue("First element should not be in the list", |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 110 | !mBuffer.contains(toWrite1Bytes)); |
Nataniel Borges | 98d92aa | 2019-01-03 14:22:44 -0800 | [diff] [blame] | 111 | assertTrue("Second element should be in the list", |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 112 | mBuffer.contains(toWrite2Bytes)); |
Nataniel Borges | 98d92aa | 2019-01-03 14:22:44 -0800 | [diff] [blame] | 113 | assertTrue("Third element should be in the list", |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 114 | mBuffer.contains(toWrite3Bytes)); |
| 115 | assertEquals("Buffer should have 2 elements", 2, mBuffer.size()); |
Nataniel Borges | 98d92aa | 2019-01-03 14:22:44 -0800 | [diff] [blame] | 116 | assertEquals(String.format("Buffer is full, used space should be %d", bufferCapacity), |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 117 | mBuffer.getBufferSize(), bufferCapacity - 1); |
| 118 | assertEquals(" Buffer is full, available space should be 0", 1, |
| 119 | mBuffer.getAvailableSpace()); |
Nataniel Borges | 98d92aa | 2019-01-03 14:22:44 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | @Test |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 123 | public void test_addItemMustOverwriteMultiple() { |
Nataniel Borges | 98d92aa | 2019-01-03 14:22:44 -0800 | [diff] [blame] | 124 | ProtoOutputStream toWriteSmall1 = getDummy(1); |
| 125 | ProtoOutputStream toWriteSmall2 = getDummy(2); |
Nataniel Borges | 023ecb5 | 2019-01-16 14:15:43 -0800 | [diff] [blame] | 126 | final int objectSize = toWriteSmall1.getRawSize(); |
Nataniel Borges | 98d92aa | 2019-01-03 14:22:44 -0800 | [diff] [blame] | 127 | final int bufferCapacity = objectSize * 2; |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 128 | mBuffer.setCapacity(bufferCapacity); |
| 129 | mBuffer.resetBuffer(); |
Nataniel Borges | 98d92aa | 2019-01-03 14:22:44 -0800 | [diff] [blame] | 130 | |
| 131 | ProtoOutputStream toWriteBig = new ProtoOutputStream(); |
| 132 | toWriteBig.write(MAGIC_NUMBER, 1); |
| 133 | toWriteBig.write(MAGIC_NUMBER, 2); |
Nataniel Borges | 98d92aa | 2019-01-03 14:22:44 -0800 | [diff] [blame] | 134 | |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 135 | mBuffer.add(toWriteSmall1); |
Nataniel Borges | 023ecb5 | 2019-01-16 14:15:43 -0800 | [diff] [blame] | 136 | byte[] toWriteSmall1Bytes = toWriteSmall1.getBytes(); |
Nataniel Borges | 98d92aa | 2019-01-03 14:22:44 -0800 | [diff] [blame] | 137 | assertTrue("First element should be in the list", |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 138 | mBuffer.contains(toWriteSmall1Bytes)); |
Nataniel Borges | 98d92aa | 2019-01-03 14:22:44 -0800 | [diff] [blame] | 139 | |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 140 | mBuffer.add(toWriteSmall2); |
Nataniel Borges | 023ecb5 | 2019-01-16 14:15:43 -0800 | [diff] [blame] | 141 | byte[] toWriteSmall2Bytes = toWriteSmall2.getBytes(); |
Nataniel Borges | 98d92aa | 2019-01-03 14:22:44 -0800 | [diff] [blame] | 142 | assertTrue("First element should be in the list", |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 143 | mBuffer.contains(toWriteSmall1Bytes)); |
Nataniel Borges | 98d92aa | 2019-01-03 14:22:44 -0800 | [diff] [blame] | 144 | assertTrue("Second element should be in the list", |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 145 | mBuffer.contains(toWriteSmall2Bytes)); |
Nataniel Borges | 98d92aa | 2019-01-03 14:22:44 -0800 | [diff] [blame] | 146 | |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 147 | mBuffer.add(toWriteBig); |
Nataniel Borges | 023ecb5 | 2019-01-16 14:15:43 -0800 | [diff] [blame] | 148 | byte[] toWriteBigBytes = toWriteBig.getBytes(); |
Nataniel Borges | 98d92aa | 2019-01-03 14:22:44 -0800 | [diff] [blame] | 149 | assertTrue("Third element should overwrite all others", |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 150 | !mBuffer.contains(toWriteSmall1Bytes)); |
Nataniel Borges | 98d92aa | 2019-01-03 14:22:44 -0800 | [diff] [blame] | 151 | assertTrue("Third element should overwrite all others", |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 152 | !mBuffer.contains(toWriteSmall2Bytes)); |
Nataniel Borges | 98d92aa | 2019-01-03 14:22:44 -0800 | [diff] [blame] | 153 | assertTrue("Third element should overwrite all others", |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 154 | mBuffer.contains(toWriteBigBytes)); |
Nataniel Borges | 98d92aa | 2019-01-03 14:22:44 -0800 | [diff] [blame] | 155 | |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 156 | assertEquals(" Buffer should have only 1 big element", 1, mBuffer.size()); |
Nataniel Borges | 98d92aa | 2019-01-03 14:22:44 -0800 | [diff] [blame] | 157 | assertEquals(String.format(" Buffer is full, used space should be %d", bufferCapacity), |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 158 | mBuffer.getBufferSize(), bufferCapacity); |
| 159 | assertEquals(" Buffer is full, available space should be 0", 0, |
| 160 | mBuffer.getAvailableSpace()); |
Nataniel Borges | 98d92aa | 2019-01-03 14:22:44 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 163 | @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 Borges | 98d92aa | 2019-01-03 14:22:44 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Nataniel Borges | 5a38bad | 2019-01-03 10:32:03 -0800 | [diff] [blame] | 176 | 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 Borges | 5a38bad | 2019-01-03 10:32:03 -0800 | [diff] [blame] | 184 | } |