Adrian Roos | 111aff9 | 2017-09-27 18:11:46 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 com.android.server.wm; |
| 18 | |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 19 | import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation; |
| 20 | |
Tadashi G. Takaoka | a7a6695 | 2018-11-16 15:04:21 +0900 | [diff] [blame] | 21 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.doAnswer; |
| 22 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.mock; |
| 23 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify; |
| 24 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.verifyZeroInteractions; |
| 25 | |
Adrian Roos | 111aff9 | 2017-09-27 18:11:46 +0200 | [diff] [blame] | 26 | import static org.junit.Assert.assertArrayEquals; |
| 27 | import static org.junit.Assert.assertEquals; |
| 28 | import static org.junit.Assert.assertFalse; |
| 29 | import static org.junit.Assert.assertTrue; |
| 30 | import static org.mockito.ArgumentMatchers.any; |
| 31 | import static org.mockito.ArgumentMatchers.eq; |
Adrian Roos | 111aff9 | 2017-09-27 18:11:46 +0200 | [diff] [blame] | 32 | |
| 33 | import android.content.Context; |
| 34 | import android.platform.test.annotations.Presubmit; |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 35 | import android.testing.DexmakerShareClassLoaderRule; |
Adrian Roos | 111aff9 | 2017-09-27 18:11:46 +0200 | [diff] [blame] | 36 | import android.util.proto.ProtoOutputStream; |
Nataniel Borges | 48d9aa9 | 2019-02-01 14:45:55 -0800 | [diff] [blame] | 37 | import android.view.Choreographer; |
Adrian Roos | 111aff9 | 2017-09-27 18:11:46 +0200 | [diff] [blame] | 38 | |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 39 | import androidx.test.filters.SmallTest; |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 40 | |
Adrian Roos | 111aff9 | 2017-09-27 18:11:46 +0200 | [diff] [blame] | 41 | import com.android.internal.util.Preconditions; |
Adrian Roos | 111aff9 | 2017-09-27 18:11:46 +0200 | [diff] [blame] | 42 | |
| 43 | import org.junit.After; |
| 44 | import org.junit.Before; |
| 45 | import org.junit.Ignore; |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 46 | import org.junit.Rule; |
Adrian Roos | 111aff9 | 2017-09-27 18:11:46 +0200 | [diff] [blame] | 47 | import org.junit.Test; |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 48 | import org.mockito.Mock; |
| 49 | import org.mockito.MockitoAnnotations; |
Adrian Roos | 111aff9 | 2017-09-27 18:11:46 +0200 | [diff] [blame] | 50 | |
| 51 | import java.io.File; |
| 52 | import java.io.FileInputStream; |
| 53 | import java.io.InputStream; |
| 54 | import java.io.PrintWriter; |
| 55 | import java.nio.charset.StandardCharsets; |
| 56 | |
| 57 | /** |
| 58 | * Test class for {@link WindowTracing}. |
| 59 | * |
| 60 | * Build/Install/Run: |
Yunfan Chen | d552c16 | 2019-02-08 16:51:28 +0900 | [diff] [blame] | 61 | * atest WmTests:WindowTracingTest |
Adrian Roos | 111aff9 | 2017-09-27 18:11:46 +0200 | [diff] [blame] | 62 | */ |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 63 | @SmallTest |
| 64 | @Presubmit |
| 65 | public class WindowTracingTest { |
Adrian Roos | 111aff9 | 2017-09-27 18:11:46 +0200 | [diff] [blame] | 66 | |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 67 | private static final byte[] MAGIC_HEADER = new byte[]{ |
| 68 | 0x9, 0x57, 0x49, 0x4e, 0x54, 0x52, 0x41, 0x43, 0x45, |
Adrian Roos | 111aff9 | 2017-09-27 18:11:46 +0200 | [diff] [blame] | 69 | }; |
| 70 | |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 71 | @Rule |
| 72 | public final DexmakerShareClassLoaderRule mDexmakerShareClassLoaderRule = |
| 73 | new DexmakerShareClassLoaderRule(); |
| 74 | |
| 75 | @Mock |
Adrian Roos | 111aff9 | 2017-09-27 18:11:46 +0200 | [diff] [blame] | 76 | private WindowManagerService mWmMock; |
Nataniel Borges | 48d9aa9 | 2019-02-01 14:45:55 -0800 | [diff] [blame] | 77 | @Mock |
| 78 | private Choreographer mChoreographer; |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 79 | private WindowTracing mWindowTracing; |
Adrian Roos | 111aff9 | 2017-09-27 18:11:46 +0200 | [diff] [blame] | 80 | private File mFile; |
| 81 | |
Adrian Roos | 111aff9 | 2017-09-27 18:11:46 +0200 | [diff] [blame] | 82 | @Before |
| 83 | public void setUp() throws Exception { |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 84 | MockitoAnnotations.initMocks(this); |
Adrian Roos | 111aff9 | 2017-09-27 18:11:46 +0200 | [diff] [blame] | 85 | |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 86 | final Context testContext = getInstrumentation().getContext(); |
| 87 | mFile = testContext.getFileStreamPath("tracing_test.dat"); |
Adrian Roos | 111aff9 | 2017-09-27 18:11:46 +0200 | [diff] [blame] | 88 | mFile.delete(); |
| 89 | |
Nataniel Borges | 48d9aa9 | 2019-02-01 14:45:55 -0800 | [diff] [blame] | 90 | mWindowTracing = new WindowTracing(mFile, mWmMock, mChoreographer, |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 91 | new WindowManagerGlobalLock(), 1024); |
Adrian Roos | 111aff9 | 2017-09-27 18:11:46 +0200 | [diff] [blame] | 92 | } |
| 93 | |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 94 | @After |
| 95 | public void tearDown() throws Exception { |
| 96 | mFile.delete(); |
| 97 | } |
| 98 | |
Adrian Roos | 111aff9 | 2017-09-27 18:11:46 +0200 | [diff] [blame] | 99 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 100 | public void isEnabled_returnsFalseByDefault() { |
Adrian Roos | 111aff9 | 2017-09-27 18:11:46 +0200 | [diff] [blame] | 101 | assertFalse(mWindowTracing.isEnabled()); |
| 102 | } |
| 103 | |
| 104 | @Test |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 105 | public void isEnabled_returnsTrueAfterStart() { |
Adrian Roos | 111aff9 | 2017-09-27 18:11:46 +0200 | [diff] [blame] | 106 | mWindowTracing.startTrace(mock(PrintWriter.class)); |
| 107 | assertTrue(mWindowTracing.isEnabled()); |
| 108 | } |
| 109 | |
| 110 | @Test |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 111 | public void isEnabled_returnsFalseAfterStop() { |
Adrian Roos | 111aff9 | 2017-09-27 18:11:46 +0200 | [diff] [blame] | 112 | mWindowTracing.startTrace(mock(PrintWriter.class)); |
| 113 | mWindowTracing.stopTrace(mock(PrintWriter.class)); |
| 114 | assertFalse(mWindowTracing.isEnabled()); |
| 115 | } |
| 116 | |
| 117 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 118 | public void trace_discared_whenNotTracing() { |
Nataniel Borges | 48d9aa9 | 2019-02-01 14:45:55 -0800 | [diff] [blame] | 119 | mWindowTracing.logState("where"); |
Adrian Roos | 111aff9 | 2017-09-27 18:11:46 +0200 | [diff] [blame] | 120 | verifyZeroInteractions(mWmMock); |
| 121 | } |
| 122 | |
| 123 | @Test |
| 124 | public void trace_dumpsWindowManagerState_whenTracing() throws Exception { |
| 125 | mWindowTracing.startTrace(mock(PrintWriter.class)); |
Nataniel Borges | 48d9aa9 | 2019-02-01 14:45:55 -0800 | [diff] [blame] | 126 | mWindowTracing.logState("where"); |
Nataniel Borges | 023ecb5 | 2019-01-16 14:15:43 -0800 | [diff] [blame] | 127 | verify(mWmMock).writeToProtoLocked(any(), eq(WindowTraceLogLevel.TRIM)); |
Adrian Roos | 111aff9 | 2017-09-27 18:11:46 +0200 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | @Test |
| 131 | public void traceFile_startsWithMagicHeader() throws Exception { |
| 132 | mWindowTracing.startTrace(mock(PrintWriter.class)); |
| 133 | mWindowTracing.stopTrace(mock(PrintWriter.class)); |
| 134 | |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 135 | assertTrue("Trace file should exist", mFile.exists()); |
| 136 | |
Adrian Roos | 111aff9 | 2017-09-27 18:11:46 +0200 | [diff] [blame] | 137 | byte[] header = new byte[MAGIC_HEADER.length]; |
| 138 | try (InputStream is = new FileInputStream(mFile)) { |
| 139 | assertEquals(MAGIC_HEADER.length, is.read(header)); |
| 140 | assertArrayEquals(MAGIC_HEADER, header); |
| 141 | } |
| 142 | } |
| 143 | |
Adrian Roos | 111aff9 | 2017-09-27 18:11:46 +0200 | [diff] [blame] | 144 | @Ignore("Figure out why this test is crashing when setting up mWmMock.") |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 145 | @Test |
Adrian Roos | 111aff9 | 2017-09-27 18:11:46 +0200 | [diff] [blame] | 146 | public void tracing_endsUpInFile() throws Exception { |
| 147 | mWindowTracing.startTrace(mock(PrintWriter.class)); |
| 148 | |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 149 | doAnswer(inv -> { |
Adrian Roos | 111aff9 | 2017-09-27 18:11:46 +0200 | [diff] [blame] | 150 | inv.<ProtoOutputStream>getArgument(0).write( |
| 151 | WindowManagerTraceProto.WHERE, "TEST_WM_PROTO"); |
| 152 | return null; |
| 153 | }).when(mWmMock).writeToProtoLocked(any(), any()); |
Nataniel Borges | 48d9aa9 | 2019-02-01 14:45:55 -0800 | [diff] [blame] | 154 | mWindowTracing.logState("TEST_WHERE"); |
Adrian Roos | 111aff9 | 2017-09-27 18:11:46 +0200 | [diff] [blame] | 155 | |
| 156 | mWindowTracing.stopTrace(mock(PrintWriter.class)); |
| 157 | |
| 158 | byte[] file = new byte[1000]; |
| 159 | int fileLength; |
| 160 | try (InputStream is = new FileInputStream(mFile)) { |
| 161 | fileLength = is.read(file); |
| 162 | assertTrue(containsBytes(file, fileLength, |
| 163 | "TEST_WHERE".getBytes(StandardCharsets.UTF_8))); |
| 164 | assertTrue(containsBytes(file, fileLength, |
| 165 | "TEST_WM_PROTO".getBytes(StandardCharsets.UTF_8))); |
| 166 | } |
| 167 | } |
| 168 | |
Adrian Roos | 111aff9 | 2017-09-27 18:11:46 +0200 | [diff] [blame] | 169 | /** Return true if {@code needle} appears anywhere in {@code haystack[0..length]} */ |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 170 | private static boolean containsBytes(byte[] haystack, int haystackLength, byte[] needle) { |
| 171 | Preconditions.checkArgument(haystackLength > 0); |
Adrian Roos | 111aff9 | 2017-09-27 18:11:46 +0200 | [diff] [blame] | 172 | Preconditions.checkArgument(needle.length > 0); |
| 173 | |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 174 | outer: for (int i = 0; i <= haystackLength - needle.length; i++) { |
Adrian Roos | 111aff9 | 2017-09-27 18:11:46 +0200 | [diff] [blame] | 175 | for (int j = 0; j < needle.length; j++) { |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 176 | if (haystack[i + j] != needle[j]) { |
Adrian Roos | 111aff9 | 2017-09-27 18:11:46 +0200 | [diff] [blame] | 177 | continue outer; |
| 178 | } |
| 179 | } |
| 180 | return true; |
| 181 | } |
| 182 | return false; |
| 183 | } |
| 184 | |
| 185 | @Test |
| 186 | public void test_containsBytes() { |
| 187 | byte[] haystack = "hello_world".getBytes(StandardCharsets.UTF_8); |
| 188 | assertTrue(containsBytes(haystack, haystack.length, |
| 189 | "hello".getBytes(StandardCharsets.UTF_8))); |
| 190 | assertTrue(containsBytes(haystack, haystack.length, |
| 191 | "world".getBytes(StandardCharsets.UTF_8))); |
| 192 | assertFalse(containsBytes(haystack, 6, |
| 193 | "world".getBytes(StandardCharsets.UTF_8))); |
| 194 | assertFalse(containsBytes(haystack, haystack.length, |
| 195 | "world_".getBytes(StandardCharsets.UTF_8))); |
| 196 | assertFalse(containsBytes(haystack, haystack.length, |
| 197 | "absent".getBytes(StandardCharsets.UTF_8))); |
| 198 | } |
| 199 | } |