summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/TransparentOverlayTests.cs
blob: 6fd5772d7cec191551ddd8ff338e57a0836e7da0 (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;
using System.Linq;

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

namespace Xamarin.Forms.Controls.Issues
{
#if UITEST
	[Category(UITestCategories.InputTransparent)]
#endif
	[Preserve(AllMembers = true)]
	[Issue(IssueTracker.None, 618, "Transparent Overlays", PlatformAffected.All)]
	public class TransparentOverlayTests : TestNavigationPage
	{
		readonly Color _transparentColor = Color.Transparent;
		readonly Color _nontransparentColor = Color.Blue;

		double _transparentOpacity = 0;
		double _nonTransparentOpacity = 0.2;

        const string Running = "Running...";
		const string Success = "Success";
        const string Failure = "Failure";
		const string DefaultButtonText = "Button";
		const string Overlay = "overlay";
		const string AddOverlay = "Add Overlay";

		protected override void Init()
		{
			PushAsync(Menu());
		}

		ContentPage Menu()
		{
			var layout = new StackLayout();

			layout.Children.Add(new Label {Text = "Select a test below"});

			foreach (var test in GenerateTests)
			{
				layout.Children.Add(MenuButton(test));
			}

			return new ContentPage { Content = layout };
		}

		Button MenuButton(TestPoint test)
		{
			var button = new Button { Text = test.ToString(), AutomationId = test.AutomationId };

			button.Clicked += (sender, args) => PushAsync(CreateTestPage(test));

			return button;
		}

		[Preserve(AllMembers = true)]
		public struct TestPoint
		{
			public TestPoint(int i) : this()
			{
				AutomationId = $"transparenttest{i}";

				Opacity = (i & (1 << 0)) == 0;
				InputTransparent = (i & (1 << 1)) == 0;
				BackgroundColor = (i & (1 << 2)) == 0;

				// Layouts should be input transparent _only_ if they were explicitly told to be
				ShouldBeTransparent = InputTransparent;
			}

			internal string AutomationId { get; set; }
			internal bool ShouldBeTransparent { get; set; }

			internal bool Opacity { get; set; }
			internal bool InputTransparent { get; set; }
			internal bool BackgroundColor { get; set; }
			
			public override string ToString()
			{
				return $"O{(Opacity ? "1" : "0")}, B{(BackgroundColor ? "1" : "0")}, I{(InputTransparent ? "1" : "0")}";
			}
		}

		static IEnumerable<TestPoint> GenerateTests
		{
			get { return Enumerable.Range(0, 8).Select(i => new TestPoint(i)); }
		}

		ContentPage CreateTestPage(TestPoint test)
		{
			Color backgroundColor = test.BackgroundColor? _transparentColor : _nontransparentColor;
			double opacity = test.Opacity ? _transparentOpacity : _nonTransparentOpacity;
			bool inputTransparent = test.InputTransparent;

            var grid = new Grid
            {
                AutomationId = "testgrid",
				HorizontalOptions = LayoutOptions.Fill,
				VerticalOptions = LayoutOptions.Fill
			};

			grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
            grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
			grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
			grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Star });

			var instructions = new Label
			{
				HorizontalOptions = LayoutOptions.Fill,
				HorizontalTextAlignment = TextAlignment.Center,
				Text = $"Tap the 'Add Overlay' button, then tap the button labeled 'Button'."
				       + (test.ShouldBeTransparent
					       ? $" If the label below's text changes to {Success} the test has passed."
					       : " If the label below's text remains unchanged, the test has passed.")
			};

			grid.Children.Add(instructions);

            var results = new Label 
            { 
                HorizontalOptions = LayoutOptions.Fill,
                HorizontalTextAlignment = TextAlignment.Center, 
                Text = Running 
            };

            grid.Children.Add(results);
            Grid.SetRow(results, 1);

			var button = new Button
			{
				Text = DefaultButtonText,
				HorizontalOptions = LayoutOptions.Center,
				VerticalOptions = LayoutOptions.Center
			};

			button.Clicked += (sender, args) =>
			{
				results.Text = test.ShouldBeTransparent ? Success : Failure;
			};

			var layout = new StackLayout
			{
                AutomationId = Overlay,
				HorizontalOptions = LayoutOptions.Fill,
				VerticalOptions = LayoutOptions.Fill,
				BackgroundColor = backgroundColor,
				InputTransparent = inputTransparent,
				Opacity = opacity
			};

			grid.Children.Add(button);
			Grid.SetRow(button, 3);

			var addOverlayButton = new Button() { Text = AddOverlay };
			addOverlayButton.Clicked += (sender, args) =>
			{
				grid.Children.Add(layout);
				Grid.SetRow(layout, 3);
			};

			grid.Children.Add(addOverlayButton);
			Grid.SetRow(addOverlayButton, 2);

			return new ContentPage { Content = grid, Title = test.ToString()};
		}

#if UITEST
        [Test, TestCaseSource(nameof(GenerateTests))]
        public void VerifyInputTransparent(TestPoint test)
        {
            RunningApp.WaitForElement(q => q.Marked(test.AutomationId));
            RunningApp.Tap(test.AutomationId);

			// Determine the location of the button; we have to do this before adding the overlay because
			// otherwise the iOS UI tests won't be able to find it consistently
			var button = RunningApp.WaitForElement(DefaultButtonText);
			var coords = new Tuple<float, float>(button[0].Rect.CenterX, button[0].Rect.CenterY);

			// Add the overlay 
			RunningApp.WaitForElement(AddOverlay);
			RunningApp.Tap(AddOverlay);

			// Now tap the screen at the Button's location
			// We can't do RunningApp.Tap(DefaultButtonText) because the UI tests on iOS can't see it
			RunningApp.TapCoordinates(coords.Item1, coords.Item2);

			// Check the results
            RunningApp.WaitForElement(test.ShouldBeTransparent ? Success : Running); 
		}
#endif

	}
}