tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / javaunohelper / test / com / sun / star / lib / uno / helper / InterfaceContainer_Test.java
blobe3e9266afd4d328fd2e80c49b77926486207ea88
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package com.sun.star.lib.uno.helper;
21 import com.sun.star.uno.XWeak;
22 import com.sun.star.lang.XTypeProvider;
23 import com.sun.star.lib.uno.environments.java.java_environment;
25 import java.util.ArrayList;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.ListIterator;
29 import java.util.NoSuchElementException;
31 import java.util.logging.Level;
32 import java.util.logging.Logger;
34 import static org.junit.Assert.assertArrayEquals;
35 import static org.junit.Assert.assertEquals;
36 import static org.junit.Assert.assertFalse;
37 import static org.junit.Assert.assertNotNull;
38 import static org.junit.Assert.assertSame;
39 import static org.junit.Assert.assertTrue;
40 import static org.junit.Assert.fail;
41 import org.junit.Before;
42 import org.junit.Test;
44 public class InterfaceContainer_Test
46 private static final Logger logger = Logger.getLogger(InterfaceContainer_Test.class.getName());
47 java_environment env= new java_environment(null);
48 /** Creates a new instance of InterfaceContainerTest */
49 AWeakBase obj1,obj2,obj3,obj4;
50 Object proxyObj1Weak1;
51 Object proxyObj3Weak1;
52 Object proxyObj3Weak2;
53 Object proxyObj3TypeProv;
54 Object proxyObj2TypeProv;
55 //contains original objects
56 List<Object> list1;
57 //contains original objects + proxies
58 List<Object> list2;
59 //contains original object + proxies + null value
60 List<Object> list3;
62 /** Class variables are initialized before each Test method */
63 @Before public void setUp() throws Exception
65 obj1= new AWeakBase();
66 obj2= new AWeakBase();
67 obj3= new AWeakBase();
68 obj4= new AWeakBase();
70 proxyObj1Weak1= ProxyProvider.createProxy(obj1, XWeak.class);
71 proxyObj3Weak1= ProxyProvider.createProxy(obj3, XWeak.class);
72 proxyObj3Weak2= ProxyProvider.createProxy(obj3, XWeak.class);
73 assertNotNull(proxyObj1Weak1);
74 assertNotNull(proxyObj3Weak1);
75 assertNotNull(proxyObj3Weak2);
77 proxyObj2TypeProv= ProxyProvider.createProxy(obj2, XTypeProvider.class);
78 proxyObj3TypeProv= ProxyProvider.createProxy(obj3, XTypeProvider.class);
79 assertNotNull(proxyObj2TypeProv);
80 assertNotNull(proxyObj3TypeProv);
82 list1= new ArrayList<Object>();
83 list1.add(obj1);
84 list1.add(obj2);
85 list1.add(obj3);
87 list2= new ArrayList<Object>();
88 list2.add(obj1);
89 list2.add(proxyObj2TypeProv);
90 list2.add(proxyObj3TypeProv);
92 list3= new ArrayList<Object>();
93 list3.add(obj1);
94 list3.add(null);
95 list3.add(proxyObj2TypeProv);
96 list3.add(proxyObj3Weak1);
99 /** Tests add(object), size(), clear();
101 @Test public void add() throws Exception
103 logger.log(Level.INFO, "Testing List.add(Object), List.size(), List.clear(), List.isEmpty()");
104 InterfaceContainer cont= new InterfaceContainer();
106 assertEquals(cont.size(), 0);
108 assertTrue(cont.add(obj1));
109 assertEquals(cont.size(), 1);
111 assertTrue(cont.add(obj1)); // add the same object again
112 assertEquals(cont.size(), 2);
114 assertTrue(cont.add(proxyObj3TypeProv));
115 assertEquals(cont.size(), 3);
117 assertFalse(cont.isEmpty());
118 cont.clear();
119 assertEquals(cont.size(), 0);
120 assertTrue(cont.isEmpty());
123 /**Tests list.addAll(Collection c), list.addAll(int index, Collection c)
125 @Test public void listConstructors() throws Exception
127 logger.log(Level.INFO, "Testing Constructors of InterfaceContainer");
128 InterfaceContainer cont= new InterfaceContainer(100);
130 assertEquals(cont.elementData.length, 100);
133 @Test public void trimToSize() throws Exception
135 logger.log(Level.INFO, "Testing InterfaceContainer.trimToSize");
136 InterfaceContainer cont= new InterfaceContainer(100);
138 cont.trimToSize();
139 assertTrue(cont.isEmpty());
140 cont= new InterfaceContainer(10);
141 cont.addAll(list1);
142 cont.trimToSize();
143 assertEquals(cont.elementData.length, list1.size());
146 @Test public void ensureCapacity() throws Exception
148 logger.log(Level.INFO, "Testing InterfaceContainer.ensureCapacity");
149 InterfaceContainer cont= new InterfaceContainer(10);
151 cont.ensureCapacity(9);
152 assertTrue(cont.elementData.length >= 9);
154 cont.ensureCapacity(11);
155 assertTrue(cont.elementData.length >= 11);
158 @Test public void addAll() throws Exception
160 logger.log(Level.INFO, "Testing List.addAll(Collection c), List.addAll(int index, Collection c)");
161 InterfaceContainer cont= new InterfaceContainer();
163 assertTrue(cont.addAll(list1));
164 assertEquals(cont.size(), list1.size());
165 for (int c= 0; c < cont.size(); c++)
167 assertSame(list1.get(c), cont.get(c));
169 assertTrue(cont.add(obj1));
170 assertTrue(cont.addAll(1, list2));
171 assertSame(cont.get(0), list1.get(0));
172 assertSame(cont.get(1), list2.get(0));
173 assertSame(cont.get(2), list2.get(1));
174 assertSame(cont.get(3), list2.get(2));
175 assertSame(cont.get(4), list1.get(1));
176 assertSame(cont.get(5), list1.get(2));
177 assertSame(cont.get(6), obj1);
179 cont.clear();
180 cont.addAll(list3);
181 // the null element in list3 at index 1 is not added to cont
182 assertSame(cont.get(0), list3.get(0));
183 assertSame(cont.get(1), list3.get(2));
184 assertSame(cont.get(2), list3.get(3));
187 /** Tests List.add(int index, Object element), List.get(int index)
189 @Test public void get() throws Exception
191 logger.log(Level.INFO, "Testing List.add(int index, Object element), List.get(int index)");
192 InterfaceContainer cont= new InterfaceContainer();
194 cont.add(0, obj1);
195 cont.add(1, obj2);
196 cont.add(1, proxyObj3Weak1);
197 cont.add(3, obj3);
199 assertSame(cont.get(0), obj1);
200 assertSame(cont.get(1), proxyObj3Weak1);
201 assertSame(cont.get(2), obj2);
202 assertSame(cont.get(3), obj3);
204 try {
205 cont.add(5, obj1);
206 fail("IndexOutOfBoundsException expected");
207 } catch (IndexOutOfBoundsException indexOutOfBoundsException) {
208 logger.log(Level.FINE, "IndexOutOfBoundsException caught");
212 /** Tests List.contains
214 @Test public void contains() throws Exception
216 logger.log(Level.INFO, "Testing List.contains()");
217 InterfaceContainer cont= new InterfaceContainer();
219 assertFalse(cont.contains(obj1)); // nothing in the list
221 cont.add(obj1);
222 cont.add(proxyObj2TypeProv);
223 cont.add(proxyObj3TypeProv);
225 assertTrue(cont.contains(obj1));
226 assertTrue(cont.contains(obj2));
227 assertTrue(cont.contains(proxyObj3Weak1));
228 assertTrue(cont.contains(proxyObj3Weak2));
229 assertTrue(cont.contains(proxyObj1Weak1));
230 assertTrue(cont.contains(obj3));
231 assertFalse(cont.contains(null));
234 /** Tests List.containsAll
236 @Test public void containsAll() throws Exception
238 logger.log(Level.INFO, "Testing List.containsAll");
239 InterfaceContainer cont= new InterfaceContainer();
241 cont.addAll(list1);
242 assertTrue(cont.containsAll(list1));
244 cont.clear();
245 cont.addAll(list2);
246 assertTrue(cont.containsAll(list2));
248 cont.clear();
249 cont.addAll(list3); // the null element in list3 is not added to cont
250 assertFalse(cont.containsAll(list3));
252 cont.clear();
253 for( int x= 0; x < 6; x++)
254 cont.add(obj4);
255 assertFalse(cont.contains(list1));
257 cont.add(1, list1.get(0));
258 cont.add(3, list1.get(1));
259 cont.add(5, list1.get(2));
260 assertFalse(cont.contains(list1));
262 /** Tests List.indexOf, List.lastIndexOf
264 @Test public void indexOf() throws Exception
266 logger.log(Level.INFO, "Testing List.indexOf(Object element), List.lastIndexOf(Object element)");
267 InterfaceContainer cont= new InterfaceContainer();
269 cont.addAll(list1);
270 cont.addAll(list1);
271 assertEquals(cont.indexOf(obj3), 2);
272 assertEquals(cont.lastIndexOf(obj3), 5);
274 cont.clear();
275 cont.addAll(list2);
276 cont.addAll(list2);
277 assertEquals(cont.indexOf(proxyObj3Weak1), 2);
278 assertEquals(cont.lastIndexOf(proxyObj3Weak2), 5);
281 /** Tests List.remove(int index), List.remove(Object element), List.removeAll(Collection c)
283 @Test public void remove() throws Exception
285 logger.log(Level.INFO, "Testing List.remove(int index), List.remove(Object element), List.removeAll(Collection c)");
286 InterfaceContainer cont= new InterfaceContainer();
288 cont.addAll(list2);
289 assertTrue(proxyObj2TypeProv.equals(cont.remove(1)));
290 assertEquals(cont.size(), 2);
292 cont.clear();
293 cont.addAll(list2);
294 assertTrue(cont.remove(obj1));
295 assertTrue(cont.remove(proxyObj2TypeProv));
296 assertTrue(cont.remove(proxyObj3Weak2));
297 assertTrue(cont.isEmpty());
299 cont.clear();
300 cont.addAll(list3);
301 assertTrue(cont.removeAll(list3));
302 assertTrue(cont.isEmpty());
304 cont.addAll(list2);
305 List<Object> list= new ArrayList<Object>();
306 list.add(list2.get(0));
307 list.add(list2.get(1));
308 list.add(proxyObj3Weak2);
309 assertTrue(cont.removeAll(list));
310 assertTrue(cont.isEmpty());
313 /** Tests List.retainAll
315 @Test public void retainAll() throws Exception
317 logger.log(Level.INFO, "Testing List.retainAll(Collection c)");
318 InterfaceContainer cont= new InterfaceContainer();
320 cont.addAll(list1); //obj1, obj2, obj3
321 cont.addAll(list2); //obj1, proxyObj2TypeProv, proxyObj3TypeProv
322 List<Object> list = new ArrayList<Object>();
323 list.add(obj1);
324 list.add(proxyObj3Weak1);
326 assertTrue(cont.retainAll(list));
327 assertSame(cont.get(0), obj1);
328 assertSame(cont.get(1), obj3);
329 assertSame(cont.get(2), obj1);
330 assertSame(cont.get(3), proxyObj3TypeProv);
331 assertEquals(cont.size(), 4);
334 /** Tests List.set(int index, Object element)
336 @Test public void set() throws Exception
338 logger.log(Level.INFO, "Testing List.set(int index, Object element)");
339 InterfaceContainer cont= new InterfaceContainer();
341 cont.addAll(list2);
342 Object o1= cont.set(0, obj3);
343 Object o2= cont.set(2, proxyObj3Weak1);
345 assertSame(list2.get(0), o1);
346 assertSame(list2.get(2), o2);
347 assertSame(cont.get(0), obj3);
348 assertSame(cont.get(2), proxyObj3Weak1);
351 /** Tests List.toArray(), List.toArray(Object[] a)
353 @Test public void toArray() throws Exception
355 logger.log(Level.INFO, "Testing List.toArray(), List.toArray(Object[] a)");
356 InterfaceContainer cont= new InterfaceContainer();
358 cont.addAll(list1);
359 Object[] ar= cont.toArray();
360 Object[] arOrig= list1.toArray();
361 assertEquals(ar.length, arOrig.length);
362 assertArrayEquals(ar, arOrig);
364 XWeak[] arWeak= new XWeak[3];
365 XWeak[] arWeak2= (XWeak[])cont.toArray(arWeak);
366 assertEquals(ar.length, arWeak2.length);
367 assertArrayEquals(ar, arWeak2);
370 @Test public void Iterator_next() throws Exception
372 logger.log(Level.INFO, "Testing InterfaceContainer.iterator, Iterator.next()");
373 InterfaceContainer cont= new InterfaceContainer();
375 cont.addAll(list1);
376 Iterator it= cont.iterator();
377 assertSame(it.next(), list1.get(0));
378 assertSame(it.next(), list1.get(1));
379 assertSame(it.next(), list1.get(2));
381 try {
382 it.next();
383 fail("NoSuchElementException expected");
384 } catch (NoSuchElementException noSuchElementException) {
385 logger.log(Level.FINE, "NoSuchElementException caught");
389 @Test public void Iterator_hasNext() throws Exception
391 logger.log(Level.INFO, "Testing, Iterator.next()");
392 InterfaceContainer cont= new InterfaceContainer();
394 Iterator it= cont.iterator();
395 assertFalse(it.hasNext());
397 cont.addAll(list1);
398 it= cont.iterator();
399 assertTrue(it.hasNext());
401 it.next();
402 assertTrue(it.hasNext());
404 it.next();
405 assertTrue(it.hasNext());
407 it.next();
408 assertFalse(it.hasNext());
411 @Test public void Iterator_remove() throws Exception
413 logger.log(Level.INFO, "Testing, Iterator.remove()");
414 InterfaceContainer cont= new InterfaceContainer();
416 Iterator it= cont.iterator();
417 try {
418 it.remove();
419 fail("IllegalStateException expected");
420 } catch (IllegalStateException illegalStateException) {
421 logger.log(Level.FINE, "IllegalStateException caught");
424 cont.add(obj1);
425 it= cont.iterator();
426 it.next();
427 it.remove();
428 assertTrue(cont.isEmpty());
429 try {
430 it.remove();
431 fail("IllegalStateException expected");
432 } catch (IllegalStateException illegalStateException) {
433 logger.log(Level.FINE, "IllegalStateException caught");
436 cont.clear();
437 cont.addAll(list1);
438 it= cont.iterator();
439 while (it.hasNext())
441 it.next();
442 it.remove();
444 assertTrue(cont.isEmpty());
446 // 2 iterators, remove must not impair the other iterator
447 cont.clear();
448 cont.addAll(list1);
449 int size= cont.size();
450 it= cont.iterator();
451 Iterator it2= cont.iterator();
452 while (it.hasNext())
454 it.next();
455 it.remove();
457 for( int c= 0; c < size; c++)
458 it2.next();
459 assertEquals(cont.size(), 0);
462 @Test public void ListIterator_next() throws Exception
464 logger.log(Level.INFO, "Testing InterfaceContainer.listIterator, ListIterator.next()");
465 InterfaceContainer cont= new InterfaceContainer();
467 cont.addAll(list1);
468 Iterator it= cont.listIterator();
469 assertSame(it.next(), list1.get(0));
470 assertSame(it.next(), list1.get(1));
471 assertSame(it.next(), list1.get(2));
472 try {
473 it.next();
474 fail("NoSuchElementException expected");
475 } catch (NoSuchElementException noSuchElementException) {
476 logger.log(Level.FINE, "NoSuchElementException caught");
480 @Test public void ListIterator_hasNext() throws Exception
482 logger.log(Level.INFO, "Testing ListIterator.hasNext()");
483 InterfaceContainer cont= new InterfaceContainer();
485 Iterator it= cont.listIterator();
486 assertFalse(it.hasNext());
488 cont.addAll(list1);
489 it= cont.listIterator();
490 assertTrue(it.hasNext());
491 it.next();
492 assertTrue(it.hasNext());
493 it.next();
494 assertTrue(it.hasNext());
495 it.next();
496 assertFalse(it.hasNext());
499 @Test public void ListIterator_remove() throws Exception
501 logger.log(Level.INFO, "Testing ListIterator.remove()");
502 InterfaceContainer cont= new InterfaceContainer();
504 ListIterator it= cont.listIterator();
505 try {
506 it.remove();
507 fail("IllegalStateException expected");
508 } catch (IllegalStateException illegalStateException) {
509 logger.log(Level.FINE, "IllegalStateException caught");
512 cont.add(obj1);
513 it= cont.listIterator();
514 it.next();
515 it.remove();
516 assertTrue(cont.isEmpty());
517 try {
518 it.remove();
519 fail("IllegalStateException expected");
520 } catch (IllegalStateException illegalStateException) {
521 logger.log(Level.FINE, "IllegalStateException caught");
524 cont.clear();
525 cont.addAll(list1);
526 it= cont.listIterator();
527 while (it.hasNext())
529 it.next();
530 it.remove();
532 assertTrue(cont.isEmpty());
534 // 2 iterators, remove must not impair the other iterator
535 cont.clear();
536 cont.addAll(list1);
537 int size= cont.size();
538 it= cont.listIterator();
539 Iterator it2= cont.listIterator();
540 while (it.hasNext())
542 it.next();
543 it.remove();
545 for( int c= 0; c < size; c++)
546 it2.next();
547 assertEquals(cont.size(), 0);
550 @Test public void ListIterator_hasPrevious() throws Exception
552 logger.log(Level.INFO, "Testing ListIterator.hasPrevious()");
553 InterfaceContainer cont= new InterfaceContainer();
555 ListIterator it= cont.listIterator();
556 assertFalse(it.hasPrevious());
557 cont.addAll(list1);
558 it= cont.listIterator();
559 while (it.hasNext())
561 it.next();
562 assertTrue(it.hasPrevious());
566 @Test public void ListIterator_previous() throws Exception
568 logger.log(Level.INFO, "Testing ListIterator.previous()");
569 InterfaceContainer cont= new InterfaceContainer();
571 cont.addAll(list1);
572 // go to the end of our list and list1
573 ListIterator it= cont.listIterator();
574 while (it.hasNext()) {
575 it.next();
577 ListIterator it_list1= list1.listIterator();
578 while (it_list1.hasNext()) {
579 it_list1.next();
582 while (it.hasPrevious())
584 assertSame(it.previous(), it_list1.previous());
586 try {
587 it.previous();
588 fail("NoSuchElementException expected");
589 } catch (NoSuchElementException noSuchElementException) {
590 logger.log(Level.FINE, "NoSuchElementException caught");
594 @Test public void ListIterator_nextIndex() throws Exception
596 logger.log(Level.INFO, "Testing ListIterator.nextIndex()");
597 InterfaceContainer cont= new InterfaceContainer();
599 ListIterator it;
600 cont.addAll(list1);
601 it= cont.listIterator();
602 assertEquals(it.nextIndex(), 0);
603 it.next();
604 assertEquals(it.nextIndex(), 1);
605 it.next();
606 assertEquals(it.nextIndex(), 2);
609 @Test public void ListIterator_previousIndex() throws Exception
611 logger.log(Level.INFO, "Testing ListIterator.previousIndex()");
612 InterfaceContainer cont= new InterfaceContainer();
614 ListIterator it;
615 cont.addAll(list1);
616 it= cont.listIterator();
617 while (it.hasNext()) {
618 it.next();
621 assertEquals(it.previousIndex(), 2);
622 it.previous();
623 assertEquals(it.previousIndex(), 1);
624 it.previous();
625 assertEquals(it.previousIndex(), 0);
626 it.previous();
629 @SuppressWarnings("unchecked")
630 @Test public void ListIterator_add() throws Exception
632 logger.log(Level.INFO, "Testing ListIterator.add()");
633 InterfaceContainer cont= new InterfaceContainer();
635 ListIterator it= cont.listIterator();
636 it.add(obj1);
637 assertEquals(cont.size(), 1);
638 it.add(obj2);
639 assertEquals(cont.size(), 2);
640 it.add(obj3);
641 assertSame(it.previous(), obj3);
642 assertSame(it.previous(), obj2);
643 assertSame(it.previous(), obj1);
646 @Test public void disposeAndClear() throws Exception
648 logger.log(Level.INFO, "Testing InterfaceContainer.disposeAndClear");
649 InterfaceContainer cont= new InterfaceContainer(10);
651 cont.add(obj1);
652 cont.add(obj2);
653 cont.add(obj3);
654 cont.add(proxyObj1Weak1);
655 cont.add(proxyObj3TypeProv);
656 logger.log(Level.FINE, "Two proxies are called. Check the output:");
657 cont.disposeAndClear(new com.sun.star.lang.EventObject());
658 assertEquals(obj1.nDisposingCalled, 1);
659 assertEquals(obj2.nDisposingCalled, 1);
660 assertEquals(obj3.nDisposingCalled, 1);