1/*
2 * Copyright (c) 2012, 2013, 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
24/*
25 * @test
26 * @bug 4726194 7124209
27 * @summary Tests for 4726194
28 * @author Phil Milne
29 */
30import java.awt.*;
31import java.lang.reflect.InvocationTargetException;
32import java.util.*;
33import java.util.List;
34import javax.swing.*;
35
36public class bug4726194 {
37
38    private static String[] hConstraints = {SpringLayout.WEST, "Width", SpringLayout.EAST, SpringLayout.HORIZONTAL_CENTER};
39    private static String[] vConstraints = {SpringLayout.NORTH, "Height", SpringLayout.SOUTH, SpringLayout.VERTICAL_CENTER, SpringLayout.BASELINE};
40    private static int[] FAIL = new int[3];
41    private static boolean TEST_DUPLICATES = false;
42
43    public static void main(String[] args) {
44        try {
45            SwingUtilities.invokeAndWait(new Runnable() {
46                @Override
47                public void run() {
48                    int minLevel = 2;
49                    int maxLevel = 2;
50                    for (int i = minLevel; i <= maxLevel; i++) {
51                        test(i, true);
52                        test(i, false);
53                    }
54                }
55            });
56        } catch (InterruptedException | InvocationTargetException ex) {
57            ex.printStackTrace();
58            throw new RuntimeException("FAILED: SwingUtilities.invokeAndWait method failed!");
59        }
60    }
61
62    public static void test(int level, boolean horizontal) {
63        List result = new ArrayList();
64        String[] constraints = horizontal ? hConstraints : vConstraints;
65        test(level, constraints, result, Arrays.asList(new Object[level]));
66        JTextField tf = new JTextField("");
67        tf.setFont(new Font("Dialog", Font.PLAIN, 6));
68        System.out.print("\t\t");
69        for (int j = 0; j < constraints.length; j++) {
70            String constraint = constraints[j];
71            System.out.print(constraint + "                ".substring(constraint.length()));
72        }
73        System.out.println("");
74        for (int i = 0; i < result.size(); i++) {
75            SpringLayout.Constraints c = new SpringLayout.Constraints(tf);
76            List cc = (List) result.get(i);
77            for (int j = 0; j < cc.size(); j++) {
78                String constraint = (String) cc.get(j);
79                c.setConstraint(constraint, Spring.constant((j + 1) * 10));
80            }
81            System.out.print(" Input:\t\t");
82            for (int j = 0; j < constraints.length; j++) {
83                String constraint = constraints[j];
84                int jj = cc.indexOf(constraint);
85                String val = cc.contains(constraint) ? Integer.toString((jj + 1) * 10) : "?";
86                System.out.print(val + "\t\t");
87            }
88            System.out.println("");
89            System.out.print("Output:\t\t");
90            for (int j = 0; j < constraints.length; j++) {
91                String constraint = constraints[j];
92                Spring spring = c.getConstraint(constraint);
93                String springVal = (spring == null) ? "?" : Integer.toString(spring.getValue());
94                System.out.print(springVal);
95                System.out.print("\t\t");
96            }
97            for (int j = 0; j < cc.size(); j++) {
98                String constraint = (String) cc.get(j);
99                Spring con = c.getConstraint(constraint);
100                if (con == null || con.getValue() != (j + 1) * 10) {
101                    throw new RuntimeException("Values are wrong!!! ");
102                }
103            }
104            if (horizontal) {
105                int[] a1 = getValues(c, new String[]{SpringLayout.WEST, SpringLayout.WIDTH, SpringLayout.EAST});
106                if (a1[0] + a1[1] != a1[2]) {
107                    throw new RuntimeException("WEST + WIDTH != EAST!!! ");
108                }
109                int[] a2 = getValues(c, new String[]{SpringLayout.WEST, SpringLayout.WIDTH, SpringLayout.HORIZONTAL_CENTER});
110                if (a2[0] + a2[1] / 2 != a2[2]) {
111                    throw new RuntimeException("WEST + WIDTH/2 != HORIZONTAL_CENTER!!! ");
112                }
113            } else {
114                int[] a3 = getValues(c, new String[]{SpringLayout.NORTH, SpringLayout.HEIGHT, SpringLayout.SOUTH});
115                if (a3[0] + a3[1] != a3[2]) {
116                    throw new RuntimeException("NORTH + HEIGHT != SOUTH!!! ");
117                }
118                int[] a4 = getValues(c, new String[]{SpringLayout.NORTH, SpringLayout.HEIGHT, SpringLayout.VERTICAL_CENTER});
119                int vcDiff = Math.abs(a4[0] + a4[1] / 2 - a4[2]);
120                if (vcDiff > 1) {
121                    throw new RuntimeException("NORTH + HEIGHT/2 != VERTICAL_CENTER!!! ");
122                }
123                int[] a5 = getValues(c, new String[]{SpringLayout.NORTH, SpringLayout.BASELINE, SpringLayout.SOUTH});
124                if (a5[0] > a5[1] != a5[1] > a5[2]) {
125                    throw new RuntimeException("BASELINE is not in the range: [NORTH, SOUTH]!!!");
126                }
127            }
128            System.out.println("");
129        }
130        System.out.println("");
131    }
132
133    private static int[] getValues(SpringLayout.Constraints con, String[] cNames) {
134        int[] result = new int[cNames.length];
135        for (int i = 0; i < cNames.length; i++) {
136            String name = cNames[i];
137            Spring s = con.getConstraint(name);
138            if (s == null) {
139                System.out.print("Warning: " + name + " is undefined. ");
140                return FAIL;
141            }
142            result[i] = s.getValue();
143        }
144        return result;
145    }
146
147    public static void test(int level, String[] constraints, List result, List soFar) {
148        if (level == 0) {
149            result.add(soFar);
150            return;
151        }
152        for (int i = 0; i < constraints.length; i++) {
153            if (soFar.contains(constraints[i]) && !TEST_DUPLICATES) {
154                continue;
155            }
156            List child = new ArrayList(soFar);
157            child.set(level - 1, constraints[i]);
158            test(level - 1, constraints, result, child);
159        }
160    }
161}
162