• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.test.tracing.coroutines
18 
19 import android.platform.test.annotations.DisableFlags
20 import com.android.app.tracing.coroutines.createCoroutineTracingContext
21 import com.android.app.tracing.coroutines.traceCoroutine
22 import com.android.app.tracing.coroutines.traceThreadLocal
23 import com.android.systemui.Flags.FLAG_COROUTINE_TRACING
24 import com.android.test.tracing.coroutines.util.FakeTraceState
25 import kotlin.coroutines.CoroutineContext
26 import kotlin.coroutines.EmptyCoroutineContext
27 import kotlinx.coroutines.withContext
28 import org.junit.Assert.assertFalse
29 import org.junit.Assert.assertNull
30 import org.junit.Assert.fail
31 import org.junit.Test
32 
33 @DisableFlags(FLAG_COROUTINE_TRACING)
34 class FlagDisabledTest : TestBase() {
<lambda>null35     override val extraContext: CoroutineContext by lazy { EmptyCoroutineContext }
36 
37     @Test
<lambda>null38     fun tracingDisabledWhenFlagIsOff() = runTest {
39         assertFalse(com.android.systemui.Flags.coroutineTracing())
40         assertNull(traceThreadLocal.get())
41         withContext(createCoroutineTracingContext(testMode = true)) {
42             assertNull(traceThreadLocal.get())
43             traceCoroutine("hello") { // should not crash
44                 assertNull(traceThreadLocal.get())
45             }
46 
47             // Change Trace.isEnabled() to false so that the lazy-String is not called for async
48             // tracing, which would be expected even when coroutine tracing is disabled.
49             FakeTraceState.isTracingEnabled = false
50 
51             // Verify that the lazy-String is not called when tracing is disabled and feature flag
52             // is off
53             traceCoroutine({
54                 fail("Lazy string should not be called when FLAG_COROUTINE_TRACING is disabled")
55                 "error"
56             }) {
57                 assertNull(traceThreadLocal.get())
58             }
59         }
60     }
61 }
62