chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
//            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<double> Input { get; set; } = new Input<double>();
 
        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<double> secs = new Input<double>() { Step = delay, Property = prop};
            
            var plan = new TestPlan();
            plan.ChildTestSteps.Add(delay);
            
            delay.DelaySecs = 2;
            Assert.AreEqual(secs.Value, delay.DelaySecs);
 
            Input<double> secs2 = new Input<double>() { Step = delay, Property = prop };
            Assert.AreEqual(secs, secs2);
 
            Input<double> secs3 = new Input<double>() { Step = delay, Property = null };
            Input<double> secs4 = new Input<double>() { Step = null, Property = prop };
            Input<double> secs5 = new Input<double>() { Step = null, Property = null };
            Input<double> secs6 = new Input<double>() { 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<double> a = null;
            Input<double> b = null;
            Assert.IsTrue(a == b);
            a = new Input<double>(){};
            Assert.IsTrue(a != b);
            b = new Input<double>(){};
 
            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<IStringReadOnlyValueAnnotation>();
 
            {
                // 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<string>();
            Assert.DoesNotThrow(() => x.ToString());
            Assert.AreEqual("", x.ToString());
        }
        
        /// <summary>
        /// OpenTAP issue: #666
        /// </summary>
        [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<string> 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<string> { "One", "Two", "Three" };
        }
 
        public override void Run()
        {
            
        }
    }
 
    [Display("Handle Input")]
    public class HandleInputStep : TestStep
    {
        [Display("Input Bool Array")]
        public Input<bool[]> InputBoolArray { get; set; }
        [Display("Input Double")]
        public Input<double> InputDouble { get; set; }
        [Display("Input Double Array")]
        public Input<double[]> InputDoubleArray { get; set; }
        [Display("Input String")]
        public Input<string> InputString { get; set; }
        [Display("Input String Array")]
        public Input<string[]> InputStringArray { get; set; }
        [Display("Input String List")]
        public Input<List<string>> InputStringList { get; set; }
 
        public HandleInputStep()
        {
            InputDouble = new Input<double>();
            InputDoubleArray = new Input<double[]>();
            InputBoolArray = new Input<bool[]>();
            InputString = new Input<string>();
            InputStringArray = new Input<string[]>();
            InputStringList = new Input<List<string>>();
        }
 
        public override void Run()
        {
            throw new NotImplementedException();
        }
    }
}