Removed
Link Here
|
1 |
/* |
2 |
* Sun Public License Notice |
3 |
* |
4 |
* The contents of this file are subject to the Sun Public License |
5 |
* Version 1.0 (the "License"). You may not use this file except in |
6 |
* compliance with the License. A copy of the License is available at |
7 |
* http://www.sun.com/ |
8 |
* |
9 |
* The Original Code is Forte for Java, Community Edition. The Initial |
10 |
* Developer of the Original Code is Sun Microsystems, Inc. Portions |
11 |
* Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved. |
12 |
*/ |
13 |
|
14 |
package org.netbeans.spi.looks; |
15 |
|
16 |
import java.lang.reflect.*; |
17 |
import java.util.*; |
18 |
import java.awt.*; |
19 |
import java.awt.datatransfer.Transferable; |
20 |
import java.io.IOException; |
21 |
import javax.swing.Action; |
22 |
|
23 |
import org.openide.nodes.*; |
24 |
import org.openide.util.actions.SystemAction; |
25 |
import org.openide.util.HelpCtx; |
26 |
import org.openide.util.datatransfer.NewType; |
27 |
import org.openide.util.datatransfer.PasteType; |
28 |
import org.openide.util.Lookup; |
29 |
|
30 |
import org.netbeans.api.looks.*; |
31 |
import org.netbeans.spi.looks.*; |
32 |
|
33 |
import org.netbeans.junit.*; |
34 |
/** |
35 |
* |
36 |
* @author Svata Dedic |
37 |
*/ |
38 |
public class ProxyLookTest extends NbTestCase { |
39 |
static final Object DATA = new Object(); |
40 |
|
41 |
/** |
42 |
* The Look being tested |
43 |
*/ |
44 |
Prx look; |
45 |
|
46 |
MySubst mySubst; |
47 |
|
48 |
Look.NodeSubstitute proxySubst; |
49 |
|
50 |
ProxyDel[] maskDelegates; |
51 |
|
52 |
Object[] customData; |
53 |
|
54 |
Object[] actualDelegateData; |
55 |
|
56 |
Look.NodeSubstitute[] delegateSubsts; |
57 |
|
58 |
int delegateCount; |
59 |
/** |
60 |
* Delegates, which will be returned from delegateTo |
61 |
*/ |
62 |
Look[] forceDelegates; |
63 |
|
64 |
Method substAccessor; |
65 |
|
66 |
int testedMethodIndex = -1; |
67 |
|
68 |
String fakeName; |
69 |
|
70 |
static final int FAKE = 39; |
71 |
|
72 |
/** Creates a new instance of ProxyLookTest */ |
73 |
public ProxyLookTest(String n) { |
74 |
super(n); |
75 |
} |
76 |
|
77 |
public String name() { |
78 |
if (fakeName != null) |
79 |
return fakeName; |
80 |
else |
81 |
return super.name(); |
82 |
} |
83 |
|
84 |
public void setUp() { |
85 |
look = new Prx(); |
86 |
setupDelegates(); |
87 |
} |
88 |
|
89 |
public void tearDown() { |
90 |
} |
91 |
|
92 |
public static void main(java.lang.String[] args) { |
93 |
junit.textui.TestRunner.run(suite()); |
94 |
} |
95 |
|
96 |
public static NbTest suite() { |
97 |
NbTestSuite suite = new NbTestSuite(ProxyLookTest.class); |
98 |
return suite; |
99 |
} |
100 |
|
101 |
private void setupDelegates() { |
102 |
maskDelegates = new ProxyDel[40]; |
103 |
customData = new Object[40]; |
104 |
delegateSubsts = new Look.NodeSubstitute[40]; |
105 |
int count = 0; |
106 |
Look del = new SampleLook1Hid(); |
107 |
for (int i = 1; i <= ProxyLook.HAS_CUSTOMIZER; i <<= 1, count++) { |
108 |
maskDelegates[count] = new ProxyDel(del, count, 1 << count, customData[count] = new Object()); |
109 |
} |
110 |
delegateCount = count; |
111 |
mySubst = new MySubst(look); |
112 |
} |
113 |
|
114 |
/** |
115 |
* Tests whether the createData was called |
116 |
*/ |
117 |
public void testProxySubstitute() { |
118 |
assertSame("Incorrect substitute on the proxy look", mySubst, proxySubst); |
119 |
} |
120 |
|
121 |
/** |
122 |
* Tests whether the createData was called |
123 |
*/ |
124 |
public void testCreateData() { |
125 |
if (!look.createCalled) { |
126 |
fail("Create was not called"); |
127 |
} |
128 |
} |
129 |
|
130 |
/** |
131 |
* Checks whether there are correct data attached to the Look. |
132 |
*/ |
133 |
public void testOwnAttachedData() { |
134 |
if (look.getAttachedData(mySubst) != DATA) { |
135 |
fail("ProxyLook got incorrect attached data"); |
136 |
} |
137 |
} |
138 |
|
139 |
public void testDelegateSubstitutes() { |
140 |
forceDelegates = maskDelegates; |
141 |
look.delAll = true; |
142 |
Object[] children = look.getChildObjects(mySubst); |
143 |
for (int i = 0; i < delegateCount; i++) { |
144 |
assertNotNull("Delegate " + maskDelegates[i] + " reported null substitute", |
145 |
delegateSubsts[i]); |
146 |
// check the substitute the ProxyLook knows is |
147 |
// the same as reported by the delegate |
148 |
assertSame("ProxyLook recorded incorrect substitute for " + maskDelegates[i], |
149 |
getSubstitute(mySubst, maskDelegates[i], i), delegateSubsts[i]); |
150 |
} |
151 |
} |
152 |
|
153 |
public void testDelegateAttachedData() { |
154 |
forceDelegates = maskDelegates; |
155 |
actualDelegateData = new Object[delegateCount]; |
156 |
look.delAll = true; |
157 |
Object[] x = look.getChildObjects(mySubst); |
158 |
for (int i = 0; i < delegateCount; i++) { |
159 |
assertSame("Attached data mismatch for delegate " + maskDelegates[i], |
160 |
actualDelegateData[i], customData[i]); |
161 |
} |
162 |
} |
163 |
|
164 |
public void testCanCopy() { |
165 |
ProxyDel del = createDelegate(ProxyLook.CAN_COPY); |
166 |
boolean b = look.canCopy(mySubst); |
167 |
assertTrue("Incorrect value", b); |
168 |
ensureQueried(ProxyLook.CAN_COPY, getIndex(ProxyLook.CAN_COPY)); |
169 |
assertTrue("Fake delegate queried", del.queried == 0); |
170 |
} |
171 |
|
172 |
public void testCanCut() { |
173 |
ProxyDel del = createDelegate(ProxyLook.CAN_CUT); |
174 |
boolean b = look.canCut(mySubst); |
175 |
assertTrue("Incorrect value", b); |
176 |
ensureQueried(ProxyLook.CAN_CUT, getIndex(ProxyLook.CAN_CUT)); |
177 |
assertTrue("Fake delegate queried", del.queried == 0); |
178 |
} |
179 |
|
180 |
public void testCanDestroy() { |
181 |
ProxyDel del = createDelegate(ProxyLook.CAN_DESTROY); |
182 |
boolean b = look.canDestroy(mySubst); |
183 |
assertTrue("Incorrect value", b); |
184 |
ensureQueried(ProxyLook.CAN_DESTROY, getIndex(ProxyLook.CAN_DESTROY)); |
185 |
assertTrue("Fake delegate queried", del.queried == 0); |
186 |
} |
187 |
|
188 |
public void testCanRename() { |
189 |
ProxyDel del = createDelegate(ProxyLook.CAN_RENAME); |
190 |
boolean b = look.canRename(mySubst); |
191 |
assertTrue("Incorrect value", b); |
192 |
ensureQueried(ProxyLook.CAN_RENAME, getIndex(ProxyLook.CAN_RENAME)); |
193 |
assertTrue("Fake delegate queried", del.queried == 0); |
194 |
} |
195 |
|
196 |
public void testClipboardCut() { |
197 |
ProxyDel del = createDelegate(ProxyLook.CLIPBOARD_CUT); |
198 |
try { |
199 |
Transferable t = look.clipboardCut(mySubst); |
200 |
int index = getIndex(ProxyLook.CLIPBOARD_CUT); |
201 |
assertSame("Incorrect transferable: ", t, |
202 |
maskDelegates[index].clipboardCut(delegateSubsts[index])); |
203 |
ensureQueried(ProxyLook.CLIPBOARD_CUT, index); |
204 |
assertTrue("Fake delegate queried", del.queried == 0); |
205 |
} catch (IOException ex) { |
206 |
fail(ex.getMessage()); |
207 |
} |
208 |
} |
209 |
|
210 |
public void testDrag() { |
211 |
ProxyDel del = createDelegate(ProxyLook.DRAG); |
212 |
try { |
213 |
Transferable t = look.drag(mySubst); |
214 |
int index = getIndex(ProxyLook.DRAG); |
215 |
assertSame("Incorrect transferable: ", t, |
216 |
maskDelegates[index].drag(delegateSubsts[index])); |
217 |
ensureQueried(ProxyLook.DRAG, index); |
218 |
assertTrue("Fake delegate queried", del.queried == 0); |
219 |
} catch (IOException ex) { |
220 |
fail(ex.getMessage()); |
221 |
} |
222 |
} |
223 |
|
224 |
public void testClipboardCopy() { |
225 |
ProxyDel del = createDelegate(ProxyLook.CLIPBOARD_COPY); |
226 |
try { |
227 |
Transferable t = look.clipboardCopy(mySubst); |
228 |
int index = getIndex(ProxyLook.CLIPBOARD_COPY); |
229 |
assertSame("Incorrect transferable: ", t, |
230 |
maskDelegates[index].clipboardCopy(delegateSubsts[index])); |
231 |
ensureQueried(ProxyLook.CLIPBOARD_COPY, index); |
232 |
assertTrue("Fake delegate queried", del.queried == 0); |
233 |
} catch (IOException ex) { |
234 |
fail(ex.getMessage()); |
235 |
} |
236 |
} |
237 |
|
238 |
public void testGetCustomizer() { |
239 |
ProxyDel del = createDelegate(ProxyLook.GET_CUSTOMIZER); |
240 |
Component c = look.getCustomizer(mySubst); |
241 |
int index = getIndex(ProxyLook.GET_CUSTOMIZER); |
242 |
assertSame("Incorrect customizer", c, |
243 |
maskDelegates[index].getCustomizer(delegateSubsts[index])); |
244 |
ensureQueried(ProxyLook.GET_CUSTOMIZER, index); |
245 |
assertTrue("Fake delegate queried", del.queried == 0); |
246 |
} |
247 |
|
248 |
public void testGetDefaultAction() { |
249 |
ProxyDel del = createDelegate(ProxyLook.GET_DEFAULT_ACTION); |
250 |
Object n = look.getDefaultAction(mySubst); |
251 |
int index = getIndex(ProxyLook.GET_DEFAULT_ACTION); |
252 |
assertSame(maskDelegates[index].getDefaultAction(delegateSubsts[index]), n); |
253 |
assertTrue("Fake delegate queried", del.queried == 0); |
254 |
} |
255 |
|
256 |
public void testGetName() { |
257 |
ProxyDel del = createDelegate(ProxyLook.GET_NAME); |
258 |
String n = look.getName(mySubst); |
259 |
int index = getIndex(ProxyLook.GET_NAME); |
260 |
assertEquals(maskDelegates[index].getName(delegateSubsts[index]), n); |
261 |
assertTrue("Fake delegate queried", del.queried == 0); |
262 |
} |
263 |
|
264 |
public void testGetDisplayName() { |
265 |
ProxyDel del = createDelegate(ProxyLook.GET_DISPLAY_NAME); |
266 |
String n = look.getDisplayName(mySubst); |
267 |
int index = getIndex(ProxyLook.GET_DISPLAY_NAME); |
268 |
assertEquals(maskDelegates[index].getDisplayName(delegateSubsts[index]), n); |
269 |
assertTrue("Fake delegate queried", del.queried == 0); |
270 |
} |
271 |
|
272 |
public void testGetShortDescription() { |
273 |
ProxyDel del = createDelegate(ProxyLook.GET_SHORT_DESCRIPTION); |
274 |
String n = look.getShortDescription(mySubst); |
275 |
int index = getIndex(ProxyLook.GET_SHORT_DESCRIPTION); |
276 |
assertEquals(maskDelegates[index].getShortDescription(delegateSubsts[index]), n); |
277 |
assertTrue("Fake delegate queried", del.queried == 0); |
278 |
} |
279 |
|
280 |
public void testGetDropType() { |
281 |
ProxyDel del = createDelegate(ProxyLook.GET_DROP_TYPE); |
282 |
PasteType t = look.getDropType(mySubst, null, 0, 0); |
283 |
int index = getIndex(ProxyLook.GET_DROP_TYPE); |
284 |
assertSame(maskDelegates[index].getDropType(delegateSubsts[index], null, 0, 0), |
285 |
t); |
286 |
assertTrue("Fake delegate queried", del.queried == 0); |
287 |
} |
288 |
|
289 |
public void testGetIcon() { |
290 |
ProxyDel del = createDelegate(ProxyLook.GET_ICON); |
291 |
int index = getIndex(ProxyLook.GET_ICON); |
292 |
assertSame(maskDelegates[index].getIcon(delegateSubsts[index], 0), look.getIcon(mySubst, 0)); |
293 |
assertTrue("Fake delegate queried", del.queried == 0); |
294 |
} |
295 |
|
296 |
public void testGetOpenedIcon() { |
297 |
ProxyDel del = createDelegate(ProxyLook.GET_OPENED_ICON); |
298 |
int index = getIndex(ProxyLook.GET_OPENED_ICON); |
299 |
assertSame(maskDelegates[index].getOpenedIcon(delegateSubsts[index], 0), look.getOpenedIcon(mySubst, 0)); |
300 |
assertTrue("Fake delegate queried", del.queried == 0); |
301 |
} |
302 |
|
303 |
public void testDestroy() { |
304 |
try { |
305 |
look.destroy(mySubst); |
306 |
for (int i = 0; i < delegateCount; i++) { |
307 |
assertTrue("Destroy not called on #" + i, |
308 |
(maskDelegates[i].queried & ProxyLook.DESTROY) > 0); |
309 |
} |
310 |
} catch (IOException ex) { |
311 |
fail(ex.getMessage()); |
312 |
} |
313 |
} |
314 |
|
315 |
public void testGetActions() { |
316 |
int index = getIndex(ProxyLook.GET_ACTIONS); |
317 |
ProxyDel del; |
318 |
ProxyDel first = maskDelegates[index]; |
319 |
Object[] result; |
320 |
|
321 |
maskDelegates[index] = null; |
322 |
result = look.getActions(mySubst); |
323 |
assertNull(result); |
324 |
maskDelegates[index] = first; |
325 |
|
326 |
del = createDelegate(ProxyLook.GET_ACTIONS); |
327 |
result = look.getActions(mySubst); |
328 |
Object[] expected = first.getActions(delegateSubsts[index]); |
329 |
assertEquals(Arrays.asList(expected), Arrays.asList(result)); |
330 |
} |
331 |
|
332 |
public void testGetAllActions() { |
333 |
int index = getIndex(ProxyLook.GET_ACTIONS); |
334 |
look.delAll = true; |
335 |
|
336 |
// check that no response will yield an empty array: |
337 |
ProxyDel del; |
338 |
Object[] result; |
339 |
|
340 |
del = maskDelegates[index]; |
341 |
maskDelegates[index] = null; |
342 |
result = look.getActions(mySubst); |
343 |
assertNull(result); |
344 |
maskDelegates[index] = del; |
345 |
|
346 |
result = look.getActions(mySubst); |
347 |
Object[] expected1 = del.getActions(delegateSubsts[index]); |
348 |
// exactly one Look providing an action |
349 |
assertEquals(expected1.length, result.length); |
350 |
assertEquals(Arrays.asList(expected1), Arrays.asList(result)); |
351 |
ensureQueried(ProxyLook.GET_ACTIONS, delegateCount); |
352 |
|
353 |
// test merge from two Looks |
354 |
del = createDelegate(ProxyLook.GET_ACTIONS); |
355 |
|
356 |
ProxyDel first = maskDelegates[index]; |
357 |
result = look.getActions(mySubst); |
358 |
expected1 = first.getActions(delegateSubsts[index]); |
359 |
Object[] expected2 = del.getActions(delegateSubsts[delegateCount]); |
360 |
assertEquals(expected1.length |
361 |
+ expected2.length, |
362 |
result.length); |
363 |
Collection c = new ArrayList(Arrays.asList(expected1)); |
364 |
c.addAll(Arrays.asList(expected2)); |
365 |
assertEquals(c, Arrays.asList(result)); |
366 |
} |
367 |
|
368 |
public void testGetChildObjects() { |
369 |
int index = getIndex(ProxyLook.GET_CHILD_OBJECTS); |
370 |
ProxyDel del; |
371 |
ProxyDel first = maskDelegates[index]; |
372 |
Object[] result; |
373 |
|
374 |
maskDelegates[index] = null; |
375 |
result = look.getChildObjects(mySubst); |
376 |
assertNull(result); |
377 |
maskDelegates[index] = first; |
378 |
|
379 |
del = createDelegate(ProxyLook.GET_CHILD_OBJECTS); |
380 |
result = look.getChildObjects(mySubst); |
381 |
Object[] expected = first.getChildObjects(delegateSubsts[index]); |
382 |
assertEquals(Arrays.asList(expected), Arrays.asList(result)); |
383 |
} |
384 |
|
385 |
public void testGetAllChildObjects() { |
386 |
int index = getIndex(ProxyLook.GET_CHILD_OBJECTS); |
387 |
look.delAll = true; |
388 |
|
389 |
// check that no response will yield an empty array: |
390 |
ProxyDel del; |
391 |
Object[] result; |
392 |
|
393 |
del = maskDelegates[index]; |
394 |
maskDelegates[index] = null; |
395 |
result = look.getChildObjects(mySubst); |
396 |
assertNull(result); |
397 |
maskDelegates[index] = del; |
398 |
|
399 |
result = look.getChildObjects(mySubst); |
400 |
Object[] expected1 = del.getChildObjects(delegateSubsts[index]); |
401 |
// exactly one Look providing an action |
402 |
assertEquals(expected1.length, result.length); |
403 |
assertEquals(Arrays.asList(expected1), Arrays.asList(result)); |
404 |
ensureQueried(ProxyLook.GET_CHILD_OBJECTS, delegateCount); |
405 |
|
406 |
// test merge from two Looks |
407 |
del = createDelegate(ProxyLook.GET_CHILD_OBJECTS); |
408 |
|
409 |
ProxyDel first = maskDelegates[index]; |
410 |
result = look.getChildObjects(mySubst); |
411 |
expected1 = first.getChildObjects(delegateSubsts[index]); |
412 |
Object[] expected2 = del.getChildObjects(delegateSubsts[delegateCount]); |
413 |
assertEquals(expected1.length |
414 |
+ expected2.length, |
415 |
result.length); |
416 |
Collection c = new ArrayList(Arrays.asList(expected1)); |
417 |
c.addAll(Arrays.asList(expected2)); |
418 |
assertEquals(c, Arrays.asList(result)); |
419 |
} |
420 |
|
421 |
public void testGetContextActions() { |
422 |
int index = getIndex(ProxyLook.GET_CONTEXT_ACTIONS); |
423 |
ProxyDel del; |
424 |
ProxyDel first = maskDelegates[index]; |
425 |
Object[] result; |
426 |
|
427 |
maskDelegates[index] = null; |
428 |
result = look.getContextActions(mySubst); |
429 |
assertNull(result); |
430 |
maskDelegates[index] = first; |
431 |
|
432 |
del = createDelegate(ProxyLook.GET_CONTEXT_ACTIONS); |
433 |
result = look.getContextActions(mySubst); |
434 |
Object[] expected = first.getContextActions(delegateSubsts[index]); |
435 |
assertEquals(Arrays.asList(expected), Arrays.asList(result)); |
436 |
} |
437 |
|
438 |
public void testGetAllContextActions() { |
439 |
int index = getIndex(ProxyLook.GET_CONTEXT_ACTIONS); |
440 |
look.delAll = true; |
441 |
|
442 |
// check that no response will yield an empty array: |
443 |
ProxyDel del; |
444 |
Object[] result; |
445 |
|
446 |
del = maskDelegates[index]; |
447 |
maskDelegates[index] = null; |
448 |
result = look.getContextActions(mySubst); |
449 |
assertNull(result); |
450 |
maskDelegates[index] = del; |
451 |
|
452 |
result = look.getContextActions(mySubst); |
453 |
Object[] expected1 = del.getContextActions(delegateSubsts[index]); |
454 |
// exactly one Look providing an action |
455 |
assertEquals(expected1.length, result.length); |
456 |
assertEquals(Arrays.asList(expected1), Arrays.asList(result)); |
457 |
ensureQueried(ProxyLook.GET_CONTEXT_ACTIONS, delegateCount); |
458 |
|
459 |
// test merge from two Looks |
460 |
del = createDelegate(ProxyLook.GET_CONTEXT_ACTIONS); |
461 |
|
462 |
ProxyDel first = maskDelegates[index]; |
463 |
result = look.getContextActions(mySubst); |
464 |
expected1 = first.getContextActions(delegateSubsts[index]); |
465 |
Object[] expected2 = del.getContextActions(delegateSubsts[delegateCount]); |
466 |
assertEquals(expected1.length |
467 |
+ expected2.length, |
468 |
result.length); |
469 |
Collection c = new ArrayList(Arrays.asList(expected1)); |
470 |
c.addAll(Arrays.asList(expected2)); |
471 |
assertEquals(c, Arrays.asList(result)); |
472 |
} |
473 |
|
474 |
public void testGetPasteTypes() { |
475 |
int index = getIndex(ProxyLook.GET_PASTE_TYPES); |
476 |
ProxyDel del; |
477 |
ProxyDel first = maskDelegates[index]; |
478 |
Object[] result; |
479 |
|
480 |
maskDelegates[index] = null; |
481 |
result = look.getPasteTypes(mySubst, null); |
482 |
assertNull(result); |
483 |
maskDelegates[index] = first; |
484 |
|
485 |
del = createDelegate(ProxyLook.GET_PASTE_TYPES); |
486 |
result = look.getPasteTypes(mySubst, null); |
487 |
Object[] expected = first.getPasteTypes(delegateSubsts[index], null); |
488 |
assertEquals(Arrays.asList(expected), Arrays.asList(result)); |
489 |
} |
490 |
|
491 |
public void testGetAllPasteTypes() { |
492 |
int index = getIndex(ProxyLook.GET_PASTE_TYPES); |
493 |
look.delAll = true; |
494 |
|
495 |
// check that no response will yield an empty array: |
496 |
ProxyDel del; |
497 |
Object[] result; |
498 |
|
499 |
del = maskDelegates[index]; |
500 |
maskDelegates[index] = null; |
501 |
result = look.getPasteTypes(mySubst, null); |
502 |
assertNull(result); |
503 |
maskDelegates[index] = del; |
504 |
|
505 |
result = look.getPasteTypes(mySubst, null); |
506 |
Object[] expected1 = del.getPasteTypes(delegateSubsts[index], null); |
507 |
// exactly one Look providing an action |
508 |
assertEquals(expected1.length, result.length); |
509 |
assertEquals(Arrays.asList(expected1), Arrays.asList(result)); |
510 |
ensureQueried(ProxyLook.GET_PASTE_TYPES, delegateCount); |
511 |
|
512 |
// test merge from two Looks |
513 |
del = createDelegate(ProxyLook.GET_PASTE_TYPES); |
514 |
|
515 |
ProxyDel first = maskDelegates[index]; |
516 |
result = look.getPasteTypes(mySubst, null); |
517 |
expected1 = first.getPasteTypes(delegateSubsts[index], null); |
518 |
Object[] expected2 = del.getPasteTypes(delegateSubsts[delegateCount], null); |
519 |
assertEquals(expected1.length |
520 |
+ expected2.length, |
521 |
result.length); |
522 |
Collection c = new ArrayList(Arrays.asList(expected1)); |
523 |
c.addAll(Arrays.asList(expected2)); |
524 |
assertEquals(c, Arrays.asList(result)); |
525 |
} |
526 |
|
527 |
|
528 |
public void testGetNewTypes() { |
529 |
int index = getIndex(ProxyLook.GET_NEW_TYPES); |
530 |
ProxyDel del; |
531 |
ProxyDel first = maskDelegates[index]; |
532 |
Object[] result; |
533 |
|
534 |
maskDelegates[index] = null; |
535 |
result = look.getNewTypes(mySubst); |
536 |
assertNull(result); |
537 |
maskDelegates[index] = first; |
538 |
|
539 |
del = createDelegate(ProxyLook.GET_NEW_TYPES); |
540 |
result = look.getNewTypes(mySubst); |
541 |
Object[] expected = first.getNewTypes(delegateSubsts[index]); |
542 |
assertEquals(Arrays.asList(expected), Arrays.asList(result)); |
543 |
} |
544 |
|
545 |
public void testGetAllNewTypes() { |
546 |
int index = getIndex(ProxyLook.GET_NEW_TYPES); |
547 |
look.delAll = true; |
548 |
|
549 |
// check that no response will yield an empty array: |
550 |
ProxyDel del; |
551 |
Object[] result; |
552 |
|
553 |
del = maskDelegates[index]; |
554 |
maskDelegates[index] = null; |
555 |
result = look.getNewTypes(mySubst); |
556 |
assertNull(result); |
557 |
maskDelegates[index] = del; |
558 |
|
559 |
result = look.getNewTypes(mySubst); |
560 |
Object[] expected1 = del.getNewTypes(delegateSubsts[index]); |
561 |
// exactly one Look providing an action |
562 |
assertEquals(expected1.length, result.length); |
563 |
assertEquals(Arrays.asList(expected1), Arrays.asList(result)); |
564 |
ensureQueried(ProxyLook.GET_NEW_TYPES, delegateCount); |
565 |
|
566 |
// test merge from two Looks |
567 |
del = createDelegate(ProxyLook.GET_NEW_TYPES); |
568 |
|
569 |
ProxyDel first = maskDelegates[index]; |
570 |
result = look.getNewTypes(mySubst); |
571 |
expected1 = first.getNewTypes(delegateSubsts[index]); |
572 |
Object[] expected2 = del.getNewTypes(delegateSubsts[delegateCount]); |
573 |
assertEquals(expected1.length |
574 |
+ expected2.length, |
575 |
result.length); |
576 |
Collection c = new ArrayList(Arrays.asList(expected1)); |
577 |
c.addAll(Arrays.asList(expected2)); |
578 |
assertEquals(c, Arrays.asList(result)); |
579 |
} |
580 |
|
581 |
public void testGetPropertySets() { |
582 |
int index = getIndex(ProxyLook.GET_PROPERTY_SETS); |
583 |
ProxyDel del; |
584 |
ProxyDel first = maskDelegates[index]; |
585 |
Object[] result; |
586 |
|
587 |
maskDelegates[index] = null; |
588 |
result = look.getPropertySets(mySubst); |
589 |
assertNull(result); |
590 |
maskDelegates[index] = first; |
591 |
|
592 |
del = createDelegate(ProxyLook.GET_PROPERTY_SETS); |
593 |
result = look.getPropertySets(mySubst); |
594 |
Object[] expected = first.getPropertySets(delegateSubsts[index]); |
595 |
assertEquals(Arrays.asList(expected), Arrays.asList(result)); |
596 |
} |
597 |
|
598 |
public void testGetAllPropertySets() { |
599 |
int index = getIndex(ProxyLook.GET_PROPERTY_SETS); |
600 |
look.delAll = true; |
601 |
|
602 |
// check that no response will yield an empty array: |
603 |
ProxyDel del; |
604 |
Object[] result; |
605 |
|
606 |
del = maskDelegates[index]; |
607 |
maskDelegates[index] = null; |
608 |
result = look.getPropertySets(mySubst); |
609 |
assertNull(result); |
610 |
maskDelegates[index] = del; |
611 |
|
612 |
result = look.getPropertySets(mySubst); |
613 |
Object[] expected1 = del.getPropertySets(delegateSubsts[index]); |
614 |
// exactly one Look providing an action |
615 |
assertEquals(expected1.length, result.length); |
616 |
assertEquals(Arrays.asList(expected1), Arrays.asList(result)); |
617 |
ensureQueried(ProxyLook.GET_PROPERTY_SETS, delegateCount); |
618 |
|
619 |
// test merge from two Looks |
620 |
del = createDelegate(ProxyLook.GET_PROPERTY_SETS); |
621 |
|
622 |
ProxyDel first = maskDelegates[index]; |
623 |
result = look.getPropertySets(mySubst); |
624 |
expected1 = first.getPropertySets(delegateSubsts[index]); |
625 |
Object[] expected2 = del.getPropertySets(delegateSubsts[delegateCount]); |
626 |
assertEquals(expected1.length |
627 |
+ expected2.length, |
628 |
result.length); |
629 |
Collection c = new ArrayList(Arrays.asList(expected1)); |
630 |
c.addAll(Arrays.asList(expected2)); |
631 |
assertEquals(c, Arrays.asList(result)); |
632 |
} |
633 |
|
634 |
public void testGetDuplicatePropertySets() { |
635 |
int index = getIndex(ProxyLook.GET_PROPERTY_SETS); |
636 |
look.delAll = true; |
637 |
|
638 |
// check that no response will yield an empty array: |
639 |
ProxyDel del; |
640 |
Object[] result; |
641 |
Object[] expected1; |
642 |
|
643 |
maskDelegates[0] = maskDelegates[index]; |
644 |
|
645 |
// test merge from two Looks |
646 |
del = createDelegate(ProxyLook.GET_PROPERTY_SETS); |
647 |
|
648 |
ProxyDel first = maskDelegates[index]; |
649 |
result = look.getPropertySets(mySubst); |
650 |
expected1 = first.getPropertySets(delegateSubsts[index]); |
651 |
Object[] expected2 = del.getPropertySets(delegateSubsts[delegateCount]); |
652 |
assertEquals(expected1.length |
653 |
+ expected2.length, |
654 |
result.length); |
655 |
Collection c = new ArrayList(Arrays.asList(expected1)); |
656 |
c.addAll(Arrays.asList(expected2)); |
657 |
assertEquals(c, Arrays.asList(result)); |
658 |
} |
659 |
|
660 |
private void ensureQueried(long mask, int lastIndex) { |
661 |
int end = lastIndex == -1 ? maskDelegates.length : lastIndex; |
662 |
for (int index = 0; index <= end; index++) { |
663 |
if (maskDelegates[index] == null) |
664 |
continue; |
665 |
assertTrue("Delegate #" + index + " not queried", |
666 |
(maskDelegates[index].queried & mask) > 0); |
667 |
} |
668 |
} |
669 |
|
670 |
private int getIndex(long mask) { |
671 |
int i = 0; |
672 |
while (mask > 1) { |
673 |
mask >>= 1; |
674 |
i++; |
675 |
} |
676 |
return i; |
677 |
} |
678 |
|
679 |
private ProxyDel createDelegate(long mask) { |
680 |
Look del = new SampleLook2Hid(); |
681 |
ProxyDel p = new ProxyDel(del, delegateCount, mask, null); |
682 |
maskDelegates[delegateCount] = p; |
683 |
return p; |
684 |
} |
685 |
|
686 |
Look.NodeSubstitute getSubstitute(Look.NodeSubstitute s, Look l, int index) { |
687 |
try { |
688 |
if (substAccessor == null) { |
689 |
substAccessor = ProxyLook.class.getDeclaredMethod( |
690 |
"s", new Class[] { |
691 |
Look.NodeSubstitute.class, Look.class, Integer.TYPE |
692 |
}); |
693 |
substAccessor.setAccessible(true); |
694 |
} |
695 |
return (Look.NodeSubstitute)substAccessor.invoke(look, new Object[] { |
696 |
s, l, new Integer(index) |
697 |
}); |
698 |
} catch (Exception ex) { |
699 |
fail("Unexpected exception: " + ex.getMessage()); |
700 |
return null; |
701 |
} |
702 |
} |
703 |
|
704 |
class ProxyDel extends Look { |
705 |
long mask; |
706 |
Look del; |
707 |
int index; |
708 |
NodeSubstitute substitute; |
709 |
Object data; |
710 |
long queried; |
711 |
|
712 |
ProxyDel(Look delegate, int index, long mask, Object data) { |
713 |
this.index = index; |
714 |
this.mask = mask; |
715 |
this.del = delegate; |
716 |
this.data = data; |
717 |
} |
718 |
|
719 |
protected Object createAttachedData(NodeSubstitute s) { |
720 |
if (s == null) { |
721 |
assertNotNull(s); |
722 |
} |
723 |
delegateSubsts[index] = s; |
724 |
return data; |
725 |
} |
726 |
|
727 |
boolean isEnabled(long action) { |
728 |
return (mask & action) > 0; |
729 |
} |
730 |
|
731 |
public boolean canCopy(Look.NodeSubstitute substitute) { |
732 |
queried |= ProxyLook.CAN_COPY; |
733 |
return isEnabled(ProxyLook.CAN_COPY) ? |
734 |
del.canCopy(substitute) : false; |
735 |
} |
736 |
|
737 |
public boolean canCut(Look.NodeSubstitute substitute) { |
738 |
queried |= ProxyLook.CAN_CUT; |
739 |
return isEnabled(ProxyLook.CAN_CUT) ? |
740 |
del.canCut(substitute) : false; |
741 |
} |
742 |
|
743 |
public boolean canDestroy(Look.NodeSubstitute substitute) { |
744 |
queried |= ProxyLook.CAN_DESTROY; |
745 |
return isEnabled(ProxyLook.CAN_DESTROY) ? |
746 |
del.canDestroy(substitute) : false; |
747 |
} |
748 |
|
749 |
public boolean canRename(Look.NodeSubstitute substitute) { |
750 |
queried |= ProxyLook.CAN_RENAME; |
751 |
return isEnabled(ProxyLook.CAN_RENAME) ? |
752 |
del.canRename(substitute) : false; |
753 |
} |
754 |
|
755 |
public Transferable clipboardCopy(Look.NodeSubstitute substitute) throws IOException { |
756 |
queried |= ProxyLook.CLIPBOARD_COPY; |
757 |
return isEnabled(ProxyLook.CLIPBOARD_COPY) ? |
758 |
del.clipboardCopy(substitute) : null; |
759 |
} |
760 |
|
761 |
public Transferable clipboardCut(Look.NodeSubstitute substitute) throws IOException { |
762 |
queried |= ProxyLook.CLIPBOARD_CUT; |
763 |
return isEnabled(ProxyLook.CLIPBOARD_CUT) ? |
764 |
del.clipboardCut(substitute) : null; |
765 |
} |
766 |
|
767 |
public Lookup createLookup(Look.NodeSubstitute substitute) { |
768 |
queried |= ProxyLook.CREATE_LOOKUP; |
769 |
return isEnabled(ProxyLook.CREATE_LOOKUP) ? |
770 |
del.createLookup(substitute) : null; |
771 |
} |
772 |
|
773 |
public void destroy(Look.NodeSubstitute substitute) throws IOException { |
774 |
queried |= ProxyLook.DESTROY; |
775 |
if (isEnabled(ProxyLook.DESTROY)) |
776 |
del.destroy(substitute); |
777 |
} |
778 |
|
779 |
public Transferable drag(Look.NodeSubstitute substitute) throws IOException { |
780 |
queried |= ProxyLook.DRAG; |
781 |
return isEnabled(ProxyLook.DRAG) ? |
782 |
del.drag(substitute) : null; |
783 |
} |
784 |
|
785 |
public Action[] getActions(Look.NodeSubstitute substitute) { |
786 |
queried |= ProxyLook.GET_ACTIONS; |
787 |
return isEnabled(ProxyLook.GET_ACTIONS) ? |
788 |
del.getActions(substitute) : null; |
789 |
} |
790 |
|
791 |
public Object[] getChildObjects(Look.NodeSubstitute substitute) { |
792 |
queried |= ProxyLook.GET_CHILD_OBJECTS; |
793 |
// for testing purposes -- |
794 |
if (actualDelegateData != null) { |
795 |
actualDelegateData[index] = getAttachedData(substitute); |
796 |
} |
797 |
return isEnabled(ProxyLook.GET_CHILD_OBJECTS) ? |
798 |
del.getChildObjects(substitute) : null; |
799 |
} |
800 |
|
801 |
public Action[] getContextActions(Look.NodeSubstitute substitute) { |
802 |
queried |= ProxyLook.GET_CONTEXT_ACTIONS; |
803 |
return isEnabled(ProxyLook.GET_CONTEXT_ACTIONS) ? |
804 |
del.getContextActions(substitute) : null; |
805 |
} |
806 |
|
807 |
public java.awt.Component getCustomizer(Look.NodeSubstitute substitute) { |
808 |
queried |= ProxyLook.GET_CUSTOMIZER; |
809 |
return isEnabled(ProxyLook.GET_CUSTOMIZER) ? |
810 |
del.getCustomizer(substitute) : null; |
811 |
} |
812 |
|
813 |
public Action getDefaultAction(Look.NodeSubstitute substitute) { |
814 |
queried |= ProxyLook.GET_DEFAULT_ACTION; |
815 |
return isEnabled(ProxyLook.GET_DEFAULT_ACTION) ? |
816 |
del.getDefaultAction(substitute) : null; |
817 |
} |
818 |
|
819 |
public String getDisplayName() { |
820 |
return "Proxy Delegate (" + mask + ")"; |
821 |
} |
822 |
|
823 |
public String getDisplayName(Look.NodeSubstitute substitute) { |
824 |
queried |= ProxyLook.GET_DISPLAY_NAME; |
825 |
return isEnabled(ProxyLook.GET_DISPLAY_NAME) ? |
826 |
del.getDisplayName(substitute) : null; |
827 |
} |
828 |
|
829 |
public PasteType getDropType(Look.NodeSubstitute substitute, Transferable t, int action, int index) { |
830 |
queried |= ProxyLook.GET_DROP_TYPE; |
831 |
return isEnabled(ProxyLook.GET_DROP_TYPE) ? |
832 |
del.getDropType(substitute, t, action, index) : null; |
833 |
} |
834 |
|
835 |
public HelpCtx getHelpCtx(Look.NodeSubstitute substitute) { |
836 |
queried |= ProxyLook.GET_HELP_CTX; |
837 |
return isEnabled(ProxyLook.GET_HELP_CTX) ? |
838 |
del.getHelpCtx(substitute) : null; |
839 |
} |
840 |
|
841 |
public Image getIcon(Look.NodeSubstitute substitute, int type) { |
842 |
queried |= ProxyLook.GET_ICON; |
843 |
return isEnabled(ProxyLook.GET_ICON) ? |
844 |
del.getIcon(substitute, type) : null; |
845 |
} |
846 |
|
847 |
public String getName() { |
848 |
return "ProxyDelegate(" + mask + ")"; |
849 |
} |
850 |
|
851 |
public String getName(Look.NodeSubstitute substitute) { |
852 |
queried |= ProxyLook.GET_NAME; |
853 |
return isEnabled(ProxyLook.GET_NAME) ? |
854 |
del.getName(substitute) : null; |
855 |
} |
856 |
|
857 |
public NewType[] getNewTypes(Look.NodeSubstitute substitute) { |
858 |
queried |= ProxyLook.GET_NEW_TYPES; |
859 |
return isEnabled(ProxyLook.GET_NEW_TYPES) ? |
860 |
del.getNewTypes(substitute) : null; |
861 |
} |
862 |
|
863 |
public Image getOpenedIcon(Look.NodeSubstitute substitute, int type) { |
864 |
queried |= ProxyLook.GET_OPENED_ICON; |
865 |
return isEnabled(ProxyLook.GET_OPENED_ICON) ? |
866 |
del.getOpenedIcon(substitute, type) : null; |
867 |
} |
868 |
|
869 |
public PasteType[] getPasteTypes(Look.NodeSubstitute substitute, Transferable t) { |
870 |
queried |= ProxyLook.GET_PASTE_TYPES; |
871 |
return isEnabled(ProxyLook.GET_PASTE_TYPES) ? |
872 |
del.getPasteTypes(substitute, t) : null; |
873 |
} |
874 |
|
875 |
public Node.PropertySet[] getPropertySets(Look.NodeSubstitute substitute) { |
876 |
queried |= ProxyLook.GET_PROPERTY_SETS; |
877 |
return isEnabled(ProxyLook.GET_PROPERTY_SETS) ? |
878 |
del.getPropertySets(substitute) : null; |
879 |
} |
880 |
|
881 |
public String getShortDescription(Look.NodeSubstitute substitute) { |
882 |
queried |= ProxyLook.GET_SHORT_DESCRIPTION; |
883 |
return isEnabled(ProxyLook.GET_SHORT_DESCRIPTION) ? |
884 |
del.getShortDescription(substitute) : null; |
885 |
} |
886 |
|
887 |
public boolean hasCustomizer(Look.NodeSubstitute substitute) { |
888 |
queried |= ProxyLook.HAS_CUSTOMIZER; |
889 |
return isEnabled(ProxyLook.HAS_CUSTOMIZER) ? |
890 |
del.hasCustomizer(substitute) : false; |
891 |
} |
892 |
|
893 |
public void setName(Look.NodeSubstitute substitute, String newName) { |
894 |
queried |= ProxyLook.SET_NAME; |
895 |
if (isEnabled(ProxyLook.SET_NAME)) |
896 |
del.setName(substitute, newName); |
897 |
} |
898 |
|
899 |
public Look[] availableLooks(Look.NodeSubstitute substitute) { |
900 |
return null; |
901 |
} |
902 |
|
903 |
public String toString() { |
904 |
return Integer.toString(index); |
905 |
} |
906 |
} |
907 |
|
908 |
class Prx extends ProxyLook { |
909 |
boolean createCalled = false; |
910 |
long delegateMask; |
911 |
boolean delAll; |
912 |
|
913 |
protected Object createAttachedData(NodeSubstitute s) { |
914 |
proxySubst = s; |
915 |
createCalled = true; |
916 |
return DATA; |
917 |
} |
918 |
|
919 |
protected boolean delegateAll(long mask, NodeSubstitute s) { |
920 |
return delAll; |
921 |
} |
922 |
|
923 |
public String getDisplayName() { |
924 |
return "Test look"; |
925 |
} |
926 |
|
927 |
public String getName() { |
928 |
return "TestName"; |
929 |
} |
930 |
|
931 |
public Look[] delegateTo(long method, NodeSubstitute s) { |
932 |
delegateMask |= method; |
933 |
if (forceDelegates != null) |
934 |
return forceDelegates; |
935 |
return maskDelegates; |
936 |
} |
937 |
} |
938 |
|
939 |
private static final class MySubst extends Look.NodeSubstitute { |
940 |
public MySubst(Look look) { |
941 |
super.attachTo(look); |
942 |
} |
943 |
|
944 |
public void setLook(Look look) { |
945 |
super.attachTo(look); |
946 |
} |
947 |
|
948 |
|
949 |
public void fireDisplayNameChange(String o, String n) { |
950 |
} |
951 |
|
952 |
public void fireIconChange() { |
953 |
} |
954 |
|
955 |
public void fireNameChange(String o, String n) { |
956 |
} |
957 |
|
958 |
public void fireNodeDestroyed() { |
959 |
} |
960 |
|
961 |
public void fireOpenedIconChange() { |
962 |
} |
963 |
|
964 |
public void firePropertyChange(String name, Object o, Object n) { |
965 |
} |
966 |
|
967 |
public void firePropertySetsChange(Node.PropertySet[] o, Node.PropertySet[] n) { |
968 |
} |
969 |
|
970 |
public void fireShortDescriptionChange(String o, String n) { |
971 |
} |
972 |
|
973 |
public Object getRepresentedObject() { |
974 |
return null; |
975 |
} |
976 |
|
977 |
public void refreshChildren() { |
978 |
} |
979 |
} |
980 |
} |