xamarin-macios/tests/introspection/ApiClassPtrTest.cs

119 строки
3.0 KiB
C#
Исходник Обычный вид История

//
// Test fixture for class_ptr introspection tests
//
// Authors:
// Alex Soto <alex.soto@xamarin.com>
//
// Copyright 2012-2014, 2016 Xamarin Inc.
//
using System;
using System.Reflection;
using System.Linq;
using NUnit.Framework;
using Xamarin.Utils;
using System.Runtime.CompilerServices;
using Foundation;
using ObjCRuntime;
#if !NET
using NativeHandle = System.IntPtr;
#endif
namespace Introspection {
public abstract class ApiClassPtrTest : ApiBaseTest {
protected virtual bool Skip (Type type)
{
// skip delegate (and other protocol references)
foreach (object ca in type.GetCustomAttributes (false)) {
if (ca is ProtocolAttribute)
return true;
if (ca is ModelAttribute)
return true;
}
[mtouch] Add `force-rejected-types-removal` optimization (#8009) This optimization can be enabled when it's not possible to use the managed linker (e.g. **Don't link**) or when the managed linker cannot remove references to deprecated types that would cause an application to be rejected by Apple. References to the existing types will be renamed, e.g. `UIWebView` to `DeprecatedWebView`, in every assemblies. The type definition is also renamed (for validity) and all custom attributes on the types and their members will be removed. Code inside the members will be replaced with a `throw new NotSupportedException ();`. The msbuild test app `MyReleaseBuild` has been updated to test that the optimization is working as expected (device builds are slow so reusing this test has little impact in test time). Basically the test ensure that `UIWebView` is used and cannot be removed by the compiler (optimization) or the managed linker (since it's referenced). Since the optimization is enabled then we can `grep` then final `.app` directory to ensure there's no mention of `UIWebView` inside any of the files that would be submitted. The application can be run, by itself, and will turn green if OK, red if `DeprecatedWebView` can't be found (skeleton replacement for `UIWebView`) or orange if a `NotSupportedException` is thrown. Finally introspection tests have been updated to skip over the deprecated (and renamed) types. It should not be an issue right now, since this optimization is not enabled by default, but it made testing easier.
2020-03-02 17:20:29 +03:00
// skip types that we renamed / rewrite since they won't behave correctly (by design)
if (SkipDueToRejectedTypes (type))
return true;
return SkipDueToAttribute (type);
}
Type GetExtendedType (Type extensionType)
{
var method =
(from m in extensionType.GetMethods (BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)
where m.IsDefined (typeof (ExtensionAttribute), false)
select m).FirstOrDefault ();
if (method is not null) {
var paramType = method.GetParameters () [0].ParameterType;
if (paramType.Name == "String")
return typeof (NSString);
else
return paramType;
} else
return null;
}
IntPtr GetClassPtrFromRegister (Type t)
{
var attribs = t.GetCustomAttributes (typeof (RegisterAttribute), true);
if (attribs.Length > 0) {
var register = ((RegisterAttribute) attribs [0]);
return Class.GetHandle (register.Name);
}
return IntPtr.Zero;
}
[Test]
public void VerifyClassPtr ()
{
foreach (Type t in Assembly.GetTypes ()) {
if (t.IsNested || !NSObjectType.IsAssignableFrom (t))
continue;
if (t.ContainsGenericParameters)
continue;
if (Skip (t))
continue;
FieldInfo fi = t.GetField ("class_ptr", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
if (fi is null)
continue;
IntPtr class_ptr = (IntPtr) (NativeHandle) fi.GetValue (null);
IntPtr register_class_ptr = GetClassPtrFromRegister (t);
Assert.AreEqual (class_ptr, register_class_ptr, "class_ptr and RegisterAttribute are different: " + t.Name);
}
}
[Test]
public void VerifyClassPtrCategories ()
{
foreach (Type t in Assembly.GetTypes ().Where (t => t.IsClass && t.IsSealed && t.IsAbstract)) {
if (Skip (t))
continue;
FieldInfo fi = t.GetField ("class_ptr", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
if (fi is null)
continue;
IntPtr class_ptr = (IntPtr) (NativeHandle) fi.GetValue (null);
var extendedType = GetExtendedType (t);
IntPtr extended_class_ptr;
if (extendedType is null)
extended_class_ptr = IntPtr.Zero;
else
extended_class_ptr = GetClassPtrFromRegister (extendedType);
Assert.AreEqual (class_ptr, extended_class_ptr, "class_ptr and RegisterAttribute from extended class are different: " + t.Name);
}
}
}
}