Skip to content

Commit 0002e75

Browse files
committed
Add a little starting point thread safety and test coverage for MultiSelectorBehaviours and TwoListSynchronizer.
1 parent a631d39 commit 0002e75

14 files changed

+769
-42
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
namespace PrimS.SelectedItemsSynchronizer.CiTests
2+
{
3+
using FluentAssertions;
4+
using Microsoft.VisualStudio.TestTools.UnitTesting;
5+
using System;
6+
using System.Collections;
7+
using System.Collections.Generic;
8+
using System.Collections.ObjectModel;
9+
using System.Linq;
10+
using System.Windows.Controls;
11+
12+
[TestClass]
13+
public class MultiSelectorBehavioursTests
14+
{
15+
List<string> names;
16+
ObservableCollection<string> selectedNames;
17+
ListView listView;
18+
19+
[TestInitialize]
20+
public void TestInitialize()
21+
{
22+
names = new List<string>() { "Abraham", "Lincoln", "James", "Buchanan" };
23+
selectedNames = new ObservableCollection<string>();
24+
listView = new ListView();
25+
listView.ItemsSource = names;
26+
listView.SelectionMode = SelectionMode.Extended;
27+
MultiSelectorBehaviours.SetSynchronizedSelectedItems(listView, (IList)selectedNames);
28+
}
29+
30+
[TestMethod]
31+
public void InitialiseToNoSelection()
32+
{
33+
this.selectedNames.Count().Should().Be(0);
34+
}
35+
36+
[TestMethod]
37+
public void ShouldSynchroniseListViewSelectAll()
38+
{
39+
// Act
40+
this.listView.SelectAll();
41+
42+
// Assert
43+
this.selectedNames.Count().Should().Be(this.names.Count());
44+
}
45+
46+
[TestMethod]
47+
public void ShouldSynchroniseListViewSetSelectedIndex()
48+
{
49+
// Act
50+
this.listView.SelectedIndex = 0;
51+
52+
// Assert
53+
this.selectedNames.Count().Should().Be(1);
54+
}
55+
56+
[TestMethod]
57+
public void ShouldSynchroniseListViewSetSelectedItem()
58+
{
59+
//Arrange
60+
Random random = new Random(DateTime.Now.Millisecond);
61+
object itemToSelect = this.listView.Items.GetItemAt(random.Next(this.names.Count));
62+
63+
// Act
64+
this.listView.SelectedItem = itemToSelect;
65+
66+
// Assert
67+
this.selectedNames.Count().Should().Be(1);
68+
}
69+
70+
[TestMethod]
71+
public void ShouldSynchroniseListViewAddSingleSelectedItem()
72+
{
73+
//Arrange
74+
Random random = new Random(DateTime.Now.Millisecond);
75+
object itemToSelect = this.listView.Items.GetItemAt(random.Next(this.names.Count));
76+
77+
// Act
78+
this.listView.SelectedItems.Add(itemToSelect);
79+
80+
// Assert
81+
this.selectedNames.Count().Should().Be(1);
82+
}
83+
84+
[TestMethod]
85+
public void ShouldSynchroniseListViewAddMultipleSelectedItems()
86+
{
87+
// Act
88+
this.listView.SelectedItems.Add(this.listView.Items.GetItemAt(0));
89+
this.listView.SelectedItems.Add(this.listView.Items.GetItemAt(1));
90+
91+
// Assert
92+
this.selectedNames.ShouldBeEquivalentTo(this.listView.SelectedItems, opt => opt.WithStrictOrdering());
93+
}
94+
95+
[TestMethod]
96+
public void ShouldSynchroniseListAddMultipleSelectedItems()
97+
{
98+
// Act
99+
this.selectedNames.Add(this.names.First());
100+
this.selectedNames.Add(this.names.Last());
101+
102+
// Assert
103+
this.listView.SelectedItems.ShouldBeEquivalentTo(this.selectedNames, opt => opt.WithStrictOrdering());
104+
}
105+
106+
[TestMethod]
107+
public void ShouldNotSynchroniseListAddMultipleSelectedItems()
108+
{
109+
//Arrange
110+
ObservableCollection<string> secondSelectedNames = new ObservableCollection<string>();
111+
112+
// Act
113+
MultiSelectorBehaviours.SetSynchronizedSelectedItems(listView, (IList)secondSelectedNames);
114+
this.selectedNames.Add(this.names.First());
115+
this.selectedNames.Add(this.names.Last());
116+
117+
// Assert
118+
this.listView.SelectedItems.Should().NotBeEquivalentTo(this.selectedNames);
119+
}
120+
121+
[TestMethod]
122+
public void ShouldSynchroniseChangedListAddMultipleSelectedItems()
123+
{
124+
//Arrange
125+
ObservableCollection<string> secondSelectedNames = new ObservableCollection<string>();
126+
127+
// Act
128+
MultiSelectorBehaviours.SetSynchronizedSelectedItems(listView, (IList)secondSelectedNames);
129+
secondSelectedNames.Add(this.names.First());
130+
secondSelectedNames.Add(this.names.Last());
131+
132+
// Assert
133+
this.listView.SelectedItems.ShouldBeEquivalentTo(secondSelectedNames);
134+
}
135+
}
136+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6+
<ProjectGuid>{918FE624-1E12-48BE-A3F0-FC1B5CF1412F}</ProjectGuid>
7+
<OutputType>Library</OutputType>
8+
<AppDesignerFolder>Properties</AppDesignerFolder>
9+
<RootNamespace>PrimS.SelectedItemsSynchronizer.CiTests</RootNamespace>
10+
<AssemblyName>PrimS.SelectedItemsSynchronizer.CiTests</AssemblyName>
11+
<TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
14+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
15+
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
16+
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
17+
<IsCodedUITest>False</IsCodedUITest>
18+
<TestProjectType>UnitTest</TestProjectType>
19+
</PropertyGroup>
20+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
21+
<DebugSymbols>true</DebugSymbols>
22+
<DebugType>full</DebugType>
23+
<Optimize>false</Optimize>
24+
<OutputPath>bin\Debug\</OutputPath>
25+
<DefineConstants>DEBUG;TRACE</DefineConstants>
26+
<ErrorReport>prompt</ErrorReport>
27+
<WarningLevel>4</WarningLevel>
28+
</PropertyGroup>
29+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
30+
<DebugType>pdbonly</DebugType>
31+
<Optimize>true</Optimize>
32+
<OutputPath>bin\Release\</OutputPath>
33+
<DefineConstants>TRACE</DefineConstants>
34+
<ErrorReport>prompt</ErrorReport>
35+
<WarningLevel>4</WarningLevel>
36+
</PropertyGroup>
37+
<ItemGroup>
38+
<Reference Include="FluentAssertions">
39+
<HintPath>..\packages\FluentAssertions.3.3.0.20\lib\net45\FluentAssertions.dll</HintPath>
40+
</Reference>
41+
<Reference Include="FluentAssertions.Core">
42+
<HintPath>..\packages\FluentAssertions.3.3.0.20\lib\net45\FluentAssertions.Core.dll</HintPath>
43+
</Reference>
44+
<Reference Include="PresentationCore" />
45+
<Reference Include="PresentationFramework" />
46+
<Reference Include="System" />
47+
<Reference Include="System.Xaml" />
48+
<Reference Include="System.Xml" />
49+
<Reference Include="System.Xml.Linq" />
50+
<Reference Include="WindowsBase" />
51+
</ItemGroup>
52+
<Choose>
53+
<When Condition="('$(VisualStudioVersion)' == '10.0' or '$(VisualStudioVersion)' == '') and '$(TargetFrameworkVersion)' == 'v3.5'">
54+
<ItemGroup>
55+
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
56+
</ItemGroup>
57+
</When>
58+
<Otherwise>
59+
<ItemGroup>
60+
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework" />
61+
</ItemGroup>
62+
</Otherwise>
63+
</Choose>
64+
<ItemGroup>
65+
<Compile Include="MultiSelectorBehavioursTests.cs" />
66+
<Compile Include="TwoListSynchronizerDeletionBehaviour.cs" />
67+
<Compile Include="TwoListSynchronizerInitialization.cs" />
68+
<Compile Include="TwoListSynchronizerInsertionBehaviour.cs" />
69+
<Compile Include="TwoListSynchronizerMoveBehaviour.cs" />
70+
<Compile Include="TwoListSynchronizerReplacementBehaviour.cs" />
71+
<Compile Include="TwoListSynchronizerAdditionBehaviour.cs" />
72+
<Compile Include="Properties\AssemblyInfo.cs" />
73+
</ItemGroup>
74+
<ItemGroup>
75+
<ProjectReference Include="..\SelectedItemsSynchronizer\PrimS.SelectedItemsSynchronizer.csproj">
76+
<Project>{6fd3314f-9a43-4fcf-a073-6c97ca2d1e81}</Project>
77+
<Name>PrimS.SelectedItemsSynchronizer</Name>
78+
</ProjectReference>
79+
</ItemGroup>
80+
<ItemGroup>
81+
<None Include="packages.config" />
82+
</ItemGroup>
83+
<Choose>
84+
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
85+
<ItemGroup>
86+
<Reference Include="Microsoft.VisualStudio.QualityTools.CodedUITestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
87+
<Private>False</Private>
88+
</Reference>
89+
<Reference Include="Microsoft.VisualStudio.TestTools.UITest.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
90+
<Private>False</Private>
91+
</Reference>
92+
<Reference Include="Microsoft.VisualStudio.TestTools.UITest.Extension, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
93+
<Private>False</Private>
94+
</Reference>
95+
<Reference Include="Microsoft.VisualStudio.TestTools.UITesting, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
96+
<Private>False</Private>
97+
</Reference>
98+
</ItemGroup>
99+
</When>
100+
</Choose>
101+
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
102+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
103+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
104+
Other similar extension points exist, see Microsoft.Common.targets.
105+
<Target Name="BeforeBuild">
106+
</Target>
107+
<Target Name="AfterBuild">
108+
</Target>
109+
-->
110+
</Project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("PrimS.SelectedItemsSynchronizer.CiTests")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("PrimS.SelectedItemsSynchronizer.CiTests")]
13+
[assembly: AssemblyCopyright("Copyright © 2014")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("5726167d-d6a5-4e8b-930f-e5d3009afd66")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
namespace PrimS.SelectedItemsSynchronizer.CiTests
2+
{
3+
using System;
4+
using System.Collections.ObjectModel;
5+
using System.Linq;
6+
using FluentAssertions;
7+
using Microsoft.VisualStudio.TestTools.UnitTesting;
8+
9+
[TestClass]
10+
public class TwoListSynchronizerAdditionBehaviour
11+
{
12+
private ObservableCollection<string> masterList;
13+
private ObservableCollection<string> targetList;
14+
private TwoListSynchronizer sut;
15+
16+
[ClassInitialize]
17+
public static void ClassInitialize(TestContext context)
18+
{
19+
}
20+
21+
[TestInitialize]
22+
public void TestInitialize()
23+
{
24+
this.masterList = new ObservableCollection<string>();
25+
this.targetList = new ObservableCollection<string>();
26+
this.sut = new TwoListSynchronizer(this.masterList, this.targetList);
27+
}
28+
29+
[TestMethod]
30+
public void ShouldNotSynchroniseAddition()
31+
{
32+
// Act
33+
this.masterList.Add("testString");
34+
35+
// Assert
36+
this.targetList.Count().Should().Be(0);
37+
}
38+
39+
[TestMethod]
40+
public void ShouldSynchroniseAddition()
41+
{
42+
// Arrange
43+
this.sut.StartSynchronizing();
44+
45+
// Act
46+
this.masterList.Add("testString");
47+
48+
// Assert
49+
this.targetList.Count().Should().Be(1);
50+
}
51+
52+
[TestMethod]
53+
public void ShouldSynchroniseAdditionOnTarget()
54+
{
55+
// Arrange
56+
this.sut.StartSynchronizing();
57+
58+
// Act
59+
this.targetList.Add("testString");
60+
61+
// Assert
62+
this.masterList.Count().Should().Be(1);
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)