From 7845caa577769e36f9a28a28637d6e48ff45e5c8 Mon Sep 17 00:00:00 2001 From: Alex Soto Date: Fri, 23 Sep 2016 19:17:57 -0500 Subject: [PATCH] [AVFoundaation] Fix bug 44322 - AVAssetDownloadUrlSession.CreateSession throws a System.InvalidCastException: Specified cast is not valid. https://bugzilla.xamarin.com/show_bug.cgi?id=44322 AVAssetDownloadUrlSession.CreateSession according to headers it should return an AVAssetDownloadUrlSession but it is returning an apple internal type NSURLBackgroundSession so with our current bindings it throws an InvalidCastException, adding ForcedTypeAttribute will create the managed type wihout the actual typecheck. Added test verifing that the API no longer throws. --- src/avfoundation.cs | 1 + .../AVAssetDownloadUrlSessionTests.cs | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/avfoundation.cs b/src/avfoundation.cs index 780632ec1b..5351406b12 100644 --- a/src/avfoundation.cs +++ b/src/avfoundation.cs @@ -10453,6 +10453,7 @@ namespace XamCore.AVFoundation { [BaseType (typeof (NSUrlSession), Name = "AVAssetDownloadURLSession")] interface AVAssetDownloadUrlSession { [Static] + [ForcedType] [Export ("sessionWithConfiguration:assetDownloadDelegate:delegateQueue:")] AVAssetDownloadUrlSession CreateSession (NSUrlSessionConfiguration configuration, [NullAllowed] IAVAssetDownloadDelegate @delegate, [NullAllowed] NSOperationQueue delegateQueue); diff --git a/tests/monotouch-test/AVFoundation/AVAssetDownloadUrlSessionTests.cs b/tests/monotouch-test/AVFoundation/AVAssetDownloadUrlSessionTests.cs index 21ea0de680..ca9aa4c6a8 100644 --- a/tests/monotouch-test/AVFoundation/AVAssetDownloadUrlSessionTests.cs +++ b/tests/monotouch-test/AVFoundation/AVAssetDownloadUrlSessionTests.cs @@ -11,6 +11,7 @@ #if !__WATCHOS__ && !__TVOS__ using System; +using ObjCRuntime; #if XAMCORE_2_0 using Foundation; @@ -38,6 +39,20 @@ namespace monotouchtest { Assert.Throws (() => AVAssetDownloadUrlSession.FromWeakConfiguration (NSUrlSessionConfiguration.DefaultSessionConfiguration, new NSObject (), null), "FromWeakConfiguration should throw NotSupportedException"); } + [Test] + public void CreateSessionTest () + { + if (!TestRuntime.CheckXcodeVersion (7, 0)) + Assert.Ignore ("Ignoring AVAssetDownloadUrlSession tests: Requires iOS9+"); + + if (Runtime.Arch == Arch.DEVICE) + Assert.Ignore ("Ignoring CreateSessionTest tests: Requires com.apple.developer.media-asset-download entitlement"); + + using (var backgroundConfiguration = NSUrlSessionConfiguration.CreateBackgroundSessionConfiguration ("HLS-Identifier")) { + Assert.DoesNotThrow (() => AVAssetDownloadUrlSession.CreateSession (backgroundConfiguration, null, NSOperationQueue.MainQueue), "Should not throw InvalidCastException"); + } + } + // FIXME: Disabling this test from now, will reenable once apple releases docs on what is ecpected to have this entitlement key // Reason: Creating an AVAssetDownloadURLSession requires the com.apple.developer.media-asset-download entitlement // [Test]