Skip to content

Commit

Permalink
[PassKit] Finish implementation of PKPayLaterValidateAmount. Fixes #1…
Browse files Browse the repository at this point in the history
  • Loading branch information
rolfbjarne authored Oct 1, 2024
1 parent 3db59cc commit c4a5627
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 88 deletions.
129 changes: 68 additions & 61 deletions src/PassKit/PKPayLaterView.cs
Original file line number Diff line number Diff line change
@@ -1,72 +1,79 @@
// Can be uncommented when this issue is resolved: # https://github.com/xamarin/xamarin-macios/issues/19271
#nullable enable

// #nullable enable
#if IOS && !__MACCATALYST__

// #if IOS && !__MACCATALYST__
using System;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

// using System;
// using System.Runtime.InteropServices;
// using System.Runtime.CompilerServices;
// using ObjCRuntime;
// using Foundation;
// using PassKit;
using Foundation;
using ObjCRuntime;
using PassKit;

// #if !NET
// using NativeHandle = System.IntPtr;
// #endif
namespace PassKit {

// namespace PassKit {
/// <summary>The delegate that is called when <see cref="PKPayLaterView.ValidateAmount(System.Decimal,System.String,PassKit.PKPayLaterViewValidateAmountCallback)" /> has determined whether the Pay Later Merchandising information is valid.</summary>
/// <param name="eligible">True if the Pay Later Merchandising information is valid, false otherwise.</param>
public delegate void PKPayLaterViewValidateAmountCallback (bool eligible);

// public partial class PKPayLaterView {
public partial class PKPayLaterView {
[UnmanagedCallersOnly]
static void TrampolineValidateAmount (IntPtr block, byte eligible)
{
var del = BlockLiteral.GetTarget<PKPayLaterViewValidateAmountCallback> (block);
if (del is not null) {
del (eligible != 0);
}
}

// #if !NET
// delegate void PKPayLaterValidateAmountCompletionHandler (IntPtr block, byte eligible);
// static PKPayLaterValidateAmountCompletionHandler static_ValidateAmount = TrampolineValidateAmount;
/// <summary>Checks whether the Pay Later Merchandising information is valid for the specified amount and currency.</summary>
/// <param name="amount">The amount to check for.</param>
/// <param name="currencyCode">The ISO 4217 currency code to use.</param>
/// <param name="callback">The delegate that will be called with the result.</param>
[SupportedOSPlatform ("ios17.0")]
[UnsupportedOSPlatform ("maccatalyst")]
[UnsupportedOSPlatform ("macos")]
[UnsupportedOSPlatform ("tvos")]
[BindingImpl (BindingImplOptions.Optimizable)]
public static void ValidateAmount (NSDecimalNumber amount, string currencyCode, PKPayLaterViewValidateAmountCallback callback)
{
if (callback is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (callback));

// [MonoPInvokeCallback (typeof (PKPayLaterValidateAmountCompletionHandler))]
// #else
// [UnmanagedCallersOnly]
// #endif
// static void TrampolineValidateAmount (IntPtr block, byte eligible)
// {
// var del = BlockLiteral.GetTarget<Action<bool>> (block);
// if (del is not null) {
// del (eligible != 0);
// }
// }
unsafe {
delegate* unmanaged<IntPtr, byte, void> trampoline = &TrampolineValidateAmount;
using var block = new BlockLiteral (trampoline, callback, typeof (PKPayLaterView), nameof (TrampolineValidateAmount));
var nsCurrencyCodePtr = NSString.CreateNative (currencyCode);
try {
PKPayLaterValidateAmount (amount.Handle, nsCurrencyCodePtr, &block);
} finally {
NSString.ReleaseNative (nsCurrencyCodePtr);
}
}
}

// #if NET
// [SupportedOSPlatform ("ios17.0")]
// [UnsupportedOSPlatform ("maccatalyst")]
// [UnsupportedOSPlatform ("macos")]
// [UnsupportedOSPlatform ("tvos")]
// #endif
// [BindingImpl (BindingImplOptions.Optimizable)]
// public static void ValidateAmount (NSDecimalNumber amount, string currencyCode, Action<bool> callback)
// {
// if (callback is null)
// ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (callback));
/// <summary>Checks whether the Pay Later Merchandising information is valid for the specified amount and currency.</summary>
/// <param name="amount">The amount to check for.</param>
/// <param name="currencyCode">The ISO 4217 currency code to use.</param>
/// <param name="callback">The delegate that will be called with the result.</param>
[SupportedOSPlatform ("ios17.0")]
[UnsupportedOSPlatform ("maccatalyst")]
[UnsupportedOSPlatform ("macos")]
[UnsupportedOSPlatform ("tvos")]
[BindingImpl (BindingImplOptions.Optimizable)]
public static void ValidateAmount (decimal amount, string currencyCode, PKPayLaterViewValidateAmountCallback callback)
{
using var decimalAmount = new NSDecimalNumber ((NSDecimal) amount);
ValidateAmount (decimalAmount, currencyCode, callback);
}

// unsafe {
// #if NET
// delegate* unmanaged<IntPtr, byte, void> trampoline = &TrampolineValidateAmount;
// using var block = new BlockLiteral (trampoline, callback, typeof (PKPayLaterView), nameof (TrampolineValidateAmount));
// #else
// using var block = new BlockLiteral ();
// block.SetupBlockUnsafe (static_ValidateAmount, callback);
// #endif
// var nsCurrencyCodePtr = NSString.CreateNative (currencyCode);
// try {
// PKPayLaterValidateAmount (amount.Handle, nsCurrencyCodePtr, &block);
// } finally {
// NSString.ReleaseNative (nsCurrencyCodePtr);
// }
// }
// }
[SupportedOSPlatform ("ios17.0")]
[UnsupportedOSPlatform ("maccatalyst")]
[UnsupportedOSPlatform ("macos")]
[UnsupportedOSPlatform ("tvos")]
[DllImport (Constants.PassKitLibrary)]
unsafe static extern void PKPayLaterValidateAmount (IntPtr /* NSDecimalNumber */ amount, IntPtr /* NSString */ currencyCode, BlockLiteral* callback);
}
}

