|
| 1 | +package com.canhub.cropper |
| 2 | + |
| 3 | +import android.net.Uri |
| 4 | +import org.junit.Assert.assertEquals |
| 5 | +import org.junit.Assert.assertNotNull |
| 6 | +import org.junit.Assert.assertTrue |
| 7 | +import org.junit.Test |
| 8 | + |
| 9 | +/** |
| 10 | + * Test suite for CropException sealed class hierarchy. |
| 11 | + * |
| 12 | + * Covers: |
| 13 | + * - All exception types and their messages |
| 14 | + * - Exception inheritance structure |
| 15 | + * - Message formatting |
| 16 | + * - Serialization compatibility |
| 17 | + */ |
| 18 | +class CropExceptionTest { |
| 19 | + |
| 20 | + // ==================== Exception Type Tests ==================== |
| 21 | + |
| 22 | + @Test |
| 23 | + fun `WHEN Cancellation exception created THEN has correct message`() { |
| 24 | + // WHEN |
| 25 | + val exception = CropException.Cancellation() |
| 26 | + |
| 27 | + // THEN |
| 28 | + assertTrue(exception.message!!.contains("crop:")) |
| 29 | + assertTrue(exception.message!!.contains("cropping has been cancelled by the user")) |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + fun `WHEN Cancellation exception created THEN extends CropException`() { |
| 34 | + // WHEN |
| 35 | + val exception = CropException.Cancellation() |
| 36 | + |
| 37 | + // THEN |
| 38 | + assertTrue(exception is CropException) |
| 39 | + assertTrue(exception is Exception) |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + fun `WHEN FailedToLoadBitmap exception created THEN includes URI and message`() { |
| 44 | + // GIVEN |
| 45 | + val uri = Uri.parse("content://com.example/image.jpg") |
| 46 | + val errorMessage = "OutOfMemoryError" |
| 47 | + |
| 48 | + // WHEN |
| 49 | + val exception = CropException.FailedToLoadBitmap(uri, errorMessage) |
| 50 | + |
| 51 | + // THEN |
| 52 | + assertNotNull(exception.message) |
| 53 | + assertTrue(exception.message!!.contains("crop:")) |
| 54 | + assertTrue(exception.message!!.contains("Failed to load sampled bitmap")) |
| 55 | + assertTrue(exception.message!!.contains(uri.toString())) |
| 56 | + assertTrue(exception.message!!.contains(errorMessage)) |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + fun `WHEN FailedToLoadBitmap exception created with null message THEN includes null in message`() { |
| 61 | + // GIVEN |
| 62 | + val uri = Uri.parse("content://com.example/image.jpg") |
| 63 | + |
| 64 | + // WHEN |
| 65 | + val exception = CropException.FailedToLoadBitmap(uri, null) |
| 66 | + |
| 67 | + // THEN |
| 68 | + assertNotNull(exception.message) |
| 69 | + assertTrue(exception.message!!.contains("crop:")) |
| 70 | + assertTrue(exception.message!!.contains("Failed to load sampled bitmap")) |
| 71 | + assertTrue(exception.message!!.contains(uri.toString())) |
| 72 | + assertTrue(exception.message!!.contains("null")) |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + fun `WHEN FailedToLoadBitmap exception created THEN extends CropException`() { |
| 77 | + // GIVEN |
| 78 | + val uri = Uri.parse("content://com.example/image.jpg") |
| 79 | + |
| 80 | + // WHEN |
| 81 | + val exception = CropException.FailedToLoadBitmap(uri, "test error") |
| 82 | + |
| 83 | + // THEN |
| 84 | + assertTrue(exception is CropException) |
| 85 | + assertTrue(exception is Exception) |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + fun `WHEN FailedToDecodeImage exception created THEN includes URI`() { |
| 90 | + // GIVEN |
| 91 | + val uri = Uri.parse("content://com.example/corrupted.jpg") |
| 92 | + |
| 93 | + // WHEN |
| 94 | + val exception = CropException.FailedToDecodeImage(uri) |
| 95 | + |
| 96 | + // THEN |
| 97 | + assertNotNull(exception.message) |
| 98 | + assertTrue(exception.message!!.contains("crop:")) |
| 99 | + assertTrue(exception.message!!.contains("Failed to decode image")) |
| 100 | + assertTrue(exception.message!!.contains(uri.toString())) |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + fun `WHEN FailedToDecodeImage exception created THEN extends CropException`() { |
| 105 | + // GIVEN |
| 106 | + val uri = Uri.parse("content://com.example/image.jpg") |
| 107 | + |
| 108 | + // WHEN |
| 109 | + val exception = CropException.FailedToDecodeImage(uri) |
| 110 | + |
| 111 | + // THEN |
| 112 | + assertTrue(exception is CropException) |
| 113 | + assertTrue(exception is Exception) |
| 114 | + } |
| 115 | + |
| 116 | + // ==================== Message Prefix Tests ==================== |
| 117 | + |
| 118 | + @Test |
| 119 | + fun `WHEN any CropException created THEN message starts with crop prefix`() { |
| 120 | + // GIVEN |
| 121 | + val uri = Uri.parse("content://test") |
| 122 | + |
| 123 | + // WHEN |
| 124 | + val cancellation = CropException.Cancellation() |
| 125 | + val failedToLoad = CropException.FailedToLoadBitmap(uri, "error") |
| 126 | + val failedToDecode = CropException.FailedToDecodeImage(uri) |
| 127 | + |
| 128 | + // THEN |
| 129 | + assertTrue(cancellation.message!!.startsWith("crop:")) |
| 130 | + assertTrue(failedToLoad.message!!.startsWith("crop:")) |
| 131 | + assertTrue(failedToDecode.message!!.startsWith("crop:")) |
| 132 | + } |
| 133 | + |
| 134 | + // ==================== Throwing and Catching Tests ==================== |
| 135 | + |
| 136 | + @Test |
| 137 | + fun `WHEN Cancellation exception thrown THEN can be caught as CropException`() { |
| 138 | + // WHEN/THEN |
| 139 | + try { |
| 140 | + throw CropException.Cancellation() |
| 141 | + } catch (e: CropException) { |
| 142 | + assertTrue(e is CropException.Cancellation) |
| 143 | + assertTrue(e.message!!.contains("cancelled")) |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + fun `WHEN FailedToLoadBitmap exception thrown THEN can be caught as CropException`() { |
| 149 | + // GIVEN |
| 150 | + val uri = Uri.parse("content://test") |
| 151 | + |
| 152 | + // WHEN/THEN |
| 153 | + try { |
| 154 | + throw CropException.FailedToLoadBitmap(uri, "test error") |
| 155 | + } catch (e: CropException) { |
| 156 | + assertTrue(e is CropException.FailedToLoadBitmap) |
| 157 | + assertTrue(e.message!!.contains("Failed to load sampled bitmap")) |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + fun `WHEN FailedToDecodeImage exception thrown THEN can be caught as CropException`() { |
| 163 | + // GIVEN |
| 164 | + val uri = Uri.parse("content://test") |
| 165 | + |
| 166 | + // WHEN/THEN |
| 167 | + try { |
| 168 | + throw CropException.FailedToDecodeImage(uri) |
| 169 | + } catch (e: CropException) { |
| 170 | + assertTrue(e is CropException.FailedToDecodeImage) |
| 171 | + assertTrue(e.message!!.contains("Failed to decode image")) |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + @Test |
| 176 | + fun `WHEN any CropException thrown THEN can be caught as Exception`() { |
| 177 | + // GIVEN |
| 178 | + val uri = Uri.parse("content://test") |
| 179 | + |
| 180 | + // WHEN/THEN - Cancellation |
| 181 | + try { |
| 182 | + throw CropException.Cancellation() |
| 183 | + } catch (e: Exception) { |
| 184 | + assertTrue(e is CropException) |
| 185 | + } |
| 186 | + |
| 187 | + // WHEN/THEN - FailedToLoadBitmap |
| 188 | + try { |
| 189 | + throw CropException.FailedToLoadBitmap(uri, "error") |
| 190 | + } catch (e: Exception) { |
| 191 | + assertTrue(e is CropException) |
| 192 | + } |
| 193 | + |
| 194 | + // WHEN/THEN - FailedToDecodeImage |
| 195 | + try { |
| 196 | + throw CropException.FailedToDecodeImage(uri) |
| 197 | + } catch (e: Exception) { |
| 198 | + assertTrue(e is CropException) |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + // ==================== URI Formatting Tests ==================== |
| 203 | + |
| 204 | + @Test |
| 205 | + fun `WHEN FailedToLoadBitmap created with file URI THEN message includes file path`() { |
| 206 | + // GIVEN |
| 207 | + val uri = Uri.parse("file:///storage/emulated/0/Pictures/image.jpg") |
| 208 | + |
| 209 | + // WHEN |
| 210 | + val exception = CropException.FailedToLoadBitmap(uri, "disk error") |
| 211 | + |
| 212 | + // THEN |
| 213 | + assertTrue(exception.message!!.contains(uri.toString())) |
| 214 | + assertTrue(exception.message!!.contains("file://")) |
| 215 | + } |
| 216 | + |
| 217 | + @Test |
| 218 | + fun `WHEN FailedToDecodeImage created with content URI THEN message includes content URI`() { |
| 219 | + // GIVEN |
| 220 | + val uri = Uri.parse("content://com.android.providers.media.documents/document/image%3A123") |
| 221 | + |
| 222 | + // WHEN |
| 223 | + val exception = CropException.FailedToDecodeImage(uri) |
| 224 | + |
| 225 | + // THEN |
| 226 | + assertTrue(exception.message!!.contains(uri.toString())) |
| 227 | + assertTrue(exception.message!!.contains("content://")) |
| 228 | + } |
| 229 | + |
| 230 | + // ==================== Edge Cases ==================== |
| 231 | + |
| 232 | + @Test |
| 233 | + fun `WHEN FailedToLoadBitmap created with empty error message THEN includes empty string`() { |
| 234 | + // GIVEN |
| 235 | + val uri = Uri.parse("content://test") |
| 236 | + |
| 237 | + // WHEN |
| 238 | + val exception = CropException.FailedToLoadBitmap(uri, "") |
| 239 | + |
| 240 | + // THEN |
| 241 | + assertNotNull(exception.message) |
| 242 | + // Message should still be valid even with empty error |
| 243 | + assertTrue(exception.message!!.contains("Failed to load sampled bitmap")) |
| 244 | + } |
| 245 | + |
| 246 | + @Test |
| 247 | + fun `WHEN FailedToLoadBitmap created with multiline error message THEN preserves newlines`() { |
| 248 | + // GIVEN |
| 249 | + val uri = Uri.parse("content://test") |
| 250 | + val multilineError = "Error on line 1\nError on line 2" |
| 251 | + |
| 252 | + // WHEN |
| 253 | + val exception = CropException.FailedToLoadBitmap(uri, multilineError) |
| 254 | + |
| 255 | + // THEN |
| 256 | + assertTrue(exception.message!!.contains("Error on line 1")) |
| 257 | + assertTrue(exception.message!!.contains("Error on line 2")) |
| 258 | + } |
| 259 | + |
| 260 | + @Test |
| 261 | + fun `WHEN exception created with URI containing special characters THEN message is valid`() { |
| 262 | + // GIVEN |
| 263 | + val uri = Uri.parse("content://provider/path/with%20spaces/and&special?chars=123") |
| 264 | + |
| 265 | + // WHEN |
| 266 | + val exception = CropException.FailedToDecodeImage(uri) |
| 267 | + |
| 268 | + // THEN |
| 269 | + assertNotNull(exception.message) |
| 270 | + assertTrue(exception.message!!.contains(uri.toString())) |
| 271 | + } |
| 272 | + |
| 273 | + // ==================== Sealed Class Tests ==================== |
| 274 | + |
| 275 | + @Test |
| 276 | + fun `WHEN checking sealed hierarchy THEN only three exception types exist`() { |
| 277 | + // GIVEN |
| 278 | + val uri = Uri.parse("content://test") |
| 279 | + |
| 280 | + // WHEN - Create all three types |
| 281 | + val cancellation: CropException = CropException.Cancellation() |
| 282 | + val failedToLoad: CropException = CropException.FailedToLoadBitmap(uri, "error") |
| 283 | + val failedToDecode: CropException = CropException.FailedToDecodeImage(uri) |
| 284 | + |
| 285 | + // THEN - Verify types using when expression (exhaustive for sealed class) |
| 286 | + listOf(cancellation, failedToLoad, failedToDecode).forEach { exception -> |
| 287 | + when (exception) { |
| 288 | + is CropException.Cancellation -> assertTrue(true) |
| 289 | + is CropException.FailedToLoadBitmap -> assertTrue(true) |
| 290 | + is CropException.FailedToDecodeImage -> assertTrue(true) |
| 291 | + // No else needed - sealed class is exhaustive |
| 292 | + } |
| 293 | + } |
| 294 | + } |
| 295 | + |
| 296 | + @Test |
| 297 | + fun `WHEN comparing exception messages THEN each type has unique message pattern`() { |
| 298 | + // GIVEN |
| 299 | + val uri = Uri.parse("content://test") |
| 300 | + val cancellation = CropException.Cancellation() |
| 301 | + val failedToLoad = CropException.FailedToLoadBitmap(uri, "error") |
| 302 | + val failedToDecode = CropException.FailedToDecodeImage(uri) |
| 303 | + |
| 304 | + // THEN - Each has unique identifying text |
| 305 | + assertTrue(cancellation.message!!.contains("cancelled by the user")) |
| 306 | + assertTrue(failedToLoad.message!!.contains("Failed to load sampled bitmap")) |
| 307 | + assertTrue(failedToDecode.message!!.contains("Failed to decode image")) |
| 308 | + |
| 309 | + // AND messages are different from each other |
| 310 | + assertTrue(cancellation.message != failedToLoad.message) |
| 311 | + assertTrue(failedToLoad.message != failedToDecode.message) |
| 312 | + assertTrue(cancellation.message != failedToDecode.message) |
| 313 | + } |
| 314 | +} |
0 commit comments