|
| 1 | +/* |
| 2 | + * Wire |
| 3 | + * Copyright (C) 2024 Wire Swiss GmbH |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see http://www.gnu.org/licenses/. |
| 17 | + */ |
| 18 | + |
| 19 | +package com.wire.android.ui.home.conversations.model.messagetypes.image |
| 20 | + |
| 21 | +import androidx.compose.ui.test.junit4.createComposeRule |
| 22 | +import androidx.compose.ui.unit.dp |
| 23 | +import com.wire.android.ui.WireTestTheme |
| 24 | +import kotlinx.coroutines.test.runTest |
| 25 | +import org.junit.Assert.assertEquals |
| 26 | +import org.junit.Assert.assertFalse |
| 27 | +import org.junit.Assert.assertTrue |
| 28 | +import org.junit.Rule |
| 29 | +import org.junit.Test |
| 30 | + |
| 31 | +class VisualMediaParamsTest { |
| 32 | + |
| 33 | + @get:Rule |
| 34 | + val composeTestRule = createComposeRule() |
| 35 | + |
| 36 | + @Test |
| 37 | + fun givenNonPositiveRealDimensions_shouldReturnMinSizeAndLandscapeFlag() = runTest { |
| 38 | + val params = VisualMediaParams(realMediaWidth = 0, realMediaHeight = -10) |
| 39 | + |
| 40 | + val minW = 40.dp |
| 41 | + val minH = 30.dp |
| 42 | + |
| 43 | + var result: NormalizedSize? = null |
| 44 | + |
| 45 | + composeTestRule.setContent { |
| 46 | + WireTestTheme { |
| 47 | + result = params.normalizedSize( |
| 48 | + minW = minW, |
| 49 | + minH = minH, |
| 50 | + maxBounds = MaxBounds.DpBounds( |
| 51 | + maxW = 200.dp, |
| 52 | + maxH = 200.dp |
| 53 | + ) |
| 54 | + ) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + composeTestRule.runOnIdle { |
| 59 | + val size = result!! |
| 60 | + assertEquals(minW, size.width) |
| 61 | + assertEquals(minH, size.height) |
| 62 | + assertFalse(size.isPortrait) |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + fun givenLandscapeImageAndDpBounds_shouldFitWithinMaxAndRespectAspectRatio() = runTest { |
| 68 | + val params = VisualMediaParams(realMediaWidth = 1920, realMediaHeight = 1080) |
| 69 | + |
| 70 | + val minW = 40.dp |
| 71 | + val minH = 40.dp |
| 72 | + val maxW = 300.dp |
| 73 | + val maxH = 300.dp |
| 74 | + |
| 75 | + var result: NormalizedSize? = null |
| 76 | + |
| 77 | + composeTestRule.setContent { |
| 78 | + WireTestTheme { |
| 79 | + result = params.normalizedSize( |
| 80 | + minW = minW, |
| 81 | + minH = minH, |
| 82 | + maxBounds = MaxBounds.DpBounds( |
| 83 | + maxW = maxW, |
| 84 | + maxH = maxH |
| 85 | + ) |
| 86 | + ) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + composeTestRule.runOnIdle { |
| 91 | + val size = result!! |
| 92 | + |
| 93 | + assertTrue(size.width <= maxW) |
| 94 | + assertTrue(size.height <= maxH) |
| 95 | + |
| 96 | + assertTrue(size.width >= minW) |
| 97 | + assertTrue(size.height >= minH) |
| 98 | + |
| 99 | + assertFalse(size.isPortrait) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + fun givenPortraitImageAndDpBounds_shouldFitWithinMaxAndRespectAspectRatio() = runTest { |
| 105 | + val params = VisualMediaParams(realMediaWidth = 1080, realMediaHeight = 1920) |
| 106 | + |
| 107 | + val minW = 40.dp |
| 108 | + val minH = 40.dp |
| 109 | + val maxW = 300.dp |
| 110 | + val maxH = 300.dp |
| 111 | + |
| 112 | + var result: NormalizedSize? = null |
| 113 | + |
| 114 | + composeTestRule.setContent { |
| 115 | + WireTestTheme { |
| 116 | + result = params.normalizedSize( |
| 117 | + minW = minW, |
| 118 | + minH = minH, |
| 119 | + maxBounds = MaxBounds.DpBounds( |
| 120 | + maxW = maxW, |
| 121 | + maxH = maxH |
| 122 | + ) |
| 123 | + ) |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + composeTestRule.runOnIdle { |
| 128 | + val size = result!! |
| 129 | + |
| 130 | + assertTrue(size.width <= maxW) |
| 131 | + assertTrue(size.height <= maxH) |
| 132 | + |
| 133 | + assertTrue(size.width >= minW) |
| 134 | + assertTrue(size.height >= minH) |
| 135 | + |
| 136 | + assertTrue(size.isPortrait) |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + fun givenMinWidthGreaterThanMaxWidth_shouldNotCrashAndClampToMax() = runTest { |
| 142 | + val params = VisualMediaParams(realMediaWidth = 2000, realMediaHeight = 1000) |
| 143 | + |
| 144 | + val minW = 300.dp |
| 145 | + val minH = 150.dp |
| 146 | + val maxW = 200.dp |
| 147 | + val maxH = 120.dp |
| 148 | + |
| 149 | + var result: NormalizedSize? = null |
| 150 | + |
| 151 | + composeTestRule.setContent { |
| 152 | + WireTestTheme { |
| 153 | + result = params.normalizedSize( |
| 154 | + minW = minW, |
| 155 | + minH = minH, |
| 156 | + maxBounds = MaxBounds.DpBounds( |
| 157 | + maxW = maxW, |
| 158 | + maxH = maxH |
| 159 | + ) |
| 160 | + ) |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + composeTestRule.runOnIdle { |
| 165 | + val size = result!! |
| 166 | + |
| 167 | + assertEquals(maxW, size.width) |
| 168 | + |
| 169 | + assertTrue(size.height > 0.dp) |
| 170 | + assertTrue(size.height <= maxH) |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + @Test |
| 175 | + fun givenScreenFractionBounds_shouldStayWithinCalculatedMaxBounds() = runTest { |
| 176 | + val params = VisualMediaParams(realMediaWidth = 1920, realMediaHeight = 1080) |
| 177 | + |
| 178 | + val minW = 80.dp |
| 179 | + val minH = 80.dp |
| 180 | + val fractionW = 0.2f |
| 181 | + val fractionH = 0.2f |
| 182 | + |
| 183 | + var result: NormalizedSize? = null |
| 184 | + |
| 185 | + composeTestRule.setContent { |
| 186 | + WireTestTheme { |
| 187 | + result = params.normalizedSize( |
| 188 | + minW = minW, |
| 189 | + minH = minH, |
| 190 | + maxBounds = MaxBounds.ScreenFraction( |
| 191 | + maxWFraction = fractionW, |
| 192 | + maxHFraction = fractionH |
| 193 | + ) |
| 194 | + ) |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + composeTestRule.runOnIdle { |
| 199 | + val size = result!! |
| 200 | + |
| 201 | + assertTrue(size.width > 0.dp) |
| 202 | + assertTrue(size.height > 0.dp) |
| 203 | + } |
| 204 | + } |
| 205 | +} |
0 commit comments