Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ObservableLookupTests.cs « Xamarin.PropertyEditing.Tests - github.com/xamarin/Xamarin.PropertyEditing.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c0ab5b7a955f25eec9da2340e79876ebdfcab68a (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
using System;
using System.Collections.Specialized;
using System.Linq;
using NUnit.Framework;

namespace Xamarin.PropertyEditing.Tests
{
	[TestFixture]
	internal class ObservableLookupTests
	{
		[TestCase ("key")]
		[TestCase (null)]
		public void RemoveGroup (string key)
		{
			const string value = "value";
			var lookup = new ObservableLookup<string, string> ();
			lookup.Add (key, value);
			Assume.That (lookup.Contains (key), Is.True);
			Assume.That (lookup[key], Contains.Item (value));

			bool groupRemoved = false;
			lookup.CollectionChanged += (sender, args) => {
				if (args.Action == NotifyCollectionChangedAction.Remove) {
					var g = args.OldItems[0] as IGrouping<string, string>;
					if (g != null && g.Key == key)
						groupRemoved = true;
				}
			};

			var grouping = lookup[key];
			lookup.Remove (key);

			Assert.That (groupRemoved, Is.True);
			Assert.That (lookup, Does.Not.Contain (grouping));
		}

		[TestCase ("key")]
		[TestCase (null)] // The reality is that null as a key receives special treatment
		public void RemoveLastItemInGroup (string key)
		{
			const string value = "value";
			var lookup = new ObservableLookup<string, string> ();
			lookup.Add (key, value);
			Assume.That (lookup.Contains (key), Is.True);
			Assume.That (lookup[key], Contains.Item (value));

			bool itemRemoved = false, groupRemoved = false;
			lookup.CollectionChanged += (sender, args) => {
				if (args.Action == NotifyCollectionChangedAction.Remove) {
					var g = args.OldItems[0] as IGrouping<string, string>;
					if (g != null && g.Key == key)
						groupRemoved = true;
				}
			};

			var grouping = lookup[key];
			((INotifyCollectionChanged) grouping).CollectionChanged += (sender, args) => {
				if (args.Action == NotifyCollectionChangedAction.Remove) {
					if ((string)args.OldItems[0] == value)
						itemRemoved = true;
				}
			};

			lookup.Remove (key, value);

			Assert.That (itemRemoved, Is.True);
			Assert.That (groupRemoved, Is.True);
			Assert.That (grouping, Does.Not.Contains (value));
			Assert.That (lookup, Does.Not.Contain (grouping));
		}

		[TestCase ("group")]
		[TestCase (null)]
		[Description ("If removing the last item from a group removes the group, adding a group with no items shouldn't add it")]
		public void AddingEmptyElementsIsIgnored (string groupName)
		{
			var lookup = new ObservableLookup<string, string> ();
			bool changed = false;
			lookup.CollectionChanged += (sender, args) => {
				changed = true;
			};
			lookup.Add (groupName, Enumerable.Empty<string> ());

			Assert.That (lookup, Is.Empty);
			Assert.That (changed, Is.False);
		}

		[TestCase ("group")]
		[TestCase (null)]
		[Description ("If removing the last item from a group removes the group, adding a group with no items shouldn't add it")]
		public void AddingEmptyGroupingIsIgnored (string groupName)
		{
			var lookup = new ObservableLookup<string, string> ();
			bool changed = false;
			lookup.CollectionChanged += (sender, args) => {
				changed = true;
			};
			lookup.Add (new ObservableGrouping<string, string> (groupName));

			Assert.That (lookup, Is.Empty);
			Assert.That (changed, Is.False);
		}

		[TestCase ("group")]
		[TestCase (null)]
		[Description ("If removing the last item from a group removes the group, adding a group with no items shouldn't add it")]
		public void InsertingEmptyIsIgnored (string groupName)
		{
			var lookup = new ObservableLookup<string, string> ();
			bool changed = false;
			lookup.CollectionChanged += (sender, args) => {
				changed = true;
			};
			lookup.Insert (0, new ObservableGrouping<string, string> (groupName));

			Assert.That (lookup, Is.Empty);
			Assert.That (changed, Is.False);
		}

		[Test]
		public void RemoveLastElementFromNullGroup ()
		{
			const string element = "test";
			var lookup = new ObservableLookup<string, string> ();
			lookup.Add (null, element);

			int groupChanged = 0;
			var g = lookup[null];
			((INotifyCollectionChanged) g).CollectionChanged += (sender, args) => {
				groupChanged++;
				Assert.That (args.Action, Is.EqualTo (NotifyCollectionChangedAction.Remove));
				Assert.That (args.OldItems[0], Is.SameAs (element));
			};

			int lookupChanged = 0;
			lookup.CollectionChanged += (sender, args) => {
				lookupChanged++;
				Assert.That (args.Action, Is.EqualTo (NotifyCollectionChangedAction.Remove));
				Assert.That (args.OldItems[0], Is.SameAs (g));
			};

			lookup.Remove (null, element);
			Assert.That (lookupChanged, Is.EqualTo (1));
			Assert.That (groupChanged, Is.EqualTo (1));
		}
	}
}