Configurator.Net: Adding Test project

git-svn-id: https://arducopter.googlecode.com/svn/trunk@1219 f9c3cf11-9bcb-44bc-f272-b75c42450872
This commit is contained in:
mandrolic 2010-12-20 22:14:39 +00:00
parent 075fb61a92
commit 930b09b286
21 changed files with 331 additions and 26 deletions

View File

@ -44,7 +44,7 @@
<Compile Include="PresentationModels\AcroModeConfigVm.cs" />
<Compile Include="PresentationModels\AltitudeHoldConfigVm.cs" />
<Compile Include="PresentationModels\ConfigWithPidsBase.cs" />
<Compile Include="PresentationModels\DelegateCommand.cs" />
<Compile Include="Core\DelegateCommand.cs" />
<Compile Include="PresentationModels\MonitorVm.cs" />
<Compile Include="PresentationModels\MainVm.cs" />
<Compile Include="PresentationModels\FlightDataMonitorVm.cs" />

View File

@ -3,6 +3,8 @@ Microsoft Visual Studio Solution File, Format Version 10.00
# Visual Studio 2008
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArducopterConfigurator", "ArducopterConfigurator.csproj", "{AF26391C-7E45-4401-8984-96139A67FF31}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArducopterConfiguratorTest", "Test\ArducopterConfiguratorTest.csproj", "{36B62E9F-668C-45BF-AD72-CFF0C0A65898}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -13,6 +15,10 @@ Global
{AF26391C-7E45-4401-8984-96139A67FF31}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AF26391C-7E45-4401-8984-96139A67FF31}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AF26391C-7E45-4401-8984-96139A67FF31}.Release|Any CPU.Build.0 = Release|Any CPU
{36B62E9F-668C-45BF-AD72-CFF0C0A65898}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{36B62E9F-668C-45BF-AD72-CFF0C0A65898}.Debug|Any CPU.Build.0 = Debug|Any CPU
{36B62E9F-668C-45BF-AD72-CFF0C0A65898}.Release|Any CPU.ActiveCfg = Release|Any CPU
{36B62E9F-668C-45BF-AD72-CFF0C0A65898}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -8,13 +8,22 @@ using ArducopterConfigurator.PresentationModels;
namespace ArducopterConfigurator
{
public interface IComms
{
event Action<string> LineOfDataReceived;
string CommPort { get; set; }
void Send(string send);
bool Connect();
bool DisConnect();
}
/// <summary>
/// Represents a session of communication with the Arducopter
/// </summary>
/// <remarks>
/// Looks after connection state etc
/// </remarks>
public class CommsSession
public class CommsSession : IComms
{
private readonly SerialPort _sp;
private BackgroundWorker _bgWorker;
@ -103,4 +112,6 @@ namespace ArducopterConfigurator
_sp.Write(send);
}
}
}

View File

