Browse Source

Add a unit test to the imageviewer example. (#3527)

* Add a unit test to the imageviewer example.

* Fix android build of imageviewer
pull/3529/head
Alexander Maryanovsky 1 year ago committed by GitHub
parent
commit
8c724096fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      examples/imageviewer/androidApp/build.gradle.kts
  2. 2
      examples/imageviewer/gradle.properties
  3. 13
      examples/imageviewer/shared/build.gradle.kts
  4. 3
      examples/imageviewer/shared/src/androidMain/kotlin/example/imageviewer/view/ScrollableColumn.android.kt
  5. 11
      examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/view/GalleryScreen.kt
  6. 5
      examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/view/ScrollableColumn.common.kt
  7. 11
      examples/imageviewer/shared/src/desktopMain/kotlin/example/imageviewer/view/ScrollableColumn.desktop.kt
  8. 39
      examples/imageviewer/shared/src/desktopTest/kotlin/ImageViewerTest.kt
  9. 3
      examples/imageviewer/shared/src/iosMain/kotlin/example/imageviewer/view/ScrollableColumn.ios.kt

6
examples/imageviewer/androidApp/build.gradle.kts

@ -6,7 +6,7 @@ plugins {
} }
kotlin { kotlin {
android() androidTarget()
sourceSets { sourceSets {
val androidMain by getting { val androidMain by getting {
dependencies { dependencies {
@ -17,11 +17,11 @@ kotlin {
} }
android { android {
compileSdk = 33 compileSdk = 34
defaultConfig { defaultConfig {
applicationId = "org.jetbrains.Imageviewer" applicationId = "org.jetbrains.Imageviewer"
minSdk = 26 minSdk = 26
targetSdk = 33 targetSdk = 34
versionCode = 1 versionCode = 1
versionName = "1.0" versionName = "1.0"
} }

2
examples/imageviewer/gradle.properties

@ -12,4 +12,4 @@ kotlin.native.useEmbeddableCompilerJar=true
kotlin.native.binary.memoryModel=experimental kotlin.native.binary.memoryModel=experimental
kotlin.version=1.9.0 kotlin.version=1.9.0
agp.version=7.1.3 agp.version=7.1.3
compose.version=1.5.0-beta02 compose.version=1.5.0-rc01

13
examples/imageviewer/shared/build.gradle.kts

@ -12,7 +12,7 @@ plugins {
version = "1.0-SNAPSHOT" version = "1.0-SNAPSHOT"
kotlin { kotlin {
android() androidTarget()
jvm("desktop") jvm("desktop")
ios() ios()
iosSimulatorArm64() iosSimulatorArm64()
@ -71,18 +71,25 @@ kotlin {
implementation(project(":mapview-desktop")) implementation(project(":mapview-desktop"))
} }
} }
val desktopTest by getting {
dependencies {
implementation(compose.desktop.currentOs)
implementation(compose.desktop.uiTestJUnit4)
}
}
} }
} }
android { android {
compileSdk = 33 compileSdk = 34
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml") sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
sourceSets["main"].res.srcDirs("src/androidMain/res") sourceSets["main"].res.srcDirs("src/androidMain/res")
sourceSets["main"].resources.srcDirs("src/commonMain/resources") sourceSets["main"].resources.srcDirs("src/commonMain/resources")
defaultConfig { defaultConfig {
minSdk = 26 minSdk = 26
targetSdk = 33 targetSdk = 34
} }
compileOptions { compileOptions {
sourceCompatibility = JavaVersion.VERSION_11 sourceCompatibility = JavaVersion.VERSION_11

3
examples/imageviewer/shared/src/androidMain/kotlin/example/imageviewer/view/ScrollableColumn.android.kt

@ -1,8 +1,9 @@
package example.imageviewer.view package example.imageviewer.view
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
@Composable @Composable
actual fun ScrollableColumn(modifier: Modifier, content: @Composable () -> Unit) = actual fun ScrollableColumn(modifier: Modifier, content: @Composable ColumnScope.() -> Unit) =
TouchScrollableColumn(modifier, content) TouchScrollableColumn(modifier, content)

11
examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/view/GalleryScreen.kt

@ -31,6 +31,7 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ImageBitmap import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.graphicsLayer import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import example.imageviewer.* import example.imageviewer.*
import example.imageviewer.icon.IconMenu import example.imageviewer.icon.IconMenu
@ -143,7 +144,10 @@ fun GalleryScreen(
TopLayout( TopLayout(
alignLeftContent = {}, alignLeftContent = {},
alignRightContent = { alignRightContent = {
CircularButton(imageVector = IconMenu) { CircularButton(
imageVector = IconMenu,
modifier = Modifier.testTag("toggleGalleryStyleButton")
) {
galleryStyle = when (galleryStyle) { galleryStyle = when (galleryStyle) {
GalleryStyle.SQUARES -> GalleryStyle.LIST GalleryStyle.SQUARES -> GalleryStyle.LIST
GalleryStyle.LIST -> GalleryStyle.SQUARES GalleryStyle.LIST -> GalleryStyle.SQUARES
@ -159,7 +163,6 @@ fun GalleryScreen(
pagerState = pagerState, pagerState = pagerState,
onSelect = { selectPicture(it) }, onSelect = { selectPicture(it) },
) )
GalleryStyle.LIST -> ListGalleryView( GalleryStyle.LIST -> ListGalleryView(
pictures = pictures, pictures = pictures,
onSelect = { selectPicture(it) }, onSelect = { selectPicture(it) },
@ -183,7 +186,7 @@ private fun SquaresGalleryView(
onSelect: (index: Int) -> Unit, onSelect: (index: Int) -> Unit,
) { ) {
LazyVerticalGrid( LazyVerticalGrid(
modifier = Modifier.padding(top = 4.dp), modifier = Modifier.padding(top = 4.dp).testTag("squaresGalleryView"),
columns = GridCells.Adaptive(minSize = 130.dp), columns = GridCells.Adaptive(minSize = 130.dp),
verticalArrangement = Arrangement.spacedBy(1.dp), verticalArrangement = Arrangement.spacedBy(1.dp),
horizontalArrangement = Arrangement.spacedBy(1.dp) horizontalArrangement = Arrangement.spacedBy(1.dp)
@ -253,7 +256,7 @@ private fun ListGalleryView(
) { ) {
val notification = LocalNotification.current val notification = LocalNotification.current
ScrollableColumn( ScrollableColumn(
modifier = Modifier.fillMaxSize() modifier = Modifier.fillMaxSize().testTag("listGalleryView")
) { ) {
Spacer(modifier = Modifier.height(10.dp)) Spacer(modifier = Modifier.height(10.dp))
for (p in pictures.withIndex()) { for (p in pictures.withIndex()) {

5
examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/view/ScrollableColumn.common.kt

@ -1,16 +1,17 @@
package example.imageviewer.view package example.imageviewer.view
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll import androidx.compose.foundation.verticalScroll
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
@Composable @Composable
expect fun ScrollableColumn(modifier: Modifier, content: @Composable () -> Unit) expect fun ScrollableColumn(modifier: Modifier, content: @Composable ColumnScope.() -> Unit)
@Composable @Composable
fun TouchScrollableColumn(modifier: Modifier, content: @Composable () -> Unit) { fun TouchScrollableColumn(modifier: Modifier, content: @Composable ColumnScope.() -> Unit) {
val scrollState = rememberScrollState() val scrollState = rememberScrollState()
Column(modifier.verticalScroll(scrollState)) { Column(modifier.verticalScroll(scrollState)) {
content() content()

11
examples/imageviewer/shared/src/desktopMain/kotlin/example/imageviewer/view/ScrollableColumn.desktop.kt

@ -1,10 +1,7 @@
package example.imageviewer.view package example.imageviewer.view
import androidx.compose.foundation.VerticalScrollbar import androidx.compose.foundation.VerticalScrollbar
import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.*
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.rememberScrollbarAdapter import androidx.compose.foundation.rememberScrollbarAdapter
import androidx.compose.foundation.verticalScroll import androidx.compose.foundation.verticalScroll
@ -14,12 +11,10 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
@Composable @Composable
actual fun ScrollableColumn(modifier: Modifier, content: @Composable () -> Unit) { actual fun ScrollableColumn(modifier: Modifier, content: @Composable ColumnScope.() -> Unit) {
val scrollState = rememberScrollState() val scrollState = rememberScrollState()
Modifier.verticalScroll(scrollState)
Box(modifier) { Box(modifier) {
Column(modifier.verticalScroll(scrollState)) { Column(Modifier.verticalScroll(scrollState)) {
content() content()
} }
VerticalScrollbar( VerticalScrollbar(

39
examples/imageviewer/shared/src/desktopTest/kotlin/ImageViewerTest.kt

@ -0,0 +1,39 @@
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.performClick
import example.imageviewer.*
import example.imageviewer.filter.PlatformContext
import example.imageviewer.model.PictureData
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import org.junit.Rule
import org.junit.Test
class ImageViewerTest {
@get:Rule
val rule = createComposeRule()
private val dependencies = object : Dependencies() {
override val notification: Notification = object : PopupNotification(localization) {
override fun showPopUpMessage(text: String) {
}
}
override val imageStorage: DesktopImageStorage = DesktopImageStorage(pictures, CoroutineScope(Dispatchers.Main))
override val sharePicture: SharePicture = object : SharePicture {
override fun share(context: PlatformContext, picture: PictureData) { }
}
}
@Test
fun testToggleGalleryStyleButton() {
rule.setContent {
ImageViewerCommon(dependencies)
}
rule.onNodeWithTag("squaresGalleryView").assertExists()
rule.onNodeWithTag("listGalleryView").assertDoesNotExist()
rule.onNodeWithTag("toggleGalleryStyleButton").performClick()
rule.onNodeWithTag("squaresGalleryView").assertDoesNotExist()
rule.onNodeWithTag("listGalleryView").assertExists()
}
}

3
examples/imageviewer/shared/src/iosMain/kotlin/example/imageviewer/view/ScrollableColumn.ios.kt

@ -1,8 +1,9 @@
package example.imageviewer.view package example.imageviewer.view
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
@Composable @Composable
actual fun ScrollableColumn(modifier: Modifier, content: @Composable () -> Unit) = actual fun ScrollableColumn(modifier: Modifier, content: @Composable ColumnScope.() -> Unit) =
TouchScrollableColumn(modifier, content) TouchScrollableColumn(modifier, content)

Loading…
Cancel
Save