|
| 1 | +/* Copyright (c) 2020 Florian Kistner, All Rights Reserved |
| 2 | + * |
| 3 | + * The contents of this file is dual-licensed under 2 |
| 4 | + * alternative Open Source/Free licenses: LGPL 2.1 or later and |
| 5 | + * Apache License 2.0. (starting with JNA version 4.0.0). |
| 6 | + * |
| 7 | + * You can freely decide which license you want to apply to |
| 8 | + * the project. |
| 9 | + * |
| 10 | + * You may obtain a copy of the LGPL License at: |
| 11 | + * |
| 12 | + * http://www.gnu.org/licenses/licenses.html |
| 13 | + * |
| 14 | + * A copy is also included in the downloadable source code package |
| 15 | + * containing JNA, in file "LGPL2.1". |
| 16 | + * |
| 17 | + * You may obtain a copy of the Apache License at: |
| 18 | + * |
| 19 | + * http://www.apache.org/licenses/ |
| 20 | + * |
| 21 | + * A copy is also included in the downloadable source code package |
| 22 | + * containing JNA, in file "AL2.0". |
| 23 | + */ |
| 24 | +package com.sun.jna.different_package; |
| 25 | + |
| 26 | +import com.sun.jna.*; |
| 27 | +import junit.framework.TestCase; |
| 28 | + |
| 29 | +public class PrivateDirectCallbacksTest extends TestCase { |
| 30 | + private interface PrivateWithCallbackArgumentTestLibrary extends Library { |
| 31 | + PrivateWithCallbackArgumentTestLibrary INSTANCE = new DirectPrivateWithCallbackArgumentTestLibrary(); |
| 32 | + |
| 33 | + interface VoidCallback extends Callback { |
| 34 | + void callback(); |
| 35 | + } |
| 36 | + |
| 37 | + void callVoidCallback(VoidCallback c); |
| 38 | + } |
| 39 | + |
| 40 | + private static class DirectPrivateWithCallbackArgumentTestLibrary implements PrivateWithCallbackArgumentTestLibrary { |
| 41 | + @Override |
| 42 | + public native void callVoidCallback(VoidCallback c); |
| 43 | + |
| 44 | + static { |
| 45 | + Native.register("testlib"); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + public void testCallVoidCallback() { |
| 50 | + final boolean[] called = {false}; |
| 51 | + PrivateWithCallbackArgumentTestLibrary.VoidCallback cb = new PrivateWithCallbackArgumentTestLibrary.VoidCallback() { |
| 52 | + @Override |
| 53 | + public void callback() { |
| 54 | + called[0] = true; |
| 55 | + } |
| 56 | + }; |
| 57 | + PrivateWithCallbackArgumentTestLibrary.INSTANCE.callVoidCallback(cb); |
| 58 | + assertTrue("Callback not called", called[0]); |
| 59 | + } |
| 60 | + |
| 61 | + private interface PrivateWithCallbackReturnTestLibrary extends Library { |
| 62 | + PrivateWithCallbackReturnTestLibrary INSTANCE = new DirectPrivateWithCallbackReturnTestLibrary(); |
| 63 | + |
| 64 | + interface Int32CallbackX extends Callback { |
| 65 | + int callback(int arg); |
| 66 | + } |
| 67 | + |
| 68 | + Int32CallbackX returnCallback(); |
| 69 | + |
| 70 | + Int32CallbackX returnCallbackArgument(Int32CallbackX cb); |
| 71 | + } |
| 72 | + |
| 73 | + private static class DirectPrivateWithCallbackReturnTestLibrary implements PrivateWithCallbackReturnTestLibrary { |
| 74 | + @Override |
| 75 | + public native Int32CallbackX returnCallback(); |
| 76 | + |
| 77 | + @Override |
| 78 | + public native Int32CallbackX returnCallbackArgument(Int32CallbackX cb); |
| 79 | + |
| 80 | + static { |
| 81 | + Native.register("testlib"); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + public void testInvokeCallback() { |
| 86 | + PrivateWithCallbackReturnTestLibrary.Int32CallbackX cb = PrivateWithCallbackReturnTestLibrary.INSTANCE.returnCallback(); |
| 87 | + assertNotNull("Callback should not be null", cb); |
| 88 | + assertEquals("Callback should be callable", 1, cb.callback(1)); |
| 89 | + |
| 90 | + PrivateWithCallbackReturnTestLibrary.Int32CallbackX cb2 = new PrivateWithCallbackReturnTestLibrary.Int32CallbackX() { |
| 91 | + @Override |
| 92 | + public int callback(int arg) { |
| 93 | + return 0; |
| 94 | + } |
| 95 | + }; |
| 96 | + assertSame("Java callback should be looked up", |
| 97 | + cb2, PrivateWithCallbackReturnTestLibrary.INSTANCE.returnCallbackArgument(cb2)); |
| 98 | + assertSame("Existing native function wrapper should be reused", |
| 99 | + cb, PrivateWithCallbackReturnTestLibrary.INSTANCE.returnCallbackArgument(cb)); |
| 100 | + } |
| 101 | + |
| 102 | + public static void main(java.lang.String[] argList) { |
| 103 | + junit.textui.TestRunner.run(PrivateDirectCallbacksTest.class); |
| 104 | + } |
| 105 | +} |
0 commit comments