summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla51173.cs
blob: 46841122688c935d5f85fe11fe6a756577df5dfd (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using System;
using System.Globalization;
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;

#if UITEST
using Xamarin.Forms.Core.UITests;
using Xamarin.UITest;
using NUnit.Framework;
#endif

namespace Xamarin.Forms.Controls.Issues
{
#if UITEST
    [Category(UITestCategories.Image)]
#endif

    [Preserve(AllMembers = true)]
	[Issue(IssueTracker.Bugzilla, 51173, "ImageRenderer, async void SetImage - Cannot catch exceptions", PlatformAffected.All)]
	public class Bugzilla51173 : TestContentPage
	{
#if UITEST
		[Test(Description = "Attempt to load an image from a URI which does not exist")]
		public void Bugzilla51173_NonexistentUri()
		{
			
			RunningApp.WaitForElement(q => q.Marked(UriDoesNotExist));

			RunningApp.Tap(UriDoesNotExist);
			RunningApp.WaitForElement(q => q.Marked(ErrorLogged));
			RunningApp.WaitForElement(q => q.Marked(NotLoading));
		}

		[Test(Description = "The IImageSourceHandler itself throws an exception")]
		public void Bugzilla51173_HandlerThrowsException()
		{
			RunningApp.WaitForElement(q => q.Marked(HandlerThrows));
			
			RunningApp.Tap(HandlerThrows);
			RunningApp.WaitForElement(q => q.Marked(ErrorLogged));
			RunningApp.WaitForElement(q => q.Marked(NotLoading));
		}

		[Test(Description = "The URI is valid, but the image data is not")]
		public void Bugzilla51173_RealUriWithInvalidImageData()
		{
			RunningApp.WaitForElement(q => q.Marked(RealUriInvalidImage));

			RunningApp.Tap(RealUriInvalidImage);
			RunningApp.WaitForElement(q => q.Marked(ErrorLogged));
			RunningApp.WaitForElement(q => q.Marked(NotLoading));
		}

		[Test(Description = "Attempt to load a local image which does not exist")]
		public void Bugzilla51173_NonexistentImage()
		{
			RunningApp.WaitForElement(q => q.Marked(ImageDoesNotExist));

			RunningApp.Tap(ImageDoesNotExist);
			RunningApp.WaitForElement(q => q.Marked(ErrorLogged));
			RunningApp.WaitForElement(q => q.Marked(NotLoading));
		}

		[Test(Description = "Attempt to load a local image which exists, but has corrupt data")]
		public void Bugzilla51173_InvalidImage()
		{
			RunningApp.WaitForElement(q => q.Marked(ImageIsInvalid));

			RunningApp.Tap(ImageIsInvalid);
			RunningApp.WaitForElement(q => q.Marked(ErrorLogged));
			RunningApp.WaitForElement(q => q.Marked(NotLoading));
		}

		[Test(Description = "Load a valid image")]
		public void Bugzilla51173_ValidImage()
		{
			RunningApp.WaitForElement(q => q.Marked(ValidImage));
			RunningApp.Tap(ValidImage);
			RunningApp.WaitForElement(q => q.Marked(NotLoading));
		}
#endif

		const string ValidImage = "Valid Image";
		const string ImageDoesNotExist = "Non-existent Image File";
		const string ImageIsInvalid = "Invalid Image File (bad data)";
		const string UriDoesNotExist = "Non-existent URI";
		const string HandlerThrows = "Source Throws Exception";
		const string RealUriInvalidImage = "Valid URI with invalid image file";
		const string ErrorLogged = "Error logged";

		const string Loading = "Loading";
		const string NotLoading = "Not Loading";

		Label _results;
		Image _image;

		class LoadingConverter : IValueConverter
		{
			public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
			{
				if ((bool)value)
				{
					return Loading;
				}

				return NotLoading;
			}

			public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
			{
				throw new NotImplementedException();
			}
		}

		protected override void Init()
		{
			_results = new Label { Margin = 10, FontAttributes = FontAttributes.Bold, BackgroundColor = Color.Silver, HorizontalTextAlignment = TextAlignment.Center};

			var errorMessage = new Label();

			Log.Listeners.Add(
				new DelegateLogListener((c, m) => Device.BeginInvokeOnMainThread(() =>
				{
					_results.Text = ErrorLogged;
					errorMessage.Text = m;
				})));
			
			var instructions = new Label
			{
				Text =
					"Pressing the 'Valid Image' button should display an image of a coffee cup. Every other button should cause the messager 'Error logged' to appear at the top of the page."
			};

			_image = new Image { BackgroundColor = Color.Bisque, HeightRequest = 20 };

			var loadingState = new Label();
			loadingState.SetBinding(Label.TextProperty, new Binding(Image.IsLoadingProperty.PropertyName, BindingMode.Default, new LoadingConverter()));
			loadingState.BindingContext = _image;

			var legit = CreateTest(() => _image.Source = ImageSource.FromFile("coffee.png"), ValidImage);

			var invalidImageFileName = CreateTest(() => _image.Source = ImageSource.FromFile("fake.png"), ImageDoesNotExist); 

			var invalidImageFile = CreateTest(() => _image.Source = ImageSource.FromFile("invalidimage.jpg"), ImageIsInvalid);

			var fakeUri = CreateTest(() => _image.Source = ImageSource.FromUri(new Uri("http://not.real")), UriDoesNotExist);

			var crashImage = CreateTest(() => _image.Source = new FailImageSource(), HandlerThrows);

			var uriInvalidImageData =
				CreateTest(() => _image.Source = ImageSource.FromUri(new Uri("https://gist.githubusercontent.com/hartez/a2dda6b5c78852bcf4832af18f21a023/raw/39f4cd2e9fe8514694ac7fa0943017eb9308853d/corrupt.jpg")),
					RealUriInvalidImage);

			Content = new StackLayout
			{
				Margin = new Thickness(5, 20, 5, 0),
				Children =
				{
					_image,
					instructions,
					legit,
					invalidImageFileName,
					invalidImageFile,
					fakeUri,
					crashImage,
					uriInvalidImageData,
					_results,
					loadingState,
					errorMessage
				}
			};
		}

		Button CreateTest(Action imageLoadAction, string title)
		{
			var button = new Button { Text = title };

			button.Clicked += (sender, args) =>
			{
				_results.Text = "";
				imageLoadAction();
			};

			return button;
		}
	}
}