This repository was archived by the owner on Nov 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 562
Expand file tree
/
Copy pathAdapterViewTest.kt
More file actions
217 lines (194 loc) · 8.16 KB
/
AdapterViewTest.kt
File metadata and controls
217 lines (194 loc) · 8.16 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package androidx.core.widget
import android.support.test.InstrumentationRegistry
import android.widget.AbsListView.CHOICE_MODE_SINGLE
import android.widget.AdapterView
import android.widget.AdapterView.INVALID_POSITION
import android.widget.AdapterView.INVALID_ROW_ID
import android.widget.ArrayAdapter
import android.widget.ListView
import android.widget.Spinner
import androidx.core.view.get
import androidx.testutils.assertThrows
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import java.lang.ClassCastException
import java.lang.reflect.InvocationTargetException
class AdapterViewTest {
private val context = InstrumentationRegistry.getContext()
private val data = listOf("KitKat", "Lollipop", "Marshmallow", "Nougat", "Oreo")
private val arrayAdapter: ArrayAdapter<String>
get() = ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, data)
.apply { setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) }
private val listView: ListView
get() = ListView(context).apply { adapter = arrayAdapter; choiceMode = CHOICE_MODE_SINGLE }
private val spinner: Spinner
get() = Spinner(context).apply { adapter = arrayAdapter }
private var testItem: Any? = null
private var testOnNothingSelectedTriggered = false
@Before
fun setup() {
testItem = null
testOnNothingSelectedTriggered = false
}
@Test
fun onItemClick() {
val adapterView = listView
adapterView.onItemClick { item: String -> testItem = item }
for (position in data.indices) {
assertTrue(
"listener not set",
adapterView.performItemClick(null, position, INVALID_ROW_ID)
)
assertEquals(data[position], testItem)
}
}
@Test(expected = ClassCastException::class)
fun onItemClickCastExceptionOnWrongClass() {
val adapterView = listView
adapterView.onItemClick { item: WrongClass -> testItem = item }
adapterView.performItemClick(null, 1, INVALID_ROW_ID)
}
@Test(expected = RuntimeException::class)
fun onItemClickRuntimeExceptionWithSpinner() {
spinner.onItemClick { _: Any? -> }
}
/**
* borrowed from [AdapterViewTest line:279](https://android.googlesource.com/platform/cts/+/42fbcbb2518ea10cc729c44614a93b182bf58696/tests/tests/widget/src/android/widget/cts/AdapterViewTest.java#279)
*/
@Test
fun onItemLongClick() {
val adapterView = listView
adapterView.onItemLongClick { item: String -> testItem = item; true }
adapterView.layout(0, 0, LAYOUT_WIDTH, LAYOUT_HEIGHT)
val position = 1
adapterView.showContextMenuForChild(adapterView[position])
assertEquals(data[position], testItem)
}
@Test(expected = ClassCastException::class)
fun onItemLongClickCastExceptionOnWrongClass() {
val adapterView = listView
adapterView.onItemLongClick { item: WrongClass -> testItem = item; true }
adapterView.layout(0, 0, LAYOUT_WIDTH, LAYOUT_HEIGHT)
adapterView.showContextMenuForChild(adapterView[1])
}
@Test
fun onItemSelected() {
listOf(listView, spinner).forEach { adapterView ->
adapterView.onItemSelected { parent, _, position, _ ->
testItem = parent.getItemAtPosition(position)
}
for (i in data.indices) checkSelectionForPosition(adapterView, i)
}
}
@Test
fun onItemSelectedWithCast() {
listOf(listView, spinner).forEach { adapterView ->
adapterView.onItemSelected { item: String -> testItem = item }
for (i in data.indices) checkSelectionForPosition(adapterView, i)
}
}
@Test
fun onItemSelectedWithCastIgnoresOnNothingSelectedActions() {
listOf(listView, spinner).forEach { adapterView ->
adapterView.onItemSelected { item: String -> testItem = item }
assertFalse(testOnNothingSelectedTriggered)
assertNull(testItem)
selectAndFireOnSelected(adapterView, INVALID_POSITION)
assertFalse(testOnNothingSelectedTriggered)
assertNull(testItem)
}
}
@Test
fun onItemSelectedWithCastExceptionOnWrongClass() {
listOf(listView, spinner).forEach { adapterView ->
adapterView.onItemSelected<WrongClass> { item -> testItem = item }
assertThrows<ClassCastException> {
for (i in data.indices) checkSelectionForPosition(adapterView, i)
}
}
}
@Test
fun onItemSelectedWithHandledOnNothingSelected() {
listOf(listView, spinner).forEach { adapterView ->
adapterView.onItemSelected(
onNothingSelected = { _: AdapterView<*> -> testOnNothingSelectedTriggered = true },
onItemSelected = { parent, _, position, _ ->
testItem = parent.getItemAtPosition(position)
})
checkSelectionForPosition(adapterView, INVALID_POSITION)
for (i in data.indices) checkSelectionForPosition(adapterView, i)
checkSelectionForPosition(adapterView, INVALID_POSITION)
}
}
@Test
fun onItemSelectedWithCastWithHandledOnNothingSelected() {
listOf(listView, spinner).forEach { adapterView ->
adapterView.onItemSelected(
onNothingSelected = { testOnNothingSelectedTriggered = true },
onItemSelected = { item: String -> testItem = item }
)
checkSelectionForPosition(adapterView, INVALID_POSITION)
for (i in data.indices) checkSelectionForPosition(adapterView, i)
checkSelectionForPosition(adapterView, INVALID_POSITION)
}
}
private fun checkSelectionForPosition(adapterView: AdapterView<*>, position: Int) {
assertFalse(testOnNothingSelectedTriggered)
assertNull(testItem)
selectAndFireOnSelected(adapterView, position)
if (position < 0) {
assertTrue(testOnNothingSelectedTriggered)
assertNull(testItem)
} else {
assertFalse(testOnNothingSelectedTriggered)
assertEquals(data[position], testItem)
}
testOnNothingSelectedTriggered = false
testItem = null
}
companion object {
private const val LAYOUT_WIDTH = 200
private const val LAYOUT_HEIGHT = 200
class WrongClass
/**
* Reflection used to shortcut trigger selection via AdapterView#fireOnSelected()
*
* More comprehensive test would involve ActivityRule like in [AdapterViewTest line:286](https://android.googlesource.com/platform/cts/+/42fbcbb2518ea10cc729c44614a93b182bf58696/tests/tests/widget/src/android/widget/cts/AdapterViewTest.java#286)
*
* @see android.widget.AdapterView
*/
private fun selectAndFireOnSelected(adapterView: AdapterView<*>, position: Int) {
try {
AdapterView::class.java
.getDeclaredMethod("setNextSelectedPositionInt", Int::class.java)
.apply { isAccessible = true }
.invoke(adapterView, position)
AdapterView::class.java
.getDeclaredMethod("fireOnSelected")
.apply { isAccessible = true }
.invoke(adapterView)
} catch (e: InvocationTargetException) {
throw e.targetException
}
}
}
}