Andrey Kulikov | 3832037 | 2020-05-07 19:55:05 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package androidx.ui.core.lifecycleowner |
| 18 | |
| 19 | import android.widget.FrameLayout |
| 20 | import androidx.activity.ComponentActivity |
| 21 | import androidx.compose.Recomposer |
| 22 | import androidx.lifecycle.LifecycleOwner |
| 23 | import androidx.test.filters.SmallTest |
| 24 | import androidx.ui.core.LifecycleOwnerAmbient |
| 25 | import androidx.ui.core.setContent |
| 26 | import org.junit.Assert.assertEquals |
| 27 | import org.junit.Assert.assertTrue |
| 28 | import org.junit.Before |
| 29 | import org.junit.Rule |
| 30 | import org.junit.Test |
| 31 | import org.junit.runner.RunWith |
| 32 | import org.junit.runners.JUnit4 |
| 33 | import java.util.concurrent.CountDownLatch |
| 34 | import java.util.concurrent.TimeUnit |
| 35 | |
| 36 | @SmallTest |
| 37 | @RunWith(JUnit4::class) |
| 38 | class 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 | } |