summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue2948.cs
blob: 20cdd96561b0176a6a796e6fe7de83d2df684625 (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
using System;

using Xamarin.Forms.CustomAttributes;
using System.Collections.Generic;
using System.Threading;
using Xamarin.Forms.Internals;

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

namespace Xamarin.Forms.Controls.Issues
{
	[Preserve (AllMembers = true)]
	[Issue (IssueTracker.Github, 2948, "MasterDetailPage Detail is interactive even when Master is open when in Landscape")]
	public class Issue2948 : TestMasterDetailPage
	{
		static MasterDetailPage s_mdp;

		protected override void Init ()
		{
			s_mdp = this;
			var menuPage = new MenuPage ();

			menuPage.Menu.ItemSelected += (sender, e) => NavigateTo (e.SelectedItem as MenuItem);

			Master = menuPage;
			Detail = new NavigationPage (new ContractsPage ());
		}

		[Preserve (AllMembers = true)]
		public class MenuListData : List<MenuItem>
		{
			public MenuListData ()
			{
				Add (new MenuItem () { 
					Title = "Contracts", 
					IconSource = "bank.png", 
					TargetType = typeof(ContractsPage)
				});

				Add (new MenuItem () { 
					Title = "Leads", 
					IconSource = "bank.png", 
					TargetType = typeof(ContractsPage)
				});

				Add (new MenuItem () { 
					Title = "Accounts", 
					IconSource = "bank.png", 
					TargetType = typeof(ContractsPage)
				});

				Add (new MenuItem () {
					Title = "Opportunities",
					IconSource = "bank.png",
					TargetType = typeof(ContractsPage)
				});
			}
		}

		[Preserve (AllMembers = true)]
		public class ContractsPage : ContentPage
		{
			public ContractsPage ()
			{
				Title = "Contracts";
				Icon = "bank.png";

				var grid = new Grid();
				grid.ColumnDefinitions.Add(new ColumnDefinition());
				grid.ColumnDefinitions.Add(new ColumnDefinition());

				var btn = new Button {
					HeightRequest = 300,
					HorizontalOptions = LayoutOptions.End,
					BackgroundColor = Color.Pink,
					AutomationId = "btnOnDetail"
				};

				btn.Clicked+= (object sender, EventArgs e) => {
					DisplayAlert("Clicked","I was clicked","Ok");
				};

				Grid.SetColumn(btn,1);

				grid.Children.Add(btn);

				var showMasterButton = new Button {
					AutomationId = "ShowMasterBtn",
					Text = "Show Master"
				};
				showMasterButton.Clicked += (sender, e) => {
					s_mdp.IsPresented = true;
				};

				Content = new ScrollView {

					Content = new StackLayout {
						Children = {
							showMasterButton,
							grid,
							new BoxView {
								HeightRequest = 100,
								Color = Color.Red,
							},
							new BoxView {
								HeightRequest = 200,
								Color = Color.Green,
							},
							new BoxView {
								HeightRequest = 300,
								Color = Color.Red,
							},
							new BoxView {
								HeightRequest = 400,
								Color = Color.Green,
							},
							new BoxView {
								HeightRequest = 500,
								Color = Color.Red,
							}
						}
					},

				};
			}
		}

		[Preserve (AllMembers = true)]
		public class MenuListView : ListView
		{
			public MenuListView ()
			{
				List<MenuItem> data = new MenuListData ();

				ItemsSource = data;
				VerticalOptions = LayoutOptions.FillAndExpand;
				BackgroundColor = Color.Transparent;

				var cell = new DataTemplate (typeof(ImageCell));
				cell.SetBinding (TextCell.TextProperty, "Title");
				cell.SetBinding (ImageCell.ImageSourceProperty, "IconSource");

				ItemTemplate = cell;
				SelectedItem = data [0];
			}
		}

		public class MenuPage : ContentPage
		{
			public ListView Menu { get; set; }

			public MenuPage ()
			{
				Title = "Menu";
				BackgroundColor = Color.FromHex ("333333");

				Menu = new MenuListView ();

				var menuLabel = new ContentView {
					Padding = new Thickness (10, 36, 0, 5),
					Content = new Label {
						TextColor = Color.FromHex ("AAAAAA"),
						Text = "MENU", 
					}
				};

				var layout = new StackLayout { 
					Spacing = 0, 
					VerticalOptions = LayoutOptions.FillAndExpand
				};
				layout.Children.Add (menuLabel);
				layout.Children.Add (Menu);

				Content = layout;
			}
		}

		void NavigateTo (MenuItem menu)
		{
			var displayPage = (Page)Activator.CreateInstance (menu.TargetType);

			Detail = new NavigationPage (displayPage);

		}

		[Preserve (AllMembers = true)]
		public class MenuItem
		{
			public string Title { get; set; }

			public string IconSource { get; set; }

			public Type TargetType { get; set; }
		}

#if UITEST
		[Test]
		public void Issue2948Test ()
		{
			RunningApp.Screenshot ("I am at Issue 2948");
			RunningApp.SetOrientationLandscape ();
			Thread.Sleep (5000);
			if (ShouldRunTest ()) {
				OpenMDP ();
				var btns = RunningApp.Query (c => c.Marked ("btnOnDetail"));
				if (btns.Length > 0) {
					// on iOS the button could be out of screen
					RunningApp.Tap (c => c.Marked ("btnOnDetail"));
					RunningApp.Screenshot ("I in landscape and master is open");
				}
				RunningApp.WaitForNoElement (c => c.Marked ("Clicked"),"Time out",new TimeSpan(0,0,1));
			}
		}

		[TearDown]
		public void TestTearDown()
		{
			RunningApp.SetOrientationPortrait();
		}

		public bool ShouldRunTest() {
			var isMasterVisible = RunningApp.Query (q => q.Marked ("Leads")).Length > 0;
			return !isMasterVisible;
		}
		public void OpenMDP() {
#if __IOS__
			RunningApp.Tap (q => q.Marked ("Menu"));
#else
			RunningApp.Tap ("ShowMasterBtn");
#endif
		}
#endif
	}
}