-
Notifications
You must be signed in to change notification settings - Fork 52
/
ScreenDimensions.kt
57 lines (48 loc) · 1.76 KB
/
ScreenDimensions.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
46
47
48
49
50
51
52
53
54
55
56
57
package com.example.android.architecture.blueprints.todoapp.test.chapter13
import android.support.test.InstrumentationRegistry
import android.support.test.uiautomator.UiDevice
import java.util.*
/**
* Calculates screen dimensions, navigation, status and action bars dimensions.
* Generates random coordinates for monkey clicks.
*/
object ScreenDimensions {
private val heightWithoutNavigationBar: Int
private var width = 0
private val uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
private val appContext = InstrumentationRegistry.getInstrumentation().targetContext
private val navBarResourceId =
appContext.resources.getIdentifier("navigation_bar_height", "dimen", "android")
private val statusBarResourceId =
appContext.resources.getIdentifier("status_bar_height", "dimen", "android")
init {
width = uiDevice.displayWidth
heightWithoutNavigationBar = uiDevice.displayHeight - ScreenDimensions.navigationBarHeight
}
/**
* Calculate navigation bar height.
*/
val navigationBarHeight : Int get() {
return if (navBarResourceId > 0) {
appContext.resources.getDimensionPixelSize(navBarResourceId)
} else {
0
}
}
/**
* Calculate status bar height.
*/
val statusBarHeight: Int get() {
return if (statusBarResourceId > 0) {
appContext.resources.getDimensionPixelSize(statusBarResourceId)
} else {
0
}
}
val randomY: Int
get() = (statusBarHeight..heightWithoutNavigationBar).random()
val randomX: Int
get() = (0..width).random()
private fun IntRange.random() =
Random().nextInt((endInclusive + 1) - start) + start
}