blob: e3691c6539591d54b3fb0f19342b6b5ebebafab0 [file] [log] [blame]
Adrian Roos111aff92017-09-27 18:11:46 +02001/*
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
17package com.android.server.wm;
18
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070019import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
20
Tadashi G. Takaokaa7a66952018-11-16 15:04:21 +090021import static com.android.dx.mockito.inline.extended.ExtendedMockito.doAnswer;
22import static com.android.dx.mockito.inline.extended.ExtendedMockito.mock;
Nataniel Borges8a5106c2019-03-06 11:31:34 -080023import static com.android.dx.mockito.inline.extended.ExtendedMockito.times;
Tadashi G. Takaokaa7a66952018-11-16 15:04:21 +090024import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify;
25import static com.android.dx.mockito.inline.extended.ExtendedMockito.verifyZeroInteractions;
26
Adrian Roos111aff92017-09-27 18:11:46 +020027import static org.junit.Assert.assertArrayEquals;
28import static org.junit.Assert.assertEquals;
29import static org.junit.Assert.assertFalse;
30import static org.junit.Assert.assertTrue;
31import static org.mockito.ArgumentMatchers.any;
32import static org.mockito.ArgumentMatchers.eq;
Adrian Roos111aff92017-09-27 18:11:46 +020033
34import android.content.Context;
35import android.platform.test.annotations.Presubmit;
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070036import android.testing.DexmakerShareClassLoaderRule;
Adrian Roos111aff92017-09-27 18:11:46 +020037import android.util.proto.ProtoOutputStream;
Nataniel Borges48d9aa92019-02-01 14:45:55 -080038import android.view.Choreographer;
Adrian Roos111aff92017-09-27 18:11:46 +020039
Brett Chabota26eda92018-07-23 13:08:30 -070040import androidx.test.filters.SmallTest;
Brett Chabota26eda92018-07-23 13:08:30 -070041
Adrian Roos111aff92017-09-27 18:11:46 +020042import com.android.internal.util.Preconditions;
Adrian Roos111aff92017-09-27 18:11:46 +020043
44import org.junit.After;
45import org.junit.Before;
46import org.junit.Ignore;
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070047import org.junit.Rule;
Adrian Roos111aff92017-09-27 18:11:46 +020048import org.junit.Test;
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070049import org.mockito.Mock;
50import org.mockito.MockitoAnnotations;
Adrian Roos111aff92017-09-27 18:11:46 +020051
52import java.io.File;
53import java.io.FileInputStream;
54import java.io.InputStream;
55import java.io.PrintWriter;
56import java.nio.charset.StandardCharsets;
57
58/**
59 * Test class for {@link WindowTracing}.
60 *
61 * Build/Install/Run:
Yunfan Chend552c162019-02-08 16:51:28 +090062 * atest WmTests:WindowTracingTest
Adrian Roos111aff92017-09-27 18:11:46 +020063 */
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070064@SmallTest
65@Presubmit
66public class WindowTracingTest {
Adrian Roos111aff92017-09-27 18:11:46 +020067
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070068 private static final byte[] MAGIC_HEADER = new byte[]{
69 0x9, 0x57, 0x49, 0x4e, 0x54, 0x52, 0x41, 0x43, 0x45,
Adrian Roos111aff92017-09-27 18:11:46 +020070 };
71
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070072 @Rule
73 public final DexmakerShareClassLoaderRule mDexmakerShareClassLoaderRule =
74 new DexmakerShareClassLoaderRule();
75
76 @Mock
Adrian Roos111aff92017-09-27 18:11:46 +020077 private WindowManagerService mWmMock;
Nataniel Borges48d9aa92019-02-01 14:45:55 -080078 @Mock
79 private Choreographer mChoreographer;
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070080 private WindowTracing mWindowTracing;
Adrian Roos111aff92017-09-27 18:11:46 +020081 private File mFile;
82
Adrian Roos111aff92017-09-27 18:11:46 +020083 @Before
84 public void setUp() throws Exception {
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070085 MockitoAnnotations.initMocks(this);
Adrian Roos111aff92017-09-27 18:11:46 +020086
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070087 final Context testContext = getInstrumentation().getContext();
88 mFile = testContext.getFileStreamPath("tracing_test.dat");
Adrian Roos111aff92017-09-27 18:11:46 +020089 mFile.delete();
90
Nataniel Borges48d9aa92019-02-01 14:45:55 -080091 mWindowTracing = new WindowTracing(mFile, mWmMock, mChoreographer,
Nataniel Borges7d37ce22019-02-05 10:07:02 -080092 new WindowManagerGlobalLock(), 1024);
Adrian Roos111aff92017-09-27 18:11:46 +020093 }
94
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070095 @After
96 public void tearDown() throws Exception {
97 mFile.delete();
98 }
99
Adrian Roos111aff92017-09-27 18:11:46 +0200100 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700101 public void isEnabled_returnsFalseByDefault() {
Adrian Roos111aff92017-09-27 18:11:46 +0200102 assertFalse(mWindowTracing.isEnabled());
103 }
104
105 @Test
Nataniel Borges7d37ce22019-02-05 10:07:02 -0800106 public void isEnabled_returnsTrueAfterStart() {
Adrian Roos111aff92017-09-27 18:11:46 +0200107 mWindowTracing.startTrace(mock(PrintWriter.class));
108 assertTrue(mWindowTracing.isEnabled());
109 }
110
111 @Test
Nataniel Borges7d37ce22019-02-05 10:07:02 -0800112 public void isEnabled_returnsFalseAfterStop() {
Adrian Roos111aff92017-09-27 18:11:46 +0200113 mWindowTracing.startTrace(mock(PrintWriter.class));
114 mWindowTracing.stopTrace(mock(PrintWriter.class));
115 assertFalse(mWindowTracing.isEnabled());
116 }
117
118 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700119 public void trace_discared_whenNotTracing() {
Nataniel Borges48d9aa92019-02-01 14:45:55 -0800120 mWindowTracing.logState("where");
Adrian Roos111aff92017-09-27 18:11:46 +0200121 verifyZeroInteractions(mWmMock);
122 }
123
124 @Test
125 public void trace_dumpsWindowManagerState_whenTracing() throws Exception {
126 mWindowTracing.startTrace(mock(PrintWriter.class));
Nataniel Borges48d9aa92019-02-01 14:45:55 -0800127 mWindowTracing.logState("where");
Nataniel Borges8a5106c2019-03-06 11:31:34 -0800128 verify(mWmMock, times(2)).writeToProtoLocked(any(), eq(WindowTraceLogLevel.TRIM));
Adrian Roos111aff92017-09-27 18:11:46 +0200129 }
130
131 @Test
132 public void traceFile_startsWithMagicHeader() throws Exception {
133 mWindowTracing.startTrace(mock(PrintWriter.class));
134 mWindowTracing.stopTrace(mock(PrintWriter.class));
135
Nataniel Borges7d37ce22019-02-05 10:07:02 -0800136 assertTrue("Trace file should exist", mFile.exists());
137
Adrian Roos111aff92017-09-27 18:11:46 +0200138 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 Roos111aff92017-09-27 18:11:46 +0200145 @Ignore("Figure out why this test is crashing when setting up mWmMock.")
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700146 @Test
Adrian Roos111aff92017-09-27 18:11:46 +0200147 public void tracing_endsUpInFile() throws Exception {
148 mWindowTracing.startTrace(mock(PrintWriter.class));
149
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700150 doAnswer(inv -> {
Adrian Roos111aff92017-09-27 18:11:46 +0200151 inv.<ProtoOutputStream>getArgument(0).write(
152 WindowManagerTraceProto.WHERE, "TEST_WM_PROTO");
153 return null;
154 }).when(mWmMock).writeToProtoLocked(any(), any());
Nataniel Borges48d9aa92019-02-01 14:45:55 -0800155 mWindowTracing.logState("TEST_WHERE");
Adrian Roos111aff92017-09-27 18:11:46 +0200156
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 Roos111aff92017-09-27 18:11:46 +0200170 /** Return true if {@code needle} appears anywhere in {@code haystack[0..length]} */
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700171 private static boolean containsBytes(byte[] haystack, int haystackLength, byte[] needle) {
172 Preconditions.checkArgument(haystackLength > 0);
Adrian Roos111aff92017-09-27 18:11:46 +0200173 Preconditions.checkArgument(needle.length > 0);
174
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700175 outer: for (int i = 0; i <= haystackLength - needle.length; i++) {
Adrian Roos111aff92017-09-27 18:11:46 +0200176 for (int j = 0; j < needle.length; j++) {
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700177 if (haystack[i + j] != needle[j]) {
Adrian Roos111aff92017-09-27 18:11:46 +0200178 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}