2020-12-18 13:45:06 +03:00
|
|
|
#if __MACOS__
|
|
|
|
using System;
|
2016-05-26 16:06:52 +03:00
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
using AppKit;
|
|
|
|
using Foundation;
|
|
|
|
using ObjCRuntime;
|
|
|
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
2021-11-20 00:52:19 +03:00
|
|
|
#if !NET
|
|
|
|
using NativeHandle = System.IntPtr;
|
|
|
|
#endif
|
|
|
|
|
2016-05-26 16:06:52 +03:00
|
|
|
namespace MonoMacFixtures.AppKit
|
|
|
|
{
|
|
|
|
[TestFixture]
|
2021-03-16 17:15:30 +03:00
|
|
|
[Preserve (AllMembers = true)]
|
2016-05-26 16:06:52 +03:00
|
|
|
public class CellTest
|
|
|
|
{
|
|
|
|
[Test]
|
|
|
|
public void CopyTest ()
|
|
|
|
{
|
|
|
|
using (var cell = new CustomCell ())
|
|
|
|
Check (cell.Handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void CopyDerivedTest ()
|
|
|
|
{
|
|
|
|
using (var cell = new DerivedCell ())
|
|
|
|
Check (cell.Handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Check (IntPtr cell_handle)
|
|
|
|
{
|
|
|
|
var clone_ptr = IntPtr_objc_msgSend (cell_handle, Selector.GetHandle ("copyWithZone:"), IntPtr.Zero);
|
|
|
|
// Console.WriteLine ("Created cell 0x{0} (GCHandle: 0x{2}) with clone 0x{1} (GCHandle: 0x{3})", cell_handle.ToString ("x"), clone_ptr.ToString ("x"), GetGCHandle (cell_handle).ToString ("x"), GetGCHandle (clone_ptr).ToString ("x"));
|
|
|
|
|
|
|
|
Assert.AreNotEqual (GetGCHandle (cell_handle), GetGCHandle (clone_ptr), "gchandle #1");
|
|
|
|
CustomCell.expectedHandle = cell_handle;
|
|
|
|
objc_msgSend (Class.GetHandle (typeof (CustomCell)), Selector.GetHandle ("foo:"), cell_handle);
|
|
|
|
|
|
|
|
Assert.AreNotEqual (GetGCHandle (cell_handle), GetGCHandle (clone_ptr), "gchandle #2");
|
|
|
|
CustomCell.expectedHandle = clone_ptr;
|
|
|
|
objc_msgSend (Class.GetHandle (typeof (CustomCell)), Selector.GetHandle ("foo:"), clone_ptr);
|
|
|
|
|
|
|
|
Assert.AreNotEqual (GetGCHandle (cell_handle), GetGCHandle (clone_ptr), "gchandle #3");
|
|
|
|
|
|
|
|
objc_msgSend (clone_ptr, Selector.GetHandle ("release"));
|
|
|
|
}
|
|
|
|
|
|
|
|
[DllImport ("__Internal", EntryPoint = "xamarin_get_gchandle")]
|
|
|
|
extern static int GetGCHandle (IntPtr ptr);
|
|
|
|
|
|
|
|
const string LIBOBJC_DYLIB = "/usr/lib/libobjc.dylib";
|
|
|
|
|
|
|
|
[DllImport (LIBOBJC_DYLIB, EntryPoint="objc_msgSend")]
|
|
|
|
public extern static IntPtr IntPtr_objc_msgSend (IntPtr receiver, IntPtr selector, IntPtr p1);
|
|
|
|
|
|
|
|
[DllImport (LIBOBJC_DYLIB, EntryPoint="objc_msgSend")]
|
|
|
|
public extern static void objc_msgSend (IntPtr receiver, IntPtr selector, IntPtr p1);
|
|
|
|
|
|
|
|
[DllImport (LIBOBJC_DYLIB, EntryPoint="objc_msgSend")]
|
|
|
|
public extern static void objc_msgSend (IntPtr receiver, IntPtr selector);
|
|
|
|
|
|
|
|
[DllImport (LIBOBJC_DYLIB)]
|
|
|
|
internal extern static IntPtr object_getInstanceVariable (IntPtr cls, string name, out IntPtr value);
|
|
|
|
}
|
|
|
|
|
|
|
|
class CustomCell : NSCell {
|
2021-11-20 00:52:19 +03:00
|
|
|
public static NativeHandle expectedHandle;
|
2016-05-26 16:06:52 +03:00
|
|
|
|
2021-11-20 00:52:19 +03:00
|
|
|
public CustomCell (NativeHandle ptr) : base (ptr) { }
|
2016-05-26 16:06:52 +03:00
|
|
|
public CustomCell () { }
|
|
|
|
|
|
|
|
[Export ("foo:")]
|
|
|
|
public static void Foo (CustomCell mySelf)
|
|
|
|
{
|
|
|
|
Assert.AreEqual (expectedHandle, mySelf.Handle, "Handle");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class DerivedCell : CustomCell
|
|
|
|
{
|
2021-11-20 00:52:19 +03:00
|
|
|
public DerivedCell (NativeHandle ptr) : base (ptr) { }
|
2016-05-26 16:06:52 +03:00
|
|
|
public DerivedCell () { }
|
|
|
|
|
|
|
|
public override NSObject Copy (NSZone zone)
|
|
|
|
{
|
|
|
|
return base.Copy (zone);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-18 13:45:06 +03:00
|
|
|
#endif // __MACOS__
|