diff --git a/images/coil/src/test/java/com/google/android/horologist/compose/material/CoilPaintableTest.kt b/images/coil/src/test/java/com/google/android/horologist/compose/material/CoilPaintableTest.kt new file mode 100644 index 0000000000..1c3a374c09 --- /dev/null +++ b/images/coil/src/test/java/com/google/android/horologist/compose/material/CoilPaintableTest.kt @@ -0,0 +1,195 @@ +/* + * Copyright 2022 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 + * + * https://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. + */ + +@file:OptIn(ExperimentalCoilApi::class) + +package com.google.android.horologist.compose.material + +import android.graphics.BitmapFactory +import androidx.compose.foundation.Image +import androidx.compose.foundation.layout.size +import androidx.compose.foundation.shape.CircleShape +import androidx.compose.runtime.Composable +import androidx.compose.runtime.CompositionLocalProvider +import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.alpha +import androidx.compose.ui.draw.clip +import androidx.compose.ui.layout.ContentScale +import androidx.compose.ui.platform.LocalContext +import androidx.core.graphics.drawable.toDrawable +import androidx.wear.compose.material.ChipDefaults +import androidx.wear.compose.material.LocalContentAlpha +import coil.annotation.ExperimentalCoilApi +import coil.compose.AsyncImage +import coil.decode.DataSource +import coil.request.ImageRequest +import coil.request.SuccessResult +import coil.test.FakeImageLoaderEngine +import coil.transform.CircleCropTransformation +import com.google.android.horologist.compose.material.util.DECORATIVE_ELEMENT_CONTENT_DESCRIPTION +import com.google.android.horologist.images.coil.CoilPaintable +import com.google.android.horologist.images.coil.R.drawable.sample_image +import com.google.android.horologist.screenshots.rng.WearLegacyComponentTest +import org.junit.Test + +class CoilPaintableTest : WearLegacyComponentTest() { + + override val imageLoader = FakeImageLoaderEngine.Builder() + .intercept( + predicate = { + it == sample_image + }, + interceptor = { + val rawBitmap = + BitmapFactory.decodeFile("src/main/res/drawable-nodpi/sample_image.png") + + val resultBitmap = it.request.transformations.fold(rawBitmap) { bitmap, transformation -> + transformation.transform(bitmap, it.size) + } + + SuccessResult( + drawable = resultBitmap.toDrawable(it.request.context.resources), + request = it.request, + dataSource = DataSource.MEMORY, + ) + }, + ) + .build() + + @Test + fun imageFromChip() { + runComponentTest { + val paintable = CoilPaintable( + sample_image, + ) + + ImageFromChip(paintable) + } + } + + @Test + fun imageFromChipAlpha50() { + runComponentTest { + val paintable = CoilPaintable( + sample_image, + ) + + CompositionLocalProvider(LocalContentAlpha provides 0.5f) { + ImageFromChip(paintable) + } + } + } + + @Test + fun imageDirectAlpha50() { + runComponentTest { + val paintable = CoilPaintable( + sample_image, + ) + + Image( + painter = paintable.rememberPainter(), + contentDescription = DECORATIVE_ELEMENT_CONTENT_DESCRIPTION, + modifier = Modifier + .size(ChipDefaults.LargeIconSize) + .clip(CircleShape) + .alpha(0.5f), + ) + } + } + + @Test + fun imageDirectOnlyDefaults() { + runComponentTest { + val paintable = CoilPaintable( + sample_image, + ) + + Image( + painter = paintable.rememberPainter(), + contentDescription = DECORATIVE_ELEMENT_CONTENT_DESCRIPTION, + modifier = Modifier + .size(ChipDefaults.LargeIconSize), + ) + } + } + + @Test + fun imageRequest() { + runComponentTest { + val paintable = CoilPaintable( + ImageRequest.Builder(LocalContext.current) + .data(sample_image) + .build(), + ) + + Image( + painter = paintable.rememberPainter(), + contentDescription = DECORATIVE_ELEMENT_CONTENT_DESCRIPTION, + modifier = Modifier + .size(ChipDefaults.LargeIconSize) + .clip(CircleShape), + ) + } + } + + @Test + fun imageRequestWithTransformationAlpha50() { + runComponentTest { + val paintable = CoilPaintable( + ImageRequest.Builder(LocalContext.current) + .data(sample_image) + .transformations(CircleCropTransformation()) + .build(), + ) + + Image( + painter = paintable.rememberPainter(), + contentDescription = DECORATIVE_ELEMENT_CONTENT_DESCRIPTION, + alpha = 0.5f, + modifier = Modifier.size(ChipDefaults.LargeIconSize), + ) + } + } + + // Not using CoilPaintable, just Coil directly + @Test + fun coilDirectlyAsyncImageAlpha50() { + runComponentTest { + AsyncImage( + contentDescription = DECORATIVE_ELEMENT_CONTENT_DESCRIPTION, + model = sample_image, + modifier = Modifier + .size(ChipDefaults.LargeIconSize) + .clip(CircleShape) + .alpha(0.5f), + ) + } + } + + @Composable + private fun ImageFromChip(paintable: CoilPaintable) { + Image( + painter = paintable.rememberPainter(), + contentDescription = DECORATIVE_ELEMENT_CONTENT_DESCRIPTION, + modifier = Modifier + .size(ChipDefaults.LargeIconSize) + .clip(CircleShape), + contentScale = ContentScale.Crop, + alpha = LocalContentAlpha.current, + ) + } +} diff --git a/images/coil/src/test/snapshots/images/com.google.android.horologist.compose.material_CoilPaintableTest_coilDirectlyAsyncImageAlpha50.png b/images/coil/src/test/snapshots/images/com.google.android.horologist.compose.material_CoilPaintableTest_coilDirectlyAsyncImageAlpha50.png new file mode 100644 index 0000000000..e4b3aa7771 --- /dev/null +++ b/images/coil/src/test/snapshots/images/com.google.android.horologist.compose.material_CoilPaintableTest_coilDirectlyAsyncImageAlpha50.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cab053fdd5e713838f2cbe59569debdfbe2101c7adb08c7a2d70c7235dd77b97 +size 2001 diff --git a/images/coil/src/test/snapshots/images/com.google.android.horologist.compose.material_CoilPaintableTest_imageDirectAlpha50.png b/images/coil/src/test/snapshots/images/com.google.android.horologist.compose.material_CoilPaintableTest_imageDirectAlpha50.png new file mode 100644 index 0000000000..e4b3aa7771 --- /dev/null +++ b/images/coil/src/test/snapshots/images/com.google.android.horologist.compose.material_CoilPaintableTest_imageDirectAlpha50.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cab053fdd5e713838f2cbe59569debdfbe2101c7adb08c7a2d70c7235dd77b97 +size 2001 diff --git a/images/coil/src/test/snapshots/images/com.google.android.horologist.compose.material_CoilPaintableTest_imageDirectOnlyDefaults.png b/images/coil/src/test/snapshots/images/com.google.android.horologist.compose.material_CoilPaintableTest_imageDirectOnlyDefaults.png new file mode 100644 index 0000000000..0f451d77bb --- /dev/null +++ b/images/coil/src/test/snapshots/images/com.google.android.horologist.compose.material_CoilPaintableTest_imageDirectOnlyDefaults.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3c172a7698fb0d0f17d263b9d9ae39c13d4458971bd1cfefa0223f842c0957e4 +size 1222 diff --git a/images/coil/src/test/snapshots/images/com.google.android.horologist.compose.material_CoilPaintableTest_imageFromChip.png b/images/coil/src/test/snapshots/images/com.google.android.horologist.compose.material_CoilPaintableTest_imageFromChip.png new file mode 100644 index 0000000000..efd4875c12 --- /dev/null +++ b/images/coil/src/test/snapshots/images/com.google.android.horologist.compose.material_CoilPaintableTest_imageFromChip.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:68d861374604120050d58dcf166d9fcc028557add0947315e239922b5b5b1f47 +size 2158 diff --git a/images/coil/src/test/snapshots/images/com.google.android.horologist.compose.material_CoilPaintableTest_imageFromChipAlpha50.png b/images/coil/src/test/snapshots/images/com.google.android.horologist.compose.material_CoilPaintableTest_imageFromChipAlpha50.png new file mode 100644 index 0000000000..b1ee1bad07 --- /dev/null +++ b/images/coil/src/test/snapshots/images/com.google.android.horologist.compose.material_CoilPaintableTest_imageFromChipAlpha50.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7f755418fcfe37eaf013d9b7c0b9685eff2bc888914c3025ed7a6d85e3f0d95d +size 2153 diff --git a/images/coil/src/test/snapshots/images/com.google.android.horologist.compose.material_CoilPaintableTest_imageRequest.png b/images/coil/src/test/snapshots/images/com.google.android.horologist.compose.material_CoilPaintableTest_imageRequest.png new file mode 100644 index 0000000000..efd4875c12 --- /dev/null +++ b/images/coil/src/test/snapshots/images/com.google.android.horologist.compose.material_CoilPaintableTest_imageRequest.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:68d861374604120050d58dcf166d9fcc028557add0947315e239922b5b5b1f47 +size 2158 diff --git a/images/coil/src/test/snapshots/images/com.google.android.horologist.compose.material_CoilPaintableTest_imageRequestWithTransformationAlpha50.png b/images/coil/src/test/snapshots/images/com.google.android.horologist.compose.material_CoilPaintableTest_imageRequestWithTransformationAlpha50.png new file mode 100644 index 0000000000..9406557fff --- /dev/null +++ b/images/coil/src/test/snapshots/images/com.google.android.horologist.compose.material_CoilPaintableTest_imageRequestWithTransformationAlpha50.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9302c33152c908db77786d668397df5be1ffd0c919c9fb742bef1b166ff8f07d +size 1732