Skip to content

Commit cd044dd

Browse files
committed
chore: refactor parts of code add rest of tests for ADB module
1 parent 1a1d671 commit cd044dd

File tree

4 files changed

+91
-34
lines changed

4 files changed

+91
-34
lines changed

MiniSim/Service/Adb.swift

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,22 @@
66
//
77

88
import Foundation
9-
import ShellOut
109

1110
protocol ADBProtocol {
1211
static var shell: ShellProtocol.Type { get set }
1312

1413
static func getAdbPath() throws -> String
1514
static func getEmulatorPath() throws -> String
16-
static func getAdbId(
17-
for deviceName: String,
18-
adbPath: String
19-
) throws -> String
15+
static func getAdbId(for deviceName: String) throws -> String
2016
static func checkAndroidHome(
2117
path: String,
2218
fileManager: FileManager
2319
) throws -> Bool
24-
static func isAccesibilityOn(
25-
deviceId: String,
26-
adbPath: String
27-
) -> Bool
20+
static func isAccesibilityOn(deviceId: String) -> Bool
21+
static func toggleAccesibility(deviceId: String)
2822
}
2923

3024
final class ADB: ADBProtocol {
31-
3225
static var shell: ShellProtocol.Type = Shell.self
3326

3427
static let talkbackOn = "com.google.android.marvin.talkback/com.google.android.marvin.talkback.TalkBackService"
@@ -89,10 +82,8 @@ final class ADB: ADBProtocol {
8982
try getAndroidHome() + Paths.emulator.rawValue
9083
}
9184

92-
static func getAdbId(
93-
for deviceName: String,
94-
adbPath: String
95-
) throws -> String {
85+
static func getAdbId(for deviceName: String) throws -> String {
86+
let adbPath = try Self.getAdbPath()
9687
let onlineDevices = try shell.execute(command: "\(adbPath) devices")
9788
let splitted = onlineDevices.components(separatedBy: "\n")
9889

@@ -116,9 +107,12 @@ final class ADB: ADBProtocol {
116107
throw DeviceError.deviceNotFound
117108
}
118109

119-
static func isAccesibilityOn(deviceId: String, adbPath: String) -> Bool {
110+
static func isAccesibilityOn(deviceId: String) -> Bool {
111+
guard let adbPath = try? Self.getAdbPath() else {
112+
return false
113+
}
120114
let shellCommand = "\(adbPath) -s \(deviceId) shell settings get secure enabled_accessibility_services"
121-
guard let result = try? shellOut(to: [shellCommand]) else {
115+
guard let result = try? shell.execute(command: shellCommand) else {
122116
return false
123117
}
124118

@@ -128,4 +122,16 @@ final class ADB: ADBProtocol {
128122

129123
return false
130124
}
125+
126+
static func toggleAccesibility(deviceId: String) {
127+
guard let adbPath = try? Self.getAdbPath() else {
128+
return
129+
}
130+
let a11yIsEnabled = Self.isAccesibilityOn(deviceId: deviceId)
131+
let value = a11yIsEnabled ? ADB.talkbackOff : ADB.talkbackOn
132+
let shellCmd = "\(adbPath) -s \(deviceId) shell settings put secure enabled_accessibility_services \(value)"
133+
134+
// Ignore the error if toggling a11y fails.
135+
_ = try? shell.execute(command: shellCmd)
136+
}
131137
}

MiniSim/Service/DeviceService.swift

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -385,21 +385,16 @@ extension DeviceService {
385385
return splitted
386386
.filter { !$0.isEmpty }
387387
.map { deviceName in
388-
let adbId = try? ADB.getAdbId(for: deviceName, adbPath: adbPath)
388+
let adbId = try? ADB.getAdbId(for: deviceName)
389389
return Device(name: deviceName, identifier: adbId, booted: adbId != nil, platform: .android)
390390
}
391391
}
392392

393393
static func toggleA11y(device: Device) throws {
394-
let adbPath = try ADB.getAdbPath()
395394
guard let adbId = device.identifier else {
396395
throw DeviceError.deviceNotFound
397396
}
398-
399-
let a11yIsEnabled = ADB.isAccesibilityOn(deviceId: adbId, adbPath: adbPath)
400-
let value = a11yIsEnabled ? ADB.talkbackOff : ADB.talkbackOn
401-
let shellCmd = "\(adbPath) -s \(adbId) shell settings put secure enabled_accessibility_services \(value)"
402-
_ = try? shellOut(to: shellCmd)
397+
ADB.toggleAccesibility(deviceId: adbId)
403398
}
404399

405400
static func sendText(device: Device, text: String) throws {

MiniSimTests/ADBTests.swift

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ final class ADBTests: XCTestCase {
4949
fileManager: FileManagerStub()
5050
)
5151
XCTAssertEqual(output, true)
52-
XCTAssertEqual(ShellStub.executedCommand, defaultHomePath + "/emulator/emulator")
53-
XCTAssertEqual(ShellStub.passedParameters, ["-list-avds"])
52+
XCTAssertEqual(ShellStub.lastExecutedCommand, defaultHomePath + "/emulator/emulator")
53+
XCTAssertEqual(ShellStub.lastPassedArguments, ["-list-avds"])
5454

5555
XCTAssertThrowsError(
5656
try ADB.checkAndroidHome(
@@ -73,4 +73,55 @@ final class ADBTests: XCTestCase {
7373
defaultHomePath + "/cmdline-tools/latest/bin/avdmanager"
7474
)
7575
}
76+
77+
func testGetAdbId() throws {
78+
ShellStub.mockedExecute = { command, _, _ in
79+
if command.contains("devices") {
80+
return """
81+
List of devices attached
82+
emulator-5554 device
83+
emulator-5556 device
84+
"""
85+
}
86+
87+
if command.contains("avd name") {
88+
return """
89+
Pixel_XL_API_32
90+
OK
91+
"""
92+
}
93+
94+
return ""
95+
}
96+
let adbId = try ADB.getAdbId(for: "Pixel_XL_API_32")
97+
98+
XCTAssertEqual(adbId, "emulator-5554")
99+
100+
XCTAssertThrowsError(
101+
try ADB.getAdbId(for: "Pixel_Not_Found")
102+
)
103+
}
104+
105+
func testIsAccesibilityOn() throws {
106+
var isA11yOn: Bool
107+
isA11yOn = ADB.isAccesibilityOn(deviceId: "emulator-5544")
108+
XCTAssertFalse(isA11yOn)
109+
110+
ShellStub.mockedExecute = { _, _, _ in
111+
"com.google.android.marvin.talkback/com.google.android.marvin.talkback.TalkBackService"
112+
}
113+
isA11yOn = ADB.isAccesibilityOn(deviceId: "emulator-5544")
114+
XCTAssertTrue(isA11yOn)
115+
}
116+
117+
func testToggle11y() {
118+
UserDefaults.standard.androidHome = "adbPath"
119+
let expectedCommand = """
120+
adbPath/platform-tools/adb -s emulator-5544 shell settings put secure enabled_accessibility_services com.google.android.marvin.talkback/com.google.android.marvin.talkback.TalkBackService
121+
"""
122+
123+
ADB.toggleAccesibility(deviceId: "emulator-5544")
124+
XCTAssertEqual(ShellStub.lastExecutedCommand, expectedCommand)
125+
XCTAssertEqual(ShellStub.lastPassedArguments, [])
126+
}
76127
}

MiniSimTests/Mocks/ShellStub.swift

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,25 @@ import Foundation
33
@testable import MiniSim
44

55
class ShellStub: ShellProtocol {
6-
private(set) static var executedCommand: String = ""
7-
private(set) static var passedParameters: [String] = []
8-
private(set) static var passedPath: String = ""
6+
private(set) static var lastExecutedCommand: String = ""
7+
private(set) static var lastPassedArguments: [String] = []
8+
private(set) static var lastPassedPath: String = ""
9+
static var mockedExecute: ((_ command: String, _ arguments: [String], _ atPath: String) -> String)?
910

1011
static func execute(command: String, arguments: [String], atPath: String) throws -> String {
11-
executedCommand = command
12-
passedParameters = arguments
13-
passedPath = atPath
12+
lastExecutedCommand = command
13+
lastPassedArguments = arguments
14+
lastPassedPath = atPath
15+
if let mockedExecute {
16+
return mockedExecute(command, arguments, atPath)
17+
}
1418
return ""
1519
}
1620

1721
static func tearDown() {
18-
executedCommand = ""
19-
passedParameters = []
20-
passedPath = ""
22+
lastExecutedCommand = ""
23+
lastPassedArguments = []
24+
lastPassedPath = ""
25+
mockedExecute = nil
2126
}
2227
}

0 commit comments

Comments
 (0)