-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathNSObjectTest.java
181 lines (132 loc) · 5.18 KB
/
NSObjectTest.java
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
package ca.weblite.objc;
import static ca.weblite.objc.RuntimeUtils.*;
import static org.junit.jupiter.api.Assertions.*;
import java.util.List;
import ca.weblite.objc.foundation.NSRange;
import org.junit.jupiter.api.Test;
import com.sun.jna.Pointer;
import com.sun.jna.Structure;
import ca.weblite.objc.annotations.Msg;
/**
*
* @author shannah
*/
public class NSObjectTest {
/**
* Test of load method, of class Proxy.
*/
@Test
public void testNSArray() {
NSObject o = new NSObject(msgPointer("NSMutableArray", "array"));
o.init("NSObject");
long expectedCount = 0;
long actualCount = (Long)o.send("count");
assertEquals(expectedCount, actualCount);
// Add a string to the array and check that
// the last object matches that string.
// Strings should be automatically converted to NSStrings
String aString = "foobar";
o.send("addObject:", aString);
String obj = (String)o.send("lastObject");
assertEquals(aString, obj);
// There should be one object in the array
expectedCount = 1;
actualCount = (Long)o.send("count");
assertEquals(expectedCount, actualCount);
//Now the string is there
boolean expectedContains = true;
boolean actualContains = o.sendBoolean("containsObject:", aString);
assertEquals(expectedContains, actualContains);
// Let's try to call a method that takes a structure as one of the
// parameters
Pointer[] buffer = new Pointer[1];
NSRange.ByValue range = new NSRange.ByValue();
range.setLength(1);
range.setLocation(0);
o.send("getObjects:range:", buffer, range);
assertEquals(1, buffer.length);
// Make sure that the first (and only entry) in the
// buffer is the same string that we added previously.
assertEquals(aString, str(buffer[0]));
Proxy enumerator = o.sendProxy("objectEnumerator");
String placeHolder = (String)enumerator.send("nextObject");
assertEquals(aString, placeHolder);
Proxy newArray = o.sendProxy("arrayByAddingObject:", "Another String");
//System.out.println(newArray);
assertEquals(2, newArray.sendInt("count"));
}
@Test
public void testCustomClass(){
TestCustomClass cls = new TestCustomClass();
cls.init("NSObject");
String res = (String)cls.send("myCustomString");
assertEquals("My custom string", res);
cls.send("setMyCustomString:", "Changed String");
res = (String)cls.send("myCustomString");
assertEquals("Changed String", res);
int iRes = cls.sendInt("getCustomInt");
assertEquals(34, iRes);
cls.send("setCustomInt:", 12);
assertEquals(12, cls.sendInt("getCustomInt"));
double dRes = cls.sendDouble("getMyDouble");
assertEquals(1.5, dRes);
cls.send("setMyDouble:", 3.0);
dRes = cls.sendDouble("getMyDouble");
assertEquals(3.0, dRes);
assertEquals(3.0, cls.dNum);
Proxy myObj = (Proxy)cls.send("getMyObj");
assertNull(myObj);
Proxy array = Client.getInstance().sendProxy("NSMutableArray", "array");
cls.send("setMyObj:", array);
myObj = cls.sendProxy("getMyObj");
assertEquals(array, myObj);
}
@Test
public void debugModeTest() {
setDebugMode(true);
TestCustomClass cls = new TestCustomClass();
cls.init("NSObject");
cls.send("myCustomString");
setDebugMode(false);
cls.send("myCustomString");
System.out.println("Exactly one set of debug logs should be printed above");
}
public static class TestCustomClass extends NSObject {
private String str = "My custom string";
private int iNum = 34;
private double dNum = 1.5;
private Proxy myObj;
@Msg(selector="myCustomString", like="NSObject.description")
public String myCustomString(){
return str;
}
@Msg(selector="setMyCustomString:", signature="v@:@")
public void setMyCustomString(String str){
this.str = str;
}
@Msg(selector="getCustomInt", signature="i@:")
public int getCustomInt(){
return iNum;
}
@Msg(selector="setCustomInt:", signature="v@:i")
public void setCustomInt(int i){
this.iNum = i;
}
@Msg(selector="getMyDouble", signature="d@:")
public double getMyDouble(){
return dNum;
}
@Msg(selector="setMyDouble:", signature="v@:d")
public void setMyDouble(double d){
dNum = d;
}
@Msg(selector="getMyObj", signature="@@:")
public Proxy getMyObj(){
return myObj;
}
@Msg(selector="setMyObj:", signature="v@:@")
public void setMyObj(Proxy obj){
myObj = obj;
}
}
}