1 /*
2  * Copyright (C) 2020 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 android.graphics.gpuprofiling.cts;
18 
19 import static org.junit.Assume.assumeFalse;
20 
21 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
22 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
23 import com.android.tradefed.util.CommandResult;
24 import com.android.tradefed.util.CommandStatus;
25 
26 import perfetto.protos.PerfettoConfig.TracingServiceState;
27 import perfetto.protos.PerfettoConfig.DataSourceDescriptor;
28 
29 import java.util.Base64;
30 
31 import org.junit.Assert;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 
36 /**
37  * Test that ensures the device registers android.surfaceflinger.frame perfetto producer
38  */
39 @RunWith(DeviceJUnit4ClassRunner.class)
40 public class CtsFrameTracerDataSourceTest extends BaseHostJUnit4Test {
41     public static final String TAG = "GpuProfilingDataDeviceActivity";
42     private static final String FRAME_TRACER_SOURCE_NAME = "android.surfaceflinger.frame";
43 
44     // Copied from PackageManager
45     private static final String FEATURE_AUTOMOTIVE = "android.hardware.type.automotive";
46     private static final String FEATURE_EMBEDDED = "android.hardware.type.embedded";
47     private static final String FEATURE_LEANBACK_ONLY = "android.software.leanback_only";
48     private static final String FEATURE_WATCH = "android.hardware.type.watch";
49     private static final String FEATURE_TELEVISION = "android.hardware.type.television";
50 
bypassTestForFeatures(String... features)51     private void bypassTestForFeatures(String... features) throws Exception {
52         for (String feature : features) {
53             assumeFalse(hasDeviceFeature(feature));
54         }
55     }
56 
57     @Before
setUp()58     public void setUp() throws Exception {
59         // We do not care about non-handheld devices
60         bypassTestForFeatures(FEATURE_AUTOMOTIVE, FEATURE_EMBEDDED, FEATURE_LEANBACK_ONLY,
61                 FEATURE_WATCH, FEATURE_TELEVISION);
62     }
63 
64     @Test
testFrameTracerProducerAvailable()65     public void testFrameTracerProducerAvailable() throws Exception {
66         CommandResult queryResult = getDevice().executeShellV2Command("perfetto --query-raw | base64");
67         Assert.assertEquals(CommandStatus.SUCCESS, queryResult.getStatus());
68         byte[] decodedBytes = Base64.getMimeDecoder().decode(queryResult.getStdout());
69         TracingServiceState state = TracingServiceState.parseFrom(decodedBytes);
70         int dataSourcesCount = state.getDataSourcesCount();
71         Assert.assertTrue("No sources found", dataSourcesCount > 0);
72         boolean sourceFound = false;
73         for (int i = 0; i < dataSourcesCount; i++) {
74             DataSourceDescriptor descriptor = state.getDataSources(i).getDsDescriptor();
75             if (descriptor != null) {
76                 if (descriptor.getName().equals(FRAME_TRACER_SOURCE_NAME)) {
77                     sourceFound = true;
78                     break;
79                 }
80             }
81         }
82         Assert.assertTrue("Producer " + FRAME_TRACER_SOURCE_NAME + " not found", sourceFound);
83     }
84 }
85