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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace OpenTap
{
    /// <summary> 
    /// Marks a setting that can be enabled/disabled by the user. UIs are expected to render a checkbox in front of the actual value. 
    /// Settings of type <see cref="Enabled{T}"/> gets annotated with an annotation that implements this.
    /// </summary>
    public interface IEnabledValueAnnotation : IAnnotation
    {
        /// <summary>
        /// Indicates whether this setting is enabled.
        /// </summary>
        AnnotationCollection IsEnabled { get; }
 
        /// <summary>
        /// Annotations describing the actual value.
        /// </summary>
        AnnotationCollection Value { get; }
    }
 
    internal class BreakConditionsAnnotation : IEnabledValueAnnotation, IOwnedAnnotation, IMembersAnnotation
    {
        internal class BreakConditionValueAnnotation : IStringReadOnlyValueAnnotation, IValueDescriptionAnnotation, IAccessAnnotation
        {
            static BreakCondition[] conditions = Enum.GetValues(typeof(BreakCondition)).OfType<BreakCondition>().ToArray();
 
            static BreakCondition[] breakConditions =
                conditions.Where(x => x.ToString().Contains("Break")).ToArray();
            static string getEnumString(BreakCondition value)
            {
                if (value == 0) return "None";
                var sb = new StringBuilder();
                var breakFlags = breakConditions.Where(x => value.HasFlag(x));
                if (breakFlags.Any())
                {
                    sb.AppendFormat("Break on {0}", breakFlags.First().ToString().Substring("BreakOn".Length));
                    var breakFlags2 = breakFlags.Skip(1);
                    foreach (var x in breakFlags2)
                    {
                        sb.AppendFormat(" or {0}", x.ToString().Substring("BreakOn".Length));
                    }
                }
                return sb.ToString();
            }
 
            static BreakCondition convertAbortCondition(EngineSettings.AbortTestPlanType abortType)
            {
                return ((abortType.HasFlag(EngineSettings.AbortTestPlanType.Step_Fail)) ? BreakCondition.BreakOnFail : 0)
                       | (abortType.HasFlag(EngineSettings.AbortTestPlanType.Step_Error) ? BreakCondition.BreakOnError : 0)
                       | (abortType.HasFlag(EngineSettings.AbortTestPlanType.Step_Inconclusive) ? BreakCondition.BreakOnInconclusive : 0)
                       | (abortType.HasFlag(EngineSettings.AbortTestPlanType.Step_Pass) ? BreakCondition.BreakOnPass : 0);
            }
 
            private static (BreakCondition Condition, string InheritKind, bool MultiselectDifference) getInheritedVerdict(ITestStepParent _step)
            {
                ITestStepParent src = _step;
                src = src.Parent;
                while (src != null)
                {
                    var cond = BreakConditionProperty.GetBreakCondition(src);
                    if (cond.HasFlag(BreakCondition.Inherit) == false)
                    {
                        if (src is TestPlan)
                            return (cond, $"test plan", false);
                        return (cond, $"parent step '{((ITestStep)src).GetFormattedName()}'", false);
                    }
 
                    src = src.Parent;
                }
 
                return (convertAbortCondition(EngineSettings.Current.AbortTestPlan), "engine settings", false);
            }
 
            public (BreakCondition Condition, string InheritKind, bool MultiselectDifference) GetCondition()
            {
                if (annotation.Conditions.HasFlag(BreakCondition.Inherit))
                {
                    if(annotation.annotation.Source is ITestStepParent step)
                        return getInheritedVerdict(step);
                    if (annotation.annotation.Source is IEnumerable<ITestStepParent> stepList)
                    {
                        return getInheritedVerdict(stepList.First());
                    }
                }
 
                if (valueAnnotation.Get<IObjectValueAnnotation>().Value == null)
                {
                    var valuemem = (BreakCondition)0;
                    return (valuemem, null, true);
                }
                else
                {
                    var valuemem = (BreakCondition)valueAnnotation.Get<IObjectValueAnnotation>().Value;
                    return (valuemem, null, false);
                }
            }
 
            public string Value
            {
                get
                {
                    var (condition, _, multiselectDifference) = GetCondition();
                    if (multiselectDifference)
                        return "";
                    return getEnumString(condition);
                }
            }
 
            public bool IsReadOnly => annotation.Conditions.HasFlag(BreakCondition.Inherit);
 
            public bool IsVisible => true;
 
            readonly BreakConditionsAnnotation annotation;
            public AnnotationCollection valueAnnotation;
 
            public string Describe()
            {
                var (condition, kind, multiselectDifference) = GetCondition();
                if (multiselectDifference)
                    return "Selected Test Steps has different values for this setting.";
                var str = getEnumString(condition);
                if (kind == null) return str;
                return $"{str} (inherited from {kind}).";
            }
 
            public BreakConditionValueAnnotation(BreakConditionsAnnotation annotation)
            {
                this.annotation = annotation;
            }
        }
 
        [Flags]
        public enum Values
        {
            /// <summary> If a step completes with verdict 'Error', stop execution of any subsequent steps at this level, and return control to the parent step. </summary>
            [Display("On Error", "If a step completes with verdict 'Error', stop execution of any subsequent steps at this level, and return control to the parent step.")]
            BreakOnError = 2,
            /// <summary> If a step completes with verdict 'Fail', stop execution of any subsequent steps at this level, and return control to the parent step. </summary>
            [Display("On Fail", "If a step completes with verdict 'Fail', stop execution of any subsequent steps at this level, and return control to the parent step.")]
            BreakOnFail = 4,
            /// <summary> If a step completes with verdict 'Inclusive' the step should break execution.</summary>
            [Display("On Inconclusive", "If a step completes with verdict 'inconclusive', stop execution of any subsequent steps at this level, and return control to the parent step.")]
            BreakOnInconclusive = 8,
            /// <summary> If a step completes with verdict 'Pass' the step should break execution.</summary>
            [Display("On Pass", "If a step completes with verdict 'pass', stop execution of any subsequent steps at this level, and return control to the parent step.")]
            BreakOnPass = 16,
        }
        
        AnnotationCollection createEnabledAnnotation()
        {
            bool isEnabled = !Conditions.HasFlag(BreakCondition.Inherit);
            var sub = annotation.AnnotateSub(TypeData.GetTypeData(isEnabled), isEnabled);
            sub.Add(new AnnotationCollection.MemberAnnotation(TypeData.FromType(typeof(IEnabled)).GetMember(nameof(IEnabledValue.IsEnabled)))); // for compatibility with 9.8 UIs, emulate that this is a IsEnabled member from a Enabled<T> class
            return sub;
        }
        AnnotationCollection enabledAnnotation;
        public AnnotationCollection IsEnabled => (enabledAnnotation ?? (enabledAnnotation = createEnabledAnnotation()));
 
        BreakConditionValueAnnotation str;
        AnnotationCollection createValueAnnotation()
        {
            var _value = (Values)(int)Conditions;
            var sub = annotation.AnnotateSub(TypeData.GetTypeData(_value), _value);
            sub.Add(new AnnotationCollection.MemberAnnotation(TypeData.FromType(typeof(Enabled<Values>)).GetMember("Value"))); // for compatibility with 9.8 UIs, emulate that this is a Value member from a Enabled<T> class
            sub.Add(str = new BreakConditionValueAnnotation(this) { valueAnnotation = sub });
            return sub;
        }
        AnnotationCollection subannotations;
        public AnnotationCollection Value => (subannotations ?? (subannotations = createValueAnnotation()));
 
        internal BreakCondition Conditions
        {
            get => (BreakCondition) annotation.Get<IObjectValueAnnotation>().Value;
            set { annotation.Get<IObjectValueAnnotation>().Value = value; }
        }
 
        public IEnumerable<AnnotationCollection> Members => new[] { IsEnabled, Value }; // IMembersAnnotationthis is implemented here for compatablility with 9.8 UIs 
 
        AnnotationCollection annotation;
        internal BreakConditionsAnnotation(AnnotationCollection annotation)
        {
            this.annotation = annotation;
        }
 
        public void Read(object source)
        {
            if (subannotations != null)
            {
                Value.Get<IObjectValueAnnotation>().Value = (Values)(int)Conditions;
                Value.Read();
            }
 
            if (enabledAnnotation != null)
            {
                IsEnabled.Get<IObjectValueAnnotation>().Value = false == Conditions.HasFlag(BreakCondition.Inherit);
                IsEnabled.Read();
            }
        }
 
        public void Write(object source)
        {
            if (subannotations == null && enabledAnnotation == null) return;
            Value?.Write();
            var cond = (BreakCondition)(int)subannotations.Get<IObjectValueAnnotation>().Value;
            var dontInherit = (bool)(enabledAnnotation?.Get<IObjectValueAnnotation>().Value ?? false);
            
            if (dontInherit && cond.HasFlag(BreakCondition.Inherit))
            {
                var cond2 = str.GetCondition();
                cond = cond2.Condition;
            } 
            else if (dontInherit == false)
            {
                cond = BreakCondition.Inherit;
            }
            
            cond = cond.SetFlag(BreakCondition.Inherit, !dontInherit);
            Conditions = cond;
            annotation.Get<IObjectValueAnnotation>().Value = cond;
            Read(source);
        }
    }
}