|
1 |
| -import {join} from "path"; |
2 |
| -import {cwd} from "process"; |
3 |
| -import {VisionAdapter} from "./adapter/vision.adapter.class"; |
4 |
| -import {Image} from "./image.class"; |
5 |
| -import {LocationParameters} from "./locationparameters.class"; |
6 |
| -import {MatchRequest} from "./match-request.class"; |
7 |
| -import {MatchResult} from "./match-result.class"; |
8 |
| -import {Region} from "./region.class"; |
9 |
| -import {Screen} from "./screen.class"; |
| 1 | +import { join } from "path"; |
| 2 | +import { cwd } from "process"; |
| 3 | +import { VisionAdapter } from "./adapter/vision.adapter.class"; |
| 4 | +import { Image } from "./image.class"; |
| 5 | +import { LocationParameters } from "./locationparameters.class"; |
| 6 | +import { MatchRequest } from "./match-request.class"; |
| 7 | +import { MatchResult } from "./match-result.class"; |
| 8 | +import { Region } from "./region.class"; |
| 9 | +import { Screen } from "./screen.class"; |
| 10 | +import { mockPartial } from "sneer"; |
| 11 | +import { FileType } from "./file-type.enum"; |
10 | 12 |
|
11 | 13 | jest.mock("./adapter/native.adapter.class");
|
12 | 14 | jest.mock("./adapter/vision.adapter.class");
|
@@ -269,4 +271,85 @@ describe("Screen.", () => {
|
269 | 271 | expect(matchRegion).toEqual(expectedMatchRegion);
|
270 | 272 | })
|
271 | 273 |
|
| 274 | + describe("capture",() => { |
| 275 | + it("should capture the whole screen and save image", async() => { |
| 276 | + |
| 277 | + // GIVEN |
| 278 | + const screenshot = mockPartial<Image>({data: "pretty pretty image"}); |
| 279 | + VisionAdapter.prototype.grabScreen = jest.fn(() => Promise.resolve(screenshot)); |
| 280 | + VisionAdapter.prototype.saveImage = jest.fn(); |
| 281 | + const visionAdapterMock = new VisionAdapter(); |
| 282 | + const SUT = new Screen(visionAdapterMock); |
| 283 | + const imageName = "foobar.png" |
| 284 | + const expectedImagePath = join(cwd(), imageName) |
| 285 | + |
| 286 | + // WHEN |
| 287 | + const imagePath = await SUT.capture(imageName) |
| 288 | + |
| 289 | + // THEN |
| 290 | + expect(imagePath).toBe(expectedImagePath) |
| 291 | + expect(VisionAdapter.prototype.grabScreen).toHaveBeenCalled() |
| 292 | + expect(VisionAdapter.prototype.saveImage).toHaveBeenCalledWith(screenshot,expectedImagePath) |
| 293 | + }) |
| 294 | + |
| 295 | + it("should consider output configuration", async () => { |
| 296 | + |
| 297 | + // GIVEN |
| 298 | + const visionAdapterMock = new VisionAdapter(); |
| 299 | + const SUT = new Screen(visionAdapterMock); |
| 300 | + const imageName = "foobar" |
| 301 | + const filePath = "/path/to/file" |
| 302 | + const prefix = "answer_" |
| 303 | + const postfix = "_42" |
| 304 | + const expectedImagePath = join(filePath, `${prefix}${imageName}${postfix}${FileType.JPG.toString()}`) |
| 305 | + |
| 306 | + // WHEN |
| 307 | + const imagePath = await SUT.capture(imageName, FileType.JPG, filePath, prefix, postfix) |
| 308 | + |
| 309 | + // THEN |
| 310 | + expect(imagePath).toBe(expectedImagePath) |
| 311 | + }) |
| 312 | + }) |
| 313 | + |
| 314 | + describe("captureRegion", () => { |
| 315 | + |
| 316 | + it("should capture the specified region of the screen and save image", async () => { |
| 317 | + // GIVEN |
| 318 | + const screenshot = mockPartial<Image>({data: "pretty partial image"}); |
| 319 | + const regionToCapture = mockPartial<Region>({top:42, left:9, height: 10, width: 3.14159265359}) |
| 320 | + VisionAdapter.prototype.grabScreenRegion = jest.fn(() => Promise.resolve(screenshot)); |
| 321 | + VisionAdapter.prototype.saveImage = jest.fn(); |
| 322 | + const visionAdapterMock = new VisionAdapter(); |
| 323 | + const SUT = new Screen(visionAdapterMock); |
| 324 | + const imageName = "foobar.png" |
| 325 | + const expectedImagePath = join(cwd(), imageName) |
| 326 | + |
| 327 | + // WHEN |
| 328 | + const imagePath = await SUT.captureRegion(imageName, regionToCapture) |
| 329 | + |
| 330 | + // THEN |
| 331 | + expect(imagePath).toBe(expectedImagePath) |
| 332 | + expect(VisionAdapter.prototype.grabScreenRegion).toHaveBeenCalledWith(regionToCapture) |
| 333 | + expect(VisionAdapter.prototype.saveImage).toHaveBeenCalledWith(screenshot,expectedImagePath) |
| 334 | + }) |
| 335 | + |
| 336 | + it("should consider output configuration", async () => { |
| 337 | + |
| 338 | + // GIVEN |
| 339 | + const regionToCapture = mockPartial<Region>({top:42, left:9, height: 10, width: 3.14159265359}) |
| 340 | + const visionAdapterMock = new VisionAdapter(); |
| 341 | + const SUT = new Screen(visionAdapterMock); |
| 342 | + const imageName = "foobar" |
| 343 | + const filePath = "/path/to/file" |
| 344 | + const prefix = "answer_" |
| 345 | + const postfix = "_42" |
| 346 | + const expectedImagePath = join(filePath, `${prefix}${imageName}${postfix}${FileType.JPG.toString()}`) |
| 347 | + |
| 348 | + // WHEN |
| 349 | + const imagePath = await SUT.captureRegion(imageName, regionToCapture, FileType.JPG, filePath, prefix, postfix) |
| 350 | + |
| 351 | + // THEN |
| 352 | + expect(imagePath).toBe(expectedImagePath) |
| 353 | + }) |
| 354 | + }) |
272 | 355 | });
|
0 commit comments