Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Fix implicit conversion from null Uri to ImageSource (#1253)
Browse files Browse the repository at this point in the history
- closes #1253
  • Loading branch information
kentcb authored and StephaneDelcroix committed May 3, 2018
1 parent b0e8a12 commit b794caf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
23 changes: 20 additions & 3 deletions Xamarin.Forms.Core.UnitTests/ImageSourceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ public void TestImplicitFileConversion ()
Assert.AreEqual ("File.png", ((FileImageSource)(image.Source)).File);
}

[Test]
public void TestImplicitStringConversionWhenNull()
{
string s = null;
var sut = (ImageSource)s;
Assert.That (sut, Is.InstanceOf<FileImageSource> ());
Assert.IsNull (((FileImageSource)sut).File);
}

[Test]
public void TestImplicitUriConversion ()
{
Expand All @@ -73,11 +82,19 @@ public void TestImplicitStringUriConversion ()
Assert.AreEqual ("http://xamarin.com/img.png", ((UriImageSource)(image.Source)).Uri.AbsoluteUri);
}

[Test]
public void TestImplicitUriConversionWhenNull()
{
Uri u = null;
var sut = (ImageSource)u;
Assert.IsNull(sut);
}

[Test]
public void TestSetStringValue ()
{
var image = new Image ();
image.SetValue (Image.SourceProperty, "foo.png");
image.SetValue (Image.SourceProperty, "foo.png");
Assert.IsNotNull (image.Source);
Assert.That (image.Source, Is.InstanceOf<FileImageSource> ());
Assert.AreEqual ("foo.png", ((FileImageSource)(image.Source)).File);
Expand All @@ -92,7 +109,7 @@ public void TextBindToStringValue ()
image.BindingContext = "foo.png";
Assert.IsNotNull (image.Source);
Assert.That (image.Source, Is.InstanceOf<FileImageSource> ());
Assert.AreEqual ("foo.png", ((FileImageSource)(image.Source)).File);
Assert.AreEqual ("foo.png", ((FileImageSource)(image.Source)).File);
}

[Test]
Expand All @@ -119,7 +136,7 @@ public void TextBindToUriValue ()
Assert.AreEqual ("http://xamarin.com/img.png", ((UriImageSource)(image.Source)).Uri.AbsoluteUri);
}

class MockImageSource : ImageSource
class MockImageSource : ImageSource
{
}

Expand Down
3 changes: 3 additions & 0 deletions Xamarin.Forms.Core/ImageSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ public static implicit operator ImageSource(string source)

public static implicit operator ImageSource(Uri uri)
{
if (uri == null)
return null;

if (!uri.IsAbsoluteUri)
throw new ArgumentException("uri is relative");
return FromUri(uri);
Expand Down

0 comments on commit b794caf

Please sign in to comment.