Skip to content

Commit d09f816

Browse files
committed
(#40) Added implementation for highlight, renamed to libnut-screen-action
1 parent 6c311da commit d09f816

File tree

2 files changed

+75
-31
lines changed

2 files changed

+75
-31
lines changed

lib/provider/native/robotjs-screen-action.class.spec.ts renamed to lib/provider/native/libnut-screen-action.class.spec.ts

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import robot = require("@nut-tree/libnut");
1+
import libnut = require("@nut-tree/libnut");
22
import { Region } from "../../region.class";
3-
import { ScreenAction } from "./robotjs-screen-action.class";
3+
import { ScreenAction } from "./libnut-screen-action.class";
44

55
jest.mock("@nut-tree/libnut");
66

@@ -11,7 +11,7 @@ beforeEach(() => {
1111
const screenSize = new Region(0, 0, 100, 100);
1212
const screenShotSize = new Region(0, 0, 200, 200);
1313

14-
describe("robotjs screen action", () => {
14+
describe("libnut screen action", () => {
1515
describe("screen data", () => {
1616
it("should reject when no screenshot data is available", async () => {
1717
// GIVEN
@@ -22,13 +22,13 @@ describe("robotjs screen action", () => {
2222

2323
// THEN
2424
await expect(call()).rejects.toEqual("Unable to fetch screen content.");
25-
expect(robot.screen.capture).toBeCalledTimes(1);
25+
expect(libnut.screen.capture).toBeCalledTimes(1);
2626
});
2727

2828
it("should resolve when screenshot data is available", async () => {
2929
// GIVEN
3030
const SUT = new ScreenAction();
31-
robot.screen.capture = jest.fn(() => ({
31+
libnut.screen.capture = jest.fn(() => ({
3232
bitsPerPixel: 0,
3333
byteWidth: 0,
3434
bytesPerPixel: 0,
@@ -38,7 +38,7 @@ describe("robotjs screen action", () => {
3838
width: screenShotSize.width,
3939
})
4040
);
41-
robot.getScreenSize = jest.fn(() => ({
41+
libnut.getScreenSize = jest.fn(() => ({
4242
height: screenSize.height,
4343
width: screenSize.width,
4444
})
@@ -52,14 +52,14 @@ describe("robotjs screen action", () => {
5252
expect(image.height).toEqual(screenShotSize.height);
5353
expect(image.pixelDensity.scaleX).toEqual(2);
5454
expect(image.pixelDensity.scaleY).toEqual(2);
55-
expect(robot.screen.capture).toBeCalledTimes(1);
55+
expect(libnut.screen.capture).toBeCalledTimes(1);
5656
});
5757

5858
it("should resolve when screenshot data of a screen region is available", async () => {
5959
// GIVEN
6060
const screenRegion = new Region(0, 0, 10, 10);
6161
const SUT = new ScreenAction();
62-
robot.screen.capture = jest.fn(() => ({
62+
libnut.screen.capture = jest.fn(() => ({
6363
bitsPerPixel: 0,
6464
byteWidth: 0,
6565
bytesPerPixel: 0,
@@ -78,7 +78,7 @@ describe("robotjs screen action", () => {
7878
expect(image.height).toEqual(screenShotSize.height);
7979
expect(image.pixelDensity.scaleX).toEqual(20);
8080
expect(image.pixelDensity.scaleY).toEqual(20);
81-
expect(robot.screen.capture).toBeCalledTimes(1);
81+
expect(libnut.screen.capture).toBeCalledTimes(1);
8282
});
8383

8484
it("should reject when no screenshot of a screen region is available", async () => {
@@ -91,18 +91,18 @@ describe("robotjs screen action", () => {
9191

9292
// THEN
9393
await expect(call(screenRegion)).rejects.toEqual("Unable to fetch screen content.");
94-
expect(robot.screen.capture).toBeCalledTimes(1);
95-
expect(robot.screen.capture)
94+
expect(libnut.screen.capture).toBeCalledTimes(1);
95+
expect(libnut.screen.capture)
9696
.toBeCalledWith(screenRegion.left, screenRegion.top, screenRegion.width, screenRegion.height);
9797
});
9898
});
9999

100100
describe("screen size", () => {
101101
describe("screenWidth", () => {
102-
it("should determine screen width via robotjs", async () => {
102+
it("should determine screen width via libnut", async () => {
103103
// GIVEN
104104
const SUT = new ScreenAction();
105-
robot.getScreenSize = jest.fn(() => ({width: screenSize.width, height: screenSize.height}));
105+
libnut.getScreenSize = jest.fn(() => ({width: screenSize.width, height: screenSize.height}));
106106

107107
// WHEN
108108
const width = await SUT.screenWidth();
@@ -111,11 +111,11 @@ describe("robotjs screen action", () => {
111111
expect(width).toEqual(screenSize.width);
112112
});
113113

114-
it("should reject on robotjs errors", async () => {
114+
it("should reject on libnut errors", async () => {
115115
// GIVEN
116116
const SUT = new ScreenAction();
117117
const error = "Test error";
118-
robot.getScreenSize = jest.fn(() => {
118+
libnut.getScreenSize = jest.fn(() => {
119119
throw new Error(error);
120120
});
121121

@@ -127,10 +127,10 @@ describe("robotjs screen action", () => {
127127
});
128128

129129
describe("screenWidth", () => {
130-
it("should determine screen height via robotjs", async () => {
130+
it("should determine screen height via libnut", async () => {
131131
// GIVEN
132132
const SUT = new ScreenAction();
133-
robot.getScreenSize = jest.fn(() => ({width: screenSize.width, height: screenSize.height}));
133+
libnut.getScreenSize = jest.fn(() => ({width: screenSize.width, height: screenSize.height}));
134134

135135
// WHEN
136136
const width = await SUT.screenHeight();
@@ -139,11 +139,11 @@ describe("robotjs screen action", () => {
139139
expect(width).toEqual(screenSize.height);
140140
});
141141

142-
it("should reject on robotjs errors", async () => {
142+
it("should reject on libnut errors", async () => {
143143
// GIVEN
144144
const SUT = new ScreenAction();
145145
const error = "Test error";
146-
robot.getScreenSize = jest.fn(() => {
146+
libnut.getScreenSize = jest.fn(() => {
147147
throw new Error(error);
148148
});
149149

@@ -155,10 +155,10 @@ describe("robotjs screen action", () => {
155155
});
156156

157157
describe("screenWidth", () => {
158-
it("should determine screen size via robotjs", async () => {
158+
it("should determine screen size via libnut", async () => {
159159
// GIVEN
160160
const SUT = new ScreenAction();
161-
robot.getScreenSize = jest.fn(() => ({width: screenSize.width, height: screenSize.height}));
161+
libnut.getScreenSize = jest.fn(() => ({width: screenSize.width, height: screenSize.height}));
162162

163163
// WHEN
164164
const size = await SUT.screenSize();
@@ -167,11 +167,47 @@ describe("robotjs screen action", () => {
167167
expect(size).toEqual(screenSize);
168168
});
169169

170-
it("should reject on robotjs errors", async () => {
170+
it("should reject on libnut errors", async () => {
171171
// GIVEN
172172
const SUT = new ScreenAction();
173173
const error = "Test error";
174-
robot.getScreenSize = jest.fn(() => {
174+
libnut.getScreenSize = jest.fn(() => {
175+
throw new Error(error);
176+
});
177+
178+
// WHEN
179+
180+
// THEN
181+
expect(SUT.screenSize()).rejects.toThrowError(error);
182+
});
183+
});
184+
185+
describe("highlight", () => {
186+
it("should highlight a screen region via libnut", async () => {
187+
// GIVEN
188+
const SUT = new ScreenAction();
189+
libnut.screen.highlight = jest.fn(() => {});
190+
const x = 10;
191+
const y = 20;
192+
const w = 30;
193+
const h = 40;
194+
const testRegion = new Region(x, y, w, h);
195+
const highlightDuration = 10;
196+
const highlightOpacity = 1.0;
197+
198+
// WHEN
199+
await SUT.highlightScreenRegion(testRegion, highlightDuration, highlightOpacity);
200+
201+
// THEN
202+
expect(libnut.screen.highlight).toBeCalledTimes(1);
203+
expect(libnut.screen.highlight).toBeCalledWith(x, y, w, h, highlightDuration, highlightOpacity);
204+
});
205+
206+
it("should reject on libnut errors", async () => {
207+
// GIVEN
208+
const SUT = new ScreenAction();
209+
const error = "Test error";
210+
libnut.getScreenSize = jest.fn(() => {
175211
throw new Error(error);
176212
});
177213

lib/provider/native/robotjs-screen-action.class.ts renamed to lib/provider/native/libnut-screen-action.class.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import robot = require("@nut-tree/libnut");
1+
import libnut = require("@nut-tree/libnut");
22
import { Image } from "../../image.class";
33
import { Region } from "../../region.class";
44
import { ScreenActionProvider } from "./screen-action-provider.interface";
@@ -7,7 +7,7 @@ export class ScreenAction implements ScreenActionProvider {
77

88
private static determinePixelDensity(
99
screen: Region,
10-
screenShot: robot.Bitmap,
10+
screenShot: libnut.Bitmap,
1111
): { scaleX: number; scaleY: number } {
1212
return {
1313
scaleX: screenShot.width / screen.width,
@@ -20,9 +20,9 @@ export class ScreenAction implements ScreenActionProvider {
2020

2121
public grabScreen(): Promise<Image> {
2222
return new Promise((resolve, reject) => {
23-
const screenShot = robot.screen.capture();
23+
const screenShot = libnut.screen.capture();
2424
if (screenShot) {
25-
const screenSize = robot.getScreenSize();
25+
const screenSize = libnut.getScreenSize();
2626
const pixelScaling = ScreenAction.determinePixelDensity(
2727
new Region(0, 0, screenSize.width, screenSize.height),
2828
screenShot,
@@ -44,7 +44,7 @@ export class ScreenAction implements ScreenActionProvider {
4444

4545
public grabScreenRegion(region: Region): Promise<Image> {
4646
return new Promise((resolve, reject) => {
47-
const screenShot = robot.screen.capture(
47+
const screenShot = libnut.screen.capture(
4848
region.left,
4949
region.top,
5050
region.width,
@@ -67,10 +67,18 @@ export class ScreenAction implements ScreenActionProvider {
6767
});
6868
}
6969

70+
public highlightScreenRegion(region: Region, duration: number, opacity: number): Promise<void> {
71+
return new Promise<void>((resolve) => {
72+
libnut.screen.highlight(region.left, region.top, region.width, region.height, duration, opacity);
73+
resolve();
74+
});
75+
}
76+
77+
7078
public screenWidth(): Promise<number> {
7179
return new Promise<number>((resolve, reject) => {
7280
try {
73-
const size = robot.getScreenSize();
81+
const size = libnut.getScreenSize();
7482
resolve(size.width);
7583
} catch (e) {
7684
reject(e);
@@ -81,7 +89,7 @@ export class ScreenAction implements ScreenActionProvider {
8189
public screenHeight(): Promise<number> {
8290
return new Promise<number>((resolve, reject) => {
8391
try {
84-
const size = robot.getScreenSize();
92+
const size = libnut.getScreenSize();
8593
resolve(size.height);
8694
} catch (e) {
8795
reject(e);
@@ -92,7 +100,7 @@ export class ScreenAction implements ScreenActionProvider {
92100
public screenSize(): Promise<Region> {
93101
return new Promise<Region>((resolve, reject) => {
94102
try {
95-
const screenSize = robot.getScreenSize();
103+
const screenSize = libnut.getScreenSize();
96104
resolve(new Region(0, 0, screenSize.width, screenSize.height));
97105
} catch (e) {
98106
reject(e);

0 commit comments

Comments
 (0)