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