update dev300-m58
[ooovba.git] / bean / qa / complex / ScreenComparer.java
blob46f7435723a6522011ba0b99ed7f2d530bdc1c89
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ScreenComparer.java,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 package complex;
33 import complexlib.ComplexTestCase;
34 import java.io.File;
35 import java.awt.Rectangle;
36 import java.awt.BorderLayout;
37 import java.awt.image.BufferedImage;
38 import java.awt.image.PixelGrabber;
39 import java.awt.event.*;
40 import java.awt.Frame;
41 import javax.imageio.ImageIO;
42 import javax.imageio.stream.FileImageOutputStream;
46 class ScreenComparer
48 Rectangle m_rect;
49 BufferedImage m_img1;
50 BufferedImage m_img2;
51 BufferedImage m_imgDiff;
53 int m_diffColor;
54 public ScreenComparer(int x, int y, int width, int height)
56 this(new Rectangle(x, y, width, height));
59 public ScreenComparer(Rectangle location)
61 m_rect = location;
62 int red = 0xff;
63 int alpha = 0xff;
64 m_diffColor = (alpha << 24);
65 m_diffColor = m_diffColor | (red << 16);
68 public ScreenComparer()
70 this(new Rectangle(0, 0, 0, 0));
73 public void reset()
75 m_rect = null;
76 m_img1 = null;
77 m_img2 = null;
78 m_imgDiff = null;
81 public Rectangle getLocation()
83 return m_rect;
85 public void grabOne() throws Exception
87 grabOne(m_rect);
90 public void grabOne(Rectangle r) throws Exception
92 java.awt.Robot robot = new java.awt.Robot();
93 m_img1 = robot.createScreenCapture(r);
96 public void grabTwo() throws Exception
98 grabTwo(m_rect);
101 public void grabTwo(Rectangle r) throws Exception
103 java.awt.Robot robot = new java.awt.Robot();
104 m_img2 = robot.createScreenCapture(r);
107 public boolean compare() throws Exception
109 if (m_img1 == null || m_img2 == null)
110 throw new Exception("Only one image captured!");
111 boolean ret = true;
112 int w1 = m_img1.getWidth();
113 int h1 = m_img1.getHeight();
114 int w2 = m_img2.getWidth();
115 int h2 = m_img2.getHeight();
117 if (w1 != w2 || h1 != h2)
119 System.out.println("### 1\n");
120 //Different size. Create an image that holds both images.
121 int w = w1 > w2 ? w1 : w2;
122 int h = h1 > h2 ? h1 : h2;
123 m_imgDiff = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
124 for (int y = 0; y < h; y ++)
126 for (int x = 0; x < w; x++)
128 boolean bOutOfRange = false;
129 int pixel1 = 0;
130 int pixel2 = 0;
131 //get the pixel for m_img1
132 if (x < w1 && y < h1)
133 pixel1 = m_img1.getRGB(x, y);
134 else
135 bOutOfRange = true;
137 if (x < w2 && y < h2)
138 pixel2 = m_img2.getRGB(x, y);
139 else
140 bOutOfRange = true;
142 if (bOutOfRange || pixel1 != pixel2)
143 m_imgDiff.setRGB(x, y, m_diffColor);
144 else
145 m_imgDiff.setRGB(x, y, pixel1);
149 return false;
152 //Images have same dimension
153 int[] pixels1 = new int[w1 * h1];
154 PixelGrabber pg = new PixelGrabber(
155 m_img1.getSource(), 0, 0, w1, h1, pixels1, 0, w1);
156 pg.grabPixels();
158 int[] pixels2 = new int[w2 * h2];
159 PixelGrabber pg2 = new PixelGrabber(
160 m_img2.getSource(), 0, 0, w2, h2, pixels2, 0, w2);
161 pg2.grabPixels();
163 m_imgDiff = new BufferedImage(w1, h1, BufferedImage.TYPE_INT_ARGB);
165 //First check if the the images differ.
166 int lenAr = pixels1.length;
167 int index = 0;
168 for (index = 0; index < lenAr; index++)
170 if (pixels1[index] != pixels2[index])
171 break;
174 //If the images are different, then create the diff image
175 if (index < lenAr)
177 for (int y = 0; y < h1; y++)
179 for (int x = 0; x < w1; x++)
181 int offset = y * w1 + x;
182 if (pixels1[offset] != pixels2[offset])
184 ret = ret && false;
185 m_imgDiff.setRGB(x, y, m_diffColor);
187 else
189 m_imgDiff.setRGB(x, y, pixels1[offset]);
194 return ret;
197 /** Writes Images to a location. The
198 * directory is determined by the java property OOoBean.Images
201 public void writeImages() throws Exception
203 String imgLocation = System.getProperty("OOoBean.Images", "");
204 File file_tmp = File.createTempFile("OOoBean", "", new File(imgLocation));
205 File file1 = new File(file_tmp.getPath()+".png");
206 file_tmp.delete();
207 if (m_img1 != null)
209 ImageIO.write(m_img1, "png", file1);
210 System.out.println("\nCompared images:");
211 System.out.println("1. " + file1.getPath());
213 file1= null;
214 file_tmp= null;
215 file_tmp = File.createTempFile("OOoBean", "", new File(imgLocation));
216 file1 = new File(file_tmp.getPath()+".png");
217 file_tmp.delete();
218 if (m_img2 != null)
220 ImageIO.write(m_img2, "png", file1);
221 System.out.println("2. " + file1.getPath());
223 file1= null;
224 file_tmp= null;
225 file_tmp = File.createTempFile("OOoBean", "_diff", new File(imgLocation));
226 file1 = new File(file_tmp.getPath()+".png");
227 file_tmp.delete();
228 if (m_imgDiff != null)
230 ImageIO.write(m_imgDiff, "png", file1);
231 System.out.println("Diff image: " + file1.getPath() + "\n");