-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
16 changed files
with
340 additions
and
194 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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// TODO Search adb exists in default folders before each command | ||
// TODO Check and execute adb commands in adb bin folder. |
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
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 |
---|---|---|
@@ -1,19 +1,25 @@ | ||
import { ADBChannel, ADBResultState, ADBResult } from '../adb-wrapper' | ||
import { | ||
ADBConnection, | ||
ADBResultState, | ||
ADBResult, | ||
ADBInterfaceError | ||
} from '../adb-wrapper' | ||
import { ConsoleInterfaceMock } from '../console/console-interface/console-interface-mock' | ||
// import { ConsoleInterface } from './../console-interface/console-interface' | ||
|
||
// Mocked ConsoleInterface | ||
const cimock = new ConsoleInterfaceMock() | ||
const adbInterfaceInstance = new ADBChannel(cimock) | ||
const adbInterfaceInstance = new ADBConnection(cimock) | ||
|
||
test('Test ADB Server has killed', async () => { | ||
cimock.setConsoleOutput('') | ||
const result = await adbInterfaceInstance.KillADBServer() | ||
const expected = new ADBResult(ADBResultState.Success, 'ADB Server killed') | ||
try { | ||
cimock.setConsoleOutput('') | ||
cimock.setConsoleOutput(`Android Debug Bridge`) | ||
|
||
const expected = await new ADBResult( | ||
ADBResultState.Success, | ||
'ADB Server killed' | ||
) | ||
|
||
expect(JSON.stringify(result)).toBe(JSON.stringify(expected)) | ||
const result = await adbInterfaceInstance.KillADBServer() | ||
expect(JSON.stringify(result)).toBe(JSON.stringify(expected)) | ||
} catch (e) { | ||
expect(e).toBeInstanceOf(ADBInterfaceError) | ||
} | ||
}) |
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 |
---|---|---|
@@ -1,18 +1,19 @@ | ||
import { ADBChannel, ADBResultState, ADBResult } from '../adb-wrapper' | ||
import { ADBConnection } from '../adb-wrapper' | ||
import { ConsoleInterfaceMock } from '../console/console-interface/console-interface-mock' | ||
// import { ConsoleInterface } from './../console-interface/console-interface' | ||
|
||
// Mocked ConsoleInterface | ||
const cimock = new ConsoleInterfaceMock() | ||
const adbInterfaceInstance = new ADBChannel(cimock) | ||
|
||
test('Test ADB Listed Devices', async () => { | ||
cimock.setConsoleOutput(`* daemon not running; starting now at tcp:5037 | ||
let cimock = new ConsoleInterfaceMock() | ||
let adbInterfaceInstance = new ADBConnection(cimock) | ||
cimock.setConsoleOutput(`Android Debug Bridge`) | ||
cimock.setConsoleOutput(`* starting now at tcp:5037 | ||
* daemon started successfully | ||
List of devices attached | ||
List of devices attached | ||
2ab7dcd77d04 unauthorized | ||
`) | ||
const result = await adbInterfaceInstance.FindConnectedDevices() | ||
cimock.setConsoleOutput(`PEAR_PHONE`) | ||
cimock.returnInfinity = true | ||
|
||
let result = await adbInterfaceInstance.FindConnectedDevices() | ||
|
||
expect(typeof result).toStrictEqual(typeof Array()) | ||
}) | ||
expect(result instanceof Array).toBe(true) | ||
}, 12000) |
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,36 @@ | ||
import * as os from 'os' | ||
import { ADBResolver, ADBNotFoundError } from './index' | ||
import { ConsoleInterfaceMock } from '../console/console-interface/console-interface-mock' | ||
const adbInEnvReturn = `Android Debug Bridge version 1.0.41 | ||
Version 29.0.1-5644136 | ||
Installed as C:\Users\vinic\AppData\Local\Android\Sdk\platform-tools\adb.exe | ||
global options:` | ||
|
||
test('Should return current home dir when path is present', async () => { | ||
const consoleMock = new ConsoleInterfaceMock() | ||
consoleMock.setConsoleOutput(adbInEnvReturn) | ||
const adbResolver = new ADBResolver(os.homedir(), os.type(), consoleMock) | ||
|
||
expect(await adbResolver.getDefaultADBPath()).toBe(os.homedir()) | ||
}) | ||
|
||
test('Throw error when nothing found', () => { | ||
try { | ||
const consoleMock = new ConsoleInterfaceMock() | ||
consoleMock.setConsoleOutput(`Invalid Return`) | ||
const adbResolver = new ADBResolver(os.homedir(), os.type(), consoleMock) | ||
} catch (error) { | ||
expect(error).toBeInstanceOf(ADBNotFoundError) | ||
} | ||
}) | ||
|
||
test('Should return path to adb', async () => { | ||
const consoleMock = new ConsoleInterfaceMock() | ||
consoleMock.returnInfinity = false | ||
consoleMock.setConsoleOutput(`Invalid Return`) | ||
consoleMock.setConsoleOutput(adbInEnvReturn) | ||
const adbResolver = new ADBResolver('yourhome', 'Linux', consoleMock) | ||
let adbPath = await adbResolver.getDefaultADBPath() | ||
expect(adbPath).toBe('yourhome/Android/Sdk') | ||
}) |
16 changes: 10 additions & 6 deletions
16
src/adb-shell-resolver/index.ts → src/adb-resolver/helper-functions.ts
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,62 @@ | ||
import * as path from 'path' | ||
import * as os from 'os' | ||
import { ConsoleInterface } from '../console/console-interface' | ||
import * as helperFunctions from './helper-functions' | ||
|
||
export class ADBResolver { | ||
homeDir: string | ||
osType: string | ||
consoleInterface: ConsoleInterface | ||
|
||
private readonly successADBReturn = 'Android Debug Bridge' | ||
|
||
constructor( | ||
homeDir: string, | ||
osType: string, | ||
consoleInterfaceInstance: ConsoleInterface | ||
) { | ||
this.homeDir = homeDir | ||
this.osType = osType | ||
this.consoleInterface = consoleInterfaceInstance | ||
} | ||
|
||
private async hasAndroidInEnv(): Promise<boolean> { | ||
const result = await this.consoleInterface.execConsoleSync('adb') | ||
return result.toLocaleString().includes(this.successADBReturn) | ||
} | ||
private returnDefaultADBPath(): string { | ||
return helperFunctions.getAndroidStudioPath({ | ||
osType: this.osType, | ||
homeDir: this.homeDir | ||
}) | ||
} | ||
|
||
private async hasPlatformToolsDefaultFolder(): Promise<boolean> { | ||
const result = await this.consoleInterface.execConsoleSync('adb', { | ||
cwd: this.returnDefaultADBPath() | ||
}) | ||
return result.toLocaleString().includes(this.successADBReturn) | ||
} | ||
|
||
public async getDefaultADBPath() { | ||
if (await this.hasAndroidInEnv()) { | ||
return os.homedir() | ||
} else if (await this.hasPlatformToolsDefaultFolder()) { | ||
return this.returnDefaultADBPath() | ||
} | ||
throw new ADBNotFoundError() | ||
} | ||
|
||
public async sendADBCommand(command: string): Promise<Buffer> { | ||
return this.consoleInterface.execConsoleSync(command, { | ||
cwd: await this.getDefaultADBPath() | ||
}) | ||
} | ||
} | ||
|
||
export class ADBNotFoundError extends Error { | ||
constructor(message = 'ADB not founded in this machine') { | ||
super(message) | ||
this.message = message | ||
} | ||
} |
Oops, something went wrong.