// [DllImport (Constants.PassKitLibrary)]
// unsafe static extern void PKPayLaterValidateAmount (IntPtr /* NSDecimalNumber */ amount, IntPtr /* NSString */ currencyCode, BlockLiteral* callback);
// }
// }

// #endif
#endif
72 changes: 48 additions & 24 deletions tests/monotouch-test/PassKit/PKPayLaterViewTest.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,55 @@
// Can be uncommented when this issue is resolved: # https://github.com/xamarin/xamarin-macios/issues/19271
#if __IOS__ && !__MACCATALYST__

// #if __IOS__ && !__MACCATALYST__
using System;
using System.Threading;

// using System;
// using Foundation;
// using UIKit;
// using PassKit;
// using NUnit.Framework;
using Foundation;
using UIKit;

// namespace MonoTouchFixtures.PassKit {
using PassKit;

// [TestFixture]
// [Preserve (AllMembers = true)]
// public class PKPayLaterViewTest {
using NUnit.Framework;

// [Test]
// public void ValidateAmountTest ()
// {
// TestRuntime.AssertXcodeVersion (15, 0);
namespace MonoTouchFixtures.PassKit {

// for (int i = 0; i < 1000; i++){
// PKPayLaterView.ValidateAmount (new NSDecimalNumber (i), "USD", (eligible) => {
// Assert.False (eligible);
// });
// }
// }
// }
// }
[TestFixture]
[Preserve (AllMembers = true)]
public class PKPayLaterViewTest {

// #endif
[Test]
public void ValidateAmountTest_NSDecimal ()
{
TestRuntime.AssertXcodeVersion (15, 0);

var counter = 100;
var cnt = 0;
for (int i = 0; i < counter; i++) {
PKPayLaterView.ValidateAmount (new NSDecimalNumber (i), "USD", (eligible) => {
Interlocked.Increment (ref cnt);
});
}
// The callback is rarely called, so just assert that we don't get more callbacks than
// actual validation requests.
Assert.That (cnt, Is.Not.LessThan (0).And.Not.GreaterThan (counter), $"NSDecimalNumber overload");
}

[Test]
public void ValidateAmountTest_Decimal ()
{
TestRuntime.AssertXcodeVersion (15, 0);

var counter = 100;
var cnt = 0;
for (int i = 0; i < counter; i++) {
PKPayLaterView.ValidateAmount (i, "USD", (eligible) => {
Interlocked.Increment (ref cnt);
});
}
// The callback is rarely called, so just assert that we don't get more callbacks than
// actual validation requests.
Assert.That (cnt, Is.Not.LessThan (0).And.Not.GreaterThan (counter), $"decimal overload");
}
}
}

#endif
3 changes: 0 additions & 3 deletions tests/xtro-sharpie/api-annotations-dotnet/iOS-PassKit.ignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,3 @@

# needed to allow our WeakDelegate wrap
!extra-null-allowed! 'System.Void PassKit.PKPayLaterView::set_WeakDelegate(Foundation.NSObject)' has a extraneous [NullAllowed] on parameter #0

