[go: nahoru, domu]

blob: dfb2bab25a9c122021ae761872cda2afa75f3e00 [file] [log] [blame]
Andrey Kulikov38320372020-05-07 19:55:05 +01001/*
2 * Copyright 2020 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
17package androidx.ui.core.lifecycleowner
18
19import android.widget.FrameLayout
20import androidx.activity.ComponentActivity
21import androidx.compose.Recomposer
22import androidx.lifecycle.LifecycleOwner
23import androidx.test.filters.SmallTest
24import androidx.ui.core.LifecycleOwnerAmbient
25import androidx.ui.core.setContent
26import org.junit.Assert.assertEquals
27import org.junit.Assert.assertTrue
28import org.junit.Before
29import org.junit.Rule
30import org.junit.Test
31import org.junit.runner.RunWith
32import org.junit.runners.JUnit4
33import java.util.concurrent.CountDownLatch
34import java.util.concurrent.TimeUnit
35
36@SmallTest
37@RunWith(JUnit4::class)
38class LifecycleOwnerInComponentActivityTest {
39 @Suppress("DEPRECATION")
40 @get:Rule
41 val activityTestRule = androidx.test.rule.ActivityTestRule<ComponentActivity>(
42 ComponentActivity::class.java
43 )
44 private lateinit var activity: ComponentActivity
45
46 @Before
47 fun setup() {
48 activity = activityTestRule.activity
49 }
50
51 @Test
52 fun lifecycleOwnerIsAvailable() {
53 val latch = CountDownLatch(1)
54 var owner: LifecycleOwner? = null
55
56 activityTestRule.runOnUiThread {
57 activity.setContent {
58 owner = LifecycleOwnerAmbient.current
59 latch.countDown()
60 }
61 }
62
63 assertTrue(latch.await(1, TimeUnit.SECONDS))
64 assertEquals(activity, owner)
65 }
66
67 @Test
68 fun lifecycleOwnerIsAvailableWhenComposedIntoViewGroup() {
69 val latch = CountDownLatch(1)
70 var owner: LifecycleOwner? = null
71
72 activityTestRule.runOnUiThread {
73 val view = FrameLayout(activity)
74 activity.setContentView(view)
75 view.setContent(Recomposer.current()) {
76 owner = LifecycleOwnerAmbient.current
77 latch.countDown()
78 }
79 }
80
81 assertTrue(latch.await(1, TimeUnit.SECONDS))
82 assertEquals(activity, owner)
83 }
84}