Skip to content

Commit 273b63e

Browse files
committed
(#329) Make Screen#find only accept Image or Promise<Image> parameter type
1 parent 4de814f commit 273b63e

File tree

3 files changed

+11
-22
lines changed

3 files changed

+11
-22
lines changed

lib/assert.class.spec.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {AssertClass} from "./assert.class";
22
import {Region} from "./region.class";
33
import {ScreenClass} from "./screen.class";
44
import providerRegistry from "./provider/provider-registry.class";
5+
import {imageResource} from "../index";
56

67
jest.mock('jimp', () => {
78
});
@@ -18,7 +19,7 @@ describe("Assert", () => {
1819
// WHEN
1920

2021
// THEN
21-
await expect(SUT.isVisible(needle)).resolves.not.toThrowError();
22+
await expect(SUT.isVisible(imageResource(needle))).resolves.not.toThrowError();
2223
});
2324

2425
it("isVisible should throw if a match is found.", async () => {
@@ -31,7 +32,7 @@ describe("Assert", () => {
3132
// WHEN
3233

3334
// THEN
34-
await expect(SUT.isVisible(needle)).rejects.toThrowError(`Element '${needle}' not found`);
35+
await expect(SUT.isVisible(imageResource(needle))).rejects.toThrowError(`Element '${needle}' not found`);
3536
});
3637

3738
it("isVisible should throw if a match is found.", async () => {
@@ -46,7 +47,7 @@ describe("Assert", () => {
4647

4748
// THEN
4849
await expect(SUT
49-
.isVisible(needle, searchRegion))
50+
.isVisible(imageResource(needle), searchRegion))
5051
.rejects.toThrowError(`Element '${needle}' not found in region ${searchRegion.toString()}`
5152
);
5253
});
@@ -61,7 +62,7 @@ describe("Assert", () => {
6162
// WHEN
6263

6364
// THEN
64-
await expect(SUT.notVisible(needle)).rejects.toThrowError(`'${needle}' is visible`);
65+
await expect(SUT.notVisible(imageResource(needle))).rejects.toThrowError(`'${needle}' is visible`);
6566
});
6667

6768
it("isVisible should throw if a match is found.", async () => {
@@ -74,6 +75,6 @@ describe("Assert", () => {
7475
// WHEN
7576

7677
// THEN
77-
await expect(SUT.notVisible(needle)).resolves.not.toThrowError();
78+
await expect(SUT.notVisible(imageResource(needle))).resolves.not.toThrowError();
7879
});
7980
});

lib/region.class.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ export class Region {
2929
}
3030

3131
public toString() {
32-
return `(${this.left}, ${this.top}, ${this.left + this.width}, ${this.top +
33-
this.height})`;
32+
return `(${this.left}, ${this.top}, ${this.width}, ${this.height})`;
3433
}
3534
}

lib/screen.class.ts

+4-15
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {Region} from "./region.class";
88
import {timeout} from "./util/timeout.function";
99
import {Image} from "./image.class";
1010
import {ProviderRegistry} from "./provider/provider-registry.class";
11-
import {loadImageResource} from "./imageResources.function";
1211
import {FirstArgumentType} from "./typings";
1312
import {Point} from "./point.class";
1413

@@ -97,20 +96,15 @@ export class ScreenClass {
9796
* @param params {@link LocationParameters} which are used to fine tune search region and / or match confidence
9897
*/
9998
public async find(
100-
templateImage: string | Image | Promise<Image>,
99+
templateImage: Image | Promise<Image>,
101100
params?: LocationParameters,
102101
): Promise<Region> {
103102
const minMatch = (params && params.confidence) || this.config.confidence;
104103
const screenSize = await this.providerRegistry.getScreen().screenSize();
105104
const searchRegion = (params && params.searchRegion) || screenSize;
106105
const searchMultipleScales = (params && params.searchMultipleScales)
107106

108-
let needle: Image;
109-
if (typeof templateImage === "string") {
110-
needle = await loadImageResource(this.providerRegistry, this.config.resourceDirectory, templateImage);
111-
} else {
112-
needle = await templateImage;
113-
}
107+
const needle = await templateImage;
114108

115109
const screenImage = await this.providerRegistry.getScreen().grabScreenRegion(searchRegion);
116110

@@ -154,20 +148,15 @@ export class ScreenClass {
154148
* @param params {@link LocationParameters} which are used to fine tune search region and / or match confidence
155149
*/
156150
public async findAll(
157-
templateImage: string | Image | Promise<Image>,
151+
templateImage: Image | Promise<Image>,
158152
params?: LocationParameters,
159153
): Promise<Region[]> {
160154
const minMatch = (params && params.confidence) || this.config.confidence;
161155
const screenSize = await this.providerRegistry.getScreen().screenSize();
162156
const searchRegion = (params && params.searchRegion) || screenSize;
163157
const searchMultipleScales = (params && params.searchMultipleScales)
164158

165-
let needle: Image;
166-
if (typeof templateImage === "string") {
167-
needle = await loadImageResource(this.providerRegistry, this.config.resourceDirectory, templateImage);
168-
} else {
169-
needle = await templateImage;
170-
}
159+
const needle = await templateImage;
171160

172161
const screenImage = await this.providerRegistry.getScreen().grabScreenRegion(searchRegion);
173162

0 commit comments

Comments
 (0)