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 560
Expand file tree
/
Copy pathViewTest.kt
More file actions
354 lines (293 loc) · 9.41 KB
/
ViewTest.kt
File metadata and controls
354 lines (293 loc) · 9.41 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
* Copyright (C) 2017 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.view
import android.graphics.Bitmap
import android.graphics.Color
import android.os.SystemClock
import android.support.test.InstrumentationRegistry
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.RelativeLayout
import androidx.core.kotlin.test.R
import androidx.testutils.assertThrows
import androidx.testutils.fail
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertSame
import org.junit.Assert.assertTrue
import org.junit.Test
class ViewTest {
private val context = InstrumentationRegistry.getContext()
private val view = View(context)
private fun obtainMotionEvent(eventType: Int) = MotionEvent.obtain(
SystemClock.uptimeMillis(),
SystemClock.uptimeMillis() + 100,
eventType,
0f,
0f,
0
)
@Test
fun doOnNextLayout() {
var calls = 0
view.doOnNextLayout {
calls++
}
view.layout(0, 0, 10, 10)
assertEquals(1, calls)
// Now layout again and make sure that the listener was removed
view.layout(0, 0, 10, 10)
assertEquals(1, calls)
}
@Test
fun doOnLayoutBeforeLayout() {
var called = false
view.doOnLayout {
called = true
}
view.layout(0, 0, 10, 10)
assertTrue(called)
}
@Test
fun doOnLayoutAfterLayout() {
view.layout(0, 0, 10, 10)
var called = false
view.doOnLayout {
called = true
}
assertTrue(called)
}
@Test
fun doOnLayoutWhileLayoutRequested() {
// First layout the view
view.layout(0, 0, 10, 10)
// Then later a layout is requested
view.requestLayout()
var called = false
view.doOnLayout {
called = true
}
// Assert that we haven't been called while the layout pass is pending
assertFalse(called)
// Now layout the view and assert that we're called
view.layout(0, 0, 20, 20)
assertTrue(called)
}
@Test
fun doOnPreDraw() {
var calls = 0
view.doOnPreDraw {
calls++
}
view.viewTreeObserver.dispatchOnPreDraw()
assertEquals(1, calls)
// Now dispatch again to make sure that the listener was removed
view.viewTreeObserver.dispatchOnPreDraw()
assertEquals(1, calls)
}
@Test
fun setPadding() {
view.setPadding(42)
assertEquals(42, view.paddingLeft)
assertEquals(42, view.paddingTop)
assertEquals(42, view.paddingRight)
assertEquals(42, view.paddingBottom)
}
@Test
fun updatePadding() {
view.updatePadding(top = 10, right = 20)
assertEquals(0, view.paddingLeft)
assertEquals(10, view.paddingTop)
assertEquals(20, view.paddingRight)
assertEquals(0, view.paddingBottom)
}
@Test
fun updatePaddingNoOp() {
view.setPadding(10, 20, 30, 40)
view.updatePadding()
assertEquals(10, view.paddingLeft)
assertEquals(20, view.paddingTop)
assertEquals(30, view.paddingRight)
assertEquals(40, view.paddingBottom)
}
@Test
fun updatePaddingRelative() {
view.updatePaddingRelative(start = 10, end = 20)
assertEquals(10, view.paddingStart)
assertEquals(0, view.paddingTop)
assertEquals(20, view.paddingEnd)
assertEquals(0, view.paddingBottom)
}
@Test
fun updatePaddingRelativeNoOp() {
view.setPaddingRelative(10, 20, 30, 40)
view.updatePaddingRelative()
assertEquals(10, view.paddingStart)
assertEquals(20, view.paddingTop)
assertEquals(30, view.paddingEnd)
assertEquals(40, view.paddingBottom)
}
@Test
fun toBitmapBeforeLayout() {
assertThrows<IllegalStateException> {
view.toBitmap()
}
}
@Test
fun toBitmap() {
view.layout(0, 0, 100, 100)
val bitmap = view.toBitmap()
assertEquals(100, bitmap.width)
assertEquals(100, bitmap.height)
}
@Test
fun toBitmapCustomConfig() {
view.layout(0, 0, 100, 100)
val bitmap = view.toBitmap(Bitmap.Config.RGB_565)
assertSame(Bitmap.Config.RGB_565, bitmap.config)
}
@Test
fun toBitmapScrolls() {
val scrollView = LayoutInflater.from(context)!!
.inflate(R.layout.test_bitmap_scrolls, null, false)
val size = 100
scrollView.measure(
View.MeasureSpec.makeMeasureSpec(size, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(size, View.MeasureSpec.EXACTLY))
scrollView.layout(0, 0, size, size)
val noScroll = scrollView.toBitmap()
assertEquals(Color.WHITE, noScroll.getPixel(0, 0))
assertEquals(Color.WHITE, noScroll.getPixel(size - 1, size - 1))
scrollView.scrollTo(0, size)
val scrolls = scrollView.toBitmap()
assertEquals(Color.BLACK, scrolls.getPixel(0, 0))
assertEquals(Color.BLACK, scrolls.getPixel(size - 1, size - 1))
}
@Test fun isVisible() {
view.isVisible = true
assertTrue(view.isVisible)
assertEquals(View.VISIBLE, view.visibility)
view.isVisible = false
assertFalse(view.isVisible)
assertEquals(View.GONE, view.visibility)
}
@Test fun isInvisible() {
view.isInvisible = true
assertTrue(view.isInvisible)
assertEquals(View.INVISIBLE, view.visibility)
view.isInvisible = false
assertFalse(view.isInvisible)
assertEquals(View.VISIBLE, view.visibility)
}
@Test fun isGone() {
view.isGone = true
assertTrue(view.isGone)
assertEquals(View.GONE, view.visibility)
view.isGone = false
assertFalse(view.isGone)
assertEquals(View.VISIBLE, view.visibility)
}
@Test fun updateLayoutParams() {
view.layoutParams = ViewGroup.LayoutParams(0, 0)
view.updateLayoutParams {
assertSame(view.layoutParams, this)
width = 500
height = 1000
}
assertEquals(500, view.layoutParams.width)
assertEquals(1000, view.layoutParams.height)
}
@Test fun updateLayoutParamsAsType() {
view.layoutParams = LinearLayout.LayoutParams(0, 0)
view.updateLayoutParams<LinearLayout.LayoutParams> {
assertSame(view.layoutParams, this)
weight = 2f
}
assertEquals(2f, (view.layoutParams as LinearLayout.LayoutParams).weight)
}
@Test fun updateLayoutParamsWrongType() {
assertThrows<ClassCastException> {
view.updateLayoutParams<RelativeLayout.LayoutParams> {
fail()
}
}
}
@Test fun announceForAccessibility() {
val testView = AccessibilityAnnouncementCapturingView(context)
testView.announceForAccessibility(R.string.text)
val resolvedText = context.getText(R.string.text)
assertEquals(testView.announcement, resolvedText)
}
@Test fun doOnActionDown() {
var called = false
view.doOnActionDown {
called = true
}
view.dispatchTouchEvent(obtainMotionEvent(MotionEvent.ACTION_DOWN))
assertTrue(called)
}
@Test fun doOnActionUp() {
var called = false
view.doOnActionUp {
called = true
}
view.dispatchTouchEvent(obtainMotionEvent(MotionEvent.ACTION_UP))
assertTrue(called)
}
@Test fun doOnActionMove() {
var called = false
view.doOnActionMove {
called = true
}
view.dispatchTouchEvent(obtainMotionEvent(MotionEvent.ACTION_MOVE))
assertTrue(called)
}
@Test fun doOnActionCancel() {
var called = false
view.doOnActionCancel {
called = true
}
view.dispatchTouchEvent(obtainMotionEvent(MotionEvent.ACTION_CANCEL))
assertTrue(called)
}
@Test fun doOnActionOutside() {
var called = false
view.doOnActionOutside {
called = true
}
view.dispatchTouchEvent(obtainMotionEvent(MotionEvent.ACTION_OUTSIDE))
assertTrue(called)
}
@Test fun doOnActionPointerDown() {
var called = false
view.doOnActionPointerDown {
called = true
}
view.dispatchTouchEvent(obtainMotionEvent(MotionEvent.ACTION_POINTER_DOWN))
assertTrue(called)
}
@Test fun doOnActionPointerUp() {
var called = false
view.doOnActionPointerUp {
called = true
}
view.dispatchTouchEvent(obtainMotionEvent(MotionEvent.ACTION_POINTER_UP))
assertTrue(called)
}
}