-
Notifications
You must be signed in to change notification settings - Fork 326
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: introduce device action hooks + tests basic cases
- Loading branch information
Showing
61 changed files
with
912 additions
and
120 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
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,216 @@ | ||
import BigNumber from "bignumber.js"; | ||
import { DeviceModelId } from "@ledgerhq/types-devices"; | ||
import { | ||
deviceInfo155 as deviceInfo, | ||
deviceInfo210lo5, | ||
mockListAppsResult as innerMockListAppResult, | ||
} from "@ledgerhq/live-common/apps/mock"; | ||
import { AppOp } from "@ledgerhq/live-common/apps/types"; | ||
import { AppType, DeviceInfo } from "@ledgerhq/types-live/lib/manager"; | ||
import { Device } from "@ledgerhq/live-common/hw/actions/types"; | ||
import { Transaction } from "@ledgerhq/live-common/generated/types"; | ||
import { waitForElementById, tapById, delay } from "../helpers"; | ||
import { mockDeviceEvent } from "../bridge/server"; | ||
import { DeviceLike } from "../../src/reducers/types"; | ||
|
||
const mockListAppsResult = ( | ||
appDesc: string, | ||
installedDesc: string, | ||
deviceInfo: DeviceInfo, | ||
deviceModelId?: DeviceModelId, | ||
) => { | ||
const result = innerMockListAppResult(appDesc, installedDesc, deviceInfo, deviceModelId); | ||
Object.keys(result?.appByName).forEach(key => { | ||
result.appByName[key] = { ...result.appByName[key], type: AppType.currency }; | ||
}); | ||
return result; | ||
}; | ||
|
||
// this implement mock for the "legacy" device action (the one working with live-common/lib/hw/actions/*) | ||
export default class DeviceAction { | ||
deviceLike: DeviceLike; | ||
device: Device; | ||
|
||
constructor(deviceLike: DeviceLike) { | ||
this.deviceLike = deviceLike; | ||
this.device = this.deviceLikeToDevice(deviceLike); | ||
} | ||
|
||
deviceLikeToDevice(d: DeviceLike): Device { | ||
return { | ||
deviceId: d.id, | ||
deviceName: d.name, | ||
modelId: d.modelId, | ||
wired: false, | ||
}; | ||
} | ||
|
||
async selectMockDevice() { | ||
const elId = "device-item-" + this.deviceLike.id; | ||
await waitForElementById(elId); | ||
await tapById(elId); | ||
} | ||
|
||
async waitForSpinner() { | ||
await waitForElementById("device-action-loading"); | ||
} | ||
|
||
async openApp() { | ||
await this.waitForSpinner(); | ||
mockDeviceEvent({ type: "opened" }); | ||
} | ||
|
||
async genuineCheck(appDesc = "Bitcoin", installedDesc = "Bitcoin") { | ||
await this.waitForSpinner(); | ||
|
||
const { device } = this; | ||
const result = mockListAppsResult(appDesc, installedDesc, deviceInfo); | ||
mockDeviceEvent( | ||
{ | ||
type: "deviceChange", | ||
device, | ||
}, | ||
{ | ||
type: "listingApps", | ||
deviceInfo, | ||
}, | ||
{ | ||
type: "result", | ||
result, | ||
}, | ||
{ type: "complete" }, | ||
); | ||
} | ||
|
||
async accessManager( | ||
appDesc = "Bitcoin,Tron,Litecoin,Ethereum,Ripple,Stellar", | ||
installedDesc = "Bitcoin,Litecoin,Ethereum (outdated)", | ||
) { | ||
await this.waitForSpinner(); | ||
|
||
const { device } = this; | ||
|
||
const result = mockListAppsResult(appDesc, installedDesc, deviceInfo, device.modelId); | ||
mockDeviceEvent( | ||
{ | ||
type: "deviceChange", | ||
device, | ||
}, | ||
{ | ||
type: "listingApps", | ||
deviceInfo, | ||
}, | ||
{ | ||
type: "result", | ||
result, | ||
}, | ||
{ type: "complete" }, | ||
); | ||
} | ||
|
||
async accessManagerWithL10n( | ||
appDesc = "Bitcoin,Tron,Litecoin,Ethereum,Ripple,Stellar", | ||
installedDesc = "Bitcoin,Litecoin,Ethereum (outdated)", | ||
) { | ||
await this.waitForSpinner(); | ||
|
||
const { device } = this; | ||
|
||
const result = mockListAppsResult(appDesc, installedDesc, deviceInfo210lo5); | ||
mockDeviceEvent( | ||
{ | ||
type: "deviceChange", | ||
device, | ||
}, | ||
{ | ||
type: "listingApps", | ||
deviceInfo: deviceInfo210lo5, | ||
}, | ||
{ | ||
type: "result", | ||
result, | ||
}, | ||
{ type: "complete" }, | ||
); | ||
} | ||
|
||
async complete() { | ||
mockDeviceEvent({ type: "complete" }); | ||
} | ||
|
||
async initiateLanguageInstallation() { | ||
mockDeviceEvent({ type: "devicePermissionRequested" }); | ||
} | ||
|
||
async add50ProgressToLanguageInstallation() { | ||
mockDeviceEvent({ type: "progress", progress: 0.5 }); | ||
} | ||
|
||
async installSetOfAppsMocked( | ||
progress: number, | ||
itemProgress: number, | ||
currentAppOp: AppOp, | ||
installQueue: string[], | ||
) { | ||
mockDeviceEvent({ | ||
type: "inline-install", | ||
progress: progress, | ||
itemProgress: itemProgress, | ||
currentAppOp: currentAppOp, | ||
installQueue: installQueue, | ||
}); | ||
} | ||
|
||
async resolveDependenciesMocked(installQueue: string[]) { | ||
mockDeviceEvent({ | ||
type: "listed-apps", | ||
installQueue: installQueue, | ||
}); | ||
} | ||
|
||
async completeLanguageInstallation() { | ||
mockDeviceEvent({ type: "languageInstalled" }); | ||
} | ||
|
||
async requestImageLoad() { | ||
mockDeviceEvent({ type: "loadImagePermissionRequested" }); | ||
} | ||
|
||
async loadImageWithProgress(progress: number) { | ||
mockDeviceEvent({ type: "progress", progress }); | ||
} | ||
|
||
async requestImageCommit() { | ||
mockDeviceEvent({ type: "commitImagePermissionRequested" }); | ||
} | ||
|
||
async confirmImageLoaded(imageSize: number, imageHash: string) { | ||
mockDeviceEvent({ type: "imageLoaded", imageSize, imageHash }); | ||
} | ||
|
||
async initiateSwap(estimatedFees: BigNumber) { | ||
mockDeviceEvent({ type: "opened" }); | ||
await delay(2000); // enough time to allow the UI to switch from one action to another | ||
mockDeviceEvent({ type: "init-swap-requested", estimatedFees }); | ||
} | ||
|
||
async confirmSwap(transaction: Transaction) { | ||
mockDeviceEvent( | ||
{ | ||
type: "init-swap-result", | ||
initSwapResult: { | ||
transaction, | ||
swapId: "12345", | ||
}, | ||
}, | ||
{ | ||
type: "complete", | ||
}, | ||
); | ||
} | ||
|
||
async silentSign() { | ||
await this.waitForSpinner(); | ||
mockDeviceEvent({ type: "opened" }, { type: "complete" }); | ||
} | ||
} |
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
Oops, something went wrong.