|
| 1 | +using System.Collections.ObjectModel; |
| 2 | +using System.Collections.Specialized; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using AutoMapper.EquivalencyExpression; |
| 6 | +using FluentAssertions; |
| 7 | +using Xunit; |
| 8 | + |
| 9 | +namespace AutoMapper.Collection |
| 10 | +{ |
| 11 | + public class ObservableCollectionReorderingTests : MappingTestBase |
| 12 | + { |
| 13 | + private static void Configure(IMapperConfigurationExpression cfg) |
| 14 | + { |
| 15 | + cfg.AddCollectionMappers(); |
| 16 | + cfg.CreateMap<MapCollectionWithEqualityTests.ThingDto, MapCollectionWithEqualityTests.Thing>() |
| 17 | + .EqualityComparison((MapCollectionWithEqualityTests.ThingDto dto, MapCollectionWithEqualityTests.Thing entity) => dto.ID == entity.ID); |
| 18 | + } |
| 19 | + |
| 20 | + [Fact] |
| 21 | + public void Reordering_should_raise_Move_event_for_existing_items() |
| 22 | + { |
| 23 | + var mapper = CreateMapper(Configure); |
| 24 | + |
| 25 | + // Source in desired order |
| 26 | + var source = new[] |
| 27 | + { |
| 28 | + new MapCollectionWithEqualityTests.ThingDto { ID = 1 }, |
| 29 | + new MapCollectionWithEqualityTests.ThingDto { ID = 2 } |
| 30 | + }; |
| 31 | + |
| 32 | + // Destination has the same two existing instances but in reverse order |
| 33 | + var a = new MapCollectionWithEqualityTests.Thing { ID = 1 }; |
| 34 | + var b = new MapCollectionWithEqualityTests.Thing { ID = 2 }; |
| 35 | + var destination = new ObservableCollection<MapCollectionWithEqualityTests.Thing> { b, a }; |
| 36 | + |
| 37 | + var events = new List<NotifyCollectionChangedEventArgs>(); |
| 38 | + destination.CollectionChanged += (_, e) => events.Add(e); |
| 39 | + |
| 40 | + // Act |
| 41 | + var result = mapper.Map(source, destination); |
| 42 | + |
| 43 | + // Basic correctness checks |
| 44 | + result.Should().BeSameAs(destination); |
| 45 | + result.Select(t => t.ID).Should().ContainInOrder(1, 2); |
| 46 | + result[0].Should().BeSameAs(a); |
| 47 | + result[1].Should().BeSameAs(b); |
| 48 | + |
| 49 | + // Event expectations: a single Move from index 1 to 0 for item 'a' |
| 50 | + events.Should().HaveCount(1); |
| 51 | + events[0].Action.Should().Be(NotifyCollectionChangedAction.Move); |
| 52 | + events[0].OldStartingIndex.Should().Be(1); |
| 53 | + events[0].NewStartingIndex.Should().Be(0); |
| 54 | + events[0].OldItems![0].Should().BeSameAs(a); |
| 55 | + } |
| 56 | + |
| 57 | + [Fact] |
| 58 | + public void No_reordering_should_not_raise_any_events_for_equal_order() |
| 59 | + { |
| 60 | + var mapper = CreateMapper(Configure); |
| 61 | + |
| 62 | + var source = new[] |
| 63 | + { |
| 64 | + new MapCollectionWithEqualityTests.ThingDto { ID = 1 }, |
| 65 | + new MapCollectionWithEqualityTests.ThingDto { ID = 2 } |
| 66 | + }; |
| 67 | + |
| 68 | + var a = new MapCollectionWithEqualityTests.Thing { ID = 1 }; |
| 69 | + var b = new MapCollectionWithEqualityTests.Thing { ID = 2 }; |
| 70 | + var destination = new ObservableCollection<MapCollectionWithEqualityTests.Thing> { a, b }; |
| 71 | + |
| 72 | + var events = new List<NotifyCollectionChangedEventArgs>(); |
| 73 | + destination.CollectionChanged += (_, e) => events.Add(e); |
| 74 | + |
| 75 | + var result = mapper.Map(source, destination); |
| 76 | + |
| 77 | + result.Should().BeSameAs(destination); |
| 78 | + result.Select(t => t.ID).Should().ContainInOrder(1, 2); |
| 79 | + events.Should().BeEmpty(); |
| 80 | + } |
| 81 | + |
| 82 | + [Fact] |
| 83 | + public void Complex_reordering_should_raise_only_Move_events() |
| 84 | + { |
| 85 | + var mapper = CreateMapper(Configure); |
| 86 | + |
| 87 | + var source = new[] |
| 88 | + { |
| 89 | + new MapCollectionWithEqualityTests.ThingDto { ID = 1 }, |
| 90 | + new MapCollectionWithEqualityTests.ThingDto { ID = 2 }, |
| 91 | + new MapCollectionWithEqualityTests.ThingDto { ID = 3 } |
| 92 | + }; |
| 93 | + |
| 94 | + var a = new MapCollectionWithEqualityTests.Thing { ID = 1 }; |
| 95 | + var b = new MapCollectionWithEqualityTests.Thing { ID = 2 }; |
| 96 | + var c = new MapCollectionWithEqualityTests.Thing { ID = 3 }; |
| 97 | + var destination = new ObservableCollection<MapCollectionWithEqualityTests.Thing> { c, a, b }; |
| 98 | + |
| 99 | + var events = new List<NotifyCollectionChangedEventArgs>(); |
| 100 | + destination.CollectionChanged += (_, e) => events.Add(e); |
| 101 | + |
| 102 | + var result = mapper.Map(source, destination); |
| 103 | + |
| 104 | + result.Should().BeSameAs(destination); |
| 105 | + result.Select(t => t.ID).Should().ContainInOrder(1, 2, 3); |
| 106 | + result[0].Should().BeSameAs(a); |
| 107 | + result[1].Should().BeSameAs(b); |
| 108 | + result[2].Should().BeSameAs(c); |
| 109 | + |
| 110 | + // Expect two Move events (reorder existing items), no Add/Remove |
| 111 | + events.Should().HaveCount(2); |
| 112 | + events.Should().OnlyContain(e => e.Action == NotifyCollectionChangedAction.Move); |
| 113 | + } |
| 114 | + |
| 115 | + [Fact] |
| 116 | + public void Reordering_with_new_items_should_raise_Add_and_Move_events() |
| 117 | + { |
| 118 | + var mapper = CreateMapper(Configure); |
| 119 | + |
| 120 | + var source = new[] |
| 121 | + { |
| 122 | + new MapCollectionWithEqualityTests.ThingDto { ID = 1 }, |
| 123 | + new MapCollectionWithEqualityTests.ThingDto { ID = 4 }, |
| 124 | + new MapCollectionWithEqualityTests.ThingDto { ID = 2 } |
| 125 | + }; |
| 126 | + |
| 127 | + var a = new MapCollectionWithEqualityTests.Thing { ID = 1 }; |
| 128 | + var b = new MapCollectionWithEqualityTests.Thing { ID = 2 }; |
| 129 | + var destination = new ObservableCollection<MapCollectionWithEqualityTests.Thing> { b, a }; |
| 130 | + |
| 131 | + var events = new List<NotifyCollectionChangedEventArgs>(); |
| 132 | + destination.CollectionChanged += (_, e) => events.Add(e); |
| 133 | + |
| 134 | + var result = mapper.Map(source, destination); |
| 135 | + |
| 136 | + result.Select(t => t.ID).Should().ContainInOrder(1, 4, 2); |
| 137 | + result[0].Should().BeSameAs(a); |
| 138 | + result[2].Should().BeSameAs(b); |
| 139 | + |
| 140 | + // Expect: Move(a 1->0), Add(4 at 1), Move(b 1->2) |
| 141 | + events.Count(e => e.Action == NotifyCollectionChangedAction.Move).Should().Be(2); |
| 142 | + events.Count(e => e.Action == NotifyCollectionChangedAction.Add).Should().Be(1); |
| 143 | + events.Count(e => e.Action == NotifyCollectionChangedAction.Remove).Should().Be(0); |
| 144 | + } |
| 145 | + |
| 146 | + [Fact] |
| 147 | + public void Removing_unmatched_items_then_no_reorder_should_raise_only_Remove() |
| 148 | + { |
| 149 | + var mapper = CreateMapper(Configure); |
| 150 | + |
| 151 | + var source = new[] |
| 152 | + { |
| 153 | + new MapCollectionWithEqualityTests.ThingDto { ID = 1 }, |
| 154 | + new MapCollectionWithEqualityTests.ThingDto { ID = 2 } |
| 155 | + }; |
| 156 | + |
| 157 | + var a = new MapCollectionWithEqualityTests.Thing { ID = 1 }; |
| 158 | + var b = new MapCollectionWithEqualityTests.Thing { ID = 2 }; |
| 159 | + var extra = new MapCollectionWithEqualityTests.Thing { ID = 3 }; |
| 160 | + var destination = new ObservableCollection<MapCollectionWithEqualityTests.Thing> { extra, a, b }; |
| 161 | + |
| 162 | + var events = new List<NotifyCollectionChangedEventArgs>(); |
| 163 | + destination.CollectionChanged += (_, e) => events.Add(e); |
| 164 | + |
| 165 | + var result = mapper.Map(source, destination); |
| 166 | + |
| 167 | + result.Select(t => t.ID).Should().ContainInOrder(1, 2); |
| 168 | + events.Count(e => e.Action == NotifyCollectionChangedAction.Remove).Should().Be(1); |
| 169 | + events.Count(e => e.Action == NotifyCollectionChangedAction.Move).Should().Be(0); |
| 170 | + events.Count(e => e.Action == NotifyCollectionChangedAction.Add).Should().Be(0); |
| 171 | + } |
| 172 | + } |
| 173 | +} |
0 commit comments