summaryrefslogtreecommitdiffstats
path: root/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_RGB.java
blob: 29db52109d8b9ce9c5f65a7bc813f99bf6e43c62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*******************************************************************************
 * Copyright (c) 2000, 2003 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.swt.tests.junit;

import junit.framework.*;
import junit.textui.*;
import org.eclipse.swt.graphics.*;

/**
 * Automated Test Suite for class org.eclipse.swt.graphics.RGB
 *
 * @see org.eclipse.swt.graphics.RGB
 */
public class Test_org_eclipse_swt_graphics_RGB extends SwtTestCase {

public Test_org_eclipse_swt_graphics_RGB(String name) {
	super(name);
}

public static void main(String[] args) {
	TestRunner.run(suite());
}

protected void setUp() {
}

protected void tearDown() {
}

public void test_ConstructorIII() {
	// Test RGB(int red, int green, int blue)
	RGB rgb = new RGB(20,100,200);
	
	rgb = new RGB(0,0,0);

	rgb = new RGB(255,255,255);

	try {
		rgb = new RGB(-1, 20, 50);
		fail("No exception thrown for red < 0");
	}
	catch (IllegalArgumentException e) {
	}
	
	try {
		rgb = new RGB(256, 20, 50);
		fail("No exception thrown for red > 255");
	}
	catch (IllegalArgumentException e) {
	}
	
	try {
		rgb = new RGB(20, -1, 50);
		fail("No exception thrown for green < 0");
	}
	catch (IllegalArgumentException e) {
	}
	
	try {
		rgb = new RGB(20, 256, 50);
		fail("No exception thrown for green > 255");
	}
	catch (IllegalArgumentException e) {
	}

	try {
		rgb = new RGB(20, 50, -1);
		fail("No exception thrown for blue < 0");
	}
	catch (IllegalArgumentException e) {
	}
	
	try {
		rgb = new RGB(20, 50, 256);
		fail("No exception thrown for blue > 255");
	}
	catch (IllegalArgumentException e) {
	}
			
}

public void test_equalsLjava_lang_Object() {
	int r = 0, g = 127, b = 254;
	RGB rgb1 = new RGB(r, g, b);
	RGB rgb2;
	
	rgb2 = rgb1;
	if (!rgb1.equals(rgb2)) {
		fail("Two references to the same RGB instance not found equal");
	}
	
	rgb2 = new RGB(r, g, b);
	if (!rgb1.equals(rgb2)) {
		fail("References to two different RGB instances with same R G B parameters not found equal");
	}
	
	if (rgb1.equals(new RGB(r+1, g, b)) ||
	    rgb1.equals(new RGB(r, g+1, b)) ||
	    rgb1.equals(new RGB(r, g, b+1)) ||
	    rgb1.equals(new RGB(r+1, g+1, b+1))) {
		fail("Comparing two RGB instances with different combination of R G B parameters found equal");    	
	}
}

public void test_hashCode() {
	int r = 255, g = 100, b = 0;
	RGB rgb1 = new RGB(r, g, b);
	RGB rgb2 = new RGB(r, g, b);
	
	int hash1 = rgb1.hashCode();
	int hash2 = rgb2.hashCode();
	
	if (hash1 != hash2) {
		fail("Two RGB instances with same R G B parameters returned different hash codes");
	}
	
	if (rgb1.hashCode() == new RGB(g, b, r).hashCode() ||
		rgb1.hashCode() == new RGB(b, r, g).hashCode()) {
		fail("Two RGB instances with different R G B parameters returned the same hash code");
	}
}

public void test_toString() {
	RGB rgb = new RGB(0, 100, 200);

	String s = rgb.toString();
	
	if (s == null || s.length() == 0) {
		fail("RGB.toString returns a null or empty String");
	}
}

public static Test suite() {
	TestSuite suite = new TestSuite();
	java.util.Vector methodNames = methodNames();
	java.util.Enumeration e = methodNames.elements();
	while (e.hasMoreElements()) {
		suite.addTest(new Test_org_eclipse_swt_graphics_RGB((String)e.nextElement()));
	}
	return suite;
}
public static java.util.Vector methodNames() {
	java.util.Vector methodNames = new java.util.Vector();
	methodNames.addElement("test_ConstructorIII");
	methodNames.addElement("test_equalsLjava_lang_Object");
	methodNames.addElement("test_hashCode");
	methodNames.addElement("test_toString");
	return methodNames;
}
protected void runTest() throws Throwable {
	if (getName().equals("test_ConstructorIII")) test_ConstructorIII();
	else if (getName().equals("test_equalsLjava_lang_Object")) test_equalsLjava_lang_Object();
	else if (getName().equals("test_hashCode")) test_hashCode();
	else if (getName().equals("test_toString")) test_toString();
}
}