Skip to content

Commit

Permalink
v1.26.0
Browse files Browse the repository at this point in the history
Added tests passing correctly.
  • Loading branch information
vinicioslc committed Jun 7, 2020
1 parent 4c58dc8 commit 5b3d960
Show file tree
Hide file tree
Showing 16 changed files with 340 additions and 194 deletions.
2 changes: 2 additions & 0 deletions .todo.js
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.
22 changes: 13 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,31 @@
![GitHub](https://img.shields.io/github/license/vinicioslc/adb-interface-vscode?style=flat-square)
![Visual Studio Marketplace Installs](https://img.shields.io/visual-studio-marketplace/i/vinicioslc.adb-interface-vscode?style=flat-square)
![Visual Studio Marketplace Rating (Stars)](https://img.shields.io/visual-studio-marketplace/stars/vinicioslc.adb-interface-vscode?style=flat-square)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/308ff6c762d14642bdeb903dd7bfa552)](https://www.codacy.com/manual/vinicioslc/adb-interface-vscode?utm_source=github.com&utm_medium=referral&utm_content=vinicioslc/adb-interface-vscode&utm_campaign=Badge_Grade)
<a href="https://codeclimate.com/github/vinicioslc/adb-interface-vscode/maintainability">
<img src="https://api.codeclimate.com/v1/badges/b9fd814b1bdf974a1d16/maintainability" /></a>

# ADB Interface for VSCode

⚠️ WARN !! ONLY TESTED IN WINDOWS !! (For while, you must have ADB at your Enviroment Variables)
> ⚠️ WARN !! ONLY TESTED IN WINDOWS !! (For while, you must have ADB at your Enviroment Variables)
This are an simple ADB-Wrapper that makes possible to connect to an device over wifi connection without console use.

Help are welcome ;)

You can contribute with [Code](https://github.com/) or [Energy](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=TKRZ7F4FV4QY4&source=url)
You can contribute with
[Code](https://github.com/vinicioslc/adb-interface-vscode)
or
[Energy](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=TKRZ7F4FV4QY4&source=url)

This are an simple ADB-Wrapper that makes possible to connect to an device over wifi connection without console use.
[简体中文指南](https://www.jianshu.com/p/fb8eebc8a2c0)

## Features Todo/Done

- ☒ Kill ADB server

☒ Kills the adb server and all current connections

- ☐ ADB Resolver
- ☐ ADB Resolver - Find and resolve ADB Location

☒ Use default `ADB` in Enviroment Variable

Expand All @@ -38,9 +44,7 @@ This are an simple ADB-Wrapper that makes possible to connect to an device over

![status bar](media/record1.gif)

### Tests Health Summary

> Generated by jest-badge-generator
### Tests Health Summary by jest-badge-generator

![coverage-branches](https://raw.githubusercontent.com/vinicioslc/adb-interface-vscode/master/.badges/badge-branches.png)
![coverage-function](https://raw.githubusercontent.com/vinicioslc/adb-interface-vscode/master/.badges/badge-functions.png)
Expand All @@ -49,7 +53,7 @@ This are an simple ADB-Wrapper that makes possible to connect to an device over

## Available Commands

- ADB:📱 Reset connected devices port to :5555 (Open device port with `adb tcpip 5555`)
- ADB:📱 Reset connected devices port to :5555 (Open current device port with `adb tcpip 5555`)
- ADB:📱 Connect to device IP (need inform IP from device wanted `adb connect ${user_ip}:5555`)
- ADB:📱 Disconnect from any devices (Disconnect ever device attached `adb disconnect`)
- ADB:📱 Connect to device from List (Show an list from devices attached to connect)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"publisher": "vinicioslc",
"repository": "https://github.com/vinicioslc/adb-interface-vscode",
"engines": {
"vscode": "^1.25.0"
"vscode": "^1.26.0"
},
"icon": "media/icon.png",
"galleryBanner": {
Expand Down
56 changes: 22 additions & 34 deletions src/__tests__/connect-device-suite.test.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,32 @@
import { ADBChannel, ADBInterfaceException } from '../adb-wrapper'
import { ADBConnection, ADBInterfaceException } from '../adb-wrapper'
import { ConsoleInterfaceMock } from '../console/console-interface/console-interface-mock'
import adbCommands from '../adb-wrapper/adb-commands'

// Mocked ConsoleInterface
let cimock = new ConsoleInterfaceMock()
let adbInterfaceInstance = new ADBChannel(cimock)
const ip = '192.168.1.102'
const phoneName = 'DEVICE_NAME'

test('will connect to device seamless', async () => {
const connectDeviceIp = '192.168.1.100'
const connectExistingDeviceMock = (input: string) => {
if (input == `adb -s ${connectDeviceIp} shell getprop ro.product.model`) {
return Buffer.from('PEAR_PHONE')
} else if (input === adbCommands.CONNECT_IP_AND_PORT(connectDeviceIp)) {
return Buffer.from(`connected to ${connectDeviceIp}`)
} else {
return Buffer.from(`fail`)
}
}
cimock.setCallbackMock(connectExistingDeviceMock)
const result = await adbInterfaceInstance.ConnectToDevice(connectDeviceIp)
test('Connect to device with success', async () => {
let cimock = new ConsoleInterfaceMock()
let adbInterfaceInstance = new ADBConnection(cimock)

cimock.setConsoleOutput(`Android Debug Bridge`)
cimock.setConsoleOutput(`connected to ${ip}`)
cimock.setConsoleOutput('PEAR_PHONE')
cimock.returnInfinity = true

expect(result.message).toStrictEqual(`Connected to: PEAR_PHONE`)
const { message } = await adbInterfaceInstance.ConnectToDevice(ip)

expect(message).toStrictEqual(`Connected to: PEAR_PHONE`)
})

test('allready connected to another device', async () => {
test('Fail to connect when allready connected', async () => {
try {
const allreadyConnectedIP = '192.168.1.102'
const allreadyConnectedPhoneName = 'DEVICE_NAME'
const allreadyConnectedCallback = (input: string) => {
if (input == `adb -s 192.168.1.102 shell getprop ro.product.model`) {
return Buffer.from(allreadyConnectedPhoneName)
} else if (input === `adb connect 192.168.1.102:5555`) {
return Buffer.from(`already connected to 192.168.1.102`)
} else {
return Buffer.from(`fail`)
}
}

cimock.setCallbackMock(allreadyConnectedCallback)
await adbInterfaceInstance.ConnectToDevice(allreadyConnectedIP)
let cimock = new ConsoleInterfaceMock()
let adbInterfaceInstance = new ADBConnection(cimock)
cimock.returnInfinity = true
cimock.setConsoleOutput(`Android Debug Bridge`)
cimock.setConsoleOutput(`already connected to ${ip}`)
cimock.setConsoleOutput(phoneName)
await adbInterfaceInstance.ConnectToDevice(ip)
} catch (e) {
expect(e).toBeInstanceOf(ADBInterfaceException)
}
Expand Down
19 changes: 7 additions & 12 deletions src/__tests__/firebase-manager.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import {
ADBChannel,
ADBResultState,
ADBResult,
ADBInterfaceError,
ADBInterfaceException
} from '../adb-wrapper'
import { ADBResultState, ADBInterfaceError } from '../adb-wrapper'
import { ConsoleInterfaceMock } from '../console/console-interface/console-interface-mock'
// import { ConsoleInterface } from './../console-interface/console-interface'
import { FirebaseManagerChannel } from './../firebase-actions/index'

// Mocked ConsoleInterface
Expand Down Expand Up @@ -39,10 +32,12 @@ test('No Devices informed', async () => {
})

test('Disable firebase debugview', async () => {
cimock.setConsoleOutput('')
const result = await adbInterfaceInstance.disableFirebaseDebugView()

expect(result.state).toStrictEqual(ADBResultState.Success)
try {
cimock.setConsoleOutput('')
const result = await adbInterfaceInstance.disableFirebaseDebugView()
} catch (e) {
expect(e).toBeInstanceOf(ADBInterfaceError)
}
})

test('Enable firebase debugview success', async () => {
Expand Down
26 changes: 16 additions & 10 deletions src/__tests__/kill-adb-suite.test.ts
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)
}
})
25 changes: 13 additions & 12 deletions src/__tests__/list-adb-suite.test.ts
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)
36 changes: 36 additions & 0 deletions src/adb-resolver/common-resolver.test.ts
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')
})
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import * as os from 'os'
// TODO Verificar e executar comandos adb na pasta indicada.
const getAndroidStudioPath = ({ osType, homeDir }) => {
import * as path from 'path'

export const getAndroidStudioPath = ({ osType, homeDir }) => {
switch (osType) {
case 'Windows_NT':
return `${homeDir}/AppData/Local/Android/Sdk/platform-tools`
return path.win32.join(
homeDir,
'/AppData/Local/Android/Sdk/platform-tools'
)
case 'Darwin':
return `${homeDir}/Library/Android/sdk/platform-tools`
return path.posix.join(homeDir, '/Library/Android/sdk/platform-tools')
case 'Linux':
return `${homeDir}/Android/Sdk`
return path.posix.join(homeDir, '/Android/Sdk')
default:
throw new TypeError('Android Path Error: Invalid Platform')
}
}

const getDownloadADBUrl = ({ osType }) => {
export const getDownloadADBUrl = ({ osType }) => {
switch (osType) {
case 'Windows_NT':
return 'https://dl.google.com/android/repository/platform-tools-latest-windows.zip'
Expand Down
62 changes: 62 additions & 0 deletions src/adb-resolver/index.ts
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
}
}
Loading

0 comments on commit 5b3d960

Please sign in to comment.