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
//            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.Linq;
using System.Xml.Serialization;
 
namespace OpenTap
{
    /// <summary>   Interface for TestStep input properties. </summary>
    public interface IInput
    {
        /// <summary>   Gets or sets the TestStep that has the output property to which this Input is connected. </summary>
        ITestStep Step { get; set; }
 
        /// <summary>   Describes the <see cref="OutputAttribute"/> property on the <see cref="Step"/> to which this Input is connected.   </summary>
        IMemberData Property { get; set; }
    }
 
    /// <summary>
    /// Input type restriction for IInput.
    /// </summary>
    public interface IInputTypeRestriction : IInput
    {
        /// <summary> returns true if the concrete type is supported. </summary>
        /// <param name="concreteType"></param>
        /// <returns></returns>
        bool SupportsType(ITypeData concreteType);
    }
 
    interface IOwnedInput
    {
        object Owner { get; set; }
    }
 
    /// <summary>   
    /// A generic type that specifies input properties for a TestStep. The user can link this property to properties on other TestSteps that are marked with the <see cref="OutputAttribute"/>
    /// When used in a TestStep, Input value should always be set in the constructor.
    /// </summary>
    /// <typeparam name="T"> Generic type parameter. </typeparam>
    public class Input<T> : IInput, IInputTypeRestriction, ICloneable, IOwnedInput
    {
        /// <summary> 
        /// Describes the output property on the <see cref="Step"/> to which this Input is connected.  
        /// </summary>
        [XmlIgnore]
        public IMemberData Property { get; set; }
 
        void updatePropertyFromName()
        {
            if (string.IsNullOrEmpty(propertyName))
                return;
            else
            {
                string[] parts = propertyName.Split('|');
                if (parts.Length == 1)
                {
                    if (step != null)
                    {
                        ITypeData stepType = TypeData.GetTypeData(step);
                        Property = stepType.GetMember(parts[0]);    
                    }
                    else
                    {
                        Property = null;
                    }
                }
                else
                {
                    var typename = parts[0];
                    ITypeData stepType = TypeData.GetTypeData(typename);
                    Property = stepType.GetMember(parts[1]);
                }
            }
 
            if (Property != null)
                propertyName = null;
        }
        
        string propertyName;
        /// <summary> Gets or sets the name of the property to which this Input is connected. Used for serialization. </summary>
        public string PropertyName
        {
            get => Property != null ? Property.DeclaringType.Name + "|" + Property.Name : propertyName;
            set
            {
                propertyName = value;
                updatePropertyFromName();
                
            }
        }
 
        private ITestStepParent getTopmostParent(ITestStepParent step)
        {
            if (step == null) return null;
            ITestStepParent parent = step;
            while (parent.Parent != null)
                parent = parent.Parent;
            return parent;
        }
 
        object testStepOwner = null;
        const string OwnerNotFound = nameof(OwnerNotFound);
        ITestStep step;
 
        /// <summary>
        /// Sometimes we need to know the owner object.
        /// Since we cannot automatically get it from the owning object, we can iterate the entire test plan to find it.
        /// It's a bit of a hack, but works well.
        /// For better performance we set the owner object for all Inputs in the test plan by using the IOwnedInput (internal) interface.
        /// </summary>
        void UpdateAllOwnedInputsInPlan()
        {
            if (step == null) return;
            var topmost = getTopmostParent(step);
            foreach (var step in topmost.ChildTestSteps.RecursivelyGetAllTestSteps(TestStepSearch.All))
            {
                foreach (var inputMember in TypeData.GetTypeData(step).GetMembers().Where(member => member.TypeDescriptor.DescendsTo(typeof(IOwnedInput)) && member.Readable))
                {
                    try
                    {
                        if (inputMember.GetValue(step) is IOwnedInput owned)
                        {
                            owned.Owner = step;
                        }
                    }
                    catch
                    {
                        // Ignore errors in user code.
                    }
                }
            }
        }
 
        /// <summary>   Gets or sets the TestStep that has the output property to which this Input is connected. </summary>
        public ITestStep Step
        {
            get
            {
                // If the step is part of the test plan heirarchy, return the step.
                // Otherwise, return null.
                if (step == null) return null;
 
                if (testStepOwner == null)
                {
                    UpdateAllOwnedInputsInPlan();
                    if(testStepOwner == null)
                        testStepOwner = OwnerNotFound;
                }
 
                if (testStepOwner is ITestStep ownerStep)
                {
 
                    if (getTopmostParent(ownerStep).ChildTestSteps.GetStep(step.Id) != null)
                        return step;
 
                    // the step is not in the same plan as the owner step.
                    return null;
                }
 
                // if we could not find the owner step, then its better to assume that the value of step is ok.
                return step;
            }
            set
            {
                if(step != value)
                {
                    step = value;
                    
                    if (step == null)
                    {   
                        return;
                    }
                    stepId = step.Id; 
                    updatePropertyFromName();
                }
            }
        }
 
        // the step ID is used for storing the step ID when moving the Step owning the Input.
        // otherwise, after moving the step (taking it out of the test plan and moving it back in) the Step might be null.
        Guid stepId;
 
        /// <summary>   Gets the value of the connected output property. </summary>
        /// <exception cref="Exception">    Thrown when this Input does not contain a reference to a TestStep output. </exception>
        [XmlIgnore]
        public T Value
        {
            get
            {
                if (step == null || Property == null)
                    throw new Exception("Step input requires reference to a TestStep output.");
                
                // Wait for the step to complete
                return (T)InputOutputRelation.GetOutput(Property, step, Guid.NewGuid()); 
            }
        }
 
        T GetValueNonBlocking()
        {
            if (Step is ITestStep step && Property?.GetValue(step) is T v)
                return v;
            return default;
        }
        
        /// <summary> Converts the value of this instance to its equivalent string representation. </summary>
        /// <returns> The string representation of the value of this instance. </returns>
        public override string ToString() =>  StringConvertProvider.GetString(GetValueNonBlocking()) ?? "";
 
        /// <summary> Compares one Input to another. </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            if(obj is Input<T> other)
                return other.step == step && other.Property == Property;
            return false;
        }
 
        /// <summary> Gets the hash code.</summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            return (step?.GetHashCode() ?? 0) ^ (Property?.GetHashCode() ?? 0);
        }
 
        object ICloneable.Clone() => new Input<T>
        {
            step = step,
            Property = Property
        };
 
        /// <summary> Returns true if this input supports the concrete type. </summary>
        /// <param name="concreteType"></param>
        /// <returns></returns>
        public virtual bool SupportsType(ITypeData concreteType)
        {
            return concreteType.DescendsTo(typeof(T));
        }
 
        /// <summary> Compares two Input for equality.</summary>
        /// <returns></returns>
        public static bool operator==(Input<T> a, Input<T> b) => object.Equals(a, b);
 
        /// <summary> Compares two Input for inequality.</summary>
        /// <returns></returns>
        public static bool operator !=(Input<T> a, Input<T> b) => !(a == b);
        object IOwnedInput.Owner
        {
            get => testStepOwner as ITestStepParent;
            set => testStepOwner = value;
        }
    }
}