-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathObjectiveCTest.java
More file actions
318 lines (284 loc) · 10.4 KB
/
ObjectiveCTest.java
File metadata and controls
318 lines (284 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
* BridJ - Dynamic and blazing-fast native interop for Java.
* http://bridj.googlecode.com/
*
* Copyright (c) 2010-2015, Olivier Chafik (http://ochafik.com/)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Olivier Chafik nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY OLIVIER CHAFIK AND CONTRIBUTORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.bridj;
import static org.bridj.ObjectiveCTest.TestLib.forwardBlockCallIntIntInt;
import static org.bridj.Pointer.getPointer;
import static org.bridj.Pointer.pointerToInt;
import static org.bridj.objc.FoundationLibrary.pointerToNSString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.HashMap;
import java.util.Map;
import org.bridj.ObjectiveCTest.TestLib.Delg;
import org.bridj.ObjectiveCTest.TestLib.DelgHolder;
import org.bridj.ObjectiveCTest.TestLib.DelgImpl;
import org.bridj.ObjectiveCTest.TestLib.FwdBlock;
import org.bridj.ann.Library;
import org.bridj.ann.Runtime;
import org.bridj.objc.NSAutoreleasePool;
import org.bridj.objc.NSDictionary;
import org.bridj.objc.NSNumber;
import org.bridj.objc.NSObject;
import org.bridj.objc.NSString;
import org.bridj.objc.ObjCBlock;
import org.bridj.objc.ObjCDelegate;
import org.bridj.objc.ObjCObject;
import org.bridj.objc.ObjCProxy;
import org.bridj.objc.ObjectiveCRuntime;
import org.bridj.objc.SEL;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
@Library("Foundation")
@Runtime(ObjectiveCRuntime.class)
public class ObjectiveCTest {
static boolean mac = Platform.isMacOSX() && (System.getenv("BRIDJ_NO_OBJC") == null);
static {
if (mac)
try {
BridJ.register();
} catch (Throwable th) {
if (mac)
throw new RuntimeException(th);
}
}
protected Pointer<NSAutoreleasePool> pool;
@Before
public void init() {
if (!mac) return;
pool = NSAutoreleasePool.new_();
assertNotNull(pool);
}
@After
public void cleanup() {
if (!mac) return;
pool.get().drain();
}
//*
@Test
public void testNSNumber() {
if (!mac) return;
long n = 13;
Pointer<NSNumber> pnn = NSNumber.numberWithLong(n);
//System.out.println("pnn = " + pnn);
NSNumber nn = pnn.get();
//System.out.println("nn = " + nn);
assertEquals(n + "", nn.toString());
assertEquals(n, nn.shortValue());
assertEquals(n, nn.intValue());
assertEquals(n, nn.longValue());
assertEquals(n, nn.floatValue(), 0);
assertEquals(n, nn.doubleValue(), 0);
}
@Library("Foundation")
public static class NSWorkspace extends NSObject
{
public static native Pointer<NSWorkspace> sharedWorkspace();
public native Pointer<?> runningApplications();
}
@Test
public void testNSWorkspace() {
if (!mac) return;
BridJ.register(NSWorkspace.class);
Pointer<NSWorkspace> pWorkspace = NSWorkspace.sharedWorkspace();
assertNotNull(pWorkspace);
NSWorkspace workspace = pWorkspace.get();
assertNotNull(workspace);
}
@Test
public void testNSString() {
if (!mac) return;
for (String s : new String[] { "", "1", "ha\nha\u1234" }) {
assertEquals(s, pointerToNSString(s).get().toString());
NSString ns = new NSString(s);
assertEquals(s, ns.toString());
assertEquals(s.length(), ns.length());
}
}
@Test
public void testSEL() {
if (!mac) return;
for (String s : new String[] { "", "1", "ha:ha" }) {
SEL sel = SEL.valueOf(s);
assertEquals(s, sel.getName());
}
}
@Test
public void testNSDictionary() {
if (!mac) return;
Map<String, NSObject> map = new HashMap<String, NSObject>(), map2;
for (String s : new String[] { "", "1", "ha\nha\u1234" })
map.put(s, NSString.valueOf(s + s));
NSDictionary dic = NSDictionary.valueOf(map);
assertEquals(map.size(), dic.count());
map2 = dic.toMap();
assertEquals(map.size(), map2.size());
//assertEquals(map, map2);
for (Map.Entry<String, NSObject> e : map.entrySet()) {
String key = e.getKey();
NSObject expected = e.getValue();
NSObject got = map2.get(key);
assertEquals(expected, got);
//assertEquals(expected.toString(), got.toString());
}
}
public static class NSNonExistentTestClass extends NSObject {
public native int add2(int a, Pointer<Integer> p);
public native float incf(float v);
public native double add8(byte a, short b, int c, char d, long e, double f, Pointer<Integer> p);
}
static void test_NSNonExistentTestClass_add(ObjCObject proxy) {
if (!mac) return;
NSNonExistentTestClass p = getPointer(proxy).as(NSNonExistentTestClass.class).get();
Pointer<Integer> ptr = pointerToInt(64);
assertEquals(1 + ptr.get(), p.add2(1, ptr));
assertEquals(127, p.add8((byte)1, (short)2, (int)4, (char)8, (long)16, (double)32, ptr), 0);
}
String DESCRIPTION = "WHATEVER !!!";
@Test
public void testProxy() {
if (!mac) return;
ObjCProxy proxy = new ObjCProxy() {
public Pointer<NSString> description() {
return pointerToNSString(DESCRIPTION);
}
public int add2(int a, Pointer<Integer> p) {
return a + p.get();
}
public float incf(float v) {
return v + 1;
}
public double add8(long a, int b, short c, byte d, char e, double f, Pointer<Integer> p) {
return a + b + c + d + e + f + p.get();
}
};
test_NSNonExistentTestClass_add(proxy);
test_NSNonExistentTestClass_add(new ObjCProxy(proxy));
}
@Ignore
@Test
public void testProxyFloat() {
if (!mac) return;
Object proxy = new Object() {
public float incf(float v) {
return v + 1;
}
};
NSNonExistentTestClass p = getPointer(new ObjCProxy(proxy)).as(NSNonExistentTestClass.class).get();
System.out.println(p.description().get());
assertEquals(11, p.incf(10), 0);
}
//*/
public static class NSEvent extends NSObject {
//@Selector("addGlobalMonitorForEventsMatchingMask:handler:")
public static native Pointer addGlobalMonitorForEventsMatchingMask_handler(long mask, Pointer<NSEventGlobalCallback> handler);
}
public abstract static class NSEventGlobalCallback extends ObjCBlock {
public abstract void callback(Pointer<NSEvent> event);
}
@Ignore
@Test
public void testGlobalNSEventHook() throws Exception {
if (!mac) return;
BridJ.register(NSEvent.class);
final boolean called[] = new boolean[1];
NSEventGlobalCallback handler = new NSEventGlobalCallback() {
@Override
public void callback(Pointer<NSEvent> event) {
System.out.println("Event: " + event);
called[0] = true;
}
};
//System.out.println("handler: " + handler);
Pointer hook = NSEvent.addGlobalMonitorForEventsMatchingMask_handler(-1L/*1 << 1*/, getPointer(handler));
//System.out.println("hook: " + hook);
Thread.sleep(10000);
assertTrue(called[0]);
}
@org.bridj.ann.Library("test")
public static class TestLib {
public static interface Delg extends ObjCDelegate {
int add_to(int a, int b);
}
public static class DelgImpl extends NSObject implements Delg {
public native int add_to(int a, int b);
}
public static class DelgHolder extends NSObject {
public native void setDelegate(Pointer<Delg> delegate);
public native int outerAdd_to(int a, int b);
}
public static abstract class FwdBlock extends ObjCBlock {
public abstract int apply(int a, int b);
}
public static native int forwardBlockCallIntIntInt(Pointer<FwdBlock> block, int a, int b);
}
static void testDelegate(Delg delegate) {
DelgHolder holder = new DelgHolder();
holder.setDelegate(getPointer(delegate));
int a = 10, b = 20, expected = a + b;
int res = holder.outerAdd_to(a, b);
//System.out.println(Delg.class.getName() + ".add[through delegate](" + a + ", " + b + ") = " + res);
assertEquals(expected, res);
}
@Test
public void testNativeDelegate() {
if (!mac) return;
testDelegate(new DelgImpl());
}
static class MyDelg extends ObjCProxy implements Delg {
@Override
public int add_to(int a, int b) {
return a + b;
}
}
@Test
public void testJavaDelegate() {
if (!mac) return;
testDelegate(new MyDelg());
}
@Test
@Ignore
public void testBlock() {
if (!mac) return;
FwdBlock block = new FwdBlock() {
@Override
public int apply(int a, int b) {
return a + b;
}
};
int a = 10, b = 20, expected = a + b;
int res = forwardBlockCallIntIntInt(getPointer(block), a, b);
assertEquals(expected, res);
}
//*/
}