Skip to content

Commit b8a386a

Browse files
committed
added tests verifying search region validation
1 parent 9ae3fb0 commit b8a386a

File tree

2 files changed

+57
-12
lines changed

2 files changed

+57
-12
lines changed

lib/screen.class.spec.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,47 @@ describe("Screen.", () => {
283283
// THEN
284284
expect(matchRegion).toEqual(expectedMatchRegion);
285285
})
286+
287+
it.each([
288+
["with negative x coordinate", new Region(-1, 0, 100, 100)],
289+
["with negative y coordinate", new Region(0, -1, 100, 100)],
290+
["with negative width", new Region(0, 0, -100, 100)],
291+
["with negative height", new Region(0, 0, 100, -100)],
292+
["with region outside screen on x axis", new Region(1100, 0, 100, 100)],
293+
["with region outside screen on y axis", new Region(0, 1100, 100, 100)],
294+
["with region bigger than screen on x axis", new Region(0, 0, 1100, 100)],
295+
["with region bigger than screen on y axis", new Region(0, 0, 1000, 1100)],
296+
["with region of 1 px width", new Region(0, 0, 1, 1100)],
297+
["with region of 1 px height", new Region(0, 0, 100, 1)],
298+
["with region leaving screen on x axis", new Region(600, 0, 500, 100)],
299+
["with region leaving screen on y axis", new Region(0, 500, 100, 600)],
300+
["with NaN x coordinate", new Region("a" as unknown as number, 0, 100, 100)],
301+
["with NaN y coordinate", new Region(0, "a" as unknown as number, 100, 600)],
302+
["with NaN on width", new Region(0, 0, "a" as unknown as number, 100)],
303+
["with NaN on height", new Region(0, 0, 100, "a" as unknown as number)],
304+
])("should reject search regions %s", async (_, region) =>{
305+
306+
// GIVEN
307+
const imagePath = "test/path/to/image.png"
308+
const visionAdapterMock = new VisionAdapter();
309+
310+
const SUT = new Screen(visionAdapterMock);
311+
312+
const matchResult = new MatchResult(0.99, region);
313+
VisionAdapter.prototype.findOnScreenRegion = jest.fn(() => {
314+
return Promise.resolve(matchResult);
315+
});
316+
317+
// WHEN
318+
const findPromise = SUT.find(
319+
imagePath,
320+
{
321+
searchRegion: region
322+
});
323+
324+
// THEN
325+
await expect(findPromise).rejects.toContain(`Searching for ${imagePath} failed. Reason:`);
326+
})
286327
});
287328

288329

lib/screen.class.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,20 +100,24 @@ export class Screen {
100100
searchMultipleScales
101101
);
102102

103+
function validateSearchRegion(search: Region, screen: Region) {
104+
if ( search.left < 0 || search.top < 0 || search.width < 0 || search.height < 0 ) {
105+
throw new Error(`Negative values in search region ${search}`)
106+
}
107+
if ( isNaN(search.left) || isNaN(search.top) || isNaN(search.width) || isNaN(search.height) ) {
108+
throw new Error(`NaN values in search region ${search}`)
109+
}
110+
if ( search.width < 2 || search.height < 2 ) {
111+
throw new Error(`Search region ${search} is not large enough. Must be at least two pixels in both width and height.`)
112+
}
113+
if ( search.left + search.width > screen.width || search.top + search.height > screen.height ) {
114+
throw new Error(`Search region ${search} extends beyond screen boundaries (${screen.width}x${screen.height})`)
115+
}
116+
}
117+
103118
return new Promise<Region>(async (resolve, reject) => {
104119
try {
105-
if ( searchRegion.left < 0 || searchRegion.top < 0 || searchRegion.width < 0 || searchRegion.height < 0 ) {
106-
throw new Error(`Negative values in search region ${searchRegion}`)
107-
}
108-
if ( isNaN(searchRegion.left) || isNaN(searchRegion.top) || isNaN(searchRegion.width) || isNaN(searchRegion.height) ) {
109-
throw new Error(`NaN values in search region ${searchRegion}`)
110-
}
111-
if ( searchRegion.width < 2 || searchRegion.height < 2 ) {
112-
throw new Error(`Search region ${searchRegion} is not large enough. Must be at least two pixels in both width and height.`)
113-
}
114-
if ( searchRegion.left + searchRegion.width > screenSize.width || searchRegion.top + searchRegion.height > screenSize.height ) {
115-
throw new Error(`Search region ${searchRegion} extends beyond screen boundaries (${screenSize.width}x${screenSize.height})`)
116-
}
120+
validateSearchRegion(searchRegion, screenSize);
117121
const matchResult = await this.vision.findOnScreenRegion(matchRequest);
118122
if (matchResult.confidence >= minMatch) {
119123
const possibleHooks = this.findHooks.get(templateImageFilename) || [];

0 commit comments

Comments
 (0)