Skip to content

Commit

Permalink
Add more tests for SkiaSwingLayer
Browse files Browse the repository at this point in the history
  • Loading branch information
Walingar committed Jun 30, 2023
1 parent d7df60c commit a9a040e
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 26 deletions.
184 changes: 158 additions & 26 deletions skiko/src/awtTest/kotlin/org/jetbrains/skiko/SkiaSwingLayerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,10 @@ import org.jetbrains.skiko.swing.SkiaSwingLayer
import org.jetbrains.skiko.util.ScreenshotTestRule
import org.jetbrains.skiko.util.uiTest
import org.junit.Rule
import java.awt.Color
import java.awt.Dimension
import java.awt.Graphics
import java.awt.Graphics2D
import java.awt.RenderingHints
import javax.swing.JComponent
import javax.swing.JFrame
import javax.swing.JLayeredPane
import javax.swing.WindowConstants
import java.awt.*
import javax.swing.*
import javax.swing.JLayeredPane.DEFAULT_LAYER
import javax.swing.JLayeredPane.POPUP_LAYER
import kotlin.test.Test

@OptIn(ExperimentalSkikoApi::class)
Expand All @@ -32,13 +27,7 @@ class SkiaSwingLayerTest {
setBounds(0, 0, 200, 200)
}

val window = object : JFrame() {
override fun dispose() {
skiaLayer.dispose()
super.dispose()
}
}
try {
singleWindowTest(skiaLayer) { contentPane ->
val popup = object : JComponent() {
init {
isOpaque = false
Expand All @@ -48,10 +37,9 @@ class SkiaSwingLayerTest {
override fun paintComponent(g: Graphics?) {
val scratchGraphics = g?.create() as? Graphics2D ?: return
try {
scratchGraphics.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON
);
scratchGraphics.color = Color(0, 0, 0, 50)
scratchGraphics.fillRect(0, 0, 50, 50)

scratchGraphics.color = Color.GREEN
scratchGraphics.fillRoundRect(5, 5, 40, 40, 16, 16)
} finally {
Expand All @@ -61,17 +49,157 @@ class SkiaSwingLayerTest {
}

val layeredPane = JLayeredPane()
layeredPane.add(popup, Integer.valueOf(1))
layeredPane.add(skiaLayer, Integer.valueOf(2))
layeredPane.add(skiaLayer)
layeredPane.add(popup)

layeredPane.setLayer(skiaLayer, DEFAULT_LAYER)
layeredPane.setLayer(popup, POPUP_LAYER)

contentPane.add(layeredPane)

delay(1000)
checkScreenshot()
}
}

@Test
fun `multiple layers`() = uiTest {
val skikoView1 = fillSkikoView(color = Color.CYAN)
val skiaLayer1 = SkiaSwingLayer(skikoView1, SkiaLayerProperties().copy(renderApi = renderApi)).apply {
setBounds(0, 0, 200, 200)
}
val skikoView2 = fillSkikoView(color = Color.GREEN)
val skiaLayer2 = SkiaSwingLayer(skikoView2, SkiaLayerProperties().copy(renderApi = renderApi)).apply {
setBounds(50, 50, 200, 200)
}

val skikoView3 = fillSkikoView(color = Color.RED)
val skiaLayer3 = SkiaSwingLayer(skikoView3, SkiaLayerProperties().copy(renderApi = renderApi)).apply {
setBounds(100, 100, 25, 25)
}

singleWindowTest(
dispose = {
skiaLayer1.dispose()
skiaLayer2.dispose()
skiaLayer3.dispose()
}
) { contentPane ->
val layeredPane = JLayeredPane()

layeredPane.add(skiaLayer1)
layeredPane.add(skiaLayer2)
layeredPane.add(skiaLayer3)

layeredPane.setLayer(skiaLayer1, 0)
layeredPane.setLayer(skiaLayer2, 1)
layeredPane.setLayer(skiaLayer3, 2)

contentPane.add(layeredPane)

delay(1000)
checkScreenshot()
}
}

@Test
fun `layer resize`() = uiTest {
val skikoView = fillSkikoView()
val skiaLayer = SkiaSwingLayer(skikoView, SkiaLayerProperties().copy(renderApi = renderApi))

singleWindowTest(skiaLayer) { contentPane ->
val splitPane = JSplitPane()
splitPane.leftComponent = skiaLayer
splitPane.rightComponent = JPanel()

splitPane.dividerLocation = 50

contentPane.add(splitPane)

delay(1000)
checkScreenshot("position1")

splitPane.dividerLocation = 100
delay(1000)
checkScreenshot("position2")
}
}

@Test
fun `overlapped skia layer`() = uiTest {
val skikoView = object : SkikoView {
override fun onRender(canvas: Canvas, width: Int, height: Int, nanoTime: Long) {
val rect = Rect(0f, 0f, width.toFloat() - 10f, height.toFloat() - 10f)
canvas.drawRect(rect, Paint().apply {
this.color = Color.CYAN.rgb
})

canvas.drawRectShadow(
rect,
dx = 3f, dy = 3f,
blur = 0.5f,
Color(0, 0, 0, 50).rgb
)
}
}
val skiaLayer = SkiaSwingLayer(skikoView, SkiaLayerProperties().copy(renderApi = renderApi)).apply {
setBounds(50, 50, 50, 50)
}

singleWindowTest(skiaLayer) { contentPane ->
val panel = JPanel().apply {
background = Color.GREEN
setBounds(0, 0, 200, 200)
}

val layeredPane = JLayeredPane()
layeredPane.add(panel)
layeredPane.add(skiaLayer)

layeredPane.setLayer(panel, DEFAULT_LAYER)
layeredPane.setLayer(skiaLayer, POPUP_LAYER)

contentPane.add(layeredPane)

delay(1000)
checkScreenshot()
}
}

private inline fun singleWindowTest(
layer: SkiaSwingLayer,
block: SingleWindowTestScope.(contentPane: Container) -> Unit
) {
singleWindowTest(
dispose = {
layer.dispose()
},
block
)
}

private inline fun singleWindowTest(
crossinline dispose: () -> Unit,
block: SingleWindowTestScope.(contentPane: Container) -> Unit
) {
val window = object : JFrame() {
override fun dispose() {
dispose()
super.dispose()
}
}

try {
window.isUndecorated = true
window.size = Dimension(400, 400)
window.defaultCloseOperation = WindowConstants.DISPOSE_ON_CLOSE
window.contentPane.add(layeredPane)
window.isVisible = true

delay(1000)
screenshots.assert(window.bounds)
val scope = object : SingleWindowTestScope {
override fun checkScreenshot(id: String) {
screenshots.assert(window.bounds, id)
}
}
scope.block(window.contentPane)
} finally {
window.close()
}
Expand All @@ -84,4 +212,8 @@ class SkiaSwingLayerTest {
})
}
}

private interface SingleWindowTestScope {
fun checkScreenshot(id: String = "")
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a9a040e

Please sign in to comment.