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