-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
BulletPointSpanTest.kt
102 lines (87 loc) · 3.76 KB
/
BulletPointSpanTest.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
package com.android.example.text.styling.renderer.spans
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.text.Layout
import android.text.SpannableString
import android.text.Spanned
import org.junit.Assert.assertEquals
import org.junit.Test
import org.mockito.Matchers
import org.mockito.Matchers.eq
import org.mockito.Mockito.*
private const val GAP_WIDTH = 5
class BulletPointSpanTest {
private val canvas = mock(Canvas::class.java)
private val paint = mock(Paint::class.java)
private val text = SpannableString("text")
@Test
fun getLeadingMargin() {
// Given a span with a certain gap width
val span = BulletPointSpan(GAP_WIDTH, 0)
// Check that the margin is set correctly
val expectedMargin = (2 * BulletPointSpan.DEFAULT_BULLET_RADIUS + 2 * GAP_WIDTH).toInt()
assertEquals(expectedMargin.toLong(), span.getLeadingMargin(true).toLong())
}
@Test
fun drawLeadingMarginWithoutText() {
// Given a span
val span = BulletPointSpan(GAP_WIDTH, 0)
// When the leading margin is drawn but no text is set
span.drawLeadingMargin(canvas, paint, 0, 0, 0, 0, 0, text, 0, 0, true,
mock(Layout::class.java))
// Check that no drawing methods are called
verifyZeroInteractions(canvas)
verifyZeroInteractions(paint)
}
@Test
fun drawLeadingMarginHardwareAccelerated() {
val x = 10
val dir = 15
val top = 5
val bottom = 7
val color = Color.RED
// Given a span that is set on a text
val span = BulletPointSpan(GAP_WIDTH, color)
text.setSpan(span, 0, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
`when`(canvas.isHardwareAccelerated).thenReturn(true)
// When the leading margin is drawn
span.drawLeadingMargin(canvas, paint, x, dir, top, 0, bottom, text, 0, 0, true,
mock(Layout::class.java))
// Check that the correct canvas and paint methods are called, in the correct order
val inOrder = inOrder(canvas, paint)
inOrder.verify(paint).color = color
inOrder.verify(paint).style = eq<Paint.Style>(Paint.Style.FILL)
inOrder.verify(canvas).translate(
eq(GAP_WIDTH.toFloat() + x.toFloat() + dir * BulletPointSpan.DEFAULT_BULLET_RADIUS),
eq((top + bottom) / 2f))
inOrder.verify(canvas).drawPath(Matchers.any(Path::class.java), eq(paint))
}
@Test
fun drawLeadingMarginNotHardwareAccelerated() {
val x = 10
val dir = 15
val top = 5
val bottom = 7
val color = Color.RED
// Given a span that is set on a text
val span = BulletPointSpan(GAP_WIDTH, color)
text.setSpan(span, 0, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
`when`(canvas.isHardwareAccelerated).thenReturn(false)
// When the leading margin is drawn
span.drawLeadingMargin(canvas, paint, x, dir, top, 0, bottom, text, 0, 0, true,
mock(Layout::class.java))
// Check that the correct canvas and paint methods are called, in the correct order
val inOrder = inOrder(canvas, paint)
inOrder.verify(paint).color = color
inOrder.verify(paint).style = eq<Paint.Style>(Paint.Style.FILL)
inOrder.verify(canvas).drawCircle(eq(GAP_WIDTH.toFloat() + x.toFloat()
+ dir * BulletPointSpan.DEFAULT_BULLET_RADIUS),
eq((top + bottom) / 2f), eq(BulletPointSpan.DEFAULT_BULLET_RADIUS), eq(paint))
verify(canvas, never()).save()
verify(canvas, never()).translate(
eq(GAP_WIDTH.toFloat() + x.toFloat() + dir * BulletPointSpan.DEFAULT_BULLET_RADIUS),
eq((top + bottom) / 2f))
}
}