Skip to content

Commit a910c39

Browse files
authored
Merge pull request #11 from spring-media/feature/support_Android_9_10
Feature/support android 9 and 10
2 parents ace58b6 + 55a9ec5 commit a910c39

File tree

4 files changed

+59
-25
lines changed

4 files changed

+59
-25
lines changed

src/main/kotlin/internal/Constants.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import org.gradle.api.GradleException
66
object ShellCommands {
77
const val DUMPSYS_INPUT_METHOD = "dumpsys input_method"
88
const val DUMPSYS_WINDOW = "dumpsys window"
9+
const val DUMPSYS_WINDOW_DISPLAYS = "dumpsys window displays"
910
const val DUMPSYS_WIFI = "dumpsys wifi"
1011
const val INPUT_WAKE_UP_CALL = "input keyevent 224"
1112
const val INPUT_SLEEP_CALL = "input keyevent 223"

src/main/kotlin/internal/DeviceWrapper.kt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import internal.ShellCommands.CHANGE_LANGUAGE_VIA_APP
88
import internal.ShellCommands.DUMPSYS_INPUT_METHOD
99
import internal.ShellCommands.DUMPSYS_WIFI
1010
import internal.ShellCommands.DUMPSYS_WINDOW
11+
import internal.ShellCommands.DUMPSYS_WINDOW_DISPLAYS
1112
import internal.ShellCommands.SETTINGS_GET_ANDROID_ID
1213
import internal.ShellCommands.SETTINGS_GET_GLOBAL
1314
import internal.ShellCommands.SETTINGS_GET_STAY_ON
@@ -32,7 +33,14 @@ class DeviceWrapper(val device: IDevice, val outputReceiverProvider: OutputRecei
3233
}
3334

3435
fun getDeviceScreenResolution(): ScreenResolution {
35-
val output = analyzeOutputOfShellCommandByRegex(DUMPSYS_WINDOW, "mUnrestrictedScreen.*?(\\d+)x(\\d+)")
36+
val output: Matcher = try {
37+
analyzeOutputOfShellCommandByRegex(DUMPSYS_WINDOW, "mUnrestrictedScreen.*?(\\d+)x(\\d+)")
38+
}
39+
catch (e: GradleException) {
40+
// Android 9 & 10
41+
analyzeOutputOfShellCommandByRegex(DUMPSYS_WINDOW_DISPLAYS, "mUnrestricted.*?\\[.*\\]\\[(\\d+),(\\d+)\\]")
42+
}
43+
3644
val screenWidth = output.group(1).trim().toInt()
3745
val screenHeight = output.group(2).trim().toInt()
3846
return ScreenResolution(xValue = screenWidth, yValue = screenHeight)
@@ -44,10 +52,16 @@ class DeviceWrapper(val device: IDevice, val outputReceiverProvider: OutputRecei
4452
}
4553

4654
fun checkWifi(wifi: String) {
47-
val output = analyzeOutputOfShellCommandByRegex(DUMPSYS_WIFI, "mNetworkInfo .+ extra: \"(.+)\"")
55+
val output: Matcher = try {
56+
analyzeOutputOfShellCommandByRegex(DUMPSYS_WIFI, "mNetworkInfo .+ extra: \"(.+)\"")
57+
}
58+
catch (e: GradleException) {
59+
// Android 9 & 10
60+
analyzeOutputOfShellCommandByRegex(DUMPSYS_WIFI, "mWifiInfo\\s+SSID:\\s+(.+?),")
61+
}
4862
val currentWifi = output.group(1)
4963
if (currentWifi != wifi) {
50-
throw GradleException("Device ${getDetails()} is not connected to wifi with name $wifi")
64+
throw GradleException("Device ${getDetails()} is not connected to WiFi named $wifi. Current WiFi is: $currentWifi")
5165
}
5266
}
5367

src/test/kotlin/unitTest/tasks/CheckWifiTaskUnitTest.kt

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,16 @@ class CheckWifiTaskUnitTest : BaseUnitTest() {
2929
val bridge: AndroidDebugBridge = mock()
3030
val outputReceiver: CollectingOutputReceiver = mock()
3131
val outputReceiverProvider: OutputReceiverProvider = mock()
32-
val defaultPluginTask: DefaultPluginTask = mock()
3332

3433
val deviceCommunicator = DeviceCommunicator(bridge, outputReceiverProvider)
35-
val noDevices = emptyArray<IDevice>()
36-
val wifi = "wifi"
34+
val wifi = "wlanName"
3735
val devices = arrayOf(device)
3836
val mNetworkInfo = "mNetworkInfo [type: WIFI[], extra: \"$wifi\"]"
37+
val wifiDumpsysAndroid9 = "mWifiInfo SSID: $wifi, BSSID: 50:06:04:50:06:04, MAC: a8:db:03:a8:db:03, Supplicant state: COMPLETED, RSSI: -62, Link speed: 270Mbps, Frequency: 5180MHz, Net ID: 2, Metered hint: false, GigaAp: false, VenueName: null, WifiMode: 4, score: 60\n" +
38+
"mDhcpResults IP address xxx.xx.xx.xx/20 Gateway xxx.xx.xx.xx DNS servers: [ xxx.xx.xx.xx 8.8.8.8 8.8.4.4 ] Domains asv.cor DHCP server /xxx.xx.xx.xx Vendor info null lease 1800 seconds\n" +
39+
"mNetworkInfo [type: WIFI[], state: CONNECTED/CONNECTED, reason: (unspecified), extra: (none), failover: false, available: true, roaming: false]\n"
40+
41+
3942

4043
lateinit var projectDir: File
4144
lateinit var project: Project
@@ -53,31 +56,39 @@ class CheckWifiTaskUnitTest : BaseUnitTest() {
5356
task.wifi = wifi
5457

5558
given(outputReceiverProvider.get()).willReturn(outputReceiver)
59+
given(bridge.devices).willReturn(devices)
60+
5661
}
5762

5863
@Test(expected = GradleException::class)
5964
fun `throw gradle exception when string in extension is empty`() {
6065
task.wifi = ""
61-
given(bridge.devices).willReturn(devices)
6266

6367
task.runTask1()
6468
}
6569

6670
@Test(expected = GradleException::class)
6771
fun `throw gradle exception when string in extension is blank`() {
6872
task.wifi = " "
69-
given(bridge.devices).willReturn(devices)
7073

7174
task.runTask1()
7275
}
7376

7477
@Test
7578
fun `no gradle exception is thrown when connected to right wifi`() {
76-
given(bridge.devices).willReturn(devices)
7779
given(outputReceiver.output).willReturn(mNetworkInfo)
7880

7981
task.runTask2(device)
8082

8183
thenDeviceShouldGetDetails(device)
8284
}
85+
86+
@Test
87+
fun `no gradle exception is thrown when connected to right wifi on Android 9 and 10`() {
88+
given(outputReceiver.output).willReturn(wifiDumpsysAndroid9)
89+
90+
task.runTask2(device)
91+
92+
thenDeviceShouldGetDetails(device)
93+
}
8394
}

src/test/kotlin/unitTest/tasks/internal/UnlockerTest.kt

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.android.ddmlib.CollectingOutputReceiver
99
import com.android.ddmlib.IDevice
1010
import com.nhaarman.mockito_kotlin.*
1111
import internal.OutputReceiverProvider
12+
import internal.ShellCommands.DUMPSYS_WINDOW_DISPLAYS
1213
import org.gradle.api.GradleException
1314
import org.junit.Before
1415
import org.junit.Test
@@ -31,10 +32,20 @@ class UnlockerTest {
3132
val wrongPin1 = "111"
3233
val wrongPin2 = "11aa"
3334
val mUnrestrictedScreen = "mUnrestrictedScreen=(0,0) 100x200"
35+
val mUnrestrictedAndroid9 = "mUnrestricted=[0,0][100,200]"
3436
val mFocusedWindow = "mFocusedWindow=Window{7045664 u0 StatusBar}"
37+
38+
3539
val output = "$mUnrestrictedScreen + $mFocusedWindow"
3640
val wrongMethod = "wrong method"
3741

42+
val classToTestSwipe = Unlocker(
43+
deviceWrapper,
44+
SWIPE.string,
45+
pin,
46+
password
47+
)
48+
3849
@Before
3950
fun setup() {
4051
given(outputReceiverProvider.get()).willReturn(outputReceiver)
@@ -56,33 +67,30 @@ class UnlockerTest {
5667

5768
@Test
5869
fun `device can be unlocked by swipe`() {
59-
val classToTest = Unlocker(
60-
deviceWrapper,
61-
SWIPE.string,
62-
pin,
63-
password
64-
)
65-
6670
given(outputReceiver.output).willReturn(mUnrestrictedScreen)
6771

68-
classToTest.unlock()
72+
classToTestSwipe.unlock()
73+
74+
then(device).should().executeShellCommand(eq(DUMPSYS_WINDOW), any())
75+
then(device).should().executeShellCommand(eq("input swipe 50 160 80 40"), any())
76+
}
77+
78+
@Test
79+
fun `device with Android 9 can be unlocked by swipe`() {
80+
given(outputReceiver.output).willReturn(mUnrestrictedAndroid9)
81+
82+
classToTestSwipe.unlock()
6983

7084
then(device).should().executeShellCommand(eq(DUMPSYS_WINDOW), any())
85+
then(device).should().executeShellCommand(eq(DUMPSYS_WINDOW_DISPLAYS), any())
7186
then(device).should().executeShellCommand(eq("input swipe 50 160 80 40"), any())
7287
}
7388

7489
@Test(expected = GradleException::class)
7590
fun `gradle exception is thrown when resolution cannot be retrieved from device`() {
76-
val classToTest = Unlocker(
77-
deviceWrapper,
78-
SWIPE.string,
79-
pin,
80-
password
81-
)
82-
8391
given(outputReceiver.output).willReturn(emptyString)
8492

85-
classToTest.unlock()
93+
classToTestSwipe.unlock()
8694
}
8795

8896
@Test(expected = GradleException::class)

0 commit comments

Comments
 (0)