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.systemui.compose 18 19 import android.content.Context 20 import android.testing.AndroidTestingRunner 21 import android.testing.ViewUtils 22 import android.widget.FrameLayout 23 import androidx.compose.ui.platform.ComposeView 24 import androidx.test.filters.SmallTest 25 import com.android.systemui.SysuiTestCase 26 import com.google.common.truth.Truth.assertThat 27 import org.junit.Test 28 import org.junit.runner.RunWith 29 30 @SmallTest 31 @RunWith(AndroidTestingRunner::class) 32 class ComposeInitializerTest : SysuiTestCase() { 33 @Test 34 fun testCanAddComposeViewInInitializedWindow() { 35 if (!ComposeFacade.isComposeAvailable()) { 36 return 37 } 38 39 val root = TestWindowRoot(context) 40 try { 41 runOnMainThreadAndWaitForIdleSync { ViewUtils.attachView(root) } 42 assertThat(root.isAttachedToWindow).isTrue() 43 44 runOnMainThreadAndWaitForIdleSync { root.addView(ComposeView(context)) } 45 } finally { 46 runOnMainThreadAndWaitForIdleSync { ViewUtils.detachView(root) } 47 } 48 } 49 50 private fun runOnMainThreadAndWaitForIdleSync(f: () -> Unit) { 51 mContext.mainExecutor.execute(f) 52 waitForIdleSync() 53 } 54 55 class TestWindowRoot(context: Context) : FrameLayout(context) { 56 override fun onAttachedToWindow() { 57 super.onAttachedToWindow() 58 ComposeFacade.composeInitializer().onAttachedToWindow(this) 59 } 60 61 override fun onDetachedFromWindow() { 62 super.onDetachedFromWindow() 63 ComposeFacade.composeInitializer().onDetachedFromWindow(this) 64 } 65 } 66 } 67