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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
//            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.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Xml.Serialization;
namespace OpenTap
{
 
    /// <summary>
    /// Used to specify that a TestStep class allows any class of step to be added as  a child.
    /// </summary>
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
    public class AllowAnyChildAttribute : Attribute
    {
 
    }
 
    /// <summary>
    /// Identifies which <see cref="TestStep"/> types that allow this TestStep as a child.
    /// Parent type can be TestPlan.
    /// </summary>
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
    public class AllowAsChildInAttribute : Attribute
    {
        /// <summary>
        /// Type of <see cref="TestStep"/> that can be a parent for this TestStep.
        /// </summary>
        public Type ParentStepType { get; private set; }
        /// <summary>
        /// Identifies which <see cref="TestStep"/> types that allow this TestStep as a child.
        /// Parent type can be TestPlan.
        /// </summary>
        /// <param name="parentStepType">Type of <see cref="TestStep"/> that can be a parent for this TestStep.</param>
        public AllowAsChildInAttribute(Type parentStepType)
        {
            ParentStepType = parentStepType;
        }
 
    }
 
    /// <summary>
    /// Specifies that a TestStep class allows any child TestStep of a given type.
    /// </summary>
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
    public class AllowChildrenOfTypeAttribute : Attribute
    {
        /// <summary>
        /// Which child type is allowed.
        /// </summary>
        public Type RequiredInterface { get; private set; }
        /// <summary>
        /// Specifies that a TestStep class allows any child TestStep of a given type.
        /// </summary>
        /// <param name="requiredInterface">Which child type is allowed.</param>
        public AllowChildrenOfTypeAttribute(Type requiredInterface)
        {
            RequiredInterface = requiredInterface;
        }
    }
 
    /// <summary>
    /// This class holds a list of TestSteps and is used for the Children property of TestStepBase. 
    /// It is responsible for making sure that all TestSteps added to the list are supported/allowed 
    /// as children of the TestStep in the TestStepList.Parent field.
    /// </summary>
    [DebuggerDisplay("TestStepList {Count}")]
    public class TestStepList : ObservableCollection<ITestStep>
    {
        private string sequenceName;
        [XmlAttribute("SequenceName")]
        public string SequenceName
        {
            get => sequenceName;
            set
            {
                sequenceName = value;
                OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs(nameof(SequenceName)));
            }
        }
 
        /// <summary>
        /// When true, the nesting rules defined by <see cref="AllowAsChildInAttribute"/> and 
        /// <see cref="AllowAnyChildAttribute"/> are checked when trying to insert a step into 
        /// this list. If the rules are not fulfilled the TestStep is not inserted and a warning 
        /// is written to the log.
        /// </summary>
        public bool EnforceNestingRulesOnInsert = true;
 
        /// <summary>Determines if the TestStepList is read only.</summary>
        public bool IsReadOnly { get; set; }
 
        private ITestStepParent _Parent;
        /// <summary>
        /// Parent item of type <see cref="ITestStepParent"/> to which this list belongs.
        /// TestSteps in this list (including TestSteps that are added later) will have this item set as their <see cref="P:OpenTap.ITestStep.Parent"/>.
        /// </summary>
        public ITestStepParent Parent
        {
            get { return _Parent; }
            set
            {
                _Parent = value;
                foreach (ITestStep step in this)
                {
                    step.Parent = value;
                }
                onContentChanged(this, ChildStepsChangedAction.ListReplaced, null, -1);
                if (Parent is TestPlan)
                {
                    rebuildIdLookup();
                }
                else
                {
                    idLookup = null;
                }
            }
        }
 
        /// <summary>
        /// Removes all the items in the list.
        /// </summary>
        protected override void ClearItems()
        {
            var steps = this.ToList();
            base.ClearItems();
            foreach (var step in steps)
            {
                step.Parent = null;
                onContentChanged(this, ChildStepsChangedAction.RemovedStep, step, 0);
            }
 
        }
 
        /// <summary>Constructor for the TestStepList.</summary>
        public TestStepList()
        {
            IsReadOnly = false;
        }
 
        void rebuildIdLookup()
        {
            if (idLookup == null)
                idLookup = new Dictionary<Guid, ITestStep>();
            idLookup.Clear();
            foreach (var thing in RecursivelyGetAllTestSteps(TestStepSearch.All))
            {
                idLookup.Add(thing.Id, thing);
            }
            if (Parent is ITestStep step)
                idLookup.Add(step.Id, step);
        }
 
