Skip to content

Commit 5838fb5

Browse files
committed
[Bug] Fix crash and add unit test to cover the issue.
1 parent c2e08fa commit 5838fb5

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-2
lines changed

aztec/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ dependencies {
6767
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0', {
6868
exclude group: 'com.android.support', module: 'support-annotations'
6969
}
70+
testImplementation "org.mockito:mockito-inline:$mockitoVersion"
71+
testImplementation "org.mockito.kotlin:mockito-kotlin:$mockitoKotlinVersion"
7072

7173
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlinCoroutinesVersion"
7274
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlinCoroutinesVersion"

aztec/src/main/kotlin/org/wordpress/aztec/spans/MarkSpan.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.wordpress.aztec.spans
33
import android.graphics.Color
44
import android.text.TextPaint
55
import android.text.style.CharacterStyle
6+
import androidx.annotation.VisibleForTesting
67
import org.wordpress.aztec.AztecAttributes
78
import org.wordpress.aztec.source.CssStyleFormatter
89

@@ -25,8 +26,9 @@ class MarkSpan : CharacterStyle, IAztecInlineSpan {
2526
textColorValue = safelyParseColor(colorString)
2627
}
2728

28-
private fun safelyParseColor(colorString: String?): Int? {
29-
if (colorString == null) {
29+
@VisibleForTesting
30+
internal fun safelyParseColor(colorString: String?): Int? {
31+
if (colorString.isNullOrBlank()) {
3032
return null
3133
}
3234
return try {
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.wordpress.aztec
2+
3+
import org.junit.Assert
4+
import org.junit.Test
5+
import org.junit.runner.RunWith
6+
import org.mockito.Mock
7+
import org.mockito.Mockito
8+
import org.robolectric.RobolectricTestRunner
9+
import org.wordpress.aztec.spans.MarkSpan
10+
11+
@RunWith(RobolectricTestRunner::class)
12+
class MarkSpanTest {
13+
@Mock
14+
val markSpan = Mockito.mock(MarkSpan::class.java)
15+
/**
16+
* Test used to confirm two crashes related are fixed.
17+
*
18+
* https://github.com/wordpress-mobile/WordPress-Android/issues/20738
19+
*/
20+
@Test
21+
fun `Calling MarkSpan#safelyParseColor with empty string should not cause a crash`() {
22+
var error = false
23+
var result: Int? = null
24+
try {
25+
Mockito.`when`(markSpan.safelyParseColor("")).thenCallRealMethod()
26+
result = markSpan.safelyParseColor("")
27+
} catch (e: Exception) {
28+
error = true
29+
}
30+
Assert.assertFalse(error)
31+
Assert.assertEquals(null, result)
32+
}
33+
34+
/**
35+
* Test used to confirm two crashes related are fixed.
36+
*
37+
* https://github.com/wordpress-mobile/WordPress-Android/issues/20694
38+
*/
39+
@Test
40+
fun `Calling MarkSpan#safelyParseColor with null string should not cause a crash`() {
41+
var error = false
42+
var result: Int? = null
43+
try {
44+
Mockito.`when`(markSpan.safelyParseColor(null)).thenCallRealMethod()
45+
result = markSpan.safelyParseColor(null)
46+
} catch (e: Exception) {
47+
error = true
48+
}
49+
Assert.assertFalse(error)
50+
Assert.assertEquals(null, result)
51+
}
52+
}

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ ext {
8080
jSoupVersion = '1.11.3'
8181
wordpressUtilsVersion = '3.5.0'
8282
espressoVersion = '3.0.1'
83+
mockitoVersion = '4.5.1'
84+
mockitoKotlinVersion = '4.1.0'
8385

8486
// other
8587
wordpressLintVersion = '2.0.0'

0 commit comments

Comments
 (0)