1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 
15 package com.android.systemui.tracing
16 
17 import android.os.Handler
18 import android.os.Looper
19 import android.os.Trace.TRACE_TAG_APP
20 import android.util.Log
21 import androidx.test.ext.junit.runners.AndroidJUnit4
22 import androidx.test.filters.SmallTest
23 import com.android.app.tracing.TraceUtils.traceRunnable
24 import com.android.app.tracing.namedRunnable
25 import com.android.app.tracing.traceSection
26 import com.android.systemui.SysuiTestCase
27 import org.junit.After
28 import org.junit.Assert.assertThrows
29 import org.junit.Before
30 import org.junit.Test
31 import org.junit.runner.RunWith
32 
33 @RunWith(AndroidJUnit4::class)
34 @SmallTest
35 class TraceUtilsTest : SysuiTestCase() {
36 
37     companion object {
38         private const val TAG = "TraceUtilsTest"
39         private const val TEST_FAIL_TIMEOUT = 5000L
40 
41         // A string that is 128 characters long
42         private const val SECTION_NAME_THATS_TOO_LONG =
43             "123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_" +
44                 "123456789_123456789_123456789_123456789_12345678"
45     }
46 
47     @Before
setUpnull48     fun setUp() {
49         // Enable tracing via atrace in order to see the expected IllegalArgumentException. Trace
50         // sections won't run if tracing is disabled.
51         uiDevice.executeShellCommand("atrace --async_start -a com.android.*")
52     }
53 
54     @After
tearDownnull55     fun tearDown() {
56         uiDevice.executeShellCommand("atrace --async_stop")
57     }
58 
59     @Test
testLongTraceSection_throws_whenUsingPublicAPInull60     fun testLongTraceSection_throws_whenUsingPublicAPI() {
61         // Expects: "java.lang.IllegalArgumentException: sectionName is too long"
62         assertThrows(IllegalArgumentException::class.java) {
63             android.os.Trace.beginSection(SECTION_NAME_THATS_TOO_LONG)
64         }
65     }
66 
67     @Test
testLongTraceSection_doesNotThrow_whenUsingPrivateAPInull68     fun testLongTraceSection_doesNotThrow_whenUsingPrivateAPI() {
69         android.os.Trace.traceBegin(TRACE_TAG_APP, SECTION_NAME_THATS_TOO_LONG)
70     }
71 
72     @Test
testLongTraceSection_doesNotThrow_whenUsingAndroidXnull73     fun testLongTraceSection_doesNotThrow_whenUsingAndroidX() {
74         androidx.tracing.Trace.beginSection(SECTION_NAME_THATS_TOO_LONG)
75     }
76 
77     @Test
testLongTraceSection_doesNotThrow_whenUsingHelpernull78     fun testLongTraceSection_doesNotThrow_whenUsingHelper() {
79         traceSection(SECTION_NAME_THATS_TOO_LONG) {
80             Log.v(TAG, "com.android.app.tracing.traceSection() block.")
81         }
82     }
83 
84     @Test
testLongTraceSection_doesNotThrow_whenUsedAsTraceNameSuppliernull85     fun testLongTraceSection_doesNotThrow_whenUsedAsTraceNameSupplier() {
86         Handler(Looper.getMainLooper())
87             .runWithScissors(
88                 namedRunnable(SECTION_NAME_THATS_TOO_LONG) { Log.v(TAG, "namedRunnable() block.") },
89                 TEST_FAIL_TIMEOUT
90             )
91     }
92 
93     @Test
testLongTraceSection_doesNotThrow_whenUsingTraceRunnablenull94     fun testLongTraceSection_doesNotThrow_whenUsingTraceRunnable() {
95         traceRunnable(SECTION_NAME_THATS_TOO_LONG) { Log.v(TAG, "traceRunnable() block.") }.run()
96     }
97 }
98