        Dictionary<Guid, ITestStep> idLookup = null;
        private void TestStepList_ChildStepsChanged(TestStepList senderList, ChildStepsChangedAction Action, ITestStep Object, int Index)
        {
            if (idLookup == null) rebuildIdLookup();
            if (Action == ChildStepsChangedAction.RemovedStep)
            {
                foreach (var step in Utils.FlattenHeirarchy(new[] { Object }, subStep => subStep.ChildTestSteps))
                {
                    idLookup.Remove(step.Id);
                }
            }
            else if (Action == ChildStepsChangedAction.AddedStep)
            {
                foreach (var step in Utils.FlattenHeirarchy(new[] { Object }, subStep => subStep.ChildTestSteps))
                {
                    // in some cases a thing can be added twice.
                    // happens for TestPlanReference. Hence we should use [] and not Add 
                    idLookup[step.Id] = step;
                }
            }
            else if (Action == ChildStepsChangedAction.SetStep)
            {
                if (idLookup.TryGetValue(Object.Id, out var previous))
                {
                    foreach (var step in Utils.FlattenHeirarchy(new[] { previous }, subStep => subStep.ChildTestSteps))
                    {
                        idLookup.Remove(step.Id);
                    }
                }
                foreach (var step in Utils.FlattenHeirarchy(new[] { Object }, subStep => subStep.ChildTestSteps))
                {
                    idLookup[step.Id] = step;
                }
            }
        }
 
        /// <summary>
        /// Determines whether a TestStep of a specified type is allowed as child step to a parent of a specified type.
        /// </summary>
        public static bool AllowChild(Type parentType, Type childType)
        {
            if (parentType == null)
                throw new ArgumentNullException(nameof(parentType));
            if (childType == null)
                throw new ArgumentNullException(nameof(childType));
            return AllowChild(TypeData.FromType(parentType), TypeData.FromType(childType));
        }
 
        /// <summary>
        /// Determines whether a TestStep of a specified type is allowed as child step to a parent of a specified type.
        /// </summary>
        public static bool AllowChild(ITypeData parentType, ITypeData childType)
        {
            if (parentType == null)
                throw new ArgumentNullException(nameof(parentType));
            if (childType == null)
                throw new ArgumentNullException(nameof(childType));
            // if the parent is a TestPlan or the parent specifies "AllowAnyChild", then OK
            bool parentAllowsAnyChild = parentType.HasAttributeInherited<AllowAnyChildAttribute>();
            bool isChildIn = childType.HasAttributeInherited<AllowAsChildInAttribute>();
            if (parentAllowsAnyChild)
            {
                if (!isChildIn)
                {
                    return true;
                }
            }
 
            if (isChildIn)
            {
                // if the child specifies the parent type or the parent ancestor type in a "AllowAsChildIn" attribute, then OK
                var childIn = childType.GetAttributesInherited<AllowAsChildInAttribute>();
                foreach (AllowAsChildInAttribute attribute in childIn)
                {
                    if (parentType.DescendsTo(attribute.ParentStepType))
                        return true;
                }
            }
 
            // if the parent specifies the childType or a base type of childType in an "AllowChildrenOfType" attribute, then OK
            var child = parentType.GetAttributesInherited<AllowChildrenOfTypeAttribute>();
            foreach (AllowChildrenOfTypeAttribute attribute in child)
            {
                if (childType.DescendsTo(attribute.RequiredInterface))
                    return true;
            }
 
            // otherwise not OK
            return false;
        }
 
        private ITestStep findStepWithGuid(Guid guid)
        {
            TestStepList toplst;
 
            if (Parent != null)
            {
                ITestStepParent topitem = Parent;
                while (topitem.Parent != null) topitem = topitem.Parent;
                toplst = topitem.ChildTestSteps;
            }
            else
            {
                toplst = this;
            }
            return toplst.GetStep(guid);
        }
 
 
        /// <summary>
        /// Inserts an item into the collection at a specified index.
        /// </summary>
        /// <param name="index">Location in list.</param>
        /// <param name="item">To be inserted.</param>
        protected override void InsertItem(int index, ITestStep item)
        {
            if (item == null)
                throw new ArgumentNullException(nameof(item));
 
            throwIfRunning();
 
            var childsteps = Utils.FlattenHeirarchy([item], static step => step.ChildTestSteps);
 
            foreach (var step in childsteps)
            {
                var existingstep = findStepWithGuid(step.Id);
                if (existingstep != null)
                {
                    if (existingstep == step)
                        throw new InvalidOperationException("Test step already exists in the test plan");
                    step.Id = Guid.NewGuid();
                }
            }
 
            // Parent can be null if the list is being loaded from XML, in that case we 
            // set the parent property of the children in the setter of the Parent property
            if (Parent != null)
                item.Parent = Parent;
 
            if (EnforceNestingRulesOnInsert && Parent != null)
            {
                Type parentType = Parent.GetType();
                if (!AllowChild(parentType, item.GetType()))
                {
                    if (Parent is TestPlan)
                        throw new ArgumentException(String.Format("Cannot add test step of type {0} as a root test step (must be nested).", item.GetType().Name));
                    else
                        throw new ArgumentException(String.Format("Cannot add test step of type {0} to {1}", item.GetType().Name, Parent.GetType().Name));
                }
            }
            base.InsertItem(index, item);
 
            onContentChanged(this, ChildStepsChangedAction.AddedStep, item, index);
        }
 
