Skip to content

Commit

Permalink
Add a unit test to the imageviewer example. (#3527)
Browse files Browse the repository at this point in the history
* Add a unit test to the imageviewer example.

* Fix android build of imageviewer
  • Loading branch information
m-sasha authored Aug 18, 2023
1 parent 87d1972 commit 8c72409
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 23 deletions.
6 changes: 3 additions & 3 deletions examples/imageviewer/androidApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

kotlin {
android()
androidTarget()
sourceSets {
val androidMain by getting {
dependencies {
Expand All @@ -17,11 +17,11 @@ kotlin {
}

android {
compileSdk = 33
compileSdk = 34
defaultConfig {
applicationId = "org.jetbrains.Imageviewer"
minSdk = 26
targetSdk = 33
targetSdk = 34
versionCode = 1
versionName = "1.0"
}
Expand Down
2 changes: 1 addition & 1 deletion examples/imageviewer/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ kotlin.native.useEmbeddableCompilerJar=true
kotlin.native.binary.memoryModel=experimental
kotlin.version=1.9.0
agp.version=7.1.3
compose.version=1.5.0-beta02
compose.version=1.5.0-rc01
13 changes: 10 additions & 3 deletions examples/imageviewer/shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ plugins {
version = "1.0-SNAPSHOT"

kotlin {
android()
androidTarget()
jvm("desktop")
ios()
iosSimulatorArm64()
Expand Down Expand Up @@ -71,18 +71,25 @@ kotlin {
implementation(project(":mapview-desktop"))
}
}

val desktopTest by getting {
dependencies {
implementation(compose.desktop.currentOs)
implementation(compose.desktop.uiTestJUnit4)
}
}
}
}

android {
compileSdk = 33
compileSdk = 34
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
sourceSets["main"].res.srcDirs("src/androidMain/res")
sourceSets["main"].resources.srcDirs("src/commonMain/resources")

defaultConfig {
minSdk = 26
targetSdk = 33
targetSdk = 34
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package example.imageviewer.view

import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier

@Composable
actual fun ScrollableColumn(modifier: Modifier, content: @Composable () -> Unit) =
actual fun ScrollableColumn(modifier: Modifier, content: @Composable ColumnScope.() -> Unit) =
TouchScrollableColumn(modifier, content)
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.unit.dp
import example.imageviewer.*
import example.imageviewer.icon.IconMenu
Expand Down Expand Up @@ -143,7 +144,10 @@ fun GalleryScreen(
TopLayout(
alignLeftContent = {},
alignRightContent = {
CircularButton(imageVector = IconMenu) {
CircularButton(
imageVector = IconMenu,
modifier = Modifier.testTag("toggleGalleryStyleButton")
) {
galleryStyle = when (galleryStyle) {
GalleryStyle.SQUARES -> GalleryStyle.LIST
GalleryStyle.LIST -> GalleryStyle.SQUARES
Expand All @@ -159,7 +163,6 @@ fun GalleryScreen(
pagerState = pagerState,
onSelect = { selectPicture(it) },
)

GalleryStyle.LIST -> ListGalleryView(
pictures = pictures,
onSelect = { selectPicture(it) },
Expand All @@ -183,7 +186,7 @@ private fun SquaresGalleryView(
onSelect: (index: Int) -> Unit,
) {
LazyVerticalGrid(
modifier = Modifier.padding(top = 4.dp),
modifier = Modifier.padding(top = 4.dp).testTag("squaresGalleryView"),
columns = GridCells.Adaptive(minSize = 130.dp),
verticalArrangement = Arrangement.spacedBy(1.dp),
horizontalArrangement = Arrangement.spacedBy(1.dp)
Expand Down Expand Up @@ -253,7 +256,7 @@ private fun ListGalleryView(
) {
val notification = LocalNotification.current
ScrollableColumn(
modifier = Modifier.fillMaxSize()
modifier = Modifier.fillMaxSize().testTag("listGalleryView")
) {
Spacer(modifier = Modifier.height(10.dp))
for (p in pictures.withIndex()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package example.imageviewer.view

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier

@Composable
expect fun ScrollableColumn(modifier: Modifier, content: @Composable () -> Unit)
expect fun ScrollableColumn(modifier: Modifier, content: @Composable ColumnScope.() -> Unit)

@Composable
fun TouchScrollableColumn(modifier: Modifier, content: @Composable () -> Unit) {
fun TouchScrollableColumn(modifier: Modifier, content: @Composable ColumnScope.() -> Unit) {
val scrollState = rememberScrollState()
Column(modifier.verticalScroll(scrollState)) {
content()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package example.imageviewer.view

import androidx.compose.foundation.VerticalScrollbar
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.rememberScrollbarAdapter
import androidx.compose.foundation.verticalScroll
Expand All @@ -14,12 +11,10 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp

@Composable
actual fun ScrollableColumn(modifier: Modifier, content: @Composable () -> Unit) {
actual fun ScrollableColumn(modifier: Modifier, content: @Composable ColumnScope.() -> Unit) {
val scrollState = rememberScrollState()
Modifier.verticalScroll(scrollState)

Box(modifier) {
Column(modifier.verticalScroll(scrollState)) {
Column(Modifier.verticalScroll(scrollState)) {
content()
}
VerticalScrollbar(
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package example.imageviewer.view

import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier

@Composable
actual fun ScrollableColumn(modifier: Modifier, content: @Composable () -> Unit) =
actual fun ScrollableColumn(modifier: Modifier, content: @Composable ColumnScope.() -> Unit) =
TouchScrollableColumn(modifier, content)

0 comments on commit 8c72409

Please sign in to comment.