[SBApplication][Bug] SBApplication.FromBundleIdentifier<T> should return null when bundle ID is unknown (#9620)

* update SB bindings, add unit tests
This commit is contained in:
Whitney Schmidt 2020-09-18 11:18:26 -04:00 коммит произвёл GitHub
Родитель 1463967d00
Коммит f8eca9fb53
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 129 добавлений и 23 удалений

Просмотреть файл

@ -7,23 +7,30 @@ using ObjCRuntime;
namespace ScriptingBridge {
public partial class SBApplication
{
// We want to instance up a version of your derived class, not SBApplication
public static T FromBundleIdentifier<T> (string ident) where T : SBApplication, new()
{
using (var u = FromBundleIdentifier (ident))
return (T)System.Activator.CreateInstance (typeof(T), BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, new object [] { u.Handle }, null);
}
#if XAMCORE_4_0
public static SBApplication GetApplication (string ident) => Runtime.GetNSObject<SBApplication> (_FromBundleIdentifier (ident));
public static T FromURL<T> (NSUrl url) where T : SBApplication
{
using (var u = FromURL (url))
return (T)System.Activator.CreateInstance (typeof(T), BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, new object [] { u.Handle }, null);
}
public static T GetApplication<T> (string ident) where T : SBApplication => Runtime.GetINativeObject<T> (_FromBundleIdentifier (ident), forced_type: true, owns: false);
public static T FromProcessIdentifier<T> (int /* pid_t = int */ pid) where T : SBApplication
{
using (var u = FromProcessIdentifier (pid))
return (T)System.Activator.CreateInstance (typeof(T), BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, new object [] { u.Handle }, null);
}
public static SBApplication GetApplication (NSUrl url) => Runtime.GetNSObject<SBApplication> (_FromURL (url) ;
public static T GetApplication<T> (NSUrl url) where T : SBApplication => Runtime.GetINativeObject<T> (_FromURL (url), forced_type: true, owns: false);
public static SBApplication GetApplication (int pid) => Runtime.GetNSObject<SBApplication> (_FromProcessIdentifier (pid));
public static T GetApplication<T> (int pid) where T : SBApplication => Runtime.GetINativeObject<T> (_FromProcessIdentifier (pid), forced_type: true, owns: false);
#else
public static SBApplication FromBundleIdentifier (string ident) => Runtime.GetNSObject<SBApplication> (_FromBundleIdentifier (ident));
public static T FromBundleIdentifier<T> (string ident) where T : SBApplication => Runtime.GetINativeObject<T> (_FromBundleIdentifier (ident), forced_type: true, owns: false);
public static SBApplication FromURL (NSUrl url) => Runtime.GetNSObject<SBApplication> (_FromURL (url));
public static T FromURL<T> (NSUrl url) where T : SBApplication => Runtime.GetINativeObject<T> (_FromURL (url), forced_type: true, owns: false);
public static SBApplication FromProcessIdentifier (int pid) => Runtime.GetNSObject<SBApplication> (_FromProcessIdentifier (pid));
public static T FromProcessIdentifier<T> (int pid) where T : SBApplication => Runtime.GetINativeObject<T> (_FromProcessIdentifier (pid), forced_type: true, owns: false);
#endif
}
}
}

Просмотреть файл

@ -112,17 +112,20 @@ namespace ScriptingBridge {
[Export ("initWithBundleIdentifier:")]
IntPtr Constructor (string ident);
[Internal]
[Static]
[Export ("applicationWithBundleIdentifier:")]
SBApplication FromBundleIdentifier (string ident );
IntPtr _FromBundleIdentifier (string ident );
[Internal]
[Static]
[Export ("applicationWithURL:")]
SBApplication FromURL (NSUrl url );
IntPtr _FromURL (NSUrl url );
[Internal]
[Static]
[Export ("applicationWithProcessIdentifier:")]
SBApplication FromProcessIdentifier (int /* pid_t = int */ pid );
IntPtr _FromProcessIdentifier (int /* pid_t = int */ pid );
[Export ("classForScriptingClass:")]
Class ClassForScripting (string className );

Просмотреть файл

@ -58,6 +58,7 @@
</ItemGroup>
<ItemGroup>
<Folder Include="Resources\" />
<Folder Include="src\ScriptingBridge\" />
</ItemGroup>
<ItemGroup>
<Compile Include="src\AppKit\NSAppearance.cs" />
@ -160,6 +161,7 @@
<Compile Include="..\common\mac\TestRuntime.macos.cs">
<Link>TestRuntime.macos.cs</Link>
</Compile>
<Compile Include="src\ScriptingBridge\SBApplicationTest.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\external\Touch.Unit\Touch.Client\macOS\mobile\Touch.Client-macOS-mobile.csproj">

Просмотреть файл

@ -0,0 +1,97 @@
using NUnit.Framework;
using System;
using AppKit;
using ObjCRuntime;
using Foundation;
using ScriptingBridge;
namespace Xamarin.Mac.Tests {
public class MySBApp : SBApplication {
public MySBApp () : base (NSObjectFlag.Empty) { }
public MySBApp (NSCoder coder) : base (coder) { }
public MySBApp (NSUrl url) : base (url) { }
public MySBApp (int pid) : base (pid) { }
public MySBApp (string ident) : base (ident) { }
protected MySBApp (NSObjectFlag t) : base (t) { }
protected internal MySBApp (IntPtr handle) : base (handle) { }
}
[TestFixture]
public class SBApplicationTest {
[Test]
public void TestGetApplicationFromBundleIdentifier ()
{
const string knownBundle = "com.apple.finder";
const string unknownBundle = "com.unknown.bundle";
#if !XAMCORE_4_0
using (var app1 = SBApplication.FromBundleIdentifier (knownBundle))
using (var app2 = SBApplication.FromBundleIdentifier<MySBApp> (knownBundle))
using (var app3 = SBApplication.FromBundleIdentifier (unknownBundle))
using (var app4 = SBApplication.FromBundleIdentifier<MySBApp> (unknownBundle))
#else
var (app1 = SBApplication.GetApplication (knownBundle))
var (app2 = SBApplication.GetApplication<MySbApp> (knownBundle))
var (app3 = SBApplication.GetApplication (unknownBundle))
var (app4 = SBApplication.GetApplication<MySbApp> (unknownBundle))
#endif
{
Assert.IsNotNull (app1, "SBApplication from known bundle is null");
Assert.IsNotNull (app2, "MySBApp from known bundle is null");
Assert.IsNull (app3, "SBApplication from unknown bundle is non-null");
Assert.IsNull (app4, "MySBApp from unknown bundle is non-null");
}
}
[Test]
public void TestGetApplicationFromUrl ()
{
using (NSUrl knownUrl = new NSUrl ("http://www.xamarin.com"))
#if !XAMCORE_4_0
using (var app1 = SBApplication.FromURL (knownUrl))
using (var app2 = SBApplication.FromURL<MySBApp> (knownUrl))
#else
using (var app1 = SBApplication.GetApplication (knownUrl))
using (var app2 = SBApplication.GetApplication<MySbApp> (knownUrl))
#endif
{
Assert.IsNotNull (app1, "SBApplication from known URL is null");
Assert.IsNotNull (app2, "MySBApp from known URL is null");
}
}
[Test]
public void TestGetApplicationFromPid ()
{
int knownPid = System.Diagnostics.Process.GetCurrentProcess ().Id;
int unknownPid = -1; // valid pid is > 0
#if !XAMCORE_4_0
using (var app1 = SBApplication.FromProcessIdentifier (knownPid))
using (var app2 = SBApplication.FromProcessIdentifier<MySBApp> (knownPid))
using (var app3 = SBApplication.FromProcessIdentifier (unknownPid))
using (var app4 = SBApplication.FromProcessIdentifier<MySBApp> (unknownPid))
#else
using (var app1 = SBApplication.GetApplication (knownPid))
using (var app2 = SBApplication.GetApplication<MySbApp> (knownPid))
using (var app3 = SBApplication.GetApplication (unknownPid))
using (var app4 = SBApplication.GetApplication<MySbApp> (unknownPid)
#endif
{
Assert.IsNotNull (app1, "SBApplication from known pid is null");
Assert.IsNotNull (app2, "MySBApp from known pid is null");
Assert.IsNotNull (app3, "SBApplication from unknown pid is null");
Assert.IsNotNull (app4, "MySBApp from unknown pid is null");
}
}
}
}

Просмотреть файл

@ -5,6 +5,3 @@
!missing-null-allowed! 'Foundation.NSObject ScriptingBridge.SBObject::get_Get()' is missing an [NullAllowed] on return type
!missing-null-allowed! 'Foundation.NSObject[] ScriptingBridge.SBElementArray::Get()' is missing an [NullAllowed] on return type
!missing-null-allowed! 'ObjCRuntime.Class ScriptingBridge.SBApplication::ClassForScripting(System.String)' is missing an [NullAllowed] on return type
!missing-null-allowed! 'ScriptingBridge.SBApplication ScriptingBridge.SBApplication::FromBundleIdentifier(System.String)' is missing an [NullAllowed] on return type
!missing-null-allowed! 'ScriptingBridge.SBApplication ScriptingBridge.SBApplication::FromProcessIdentifier(System.Int32)' is missing an [NullAllowed] on return type
!missing-null-allowed! 'ScriptingBridge.SBApplication ScriptingBridge.SBApplication::FromURL(Foundation.NSUrl)' is missing an [NullAllowed] on return type