        /// <summary>
        /// Invoked when an item is set. 
        /// </summary>
        /// <param name="index"></param>
        /// <param name="item"></param>
        /// <exception cref="InvalidOperationException"></exception>
        /// <exception cref="ArgumentException"></exception>
        protected override void SetItem(int index, ITestStep item)
        {
            throwIfRunning();
 
            // Parent can be null if the list is being loaded from XML, in that case we 
            // set the parent property of the children in the setter of the Parent property
            if (Parent != null)
                item.Parent = Parent;
 
            if (EnforceNestingRulesOnInsert && Parent != null)
            {
                Type parentType = Parent.GetType();
                if (!AllowChild(parentType, item.GetType()))
                {
                    if (Parent is TestPlan)
                        throw new ArgumentException(String.Format("Cannot add test step of type {0} as a root test step (must be nested).", item.GetType().Name));
                    else
                        throw new ArgumentException(String.Format("Cannot add test step of type {0} to {1}", item.GetType().Name, Parent.GetType().Name));
                }
            }
            base.SetItem(index, item);
            onContentChanged(this, ChildStepsChangedAction.SetStep, item, index);
        }
 
        /// <summary>
        /// Invoked when an item has been moved
        /// </summary>
        /// <param name="oldIndex"></param>
        /// <param name="newIndex"></param>
        protected override void MoveItem(int oldIndex, int newIndex)
        {
            base.MoveItem(oldIndex, newIndex);
            onContentChanged(this, ChildStepsChangedAction.MovedStep, this[newIndex], newIndex);
        }
 
        /// <summary>
        /// Returns true if a TestStep of type stepType can be inserted as a child step.
        /// </summary>
        /// <param name="stepType"></param>
        /// <returns></returns>
        public bool CanInsertType(Type stepType)
        {
            if (IsReadOnly) return false;
            if (Parent != null)
            {
                Type parentType = Parent.GetType();
                if (!AllowChild(parentType, stepType))
                    return false;
            }
 
            // If no parent, anything can be inserted.
            return true;
        }
 
        /// <summary>
        /// Defines the callback interface that can get invoked when a child step lists changes. 
        /// </summary>
        /// <param name="senderList"> The list that changed</param>
        /// <param name="Action">How the list changed</param>
        /// <param name="Object">Which object changed in the list (might be null if Reset)</param>
        /// <param name="Index">The index of the item changed.</param>
        public delegate void ChildStepsChangedDelegate(TestStepList senderList, ChildStepsChangedAction Action, ITestStep Object, int Index);
 
        /// <summary>
        /// Invoked when <see cref="TestStep.ChildTestSteps"/> changes for this TestStepList and child TestStepLists.
        /// </summary>
        public event ChildStepsChangedDelegate ChildStepsChanged;
 
        /// <summary>
        /// Specifies what has changed.
        /// </summary>
        public enum ChildStepsChangedAction
        {
            /// <summary>
            /// Specifies that a step has been added to a list.
            /// </summary>
            AddedStep,
            /// <summary>
            /// Specifies that a step has been removed from the list.
            /// </summary>
            RemovedStep,
            /// <summary>
            /// Specifies that the TestStepList has been replaced. The sender is in this case the new object. Object and Index will be null.
            /// </summary>
            ListReplaced,
            /// <summary>
            /// Specifies that a step has been set.
            /// </summary>
            SetStep,
            /// <summary>
            /// Specifies that a step has been moved.
            /// </summary>
            MovedStep
        }
 
        static readonly Random changeIdRandomState = new Random();
        // generate random IDs to try avoiding collisions with other cached states.
        // changeid is often used for caching.
        internal int ChangeId { get; set; } = changeIdRandomState.Next();
 
        void onContentChanged(TestStepList sender, ChildStepsChangedAction Action, ITestStep Object, int Index)
        {
            ChangeId += 1;
            if (Parent is TestPlan)
            {
                TestStepList_ChildStepsChanged(sender, Action, Object, Index);
            }
            else if (Parent is ITestStepParent parent && parent.Parent is ITestStepParent parent2 && parent2.ChildTestSteps != null)
                Parent.Parent.ChildTestSteps.onContentChanged(sender, Action, Object, Index);
            if (ChildStepsChanged != null)
                ChildStepsChanged(sender, Action, Object, Index);
        }
 
