summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla39499.cs
blob: 16c033dcee8b52aed59bbb32be0094d6e9fcd77f (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
using System;

using Xamarin.Forms.CustomAttributes;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Collections.ObjectModel;

#if UITEST
using Xamarin.UITest;
using NUnit.Framework;
#endif

namespace Xamarin.Forms.Controls
{
	[Preserve(AllMembers = true)]
	[Issue(IssueTracker.Bugzilla, 39499, "CarouselViewTest")]
	public sealed class CarouselViewContentPage : TestContentPage // or TestMasterDetailPage, etc ...
	{
		[Preserve(AllMembers = true)]
		public sealed class Item
		{
			static int s_id = 0;

			int id;

			internal Item()
			{
				id = s_id++;
			}

			public int Id => id;

			public string Text => $"Item {Id}";
		}

		[Preserve(AllMembers = true)]
		public sealed class ItemView : ContentView
		{
			public ItemView()
			{
				var idLabel = new Label() { StyleId = "id", TextColor = Color.White };
				idLabel.SetBinding(Label.TextProperty, nameof(Item.Text));

				var stackLayout = new StackLayout
				{
					Children = {
						new Label { Text = "Target" },
						new Label { Text = "Stack" }
					},
					BackgroundColor = Color.Red
				};

				var button = CreateButton("Hide Target Stack", () =>
				{
					stackLayout.IsVisible = false;
				});

				var buttonImage = CreateButton("AddImage", () =>
				{
					stackLayout.IsVisible = true;
					stackLayout.Children.Add(new Image { Source = "menuIcon.png" });
				});
				Content = new StackLayout
				{
					Children = {
						idLabel,
						button,
						buttonImage,
						stackLayout,
					}
				};
			}

			Button CreateButton(string text, Action clicked)
			{
				var button = new Button();
				button.Text = text;
				button.Clicked += (s, e) =>
				{
					clicked();
				};
				return button;
			}
		}

		static readonly IList<Item> Items = new ObservableCollection<Item>() {
			new Item(),
			new Item(),
		};

		Button CreateButton(string text, Action onClicked = null)
		{
			var button = new Button
			{
				Text = text
			};

			if (onClicked != null)
				button.Clicked += (s, e) => onClicked();

			return button;
		}

		protected override void Init()
		{
			BackgroundColor = Color.Blue;

			var carouselView = new CarouselView
			{
				BackgroundColor = Color.Purple,
				ItemsSource = Items,
				ItemTemplate = new DataTemplate(typeof(ItemView)),
				Position = 0
			};

			var moveBar = new StackLayout
			{
				Orientation = StackOrientation.Horizontal,
				HorizontalOptions = LayoutOptions.FillAndExpand,
				Children =
				{
					CreateButton("+", () => Items.Add(new Item())),
					CreateButton("<<", () => carouselView.Position = 0),
					CreateButton("<", () =>
					{
						try
						{
							carouselView.Position--;
						}
						catch
						{
						}
					}),
					CreateButton(">", () =>
					{
						try
						{
							carouselView.Position++;
						}
						catch
						{
						}
					}),
					CreateButton(">>", () => carouselView.Position = Items.Count - 1)
				}
			};

			Content = new StackLayout
			{
				Children =
				{
					carouselView,
					moveBar
				}
			};
		}

#if UITEST
		//[Test]
		public void CarouselViewTest()
		{
			var app = RunningApp;
			app.WaitForElement(q => q.Marked("Item 0"));
			app.SwipeRightToLeft();
			app.WaitForElement(q => q.Marked("Item 1"));
			app.Tap(c => c.Marked("<"));
			app.WaitForElement(q => q.Marked("Item 0"));
		}

		[Test]
		public void CarouselViewTestAddItem()
		{
			var app = RunningApp;
			app.WaitForElement(q => q.Marked("Hide Target Stack"));
			app.Tap(c => c.Marked("+"));
			app.SwipeRightToLeft();
			app.SwipeRightToLeft();
			app.WaitForElement(q => q.Marked("Item 2"));
			app.Screenshot("I see the Item 2");
		}
#endif
	}
}