summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Pages.UnitTests/IntegrationTests.cs
blob: 480706fcefb62bad8d24177b1403ae5e0ae9f0c7 (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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NUnit.Framework;
using Xamarin.Forms.Core.UnitTests;

namespace Xamarin.Forms.Pages.UnitTests
{
	[TestFixture]
	internal class IntegrationTests
	{
		[SetUp]
		public void Setup ()
		{
			Device.PlatformServices = new MockPlatformServices ();
		}

		[TearDown]
		public void TearDown ()
		{
			Device.PlatformServices = null;
		}

		public class SimpleDataSource : BaseDataSource
		{
			readonly ObservableCollection<IDataItem> dataItems = new ObservableCollection<IDataItem> ();
			string json;

			public SimpleDataSource ()
			{
				Json = @"{
 'Name' : 'Jason Smith',
 'Phone' : '(555)248-7561',
 'PrimaryEmail' : 'jason@xamarin.com',
 'JobTitle' : 'Software Engineering Manager',
 'TimeZone' : 'PST',
 'Image' : 'https://c1.staticflickr.com/3/2877/10612763564_7f2d1734ea_b.jpg',
 'Address' : '430 Pacific Ave, San Francisico CA, 55592',
}";
			}

			public string Json
			{
				get { return json; }
				set
				{
					json = value;
					try {
						var dict = JsonConvert.DeserializeObject<Dictionary<string, object>> (json);
						foreach (var kvp in dict)
							dataItems.Add (new DataItem (kvp.Key, kvp.Value));
					} catch (Exception ex) {
						Debug.WriteLine (ex.Message);
					}
				}
			}

			protected override Task<IList<IDataItem>> GetRawData ()
			{
				return Task.FromResult<IList<IDataItem>> (dataItems);
			}

			protected override object GetValue (string key)
			{
				var target = dataItems.FirstOrDefault (d => d.Name == key);
				if (target == null)
					return null;
					//throw new KeyNotFoundException ();
				return target.Value;
			}

			protected override bool SetValue (string key, object value)
			{
				var target = dataItems.FirstOrDefault (d => d.Name == key);
				if (target == null) {
					dataItems.Add (new DataItem (key, value));
					return true;
				}
				if (target.Value == value)
					return false;
				target.Value = value;
				return true;
			}
		}

		public class TestPage : PersonDetailPage
		{
			public TestPage ()
			{
				SetBinding (DisplayNameProperty, new DataSourceBinding ("Name"));
				SetBinding (PhoneNumberProperty, new DataSourceBinding ("Phone"));
				SetBinding (EmailProperty, new DataSourceBinding ("PrimaryEmail"));
			}
		}

		[Test]
		public void Test1 ()
		{
			var page = new TestPage ();
			
			page.DataSource = new SimpleDataSource ();
			page.Platform = new UnitPlatform();

			Assert.AreEqual (9, page.DataSource.MaskedKeys.Count ());
		}

		[Test]
		public void JsonDataSourceComplex ()
		{
			var jsonDataSource = new JsonDataSource ();

			var json = @"[
	{
		'Name': 'Kristen Perez',
		'Address': 'Ap #325-3386 Ac Av.',
		'Phone': '(016977) 7108',
		'Title': 'Lorem Ipsum Dolor Incorporated',
		'Email': 'ac.risus.Morbi@interdum.co.uk',
		'List' : [
          'Foo', 'Bar', 'Baz'
        ]
	},
	{
		'Name': 'Murphy Cote',
		'Address': '906-6938 Porttitor Ave',
		'Phone': '076 9223 8954',
		'Title': 'Vulputate Industries',
		'Email': 'non@consequat.ca',
        'List' : [ { 'Second' : 'Thing' } ]
	},
	{
		'Name': 'Nicole Valdez',
		'Address': '485-9530 Ut Rd.',
		'Phone': '0800 364 0760',
		'Title': 'Diam At Ltd',
		'Email': 'nisl@ipsum.edu'
	}
]";
			jsonDataSource.Source = json;
			Debug.WriteLine (jsonDataSource);
		}
	}
}