1 package com.atlassian.selenium.visualcomparison.utils;
2
3 import com.atlassian.selenium.visualcomparison.VisualComparableClient;
4
5 import javax.imageio.ImageIO;
6 import java.awt.image.BufferedImage;
7 import java.io.File;
8 import java.io.IOException;
9 import java.util.ArrayList;
10 import java.util.List;
11
12 public class Screenshot implements Comparable<Screenshot>
13 {
14 private ScreenResolution resolution;
15 private String id;
16 private File file;
17
18 public Screenshot(VisualComparableClient client, String id, String imageDir, ScreenResolution resolution) throws IOException
19 {
20 final String filePath = imageDir + "/" + id + "." + resolution + ".png";
21 client.captureEntirePageScreenshot(filePath);
22 init(filePath, id, resolution);
23 }
24
25 public Screenshot(File file) throws IOException
26 {
27 String[] fileNameParts = file.getName().split("\\.");
28 if (fileNameParts.length != 3)
29 {
30 throw new IOException("Invalid screenshot name - " + file.getName());
31 }
32 init(file.getAbsolutePath(), fileNameParts[0], new ScreenResolution(fileNameParts[1]));
33 }
34
35 public static String generateFileName(String id, ScreenResolution resolution)
36 {
37 return id.replace('.', '-') + "." + resolution + ".png";
38 }
39
40 private void init(String filePath, String id, ScreenResolution resolution) throws IOException
41 {
42 this.id = id;
43 this.resolution = resolution;
44 this.file = new File(filePath);
45 }
46
47 public String getFileName()
48 {
49 return file.getName();
50 }
51
52 public String getId()
53 {
54 return id;
55 }
56
57 public ScreenResolution getResolution()
58 {
59 return resolution;
60 }
61
62 public BufferedImage getImage() throws IOException
63 {
64 return ImageIO.read(file);
65 }
66
67 public int compareTo(Screenshot other)
68 {
69 int result = this.id.compareTo(other.id);
70 if (result != 0) {
71 return result;
72 }
73 return this.resolution.compareTo(other.resolution);
74 }
75
76 public ScreenshotDiff getDiff(Screenshot other) throws IOException
77 {
78 return getDiff (other, null, false);
79 }
80
81 public ScreenshotDiff getDiff(Screenshot other, List<BoundingBox> ignoreAreas, boolean ignoreSingleLines) throws IOException
82 {
83 return new ScreenshotDiffer().diff(this, other, ignoreAreas, ignoreSingleLines);
84 }
85 }