/***************************************************************************
* CopyRight (C) 2009 by SC Crom-Osec SRL *
* Contact: contact@osec.ro *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the Crom Free License as published by *
* the SC Crom-Osec SRL; version 1 of the License *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* Crom Free License for more details. *
* *
* You should have received a copy of the Crom Free License along with *
* this program; if not, write to the contact@osec.ro *
***************************************************************************/
using System;
using System.IO;
using System.Xml;
using DesktopClockWidget;
using NUnit.Core;
using NUnit.Framework;
namespace DesktopClockWidgetTests
{
/// <summary>
/// Test class for XmlClockState
/// </summary>
[TestFixture(Description="Tests for XmlClockState")]
[Category("DesktopClockWidget")]
public class XmlClockStateTests
{
#region Fields.
private Random _valuesGenerator = new Random();
#endregion Fields.
#region Public section.
/// <summary>
/// Set up the tests
/// </summary>
[SetUp]
public void SetUp()
{
Assert.IsTrue(File.Exists("ClockState.xml"), "UnitTesting failure: Settings file \"ClockState.xml\" must be copied in test working folder.");
File.Copy("ClockState.xml", "ClockState.xml.rec", true);
}
/// <summary>
/// Tear down
/// </summary>
[TearDown]
public void TearDown()
{
File.Copy("ClockState.xml.rec", "ClockState.xml", true);
}
/// <summary>
/// Test method for XmlClockState.Left property
/// </summary>
[Test]
public void TestLeft()
{
int expectedValue = GetStateValue<int>("Left");
int actualValue = XmlClockState.Left;
Assert.AreEqual(expectedValue, actualValue, "getLeft property failed.");
int newLeft = _valuesGenerator.Next();
XmlClockState.Left = newLeft;
Assert.AreEqual(newLeft, GetStateValue<int>("Left"), "setLeft property failed.");
Assert.AreEqual(newLeft, XmlClockState.Left, "setLeft property failed.");
}
/// <summary>
/// Test method for XmlClockState.Top property
/// </summary>
[Test]
public void TestTop()
{
int expectedValue = GetStateValue<int>("Top");
int actualValue = XmlClockState.Top;
Assert.AreEqual(expectedValue, actualValue, "getTop property failed.");
int newTop = _valuesGenerator.Next();
XmlClockState.Top = newTop;
Assert.AreEqual(newTop, GetStateValue<int>("Top"), "setTop property failed.");
Assert.AreEqual(newTop, XmlClockState.Top, "setTop property failed.");
}
/// <summary>
/// Test method for XmlClockState.Left property when its tag in ClockState.xml is missing
/// </summary>
[Test]
public void TestMissingLeftTag()
{
using (StreamWriter clockState = new StreamWriter("ClockState.xml"))
{
clockState.Write("<ClockWidget>\r\n\t<Top>300</Top>\r\n</ClockWidget>");
}
GuiTestCaseRunner gui = new GuiTestCaseRunner(UnitTestSettings.GuiProjectPath);
gui.RunGuiTestCase("Validate missing Left tag error", false);
int left = XmlClockState.Left;
gui.WaitToFinish();
Assert.IsFalse(gui.HasErrors, "Message box failed.");
Assert.AreEqual(0, left, "Default value for Left property failed.");
}
/// <summary>
/// Test method for XmlClockState.Top property when its tag in ClockState.xml is missing
/// </summary>
[Test]
public void TestMissingTopTag()
{
using (StreamWriter clockState = new StreamWriter("ClockState.xml"))
{
clockState.Write("<ClockWidget>\r\n\t<Left>1100</Left>\r\n</ClockWidget>");
}
GuiTestCaseRunner gui = new GuiTestCaseRunner(UnitTestSettings.GuiProjectPath);
gui.RunGuiTestCase("Validate missing Top tag error", false);
int top = XmlClockState.Top;
gui.WaitToFinish();
Assert.IsFalse(gui.HasErrors, "Message box failed.");
Assert.AreEqual(0, top, "Default value for Top property failed.");
}
/// <summary>
/// Test the case when "ClockState.xml" file is missing
/// </summary>
[Test]
public void TestMissingClockStateFile()
{
File.Delete("ClockState.xml");
GuiTestCaseRunner gui = new GuiTestCaseRunner(UnitTestSettings.GuiProjectPath);
gui.RunGuiTestCase("Validate missing ClockState file error", false);
int left = XmlClockState.Left;
int top = XmlClockState.Top;
XmlClockState.Left = _valuesGenerator.Next();
XmlClockState.Top = _valuesGenerator.Next();
int left1 = XmlClockState.Left;
int top1 = XmlClockState.Top;
gui.WaitToFinish();
Assert.IsFalse(gui.HasErrors, "Message box failed.");
Assert.AreEqual(0, left, "Default value for Left property failed.");
Assert.AreEqual(0, top, "Default value for Top property failed.");
Assert.AreEqual(0, left1, "Default value for Left property failed.");
Assert.AreEqual(0, top1, "Default value for Top property failed.");
}
/// <summary>
/// Test the case when "ClockState.xml" file is locked
/// </summary>
[Test]
public void TestLockedClockStateFile()
{
using (FileStream fs = new FileStream("ClockState.xml", FileMode.Open, FileAccess.Read, FileShare.None))
{
GuiTestCaseRunner gui = new GuiTestCaseRunner(UnitTestSettings.GuiProjectPath);
gui.RunGuiTestCase("validate locked ClockState file error", false);
int left = XmlClockState.Left;
int top = XmlClockState.Top;
gui.WaitToFinish();
Assert.IsFalse(gui.HasErrors, "Message box failed.");
Assert.AreEqual(0, left, "Default value for Left property failed.");
Assert.AreEqual(0, top, "Default value for Top property failed.");
}
}
#endregion Public section.
#region Local section.
/// <summary>
/// Get state value
/// </summary>
/// <typeparam name="T">type of state value</typeparam>
/// <param name="tag">tag identifying the state location</param>
/// <returns>state value</returns>
private T GetStateValue<T>(string tag)
{
XmlNode stateNode = LoadStateNode(tag);
return (T)Convert.ChangeType(stateNode.InnerText, typeof(T));
}
/// <summary>
/// Set state value
/// </summary>
/// <typeparam name="T">type of state value</typeparam>
/// <param name="tag">tag identifying the state location</param>
/// <param name="stateValue">state value</param>
private void SetStateValue<T>(string tag, T stateValue)
{
XmlNode stateNode = LoadStateNode(tag);
if ((object)stateValue == null)
{
stateNode.InnerText = string.Empty;
}
else
{
stateNode.InnerText = stateValue.ToString();
}
}
/// <summary>
/// Load the program state
/// </summary>
/// <returns>xml document containing the program state</returns>
private XmlDocument LoadTheProgramState()
{
Assert.IsTrue(File.Exists("ClockState.xml"), "UnitTesting failure: Settings file \"ClockState.xml\" must be copied in test working folder.");
XmlDocument stateDoc = new XmlDocument();
stateDoc.Load("ClockState.xml");
return stateDoc;
}
/// <summary>
/// Load the node of given state
/// </summary>
/// <param name="tag">tag identifying the state</param>
/// <returns>node</returns>
private XmlNode LoadStateNode(string tag)
{
XmlDocument stateDoc = LoadTheProgramState();
XmlNode stateNode = stateDoc.DocumentElement.SelectSingleNode(tag);
Assert.IsNotNull(stateNode,
string.Format("UnitTesting failure: Unable to find expected tag [{0}] in clock state file.\r\n.Check if the correct version of program is tested.", tag));
return stateNode;
}
#endregion Local section.
}
}