From 4aa731c7341bb80ead16c7193bb8beaa9edaf3e7 Mon Sep 17 00:00:00 2001 From: Peter Kalauskas Date: Tue, 9 May 2023 15:36:10 -0700 Subject: [PATCH] New TraceUtilsTest New test for asserting that TraceUtils does not throw an IllegalArgumentException when passed a long section name. Also, demonstrate how the public API android.os.Trace.beginSection will throw an exception if the section name is more than 127 characters long Also, move TraceUtils to a directory that matches its declared package name. Test: atest TraceUtilsTest Bug: 280850299 Bug: 279715262 Bug: 267482189 Bug: 279784509 Change-Id: I827eb710742d4fabedd4fbb37af64e9ecee118d0 --- .../systemui/{utils => util}/TraceUtils.kt | 0 .../android/systemui/util/TraceUtilsTest.kt | 100 ++++++++++++++++++ 2 files changed, 100 insertions(+) rename packages/SystemUI/shared/src/com/android/systemui/{utils => util}/TraceUtils.kt (100%) create mode 100644 packages/SystemUI/tests/src/com/android/systemui/util/TraceUtilsTest.kt diff --git a/packages/SystemUI/shared/src/com/android/systemui/utils/TraceUtils.kt b/packages/SystemUI/shared/src/com/android/systemui/util/TraceUtils.kt similarity index 100% rename from packages/SystemUI/shared/src/com/android/systemui/utils/TraceUtils.kt rename to packages/SystemUI/shared/src/com/android/systemui/util/TraceUtils.kt diff --git a/packages/SystemUI/tests/src/com/android/systemui/util/TraceUtilsTest.kt b/packages/SystemUI/tests/src/com/android/systemui/util/TraceUtilsTest.kt new file mode 100644 index 0000000000000..6aad0ad46c2f8 --- /dev/null +++ b/packages/SystemUI/tests/src/com/android/systemui/util/TraceUtilsTest.kt @@ -0,0 +1,100 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the + * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package com.android.systemui.util + +import android.os.Handler +import android.os.Looper +import android.os.Trace.TRACE_TAG_APP +import android.testing.AndroidTestingRunner +import android.util.Log +import androidx.test.filters.SmallTest +import com.android.systemui.SysuiTestCase +import org.junit.After +import org.junit.Assert.assertThrows +import org.junit.Before +import org.junit.Ignore +import org.junit.Test +import org.junit.runner.RunWith + +@RunWith(AndroidTestingRunner::class) +@SmallTest +class TraceUtilsTest : SysuiTestCase() { + + companion object { + private const val TAG = "TraceUtilsTest" + private const val TEST_FAIL_TIMEOUT = 5000L + + // A string that is 128 characters long + private const val SECTION_NAME_THATS_TOO_LONG = + "123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_" + + "123456789_123456789_123456789_123456789_12345678" + } + + @Before + fun setUp() { + // Enable tracing via atrace in order to see the expected IllegalArgumentException. Trace + // sections won't run if tracing is disabled. + uiDevice.executeShellCommand("atrace --async_start -a com.android.*") + } + + @After + fun tearDown() { + uiDevice.executeShellCommand("atrace --async_stop") + } + + @Test + fun testLongTraceSection_throws_whenUsingPublicAPI() { + // Expects: "java.lang.IllegalArgumentException: sectionName is too long" + assertThrows(IllegalArgumentException::class.java) { + android.os.Trace.beginSection(SECTION_NAME_THATS_TOO_LONG) + } + } + + @Test + fun testLongTraceSection_doesNotThrow_whenUsingPrivateAPI() { + android.os.Trace.traceBegin(TRACE_TAG_APP, SECTION_NAME_THATS_TOO_LONG) + } + + @Test + @Ignore("b/267482189 - Enable once androidx.tracing >= 1.2.0-beta04") + fun testLongTraceSection_doesNotThrow_whenUsingAndroidX() { + androidx.tracing.Trace.beginSection(SECTION_NAME_THATS_TOO_LONG) + } + + @Test + fun testLongTraceSection_doesNotThrow_whenUsingHelper() { + traceSection(SECTION_NAME_THATS_TOO_LONG) { + Log.v(TAG, "com.android.systemui.util.traceSection() block.") + } + } + + @Test + fun testLongTraceSection_doesNotThrow_whenUsedAsTraceNameSupplier() { + Handler(Looper.getMainLooper()) + .runWithScissors( + TraceUtils.namedRunnable(SECTION_NAME_THATS_TOO_LONG) { + Log.v(TAG, "TraceUtils.namedRunnable() block.") + }, + TEST_FAIL_TIMEOUT + ) + } + + @Test + fun testLongTraceSection_doesNotThrow_whenUsingTraceRunnable() { + TraceUtils.traceRunnable(SECTION_NAME_THATS_TOO_LONG) { + Log.v(TAG, "TraceUtils.traceRunnable() block.") + } + } +}