001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Alexander T. Simbirtsev
019: * @version $Revision$
020: * Created on 23.11.2004
021: * * Window - Preferences - Java - Code Style - Code Templates
022: *
023: */package javax.swing;
024:
025: import java.io.IOException;
026:
027: public class SizeRequirementsTest extends SwingTestCase {
028: /*
029: * Class under test for void SizeRequirements(int, int, int, float)
030: */
031: public void testSizeRequirementsintintintfloat() {
032: int min = 100;
033: int pref = 10;
034: int max = 1;
035: float align = 0.3f;
036: SizeRequirements requirements = new SizeRequirements(min, pref,
037: max, align);
038: assertEquals("Minimum ", min, requirements.minimum);
039: assertEquals("Preferred ", pref, requirements.preferred);
040: assertEquals("Maximum ", max, requirements.maximum);
041: assertEquals("Alignmnent ", align, requirements.alignment, 1e-5);
042: }
043:
044: /*
045: * Class under test for void SizeRequirements(float)
046: */
047: public void testSizeRequirementsfloat() {
048: if (!isHarmony()) {
049: return;
050: }
051: int min = 0;
052: int pref = 0;
053: int max = 0;
054: float align = 0.3f;
055: SizeRequirements requirements = new SizeRequirements(0, 0, 0,
056: align);
057: assertEquals("Minimum ", min, requirements.minimum);
058: assertEquals("Preferred ", pref, requirements.preferred);
059: assertEquals("Maximum ", max, requirements.maximum);
060: assertEquals("Alignmnent ", align, requirements.alignment, 1e-5);
061: }
062:
063: /*
064: * Class under test for void SizeRequirements()
065: */
066: public void testSizeRequirements() {
067: int min = 0;
068: int pref = 0;
069: int max = 0;
070: float align = 0.5f;
071: SizeRequirements requirements = new SizeRequirements();
072: assertEquals("Minimum ", min, requirements.minimum);
073: assertEquals("Preferred ", pref, requirements.preferred);
074: assertEquals("Maximum ", max, requirements.maximum);
075: assertEquals("Alignmnent ", align, requirements.alignment, 1e-5);
076: }
077:
078: /*
079: * Class under test for String toString()
080: */
081: public void testToString() {
082: SizeRequirements requirements1 = new SizeRequirements(10, 20,
083: 30, 0.1f);
084: SizeRequirements requirements2 = new SizeRequirements(110, 240,
085: 530, 0.9f);
086: SizeRequirements requirements3 = new SizeRequirements();
087: assertNotNull(requirements1.toString());
088: assertNotNull(requirements2.toString());
089: assertNotNull(requirements3.toString());
090: }
091:
092: public void testGetTiledSizeRequirements() {
093: SizeRequirements[] reqs = new SizeRequirements[] {
094: new SizeRequirements(10, 20, 30, 1f),
095: new SizeRequirements(20, 30, 40, 1f) };
096: SizeRequirements res = SizeRequirements
097: .getTiledSizeRequirements(reqs);
098: assertEquals("Minimum ", 30, res.minimum);
099: assertEquals("Preferred ", 50, res.preferred);
100: assertEquals("Maximum ", 70, res.maximum);
101: assertEquals("Alignmnent ", 0.5f, res.alignment, 1e-5);
102: reqs = new SizeRequirements[] {
103: new SizeRequirements(10, 20, 30, 2f),
104: new SizeRequirements(20, 30, 40, 3f),
105: new SizeRequirements(30, 40, 50, -100f) };
106: res = SizeRequirements.getTiledSizeRequirements(reqs);
107: assertEquals("Minimum ", 60, res.minimum);
108: assertEquals("Preferred ", 90, res.preferred);
109: assertEquals("Maximum ", 120, res.maximum);
110: assertEquals("Alignmnent ", 0.5f, res.alignment, 1e-5);
111: reqs = new SizeRequirements[0];
112: res = SizeRequirements.getTiledSizeRequirements(reqs);
113: assertEquals("Minimum ", 0, res.minimum);
114: assertEquals("Preferred ", 0, res.preferred);
115: assertEquals("Maximum ", 0, res.maximum);
116: assertEquals("Alignmnent ", 0.5f, res.alignment, 1e-5);
117: reqs = new SizeRequirements[] {
118: new SizeRequirements(Integer.MAX_VALUE,
119: Integer.MAX_VALUE, Integer.MAX_VALUE, 0f),
120: new SizeRequirements(Integer.MAX_VALUE,
121: Integer.MAX_VALUE, Integer.MAX_VALUE, 1f),
122: new SizeRequirements(30, 20, 10, 1f) };
123: res = SizeRequirements.getTiledSizeRequirements(reqs);
124: assertEquals("Minimum ", Integer.MAX_VALUE, res.minimum);
125: assertEquals("Preferred ", Integer.MAX_VALUE, res.preferred);
126: assertEquals("Maximum ", Integer.MAX_VALUE, res.maximum);
127: assertEquals("Alignmnent ", 0.5f, res.alignment, 1e-5);
128: }
129:
130: public void testGetAlignedSizeRequirements() {
131: SizeRequirements[] reqs = new SizeRequirements[] {
132: new SizeRequirements(10, 20, 30, 0.2f),
133: new SizeRequirements(20, 30, 40, .7f) };
134: SizeRequirements res = SizeRequirements
135: .getAlignedSizeRequirements(reqs);
136: if (isHarmony()) {
137: assertEquals("Minimum ", 22, res.minimum);
138: assertEquals("Preferred ", 37, res.preferred);
139: assertEquals("Maximum ", 52, res.maximum);
140: assertEquals("Alignmnent ", 0.6363636f, res.alignment, 1e-5);
141: }
142: reqs = new SizeRequirements[] {
143: new SizeRequirements(10, 20, 30, 0f),
144: new SizeRequirements(20, 30, 40, 0.2f),
145: new SizeRequirements(30, 20, 10, 1f) };
146: res = SizeRequirements.getAlignedSizeRequirements(reqs);
147: assertEquals("Minimum ", 46, res.minimum);
148: assertEquals("Preferred ", 44, res.preferred);
149: assertEquals("Maximum ", 42, res.maximum);
150: assertEquals("Alignmnent ", 0.65217394f, res.alignment, 1e-5);
151: reqs = new SizeRequirements[0];
152: res = SizeRequirements.getAlignedSizeRequirements(reqs);
153: assertEquals("Minimum ", 0, res.minimum);
154: assertEquals("Preferred ", 0, res.preferred);
155: assertEquals("Maximum ", 0, res.maximum);
156: assertEquals("Alignmnent ", 0.0f, res.alignment, 1e-5);
157: reqs = new SizeRequirements[] {
158: new SizeRequirements(Integer.MAX_VALUE,
159: Integer.MAX_VALUE, Integer.MAX_VALUE, 0f),
160: new SizeRequirements(Integer.MAX_VALUE,
161: Integer.MAX_VALUE, Integer.MAX_VALUE, 0.2f),
162: new SizeRequirements(30, 20, 10, 1f) };
163: res = SizeRequirements.getAlignedSizeRequirements(reqs);
164: assertEquals("Minimum ", Integer.MAX_VALUE, res.minimum);
165: assertEquals("Preferred ", Integer.MAX_VALUE, res.preferred);
166: assertEquals("Maximum ", Integer.MAX_VALUE, res.maximum);
167: assertEquals("Alignmnent ", 0.2f, res.alignment, 1e-5);
168: }
169:
170: /*
171: * Class under test for void calculateTiledPositions(int, SizeRequirements, SizeRequirements[], int[], int[], boolean)
172: */
173: public void testCalculateTiledPositionsintSizeRequirementsSizeRequirementsArrayintArrayintArrayboolean() {
174: SizeRequirements total = null;
175: SizeRequirements[] children = new SizeRequirements[] {
176: new SizeRequirements(10, 40, 10, 0.9f),
177: new SizeRequirements(20, 80, 100, 0.3f),
178: new SizeRequirements(80, 70, 20, 0.6f) };
179: int[] offsets = new int[3];
180: int[] spans = new int[3];
181: SizeRequirements.calculateTiledPositions(50, total, children,
182: offsets, spans, true);
183: assertEquals("Offsets coinside ", 0, offsets[0]);
184: assertEquals("Offsets coinside ", 10, offsets[1]);
185: assertEquals("Offsets coinside ", 30, offsets[2]);
186: assertEquals("Spans coinside ", 10, spans[0]);
187: assertEquals("Spans coinside ", 20, spans[1]);
188: assertEquals("Spans coinside ", 80, spans[2]);
189: SizeRequirements.calculateTiledPositions(100, total, children,
190: offsets, spans, true);
191: assertEquals("Offsets coinside ", 0, offsets[0]);
192: assertEquals("Offsets coinside ", 10, offsets[1]);
193: assertEquals("Offsets coinside ", 30, offsets[2]);
194: assertEquals("Spans coinside ", 10, spans[0]);
195: assertEquals("Spans coinside ", 20, spans[1]);
196: assertEquals("Spans coinside ", 80, spans[2]);
197: SizeRequirements.calculateTiledPositions(250, total, children,
198: offsets, spans, true);
199: assertEquals("Offsets coinside ", 0, offsets[0]);
200: assertEquals("Offsets coinside ", 10, offsets[1]);
201: assertEquals("Offsets coinside ", 110, offsets[2]);
202: assertEquals("Spans coinside ", 10, spans[0]);
203: assertEquals("Spans coinside ", 100, spans[1]);
204: assertEquals("Spans coinside ", 20, spans[2]);
205: SizeRequirements.calculateTiledPositions(250, total, children,
206: offsets, spans, false);
207: assertEquals("Offsets coinside ", 240, offsets[0]);
208: assertEquals("Offsets coinside ", 140, offsets[1]);
209: assertEquals("Offsets coinside ", 120, offsets[2]);
210: assertEquals("Spans coinside ", 10, spans[0]);
211: assertEquals("Spans coinside ", 100, spans[1]);
212: assertEquals("Spans coinside ", 20, spans[2]);
213: SizeRequirements.calculateTiledPositions(100, total, children,
214: offsets, spans, false);
215: assertEquals("Offsets coinside ", 90, offsets[0]);
216: assertEquals("Offsets coinside ", 70, offsets[1]);
217: assertEquals("Offsets coinside ", -10, offsets[2]);
218: assertEquals("Spans coinside ", 10, spans[0]);
219: assertEquals("Spans coinside ", 20, spans[1]);
220: assertEquals("Spans coinside ", 80, spans[2]);
221: SizeRequirements.calculateTiledPositions(80, total, children,
222: offsets, spans, false);
223: assertEquals("Offsets coinside ", 70, offsets[0]);
224: assertEquals("Offsets coinside ", 50, offsets[1]);
225: assertEquals("Offsets coinside ", -30, offsets[2]);
226: assertEquals("Spans coinside ", 10, spans[0]);
227: assertEquals("Spans coinside ", 20, spans[1]);
228: assertEquals("Spans coinside ", 80, spans[2]);
229: children = new SizeRequirements[] {
230: new SizeRequirements(10, 100, 1000, 0.9f),
231: new SizeRequirements(20, 200, 2000, 0.3f),
232: new SizeRequirements(30, 300, 3000, 0.6f) };
233: SizeRequirements.calculateTiledPositions(1000, total, children,
234: offsets, spans, true);
235: assertEquals("Offsets coinside ", 0, offsets[0]);
236: assertEquals("Offsets coinside ", 166, offsets[1]);
237: assertEquals("Offsets coinside ", 499, offsets[2]);
238: assertEquals("Spans coinside ", 166, spans[0]);
239: assertEquals("Spans coinside ", 333, spans[1]);
240: assertEquals("Spans coinside ", 500, spans[2]);
241: offsets = new int[1];
242: spans = new int[1];
243: children = new SizeRequirements[] { new SizeRequirements(30,
244: 300, Integer.MAX_VALUE, 0.6f) };
245: SizeRequirements.calculateTiledPositions(350, total, children,
246: offsets, spans, true);
247: assertEquals("Offsets coinside ", 0, offsets[0]);
248: assertEquals("Spans coinside ", 350, spans[0]);
249: offsets = new int[2];
250: spans = new int[2];
251: children = new SizeRequirements[] {
252: new SizeRequirements(30, 50, Integer.MAX_VALUE, 0.0f),
253: new SizeRequirements(30, 50, Integer.MAX_VALUE, 0.0f) };
254: SizeRequirements.calculateTiledPositions(300, total, children,
255: offsets, spans, true);
256: assertEquals("Offsets coinside ", 0, offsets[0]);
257: assertEquals("Offsets coinside ", 150, offsets[1]);
258: assertEquals("Spans coinside ", 150, spans[0]);
259: assertEquals("Spans coinside ", 150, spans[1]);
260: if (isHarmony()) {
261: offsets = new int[2];
262: spans = new int[2];
263: children = new SizeRequirements[] {
264: new SizeRequirements(100, Integer.MAX_VALUE,
265: Integer.MAX_VALUE, 0.0f),
266: new SizeRequirements(100, Integer.MAX_VALUE,
267: Integer.MAX_VALUE, 0.0f) };
268: SizeRequirements.calculateTiledPositions(300, total,
269: children, offsets, spans, true);
270: assertEquals("Offsets coinside ", 0, offsets[0]);
271: assertEquals("Offsets coinside ", 150, offsets[1]);
272: assertEquals("Spans coinside ", 150, spans[0]);
273: assertEquals("Spans coinside ", 150, spans[1]);
274: }
275: }
276:
277: /*
278: * This function is being tested by:
279: * testCalculateTiledPositionsintSizeRequirementsSizeRequirementsArrayintArrayintArrayboolean()
280: */
281: public void testCalculateTiledPositionsintSizeRequirementsSizeRequirementsArrayintArrayintArray() {
282: }
283:
284: /*
285: * Class under test for void calculateAlignedPositions(int, SizeRequirements, SizeRequirements[], int[], int[], boolean)
286: */
287: public void testCalculateAlignedPositionsintSizeRequirementsSizeRequirementsArrayintArrayintArrayboolean() {
288: SizeRequirements total = new SizeRequirements(0, 0, 0, 0.4f);
289: SizeRequirements[] children = new SizeRequirements[] {
290: new SizeRequirements(10, 40, 10, 0.1f),
291: new SizeRequirements(40, 80, 100, 0.2f),
292: new SizeRequirements(60, 70, 20, 1f) };
293: int[] offsets = new int[3];
294: int[] spans = new int[3];
295: SizeRequirements.calculateAlignedPositions(50, total, children,
296: offsets, spans, true);
297: assertEquals("Offsets coinside ", 19, offsets[0]);
298: assertEquals("Offsets coinside ", 0, offsets[1]);
299: assertEquals("Offsets coinside ", 0, offsets[2]);
300: assertEquals("Spans coinside ", 10, spans[0]);
301: assertEquals("Spans coinside ", 50, spans[1]);
302: assertEquals("Spans coinside ", 20, spans[2]);
303: SizeRequirements.calculateAlignedPositions(100, total,
304: children, offsets, spans, true);
305: assertEquals("Offsets coinside ", 39, offsets[0]);
306: assertEquals("Offsets coinside ", 20, offsets[1]);
307: assertEquals("Offsets coinside ", 20, offsets[2]);
308: assertEquals("Spans coinside ", 10, spans[0]);
309: assertEquals("Spans coinside ", 80, spans[1]);
310: assertEquals("Spans coinside ", 20, spans[2]);
311: SizeRequirements.calculateAlignedPositions(200, total,
312: children, offsets, spans, true);
313: assertEquals("Offsets coinside ", 79, offsets[0]);
314: assertEquals("Offsets coinside ", 60, offsets[1]);
315: assertEquals("Offsets coinside ", 60, offsets[2]);
316: assertEquals("Spans coinside ", 10, spans[0]);
317: assertEquals("Spans coinside ", 100, spans[1]);
318: assertEquals("Spans coinside ", 20, spans[2]);
319: total = new SizeRequirements(0, 0, 0, 0.1f);
320: SizeRequirements.calculateAlignedPositions(200, total,
321: children, offsets, spans, false);
322: assertEquals("Offsets coinside ", 171, offsets[0], 1);
323: assertEquals("Offsets coinside ", 100, offsets[1], 1);
324: assertEquals("Offsets coinside ", 180, offsets[2], 1);
325: assertEquals("Spans coinside ", 10, spans[0]);
326: assertEquals("Spans coinside ", 100, spans[1]);
327: assertEquals("Spans coinside ", 20, spans[2]);
328: SizeRequirements.calculateAlignedPositions(80, total, children,
329: offsets, spans, false);
330: assertEquals("Offsets coinside ", 63, offsets[0], 1);
331: assertEquals("Offsets coinside ", 0, offsets[1], 1);
332: assertEquals("Offsets coinside ", 72, offsets[2], 1);
333: assertEquals("Spans coinside ", 10, spans[0], 1);
334: assertEquals("Spans coinside ", 80, spans[1], 1);
335: assertEquals("Spans coinside ", 8, spans[2], 1);
336: }
337:
338: /*
339: * This function is being tested by:
340: * testCalculateAlignedPositionsintSizeRequirementsSizeRequirementsArrayintArrayintArrayboolean()
341: */
342: public void testCalculateAlignedPositionsintSizeRequirementsSizeRequirementsArrayintArrayintArray() {
343: }
344:
345: public void testAdjustSizes() {
346: SizeRequirements[] reqs = new SizeRequirements[] {
347: new SizeRequirements(10, 20, 30, 0.2f),
348: new SizeRequirements(20, 30, 40, .7f) };
349: int[] res = SizeRequirements.adjustSizes(1, reqs);
350: assertTrue(res.length == 0);
351: }
352:
353: public void testWriteObject() throws Exception {
354: // SizeRequirements requirements1 = new SizeRequirements(10, 20, 30, 0.1f);
355: // SizeRequirements requirements2 = new SizeRequirements(40, 50, 60, 0.2f);
356: // ByteArrayOutputStream fo = new ByteArrayOutputStream();
357: // ObjectOutputStream so = new ObjectOutputStream(fo);
358: // so.writeObject(requirements1);
359: // so.flush();
360: // so.writeObject(requirements2);
361: // so.flush();
362: }
363:
364: public void testReadObject() throws IOException,
365: ClassNotFoundException {
366: // SizeRequirements requirements1 = new SizeRequirements(10, 20, 30, 0.1f);
367: // SizeRequirements requirements2 = new SizeRequirements(40, 50, 60, 0.2f);
368: // ByteArrayOutputStream fo = new ByteArrayOutputStream();
369: // ObjectOutputStream so = new ObjectOutputStream(fo);
370: // so.writeObject(requirements1);
371: // so.flush();
372: // InputStream fi = new ByteArrayInputStream(fo.toByteArray());
373: // ObjectInputStream si = new ObjectInputStream(fi);
374: // SizeRequirements resurrectedRequirements = (SizeRequirements)si
375: // .readObject();
376: //
377: // assertEquals("Deserialized minimum",
378: // requirements1.minimum, resurrectedRequirements.minimum);
379: //
380: // assertEquals("Deserialized preferred",
381: // requirements1.preferred, resurrectedRequirements.preferred);
382: //
383: // assertEquals("Deserialized maximum",
384: // requirements1.maximum, resurrectedRequirements.maximum);
385: //
386: // assertEquals("Deserialized alignment",
387: // requirements1.alignment, resurrectedRequirements.alignment, 1e-5);
388: //
389: // fo = new ByteArrayOutputStream();
390: // so = new ObjectOutputStream(fo);
391: // so.writeObject(requirements2);
392: // so.flush();
393: //
394: // fi = new ByteArrayInputStream(fo.toByteArray());
395: // si = new ObjectInputStream(fi);
396: // resurrectedRequirements = (SizeRequirements)si.readObject();
397: // assertEquals("Deserialized minimum",
398: // requirements2.minimum, resurrectedRequirements.minimum);
399: //
400: // assertEquals("Deserialized preferred",
401: // requirements2.preferred, resurrectedRequirements.preferred);
402: //
403: // assertEquals("Deserialized maximum",
404: // requirements2.maximum, resurrectedRequirements.maximum);
405: //
406: // assertEquals("Deserialized alignment",
407: // requirements2.alignment, resurrectedRequirements.alignment, 1e-5);
408: }
409: }
|