# https://github.com/xamarin/xamarin-macios/issues/19271
!missing-pinvoke! PKPayLaterValidateAmount is not bound

5 comments on commit c4a5627

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📚 [CI Build] Artifacts 📚

Artifacts were not provided.

Pipeline on Agent
Hash: [CI build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💻 [CI Build] Windows Integration Tests passed 💻

All Windows Integration Tests passed.

Pipeline on Agent
Hash: c4a5627f03e0555ed35395ac2e4207432c9ceb87 [CI build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💻 [CI Build] Windows Integration Tests passed 💻

All Windows Integration Tests passed.

Pipeline on Agent
Hash: c4a5627f03e0555ed35395ac2e4207432c9ceb87 [CI build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 [CI Build] Test results 🔥

Test results

❌ Tests failed on VSTS: test results

2 tests crashed, 0 tests failed, 91 tests passed.

Failures

❌ msbuild tests

🔥 Failed catastrophically on VSTS: test results - msbuild (no summary found).

Html Report (VSDrops) Download

❌ xcframework tests

🔥 Failed catastrophically on VSTS: test results - xcframework (no summary found).

Html Report (VSDrops) Download

Successes

✅ cecil: All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (iOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (MacCatalyst): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (macOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (Multiple platforms): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (tvOS): All 1 tests passed. Html Report (VSDrops) Download
✅ framework: All 2 tests passed. Html Report (VSDrops) Download
✅ fsharp: All 4 tests passed. Html Report (VSDrops) Download
✅ generator: All 1 tests passed. Html Report (VSDrops) Download
✅ interdependent-binding-projects: All 4 tests passed. Html Report (VSDrops) Download
✅ introspection: All 4 tests passed. Html Report (VSDrops) Download
✅ linker: All 40 tests passed. Html Report (VSDrops) Download
✅ monotouch (iOS): All 7 tests passed. Html Report (VSDrops) Download
✅ monotouch (MacCatalyst): All 7 tests passed. Html Report (VSDrops) Download
✅ monotouch (macOS): All 8 tests passed. Html Report (VSDrops) Download
✅ monotouch (tvOS): All 7 tests passed. Html Report (VSDrops) Download
✅ xtro: All 1 tests passed. Html Report (VSDrops) Download

Pipeline on Agent
Hash: c4a5627f03e0555ed35395ac2e4207432c9ceb87 [CI build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 [CI Build] Test results 🔥

Test results

❌ Tests failed on VSTS: test results

9 tests crashed, 0 tests failed, 21 tests passed.

Failures

❌ dotnettests tests (MacCatalyst)

🔥 Failed catastrophically on VSTS: test results - dotnettests_maccatalyst (no summary found).

Html Report (VSDrops) Download

❌ dotnettests tests (tvOS)

🔥 Failed catastrophically on VSTS: test results - dotnettests_tvos (no summary found).

Html Report (VSDrops) Download

❌ generator tests

🔥 Failed catastrophically on VSTS: test results - generator (no summary found).

Html Report (VSDrops) Download

❌ interdependent-binding-projects tests

🔥 Failed catastrophically on VSTS: test results - interdependent-binding-projects (no summary found).

Html Report (VSDrops) Download

❌ linker tests

🔥 Failed catastrophically on VSTS: test results - linker (no summary found).

Html Report (VSDrops) Download

❌ monotouch tests (iOS)

🔥 Failed catastrophically on VSTS: test results - monotouch_ios (no summary found).

Html Report (VSDrops) Download

❌ monotouch tests (MacCatalyst)

🔥 Failed catastrophically on VSTS: test results - monotouch_maccatalyst (no summary found).

Html Report (VSDrops) Download

❌ monotouch tests (macOS)

🔥 Failed catastrophically on VSTS: test results - monotouch_macos (no summary found).

Html Report (VSDrops) Download

❌ monotouch tests (tvOS)

🔥 Failed catastrophically on VSTS: test results - monotouch_tvos (no summary found).

Html Report (VSDrops) Download

Successes

✅ cecil: All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (iOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (macOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (Multiple platforms): All 1 tests passed. Html Report (VSDrops) Download
✅ framework: All 2 tests passed. Html Report (VSDrops) Download
✅ fsharp: All 4 tests passed. Html Report (VSDrops) Download
✅ introspection: All 4 tests passed. Html Report (VSDrops) Download
✅ msbuild: All 2 tests passed. Html Report (VSDrops) Download
✅ xcframework: All 4 tests passed. Html Report (VSDrops) Download
✅ xtro: All 1 tests passed. Html Report (VSDrops) Download

Pipeline on Agent
Hash: c4a5627f03e0555ed35395ac2e4207432c9ceb87 [CI build]

Please sign in to comment.