|
17 | 17 | package androidx.core.widget |
18 | 18 |
|
19 | 19 | import android.support.test.InstrumentationRegistry |
20 | | -import android.view.View |
21 | 20 | import android.widget.AdapterView |
22 | 21 | import android.widget.ArrayAdapter |
23 | 22 | import android.widget.ListView |
| 23 | +import android.widget.Spinner |
24 | 24 | 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.* |
27 | 27 | import org.junit.Before |
28 | 28 | import org.junit.Test |
29 | 29 | import java.lang.ClassCastException |
| 30 | +import java.lang.reflect.InvocationTargetException |
30 | 31 |
|
31 | 32 | class AdapterViewTest { |
32 | 33 |
|
33 | 34 | 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 |
37 | 48 |
|
38 | 49 | @Before |
39 | 50 | 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 |
48 | 53 | } |
49 | 54 |
|
50 | | - /** |
51 | | - * test that ext call is equal to call |
52 | | - * adapterView.setOnItemClickListener { _, _, position, _ -> |
53 | | - * itemUnderTest = adapterView.getItemAtPosition(position) as String |
54 | | - * } |
55 | | - */ |
56 | 55 | @Test |
57 | 56 | 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 | + } |
62 | 66 | } |
63 | 67 |
|
64 | | - @Test(expected = ClassCastException::class) |
| 68 | + @Test |
65 | 69 | 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 | + } |
70 | 75 | } |
71 | 76 |
|
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 |
79 | 84 | @Test |
80 | 85 | fun onItemLongClick() { |
81 | | - adapterView.onItemLongClick { item: String -> itemUnderTest = item; true } |
| 86 | + val adapterView = listView |
| 87 | + adapterView.onItemLongClick { item: String -> testItem = item; true } |
82 | 88 | 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) |
85 | 92 | } |
86 | 93 |
|
87 | 94 | @Test(expected = ClassCastException::class) |
88 | 95 | 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 } |
92 | 98 | adapterView.layout(0, 0, LAYOUT_WIDTH, LAYOUT_HEIGHT) |
93 | 99 | adapterView.showContextMenuForChild(adapterView[1]) |
94 | 100 | } |
95 | 101 |
|
96 | | - @Test() |
| 102 | + @Test |
97 | 103 | 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 | + } |
99 | 110 | } |
100 | 111 |
|
101 | | - @Test() |
| 112 | + @Test |
102 | 113 | 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 | + } |
104 | 128 | } |
105 | 129 |
|
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) |
109 | 153 | } |
110 | 154 |
|
111 | 155 | class WrongClass |
112 | 156 |
|
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 | + } |
119 | 189 |
|
120 | 190 | 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" |
126 | 191 | private const val LAYOUT_WIDTH = 200 |
127 | 192 | private const val LAYOUT_HEIGHT = 200 |
128 | 193 | } |
|
0 commit comments