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 
17 package com.android.server.wm;
18 
19 import static org.mockito.ArgumentMatchers.anyInt;
20 import static org.mockito.ArgumentMatchers.eq;
21 import static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.verify;
23 
24 import android.platform.test.annotations.Presubmit;
25 
26 import androidx.test.filters.SmallTest;
27 
28 import com.android.internal.protolog.ProtoLogGroup;
29 import com.android.internal.protolog.ProtoLogImpl;
30 import com.android.internal.protolog.common.ProtoLog;
31 
32 import org.junit.After;
33 import org.junit.Ignore;
34 import org.junit.Test;
35 
36 /**
37  * Check if the ProtoLogTools is used to process the WindowManager source code.
38  */
39 @SmallTest
40 @Presubmit
41 public class ProtoLogIntegrationTest {
42     @After
tearDown()43     public void tearDown() {
44         ProtoLogImpl.setSingleInstance(null);
45     }
46 
47     @Ignore("b/163095037")
48     @Test
testProtoLogToolIntegration()49     public void testProtoLogToolIntegration() {
50         ProtoLogImpl mockedProtoLog = mock(ProtoLogImpl.class);
51         runWith(mockedProtoLog, this::testProtoLog);
52         verify(mockedProtoLog).log(eq(ProtoLogImpl.LogLevel.ERROR), eq(ProtoLogGroup.TEST_GROUP),
53                 anyInt(), eq(0b0010010111),
54                 eq(com.android.internal.protolog.ProtoLogGroup.TEST_GROUP.isLogToLogcat()
55                         ? "Test completed successfully: %b %d %x %f %% %s"
56                         : null),
57                 eq(new Object[]{true, 1L, 2L, 0.3, "ok"}));
58     }
59 
testProtoLog()60     private void testProtoLog() {
61         ProtoLog.e(ProtoLogGroup.TEST_GROUP,
62                 "Test completed successfully: %b %d %x %f %% %s",
63                 true, 1, 2, 0.3, "ok");
64     }
65 
66     /**
67      * Starts protolog for the duration of {@code runnable}, with a ProtoLogImpl instance installed.
68      */
runWith(ProtoLogImpl mockInstance, Runnable runnable)69     private void runWith(ProtoLogImpl mockInstance, Runnable runnable) {
70         ProtoLogImpl original = ProtoLogImpl.getSingleInstance();
71         original.startProtoLog(null);
72         try {
73             ProtoLogImpl.setSingleInstance(mockInstance);
74             try {
75                 runnable.run();
76             } finally {
77                 ProtoLogImpl.setSingleInstance(original);
78             }
79         } finally {
80             original.stopProtoLog(null, false);
81         }
82     }
83 }
84