View Javadoc

1   package com.atlassian.selenium.visualcomparison.test;
2   
3   import java.io.File;
4   import java.io.IOException;
5   import java.util.Arrays;
6   import java.util.List;
7   import junit.framework.Assert;
8   import org.junit.Test;
9   import com.atlassian.selenium.visualcomparison.utils.BoundingBox;
10  import com.atlassian.selenium.visualcomparison.utils.Screenshot;
11  import com.atlassian.selenium.visualcomparison.utils.ScreenshotDiff;
12  
13  public class TestDiff 
14  {
15  	private Screenshot screenshot1;
16  	private Screenshot screenshot2;
17  
18  	public TestDiff () throws IOException
19  	{
20  		screenshot1 = new Screenshot (new File("src/test/resources/EAC-1.1024x768.png"));
21  		screenshot2 = new Screenshot (new File("src/test/resources/EAC-2.1024x768.png"));
22  	}
23  
24      @Test
25      public void testDiffIdentical() throws IOException
26      {	
27      	// The same screenshot compared to itself should have no differences.
28      	ScreenshotDiff diff = screenshot1.getDiff(screenshot1);
29      	Assert.assertFalse("Identical images should have no differences", diff.hasDifferences());
30      	Assert.assertEquals("Should be no areas of difference", 0, diff.getDiffAreas().size());
31      }
32  	
33      @Test
34      public void testDiffSameSizeDifferentContent() throws IOException
35      {	
36      	// Two screenshots of the same size but with different content should have differences.
37      	ScreenshotDiff diff = screenshot1.getDiff(screenshot2);
38      	Assert.assertTrue("Different images should have differences", diff.hasDifferences());
39      	List<BoundingBox> boxes = diff.getDiffAreas();
40      	Assert.assertEquals("Should be only one area of difference", 1, boxes.size());
41      	BoundingBox box = boxes.get(0);
42      	Assert.assertEquals("Bounding box left should be correct", 459, box.getLeft());
43      	Assert.assertEquals("Bounding box top should be correct", 163, box.getTop());
44      	Assert.assertEquals("Bounding box right should be correct", 1069, box.getRight());
45      	Assert.assertEquals("Bounding box bottom should be correct", 633, box.getBottom());
46      }
47      
48      @Test
49      public void testDiffIgnoreEntireDifferentArea() throws IOException
50      {
51      	// If all differences are ignored, they should be the same.
52      	// Two screenshots of the same size but with different content should have differences.
53      	ScreenshotDiff diff = screenshot1.getDiff(screenshot2, Arrays.asList(new BoundingBox [] { new BoundingBox (459, 163, 1069, 633) }), false);
54      	Assert.assertFalse("All differences should be ignored", diff.hasDifferences());
55      	Assert.assertEquals("Should be no areas of difference", 0, diff.getDiffAreas().size());
56      }
57  
58      @Test
59      public void testDiffIgnorePartialDifferentAreaVertical() throws Exception
60      {	
61      	// One large failure area with an ignored vertical stripe across the middle
62      	ScreenshotDiff diff = screenshot1.getDiff(screenshot2, Arrays.asList(new BoundingBox [] { new BoundingBox (500, 163, 550, 633) }), false);
63      	Assert.assertTrue("Different images should have differences", diff.hasDifferences());
64      	List<BoundingBox> boxes = diff.getDiffAreas();
65      	Assert.assertEquals("Should be four areas of difference with an ignored area in between", 4, boxes.size());
66  
67      	BoundingBox box1 = boxes.get(0);
68      	Assert.assertEquals("Bounding box 1 left should be correct", 459, box1.getLeft());
69      	Assert.assertEquals("Bounding box 1 top should be correct", 163, box1.getTop());
70      	Assert.assertEquals("Bounding box 1 right should be correct", 499, box1.getRight());
71      	Assert.assertEquals("Bounding box 1 bottom should be correct", 292, box1.getBottom());
72      
73      	BoundingBox box2 = boxes.get(1);
74      	Assert.assertEquals("Bounding box 2 left should be correct", 459, box2.getLeft());
75      	Assert.assertEquals("Bounding box 2 top should be correct", 339, box2.getTop());
76      	Assert.assertEquals("Bounding box 2 right should be correct", 499, box2.getRight());
77      	Assert.assertEquals("Bounding box 2 bottom should be correct", 415, box2.getBottom());
78      	
79      	BoundingBox box3 = boxes.get(2);
80      	Assert.assertEquals("Bounding box 3 left should be correct", 459, box3.getLeft());
81      	Assert.assertEquals("Bounding box 3 top should be correct", 441, box3.getTop());
82      	Assert.assertEquals("Bounding box 3 right should be correct", 499, box3.getRight());
83      	Assert.assertEquals("Bounding box 3 bottom should be correct", 633, box3.getBottom());
84      	
85      	BoundingBox box4 = boxes.get(3);
86      	Assert.assertEquals("Bounding box 4 left should be correct", 551, box4.getLeft());
87      	Assert.assertEquals("Bounding box 4 top should be correct", 163, box4.getTop());
88      	Assert.assertEquals("Bounding box 4 right should be correct", 1069, box4.getRight());
89      	Assert.assertEquals("Bounding box 4 bottom should be correct", 633, box4.getBottom());
90      }
91      
92      @Test
93      public void testDiffIgnorePartialDifferentAreaHorizontal() throws IOException
94      {	
95      	// One large failure area with an ignored horizontal stripe across the middle
96      	ScreenshotDiff diff = screenshot1.getDiff(screenshot2, Arrays.asList(new BoundingBox [] { new BoundingBox (459, 300, 1069, 400) }), false);
97      	Assert.assertTrue("Different images should have differences", diff.hasDifferences());
98      	List<BoundingBox> boxes = diff.getDiffAreas();
99      	Assert.assertEquals("Should be two areas of difference with an ignored area in between", 2, boxes.size());
100     	
101     	BoundingBox box1 = boxes.get(0);
102     	Assert.assertEquals("Bounding box 1 left should be correct", 459, box1.getLeft());
103     	Assert.assertEquals("Bounding box 1 top should be correct", 163, box1.getTop());
104     	Assert.assertEquals("Bounding box 1 right should be correct", 1069, box1.getRight());
105     	Assert.assertEquals("Bounding box 1 bottom should be correct", 299, box1.getBottom());
106     
107     	BoundingBox box2 = boxes.get(1);
108     	Assert.assertEquals("Bounding box 2 left should be correct", 459, box2.getLeft());
109     	Assert.assertEquals("Bounding box 2 top should be correct", 401, box2.getTop());
110     	Assert.assertEquals("Bounding box 2 right should be correct", 1069, box2.getRight());
111     	Assert.assertEquals("Bounding box 2 bottom should be correct", 633, box2.getBottom());
112     }
113     
114 }