summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core.UnitTests/ImageSourceTests.cs
blob: 95aa513134c948c0828bb93e3ee34106b96cdb0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.Collections.Generic;
using System.Linq;

using NUnit.Framework;
using System.IO;
using System.Threading.Tasks;
using System.Threading;

namespace Xamarin.Forms.Core.UnitTests
{
	[TestFixture]
	public class ImageSourceTests : BaseTestFixture
	{
		[SetUp]
		public override void Setup ()
		{
			base.Setup ();
			Device.PlatformServices = new MockPlatformServices ();
		}

		[Test]
		public void TestConstructors ()
		{
			var filesource = new FileImageSource { File = "File.png" };
			Assert.AreEqual ("File.png", filesource.File);

			Func<CancellationToken, Task<Stream>> stream = token => new Task<Stream> (() => new FileStream ("Foo", System.IO.FileMode.Open), token);
			var streamsource = new StreamImageSource { Stream = stream };
			Assert.AreEqual (stream, streamsource.Stream);
		}

		[Test]
		public void TestHelpers ()
		{
			var imagesource = ImageSource.FromFile ("File.png");
			Assert.That (imagesource, Is.TypeOf<FileImageSource> ());
			Assert.AreEqual ("File.png", ((FileImageSource)imagesource).File);

			Func<Stream> stream = () => new System.IO.FileStream ("Foo", System.IO.FileMode.Open);
			var streamsource = ImageSource.FromStream (stream);
			Assert.That (streamsource, Is.TypeOf<StreamImageSource> ());

			var urisource = ImageSource.FromUri (new Uri ("http://xamarin.com/img.png"));
			Assert.That (urisource, Is.TypeOf<UriImageSource> ());
			Assert.AreEqual ("http://xamarin.com/img.png", ((UriImageSource)(urisource)).Uri.AbsoluteUri);
		}

		[Test]
		public void TestImplicitFileConversion ()
		{
			var image = new Image { Source = "File.png" };
			Assert.IsTrue (image.Source != null);
			Assert.That (image.Source, Is.InstanceOf<FileImageSource> ());
			Assert.AreEqual ("File.png", ((FileImageSource)(image.Source)).File);
		}

		[Test]
		public void TestImplicitUriConversion ()
		{
			var image = new Image { Source = new Uri ("http://xamarin.com/img.png") };
			Assert.IsTrue (image.Source != null);
			Assert.That (image.Source, Is.InstanceOf<UriImageSource> ());
			Assert.AreEqual ("http://xamarin.com/img.png", ((UriImageSource)(image.Source)).Uri.AbsoluteUri);
		}

		[Test]
		public void TestImplicitStringUriConversion ()
		{
			var image = new Image { Source = "http://xamarin.com/img.png" };
			Assert.IsTrue (image.Source != null);
			Assert.That (image.Source, Is.InstanceOf<UriImageSource> ());
			Assert.AreEqual ("http://xamarin.com/img.png", ((UriImageSource)(image.Source)).Uri.AbsoluteUri);
		}

		[Test]
		public void TestSetStringValue ()
		{
			var image = new Image ();
			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);
		}

		[Test]
		public void TextBindToStringValue ()
		{
			var image = new Image ();
			image.SetBinding (Image.SourceProperty, ".");
			Assert.IsNull (image.Source);
			image.BindingContext = "foo.png";
			Assert.IsNotNull (image.Source);
			Assert.That (image.Source, Is.InstanceOf<FileImageSource> ());
			Assert.AreEqual ("foo.png", ((FileImageSource)(image.Source)).File);		
		}

		[Test]
		public void TextBindToStringUriValue ()
		{
			var image = new Image ();
			image.SetBinding (Image.SourceProperty, ".");
			Assert.IsNull (image.Source);
			image.BindingContext = "http://xamarin.com/img.png";
			Assert.IsNotNull (image.Source);
			Assert.That (image.Source, Is.InstanceOf<UriImageSource> ());
			Assert.AreEqual ("http://xamarin.com/img.png", ((UriImageSource)(image.Source)).Uri.AbsoluteUri);
		}

		[Test]
		public void TextBindToUriValue ()
		{
			var image = new Image ();
			image.SetBinding (Image.SourceProperty, ".");
			Assert.IsNull (image.Source);
			image.BindingContext = new Uri("http://xamarin.com/img.png");
			Assert.IsNotNull (image.Source);
			Assert.That (image.Source, Is.InstanceOf<UriImageSource> ());
			Assert.AreEqual ("http://xamarin.com/img.png", ((UriImageSource)(image.Source)).Uri.AbsoluteUri);
		}

		class MockImageSource : ImageSource 
		{
		}

		[Test]
		public void TestBindingContextPropagation ()
		{
			var context = new object ();
			var image = new Image ();
			image.BindingContext = context;
			var source = new MockImageSource ();
			image.Source = source;
			Assert.AreSame (context, source.BindingContext);

			image = new Image ();
			source = new MockImageSource ();
			image.Source = source;
			image.BindingContext = context;
			Assert.AreSame (context, source.BindingContext);
		}

		[Test]
		public void ImplicitCastOnAbsolutePathsShouldCreateAFileImageSource ()
		{
			var path = "/private/var/mobile/Containers/Data/Application/B1E5AB19-F815-4B4A-AB97-BD4571D53743/Documents/temp/IMG_20140603_150614_preview.jpg";
			var image = new Image { Source = path };
			Assert.That (image.Source, Is.TypeOf<FileImageSource> ());
		}
	}
}