-
Notifications
You must be signed in to change notification settings - Fork 114
/
HtmlAttributeStyleColorTest.kt
138 lines (124 loc) · 5.47 KB
/
HtmlAttributeStyleColorTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
@file:Suppress("DEPRECATION")
package org.wordpress.aztec
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import junit.framework.Assert
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
import org.wordpress.aztec.spans.IAztecAttributedSpan
/**
* Tests covering the Html style attribute - *color* property.
* Example:
* <pre><code>
* <b style="color:red">Color me red!</b>
* </code></pre>
* Tests the [ForegroundColorSpan] is properly added to various [IAztecAttributedSpan]s that
* cover the two places where these events happen:
* 1. [Html] during the [Html.fromHtml] processing.
* 2. [AztecTagHandler] while processing more complex tags.
*
* Also tests invalid html style attribute color properties.
*/
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [24, 25])
class HtmlAttributeStyleColorTest {
private val HTML_BOLD_STYLE_COLOR = "<b style=\"color:blue;\">Blue</b>"
private val HTML_BOLD_STYLE_INVALID = "<b style=\"color:@java;\">Blue</b>"
private val HTML_STRIKE_STYLE_COLOR = "<s style=\"color:red;\">Red</s>"
private val HTML_STRIKE_STYLE_INVALID = "<s style=\"color:\">Red</s>"
private val HTML_UL_ITEM_ONLY_COLOR = "<ul><li>No Color</li><li style=\"color:@black;\">Black</li></ul>"
private val HTML_OL_WHOLE_LIST_COLOR = "<ol style=\"color:#FF00FF00;\"><li>Green</li><li>Still green</li></ol>"
private lateinit var parser: AztecParser
@Before
fun init() {
parser = AztecParser(AlignmentRendering.SPAN_LEVEL)
}
/**
* Nested tag test.
* Test only the second list item has a [ForegroundColorSpan].
*/
@Test
fun formatNestedListItemColor() {
val input = HTML_UL_ITEM_ONLY_COLOR
val span = SpannableString(parser.fromHtml(input, RuntimeEnvironment.application.applicationContext))
//
// Verify only a single [ForegroundColorSpan] exists
val colorSpan = span.getSpans(6, span.length, ForegroundColorSpan::class.java)
Assert.assertEquals(1, colorSpan.count())
//
// Verify the [ForegroundColorSpan] only covers the text of the single
// list item.
val startTextIndex = span.indexOf("Black", 0, true)
val colorSpanStart = span.getSpanStart(colorSpan[0])
val colorSpanEnd = span.getSpanEnd(colorSpan[0])
Assert.assertEquals(startTextIndex, colorSpanStart)
Assert.assertEquals(startTextIndex + "Black".length, colorSpanEnd)
}
/**
* Nested tag test.
* Test the whole list has a [ForegroundColorSpan].
*/
@Test
fun formatNestedListColor() {
val input = HTML_OL_WHOLE_LIST_COLOR
val span = SpannableString(parser.fromHtml(input, RuntimeEnvironment.application.applicationContext))
//
// Verify only a single [ForegroundColorSpan] exists
val colorSpan = span.getSpans(0, span.length, ForegroundColorSpan::class.java)
Assert.assertEquals(1, colorSpan.count())
//
// Verify the [ForegroundColorSpan] covers the entire length of the list span
val colorSpanStart = span.getSpanStart(colorSpan[0])
val colorSpanEnd = span.getSpanEnd(colorSpan[0])
Assert.assertEquals(0, colorSpanStart)
Assert.assertEquals(colorSpanEnd, span.length)
}
/**
* Test color styles applied by the [Html] class. A [ForegroundColorSpan] should
* be created for the valid color style property.
*/
@Test
fun formatBoldStyleColorAttributeValid() {
val input = HTML_BOLD_STYLE_COLOR
val span = SpannableString(parser.fromHtml(input, RuntimeEnvironment.application.applicationContext))
val colorSpanCount = span.getSpans(0, span.length, ForegroundColorSpan::class.java).count()
Assert.assertEquals(1, colorSpanCount)
}
/**
* Test color styles applied by the [Html] class. Since the color property is invalid,
* no [ForegroundColorSpan] should exist inside the span.
*/
@Test
fun formatBoldStyleColorAttributeInvalid() {
val input = HTML_BOLD_STYLE_INVALID
val span = SpannableString(parser.fromHtml(input, RuntimeEnvironment.application.applicationContext))
val colorSpanCount = span.getSpans(0, span.length, ForegroundColorSpan::class.java).count()
Assert.assertEquals(0, colorSpanCount)
}
/**
* Test color styles applied by the [AztecTagHandler] class. A [ForegroundColorSpan] should
* be created for the valid color style property.
*/
@Test
fun formatStrikeStyleColorAttributeValid() {
val input = HTML_STRIKE_STYLE_COLOR
val span = SpannableString(parser.fromHtml(input, RuntimeEnvironment.application.applicationContext))
val colorSpanCount = span.getSpans(0, span.length, ForegroundColorSpan::class.java).count()
Assert.assertEquals(1, colorSpanCount)
}
/**
* Test color styles applied by the [AztecTagHandler] class. Since the color property is invalid,
* no [ForegroundColorSpan] should exist inside the span.
*/
@Test
fun formatStrikeStyleColorAttributeInvalid() {
val input = HTML_STRIKE_STYLE_INVALID
val span = SpannableString(parser.fromHtml(input, RuntimeEnvironment.application.applicationContext))
val colorSpanCount = span.getSpans(0, span.length, ForegroundColorSpan::class.java).count()
Assert.assertEquals(0, colorSpanCount)
}
}