Skip to content

Commit b61b723

Browse files
authored
Feature/329/find image parameter type (#330)
* (#329) Make `Screen#find` only accept `Image` or `Promise<Image>` parameter type * (#329) Update docstrings * (#329) Adjusted tests
1 parent 4de814f commit b61b723

File tree

3 files changed

+29
-27
lines changed

3 files changed

+29
-27
lines changed

lib/assert.class.spec.ts

+22-8
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,24 @@ 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 {Image} from "../index";
6+
import {mockPartial} from "sneer";
57

68
jest.mock('jimp', () => {
79
});
810
jest.mock("./screen.class");
911

12+
const needleId = "needleId";
13+
1014
describe("Assert", () => {
1115
it("isVisible should not throw if a match is found.", async () => {
1216
// GIVEN
1317
ScreenClass.prototype.find = jest.fn(() => Promise.resolve(new Region(0, 0, 100, 100)));
1418
const screenMock = new ScreenClass(providerRegistry);
1519
const SUT = new AssertClass(screenMock);
16-
const needle = "foo";
20+
const needle = mockPartial<Image>({
21+
id: needleId
22+
});
1723

1824
// WHEN
1925

@@ -26,12 +32,14 @@ describe("Assert", () => {
2632
ScreenClass.prototype.find = jest.fn(() => Promise.reject("foo"));
2733
const screenMock = new ScreenClass(providerRegistry);
2834
const SUT = new AssertClass(screenMock);
29-
const needle = "foo";
35+
const needle = mockPartial<Image>({
36+
id: needleId
37+
});
3038

3139
// WHEN
3240

3341
// THEN
34-
await expect(SUT.isVisible(needle)).rejects.toThrowError(`Element '${needle}' not found`);
42+
await expect(SUT.isVisible(needle)).rejects.toThrowError(`Element '${needle.id}' not found`);
3543
});
3644

3745
it("isVisible should throw if a match is found.", async () => {
@@ -40,14 +48,16 @@ describe("Assert", () => {
4048
const screenMock = new ScreenClass(providerRegistry);
4149
const SUT = new AssertClass(screenMock);
4250
const searchRegion = new Region(10, 10, 10, 10);
43-
const needle = "foo";
51+
const needle = mockPartial<Image>({
52+
id: needleId
53+
});
4454

4555
// WHEN
4656

4757
// THEN
4858
await expect(SUT
4959
.isVisible(needle, searchRegion))
50-
.rejects.toThrowError(`Element '${needle}' not found in region ${searchRegion.toString()}`
60+
.rejects.toThrowError(`Element '${needle.id}' not found in region ${searchRegion.toString()}`
5161
);
5262
});
5363

@@ -56,20 +66,24 @@ describe("Assert", () => {
5666
ScreenClass.prototype.find = jest.fn(() => Promise.resolve(new Region(0, 0, 100, 100)));
5767
const screenMock = new ScreenClass(providerRegistry);
5868
const SUT = new AssertClass(screenMock);
59-
const needle = "foo";
69+
const needle = mockPartial<Image>({
70+
id: needleId
71+
});
6072

6173
// WHEN
6274

6375
// THEN
64-
await expect(SUT.notVisible(needle)).rejects.toThrowError(`'${needle}' is visible`);
76+
await expect(SUT.notVisible(needle)).rejects.toThrowError(`'${needle.id}' is visible`);
6577
});
6678

6779
it("isVisible should throw if a match is found.", async () => {
6880
// GIVEN
6981
ScreenClass.prototype.find = jest.fn(() => Promise.reject("foo"));
7082
const screenMock = new ScreenClass(providerRegistry);
7183
const SUT = new AssertClass(screenMock);
72-
const needle = "foo";
84+
const needle = mockPartial<Image>({
85+
id: needleId
86+
});
7387

7488
// WHEN
7589

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

+6-17
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

@@ -93,24 +92,19 @@ export class ScreenClass {
9392

9493
/**
9594
* {@link find} will search for a single occurrence of a template image on a systems main screen
96-
* @param templateImage Filename of the template image, relative to {@link ScreenClass.config.resourceDirectory}, or an {@link Image} instance
95+
* @param template Template {@link Image} instance
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+
template: 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 template;
114108

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

@@ -150,24 +144,19 @@ export class ScreenClass {
150144

151145
/**
152146
* {@link findAll} will search for every occurrences of a template image on a systems main screen
153-
* @param templateImage Filename of the template image, relative to {@link ScreenClass.config.resourceDirectory}, or an {@link Image} instance
147+
* @param template Template {@link Image} instance
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+
template: FirstArgumentType<typeof ScreenClass.prototype.find>,
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 template;
171160

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

0 commit comments

Comments
 (0)