diff --git a/lib/provider/native/libnut-keyboard.class.spec.ts b/lib/provider/native/libnut-keyboard.class.spec.ts deleted file mode 100644 index 15a27fd7..00000000 --- a/lib/provider/native/libnut-keyboard.class.spec.ts +++ /dev/null @@ -1,218 +0,0 @@ -import libnut = require("@nut-tree/libnut"); -import { Key } from "../../key.enum"; -import KeyboardAction from "./libnut-keyboard.class"; - -jest.mock("@nut-tree/libnut"); - -beforeEach(() => { - jest.resetAllMocks(); -}); - -describe("libnut keyboard action", () => { - describe("click", () => { - it("should forward the keyTap call to libnut for a known key", async () => { - // GIVEN - const SUT = new KeyboardAction(); - - // WHEN - await SUT.click(Key.A); - - // THEN - expect(libnut.keyTap).toBeCalledTimes(1); - }); - - it("should reject on libnut errors", async () => { - // GIVEN - const SUT = new KeyboardAction(); - libnut.keyTap = jest.fn(() => { - throw new Error("Test error"); - }); - - // WHEN - - // THEN - await expect(SUT.click(Key.A)).rejects.toThrowError("Test error"); - }); - - it("should not forward the keyTap call to libnut for an unknown key", async () => { - // GIVEN - const SUT = new KeyboardAction(); - - // WHEN - await SUT.click(Key.Pause); - - // THEN - expect(libnut.keyTap).not.toBeCalled(); - }); - }); - - describe("type", () => { - it("should forward the type call to libnut", async () => { - // GIVEN - const SUT = new KeyboardAction(); - const payload = "testInput"; - - // WHEN - await SUT.type(payload); - - // THEN - expect(libnut.typeString).toBeCalledTimes(1); - expect(libnut.typeString).toBeCalledWith(payload); - }); - - it("should reject on libnut errors", async () => { - // GIVEN - const SUT = new KeyboardAction(); - libnut.typeString = jest.fn(() => { - throw new Error("Test error"); - }); - - // WHEN - - // THEN - await expect(SUT.type("foo")).rejects.toThrowError("Test error"); - }); - }); - - describe("pressKey", () => { - it("should forward the pressKey call to libnut for a known key", async () => { - // GIVEN - const SUT = new KeyboardAction(); - - // WHEN - await SUT.pressKey(Key.A); - - // THEN - expect(libnut.keyToggle).toBeCalledTimes(1); - expect(libnut.keyToggle).toBeCalledWith( - KeyboardAction.keyLookup(Key.A), - "down", - [] - ); - }); - - it("should treat a list of keys as modifiers + the actual key to press", async () => { - // GIVEN - const SUT = new KeyboardAction(); - - // WHEN - await SUT.pressKey(Key.LeftControl, Key.A); - - // THEN - expect(libnut.keyToggle).toBeCalledTimes(1); - expect(libnut.keyToggle).toBeCalledWith( - KeyboardAction.keyLookup(Key.A), - "down", - [KeyboardAction.keyLookup(Key.LeftControl)] - ); - }); - - it("should not forward the pressKey call to libnut for an unknown key", async () => { - // GIVEN - const SUT = new KeyboardAction(); - - // WHEN - await SUT.pressKey(Key.Pause); - - // THEN - expect(libnut.keyToggle).not.toBeCalled(); - }); - - it("should reject on libnut errors", async () => { - // GIVEN - const SUT = new KeyboardAction(); - libnut.keyToggle = jest.fn(() => { - throw new Error("Test error"); - }); - - // WHEN - - // THEN - await expect(SUT.pressKey(Key.A)).rejects.toThrowError("Test error"); - }); - }); - - describe("releaseKey", () => { - it("should forward the releaseKey call to libnut for a known key", async () => { - // GIVEN - const SUT = new KeyboardAction(); - - // WHEN - await SUT.releaseKey(Key.A); - - // THEN - expect(libnut.keyToggle).toBeCalledTimes(1); - expect(libnut.keyToggle).toBeCalledWith( - KeyboardAction.keyLookup(Key.A), - "up", - [] - ); - }); - - it("should treat a list of keys as modifiers + the actual key to release", async () => { - // GIVEN - const SUT = new KeyboardAction(); - - // WHEN - await SUT.releaseKey(Key.LeftControl, Key.A); - - // THEN - expect(libnut.keyToggle).toBeCalledTimes(1); - expect(libnut.keyToggle).toBeCalledWith( - KeyboardAction.keyLookup(Key.A), - "up", - [KeyboardAction.keyLookup(Key.LeftControl)] - ); - }); - - it("should not forward the releaseKey call to libnut for an unknown key", async () => { - // GIVEN - const SUT = new KeyboardAction(); - - // WHEN - await SUT.releaseKey(Key.Pause); - - // THEN - expect(libnut.keyToggle).not.toBeCalled(); - }); - - it("should reject on libnut errors", async () => { - // GIVEN - const SUT = new KeyboardAction(); - libnut.keyToggle = jest.fn(() => { - throw new Error("Test error"); - }); - - // WHEN - - // THEN - await expect(SUT.releaseKey(Key.A)).rejects.toThrowError("Test error"); - }); - }); - - describe("bugfix #260", () => { - it("should forward the pressKey call to libnut for 'delete'", async () => { - // GIVEN - const SUT = new KeyboardAction(); - - // WHEN - await SUT.pressKey(Key.Delete); - - // THEN - expect(libnut.keyToggle).toBeCalledTimes(1); - expect(libnut.keyToggle).toBeCalledWith("delete", "down", []); - }); - - it("should forward the releaseKey call to libnut for 'delete'", async () => { - // GIVEN - const SUT = new KeyboardAction(); - - // WHEN - await SUT.releaseKey(Key.Delete); - - // THEN - expect(libnut.keyToggle).toBeCalledTimes(1); - expect(libnut.keyToggle).toBeCalledWith("delete", "up", []); - }); - }); -}); diff --git a/lib/provider/native/libnut-keyboard.class.ts b/lib/provider/native/libnut-keyboard.class.ts deleted file mode 100644 index 5cd35620..00000000 --- a/lib/provider/native/libnut-keyboard.class.ts +++ /dev/null @@ -1,242 +0,0 @@ -import libnut = require("@nut-tree/libnut"); -import { Key } from "../../key.enum"; -import { KeyboardProviderInterface } from "../keyboard-provider.interface"; - -export default class KeyboardAction implements KeyboardProviderInterface { - public static KeyLookupMap = new Map([ - [Key.A, "a"], - [Key.B, "b"], - [Key.C, "c"], - [Key.D, "d"], - [Key.E, "e"], - [Key.F, "f"], - [Key.G, "g"], - [Key.H, "h"], - [Key.I, "i"], - [Key.J, "j"], - [Key.K, "k"], - [Key.L, "l"], - [Key.M, "m"], - [Key.N, "n"], - [Key.O, "o"], - [Key.P, "p"], - [Key.Q, "q"], - [Key.R, "r"], - [Key.S, "s"], - [Key.T, "t"], - [Key.U, "u"], - [Key.V, "v"], - [Key.W, "w"], - [Key.X, "x"], - [Key.Y, "y"], - [Key.Z, "z"], - - [Key.F1, "f1"], - [Key.F2, "f2"], - [Key.F3, "f3"], - [Key.F4, "f4"], - [Key.F5, "f5"], - [Key.F6, "f6"], - [Key.F7, "f7"], - [Key.F8, "f8"], - [Key.F9, "f9"], - [Key.F10, "f10"], - [Key.F11, "f11"], - [Key.F12, "f12"], - [Key.F13, "f13"], - [Key.F14, "f14"], - [Key.F15, "f15"], - [Key.F16, "f16"], - [Key.F17, "f17"], - [Key.F18, "f18"], - [Key.F19, "f19"], - [Key.F20, "f20"], - [Key.F21, "f21"], - [Key.F22, "f22"], - [Key.F23, "f23"], - [Key.F24, "f24"], - - [Key.Num0, "0"], - [Key.Num1, "1"], - [Key.Num2, "2"], - [Key.Num3, "3"], - [Key.Num4, "4"], - [Key.Num5, "5"], - [Key.Num6, "6"], - [Key.Num7, "7"], - [Key.Num8, "8"], - [Key.Num9, "9"], - [Key.NumPad0, "numpad_0"], - [Key.NumPad1, "numpad_1"], - [Key.NumPad2, "numpad_2"], - [Key.NumPad3, "numpad_3"], - [Key.NumPad4, "numpad_4"], - [Key.NumPad5, "numpad_5"], - [Key.NumPad6, "numpad_6"], - [Key.NumPad7, "numpad_7"], - [Key.NumPad8, "numpad_8"], - [Key.NumPad9, "numpad_9"], - [Key.Decimal, "numpad_decimal"], - - [Key.Space, "space"], - [Key.Escape, "escape"], - [Key.Tab, "tab"], - [Key.LeftAlt, "alt"], - [Key.LeftControl, "control"], - [Key.RightAlt, "right_alt"], - [Key.RightControl, "right_control"], - [Key.LeftWin, "win"], - [Key.RightWin, "right_win"], - [Key.LeftCmd, "cmd"], - [Key.RightCmd, "right_cmd"], - - [Key.Menu, "menu"], - [Key.Fn, "fn"], - - [Key.LeftShift, "shift"], - [Key.LeftSuper, "command"], - [Key.RightShift, "right_shift"], - [Key.RightSuper, "command"], - - [Key.Grave, "`"], - [Key.Minus, "-"], - [Key.Equal, "="], - [Key.Backspace, "backspace"], - [Key.LeftBracket, "["], - [Key.RightBracket, "]"], - [Key.Backslash, "\\"], - [Key.Semicolon, ";"], - [Key.Quote, "'"], - [Key.Return, "enter"], - [Key.Comma, ","], - [Key.Period, "."], - [Key.Slash, "/"], - - [Key.Left, "left"], - [Key.Up, "up"], - [Key.Right, "right"], - [Key.Down, "down"], - - [Key.Print, "printscreen"], - [Key.Pause, null], - [Key.Insert, "insert"], - [Key.Delete, "delete"], - [Key.Home, "home"], - [Key.End, "end"], - [Key.PageUp, "pageup"], - [Key.PageDown, "pagedown"], - - [Key.Add, "add"], - [Key.Subtract, "subtract"], - [Key.Multiply, "multiply"], - [Key.Divide, "divide"], - [Key.Enter, "enter"], - [Key.Clear, "clear"], - - [Key.CapsLock, "caps_lock"], - [Key.ScrollLock, "scroll_lock"], - [Key.NumLock, "num_lock"], - - [Key.AudioMute, "audio_mute"], - [Key.AudioVolDown, "audio_vol_down"], - [Key.AudioVolUp, "audio_vol_up"], - [Key.AudioPlay, "audio_play"], - [Key.AudioStop, "audio_stop"], - [Key.AudioPause, "audio_pause"], - [Key.AudioPrev, "audio_prev"], - [Key.AudioNext, "audio_next"], - [Key.AudioRewind, "audio_rewind"], - [Key.AudioForward, "audio_forward"], - [Key.AudioRepeat, "audio_repeat"], - [Key.AudioRandom, "audio_random"], - ]); - - public static keyLookup(key: Key): any { - return this.KeyLookupMap.get(key); - } - - private static mapModifierKeys(...keys: Key[]): string[] { - return keys - .map((modifier) => KeyboardAction.keyLookup(modifier)) - .filter((modifierKey) => modifierKey != null && modifierKey.length > 1); - } - - private static key( - key: Key, - event: "up" | "down", - ...modifiers: Key[] - ): Promise { - return new Promise((resolve, reject) => { - try { - const nativeKey = KeyboardAction.keyLookup(key); - const modifierKeys = this.mapModifierKeys(...modifiers); - if (nativeKey) { - libnut.keyToggle(nativeKey, event, modifierKeys); - } - resolve(); - } catch (e) { - reject(e); - } - }); - } - - constructor() {} - - public type(input: string): Promise { - return new Promise((resolve, reject) => { - try { - libnut.typeString(input); - resolve(); - } catch (e) { - reject(e); - } - }); - } - - public click(...keys: Key[]): Promise { - return new Promise((resolve, reject) => { - try { - keys.reverse(); - const [key, ...modifiers] = keys; - const nativeKey = KeyboardAction.keyLookup(key); - const modifierKeys = KeyboardAction.mapModifierKeys(...modifiers); - if (nativeKey) { - libnut.keyTap(nativeKey, modifierKeys); - } - resolve(); - } catch (e) { - reject(e); - } - }); - } - - public pressKey(...keys: Key[]): Promise { - return new Promise(async (resolve, reject) => { - try { - keys.reverse(); - const [key, ...modifiers] = keys; - await KeyboardAction.key(key, "down", ...modifiers); - resolve(); - } catch (e) { - reject(e); - } - }); - } - - public releaseKey(...keys: Key[]): Promise { - return new Promise(async (resolve, reject) => { - try { - keys.reverse(); - const [key, ...modifiers] = keys; - await KeyboardAction.key(key, "up", ...modifiers); - resolve(); - } catch (e) { - reject(e); - } - }); - } - - public setKeyboardDelay(delay: number): void { - libnut.setKeyboardDelay(delay); - } -} diff --git a/lib/provider/native/libnut-mouse.class.spec.ts b/lib/provider/native/libnut-mouse.class.spec.ts deleted file mode 100644 index e7f445d6..00000000 --- a/lib/provider/native/libnut-mouse.class.spec.ts +++ /dev/null @@ -1,327 +0,0 @@ -import libnut = require("@nut-tree/libnut"); -import { Button } from "../../button.enum"; -import { Point } from "../../point.class"; -import MouseAction from "./libnut-mouse.class"; - -jest.mock("@nut-tree/libnut"); - -beforeEach(() => { - jest.resetAllMocks(); -}); - -describe("libnut mouse action", () => { - describe("leftClick", () => { - it("should forward leftClick call to libnut", () => { - // GIVEN - const SUT = new MouseAction(); - - // WHEN - SUT.leftClick(); - - // THEN - expect(libnut.mouseClick).toBeCalledTimes(1); - expect(libnut.mouseClick).toBeCalledWith("left"); - }); - - it("should reject on libnut errors", () => { - // GIVEN - const SUT = new MouseAction(); - const error = "Test error"; - libnut.mouseClick = jest.fn(() => { - throw new Error(error); - }); - - // WHEN - - // THEN - expect(SUT.leftClick()).rejects.toThrowError(error); - }); - }); - - describe("middleClick", () => { - it("should forward middleClick call to libnut", () => { - // GIVEN - const SUT = new MouseAction(); - - // WHEN - SUT.middleClick(); - - // THEN - expect(libnut.mouseClick).toBeCalledTimes(1); - expect(libnut.mouseClick).toBeCalledWith("middle"); - }); - - it("should reject on libnut errors", () => { - // GIVEN - const SUT = new MouseAction(); - const error = "Test error"; - libnut.mouseClick = jest.fn(() => { - throw new Error(error); - }); - - // WHEN - - // THEN - expect(SUT.middleClick()).rejects.toThrowError(error); - }); - }); - - describe("rightClick", () => { - it("should forward rightClick call to libnut", () => { - // GIVEN - const SUT = new MouseAction(); - - // WHEN - SUT.rightClick(); - - // THEN - expect(libnut.mouseClick).toBeCalledTimes(1); - expect(libnut.mouseClick).toBeCalledWith("right"); - }); - - it("should reject on libnut errors", () => { - // GIVEN - const SUT = new MouseAction(); - const error = "Test error"; - libnut.mouseClick = jest.fn(() => { - throw new Error(error); - }); - - // WHEN - - // THEN - expect(SUT.rightClick()).rejects.toThrowError(error); - }); - }); - - describe("pressButton", () => { - it("should forward pressButton call to libnut with state 'down'", () => { - // GIVEN - const SUT = new MouseAction(); - - // WHEN - SUT.pressButton(Button.LEFT); - - // THEN - expect(libnut.mouseToggle).toBeCalledTimes(1); - expect(libnut.mouseToggle).toBeCalledWith("down", expect.any(String)); - }); - - it("should reject on libnut errors", () => { - // GIVEN - const SUT = new MouseAction(); - const error = "Test error"; - libnut.mouseToggle = jest.fn(() => { - throw new Error(error); - }); - - // WHEN - - // THEN - expect(SUT.pressButton(Button.LEFT)).rejects.toThrowError(error); - }); - }); - - describe("releaseButton", () => { - it("should forward pressButton call to libnut with state 'up'", () => { - // GIVEN - const SUT = new MouseAction(); - - // WHEN - SUT.releaseButton(Button.LEFT); - - // THEN - expect(libnut.mouseToggle).toBeCalledTimes(1); - expect(libnut.mouseToggle).toBeCalledWith("up", expect.any(String)); - }); - - it("should reject on libnut errors", () => { - // GIVEN - const SUT = new MouseAction(); - const error = "Test error"; - libnut.mouseToggle = jest.fn(() => { - throw new Error(error); - }); - - // WHEN - - // THEN - expect(SUT.releaseButton(Button.LEFT)).rejects.toThrowError(error); - }); - }); - - describe("scrollUp", () => { - it("should forward scrollUp call to libnut with positive y value", () => { - // GIVEN - const SUT = new MouseAction(); - const scrollAmount = 5; - - // WHEN - SUT.scrollUp(scrollAmount); - - // THEN - expect(libnut.scrollMouse).toBeCalledTimes(1); - expect(libnut.scrollMouse).toBeCalledWith(0, scrollAmount); - }); - - it("should reject on libnut errors", () => { - // GIVEN - const SUT = new MouseAction(); - const error = "Test error"; - libnut.scrollMouse = jest.fn(() => { - throw new Error(error); - }); - - // WHEN - - // THEN - expect(SUT.scrollUp(100)).rejects.toThrowError(error); - }); - }); - - describe("scrollDown", () => { - it("should forward scrollDown call to libnut with negative y value", () => { - // GIVEN - const SUT = new MouseAction(); - const scrollAmount = 5; - - // WHEN - SUT.scrollDown(scrollAmount); - - // THEN - expect(libnut.scrollMouse).toBeCalledTimes(1); - expect(libnut.scrollMouse).toBeCalledWith(0, -scrollAmount); - }); - - it("should reject on libnut errors", () => { - // GIVEN - const SUT = new MouseAction(); - const error = "Test error"; - libnut.scrollMouse = jest.fn(() => { - throw new Error(error); - }); - - // WHEN - - // THEN - expect(SUT.scrollDown(100)).rejects.toThrowError(error); - }); - }); - - describe("scrollLeft", () => { - it("should forward scrollLeft call to libnut with negative x value", () => { - // GIVEN - const SUT = new MouseAction(); - const scrollAmount = 5; - - // WHEN - SUT.scrollLeft(scrollAmount); - - // THEN - expect(libnut.scrollMouse).toBeCalledTimes(1); - expect(libnut.scrollMouse).toBeCalledWith(-scrollAmount, 0); - }); - - it("should reject on libnut errors", () => { - // GIVEN - const SUT = new MouseAction(); - const error = "Test error"; - libnut.scrollMouse = jest.fn(() => { - throw new Error(error); - }); - - // WHEN - - // THEN - expect(SUT.scrollLeft(100)).rejects.toThrowError(error); - }); - }); - - describe("scrollRight", () => { - it("should forward scrollRight call to libnut with negative x value", () => { - // GIVEN - const SUT = new MouseAction(); - const scrollAmount = 5; - - // WHEN - SUT.scrollRight(scrollAmount); - - // THEN - expect(libnut.scrollMouse).toBeCalledTimes(1); - expect(libnut.scrollMouse).toBeCalledWith(scrollAmount, 0); - }); - - it("should reject on libnut errors", () => { - // GIVEN - const SUT = new MouseAction(); - const error = "Test error"; - libnut.scrollMouse = jest.fn(() => { - throw new Error(error); - }); - - // WHEN - - // THEN - expect(SUT.scrollRight(100)).rejects.toThrowError(error); - }); - }); - - describe("currentMousePosition", () => { - it("should return the current mouse position via libnut", async () => { - // GIVEN - libnut.getMousePos = jest.fn(() => ({ x: 10, y: 100 })); - const SUT = new MouseAction(); - - // WHEN - const currentPosition = await SUT.currentMousePosition(); - - // THEN - expect(currentPosition.x).toEqual(10); - expect(currentPosition.y).toEqual(100); - }); - - it("should reject on libnut errors", () => { - // GIVEN - const SUT = new MouseAction(); - const error = "Test error"; - libnut.getMousePos = jest.fn(() => { - throw new Error(error); - }); - - // WHEN - - // THEN - expect(SUT.currentMousePosition()).rejects.toThrowError(error); - }); - }); - - describe("setMousePosition", () => { - it("should set the current mouse position via libnut", () => { - // GIVEN - const SUT = new MouseAction(); - - // WHEN - SUT.setMousePosition(new Point(10, 100)); - - // THEN - expect(libnut.moveMouse).toBeCalledTimes(1); - expect(libnut.moveMouse).toBeCalledWith(10, 100); - }); - - it("should reject on libnut errors", () => { - // GIVEN - const SUT = new MouseAction(); - const error = "Test error"; - libnut.moveMouse = jest.fn(() => { - throw new Error(error); - }); - - // WHEN - - // THEN - expect(SUT.setMousePosition(new Point(100, 100))).rejects.toThrowError( - error - ); - }); - }); -}); diff --git a/lib/provider/native/libnut-mouse.class.ts b/lib/provider/native/libnut-mouse.class.ts deleted file mode 100644 index f5edf945..00000000 --- a/lib/provider/native/libnut-mouse.class.ts +++ /dev/null @@ -1,146 +0,0 @@ -import libnut = require("@nut-tree/libnut"); -import { Button } from "../../button.enum"; -import { Point } from "../../point.class"; -import { MouseProviderInterface } from "../mouse-provider.interface"; - -export default class MouseAction implements MouseProviderInterface { - public static buttonLookup(btn: Button): any { - return this.ButtonLookupMap.get(btn); - } - - private static ButtonLookupMap: Map = new Map( - [ - [Button.LEFT, "left"], - [Button.MIDDLE, "middle"], - [Button.RIGHT, "right"], - ] - ); - - constructor() {} - - public setMouseDelay(delay: number): void { - libnut.setMouseDelay(delay); - } - - public setMousePosition(p: Point): Promise { - return new Promise((resolve, reject) => { - try { - libnut.moveMouse(p.x, p.y); - resolve(); - } catch (e) { - reject(e); - } - }); - } - - public currentMousePosition(): Promise { - return new Promise((resolve, reject) => { - try { - const position = libnut.getMousePos(); - resolve(new Point(position.x, position.y)); - } catch (e) { - reject(e); - } - }); - } - - public click(btn: Button): Promise { - return new Promise((resolve, reject) => { - try { - libnut.mouseClick(MouseAction.buttonLookup(btn)); - resolve(); - } catch (e) { - reject(e); - } - }); - } - - public doubleClick(btn: Button): Promise { - return new Promise((resolve, reject) => { - try { - libnut.mouseClick(MouseAction.buttonLookup(btn), true); - resolve(); - } catch (e) { - reject(e); - } - }); - } - - public leftClick(): Promise { - return this.click(Button.LEFT); - } - - public rightClick(): Promise { - return this.click(Button.RIGHT); - } - - public middleClick(): Promise { - return this.click(Button.MIDDLE); - } - - public pressButton(btn: Button): Promise { - return new Promise((resolve, reject) => { - try { - libnut.mouseToggle("down", MouseAction.buttonLookup(btn)); - resolve(); - } catch (e) { - reject(e); - } - }); - } - - public releaseButton(btn: Button): Promise { - return new Promise((resolve, reject) => { - try { - libnut.mouseToggle("up", MouseAction.buttonLookup(btn)); - resolve(); - } catch (e) { - reject(e); - } - }); - } - - public scrollUp(amount: number): Promise { - return new Promise((resolve, reject) => { - try { - libnut.scrollMouse(0, amount); - resolve(); - } catch (e) { - reject(e); - } - }); - } - - public scrollDown(amount: number): Promise { - return new Promise((resolve, reject) => { - try { - libnut.scrollMouse(0, -amount); - resolve(); - } catch (e) { - reject(e); - } - }); - } - - public scrollLeft(amount: number): Promise { - return new Promise((resolve, reject) => { - try { - libnut.scrollMouse(-amount, 0); - resolve(); - } catch (e) { - reject(e); - } - }); - } - - public scrollRight(amount: number): Promise { - return new Promise((resolve, reject) => { - try { - libnut.scrollMouse(amount, 0); - resolve(); - } catch (e) { - reject(e); - } - }); - } -} diff --git a/lib/provider/native/libnut-screen.class.spec.ts b/lib/provider/native/libnut-screen.class.spec.ts deleted file mode 100644 index 8f1d00d8..00000000 --- a/lib/provider/native/libnut-screen.class.spec.ts +++ /dev/null @@ -1,259 +0,0 @@ -import libnut = require("@nut-tree/libnut"); -import { Region } from "../../region.class"; -import ScreenAction from "./libnut-screen.class"; - -jest.mock("jimp", () => {}); -jest.mock("@nut-tree/libnut"); - -beforeEach(() => { - jest.resetAllMocks(); -}); - -const screenSize = new Region(0, 0, 100, 100); -const screenShotSize = new Region(0, 0, 200, 200); - -describe("libnut screen action", () => { - describe("screen data", () => { - it("should reject when no screenshot data is available", async () => { - // GIVEN - const SUT = new ScreenAction(); - - // WHEN - const call = await SUT.grabScreen; - - // THEN - await expect(call()).rejects.toEqual("Unable to fetch screen content."); - expect(libnut.screen.capture).toBeCalledTimes(1); - }); - - it("should resolve when screenshot data is available", async () => { - // GIVEN - const SUT = new ScreenAction(); - libnut.screen.capture = jest.fn(() => ({ - bitsPerPixel: 0, - byteWidth: 0, - bytesPerPixel: 0, - colorAt: jest.fn(), - height: screenShotSize.height, - image: Buffer.from( - new Array(screenShotSize.width * screenShotSize.height * 4 + 10).fill( - 0 - ) - ), - width: screenShotSize.width, - })); - libnut.getScreenSize = jest.fn(() => ({ - height: screenSize.height, - width: screenSize.width, - })); - - // WHEN - const image = await SUT.grabScreen(); - - // THEN - expect(image.width).toEqual(screenShotSize.width); - expect(image.height).toEqual(screenShotSize.height); - expect(image.pixelDensity.scaleX).toEqual(2); - expect(image.pixelDensity.scaleY).toEqual(2); - expect(image.data.length).toEqual( - screenShotSize.width * screenShotSize.height * 4 - ); - expect(libnut.screen.capture).toBeCalledTimes(1); - }); - - it("should resolve when screenshot data of a screen region is available", async () => { - // GIVEN - const screenRegion = new Region(0, 0, 10, 10); - const SUT = new ScreenAction(); - libnut.screen.capture = jest.fn(() => ({ - bitsPerPixel: 0, - byteWidth: 0, - bytesPerPixel: 0, - colorAt: jest.fn(), - height: screenShotSize.height, - image: Buffer.from( - new Array(screenShotSize.width * screenShotSize.height * 4 + 10).fill( - 0 - ) - ), - width: screenShotSize.width, - })); - - // WHEN - const image = await SUT.grabScreenRegion(screenRegion); - - // THEN - expect(image.width).toEqual(screenShotSize.width); - expect(image.height).toEqual(screenShotSize.height); - expect(image.pixelDensity.scaleX).toEqual(20); - expect(image.pixelDensity.scaleY).toEqual(20); - expect(image.data.length).toEqual( - screenShotSize.width * screenShotSize.height * 4 - ); - expect(libnut.screen.capture).toBeCalledTimes(1); - }); - - it("should reject when no screenshot of a screen region is available", async () => { - // GIVEN - const SUT = new ScreenAction(); - - // WHEN - const call = await SUT.grabScreenRegion; - const screenRegion = new Region(0, 0, 10, 10); - - // THEN - await expect(call(screenRegion)).rejects.toEqual( - "Unable to fetch screen content." - ); - expect(libnut.screen.capture).toBeCalledTimes(1); - expect(libnut.screen.capture).toBeCalledWith( - screenRegion.left, - screenRegion.top, - screenRegion.width, - screenRegion.height - ); - }); - }); - - describe("screen size", () => { - describe("screenWidth", () => { - it("should determine screen width via libnut", async () => { - // GIVEN - const SUT = new ScreenAction(); - libnut.getScreenSize = jest.fn(() => ({ - width: screenSize.width, - height: screenSize.height, - })); - - // WHEN - const width = await SUT.screenWidth(); - - // THEN - expect(width).toEqual(screenSize.width); - }); - - it("should reject on libnut errors", async () => { - // GIVEN - const SUT = new ScreenAction(); - const error = "Test error"; - libnut.getScreenSize = jest.fn(() => { - throw new Error(error); - }); - - // WHEN - - // THEN - expect(SUT.screenWidth()).rejects.toThrowError(error); - }); - }); - - describe("screenWidth", () => { - it("should determine screen height via libnut", async () => { - // GIVEN - const SUT = new ScreenAction(); - libnut.getScreenSize = jest.fn(() => ({ - width: screenSize.width, - height: screenSize.height, - })); - - // WHEN - const width = await SUT.screenHeight(); - - // THEN - expect(width).toEqual(screenSize.height); - }); - - it("should reject on libnut errors", async () => { - // GIVEN - const SUT = new ScreenAction(); - const error = "Test error"; - libnut.getScreenSize = jest.fn(() => { - throw new Error(error); - }); - - // WHEN - - // THEN - expect(SUT.screenHeight()).rejects.toThrowError(error); - }); - }); - - describe("screenWidth", () => { - it("should determine screen size via libnut", async () => { - // GIVEN - const SUT = new ScreenAction(); - libnut.getScreenSize = jest.fn(() => ({ - width: screenSize.width, - height: screenSize.height, - })); - - // WHEN - const size = await SUT.screenSize(); - - // THEN - expect(size).toEqual(screenSize); - }); - - it("should reject on libnut errors", async () => { - // GIVEN - const SUT = new ScreenAction(); - const error = "Test error"; - libnut.getScreenSize = jest.fn(() => { - throw new Error(error); - }); - - // WHEN - - // THEN - expect(SUT.screenSize()).rejects.toThrowError(error); - }); - }); - - describe("highlight", () => { - it("should highlight a screen region via libnut", async () => { - // GIVEN - const SUT = new ScreenAction(); - libnut.screen.highlight = jest.fn(() => {}); - const x = 10; - const y = 20; - const w = 30; - const h = 40; - const testRegion = new Region(x, y, w, h); - const highlightDuration = 10; - const highlightOpacity = 1.0; - - // WHEN - await SUT.highlightScreenRegion( - testRegion, - highlightDuration, - highlightOpacity - ); - - // THEN - expect(libnut.screen.highlight).toBeCalledTimes(1); - expect(libnut.screen.highlight).toBeCalledWith( - x, - y, - w, - h, - highlightDuration, - highlightOpacity - ); - }); - - it("should reject on libnut errors", async () => { - // GIVEN - const SUT = new ScreenAction(); - const error = "Test error"; - libnut.getScreenSize = jest.fn(() => { - throw new Error(error); - }); - - // WHEN - - // THEN - expect(SUT.screenSize()).rejects.toThrowError(error); - }); - }); - }); -}); diff --git a/lib/provider/native/libnut-screen.class.ts b/lib/provider/native/libnut-screen.class.ts deleted file mode 100644 index ffc29a16..00000000 --- a/lib/provider/native/libnut-screen.class.ts +++ /dev/null @@ -1,130 +0,0 @@ -import libnut = require("@nut-tree/libnut"); -import { Image } from "../../image.class"; -import { Region } from "../../region.class"; -import { ScreenProviderInterface } from "../screen-provider.interface"; -import { ColorMode } from "../../colormode.enum"; - -export default class ScreenAction implements ScreenProviderInterface { - private static determinePixelDensity( - screen: Region, - screenShot: libnut.Bitmap - ): { scaleX: number; scaleY: number } { - return { - scaleX: screenShot.width / screen.width, - scaleY: screenShot.height / screen.height, - }; - } - - constructor() {} - - public grabScreen(): Promise { - return new Promise((resolve, reject) => { - const screenShot = libnut.screen.capture(); - if (screenShot) { - const screenSize = libnut.getScreenSize(); - const pixelScaling = ScreenAction.determinePixelDensity( - new Region(0, 0, screenSize.width, screenSize.height), - screenShot - ); - resolve( - new Image( - screenShot.width, - screenShot.height, - screenShot.image.slice(0, screenShot.width * screenShot.height * 4), - 4, - "grabScreenResult", - screenShot.bitsPerPixel, - screenShot.byteWidth, - ColorMode.BGR, - pixelScaling - ) - ); - } else { - reject("Unable to fetch screen content."); - } - }); - } - - public grabScreenRegion(region: Region): Promise { - return new Promise((resolve, reject) => { - const screenShot = libnut.screen.capture( - region.left, - region.top, - region.width, - region.height - ); - if (screenShot) { - const pixelScaling = ScreenAction.determinePixelDensity( - region, - screenShot - ); - resolve( - new Image( - screenShot.width, - screenShot.height, - screenShot.image.slice(0, screenShot.width * screenShot.height * 4), - 4, - "grabScreenRegionResult", - screenShot.bitsPerPixel, - screenShot.byteWidth, - ColorMode.BGR, - pixelScaling - ) - ); - } else { - reject("Unable to fetch screen content."); - } - }); - } - - public highlightScreenRegion( - region: Region, - duration: number, - opacity: number - ): Promise { - return new Promise((resolve) => { - libnut.screen.highlight( - region.left, - region.top, - region.width, - region.height, - duration, - opacity - ); - resolve(); - }); - } - - public screenWidth(): Promise { - return new Promise((resolve, reject) => { - try { - const size = libnut.getScreenSize(); - resolve(size.width); - } catch (e) { - reject(e); - } - }); - } - - public screenHeight(): Promise { - return new Promise((resolve, reject) => { - try { - const size = libnut.getScreenSize(); - resolve(size.height); - } catch (e) { - reject(e); - } - }); - } - - public screenSize(): Promise { - return new Promise((resolve, reject) => { - try { - const screenSize = libnut.getScreenSize(); - resolve(new Region(0, 0, screenSize.width, screenSize.height)); - } catch (e) { - reject(e); - } - }); - } -} diff --git a/lib/provider/native/libnut-window.class.spec.ts b/lib/provider/native/libnut-window.class.spec.ts deleted file mode 100644 index 36efd48b..00000000 --- a/lib/provider/native/libnut-window.class.spec.ts +++ /dev/null @@ -1,158 +0,0 @@ -import libnut = require("@nut-tree/libnut"); -import WindowAction from "./libnut-window.class"; -import { Region } from "../../region.class"; - -jest.mock("@nut-tree/libnut"); - -beforeEach(() => { - jest.resetAllMocks(); -}); - -describe("libnut WindowAction", () => { - describe("getWindows", () => { - it("should resolve to a list of numeric window handles via libnut#getWindows", async () => { - // GIVEN - const SUT = new WindowAction(); - const windowList = [1, 2, 3]; - libnut.getWindows = jest.fn(() => windowList); - - // WHEN - const windows = SUT.getWindows(); - - // THEN - await expect(libnut.getWindows).toBeCalledTimes(1); - await expect(windows).resolves.toBe(windowList); - }); - - it("should reject on errors in libnut#getWindows", async () => { - // GIVEN - const SUT = new WindowAction(); - const errorMessage = "getWindows threw"; - libnut.getWindows = jest.fn(() => { - throw new Error(errorMessage); - }); - - // WHEN - const windows = SUT.getWindows(); - - // THEN - await expect(libnut.getWindows).toBeCalledTimes(1); - await expect(windows).rejects.toThrowError(errorMessage); - }); - }); - - describe("getActiveWindow", () => { - it("should resolve to a numeric window handles via libnut#getActiveWindow", async () => { - // GIVEN - const SUT = new WindowAction(); - const activeWindow = 1; - libnut.getActiveWindow = jest.fn(() => activeWindow); - - // WHEN - const window = SUT.getActiveWindow(); - - // THEN - await expect(libnut.getActiveWindow).toBeCalledTimes(1); - await expect(window).resolves.toBe(activeWindow); - }); - - it("should reject on errors in libnut#getActiveWindow", async () => { - // GIVEN - const SUT = new WindowAction(); - const errorMessage = "getActiveWindow threw"; - libnut.getActiveWindow = jest.fn(() => { - throw new Error(errorMessage); - }); - - // WHEN - const windows = SUT.getActiveWindow(); - - // THEN - await expect(libnut.getActiveWindow).toBeCalledTimes(1); - await expect(windows).rejects.toThrowError(errorMessage); - }); - }); - - describe("getWindowRegion", () => { - it("should resolve to a window region via libnut#getWindowRegion", async () => { - // GIVEN - const SUT = new WindowAction(); - const windowHandle = 100; - const windowRect = { - x: 1, - y: 2, - width: 42, - height: 23, - }; - const windowRegion = new Region( - windowRect.x, - windowRect.y, - windowRect.width, - windowRect.height - ); - libnut.getWindowRect = jest.fn(() => windowRect); - - // WHEN - const wndRegion = SUT.getWindowRegion(windowHandle); - - // THEN - await expect(libnut.getWindowRect).toBeCalledTimes(1); - await expect(libnut.getWindowRect).toBeCalledWith(windowHandle); - await expect(wndRegion).resolves.toStrictEqual(windowRegion); - }); - - it("should reject on errors in libnut#getActiveWindow", async () => { - // GIVEN - const SUT = new WindowAction(); - const errorMessage = "getWindowRect threw"; - const windowHandle = 100; - libnut.getWindowRect = jest.fn(() => { - throw new Error(errorMessage); - }); - - // WHEN - const windows = SUT.getWindowRegion(windowHandle); - - // THEN - await expect(libnut.getWindowRect).toBeCalledTimes(1); - await expect(libnut.getWindowRect).toBeCalledWith(windowHandle); - await expect(windows).rejects.toThrowError(errorMessage); - }); - }); - - describe("getWindowTitle", () => { - it("should resolve to a window title via libnut#getWindowTitle", async () => { - // GIVEN - const SUT = new WindowAction(); - const windowTitle = "test window"; - const windowHandle = 42; - libnut.getWindowTitle = jest.fn(() => windowTitle); - - // WHEN - const wndRegion = SUT.getWindowTitle(windowHandle); - - // THEN - await expect(libnut.getWindowTitle).toBeCalledTimes(1); - await expect(libnut.getWindowTitle).toBeCalledWith(windowHandle); - await expect(wndRegion).resolves.toBe(windowTitle); - }); - - it("should reject on errors in libnut#getActiveWindow", async () => { - // GIVEN - const SUT = new WindowAction(); - const errorMessage = "getWindowRect threw"; - const windowHandle = 42; - libnut.getWindowTitle = jest.fn(() => { - throw new Error(errorMessage); - }); - - // WHEN - const windows = SUT.getWindowTitle(windowHandle); - - // THEN - await expect(libnut.getWindowTitle).toBeCalledTimes(1); - await expect(libnut.getWindowTitle).toBeCalledWith(windowHandle); - await expect(windows).rejects.toThrowError(errorMessage); - }); - }); -}); diff --git a/lib/provider/native/libnut-window.class.ts b/lib/provider/native/libnut-window.class.ts deleted file mode 100644 index d4b988c9..00000000 --- a/lib/provider/native/libnut-window.class.ts +++ /dev/null @@ -1,92 +0,0 @@ -import libnut = require("@nut-tree/libnut"); -import { Region } from "../../region.class"; -import { WindowProviderInterface } from "../window-provider.interface"; -import { Size } from "../../size.class"; -import { Point } from "../../point.class"; - -export default class WindowAction implements WindowProviderInterface { - public getWindows(): Promise { - return new Promise((resolve, reject) => { - try { - resolve(libnut.getWindows()); - } catch (e) { - reject(e); - } - }); - } - - getActiveWindow(): Promise { - return new Promise((resolve, reject) => { - try { - resolve(libnut.getActiveWindow()); - } catch (e) { - reject(e); - } - }); - } - - getWindowRegion(windowHandle: number): Promise { - return new Promise((resolve, reject) => { - try { - const windowRect = libnut.getWindowRect(windowHandle); - resolve( - new Region( - windowRect.x, - windowRect.y, - windowRect.width, - windowRect.height, - ), - ); - } catch (e) { - reject(e); - } - }); - } - - getWindowTitle(windowHandle: number): Promise { - return new Promise((resolve, reject) => { - try { - resolve(libnut.getWindowTitle(windowHandle)); - } catch (e) { - reject(e); - } - }); - } - - focusWindow(windowHandle: number): Promise { - return new Promise((resolve, reject) => { - try { - resolve(libnut.focusWindow(windowHandle)); - } catch (e) { - reject(e); - } - }); - } - - moveWindow(windowHandle: number, newOrigin: Point): Promise { - return new Promise((resolve, reject) => { - try { - resolve( - libnut.moveWindow(windowHandle, { x: newOrigin.x, y: newOrigin.y }), - ); - } catch (e) { - reject(e); - } - }); - } - - resizeWindow(windowHandle: number, newSize: Size): Promise { - return new Promise((resolve, reject) => { - try { - resolve( - libnut.resizeWindow(windowHandle, { - width: newSize.width, - height: newSize.height, - }), - ); - } catch (e) { - reject(e); - } - }); - } -}