blob: 76217ae86870a64cfddd61e974c61d6a899263c2 [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;
rupashkab1c369f2011-03-14 15:01:53 +030027import java.util.LinkedList;
28import java.util.List;
rupashka3bcc9e92011-03-03 17:47:41 +030029
30/**
31 * <p>This class contains utilities useful for regression testing.
32 * <p>When using jtreg you would include this class via something like:
33 * <pre>
34 *
35 * @library ../../regtesthelpers
36 * @build Util
37 * </pre>
38 */
39
40public class Util {
41 /**
42 * Convert a rectangle from coordinate system of Component c to
43 * screen coordinate system.
44 *
45 * @param r a non-null Rectangle
46 * @param c a Component whose coordinate system is used for conversion
47 */
48 public static void convertRectToScreen(Rectangle r, Component c) {
49 Point p = new Point(r.x, r.y);
50 SwingUtilities.convertPointToScreen(p, c);
51 r.x = p.x;
52 r.y = p.y;
53 }
54
55 /**
56 * Compares two bufferedImages pixel-by-pixel.
57 * return true if all pixels in the two areas are identical
58 */
59 public static boolean compareBufferedImages(BufferedImage bufferedImage0, BufferedImage bufferedImage1) {
60 int width = bufferedImage0.getWidth();
61 int height = bufferedImage0.getHeight();
62
63 if (width != bufferedImage1.getWidth() || height != bufferedImage1.getHeight()) {
64 return false;
65 }
66
67 for (int y = 0; y < height; y++) {
68 for (int x = 0; x < width; x++) {
69 if (bufferedImage0.getRGB(x, y) != bufferedImage1.getRGB(x, y)) {
70 return false;
71 }
72 }
73 }
74
75 return true;
76 }
rupashkab1c369f2011-03-14 15:01:53 +030077
78 /**
79 * Fills the heap until OutOfMemoryError occurs. This method is useful for
80 * WeakReferences removing.
81 */
82 public static void generateOOME() {
83 List<Object> bigLeak = new LinkedList<Object>();
84
85 boolean oome = false;
86
87 System.out.print("Filling the heap");
88
89 try {
90 for(int i = 0; true ; i++) {
91 // Now, use up all RAM
92 bigLeak.add(new byte[1024 * 1024]);
93
94 System.out.print(".");
95
96 // Give the GC a change at that weakref
97 if (i % 10 == 0) {
98 System.gc();
99 try {
100 Thread.sleep(100);
101 } catch (InterruptedException e) {
102 e.printStackTrace();
103 }
104 }
105 }
106 } catch (OutOfMemoryError e) {
107 bigLeak = null;
108 oome = true;
109 }
110
111 System.out.println("");
112
113 if (!oome) {
114 throw new RuntimeException("Problem with test case - never got OOME");
115 }
116
117 System.out.println("Got OOME");
118 }
rupashka3bcc9e92011-03-03 17:47:41 +0300119}