Skip to content

Commit

Permalink
Add test for DisposableComposePanel state
Browse files Browse the repository at this point in the history
  • Loading branch information
Walingar committed Jul 4, 2023
1 parent 9cce9e1 commit d120ba2
Showing 1 changed file with 73 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package androidx.compose.ui.awt

import androidx.compose.runtime.*
import java.awt.GraphicsEnvironment
import javax.swing.JFrame
import kotlin.test.assertEquals
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import org.jetbrains.skiko.MainUIDispatcher
import org.junit.Assume
import org.junit.Test

class DisposableComposePanelTest {
@OptIn(ExperimentalComposeApi::class)
@Test
fun `compose state shouldn't reset on panel remove and add`() {
Assume.assumeFalse(GraphicsEnvironment.getLocalGraphicsEnvironment().isHeadlessInstance)

runBlocking(MainUIDispatcher) {
var initialStateCounter = 0
val composePanel = DisposableComposePanel()
composePanel.setContent {
var state by remember { mutableStateOf(0) }

LaunchedEffect(state) {
if (state == 0) {
state++
initialStateCounter++
}
}
}

val frame = JFrame()
try {
frame.contentPane.add(composePanel)
frame.isUndecorated = true

frame.pack()

frame.isVisible = true
delay(1000)
assertEquals(1, initialStateCounter)

frame.contentPane.remove(composePanel)
delay(1000)
assertEquals(1, initialStateCounter)

frame.contentPane.add(composePanel)
delay(1000)
assertEquals(1, initialStateCounter)
} finally {
composePanel.dispose()
frame.dispose()
}
}
}
}

0 comments on commit d120ba2

Please sign in to comment.