using System; using System.Collections.Generic; using System.Linq; using System.Text; using NUnit.Framework; using RemoteController; using RemoteController.ReceiverClasses; using System.Collections; namespace NUnitTest { [TestFixture] public class LibraryLightTest { [Test] public void A_Turn_Light_On() { Light l = new Light(); Assert.False(l.isLightOn, "New light object is not set to off by default"); l.on(); Assert.True(l.isLightOn, "Light was not turned on"); } } [TestFixture] public class LibraryCeilingFanTest { [Test] public void A_CeilingFanOffUponInstantiation() { CeilingFan c = new CeilingFan(); Assert.False(c.isFanOn, "Fan is instantiated and with on status"); } [Test] public void B_Turn_On_Fan() { CeilingFan c = new CeilingFan(); c.on(); Assert.True(c.isFanOn, "Fan was not turned on"); } [Test] public void C_Turn_Off_Fan() { CeilingFan c = new CeilingFan(); c.on(); Assert.True(c.isFanOn, "Fan was not turned on"); c.off(); Assert.False(c.isFanOn, "Fan was turned on but not turned off"); } } [TestFixture] public class LibraryGarageDoorTest { [Test] public void A_Garage_door_Is_Instantiated_Closed() { GarageDoor gd = new GarageDoor(); Assert.False(gd.isOpen, "Garage Door is initialised as open"); } [Test] public void B_Garage_Door_Opened_Then_Closed() { GarageDoor gd = new GarageDoor(); gd.open(); Assert.True(gd.isOpen, "Garage door was not opened"); gd.close(); Assert.False(gd.isOpen, "Garage door was not closed"); } } [TestFixture] public class LibraryStereoTest { [Test] public void A_Turn_on_Stereo() { Stereo s = new Stereo(); s.on(); Assert.True(s.isOn, "Stereo not turned on"); } [Test] public void B_Turn_On_And_Set_Volume() { Stereo s = new Stereo(); s.on(); s.setCD(); Assert.True(s.isCDSet, "CD is not set"); } [Test] public void C_Set_Volume() { Stereo s = new Stereo(); s.on(); Assert.AreEqual(s.Volume, 0, "Volume was initalised as 0"); s.setVolume(5); Assert.AreEqual(s.Volume, 5, "Volume was not set to 5"); } [Test] [ExpectedException(typeof(ArgumentException))] public void D_Out_Of_Range_Volume() { var s = new Stereo(); s.on(); s.setVolume(-1); s.setVolume(12); } [Test] [ExpectedException(typeof(StereoIsOffException))] public void E_Test_Off_Method_Prevents_Other_Methods_Calls() { Stereo s = new Stereo(); Assert.False(s.isOn, "Stereo was on"); s.setCD(); s.on(); s.setVolume(11); } } [TestFixture] public class CommandsTest { private RemoteControl rc; private LightOnCommand lightOnCmd; private LightOffCommand lightOffCmd; private Light l; private CeilingFan cfan; private CeilingFanOnCommand cfanOnCmd; private CeilingFanOffCommand cfanOffCmd; [SetUp] public void Init() { rc = new RemoteControl("My Remote Control"); l = new Light(); lightOnCmd = new LightOnCommand(l); lightOffCmd = new LightOffCommand(l); cfan = new CeilingFan(); cfanOnCmd = new CeilingFanOnCommand(cfan); cfanOffCmd = new CeilingFanOffCommand(cfan); } [Test] public void A_Test_LightOnCommand() { rc.setCommand(0, lightOnCmd, lightOffCmd); Assert.False(l.isLightOn, "Light was already on"); rc.onButtonWasPushed(0); Assert.True(l.isLightOn, "Light was not turned on by Command invocation"); } [Test] public void B_Test_Undo_LightOnCommand() { rc.setCommand(0, lightOnCmd, lightOffCmd); Assert.False(l.isLightOn, "Light was already on"); rc.onButtonWasPushed(0); Assert.True(l.isLightOn, "Light was not turned on by Command invocation"); rc.undoButtonWasPushed(); Assert.False(l.isLightOn, "Undo command did not turn off light"); } [Test] public void C_Test_LightOffCommand() { Assert.False(l.isLightOn, "Light was already on"); rc.setCommand(0, lightOnCmd, lightOffCmd); rc.onButtonWasPushed(1); Assert.False(l.isLightOn, "call to invoke an undefined command changed the state of the Light Object in slot 0"); rc.offButtonWasPushed(1); Assert.False(l.isLightOn, "call to invoke an undefined command changed the state of the Light Object in slot 0"); rc.onButtonWasPushed(0); Assert.True(l.isLightOn, "call to invoke lightOn command light did not turn on light"); rc.offButtonWasPushed(0); Assert.False(l.isLightOn, "call to invoke light off command did not turn light off"); } [Test] public void D_Ceiling_Fan_On_Off() { rc.setCommand(0, cfanOnCmd, cfanOffCmd); Assert.False(cfan.isFanOn, "Fan was already on"); rc.onButtonWasPushed(0); Assert.True(cfan.isFanOn, "Fan was not on"); rc.offButtonWasPushed(0); Assert.False(cfan.isFanOn, "Fan was still on"); rc.onButtonWasPushed(0); Assert.True(cfan.isFanOn, "Fan was not turned on"); rc.undoButtonWasPushed(); Assert.False(cfan.isFanOn, "Fan was not not turned off by undo method"); } } [TestFixture] public class CommandStereoTest { private RemoteControl rc; private Stereo s; private StereoOnWithCDCommand stereoOnWithCDCmd; private StereoOffCommand stereoOffCmd; [SetUp] public void Init() { rc = new RemoteControl("Stereo Remote Control"); s = new Stereo("Sony"); stereoOnWithCDCmd = new StereoOnWithCDCommand(s); stereoOffCmd = new StereoOffCommand(s); } [Test] public void A_Test_StereoOn_Command() { rc.setCommand(0, stereoOnWithCDCmd, stereoOffCmd); rc.onButtonWasPushed(0); Assert.True(s.isOn, "Stereo was not on"); Assert.True(s.isCDSet, "CD was not set"); rc.offButtonWasPushed(0); Assert.False(s.isOn, "Stereo is still on"); } [Test] public void B_Test_Stereo_Undo() { rc.setCommand(0, stereoOnWithCDCmd, stereoOffCmd); rc.onButtonWasPushed(0); Assert.True(s.isOn); rc.undoButtonWasPushed(); Assert.False(s.isOn, "Stereo is on"); } } [TestFixture] public class HouseTest { private House h; private RemoteControl rc; private ArrayList house_appliances; [SetUp] public void Init() { h = new House(); house_appliances = h.Appliances; } [Test] //[Ignore("This will be called in the database fixture")] public void AA_Test_Database_Is_Valid() { ArrayList a = h.CommandList; Assert.That(a.Count > 0, "No commands were returned. Check Database is populated"); } [Test] public void A_Create_Default_House() { //h.Set_Default_RemoteControl_Commands(); AA_Test_Database_Is_Valid(); //add up to seven cmds to each remote control slot, //for each of the commands available for (int i = 0; i < h.CommandList.Count && i < 7; i++) { h.setCommand(i, i); } //for each test grab the appliances and test against them IEnumerable ienumGD = h.Appliances.OfType(); IEnumerable ienumLight = h.Appliances.OfType(); Assert.That(ienumGD.Count() > 0, "There was no Garage Door object in Appliance List"); foreach (var x in ienumGD) { var y = (GarageDoor)x; Assert.False(y.isOpen, "Garage Door was open"); } Assert.That(ienumLight.Count() > 0, "No Light object in Appliance list"); foreach (var x in ienumLight) { var light = (Light)x; Assert.False(light.isLightOn, "Light was turned on at construction"); } //this will be the light cmd h.On_Button_Pushed(0); //check that only the light command is being called, not the garage door command ienumGD = h.Appliances.OfType(); foreach (var garageDoor in ienumGD) { var gd = (GarageDoor)garageDoor; Assert.False(gd.isOpen, "Garage Door opened when light command called"); } ienumLight = h.Appliances.OfType(); Assert.That(ienumLight.Count() > 0, "No Light object in Appliance list"); foreach (var x in ienumLight) { var light = (Light)x; Assert.True(light.isLightOn, "Light was not turned on after button press"); } h.Off_Button_Pushed(0); ienumLight = h.Appliances.OfType(); Assert.That(ienumLight.Count() > 0, "No Light object in Appliance list"); foreach (var x in ienumLight) { var light = (Light)x; Assert.False(light.isLightOn, "Light is still on after off button press"); } //turn light back on h.On_Button_Pushed(0); ienumLight = h.Appliances.OfType(); Assert.That(ienumLight.Count() > 0, "No Light object in Appliance list"); foreach (var x in ienumLight) { var light = (Light)x; Assert.True(light.isLightOn, "Light was not turned on after on button press"); } h.undo_button_pushed(); //light shoudl be off now ienumLight = h.Appliances.OfType(); Assert.That(ienumLight.Count() > 0, "No Light object in Appliance list"); foreach (var x in ienumLight) { var light = (Light)x; Assert.False(light.isLightOn, "Light is still on after off button press"); } } [Test] public void B_Create_Custom_Commands() { h.setCommand(5, 0); //slot 5 set to command index 0, light control h.setCommand(6, 2); //slot 6 set to cmd indx 2, garage door control IEnumerable ienumGD = h.Appliances.OfType(); IEnumerable ienumLight = h.Appliances.OfType(); Assert.That(ienumGD.Count() > 0, "There was no Garage Door object in Appliance List"); foreach (var x in ienumGD) { var y = (GarageDoor)x; Assert.False(y.isOpen, "Garage Door was open upon construction"); } Assert.That(ienumLight.Count() > 0, "No Light object in Appliance list"); foreach (var x in ienumLight) { var light = (Light)x; Assert.False(light.isLightOn, "Light was turned on at construction"); } //Turn on the light h.On_Button_Pushed(5); ienumLight = h.Appliances.OfType(); Assert.That(ienumLight.Count() > 0, "No Light object in Appliance list"); foreach (var x in ienumLight) { var light = (Light)x; Assert.True(light.isLightOn, "Light was not turned on after button press"); } //turn on garage door h.On_Button_Pushed(6); ienumGD = h.Appliances.OfType(); foreach (var garageDoor in ienumGD) { var gd = (GarageDoor)garageDoor; Assert.True(gd.isOpen, "Garage Door not opened"); } h.undo_button_pushed(); ienumGD = h.Appliances.OfType(); foreach (var garageDoor in ienumGD) { var gd = (GarageDoor)garageDoor; Assert.False(gd.isOpen, "Undo comd did not close Garage Door"); } //turn light off h.Off_Button_Pushed(5); ienumLight = h.Appliances.OfType(); Assert.That(ienumLight.Count() > 0, "No Light object in Appliance list"); foreach (var x in ienumLight) { var light = (Light)x; Assert.False(light.isLightOn, "Light was not turned off after button press"); } } } }