1 /*
2  * Copyright (C) 2022 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.tradefed.util;
18 
19 import static org.junit.Assert.assertNotNull;
20 import static org.junit.Assert.assertTrue;
21 
22 import com.android.tradefed.device.ITestDevice;
23 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
24 import com.android.tradefed.testtype.IDeviceTest;
25 
26 import org.junit.After;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 
31 import java.io.File;
32 import java.io.IOException;
33 import java.util.ArrayList;
34 import java.util.List;
35 
36 @RunWith(DeviceJUnit4ClassRunner.class)
37 public class PerfettoTraceRecorderFuncTest implements IDeviceTest {
38 
39     private ITestDevice mTestDevice;
40     private PerfettoTraceRecorder mPerfettoTraceRecorder;
41     private List<File> mTraceFiles;
42 
43     @Override
setDevice(ITestDevice device)44     public void setDevice(ITestDevice device) {
45         mTestDevice = device;
46     }
47 
48     @Override
getDevice()49     public ITestDevice getDevice() {
50         return mTestDevice;
51     }
52 
53     @Before
setup()54     public void setup() throws IOException {
55         mPerfettoTraceRecorder = new PerfettoTraceRecorder();
56         mTraceFiles = new ArrayList<>();
57     }
58 
59     @After
teardown()60     public void teardown() {
61         // delete trace files
62         for (File tracefile : mTraceFiles) {
63             tracefile.delete();
64         }
65     }
66 
67     @Test
testPerfettoTraceRecorded()68     public void testPerfettoTraceRecorded() throws IOException {
69         mPerfettoTraceRecorder.startTrace(getDevice(), null);
70         RunUtil.getDefault().sleep(5000); // collect trace for five seconds
71         File traceFile = mPerfettoTraceRecorder.stopTrace(getDevice());
72 
73         // Check if the perfetto trace file was recorded and not empty
74         assertNotNull(traceFile);
75         assertTrue(traceFile.exists());
76         mTraceFiles.add(traceFile);
77         assertTrue(traceFile.length() > 0);
78     }
79 }
80