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.compose.runtime 18 19 import android.view.View 20 import androidx.test.ext.junit.runners.AndroidJUnit4 21 import androidx.test.filters.MediumTest 22 import junit.framework.TestCase 23 import org.junit.Rule 24 import org.junit.Test 25 import org.junit.runner.RunWith 26 27 @MediumTest 28 @RunWith(AndroidJUnit4::class) 29 class DisposeTests : BaseComposeTest() { 30 @get:Rule override val activityRule = makeTestActivityRule() 31 32 private val NeverEqualObject = 33 object { equalsnull34 override fun equals(other: Any?): Boolean { 35 return false 36 } 37 } 38 39 @Test testDisposeCompositionnull40 fun testDisposeComposition() { 41 val log = mutableListOf<String>() 42 43 lateinit var recomposeScope: RecomposeScope 44 val composable = 45 @Composable { 46 recomposeScope = currentRecomposeScope 47 DisposableEffect(NeverEqualObject) { 48 log.add("onCommit") 49 onDispose { log.add("onCommitDispose") } 50 } 51 DisposableEffect(Unit) { 52 log.add("onActive") 53 onDispose { log.add("onActiveDispose") } 54 } 55 } 56 57 fun assertLog(expected: String, block: () -> Unit) { 58 log.clear() 59 block() 60 TestCase.assertEquals(expected, log.joinToString()) 61 } 62 63 assertLog("onCommit, onActive") { 64 activity.show(composable) 65 activity.waitForAFrame() 66 } 67 68 assertLog("onCommitDispose, onCommit") { 69 recomposeScope.invalidate() 70 activity.waitForAFrame() 71 } 72 73 assertLog("onActiveDispose, onCommitDispose") { 74 activity.uiThread { activity.setContentView(View(activity)) } 75 activity.waitForAFrame() 76 } 77 78 assertLog("onCommit, onActive") { 79 activity.show(composable) 80 activity.waitForAFrame() 81 } 82 } 83 } 84