887
|
1 |
/*
|
|
2 |
* Copyright 2005-2008 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
import java.awt.AWTException;
|
|
25 |
import java.awt.Color;
|
|
26 |
import java.awt.Component;
|
|
27 |
import java.awt.Dimension;
|
|
28 |
import java.awt.Frame;
|
|
29 |
import java.awt.Graphics;
|
|
30 |
import java.awt.Point;
|
|
31 |
import java.awt.Rectangle;
|
|
32 |
import java.awt.Robot;
|
|
33 |
import java.awt.Toolkit;
|
|
34 |
import java.awt.image.BufferedImage;
|
|
35 |
import java.awt.image.VolatileImage;
|
|
36 |
|
|
37 |
/*
|
|
38 |
*
|
|
39 |
*
|
|
40 |
* Tests that the use of shared memory pixmaps isn't broken:
|
|
41 |
* create a VolatileImage, fill it with red color, copy it to the screen
|
|
42 |
* make sure the pixels on the screen are red.
|
|
43 |
*
|
|
44 |
* Note that we force the use of shared memory pixmaps in the shell script.
|
|
45 |
*
|
|
46 |
* @author Dmitri.Trembovetski
|
|
47 |
*/
|
|
48 |
|
|
49 |
public class SharedMemoryPixmapsTest {
|
|
50 |
static final int IMAGE_SIZE = 100;
|
|
51 |
static boolean show = false;
|
|
52 |
final Frame testFrame;
|
|
53 |
/** Creates a new instance of SharedMemoryPixmapsTest */
|
|
54 |
public SharedMemoryPixmapsTest() {
|
|
55 |
testFrame = new Frame("SharedMemoryPixmapsTest");
|
|
56 |
testFrame.add(new TestComponent());
|
|
57 |
testFrame.pack();
|
|
58 |
testFrame.setVisible(true);
|
|
59 |
}
|
|
60 |
|
|
61 |
public static void main(String[] args) {
|
|
62 |
for (String s : args) {
|
|
63 |
if ("-show".equals(s)) {
|
|
64 |
show = true;
|
|
65 |
} else {
|
|
66 |
System.err.println("Usage: SharedMemoryPixmapsTest [-show]");
|
|
67 |
}
|
|
68 |
}
|
|
69 |
new SharedMemoryPixmapsTest();
|
|
70 |
}
|
|
71 |
|
|
72 |
private class TestComponent extends Component {
|
|
73 |
VolatileImage vi = null;
|
|
74 |
boolean tested = false;
|
|
75 |
|
|
76 |
void initVI() {
|
|
77 |
int res;
|
|
78 |
if (vi == null) {
|
|
79 |
res = VolatileImage.IMAGE_INCOMPATIBLE;
|
|
80 |
} else {
|
|
81 |
res = vi.validate(getGraphicsConfiguration());
|
|
82 |
}
|
|
83 |
if (res == VolatileImage.IMAGE_INCOMPATIBLE) {
|
|
84 |
if (vi != null) vi.flush();
|
|
85 |
vi = createVolatileImage(IMAGE_SIZE, IMAGE_SIZE);
|
|
86 |
vi.validate(getGraphicsConfiguration());
|
|
87 |
res = VolatileImage.IMAGE_RESTORED;
|
|
88 |
}
|
|
89 |
if (res == VolatileImage.IMAGE_RESTORED) {
|
|
90 |
Graphics vig = vi.getGraphics();
|
|
91 |
vig.setColor(Color.red);
|
|
92 |
vig.fillRect(0, 0, vi.getWidth(), vi.getHeight());
|
|
93 |
vig.dispose();
|
|
94 |
}
|
|
95 |
}
|
|
96 |
|
|
97 |
@Override
|
|
98 |
public synchronized void paint(Graphics g) {
|
|
99 |
do {
|
|
100 |
g.setColor(Color.green);
|
|
101 |
g.fillRect(0, 0, getWidth(), getHeight());
|
|
102 |
|
|
103 |
initVI();
|
|
104 |
g.drawImage(vi, 0, 0, null);
|
|
105 |
} while (vi.contentsLost());
|
|
106 |
|
|
107 |
Toolkit.getDefaultToolkit().sync();
|
|
108 |
if (!tested) {
|
|
109 |
if (testRendering()) {
|
|
110 |
System.err.println("Test Passed");
|
|
111 |
} else {
|
|
112 |
System.err.println("Test Failed");
|
|
113 |
}
|
|
114 |
tested = true;
|
|
115 |
}
|
|
116 |
if (!show) {
|
|
117 |
testFrame.setVisible(false);
|
|
118 |
testFrame.dispose();
|
|
119 |
}
|
|
120 |
}
|
|
121 |
|
|
122 |
private boolean testRendering() throws RuntimeException {
|
|
123 |
Robot r = null;
|
|
124 |
try {
|
|
125 |
r = new Robot();
|
|
126 |
} catch (AWTException ex) {
|
|
127 |
ex.printStackTrace();
|
|
128 |
throw new RuntimeException("Can't create Robot");
|
|
129 |
}
|
|
130 |
Point p = getLocationOnScreen();
|
|
131 |
BufferedImage b =
|
|
132 |
r.createScreenCapture(new Rectangle(p, getPreferredSize()));
|
|
133 |
for (int y = 0; y < b.getHeight(); y++) {
|
|
134 |
for (int x = 0; x < b.getWidth(); x++) {
|
|
135 |
if (b.getRGB(x, y) != Color.red.getRGB()) {
|
|
136 |
System.err.println("Incorrect pixel" + " at "
|
|
137 |
+ x + "x" + y + " : " +
|
|
138 |
Integer.toHexString(b.getRGB(x, y)));
|
|
139 |
if (show) {
|
|
140 |
return false;
|
|
141 |
}
|
|
142 |
System.err.println("Test Failed");
|
|
143 |
System.exit(1);
|
|
144 |
}
|
|
145 |
}
|
|
146 |
}
|
|
147 |
return true;
|
|
148 |
}
|
|
149 |
|
|
150 |
@Override
|
|
151 |
public Dimension getPreferredSize() {
|
|
152 |
return new Dimension(IMAGE_SIZE, IMAGE_SIZE);
|
|
153 |
}
|
|
154 |
}
|
|
155 |
|
|
156 |
}
|