-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
71c1f67
commit 5cd58f3
Showing
6 changed files
with
252 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,4 +90,4 @@ fastlane/test_output | |
|
||
iOSInjectionProject/ | ||
|
||
.DS_Store | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import Foundation | ||
import ShellOut | ||
|
||
protocol ShellProtocol { | ||
@discardableResult static func execute( | ||
command: String, | ||
arguments: [String], | ||
atPath: String | ||
) throws -> String | ||
} | ||
|
||
extension ShellProtocol { | ||
@discardableResult static func execute( | ||
command: String, | ||
arguments: [String] = [], | ||
atPath: String = "." | ||
) throws -> String { | ||
try execute(command: command, arguments: arguments, atPath: atPath) | ||
} | ||
} | ||
|
||
final class Shell: ShellProtocol { | ||
@discardableResult static func execute( | ||
command: String, | ||
arguments: [String] = [], | ||
atPath: String = "." | ||
) throws -> String { | ||
try shellOut( | ||
to: command, | ||
arguments: arguments, | ||
at: atPath | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import XCTest | ||
|
||
@testable import MiniSim | ||
|
||
class FileManagerStub: FileManager { | ||
override func fileExists(atPath path: String) -> Bool { | ||
true | ||
} | ||
} | ||
|
||
class FileManagerEmptyStub: FileManager { | ||
override func fileExists(atPath path: String) -> Bool { | ||
false | ||
} | ||
} | ||
|
||
final class ADBTests: XCTestCase { | ||
let savedAndroidHome = UserDefaults.standard.androidHome | ||
let defaultHomePath = "/Users/\(NSUserName())/Library/Android/sdk" | ||
|
||
override func setUp() { | ||
ADB.shell = ShellStub.self | ||
UserDefaults.standard.removeObject(forKey: UserDefaults.Keys.androidHome) | ||
} | ||
|
||
override func tearDown() { | ||
UserDefaults.standard.androidHome = savedAndroidHome | ||
ShellStub.tearDown() | ||
} | ||
|
||
func testGetAndroidHome() throws { | ||
let androidHome = try ADB.getAndroidHome() | ||
|
||
XCTAssertEqual(androidHome, defaultHomePath) | ||
|
||
UserDefaults.standard.androidHome = "customAndroidHome" | ||
let customAndroidHome = try ADB.getAndroidHome() | ||
|
||
XCTAssertEqual( | ||
customAndroidHome, | ||
"customAndroidHome", | ||
"Setting custom androidHome overrides default one" | ||
) | ||
} | ||
|
||
func testCheckAndroidHome() throws { | ||
let output = try ADB.checkAndroidHome( | ||
path: defaultHomePath, | ||
fileManager: FileManagerStub() | ||
) | ||
XCTAssertEqual(output, true) | ||
XCTAssertEqual(ShellStub.lastExecutedCommand, defaultHomePath + "/emulator/emulator") | ||
XCTAssertEqual(ShellStub.lastPassedArguments, ["-list-avds"]) | ||
|
||
XCTAssertThrowsError( | ||
try ADB.checkAndroidHome( | ||
path: defaultHomePath, | ||
fileManager: FileManagerEmptyStub() | ||
) | ||
) | ||
} | ||
|
||
func testGetUtilPaths() throws { | ||
let adbPath = try ADB.getAdbPath() | ||
let avdPath = try ADB.getAvdPath() | ||
|
||
XCTAssertEqual( | ||
adbPath, | ||
defaultHomePath + "/platform-tools/adb" | ||
) | ||
XCTAssertEqual( | ||
avdPath, | ||
defaultHomePath + "/cmdline-tools/latest/bin/avdmanager" | ||
) | ||
} | ||
|
||
func testGetAdbId() throws { | ||
ShellStub.mockedExecute = { command, _, _ in | ||
if command.contains("devices") { | ||
return """ | ||
List of devices attached | ||
emulator-5554 device | ||
emulator-5556 device | ||
""" | ||
} | ||
|
||
if command.contains("avd name") { | ||
return """ | ||
Pixel_XL_API_32 | ||
OK | ||
""" | ||
} | ||
|
||
return "" | ||
} | ||
let adbId = try ADB.getAdbId(for: "Pixel_XL_API_32") | ||
|
||
XCTAssertEqual(adbId, "emulator-5554") | ||
|
||
XCTAssertThrowsError( | ||
try ADB.getAdbId(for: "Pixel_Not_Found") | ||
) | ||
} | ||
|
||
func testIsAccesibilityOn() throws { | ||
var isA11yOn: Bool | ||
isA11yOn = ADB.isAccesibilityOn(deviceId: "emulator-5544") | ||
XCTAssertFalse(isA11yOn) | ||
|
||
ShellStub.mockedExecute = { _, _, _ in | ||
"com.google.android.marvin.talkback/com.google.android.marvin.talkback.TalkBackService" | ||
} | ||
isA11yOn = ADB.isAccesibilityOn(deviceId: "emulator-5544") | ||
XCTAssertTrue(isA11yOn) | ||
} | ||
|
||
func testToggle11y() { | ||
UserDefaults.standard.androidHome = "adbPath" | ||
let expectedCommand = """ | ||
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 | ||
""" | ||
|
||
ADB.toggleAccesibility(deviceId: "emulator-5544") | ||
XCTAssertEqual(ShellStub.lastExecutedCommand, expectedCommand) | ||
XCTAssertEqual(ShellStub.lastPassedArguments, []) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import Foundation | ||
|
||
@testable import MiniSim | ||
|
||
class ShellStub: ShellProtocol { | ||
private(set) static var lastExecutedCommand: String = "" | ||
private(set) static var lastPassedArguments: [String] = [] | ||
private(set) static var lastPassedPath: String = "" | ||
static var mockedExecute: ((_ command: String, _ arguments: [String], _ atPath: String) -> String)? | ||
|
||
static func execute(command: String, arguments: [String], atPath: String) throws -> String { | ||
lastExecutedCommand = command | ||
lastPassedArguments = arguments | ||
lastPassedPath = atPath | ||
if let mockedExecute { | ||
return mockedExecute(command, arguments, atPath) | ||
} | ||
return "" | ||
} | ||
|
||
static func tearDown() { | ||
lastExecutedCommand = "" | ||
lastPassedArguments = [] | ||
lastPassedPath = "" | ||
mockedExecute = nil | ||
} | ||
} |