Skip to content
This repository was archived by the owner on Nov 14, 2018. It is now read-only.

Commit fafa216

Browse files
committed
add tests
1 parent c885c5c commit fafa216

1 file changed

Lines changed: 125 additions & 60 deletions

File tree

src/androidTest/java/androidx/core/widget/AdapterViewTest.kt

Lines changed: 125 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -17,112 +17,177 @@
1717
package androidx.core.widget
1818

1919
import android.support.test.InstrumentationRegistry
20-
import android.view.View
2120
import android.widget.AdapterView
2221
import android.widget.ArrayAdapter
2322
import android.widget.ListView
23+
import android.widget.Spinner
2424
import androidx.core.view.get
25-
import org.junit.Assert.assertEquals
26-
import org.junit.Assert.assertTrue
25+
import androidx.testutils.assertThrows
26+
import org.junit.Assert.*
2727
import org.junit.Before
2828
import org.junit.Test
2929
import java.lang.ClassCastException
30+
import java.lang.reflect.InvocationTargetException
3031

3132
class AdapterViewTest {
3233

3334
private val context = InstrumentationRegistry.getContext()
34-
private lateinit var adapterView: AdapterView<*>
35-
private lateinit var adapter: ArrayAdapter<String>
36-
private var itemUnderTest: String? = null
35+
36+
private val data = listOf("KitKat", "Lollipop", "Marshmallow", "Nougat", "Oreo")
37+
private val arrayAdapter: ArrayAdapter<String>
38+
get() = ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, data)
39+
.apply { setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) }
40+
41+
private val listView: ListView
42+
get() = ListView(context).apply { adapter = arrayAdapter }
43+
private val spinner: Spinner
44+
get() = Spinner(context).apply { adapter = arrayAdapter }
45+
46+
private var testItem: Any? = null
47+
private var testOnNothingSelectedFired = false
3748

3849
@Before
3950
fun setup() {
40-
adapterView = ListView(context)
41-
adapter = ArrayAdapter(
42-
context,
43-
android.R.layout.simple_list_item_1,
44-
listOf(K, L, M, N, O)
45-
)
46-
adapterView.adapter = adapter
47-
itemUnderTest = null
51+
testItem = null
52+
testOnNothingSelectedFired = false
4853
}
4954

50-
/**
51-
* test that ext call is equal to call
52-
* adapterView.setOnItemClickListener { _, _, position, _ ->
53-
* itemUnderTest = adapterView.getItemAtPosition(position) as String
54-
* }
55-
*/
5655
@Test
5756
fun onItemClick() {
58-
adapterView.onItemClick { item: String -> itemUnderTest = item }
59-
60-
assertTrue("listener not set", adapterView.performItemClick(null, 1, -1))
61-
assertEquals(L, itemUnderTest)
57+
val adapterView = listView
58+
adapterView.onItemClick { item: String -> testItem = item }
59+
for (position in data.indices) {
60+
assertTrue(
61+
"listener not set",
62+
adapterView.performItemClick(null, position, AdapterView.INVALID_ROW_ID)
63+
)
64+
assertEquals(data[position], testItem)
65+
}
6266
}
6367

64-
@Test(expected = ClassCastException::class)
68+
@Test
6569
fun onItemClickCastExceptionOnWrongClass() {
66-
@Suppress("ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE")
67-
var any: Any? = null
68-
adapterView.onItemClick { item: WrongClass -> any = item }
69-
adapterView.performItemClick(null, 1, -1)
70+
val adapterView = listView
71+
adapterView.onItemClick { item: WrongClass -> testItem = item }
72+
assertThrows<ClassCastException> {
73+
adapterView.performItemClick(null, 1, AdapterView.INVALID_ROW_ID)
74+
}
7075
}
7176

72-
/**
73-
* test that ext call is equal to call
74-
* adapterView.setOnItemLongClickListener { _, _, position, _ ->
75-
* itemUnderTest = adapterView.getItemAtPosition(position) as String
76-
* true
77-
* }
78-
*/
77+
78+
@Test(expected = RuntimeException::class)
79+
fun onItemClickRuntimeExceptionWithSpinner() {
80+
spinner.onItemClick { _: Any? -> }
81+
}
82+
83+
// borrowed from https://android.googlesource.com/platform/cts/+/master/tests/tests/widget/src/android/widget/cts/AdapterViewTest.java#255
7984
@Test
8085
fun onItemLongClick() {
81-
adapterView.onItemLongClick { item: String -> itemUnderTest = item; true }
86+
val adapterView = listView
87+
adapterView.onItemLongClick { item: String -> testItem = item; true }
8288
adapterView.layout(0, 0, LAYOUT_WIDTH, LAYOUT_HEIGHT)
83-
adapterView.showContextMenuForChild(adapterView[1])
84-
assertEquals(L, itemUnderTest)
89+
val position = 1
90+
adapterView.showContextMenuForChild(adapterView[position])
91+
assertEquals(data[position], testItem)
8592
}
8693

