| 1 |
/* |
| 2 |
* This file is part of the LibreOffice project. |
| 3 |
* |
| 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/. |
| 7 |
* |
| 8 |
* This file incorporates work covered by the following license notice: |
| 9 |
* |
| 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 . |
| 17 |
*/ |
| 18 |
|
| 19 |
package com.sun.star.lib.uno.helper; |
| 20 |
|
| 21 |
import com.sun.star.uno.XWeak; |
| 22 |
import com.sun.star.lang.XTypeProvider; |
| 23 |
import com.sun.star.uno.UnoRuntime; |
| 24 |
import com.sun.star.lang.XEventListener; |
| 25 |
|
| 26 |
import java.util.logging.Level; |
| 27 |
import java.util.logging.Logger; |
| 28 |
|
| 29 |
import static org.junit.Assert.assertEquals; |
| 30 |
import static org.junit.Assert.assertNotNull; |
| 31 |
import org.junit.Before; |
| 32 |
import org.junit.Test; |
| 33 |
import util.WaitUnreachable; |
| 34 |
|
| 35 |
public class ComponentBase_Test |
| 36 |
{ |
| 37 |
private static final Logger logger = Logger.getLogger(ComponentBase_Test.class.getName()); |
| 38 |
AWeakBase obj1, obj2, obj3; |
| 39 |
Object proxyObj1Weak1; |
| 40 |
Object proxyObj3Weak1; |
| 41 |
Object proxyObj3Weak2; |
| 42 |
Object proxyObj3TypeProv; |
| 43 |
Object proxyObj2TypeProv; |
| 44 |
|
| 45 |
/** Class variables are initialized before each Test method */ |
| 46 |
@Before public void setUp() throws Exception |
| 47 |
{ |
| 48 |
obj1= new AWeakBase(); |
| 49 |
obj2= new AWeakBase(); |
| 50 |
obj3= new AWeakBase(); |
| 51 |
|
| 52 |
proxyObj1Weak1= ProxyProvider.createProxy(obj1, XWeak.class); |
| 53 |
proxyObj3Weak1= ProxyProvider.createProxy(obj3, XWeak.class); |
| 54 |
proxyObj3Weak2= ProxyProvider.createProxy(obj3, XWeak.class); |
| 55 |
assertNotNull(proxyObj1Weak1); |
| 56 |
assertNotNull(proxyObj3Weak1); |
| 57 |
assertNotNull(proxyObj3Weak2); |
| 58 |
|
| 59 |
proxyObj2TypeProv= ProxyProvider.createProxy(obj2, XTypeProvider.class); |
| 60 |
proxyObj3TypeProv= ProxyProvider.createProxy(obj3, XTypeProvider.class); |
| 61 |
assertNotNull(proxyObj2TypeProv); |
| 62 |
assertNotNull(proxyObj3TypeProv); |
| 63 |
} |
| 64 |
|
| 65 |
@Test public void test_dispose() throws Exception |
| 66 |
{ |
| 67 |
logger.log(Level.INFO, "Testing ComponentBase: test_dispose()"); |
| 68 |
ComponentBase comp= new ComponentBase(); |
| 69 |
|
| 70 |
logger.log(Level.FINE, "addEventListener"); |
| 71 |
comp.addEventListener(obj1); |
| 72 |
comp.addEventListener(obj2); |
| 73 |
comp.addEventListener(obj3); |
| 74 |
comp.addEventListener(UnoRuntime.queryInterface(XEventListener.class, proxyObj1Weak1)); |
| 75 |
comp.addEventListener(UnoRuntime.queryInterface(XEventListener.class, proxyObj3Weak2)); |
| 76 |
comp.addEventListener(UnoRuntime.queryInterface(XEventListener.class, proxyObj3TypeProv)); |
| 77 |
obj1.nDisposingCalled = 0; |
| 78 |
obj2.nDisposingCalled = 0; |
| 79 |
obj3.nDisposingCalled = 0; |
| 80 |
|
| 81 |
comp.dispose(); |
| 82 |
assertEquals(obj1.nDisposingCalled, 1); |
| 83 |
assertEquals(obj2.nDisposingCalled, 1); |
| 84 |
assertEquals(obj3.nDisposingCalled, 1); |
| 85 |
|
| 86 |
logger.log(Level.FINE, "Adding a listener after dispose, causes an immediate call to the listener."); |
| 87 |
obj1.nDisposingCalled= 0; |
| 88 |
comp.addEventListener(obj1); |
| 89 |
assertEquals(obj1.nDisposingCalled, 1); |
| 90 |
|
| 91 |
logger.log(Level.FINE, "Calling dispose again must not notify the listeners again."); |
| 92 |
obj1.nDisposingCalled= 0; |
| 93 |
obj2.nDisposingCalled= 0; |
| 94 |
obj3.nDisposingCalled= 0; |
| 95 |
comp.dispose(); // already disposed; |
| 96 |
assertEquals(obj1.nDisposingCalled, 0); |
| 97 |
} |
| 98 |
|
| 99 |
@Test public void test_finalize() throws Exception |
| 100 |
{ |
| 101 |
ComponentBase comp= new ComponentBase(); |
| 102 |
obj1.nDisposingCalled = 0; |
| 103 |
comp.addEventListener(obj1); |
| 104 |
WaitUnreachable u = new WaitUnreachable(comp); |
| 105 |
comp= null; |
| 106 |
u.waitUnreachable(); |
| 107 |
assertEquals(obj1.nDisposingCalled, 1); |
| 108 |
} |
| 109 |
} |