1/*
2 * Copyright (c) 2017, 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     7107905
27 * @summary Test verifies whether equals() and hashCode() methods in
28 *          PackedColorModel works properly.
29 * @run     main PackedColorModelEqualsTest
30 */
31
32import java.awt.color.ColorSpace;
33import java.awt.image.DataBuffer;
34import java.awt.image.DirectColorModel;
35
36public class PackedColorModelEqualsTest {
37
38    private static void verifyEquals(DirectColorModel m1,
39                                     DirectColorModel m2) {
40        if (m1.equals(null)) {
41            throw new RuntimeException("equals(null) returns true");
42        }
43        if (!(m1.equals(m2))) {
44            throw new RuntimeException("equals() method is not working"
45                    + " properly");
46        }
47        if (!(m2.equals(m1))) {
48            throw new RuntimeException("equals() method is not working"
49                    + " properly");
50        }
51        if (m1.hashCode() != m2.hashCode()) {
52            throw new RuntimeException("HashCode is not same for same"
53                    + " PackedColorModels");
54        }
55    }
56
57    private static void testMaskArrayEquality() {
58        /*
59         * Test with different maskArray values, since PackedColorModel
60         * is abstract we use subclass DirectColorModel.
61         */
62        DirectColorModel model1 =
63            new DirectColorModel(24, 0x00FF0000, 0x0000FF00, 0x000000FF);
64        DirectColorModel model2 =
65            new DirectColorModel(24, 0x000000FF, 0x0000FF00, 0x00FF0000);
66        if (model1.equals(model2)) {
67            throw new RuntimeException("equals() method is determining"
68                    + " ColorMap equality improperly");
69        }
70        if (model2.equals(model1)) {
71            throw new RuntimeException("equals() method is determining"
72                    + " ColorMap equality improperly");
73        }
74    }
75
76    private static void testConstructor1() {
77        /*
78         * verify equality with constructor
79         * DirectColorModel(int bits, int rmask, int gmask, int bmask,
80         *    int amask)
81         */
82        DirectColorModel model1 =
83            new DirectColorModel(32, 0xFF000000, 0x00FF0000,
84                    0x0000FF00, 0x000000FF);
85        DirectColorModel model2 =
86            new DirectColorModel(32, 0xFF000000, 0x00FF0000,
87                    0x0000FF00, 0x000000FF);
88        verifyEquals(model1, model2);
89    }
90
91    private static void testConstructor2() {
92        /*
93         * verify equality with constructor
94         * DirectColorModel(ColorSpace space, int bits, int rmask, int gmask,
95         * int bmask, int amask, boolean isAlphaPremultiplied, int transferType)
96         */
97        DirectColorModel model1 =
98            new DirectColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
99                    32, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF,
100                    false, DataBuffer.TYPE_BYTE);
101        DirectColorModel model2 =
102            new DirectColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
103                    32, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF,
104                    false, DataBuffer.TYPE_BYTE);
105        verifyEquals(model1, model2);
106    }
107
108    private static void testSamePackedColorModel() {
109        testConstructor1();
110        testConstructor2();
111    }
112    public static void main(String[] args) {
113        // test with different mask array.
114        testMaskArrayEquality();
115        // verify PackedColorModel equality using different constructors.
116        testSamePackedColorModel();
117    }
118}
119
120