/
RegistrarTest.cs
2693 lines (2317 loc) · 86 KB
/
RegistrarTest.cs
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Threading;
#if XAMCORE_2_0
#if !__TVOS__ && !__WATCHOS__ && !MONOMAC
using AddressBook;
using AddressBookUI;
#endif
using Foundation;
#if MONOMAC
using AppKit;
using UIColor = AppKit.NSColor;
using PlatformException = ObjCRuntime.RuntimeException;
using NativeException = Foundation.ObjCException;
#else
using UIKit;
using PlatformException = ObjCRuntime.RuntimeException;
using NativeException = Foundation.MonoTouchException;
#endif
using ObjCRuntime;
#if !__TVOS__
using MapKit;
#endif
#if !__WATCHOS__
using CoreAnimation;
#endif
using CoreGraphics;
using CoreLocation;
#if !__TVOS__
using Contacts;
#endif
#else
using MonoTouch;
using MonoTouch.AddressBook;
using MonoTouch.AddressBookUI;
using MonoTouch.Foundation;
using MonoTouch.ObjCRuntime;
using MonoTouch.MapKit;
using MonoTouch.CoreAnimation;
using MonoTouch.CoreGraphics;
using MonoTouch.CoreLocation;
using MonoTouch.UIKit;
using PlatformException=MonoTouch.RuntimeException;
using NativeException=MonoTouch.Foundation.MonoTouchException;
#endif
using OpenTK;
using NUnit.Framework;
using Bindings.Test;
#if XAMCORE_2_0
using RectangleF = CoreGraphics.CGRect;
using SizeF = CoreGraphics.CGSize;
using PointF = CoreGraphics.CGPoint;
using CategoryAttribute = ObjCRuntime.CategoryAttribute;
#else
using nfloat=global::System.Single;
using nint=global::System.Int32;
using nuint=global::System.UInt32;
using CategoryAttribute=MonoTouch.ObjCRuntime.CategoryAttribute;
#endif
using XamarinTests.ObjCRuntime;
namespace MonoTouchFixtures.ObjCRuntime {
[TestFixture]
[Preserve (AllMembers = true)]
public class RegistrarTest {
#if !XAMCORE_2_0
// This should throw an exception at build time (device) or at registration time (startup on simulator).
// Putting it here for now so I don't forget.
[Export ()]
public bool? IsRegistered { get; set; }
#endif
public static Registrars CurrentRegistrar {
get {
return RegistrarSharedTest.CurrentRegistrar;
}
}
[Test]
public void RegistrarRemoval ()
{
// define set by xharness when creating test variations.
// It's not safe to remove the dynamic registrar in monotouch-test (by design; some of the tested API makes it unsafe, and the linker correcty detects this),
// so the dynamic registrar will only be removed if manually requested.
// Also removal of the dynamic registrar is not supported in XM
#if OPTIMIZEALL && !__MACOS__
var shouldBeRemoved = true;
#else
var shouldBeRemoved = false;
#endif
Assert.AreEqual (shouldBeRemoved, typeof (NSObject).Assembly.GetType ("Registrar.Registrar") == null, "Registrar removal");
Assert.AreEqual (shouldBeRemoved, typeof (NSObject).Assembly.GetType ("Registrar.DynamicRegistrar") == null, "DynamicRegistrar removal");
}
#if !MONOMAC
[Test]
public void TestProperties ()
{
RegistrarTestClass obj = new RegistrarTestClass ();
IntPtr receiver = obj.Handle;
int dummy = 314;
// readonly, attribute on property
CallProperty (receiver, "Property1", ref obj.called_Property1Getter, "#Instance-1-r");
CallProperty (receiver, "setProperty1:", ref dummy, "#Instance-1-w", true);
// rw, attribute on property
CallProperty (receiver, "Property2", ref obj.called_Property2Getter, "#Instance-2-r");
CallProperty (receiver, "setProperty2:", ref obj.called_Property2Setter, "#Instance-2-w");
// writeonly, attribute on property
//CallProperty (receiver, "Property3", ref dummy, "#Instance-3-r", true);
CallProperty (receiver, "setProperty3:", ref obj.called_Property3Setter, "#Instance-3-w");
// readonly, atteribute on getter
CallProperty (receiver, "Property4", ref obj.called_Property4Getter, "#Instance-4-r");
CallProperty (receiver, "setProperty4:", ref dummy, "#Instance-4-w", true);
// rw, attribyte on getter/setter
CallProperty (receiver, "Property5", ref obj.called_Property5Getter, "#Instance-5-r");
CallProperty (receiver, "setProperty5:", ref dummy, "#Instance-5-w1", true);
CallProperty (receiver, "WProperty5:", ref obj.called_Property5Setter, "#Instance-5-w2");
// writeonly, attribute on setter
CallProperty (receiver, "setProperty6:", ref dummy, "#Instance-6-r", true);
CallProperty (receiver, "Property6:", ref obj.called_Property6Setter, "#Instance-6-w");
}
[Test]
public void TestStaticProperties ()
{
IntPtr receiver = Class.GetHandle ("RegistrarTestClass");
int dummy = 314;
RegistrarTestClass.called_StaticProperty1Getter = 0;
RegistrarTestClass.called_StaticProperty2Getter = 0;
RegistrarTestClass.called_StaticProperty4Getter = 0;
RegistrarTestClass.called_StaticProperty5Getter = 0;
RegistrarTestClass.called_StaticProperty2Setter = 0;
RegistrarTestClass.called_StaticProperty3Setter = 0;
RegistrarTestClass.called_StaticProperty5Setter = 0;
RegistrarTestClass.called_StaticProperty6Setter = 0;
// readonly, attribute on property
CallProperty (receiver, "StaticProperty1", ref RegistrarTestClass.called_StaticProperty1Getter, "#Static-1-r");
CallProperty (receiver, "setStaticProperty1:", ref dummy, "#Static-1-w", true);
// rw, attribute on property
CallProperty (receiver, "StaticProperty2", ref RegistrarTestClass.called_StaticProperty2Getter, "#Static-2-r");
CallProperty (receiver, "setStaticProperty2:", ref RegistrarTestClass.called_StaticProperty2Setter, "#Static-2-w");
// writeonly, attribute on property
CallProperty (receiver, "StaticProperty3", ref dummy, "#Static-3-r", true);
CallProperty (receiver, "setStaticProperty3:", ref RegistrarTestClass.called_StaticProperty3Setter, "#Static-3-w");
// readonly, atteribute on getter
CallProperty (receiver, "StaticProperty4", ref RegistrarTestClass.called_StaticProperty4Getter, "#Static-4-r");
CallProperty (receiver, "setStaticProperty4:", ref dummy, "#Static-4-w", true);
// rw, attribyte on getter/setter
CallProperty (receiver, "StaticProperty5", ref RegistrarTestClass.called_StaticProperty5Getter, "#Static-5-r");
CallProperty (receiver, "setStaticProperty5:", ref dummy, "#Static-5-w1", true);
CallProperty (receiver, "WStaticProperty5:", ref RegistrarTestClass.called_StaticProperty5Setter, "#Static-5-w2");
// writeonly, attribute on setter
CallProperty (receiver, "setStaticProperty6:", ref dummy, "#Static-6-r", true);
CallProperty (receiver, "StaticProperty6:", ref RegistrarTestClass.called_StaticProperty6Setter, "#Static-6-w");
}
#endif
void CallProperty (IntPtr receiver, string selector, ref int called_var, string id, bool expectFailure = false)
{
#if !MONOMAC
if (Runtime.Arch == Arch.DEVICE && expectFailure) {
Console.WriteLine ("Skipping '{0}', it's expected to throw a 'Selector not found exception', but on device it seems to crash instead", selector);
return;
}
#endif
try {
Messaging.bool_objc_msgSend (receiver, new Selector (selector).Handle);
Assert.That (!expectFailure, id + "-expected-failure-but-succeeded");
Assert.That (called_var == 1, id + "-called-var");
} catch (NativeException ex) {
Assert.That (expectFailure, id + "-expected-success-but-failed: " + ex.Message);
}
}
[Test]
public void TestINativeObject ()
{
IntPtr receiver = Class.GetHandle ("RegistrarTestClass");
IntPtr ptr;
CGPath path;
if ((CurrentRegistrar & Registrars.AllStatic) == 0)
Assert.Ignore ("This test only passes with the static registrars.");
Assert.False (Messaging.bool_objc_msgSend_IntPtr (receiver, new Selector ("INativeObject1:").Handle, IntPtr.Zero), "#a1");
Assert.True (Messaging.bool_objc_msgSend_IntPtr (receiver, new Selector ("INativeObject1:").Handle, new CGPath ().Handle), "#a2");
Assert.That (Messaging.IntPtr_objc_msgSend_bool (receiver, new Selector ("INativeObject2:").Handle, false), Is.EqualTo (IntPtr.Zero), "#b1");
Assert.That (Messaging.IntPtr_objc_msgSend_bool (receiver, new Selector ("INativeObject2:").Handle, true), Is.Not.EqualTo (IntPtr.Zero), "#b2");
void_objc_msgSend_out_IntPtr_bool (receiver, new Selector ("INativeObject3:create:").Handle, out ptr, true);
Assert.That (ptr, Is.Not.EqualTo (IntPtr.Zero), "#c1");
void_objc_msgSend_out_IntPtr_bool (receiver, new Selector ("INativeObject3:create:").Handle, out ptr, false);
Assert.That (ptr, Is.EqualTo (IntPtr.Zero), "#c2");
path = null;
ptr = IntPtr.Zero;
Assert.False (bool_objc_msgSend_ref_intptr (receiver, new Selector ("INativeObject4:").Handle, ref ptr), "#d1");
Assert.That (ptr, Is.EqualTo (IntPtr.Zero), "#d2");
path = new CGPath ();
ptr = path.Handle;
Assert.True (bool_objc_msgSend_ref_intptr (receiver, new Selector ("INativeObject4:").Handle, ref ptr), "#d3");
Assert.That (ptr, Is.EqualTo (path.Handle), "#d4");
ptr = Messaging.IntPtr_objc_msgSend_bool (receiver, new Selector ("INativeObject5:").Handle, false);
Assert.That (ptr, Is.EqualTo (IntPtr.Zero), "#e1");
ptr = Messaging.IntPtr_objc_msgSend_bool (receiver, new Selector ("INativeObject5:").Handle, true);
Assert.That (ptr, Is.Not.EqualTo (IntPtr.Zero), "#e2");
path = new CGPath (ptr);
path.AddArc (1, 2, 3, 4, 5, false); // this should crash if we get back a bogus ptr
}
[Test]
public void TestVirtual ()
{
// bug #4426
DerivedRegistrar1 d1 = new DerivedRegistrar1 ();
DerivedRegistrar2 d2 = new DerivedRegistrar2 ();
Selector sel = new Selector ("VirtualMethod");
string a = NSString.FromHandle (Messaging.IntPtr_objc_msgSend (d1.Handle, sel.Handle)).ToString ();
string b = NSString.FromHandle (Messaging.IntPtr_objc_msgSend (d2.Handle, sel.Handle)).ToString ();
Assert.That (a, Is.EqualTo (d1.GetType ().Name), "#a");
Assert.That (b, Is.EqualTo (d2.GetType ().Name), "#b");
}
[Test]
public void TestOutNSString ()
{
var obj = new RegistrarTestClass ();
var sel = new Selector ("testOutNSString:");
IntPtr ptr;
void_objc_msgSend_out_IntPtr (obj.Handle, sel.Handle, out ptr);
Assert.AreEqual ("Santa is coming", NSString.FromHandle (ptr), "#santa");
}
[Test]
public void TestInheritedStaticMethods ()
{
// bug #6170
int rv;
rv = Messaging.int_objc_msgSend (Class.GetHandle (typeof(StaticBaseClass)), Selector.GetHandle ("foo"));
Assert.AreEqual (rv, 314, "#base");
rv = Messaging.int_objc_msgSend (Class.GetHandle (typeof(StaticDerivedClass)), Selector.GetHandle ("foo"));
Assert.AreEqual (rv, 314, "#derived");
}
[Test]
public void TestStructAndOut ()
{
var obj = new RegistrarTestClass ();
var sel = new Selector ("testOutParametersWithStructs:in:out:");
NSError value = new NSError ();
IntPtr ptr;
SizeF size = new SizeF (1, 2);
void_objc_msgSend_SizeF_IntPtr_out_IntPtr (obj.Handle, sel.Handle, size, value.Handle, out ptr);
Assert.AreEqual (value.Handle, ptr, "#1");
}
#if !__TVOS__ && !__WATCHOS__ && !MONOMAC
[MonoPInvokeCallback (typeof (DActionArity1V1))]
static void DActionArity1V1Func (IntPtr block, UIBackgroundFetchResult result)
{
}
[Test]
public void TestAction ()
{
if (!Runtime.DynamicRegistrationSupported)
Assert.Ignore ("This test requires the dynamic registrar to be available.");
using (var obj = new RegistrarTestClass ()) {
var sel = new Selector ("testAction:");
var block = new BlockLiteral ();
var tramp = new DActionArity1V1 (DActionArity1V1Func);
var del = new DActionArity1V1 (DActionArity1V1Func);
block.SetupBlock (tramp, del);
void_objc_msgSend_ref_BlockLiteral (obj.Handle, sel.Handle, ref block);
block.CleanupBlock ();
}
}
#endif // !__TVOS__ && !__WATCHOS__
class TS1 : NSObject {}
class TS2 : NSObject {}
class TS3 : NSObject {}
class TS4 : NSObject {}
class TS5 : NSObject {}
class TS6 : NSObject {}
class TS7 : NSObject {}
class TS8 : NSObject {}
class TS9 : NSObject {}
class TS10 : NSObject {}
static bool tested_thread_safety;
[Test]
public void TestThreadSafety ()
{
// Create X number of threads.
// Each thread waits for a signal from the main thread to go ahead with the actual test.
if (tested_thread_safety)
Assert.Ignore ("This test can only be executed once. To run it again restart the test app.");
tested_thread_safety = true;
var threads = new Thread [5];
var types = new Type [] { typeof(TS1), typeof(TS2), typeof(TS3), typeof(TS4), typeof(TS5), typeof (TS6), typeof (TS7), typeof (TS8), typeof (TS9), typeof (TS10) };
var exceptions = new List<Exception> ();
var wait = new ManualResetEvent (false);
var start_counter = new CountdownEvent (threads.Length);
var end_counter = new CountdownEvent (threads.Length);
for (int i = 0; i < threads.Length; i++) {
threads [i] = new Thread (() => {
try {
start_counter.Signal (); // signal "I'm ready"
wait.WaitOne (); // wait for go-ahead
// Do the actual test.
// We fetch the class handle for the types in question in several threads at once.
// This will cause the registrar to try to register the type, but that should only
// be done once.
foreach (var t in types)
Class.GetHandle (t);
} catch (Exception ex) {
lock (exceptions)
exceptions.Add (ex);
} finally {
end_counter.Signal (); // signal "I'm done"
}
}) {
IsBackground = true,
};
threads [i].Start ();
}
// Wait for X "I'm ready" signals
Assert.IsTrue (start_counter.Wait (1000), "all threads didn't spin up in 1s");
wait.Set (); // let the threads go wild.
Assert.IsTrue (end_counter.Wait (5000), "all threads didn't finish testing in 5s");
for (int i = 0; i < threads.Length; i++) {
Assert.IsTrue (threads [i].Join (1000), "join #" + i.ToString ());
}
if (exceptions.Count > 0) {
Assert.Fail ("Expected no exceptions, but got:\n{0}",
new AggregateException (exceptions).ToString ());
}
}
[Test]
public void TestRetainReturnValue ()
{
IntPtr ptr;
// The NSAutoreleasePool is to flush any pending 'autorelease' calls.
// We fetch the managed object to make sure there are no lingering
// managed objects (otherwise we wouldn't know if the GC had freed
// the managed object or not).
using (var obj = new RegistrarTestClass ()) {
using (var pool = new NSAutoreleasePool ())
ptr = Messaging.IntPtr_objc_msgSend (obj.Handle, Selector.GetHandle ("testRetainArray"));
using (var rv = Runtime.GetNSObject (ptr)) {
Assert.AreEqual (2, rv.RetainCount, "array");
Assert.AreSame (typeof(NSArray), rv.GetType (), "array type");
rv.DangerousRelease ();
}
using (var pool = new NSAutoreleasePool ())
ptr = Messaging.IntPtr_objc_msgSend (obj.Handle, Selector.GetHandle ("testReturnINativeObject"));
using (var rv = Runtime.GetNSObject (ptr)) {
Assert.AreEqual (2, rv.RetainCount, "inativeobject");
Assert.AreSame (typeof(NSObject), rv.GetType (), "inativeobject type");
rv.DangerousRelease ();
}
using (var pool = new NSAutoreleasePool ())
ptr = Messaging.IntPtr_objc_msgSend (obj.Handle, Selector.GetHandle ("testRetainNSObject"));
using (var rv = Runtime.GetNSObject (ptr)) {
Assert.AreEqual (2, rv.RetainCount, "nsobject");
Assert.AreSame (typeof(NSObject), rv.GetType (), "nsobject type");
rv.DangerousRelease ();
}
using (var pool = new NSAutoreleasePool ())
ptr = Messaging.IntPtr_objc_msgSend (obj.Handle, Selector.GetHandle ("testRetainString"));
using (var rv = Runtime.GetNSObject (ptr)) {
Assert.AreEqual (2, rv.RetainCount, "string");
Assert.IsTrue (rv is NSString, "string type");
rv.DangerousRelease ();
}
}
using (var obj = new DerivedRegistrar1 ()) {
using (var pool = new NSAutoreleasePool ())
ptr = Messaging.IntPtr_objc_msgSend (obj.Handle, Selector.GetHandle ("testOverriddenRetainNSObject"));
using (var rv = Runtime.GetNSObject (ptr)) {
Assert.AreEqual (2, rv.RetainCount, "overridden nsobject");
Assert.AreSame (typeof(NSObject), rv.GetType (), "overridden nsobject type");
rv.DangerousRelease ();
}
}
}
class Props : NSObject {
[Export ("myProp")]
public string MyProp {
get;
set;
}
}
[Test]
public void TestObjCProperties ()
{
var class_handle = Class.GetHandle (typeof(Props));
Assert.AreNotEqual (IntPtr.Zero, class_getProperty (class_handle, "myProp"));
}
[DllImport ("/usr/lib/libobjc.dylib")]
static extern IntPtr class_getProperty (IntPtr cls, string name);
[Test]
public void TestNonVirtualProperty ()
{
using (var obj = new DerivedRegistrar1 ()) {
Assert.IsTrue (Messaging.bool_objc_msgSend (obj.Handle, Selector.GetHandle ("b1")));
}
}
[Test]
public void TestGeneric ()
{
var g1 = new GenericTestClass<string> ();
var g2 = new GenericTestClass<object> ();
var g3 = new DerivedGenericTestClass<string> ();
var g4 = new DerivedGenericTestClass<object> ();
var sel = new Selector ("GetTypeFullName").Handle;
string t1 = NSString.FromHandle (Messaging.IntPtr_objc_msgSend (g1.Handle, sel)).ToString ();
string t2 = NSString.FromHandle (Messaging.IntPtr_objc_msgSend (g2.Handle, sel)).ToString ();
string t3 = NSString.FromHandle (Messaging.IntPtr_objc_msgSend (g3.Handle, sel)).ToString ();
string t4 = NSString.FromHandle (Messaging.IntPtr_objc_msgSend (g4.Handle, sel)).ToString ();
Assert.AreEqual (g1.GetTypeFullName (), t1, "#t1");
Assert.AreEqual (g2.GetTypeFullName (), t2, "#t2");
Assert.AreEqual (g3.GetTypeFullName (), t3, "#t3");
Assert.AreEqual (g4.GetTypeFullName (), t4, "#t4");
var openClass = Class.GetHandle ("Open_1");
var handle = Messaging.IntPtr_objc_msgSend (openClass, Selector.GetHandle ("alloc"));
try {
handle = Messaging.IntPtr_objc_msgSend (handle, Selector.GetHandle ("init"));
Assert.Fail ("Expected [[Open_1 alloc] init] to fail.");
} catch (PlatformException mex) {
Assert.AreEqual ("Cannot construct an instance of the type 'MonoTouchFixtures.ObjCRuntime.RegistrarTest+Open`1' from Objective-C because the type is generic.", mex.Message);
} finally {
Messaging.void_objc_msgSend (handle, Selector.GetHandle ("release")); // or should this be dealloc directly?
}
Assert.DoesNotThrow (() => new Open<string> (), "Create managed open instance");
// It's possible to create an instance of a closed class.
var closedClass = Class.GetHandle (typeof (Closed));
handle = Messaging.IntPtr_objc_msgSend (closedClass, Selector.GetHandle ("alloc"));
handle = Messaging.IntPtr_objc_msgSend (handle, Selector.GetHandle ("init"));
Messaging.void_objc_msgSend (handle, Selector.GetHandle ("release"));
Assert.DoesNotThrow (() => new Closed (), "Created managed closed instance");
}
[Test]
public void TestNestedGenericType ()
{
var foo = new NestedParent<NSObject>.Nested ();
var obj = new NSObject ();
Messaging.void_objc_msgSend_IntPtr (foo.Handle, Selector.GetHandle ("foo:"), obj.Handle);
obj.Dispose ();
foo.Dispose ();
}
[Test]
public void TestInstanceMethodOnOpenGenericType ()
{
{
var foo = new Open<NSSet, string> ();
var view = new NSSet ();
var expectedU = typeof(NSSet);
var expectedV = typeof(string);
Messaging.void_objc_msgSend_IntPtr (foo.Handle, Selector.GetHandle ("bar:"), IntPtr.Zero);
Assert.IsNull (foo.LastArg);
Assert.AreEqual (expectedU, foo.UType);
Assert.AreEqual (expectedV, foo.VType);
Messaging.void_objc_msgSend_IntPtr (foo.Handle, Selector.GetHandle ("bar:"), view.Handle);
Assert.AreSame (view, foo.LastArg);
Assert.AreEqual (expectedU, foo.UType);
Assert.AreEqual (expectedV, foo.VType);
var arr = NSArray.FromNSObjects (view);
Messaging.void_objc_msgSend_IntPtr (foo.Handle, Selector.GetHandle ("zap:"), IntPtr.Zero);
Assert.IsNull (foo.LastArg);
Assert.AreEqual (expectedU, foo.UType);
Assert.AreEqual (expectedV, foo.VType);
Messaging.void_objc_msgSend_IntPtr (foo.Handle, Selector.GetHandle ("zap:"), arr.Handle);
Assert.AreSame (view, ((object[])foo.LastArg) [0]);
Assert.AreEqual (expectedU, foo.UType);
Assert.AreEqual (expectedV, foo.VType);
Assert.AreEqual (IntPtr.Zero, Messaging.IntPtr_objc_msgSend (foo.Handle, Selector.GetHandle ("xyz")), "xyz");
Assert.IsNull (foo.LastArg);
Assert.AreEqual (expectedU, foo.UType);
Assert.AreEqual (expectedV, foo.VType);
Assert.AreEqual (IntPtr.Zero, Messaging.IntPtr_objc_msgSend (foo.Handle, Selector.GetHandle ("barzap")), "barzap");
Assert.IsNull (foo.LastArg);
Assert.AreEqual (expectedU, foo.UType);
Assert.AreEqual (expectedV, foo.VType);
Messaging.void_objc_msgSend_IntPtr (foo.Handle, Selector.GetHandle ("setBarzap:"), IntPtr.Zero);
Assert.IsNull (foo.LastArg);
Assert.AreEqual (expectedU, foo.UType);
Assert.AreEqual (expectedV, foo.VType);
Messaging.void_objc_msgSend_IntPtr (foo.Handle, Selector.GetHandle ("setBarzap:"), view.Handle);
Assert.AreSame (view, foo.LastArg);
Assert.AreEqual (expectedU, foo.UType);
Assert.AreEqual (expectedV, foo.VType);
arr.Dispose ();
view.Dispose ();
foo.Dispose ();
}
{
var foo = new Open<NSObject, int> ();
var view = new NSObject ();
var expectedU = typeof(NSObject);
var expectedV = typeof(int);
Messaging.void_objc_msgSend_IntPtr (foo.Handle, Selector.GetHandle ("bar:"), IntPtr.Zero);
Assert.IsNull (foo.LastArg);
Assert.AreEqual (expectedU, foo.UType);
Assert.AreEqual (expectedV, foo.VType);
Messaging.void_objc_msgSend_IntPtr (foo.Handle, Selector.GetHandle ("bar:"), view.Handle);
Assert.AreSame (view, foo.LastArg);
Assert.AreEqual (expectedU, foo.UType);
Assert.AreEqual (expectedV, foo.VType);
var arr = NSArray.FromNSObjects (view);
Messaging.void_objc_msgSend_IntPtr (foo.Handle, Selector.GetHandle ("zap:"), IntPtr.Zero);
Assert.IsNull (foo.LastArg);
Assert.AreEqual (expectedU, foo.UType);
Assert.AreEqual (expectedV, foo.VType);
Messaging.void_objc_msgSend_IntPtr (foo.Handle, Selector.GetHandle ("zap:"), arr.Handle);
Assert.AreSame (view, ((object[])foo.LastArg) [0]);
Assert.AreEqual (expectedU, foo.UType);
Assert.AreEqual (expectedV, foo.VType);
Assert.AreEqual (IntPtr.Zero, Messaging.IntPtr_objc_msgSend (foo.Handle, Selector.GetHandle ("xyz")), "xyz");
Assert.IsNull (foo.LastArg);
Assert.AreEqual (expectedU, foo.UType);
Assert.AreEqual (expectedV, foo.VType);
Assert.AreEqual (IntPtr.Zero, Messaging.IntPtr_objc_msgSend (foo.Handle, Selector.GetHandle ("barzap")), "barzap");
Assert.IsNull (foo.LastArg);
Assert.AreEqual (expectedU, foo.UType);
Assert.AreEqual (expectedV, foo.VType);
Messaging.void_objc_msgSend_IntPtr (foo.Handle, Selector.GetHandle ("setBarzap:"), IntPtr.Zero);
Assert.IsNull (foo.LastArg);
Assert.AreEqual (expectedU, foo.UType);
Assert.AreEqual (expectedV, foo.VType);
Messaging.void_objc_msgSend_IntPtr (foo.Handle, Selector.GetHandle ("setBarzap:"), view.Handle);
Assert.AreSame (view, foo.LastArg);
Assert.AreEqual (expectedU, foo.UType);
Assert.AreEqual (expectedV, foo.VType);
arr.Dispose ();
view.Dispose ();
foo.Dispose ();
}
}
#if !__WATCHOS__ && !MONOMAC
[Test]
public void TestGenericUIView ()
{
using (var iview = new NullableIntView (new RectangleF (0, 0, 100, 100))) {
using (var strview = new StringView (new RectangleF (0, 0, 100, 100))) {
Messaging.void_objc_msgSend_RectangleF (iview.Handle, Selector.GetHandle ("drawRect:"), RectangleF.Empty);
Assert.AreEqual (typeof(int?), iview.TypeT, "int?");
Assert.AreEqual ("NullableIntView", iview.TypeName, "int? typename");
Messaging.void_objc_msgSend_RectangleF (strview.Handle, Selector.GetHandle ("drawRect:"), RectangleF.Empty);
Assert.AreEqual (typeof(string), strview.TypeT, "string");
Assert.AreEqual ("StringView", strview.TypeName, "string typename");
}
}
}
#endif // !__WATCHOS__
#if !__WATCHOS__ && !MONOMAC
[Test]
public void TestNativeEnum ()
{
//public virtual void TestNativeEnum1 (UITextWritingDirection twd)
using (var obj = new RegistrarTestClass ()) {
if (IntPtr.Size == 4) {
Messaging.void_objc_msgSend_int (obj.Handle, Selector.GetHandle ("testNativeEnum1:"), (int) UITextWritingDirection.RightToLeft);
} else {
Messaging.void_objc_msgSend_long (obj.Handle, Selector.GetHandle ("testNativeEnum1:"), (long)UITextWritingDirection.RightToLeft);
}
if (IntPtr.Size == 4) {
Messaging.void_objc_msgSend_int_int_long (obj.Handle, Selector.GetHandle ("testNativeEnum1:"), (int) UITextWritingDirection.RightToLeft, 31415, 3141592);
} else {
Messaging.void_objc_msgSend_long_int_long (obj.Handle, Selector.GetHandle ("testNativeEnum1:"), (long) UITextWritingDirection.RightToLeft, 31415, 3141592);
}
if (IntPtr.Size == 4) {
Assert.AreEqual ((int) UIPopoverArrowDirection.Right, Messaging.int_objc_msgSend (obj.Handle, Selector.GetHandle ("testNativeEnum2")), "testNativeEnum2");
Messaging.void_objc_msgSend_int (obj.Handle, Selector.GetHandle ("setTestNativeEnum2:"), (int) UIPopoverArrowDirection.Left);
} else {
Assert.AreEqual ((long) UIPopoverArrowDirection.Right, Messaging.long_objc_msgSend (obj.Handle, Selector.GetHandle ("testNativeEnum2")), "testNativeEnum2");
Messaging.void_objc_msgSend_long (obj.Handle, Selector.GetHandle ("setTestNativeEnum2:"), (long) UIPopoverArrowDirection.Left);
}
}
}
#endif // !__WATCHOS__
[Test]
public void Bug23289 ()
{
using (var obj = new RegistrarTestClass ()) {
using (var arr = new NSMutableArray (1)) {
var cl = Messaging.IntPtr_objc_msgSend (Class.GetHandle (typeof (CLLocation)), Selector.GetHandle ("alloc"));
cl = Messaging.IntPtr_objc_msgSend_double_double (cl, Selector.GetHandle ("initWithLatitude:longitude:"), 1, 2);
Messaging.void_objc_msgSend_IntPtr (arr.Handle, Selector.GetHandle ("addObject:"), cl);
Messaging.bool_objc_msgSend_IntPtr (obj.Handle, Selector.GetHandle ("testBug23289:"), arr.Handle);
Messaging.void_objc_msgSend (cl, Selector.GetHandle ("release"));
}
}
}
[Test]
public void TestCGPointParameter ()
{
using (var obj = new RegistrarTestClass ()) {
var pnt1 = new PointF (123, 456);
PointF pnt2 = new PointF ();
void_objc_msgSend_CGPoint_ref_CGPoint (obj.Handle, Selector.GetHandle ("testCGPoint:out:"), pnt1, ref pnt2);
Assert.AreEqual (123, pnt2.X, "X");
Assert.AreEqual (456, pnt2.Y, "Y");
}
}
[Test]
public void ExportedGenericsTest ()
{
using (var obj = new RegistrarTestClass ()) {
var rv = Runtime.GetNSObject<NSArray<NSString>> (Messaging.IntPtr_objc_msgSend_IntPtr (obj.Handle, Selector.GetHandle ("fetchNSArrayOfNSString:"), IntPtr.Zero));
Assert.IsNotNull (rv, "method");
using (var number_array = NSArray<NSNumber>.FromNSObjects ((NSNumber) 314)) {
rv = Runtime.GetNSObject<NSArray<NSString>> (Messaging.IntPtr_objc_msgSend_IntPtr (obj.Handle, Selector.GetHandle ("fetchNSArrayOfNSString:"), number_array.Handle));
Assert.IsNotNull (rv, "method param");
}
rv = Runtime.GetNSObject<NSArray<NSString>> (Messaging.IntPtr_objc_msgSend (obj.Handle, Selector.GetHandle ("nSArrayOfNSString")));
Assert.IsNotNull (rv, "property");
Messaging.void_objc_msgSend_IntPtr (obj.Handle, Selector.GetHandle ("setNSArrayOfNSString:"), IntPtr.Zero);
Messaging.void_objc_msgSend_IntPtr (obj.Handle, Selector.GetHandle ("setNSArrayOfNSString:"), rv.Handle);
var rv2 = Runtime.GetNSObject<NSArray<NSArray<NSString>>> (Messaging.IntPtr_objc_msgSend_IntPtr (obj.Handle, Selector.GetHandle ("fetchComplexGenericType:"), IntPtr.Zero));
Assert.IsNotNull (rv2, "complex");
using (var complex = new NSArray<NSDictionary<NSString, NSArray<NSNumber>>> ()) {
Runtime.GetNSObject<NSArray<NSArray<NSString>>> (Messaging.IntPtr_objc_msgSend_IntPtr (obj.Handle, Selector.GetHandle ("fetchComplexGenericType:"), complex.Handle));
Assert.IsNotNull (rv2, "complex param");
}
}
}
const string LIBOBJC_DYLIB = "/usr/lib/libobjc.dylib";
[DllImport (LIBOBJC_DYLIB, EntryPoint="objc_msgSend")]
extern static void void_objc_msgSend_out_IntPtr (IntPtr receiver, IntPtr selector, out IntPtr value);
[DllImport (LIBOBJC_DYLIB, EntryPoint="objc_msgSend")]
extern static void void_objc_msgSend_ref_IntPtr (IntPtr receiver, IntPtr selector, ref IntPtr value);
[DllImport (LIBOBJC_DYLIB, EntryPoint="objc_msgSend")]
extern static void void_objc_msgSend_out_IntPtr_bool (IntPtr receiver, IntPtr selector, out IntPtr path, bool create);
[DllImport (LIBOBJC_DYLIB, EntryPoint="objc_msgSend")]
extern static bool bool_objc_msgSend_ref_intptr (IntPtr receiver, IntPtr selector, ref IntPtr path);
[DllImport (LIBOBJC_DYLIB, EntryPoint="objc_msgSend")]
extern static void void_objc_msgSend_SizeF_IntPtr_out_IntPtr (IntPtr receiver, IntPtr selector, SizeF size, IntPtr input, out IntPtr value);
[DllImport (LIBOBJC_DYLIB, EntryPoint="objc_msgSend")]
extern static void void_objc_msgSend_ref_BlockLiteral (IntPtr receiver, IntPtr selector, ref BlockLiteral block);
[DllImport (LIBOBJC_DYLIB, EntryPoint="objc_msgSend")]
extern static void void_objc_msgSend_CGPoint_ref_CGPoint (IntPtr receiver, IntPtr selector, PointF p1, ref PointF p2);
#region Exported type
[Register ("RegistrarTestClass")]
class RegistrarTestClass : NSObject
{
public virtual bool B1 {
[Export ("b1")]
get {
return false;
}
}
// static properties
public static int called_StaticProperty1Getter;
[Export ("StaticProperty1")]
static bool StaticProperty1 {
get {
called_StaticProperty1Getter++;
return true;
}
}
public static int called_StaticProperty2Getter;
public static int called_StaticProperty2Setter;
[Export ("StaticProperty2")]
static bool StaticProperty2 {
get {
called_StaticProperty2Getter++;
return true;
}
set {
called_StaticProperty2Setter++;
}
}
public static int called_StaticProperty3Setter;
[Export ("StaticProperty3")]
static bool StaticProperty3 {
set {
called_StaticProperty3Setter++;
}
}
public static int called_StaticProperty4Getter;
static bool StaticProperty4 {
[Export ("StaticProperty4")]
get {
called_StaticProperty4Getter++;
return true;
}
}
public static int called_StaticProperty5Getter;
public static int called_StaticProperty5Setter;
static bool StaticProperty5 {
[Export ("StaticProperty5")]
get {
called_StaticProperty5Getter++;
return true;
}
[Export ("WStaticProperty5:")] // can't use same name as getter, and don't use the default "set_" prefix either (to ensure we're not exporting the default prefix always)
set {
called_StaticProperty5Setter++;
}
}
public static int called_StaticProperty6Setter;
static bool StaticProperty6 {
[Export ("StaticProperty6:")]
set {
called_StaticProperty6Setter++;
}
}
// instance properties
public int called_Property1Getter;
[Export ("Property1")]
bool Property1 {
get {
called_Property1Getter++;
return true;
}
}
public int called_Property2Getter;
public int called_Property2Setter;
[Export ("Property2")]
bool Property2 {
get {
called_Property2Getter++;
return true;
}
set {
called_Property2Setter++;
}
}
public int called_Property3Setter;
[Export ("Property3")]
bool Property3 {
set {
called_Property3Setter++;
}
}
public int called_Property4Getter;
bool Property4 {
[Export ("Property4")]
get {
called_Property4Getter++;
return true;
}
}
public int called_Property5Getter;
public int called_Property5Setter;
bool Property5 {
[Export ("Property5")]
get {
called_Property5Getter++;
return true;
}
[Export ("WProperty5:")] // can't use same name as getter, and don't use the default "set_" prefix either (to ensure we're not exporting the default prefix always)
set {
called_Property5Setter++;
}
}
public int called_Property6Setter;
bool Property6 {
[Export ("Property6:")]
set {
called_Property6Setter++;
}
}
[Export ("INativeObject1:")]
static bool INativeObject1 (CGPath img /*CGPath is a INativeObject */)
{
return img != null;
}
[Export ("INativeObject2:")]
static CGPath INativeObject2 (bool create)
{
return create ? new CGPath () : null;
}
[Export ("INativeObject3:create:")]
static void INativeObject3 (out CGPath path, bool create)
{
path = create ? new CGPath () : null;
}
[Export ("INativeObject4:")]
static bool INativeObject4 (ref CGPath path)
{
return path != null;
}
[Export ("INativeObject5:")]
static CGPath INativeObject5 (bool create)
{
return create ? new CGPath () : null;
}
[Export ("VirtualMethod")]
public virtual string VirtualMethod ()
{
return "base";
}
[Export ("testNSAction:")]
public void TestNSAction (Action action)
{
}
[Export ("testOutNSString:")]
public void TestOutNSString (out string value)
{
value = "Santa is coming";
}
[Export ("testOutParametersWithStructs:in:out:")]
public void TestOutParameters (SizeF a, NSError @in, out NSError value)
{
// bug 16078
value = @in;
}
#if !__TVOS__ && !__WATCHOS__ && !MONOMAC
[Export ("testAction:")]
public void TestAction ([BlockProxy (typeof (NIDActionArity1V1))] Action<UIBackgroundFetchResult> action)
{
// bug ?
}
#endif // !__TVOS__ && !__WATCHOS__
[return: ReleaseAttribute ()]
[Export ("testRetainArray")]
public NSObject[] TestRetainArray ()
{
return new NSObject[] { new NSObject () };
}
[Export ("testBug23289:")]
public bool TestBug23289 (CLLocation [] array)
{
return true;
}
[return: ReleaseAttribute ()]
[Export ("testReturnINativeObject")]
public INativeObject TestRetainINativeObject ()
{
return new NSObject ();
}
[return: ReleaseAttribute ()]
[Export ("testRetainNSObject")]
public NSObject TestRetainNSObject ()
{
return new NSObject ();
}
[return: ReleaseAttribute ()]
[Export ("testRetainString")]
public string TestRetainString ()
{
return "some string that does not match a constant NSString";
}
[return: ReleaseAttribute ()]
[Export ("testOverriddenRetainNSObject")]
public virtual NSObject TestOverriddenRetainNSObject ()
{
return new NSObject ();
}
#if !__WATCHOS__ && !MONOMAC
[Export ("testNativeEnum1:")]
public virtual void TestNativeEnum1 (UITextWritingDirection twd)
{
Assert.That (Enum.GetValues (typeof (UITextWritingDirection)), Contains.Item (twd), "TestNativeEnum1");
}
public virtual UIPopoverArrowDirection TestNativeEnum2 {
[Export ("testNativeEnum2")]
get {
return UIPopoverArrowDirection.Right;
}
[Export ("setTestNativeEnum2:")]
set {
Assert.AreEqual (UIPopoverArrowDirection.Left, value, "setTestNativeEnum2:");
}
}
[Export ("testNativeEnum3:a:b:")]
public virtual void TestNativeEnum1 (UITextWritingDirection twd, int a, long b)
{
Assert.That (Enum.GetValues (typeof (UITextWritingDirection)), Contains.Item (twd), "TestNativeEnum3");
Assert.AreEqual (31415, a, "TestNativeEnum3 a");
Assert.AreEqual (3141592, b, "TestNativeEnum3 b");
}
#endif // !__WATCHOS__
[Export ("testCGPoint:out:")]
public void TestCGPoint (PointF pnt, ref PointF pnt2)