blob: 8358fdd18e0e1887e215685bd0e539fc8a5ffa82 [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;
23import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify;
24import static com.android.dx.mockito.inline.extended.ExtendedMockito.verifyZeroInteractions;
25
Adrian Roos111aff92017-09-27 18:11:46 +020026import static org.junit.Assert.assertArrayEquals;
27import static org.junit.Assert.assertEquals;
28import static org.junit.Assert.assertFalse;
29import static org.junit.Assert.assertTrue;
30import static org.mockito.ArgumentMatchers.any;
31import static org.mockito.ArgumentMatchers.eq;
Adrian Roos111aff92017-09-27 18:11:46 +020032
33import android.content.Context;
34import android.platform.test.annotations.Presubmit;
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070035import android.testing.DexmakerShareClassLoaderRule;
Adrian Roos111aff92017-09-27 18:11:46 +020036import android.util.proto.ProtoOutputStream;
Nataniel Borges48d9aa92019-02-01 14:45:55 -080037import android.view.Choreographer;
Adrian Roos111aff92017-09-27 18:11:46 +020038
Brett Chabota26eda92018-07-23 13:08:30 -070039import androidx.test.filters.SmallTest;
Brett Chabota26eda92018-07-23 13:08:30 -070040
Adrian Roos111aff92017-09-27 18:11:46 +020041import com.android.internal.util.Preconditions;
Adrian Roos111aff92017-09-27 18:11:46 +020042
43import org.junit.After;
44import org.junit.Before;
45import org.junit.Ignore;
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070046import org.junit.Rule;
Adrian Roos111aff92017-09-27 18:11:46 +020047import org.junit.Test;
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070048import org.mockito.Mock;
49import org.mockito.MockitoAnnotations;
Adrian Roos111aff92017-09-27 18:11:46 +020050
51import java.io.File;
52import java.io.FileInputStream;
53import java.io.InputStream;
54import java.io.PrintWriter;
55import java.nio.charset.StandardCharsets;
56
57/**
58 * Test class for {@link WindowTracing}.
59 *
60 * Build/Install/Run:
Yunfan Chend552c162019-02-08 16:51:28 +090061 * atest WmTests:WindowTracingTest
Adrian Roos111aff92017-09-27 18:11:46 +020062 */
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070063@SmallTest
64@Presubmit
65public class WindowTracingTest {
Adrian Roos111aff92017-09-27 18:11:46 +020066
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070067 private static final byte[] MAGIC_HEADER = new byte[]{
68 0x9, 0x57, 0x49, 0x4e, 0x54, 0x52, 0x41, 0x43, 0x45,
Adrian Roos111aff92017-09-27 18:11:46 +020069 };
70
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070071 @Rule
72 public final DexmakerShareClassLoaderRule mDexmakerShareClassLoaderRule =
73 new DexmakerShareClassLoaderRule();
74
75 @Mock
Adrian Roos111aff92017-09-27 18:11:46 +020076 private WindowManagerService mWmMock;
Nataniel Borges48d9aa92019-02-01 14:45:55 -080077 @Mock
78 private Choreographer mChoreographer;
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070079 private WindowTracing mWindowTracing;
Adrian Roos111aff92017-09-27 18:11:46 +020080 private File mFile;
81
Adrian Roos111aff92017-09-27 18:11:46 +020082 @Before
83 public void setUp() throws Exception {
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070084 MockitoAnnotations.initMocks(this);
Adrian Roos111aff92017-09-27 18:11:46 +020085
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070086 final Context testContext = getInstrumentation().getContext();
87 mFile = testContext.getFileStreamPath("tracing_test.dat");
Adrian Roos111aff92017-09-27 18:11:46 +020088 mFile.delete();
89
Nataniel Borges48d9aa92019-02-01 14:45:55 -080090 mWindowTracing = new WindowTracing(mFile, mWmMock, mChoreographer,
Nataniel Borges7d37ce22019-02-05 10:07:02 -080091 new WindowManagerGlobalLock(), 1024);
Adrian Roos111aff92017-09-27 18:11:46 +020092 }
93
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070094 @After
95 public void tearDown() throws Exception {
96 mFile.delete();
97 }
98
Adrian Roos111aff92017-09-27 18:11:46 +020099 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700100 public void isEnabled_returnsFalseByDefault() {
Adrian Roos111aff92017-09-27 18:11:46 +0200101 assertFalse(mWindowTracing.isEnabled());
102 }
103
104 @Test
Nataniel Borges7d37ce22019-02-05 10:07:02 -0800105 public void isEnabled_returnsTrueAfterStart() {
Adrian Roos111aff92017-09-27 18:11:46 +0200106 mWindowTracing.startTrace(mock(PrintWriter.class));
107 assertTrue(mWindowTracing.isEnabled());
108 }
109
110 @Test
Nataniel Borges7d37ce22019-02-05 10:07:02 -0800111 public void isEnabled_returnsFalseAfterStop() {
Adrian Roos111aff92017-09-27 18:11:46 +0200112 mWindowTracing.startTrace(mock(PrintWriter.class));
113 mWindowTracing.stopTrace(mock(PrintWriter.class));
114 assertFalse(mWindowTracing.isEnabled());
115 }
116
117 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700118 public void trace_discared_whenNotTracing() {
Nataniel Borges48d9aa92019-02-01 14:45:55 -0800119 mWindowTracing.logState("where");
Adrian Roos111aff92017-09-27 18:11:46 +0200120 verifyZeroInteractions(mWmMock);
121 }
122
123 @Test
124 public void trace_dumpsWindowManagerState_whenTracing() throws Exception {
125 mWindowTracing.startTrace(mock(PrintWriter.class));
Nataniel Borges48d9aa92019-02-01 14:45:55 -0800126 mWindowTracing.logState("where");
Nataniel Borges023ecb52019-01-16 14:15:43 -0800127 verify(mWmMock).writeToProtoLocked(any(), eq(WindowTraceLogLevel.TRIM));
Adrian Roos111aff92017-09-27 18:11:46 +0200128 }
129
130 @Test
131 public void traceFile_startsWithMagicHeader() throws Exception {
132 mWindowTracing.startTrace(mock(PrintWriter.class));
133 mWindowTracing.stopTrace(mock(PrintWriter.class));
134
Nataniel Borges7d37ce22019-02-05 10:07:02 -0800135 assertTrue("Trace file should exist", mFile.exists());
136
Adrian Roos111aff92017-09-27 18:11:46 +0200137 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 Roos111aff92017-09-27 18:11:46 +0200144 @Ignore("Figure out why this test is crashing when setting up mWmMock.")
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700145 @Test
Adrian Roos111aff92017-09-27 18:11:46 +0200146 public void tracing_endsUpInFile() throws Exception {
147 mWindowTracing.startTrace(mock(PrintWriter.class));
148
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700149 doAnswer(inv -> {
Adrian Roos111aff92017-09-27 18:11:46 +0200150 inv.<ProtoOutputStream>getArgument(0).write(
151 WindowManagerTraceProto.WHERE, "TEST_WM_PROTO");
152 return null;
153 }).when(mWmMock).writeToProtoLocked(any(), any());
Nataniel Borges48d9aa92019-02-01 14:45:55 -0800154 mWindowTracing.logState("TEST_WHERE");
Adrian Roos111aff92017-09-27 18:11:46 +0200155
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 Roos111aff92017-09-27 18:11:46 +0200169 /** Return true if {@code needle} appears anywhere in {@code haystack[0..length]} */
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700170 private static boolean containsBytes(byte[] haystack, int haystackLength, byte[] needle) {
171 Preconditions.checkArgument(haystackLength > 0);
Adrian Roos111aff92017-09-27 18:11:46 +0200172 Preconditions.checkArgument(needle.length > 0);
173
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700174 outer: for (int i = 0; i <= haystackLength - needle.length; i++) {
Adrian Roos111aff92017-09-27 18:11:46 +0200175 for (int j = 0; j < needle.length; j++) {
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700176 if (haystack[i + j] != needle[j]) {
Adrian Roos111aff92017-09-27 18:11:46 +0200177 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}