// Copyright Keysight Technologies 2012-2019 // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, you can obtain one at http://mozilla.org/MPL/2.0/. using System; using System.Collections.Generic; using System.Linq; using System.Threading; using OpenTap.Plugins.BasicSteps; using NUnit.Framework; using OpenTap.Engine.UnitTests.TestTestSteps; namespace OpenTap.Engine.UnitTests { public class ReadInputStep : TestStep { public Input Input { get; set; } = new Input(); public override void Run() { Log.Debug("Input Value: {0}", Input.Value); } } [TestFixture] public class InputTest { [Test] public void InputBasicTests() { var prop = TypeData.FromType(typeof(DelayStep)).GetMember(nameof(DelayStep.DelaySecs)); DelayStep delay = new DelayStep(); Input secs = new Input() { Step = delay, Property = prop}; var plan = new TestPlan(); plan.ChildTestSteps.Add(delay); delay.DelaySecs = 2; Assert.AreEqual(secs.Value, delay.DelaySecs); Input secs2 = new Input() { Step = delay, Property = prop }; Assert.AreEqual(secs, secs2); Input secs3 = new Input() { Step = delay, Property = null }; Input secs4 = new Input() { Step = null, Property = prop }; Input secs5 = new Input() { Step = null, Property = null }; Input secs6 = new Input() { Step = delay, PropertyName = prop.Name}; Assert.IsFalse(secs3 == secs4); Assert.IsFalse(secs4 == secs5); Assert.IsFalse(secs3 == secs5); Assert.IsTrue(secs == secs2); Assert.IsTrue(secs == secs6); { // test serialize plan.ChildTestSteps.Add(new ReadInputStep() { Input = secs }); var planxml = new TapSerializer().SerializeToString(plan); TestPlan plan2 = (TestPlan)new TapSerializer().DeserializeFromString(planxml); var step2 = plan2.ChildTestSteps[1] as ReadInputStep; Assert.IsTrue(step2.Input.Step == plan2.ChildTestSteps[0]); } } [Test] public void NullInputTest() { Input a = null; Input b = null; Assert.IsTrue(a == b); a = new Input(){}; Assert.IsTrue(a != b); b = new Input(){}; var prop = TypeData.FromType(typeof(DelayStep)).GetMember(nameof(DelayStep.DelaySecs)); DelayStep delay = new DelayStep(); Assert.IsTrue(a == b); a.Step = delay; a.Property = prop; Assert.IsTrue(a != b); b.Step = delay; b.Property = prop; Assert.IsTrue(a == b); } [Test] public void DeletedInputTest() { var plan = new TestPlan(); var delayStep = new DelayStep(); var ifStep = new IfStep(); plan.ChildTestSteps.AddRange([delayStep, ifStep]); var a = AnnotationCollection.Annotate(ifStep); var mem = a.GetMember(nameof(ifStep.InputVerdict)); var sv = mem.Get(); { // Verify the value is initally not set Assert.That(sv.Value, Is.EqualTo("None")); Assert.That(ifStep.InputVerdict.ToString(), Is.EqualTo("NotSet")); } { // Verify that the string value is updated ifStep.InputVerdict.Step = delayStep; ifStep.InputVerdict.Property = TypeData.GetTypeData(delayStep).GetMember(nameof(delayStep.Verdict)); a.Read(); Assert.That(sv.Value, Is.EqualTo("Verdict from Delay")); Assert.That(ifStep.InputVerdict.ToString(), Is.EqualTo("NotSet")); } { // Delete the target step from the test plan and verify the string value is unset plan.ChildTestSteps.Remove(delayStep); a.Read(); Assert.That(sv.Value, Is.EqualTo("None")); Assert.That(ifStep.InputVerdict.ToString(), Is.EqualTo("NotSet")); } } [Test] public void TestSerializeInput() { var step = new HandleInputStep(); var plan = new TestPlan(); plan.Steps.Add(step); var s = new TapSerializer(); var xml = s.SerializeToString(plan); Assert.IsFalse(s.Errors.Any()); var deserialized = s.DeserializeFromString(xml); Assert.IsFalse(s.Errors.Any()); } [Test] public void UnconfiguredInputToStringTest() { var x = new Input(); Assert.DoesNotThrow(() => x.ToString()); Assert.AreEqual("", x.ToString()); } /// /// OpenTAP issue: #666 /// [Test] public void ParallelIfVerdict() { var plan = new TestPlan(); var par = new ParallelStep(); var seq = new SequenceStep(); var del1 = new DelayStep {DelaySecs = 0}; var ifVerdict = new IfStep(); var del2 = new DelayStep {DelaySecs = 0}; plan.Steps.Add(par); par.ChildTestSteps.Add(seq); seq.ChildTestSteps.Add(del1); del1.Enabled = false; seq.ChildTestSteps.Add(ifVerdict); ifVerdict.ChildTestSteps.Add(del2); ifVerdict.InputVerdict.Step = del1; ifVerdict.InputVerdict.Property = TypeData.GetTypeData(del1).GetMember(nameof(del1.Verdict)); var cancel = new CancellationTokenSource(); var trd = TapThread.StartAwaitable(() => { var r = plan.Execute(); Assert.AreEqual(Verdict.NotSet, r.Verdict); }, cancel.Token); if (!trd.Wait(TimeSpan.FromMinutes(2))) { cancel.Cancel(); Assert.Fail("Test timed out"); } } [TestCase(1)] [TestCase(2)] [TestCase(10)] public void ParallelIfVerdictDeadlock(int n) { var plan = new TestPlan(); ParallelStep parallelStep = new ParallelStep(); plan.Steps.Add(parallelStep); for (int i = 1; i < n; i++) { var step = new ParallelStep(); parallelStep.ChildTestSteps.Add(step); parallelStep = step; } var ifVerdictStep = new IfStep(); parallelStep.ChildTestSteps.Add(ifVerdictStep); ifVerdictStep.InputVerdict.Step = parallelStep; ifVerdictStep.InputVerdict.Property = TypeData.GetTypeData(parallelStep).GetMember(nameof(parallelStep.Verdict)); Assert.AreEqual(plan.Execute().Verdict, Verdict.Error); } [Test] public void MovingStepWithInputTest() { var plan = new TestPlan(); var ifVerdict = new IfStep { }; var ifVerdict2 = new IfStep{}; plan.ChildTestSteps.Add(ifVerdict); plan.ChildTestSteps.Add(ifVerdict2); ifVerdict2.InputVerdict.Step = ifVerdict; ifVerdict2.InputVerdict.Step = null; plan.ChildTestSteps.Remove(ifVerdict); plan.ChildTestSteps.Insert(0, ifVerdict); // this should still be null, but was not because of a (fixed) bug. Assert.IsNull(ifVerdict2.InputVerdict.Step); } [Test] public void RepeatVerdictTest() { var plan = new TestPlan(); var repeat0 = new RepeatStep { Count = 2 }; var repeat = new RepeatStep { Count = 2 }; var delay = new DelayStep(); var ifVerdict = new IfStep { Action = IfStep.IfStepAction.RunChildren, InputVerdict = { Step = delay, Property = TypeData.GetTypeData(delay).GetMember(nameof(delay.Verdict)) } }; var setVerdict = new VerdictStep() {VerdictOutput = Verdict.Pass}; plan.ChildTestSteps.Add(repeat0); repeat0.ChildTestSteps.Add(repeat); repeat0.ChildTestSteps.Add(ifVerdict); repeat.ChildTestSteps.Add(delay); ifVerdict.ChildTestSteps.Add(setVerdict); var run = plan.Execute(); Assert.AreEqual(Verdict.Pass, run.Verdict); } [Test] public void AssignOutputDialogWithPlan() { var plan = new TestPlan(); var selectedOutputItem = AssignOutputDialog.SelectedOutputItem.Create(plan, TypeData.GetTypeData(plan).GetMembers().First()); Assert.DoesNotThrow(() => selectedOutputItem.ToString()); } } [AllowAnyChild] public class OutputParentStep : TestStep { [Output] public double OutputValue { get; set; } public override void Run() { RunChildSteps(); } } public class GenerateOutputStep : TestStep { [Output] public bool[] OutputBoolArray { get; private set; } [Output] public double OutputDouble { get; private set; } [Output] public double[] OutputDoubleArray { get; private set; } [Output] public string OutputString { get; private set; } [Output] public string[] OutputStringArray { get; private set; } [Output] public List OutputStringList { get; private set; } public GenerateOutputStep() { OutputBoolArray = new bool[] { false, true }; OutputDouble = 1.0; OutputDoubleArray = new double[] { 1.0, 2.2 }; OutputString = "Something"; OutputStringArray = new string[] { "tom", "dick" }; OutputStringList = new List { "One", "Two", "Three" }; } public override void Run() { } } [Display("Handle Input")] public class HandleInputStep : TestStep { [Display("Input Bool Array")] public Input InputBoolArray { get; set; } [Display("Input Double")] public Input InputDouble { get; set; } [Display("Input Double Array")] public Input InputDoubleArray { get; set; } [Display("Input String")] public Input InputString { get; set; } [Display("Input String Array")] public Input InputStringArray { get; set; } [Display("Input String List")] public Input> InputStringList { get; set; } public HandleInputStep() { InputDouble = new Input(); InputDoubleArray = new Input(); InputBoolArray = new Input(); InputString = new Input(); InputStringArray = new Input(); InputStringList = new Input>(); } public override void Run() { throw new NotImplementedException(); } } }