8794
@Test(expected = ClassCastException::class)
8895
fun onItemLongClickCastExceptionOnWrongClass() {
89-
@Suppress("ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE")
90-
var any: Any? = null
91-
adapterView.onItemLongClick { item: WrongClass -> any = item; true }
96+
val adapterView = listView
97+
adapterView.onItemLongClick { item: WrongClass -> testItem = item; true }
9298
adapterView.layout(0, 0, LAYOUT_WIDTH, LAYOUT_HEIGHT)
9399
adapterView.showContextMenuForChild(adapterView[1])
94100
}
95101

96-
@Test()
102+
@Test
97103
fun onItemSelected() {
98-
TODO()
104+
listOf(listView, spinner).forEach { adapterView ->
105+
adapterView.onItemSelected { parent, _, position, _ ->
106+
testItem = parent.getItemAtPosition(position)
107+
}
108+
for (i in data.indices) checkSelectionForPosition(adapterView, i)
109+
}
99110
}
100111

101-
@Test()
112+
@Test
102113
fun onItemSelectedWithCast() {
103-
TODO()
114+
listOf(listView, spinner).forEach { adapterView ->
115+
adapterView.onItemSelected { item: String -> testItem = item }
116+
for (i in data.indices) checkSelectionForPosition(adapterView, i)
117+
}
118+
}
119+
120+
@Test
121+
fun onItemSelectedWithCastExceptionOnWrongClass() {
122+
listOf(listView, spinner).forEach { adapterView ->
123+
adapterView.onItemSelected<WrongClass> { item -> testItem = item }
124+
assertThrows<ClassCastException> {
125+
checkSelectionForPosition(adapterView, 1)
126+
}
127+
}
104128
}
105129

106-
@Test()
107-
fun onItemSelectedCastExceptionOnWrongClass() {
108-
TODO()
130+
@Test
131+
fun onItemSelectedWithHandledOnNothingSelected() {
132+
val adapterView = spinner
133+
adapterView.onItemSelected(
134+
onNothingSelected = { _: AdapterView<*> -> testOnNothingSelectedFired = true },
135+
onItemSelected = { parent, _, position, _ ->
136+
testItem = parent.getItemAtPosition(position)
137+
})
138+
checkSelectionForPosition(adapterView, AdapterView.INVALID_POSITION)
139+
for (i in data.indices) checkSelectionForPosition(adapterView, i)
140+
checkSelectionForPosition(adapterView, AdapterView.INVALID_POSITION)
141+
}
142+
143+
@Test
144+
fun onItemSelectedWithCastWithHandledOnNothingSelected() {
145+
val adapterView = spinner
146+
adapterView.onItemSelected(
147+
onNothingSelected = { testOnNothingSelectedFired = true },
148+
onItemSelected = { item: String -> testItem = item }
149+
)
150+
checkSelectionForPosition(adapterView, AdapterView.INVALID_POSITION)
151+
for (i in data.indices) checkSelectionForPosition(adapterView, i)
152+
checkSelectionForPosition(adapterView, AdapterView.INVALID_POSITION)
109153
}
110154

111155
class WrongClass
112156

113-
data class OnSelectedActionMock(
114-
val parent: AdapterView<*>?,
115-
val view: View?,
116-
val position: Int,
117-
val id: Long
118-
)
157+
private fun checkSelectionForPosition(adapterView: AdapterView<*>, position: Int) {
158+
assertFalse(testOnNothingSelectedFired)
159+
assertNull(testItem)
160+
adapterView.setSelection(position)
161+
fireOnSelected(adapterView)
162+
if (position < 0) {
163+
assertTrue(testOnNothingSelectedFired)
164+
assertNull(testItem)
165+
} else {
166+
assertEquals(data[position], testItem)
167+
assertFalse(testOnNothingSelectedFired)
168+
}
169+
testItem = null
170+
testOnNothingSelectedFired = false
171+
}
172+
173+
/**
174+
* reflection used to test to trigger selection
175+
*
176+
* workaround for using ActivityRule like here: https://android.googlesource.com/platform/cts/+/master/tests/tests/widget/src/android/widget/cts/AdapterViewTest.java#286
177+
*/
178+
private fun fireOnSelected(adapterView: AdapterView<*>) {
179+
try {
180+
AdapterView::class.java.getDeclaredMethod("fireOnSelected")
181+
.apply {
182+
isAccessible = true
183+
invoke(adapterView)
184+
}
185+
} catch (e: InvocationTargetException) {
186+
throw e.targetException
187+
}
188+
}
119189

120190
companion object {
121-
private const val K = "KitKat"
122-
private const val L = "Lollipop"
123-
private const val M = "Marshmallow"
124-
private const val N = "Nougat"
125-
private const val O = "Oreo"
126191
private const val LAYOUT_WIDTH = 200
127192
private const val LAYOUT_HEIGHT = 200
128193
}

0 commit comments

Comments
 (0)