blob: 68bc0f8c4040f99f613d853e123632dbe7b191bd [file] [log] [blame]
rupashka3bcc9e92011-03-03 17:47:41 +03001/*
2 * Copyright (c) 2011, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24import javax.swing.*;
25import java.awt.*;
26import java.awt.image.BufferedImage;
rupashka42a48d92012-04-10 19:09:48 +030027import java.util.ArrayList;
rupashkab1c369f2011-03-14 15:01:53 +030028import java.util.LinkedList;
29import java.util.List;
rupashka42a48d92012-04-10 19:09:48 +030030import java.util.concurrent.Callable;
rupashka3bcc9e92011-03-03 17:47:41 +030031
32/**
33 * <p>This class contains utilities useful for regression testing.
34 * <p>When using jtreg you would include this class via something like:
35 * <pre>
36 *
37 * @library ../../regtesthelpers
38 * @build Util
39 * </pre>
40 */
41
42public class Util {
43 /**
44 * Convert a rectangle from coordinate system of Component c to
45 * screen coordinate system.
46 *
47 * @param r a non-null Rectangle
48 * @param c a Component whose coordinate system is used for conversion
49 */
50 public static void convertRectToScreen(Rectangle r, Component c) {
51 Point p = new Point(r.x, r.y);
52 SwingUtilities.convertPointToScreen(p, c);
53 r.x = p.x;
54 r.y = p.y;
55 }
56
57 /**
58 * Compares two bufferedImages pixel-by-pixel.
59 * return true if all pixels in the two areas are identical
60 */
61 public static boolean compareBufferedImages(BufferedImage bufferedImage0, BufferedImage bufferedImage1) {
62 int width = bufferedImage0.getWidth();
63 int height = bufferedImage0.getHeight();
64
65 if (width != bufferedImage1.getWidth() || height != bufferedImage1.getHeight()) {
66 return false;
67 }
68
69 for (int y = 0; y < height; y++) {
70 for (int x = 0; x < width; x++) {
71 if (bufferedImage0.getRGB(x, y) != bufferedImage1.getRGB(x, y)) {
72 return false;
73 }
74 }
75 }
76
77 return true;
78 }
rupashkab1c369f2011-03-14 15:01:53 +030079
80 /**
81 * Fills the heap until OutOfMemoryError occurs. This method is useful for
82 * WeakReferences removing.
83 */
84 public static void generateOOME() {
85 List<Object> bigLeak = new LinkedList<Object>();
86
87 boolean oome = false;
88
89 System.out.print("Filling the heap");
90
91 try {
92 for(int i = 0; true ; i++) {
93 // Now, use up all RAM
94 bigLeak.add(new byte[1024 * 1024]);
95
96 System.out.print(".");
97
98 // Give the GC a change at that weakref
99 if (i % 10 == 0) {
100 System.gc();
101 try {
102 Thread.sleep(100);
103 } catch (InterruptedException e) {
104 e.printStackTrace();
105 }
106 }
107 }
108 } catch (OutOfMemoryError e) {
109 bigLeak = null;
110 oome = true;
111 }
112
113 System.out.println("");
114
115 if (!oome) {
116 throw new RuntimeException("Problem with test case - never got OOME");
117 }
118
119 System.out.println("Got OOME");
120 }
rupashkabef801e2011-10-11 15:22:40 +0400121
122 /**
123 * Find a sub component by class name.
124 * Always run this method on the EDT thread
125 */
126 public static Component findSubComponent(Component parent, String className) {
127 String parentClassName = parent.getClass().getName();
128
129 if (parentClassName.contains(className)) {
130 return parent;
131 }
132
133 if (parent instanceof Container) {
134 for (Component child : ((Container) parent).getComponents()) {
135 Component subComponent = findSubComponent(child, className);
136
137 if (subComponent != null) {
138 return subComponent;
139 }
140 }
141 }
142
143 return null;
144 }
rupashkad37d5142011-11-21 18:22:30 +0400145
146 /**
147 * Hits keys by robot.
148 */
149 public static void hitKeys(Robot robot, int... keys) {
150 for (int i = 0; i < keys.length; i++) {
151 robot.keyPress(keys[i]);
152 }
153
154 for (int i = keys.length - 1; i >= 0; i--) {
155 robot.keyRelease(keys[i]);
156 }
157 }
rupashka42a48d92012-04-10 19:09:48 +0300158
159 /**
160 * Invokes the <code>task</code> on the EDT thread.
161 *
162 * @return result of the <code>task</code>
163 */
164 public static <T> T invokeOnEDT(final Callable<T> task) throws Exception {
165 final List<T> result = new ArrayList<>(1);
166 final Exception[] exception = new Exception[1];
167
168 SwingUtilities.invokeAndWait(new Runnable() {
169 @Override
170 public void run() {
171 try {
172 result.add(task.call());
173 } catch (Exception e) {
174 exception[0] = e;
175 }
176 }
177 });
178
179 if (exception[0] != null) {
180 throw exception[0];
181 }
182
183 return result.get(0);
184 }
rupashka3bcc9e92011-03-03 17:47:41 +0300185}