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
using System;
using System.ComponentModel;
using System.Linq;
using NUnit.Framework;
using OpenTap.Engine.UnitTests.TestTestSteps;
using OpenTap.UnitTests;
 
namespace OpenTap.Engine.UnitTests
{
    [TestFixture]
    public class PlanRunMonitorTests
    {
        [Browsable(false)]
        public class TestTestPlanRunMonitor : ComponentSettings<TestTestPlanRunMonitor>, ITestPlanRunMonitor
        {
            public bool IsEnabled { get; set; }
            public IResultListener ListenerToAdd { get; set; }
            public bool Entered { get; set; }
            public bool Exited { get; set; }
            public bool ThrowOnEnter { get; set; }
 
            public void EnterTestPlanRun(TestPlanRun plan)
            {
                if (!IsEnabled) return;
                Entered = true;
                if (ThrowOnEnter)
                    throw new Exception("Intended exception");
                if (ListenerToAdd != null)
                    plan.AddResultListener(ListenerToAdd);
            }
 
            public void ExitTestPlanRun(TestPlanRun plan)
            {
                if (!IsEnabled) return;
                Exited = true;
                if (ListenerToAdd != null)
                    plan.RemoveResultListener(ListenerToAdd);
            }
        }
 
        [Test]
        public void TestAddResultListenerInPlanRunMonitor()
        {
            // Verify that a result listener can be added by an ITestPlanRunMonitor.
            var plan = new TestPlan();
            plan.ChildTestSteps.Add(new SineResultsStep());
            using (Session.Create(SessionOptions.OverlayComponentSettings))
            {
                var listener = new PlanRunCollectorListener {CollectResults = true};
                TestTestPlanRunMonitor.Current.IsEnabled = true;
                TestTestPlanRunMonitor.Current.ListenerToAdd = listener;
                var executed = plan.Execute();
                Assert.IsFalse(executed.ResultListeners.Contains(listener));
                Assert.IsTrue(listener.WasOpened);
                Assert.IsFalse(listener.IsConnected);
                Assert.IsTrue(listener.Results.Any());
                Assert.IsFalse(ResultSettings.Current.Any());
            }
        }
 
        public class UnopenedInstrument : Instrument
        {
            public bool WasOpened { get; set; } = false;
            public override void Open()
            {
                WasOpened = true;
                base.Open();
            }
        }
 
        public class UnopenedResultListener : ResultListener
        {
            public bool WasOpened { get; set; } = false;
            public override void Open()
            {
                WasOpened = true;
                base.Open();
            }
        }
        [Test]
        public void TestNoResourcesOpenedOnRunMonitorThrow([Values(true, false)] bool throwOnEnter)
        {
            var plan = new TestPlan();
            var ins = new UnopenedInstrument();
            var res = new UnopenedResultListener();
            plan.ChildTestSteps.Add(new AnnotationTest.InstrumentStep() { Instrument = ins });
 
            var resourceManagers = TypeData.GetDerivedTypes<IResourceManager>().Select(td => td.CreateInstance())
                .Cast<IResourceManager>().ToArray();
 
            foreach (var rm in resourceManagers)
            {
                using (Session.Create(SessionOptions.OverlayComponentSettings))
                {
                    EngineSettings.Current.ResourceManagerType = rm;
                    InstrumentSettings.Current.Add(ins);
                    ResultSettings.Current.Add(res);
                    TestTestPlanRunMonitor.Current.IsEnabled = true;
                    TestTestPlanRunMonitor.Current.ThrowOnEnter = throwOnEnter;
                    var executed = plan.Execute();
                    Assert.AreEqual(throwOnEnter, executed.FailedToStart);
                    Assert.AreNotEqual(throwOnEnter, ins.WasOpened);
                    Assert.AreNotEqual(throwOnEnter, res.WasOpened);
                }
            }
        }
 
        [Test]
        public void PlanRunMonitorGeneralTest()
        {
            var plan = new TestPlan();
            plan.ChildTestSteps.Add(new SineResultsStep());
            using (Session.Create(SessionOptions.OverlayComponentSettings))
            {
                // Verify that run monitor is both entered and exited.
                TestTestPlanRunMonitor.Current.IsEnabled = true;
                plan.Execute();
                Assert.IsTrue(TestTestPlanRunMonitor.Current.Entered);
                Assert.IsTrue(TestTestPlanRunMonitor.Current.Exited);
            }
 
            using (Session.Create(SessionOptions.OverlayComponentSettings))
            {
                // Verify that run monitor is both entered and exited, even if the plan failed to start.
                TestTestPlanRunMonitor.Current.IsEnabled = true;
                TestTestPlanRunMonitor.Current.ThrowOnEnter = true;
                var run = plan.Execute();
                Assert.IsTrue(run.FailedToStart);
                Assert.IsTrue(TestTestPlanRunMonitor.Current.Entered);
                Assert.IsTrue(TestTestPlanRunMonitor.Current.Exited);
            }
            
            // verify that the overlaid session works.
            Assert.IsFalse(TestTestPlanRunMonitor.Current.ThrowOnEnter);
        }
        
        [Test]
        public void PlanRunMonitorOpenTestTest()
        {
            var plan = new TestPlan();
            plan.ChildTestSteps.Add(new SineResultsStep());
            using (Session.Create(SessionOptions.OverlayComponentSettings))
            {
                // Verify that run monitor is both entered and exited.
                TestTestPlanRunMonitor.Current.IsEnabled = true;
                var listener = new PlanRunCollectorListener {CollectResults = true};
                TestTestPlanRunMonitor.Current.ListenerToAdd = listener;
                plan.Open();
                var executed = plan.Execute();
                var listener2 = new PlanRunCollectorListener {CollectResults = true};
                TestTestPlanRunMonitor.Current.ListenerToAdd = listener2;
                var executed2 = plan.Execute();
                Assert.IsFalse(executed.ResultListeners.Contains(listener));
                Assert.IsFalse(executed2.ResultListeners.Contains(listener2));
                Assert.IsFalse(executed2.ResultListeners.Contains(listener));
                Assert.IsTrue(listener.WasOpened);
                Assert.IsTrue(listener.IsConnected);
                Assert.IsTrue(listener.Results.Any());
                Assert.IsTrue(listener2.Results.Any());
                Assert.IsTrue(listener2.IsConnected);
                Assert.IsFalse(ResultSettings.Current.Any());
                Assert.IsTrue(TestTestPlanRunMonitor.Current.Entered);
                Assert.IsTrue(TestTestPlanRunMonitor.Current.Exited);
                plan.Close();
                Assert.IsFalse(listener.IsConnected);
            }
 
        }
    }
}