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

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

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

namespace Xamarin.Forms.Controls
{
	[Preserve (AllMembers = true)]
	[Issue (IssueTracker.Bugzilla, 36681, "[A] NRE when Picker Replaces Page Content (pre-AppCompat only)", PlatformAffected.Android)]
	public class Bugzilla36681 : TestTabbedPage // or TestMasterDetailPage, etc ...
	{
		public class PickerPage : ContentPage
		{
			public Picker Picker { get; private set; }
			public Label Label { get; private set; }
			public PickerPage ()
			{
				Picker = new Picker { Title = "Select Item", AutomationId = "picker" };

				var items = new List<string> { "item", "item2", "item3", "item4" };
				foreach (var i in items)
					Picker.Items.Add (i);

				Picker.FocusChangeRequested += Picker_FocusChangeRequested;
				Picker.SelectedIndexChanged += Picker_SelectedIndexChanged;

				StackLayout stack = new StackLayout { Padding = 20 };
				stack.Children.Add (Picker);

				Content = stack;
			}

			void Picker_FocusChangeRequested (object sender, FocusRequestArgs e)
			{
				SwitchContent ();
			}

			void Picker_SelectedIndexChanged (object sender, EventArgs e)
			{
				SwitchContent ();
			}

			void SwitchContent ()
			{
				var x = Parent as TabbedPage;
				var y = x.CurrentPage as ContentPage;
				y.Content = new Label {
					Text = "Success!"
				};
				y.Padding = new Thickness (0, 20, 0, 0);
			}
		}

		protected override void Init ()
		{
			PickerPage pickerPage = new PickerPage { Title = "Picker Page" };
			Children.Add (pickerPage);
			Children.Add (new ContentPage { BackgroundColor = Color.Blue, Title = "Page 2" });
		}

#if UITEST
		[Test]
		public void Bugzilla36681Test ()
		{
			if (RunningApp is Xamarin.UITest.Android.AndroidApp) {
				RunningApp.WaitForElement (q => q.Marked ("picker"));
				RunningApp.Tap ("picker");
				var ok = RunningApp.Query ("OK");
				if (ok.Length > 0 && ok[0].Id == "button1" ) { //only in pre-AppCompat; this is the culprit!
					// We check that the query has any results and that the first
					// result matched the id "button1" because the query reports a phantom OK button
					// on Android >= 6.0
					RunningApp.Tap ("OK");
				} else {
					RunningApp.WaitForElement (q => q.Marked ("item2"));
					RunningApp.Tap ("item2");
				}
				RunningApp.WaitForElement (q => q.Marked ("Success!"));
			}
		}
#endif
	}
}