@ -4,7 +4,7 @@ namespace ArducopterConfigurator.PresentationModels
{
public class AcroModeConfigVm : ConfigWithPidsBase
{
public AcroModeConfigVm(CommsSession sp) : base(sp)
public AcroModeConfigVm(IComms sp) : base(sp)
{
PropsInUpdateOrder = new[]
{

View File

@ -2,7 +2,7 @@ namespace ArducopterConfigurator.PresentationModels
{
public class AltitudeHoldConfigVm : MonitorVm
{
public AltitudeHoldConfigVm(CommsSession sp)
public AltitudeHoldConfigVm(IComms sp)
: base(sp)
{
PropsInUpdateOrder = new[] { "P", "I", "D", };
@ -14,9 +14,9 @@ namespace ArducopterConfigurator.PresentationModels
public ICommand RefreshCommand { get; private set; }
public ICommand UpdateCommand { get; private set; }
public float P { get; protected set; }
public float I { get; protected set; }
public float D { get; protected set; }
public float P { get; set; }
public float I { get; set; }
public float D { get; set; }
private void RefreshValues()

View File

@ -6,7 +6,7 @@ namespace ArducopterConfigurator.PresentationModels
{
public class CalibrationOffsetsDataVm : MonitorVm
{
public CalibrationOffsetsDataVm(CommsSession sp) : base(sp)
public CalibrationOffsetsDataVm(IComms sp) : base(sp)
{
PropsInUpdateOrder = new[]
{

View File

@ -19,7 +19,7 @@ namespace ArducopterConfigurator.PresentationModels
public float YawD { get; set; }
public ConfigWithPidsBase(CommsSession sp) : base(sp)
public ConfigWithPidsBase(IComms sp) : base(sp)
{
}

View File

@ -7,7 +7,7 @@ namespace ArducopterConfigurator.PresentationModels
{
public class FlightDataVm : MonitorVm
{
public FlightDataVm(CommsSession _sp) : base(_sp)
public FlightDataVm(IComms _sp) : base(_sp)
{
}

View File

@ -9,17 +9,17 @@ namespace ArducopterConfigurator.PresentationModels
{
public class MainVm : NotifyProperyChangedBase, IPresentationModel
{
private readonly CommsSession _session;
private readonly IComms _session;
private bool _isConnected;
private MonitorVm _selectedVm;
private string _selectedPort;
private string _apmVersion;
private System.Windows.Forms.Timer _connectionAttemptsTimer;
private Timer _connectionAttemptsTimer;
private SessionStates _connectionState;
public MainVm(CommsSession session)
public MainVm(IComms session)
{
_session = session;
_session.LineOfDataReceived += _session_LineOfDataReceived;
@ -157,7 +157,6 @@ namespace ArducopterConfigurator.PresentationModels
_session.Send("!");
}
public void Disconnect()
{
_session.Send("X");
@ -165,9 +164,6 @@ namespace ArducopterConfigurator.PresentationModels
ConnectionState = SessionStates.Disconnected;
}
public string ApmVersion
{
get { return _apmVersion; }

View File

@ -12,11 +12,11 @@ namespace ArducopterConfigurator
/// </remarks>
public abstract class MonitorVm : NotifyProperyChangedBase, IPresentationModel
{
private CommsSession _sp;
private IComms _sp;
protected bool isActive;
protected string[] PropsInUpdateOrder;
protected MonitorVm(CommsSession sp)
protected MonitorVm(IComms sp)
{
_sp = sp;
_sp.LineOfDataReceived += sp_LineOfDataReceived;

View File

@ -2,7 +2,7 @@ namespace ArducopterConfigurator.PresentationModels
{
public class MotorCommandsVm : MonitorVm
{
public MotorCommandsVm(CommsSession _sp)
public MotorCommandsVm(IComms _sp)
: base(_sp)
{
}

View File

@ -2,7 +2,7 @@ namespace ArducopterConfigurator.PresentationModels
{
public class PositionHoldConfigVm : ConfigWithPidsBase
{
public PositionHoldConfigVm(CommsSession sp)
public PositionHoldConfigVm(IComms sp)
: base(sp)
{
PropsInUpdateOrder = new[]

View File

@ -7,7 +7,7 @@ namespace ArducopterConfigurator.PresentationModels
{
private string _text;
public SerialMonitorVm(CommsSession _sp) : base(_sp)
public SerialMonitorVm(IComms _sp) : base(_sp)
{
_sp.LineOfDataReceived += new Action<string>(_sp_DataReceived);

View File

@ -2,7 +2,7 @@ namespace ArducopterConfigurator.PresentationModels
{
public class StableModeConfigVm : ConfigWithPidsBase
{
public StableModeConfigVm(CommsSession sp)
public StableModeConfigVm(IComms sp)
: base(sp)
{
PropsInUpdateOrder = new[]
@ -28,9 +28,9 @@ namespace ArducopterConfigurator.PresentationModels
public ICommand UpdateCommand { get; private set; }
public float KPrate { get; private set; }
public float KPrate { get; set; }
public bool MagnetometerEnable { get; private set; }
public bool MagnetometerEnable { get; set; }
private void RefreshValues()
{

View File

@ -5,7 +5,7 @@ namespace ArducopterConfigurator.PresentationModels
{
public class TransmitterChannelsVm : MonitorVm
{
public TransmitterChannelsVm(CommsSession sp) : base(sp)
public TransmitterChannelsVm(IComms sp) : base(sp)
{
PropsInUpdateOrder = new[]
{

View File

@ -0,0 +1,48 @@
using ArducopterConfigurator.PresentationModels;
using NUnit.Framework;
namespace ArducopterConfiguratorTest
{
[TestFixture]
public class AltitudeHoldVmTest
{
private FakeComms _fakeComms;
private AltitudeHoldConfigVm _vm;
[SetUp]
public void Setup()
{
_fakeComms = new FakeComms();
_vm = new AltitudeHoldConfigVm(_fakeComms);
}
[Test]
public void UpdateStringSentIsCorrect()
{
_vm.P = 1.0F;
_vm.I = 2.0F;
_vm.D = 3.0F;
_vm.UpdateCommand.Execute(null);
Assert.AreEqual(1, _fakeComms.SentItems.Count);
Assert.AreEqual("E1;3;2", _fakeComms.SentItems[0]);
}
[Test]
// For whatever reason, for Altitude the properties are sent in P, D ,I
// order, but received in P,I,D order
public void UpdateStringReceivedPopulatesValuesCorrectly()
{
_fakeComms.FireLineRecieve("F1;2;3");
Assert.AreEqual(1f, _vm.P);
Assert.AreEqual(2f, _vm.I);
Assert.AreEqual(3f, _vm.D);
}
}
}

View File

@ -0,0 +1,62 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.30729</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{36B62E9F-668C-45BF-AD72-CFF0C0A65898}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ArducopterConfiguratorTest</RootNamespace>
<AssemblyName>ArducopterConfiguratorTest</AssemblyName>
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="nunit.framework, Version=2.5.9.10348, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\Lib3\nunit.framework.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="AltitudeHoldVmTest.cs" />
<Compile Include="MainVmTests.cs" />
<Compile Include="MotorCommandsVmTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ArducopterConfigurator.csproj">
<Project>{AF26391C-7E45-4401-8984-96139A67FF31}</Project>
<Name>ArducopterConfigurator</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View File

@ -0,0 +1,123 @@
using System;
using System.Collections.Generic;
using System.Text;
using ArducopterConfigurator;
using ArducopterConfigurator.PresentationModels;
using NUnit.Framework;
namespace ArducopterConfiguratorTest
{
internal class FakeComms : IComms
{
public event Action<string> LineOfDataReceived;
public string CommPort { get; set; }
public List<string> SentItems = new List<string>();
public void Send(string send)
{
SentItems.Add(send);
}
public bool Connect()
{
return true;
}
public bool DisConnect()
{
return true;
}
public void FireLineRecieve(string s)
{
if (LineOfDataReceived != null)
LineOfDataReceived(s);
}
}
[TestFixture]
public class MainVmTests
{
private FakeComms _fakeComms;
private MainVm _vm;
[SetUp]
public void Setup()
{
_fakeComms = new FakeComms();
_vm = new MainVm(_fakeComms);
}
[Test]
public void StateInitiallyDisconnected()
{
Assert.AreEqual(MainVm.SessionStates.Disconnected,_vm.ConnectionState);
}
}
[TestFixture]
public class StableModeConfigVmTest
{
private FakeComms _fakeComms;
private StableModeConfigVm _vm;
[SetUp]
public void Setup()
{
_fakeComms = new FakeComms();
_vm = new StableModeConfigVm(_fakeComms);
}
[Test]
public void UpdateStringSentIsCorrect()
{
_vm.PitchP = 1.0F;
_vm.PitchI = 2.0F;
_vm.PitchD = 3.0F;
_vm.RollP = 5.0F;
_vm.RollI = 6.0F;
_vm.RollD = 7.0F;
_vm.YawP = 8.0F;
_vm.YawI = 9.0F;
_vm.YawD = 10.0F;
_vm.MagnetometerEnable = true;
_vm.KPrate = 4.0F;
_vm.UpdateCommand.Execute(null);
Assert.AreEqual(1,_fakeComms.SentItems.Count);
//A[KP Quad Roll];[KI Quad Roll];[KP RATE ROLL];
// [KP Quad Pitch];[KI Quad Pitch];[KP RATE PITCH];
// [KP Quad Yaw];[KI Quad Yaw];[KP Rate Yaw];
// [KP Rate];[Magneto]
Assert.AreEqual("A5;6;7;1;2;3;8;9;10;4;1", _fakeComms.SentItems[0]);
}
[Test]
public void UpdateStringReceivedPopulatesValuesCorrectly()
{
_fakeComms.FireLineRecieve("B5;6;7;1;2;3;8;9;10;4;1");
Assert.AreEqual(1.0f,_vm.PitchP);
Assert.AreEqual(2f,_vm.PitchI);
Assert.AreEqual(3f,_vm.PitchD);
Assert.AreEqual(5f,_vm.RollP);
Assert.AreEqual(6f,_vm.RollI);
Assert.AreEqual(7f,_vm.RollD);
Assert.AreEqual(8f,_vm.YawP);
Assert.AreEqual(9f,_vm.YawI);
Assert.AreEqual(10f,_vm.YawD);
Assert.AreEqual(1f,_vm.MagnetometerEnable);
Assert.AreEqual(4f,_vm.KPrate);
}
}
}

View File

@ -0,0 +1,23 @@
using ArducopterConfigurator.PresentationModels;
using NUnit.Framework;
namespace ArducopterConfiguratorTest
{
[TestFixture]
public class MotorCommandsVmTest
{
private FakeComms _fakeComms;
private MotorCommandsVm _vm;
[SetUp]
public void Setup()
{
_fakeComms = new FakeComms();
_vm = new MotorCommandsVm(_fakeComms);
}
}
}

View File

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("ArducopterConfiguratorTest")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("TOSHIBA")]
[assembly: AssemblyProduct("ArducopterConfiguratorTest")]
[assembly: AssemblyCopyright("Copyright © TOSHIBA 2010")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("19d172a3-8241-459a-bee7-3318a20fce16")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]