summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/ImageLoadingErrorHandling.cs
blob: b2607c713484b6e73b2b7348f824029653b25f79 (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
using System;
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;

namespace Xamarin.Forms.Controls
{
	[Preserve(AllMembers = true)]
	[Issue(IssueTracker.None, 0, "Image Loading Error Handling", PlatformAffected.WinRT | PlatformAffected.UWP)]
	public class ImageLoadingErrorHandling : TestContentPage
	{
		protected override void Init()
		{
			Log.Listeners.Add(
				new DelegateLogListener((c, m) => Device.BeginInvokeOnMainThread(() => DisplayAlert(c, m, "Cool, Thanks"))));

			var image = new Image() {BackgroundColor = Color.White};

			Grid legit = CreateTest(() => image.Source = ImageSource.FromFile("coffee.png"),
				"Valid Image",
				"Clicking this button should load an image at the top of the page.",
				Color.Silver);

			Grid invalidImageFileName = CreateTest(() => image.Source = ImageSource.FromFile("fake.png"),
				"Non-existent Image File",
				"Clicking this button should display an alert dialog with an error that the image failed to load.");

			Grid invalidImageFile = CreateTest(() => image.Source = ImageSource.FromFile("invalidimage.jpg"),
				"Invalid Image File (bad data)",
				"Clicking this button should display an alert dialog with an error that the image failed to load.",
				Color.Silver);

			Grid fakeUri = CreateTest(() => image.Source = ImageSource.FromUri(new Uri("http://not.real")),
				"Non-existent URI",
				(Device.RuntimePlatform == Device.UWP || Device.RuntimePlatform == Device.WinRT) && Device.Idiom == TargetIdiom.Phone
				? "Clicking this button should display an alert dialog. The error message should include the text 'NotFound'."
				: "Clicking this button should display an alert dialog. The error message should include the text 'the server name or address could not be resolved'.");

			// This used to crash the app with an uncatchable error; need to make sure it's not still doing that
			Grid crashImage = CreateTest(() => image.Source = new FailImageSource(),
				"Source Throws Exception",
				"Clicking this button should display an alert dialog. The error messages hould include the test 'error updating image source'.",
				Color.Silver);

			Grid uriInvalidImageData =
				CreateTest(() => image.Source = ImageSource.FromUri(new Uri("https://gist.githubusercontent.com/hartez/a2dda6b5c78852bcf4832af18f21a023/raw/39f4cd2e9fe8514694ac7fa0943017eb9308853d/corrupt.jpg")),
					"Valid URI with invalid image file",
					"Clicking this button should display an alert dialog. The error message should include the text 'UriImageSourceHandler could not load https://gist.githubusercontent.com/hartez/a2dda6b5c78852bcf4832af18f21a023/raw/39f4cd2e9fe8514694ac7fa0943017eb9308853d/corrupt.jpg'");

			Content = new StackLayout
			{
				Children =
				{
					image,
					legit,
					invalidImageFileName,
					invalidImageFile,
					fakeUri,
					crashImage,
					uriInvalidImageData
				}
			};
		}

		static Grid CreateTest(Action imageLoadAction, string title, string instructions, Color? backgroundColor = null)
		{
			var button = new Button { Text = "Test" };

			button.Clicked += (sender, args) => imageLoadAction();

			var titleLabel = new Label
			{
				Text = title,
				FontAttributes = FontAttributes.Bold
			};

			var label = new Label
			{
				Text = instructions
			};

			var grid = new Grid
			{
				ColumnDefinitions =
					new ColumnDefinitionCollection { new ColumnDefinition(), new ColumnDefinition(), new ColumnDefinition() },
				RowDefinitions = new RowDefinitionCollection { new RowDefinition { Height = 80 } }
			};

			if (backgroundColor.HasValue)
			{
				grid.BackgroundColor = backgroundColor.Value;
			}

			grid.AddChild(titleLabel, 0, 0);
			grid.AddChild(label, 1, 0);
			grid.AddChild(button, 2, 0);

			return grid;
		}
	}
}