-
Notifications
You must be signed in to change notification settings - Fork 690
/
Listener.kt
46 lines (32 loc) · 1.02 KB
/
Listener.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
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import kotlin.properties.Delegates
interface TextChangedListener {
fun onTextChanged(oldText: String, newText: String)
}
class PrintingTextChangedListener : TextChangedListener {
var text = ""
override fun onTextChanged(oldText: String, newText: String) {
text = "Text is changed: $oldText -> $newText"
}
}
class TextView {
val listeners = mutableListOf<TextChangedListener>()
var text: String by Delegates.observable("<empty>") { _, old, new ->
listeners.forEach { it.onTextChanged(old, new) }
}
}
class ListenerTest {
@Test
fun Listener() {
val listener = PrintingTextChangedListener()
val textView = TextView().apply {
listeners.add(listener)
}
with(textView) {
text = "Lorem ipsum"
text = "dolor sit amet"
}
assertThat(listener.text).isEqualTo("Text is changed: Lorem ipsum -> dolor sit amet")
}
}