        TestPlan getTestPlanParent()
        {
            var parent = Parent;
 
            while (parent is ITestStep)
                parent = parent.Parent;
 
            return parent as TestPlan;
        }
 
        void throwIfRunning()
        {
            var plan = getTestPlanParent();
            if (plan != null && plan.IsRunning)
                throw new InvalidOperationException("Test plans cannot be modified while running.");
        }
 
        /// <summary>
        /// Removes the item at the specified index of the collection.
        /// </summary>
        protected override void RemoveItem(int index)
        {
            throwIfRunning();
            var item = this[index];
            base.RemoveItem(index);
            item.Parent = null;
            onContentChanged(this, ChildStepsChangedAction.RemovedStep, item, index);
        }
 
        /// <summary> Removed a number of steps from the test plan. </summary>
        /// <param name="steps">The steps to remove.</param>
        public void RemoveItems(IEnumerable<ITestStep> steps)
        {
            // find the topmost parent (usually the test plan itself).
            ITestStepParent parent = Parent;
            while (parent.Parent != null)
                parent = parent.Parent;
 
 
            HashSet<ITestStep> allRemovedSteps = Utils.FlattenHeirarchy(steps, s => s.ChildTestSteps).Distinct().ToHashSet();
            List<ITestStep> filteredsteps = steps.Where(s => allRemovedSteps.Contains(s.Parent as ITestStep) == false).ToList();
 
            if (filteredsteps.Any(step => step.Parent.ChildTestSteps.IsReadOnly))
                throw new InvalidOperationException("Cannot remove a step from a read-only list.");
 
            foreach (var step in filteredsteps)
                step.Parent.ChildTestSteps.Remove(step);
 
            {
                // For all remaining steps, check if they have any TestStep properties
                // If so, check that that property still exists in the plan. Otherwise set it to null.
                var AllSteps = Utils.FlattenHeirarchy(parent.ChildTestSteps, x => x.ChildTestSteps).ToHashSet();
 
                Dictionary<Type, System.Reflection.PropertyInfo[]> propLookup = new Dictionary<Type, System.Reflection.PropertyInfo[]>();
                foreach (var step in AllSteps)
                {
                    var t = step.GetType();
                    if (propLookup.ContainsKey(t)) continue;
                    var props = t.GetPropertiesTap().Where(p => p.PropertyType.DescendsTo(typeof(ITestStep)) && p.GetSetMethod() != null).ToArray();
                    propLookup[t] = props;
                }
 
                foreach (var step in AllSteps)
                {
                    var t = step.GetType();
                    if (propLookup.ContainsKey(t) == false) continue;
                    foreach (var prop in propLookup[t])
                    {
                        ITestStep value = (ITestStep)prop.GetValue(step);
                        if (AllSteps.Contains(value) == false)
                        {
                            try
                            {
                                prop.SetValue(step, null);
                            }
                            catch
                            {
                                // catch all usercode errors here.
                            }
                        }
                    }
                }
            }
        }
 
 
        /// <summary>
        /// Recursively iterates steps and child steps to collect all steps in the list.
        /// </summary>
        /// <param name="stepSearch">Search pattern.</param>
        /// <returns></returns>
        public IEnumerable<ITestStep> RecursivelyGetAllTestSteps(TestStepSearch stepSearch)
        {
            return Utils.FlattenHeirarchy(GetSteps(stepSearch), x => x.ChildTestSteps.GetSteps(stepSearch));
        }
 
        /// <summary>
        /// Gets steps based on the search pattern. Ignores child steps. Returns null if not found.  
        /// </summary>
        /// <param name="stepSearch">Search pattern.</param>
        /// <returns></returns>
        public IEnumerable<ITestStep> GetSteps(TestStepSearch stepSearch)
        {
            if (stepSearch == TestStepSearch.All) return this;
            return this.Where(step => stepSearch == TestStepSearch.EnabledOnly == step.Enabled);
        }
 
        /// <summary>
        /// Returns the test step that matches the <see cref="TestStep.Id"/>. Returns null if not found.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ITestStep GetStep(Guid id)
        {
            if (Parent == null && idLookup == null)
            {
                rebuildIdLookup();
            }
            if (idLookup != null)
            {
                if (idLookup.TryGetValue(id, out ITestStep result))
                    return result;
                return null;
            }
            if (this._Parent is ITestStep thisStep && thisStep.Id == id)
                return thisStep;
            foreach (ITestStep step in this)
            {
                if (step.Id == id)
                    return step;
                if (step.ChildTestSteps.Count > 0)
                {
                    ITestStep ret = step.ChildTestSteps.GetStep(id);
                    if (ret != null)
                        return ret;
                }
            }
            return null;
        }
 
        internal void AddRange(IEnumerable<ITestStep> steps)
        {
            foreach (var step in steps)
                Add(step);
        }
    }
}