[iOS] Don't crash if no Thumbnail specified for AppLink (#5684)
authorSamantha Houts <samhouts@users.noreply.github.com>
Wed, 27 Mar 2019 23:41:38 +0000 (16:41 -0700)
committerGitHub <noreply@github.com>
Wed, 27 Mar 2019 23:41:38 +0000 (16:41 -0700)
* Add test for #5470

* [iOS] Don't crash if no Thumbnail specified for AppLink

fixes #5470

* Add braces

* fix test on Android

Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue5470.cs [new file with mode: 0644]
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems
Xamarin.Forms.Core.UITests.Shared/UITestCategories.cs
Xamarin.Forms.Platform.iOS/iOSAppLinks.cs

diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue5470.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue5470.cs
new file mode 100644 (file)
index 0000000..66f15ff
--- /dev/null
@@ -0,0 +1,66 @@
+using Xamarin.Forms.CustomAttributes;
+using Xamarin.Forms.Internals;
+using System;
+using System.Threading.Tasks;
+
+#if UITEST
+using Xamarin.Forms.Core.UITests;
+using Xamarin.UITest;
+using NUnit.Framework;
+#endif
+
+namespace Xamarin.Forms.Controls.Issues
+{
+#if UITEST
+       [Category(UITestCategories.AppLinks)]
+#endif
+       [Preserve(AllMembers = true)]
+       [Issue(IssueTracker.Github, 5470, "ApplinkEntry Thumbnail required after upgrading to 3.5/3.6", PlatformAffected.iOS)]
+       public class Issue5470 : TestContentPage 
+       {
+               protected override void Init()
+               {
+                       // android needs firebase for this to work, so skip it unless iOS
+                       if (Device.RuntimePlatform == Device.iOS)
+                               Application.Current.AppLinks.RegisterLink(GetEntry());
+
+                       // Initialize ui here instead of ctor
+                       Content = new Label
+                       {
+                               AutomationId = "IssuePageLabel",
+                               Text = "If this is iOS, I just tried to register an applink without a Thumbnail. If this did not crash, this test has succeeded."
+                       };
+               }
+
+               AppLinkEntry GetEntry()
+               {
+                       if (string.IsNullOrEmpty(Title))
+                               Title = "ApplinkEntry Thumbnail required after upgrading to 3.5/3.6";
+
+                       var type = GetType().ToString();
+                       var entry = new AppLinkEntry
+                       {
+                               Title = Title,
+                               Description = $"ApplinkEntry Thumbnail required after upgrading to 3.5/3.6",
+                               AppLinkUri = new Uri($"http://blah/gallery/{type}", UriKind.RelativeOrAbsolute),
+                               IsLinkActive = true,
+                               Thumbnail = null
+                       };
+
+                       entry.KeyValues.Add("contentType", "GalleryPage");
+                       entry.KeyValues.Add("appName", "blah");
+                       entry.KeyValues.Add("companyName", "Xamarin");
+
+                       return entry;
+               }
+
+#if UITEST && __IOS__
+               [Test]
+               public async void Issue5470Test() 
+               {
+                       await Task.Delay(500); // give it time to crash
+                       RunningApp.WaitForElement (q => q.Marked ("IssuePageLabel"));
+               }
+#endif
+       }
+}
\ No newline at end of file
index d19a34e..03d7d2d 100644 (file)
       <SubType>Code</SubType>
       <DependentUpon>%(Filename)</DependentUpon>
     </Compile>
+    <Compile Include="$(MSBuildThisFileDirectory)Issue5470.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)_Template.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla56298.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla42620.cs" />
     </EmbeddedResource>
     <EmbeddedResource Include="$(MSBuildThisFileDirectory)VisualControlsPage.xaml">
       <SubType>Designer</SubType>
-       <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
+      <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
     </EmbeddedResource>
     <EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue4356.xaml">
       <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
index 78cf6ab..2829aa1 100644 (file)
@@ -46,5 +46,6 @@
                public const string ManualReview = "ManualReview";
                public const string Performance = "Performance";
                public const string Visual = "Visual";
+               public const string AppLinks = "AppLinks";
        }
 }
\ No newline at end of file
index a0a0e0c..8b221d9 100644 (file)
@@ -107,12 +107,15 @@ namespace Xamarin.Forms.Platform.iOS
                                Url = new NSUrl(deepLinkUri.AppLinkUri.ToString())
                        };
 
-                       using (var uiimage = await deepLinkUri.Thumbnail.GetNativeImageAsync())
+                       if (deepLinkUri.Thumbnail != null)
                        {
-                               if (uiimage == null)
-                                       throw new InvalidOperationException("AppLinkEntry Thumbnail must be set to a valid source");
+                               using (var uiimage = await deepLinkUri.Thumbnail.GetNativeImageAsync())
+                               {
+                                       if (uiimage == null)
+                                               throw new InvalidOperationException("AppLinkEntry Thumbnail must be set to a valid source");
 
-                               searchableAttributeSet.ThumbnailData = uiimage.AsPNG();
+                                       searchableAttributeSet.ThumbnailData = uiimage.AsPNG();
+                               }
                        }
 
                        return searchableAttributeSet;