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
//            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 NUnit.Framework;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Serialization;
 
namespace OpenTap.Engine.UnitTests
{
    public class TestPort : Port
    {
        public double PortImpedance { get; set; }
 
        public TestPort(IResource res, string name) : base(res, name)
        {
            PortImpedance = 50;
        }
    }
 
    public class TestPortInstrument : Instrument
    {
        [XmlIgnore]
        public TestPort Port1 { get; set; }
        [XmlIgnore]
        public TestPort Port2 { get; set; }
        public TestPortInstrument()
        {
            Port1 = new TestPort(this, "Port1");
            Port2 = new TestPort(this, "Port2");
        }
 
    }
 
    public class TestPortStep : TestStep
    {
        public IEnumerable<Port> AvailPorts
        {
            get { return InstrumentSettings.Current.OfType<TestPortInstrument>().SelectMany(inst => new Port[] { inst.Port1, inst.Port2 }); }
        }
 
        Port selectedPort;
        [AvailableValues("AvailPorts")]
        public Port SelectedPort { get { return selectedPort; } set { selectedPort = value; } }
        public override void Run()
        {
            Assert.IsTrue(SelectedPort is TestPort);
        }
    }
 
    [TestFixture]
    public class PortTest
    {
        [Test]
        public void TestPortSerialization()
        {
            var currentSettingsDir = ComponentSettings.GetSettingsDirectory("Bench");
            // Try to invoke the ComponentSettings serializer/deserializer for connections
            // Important thing is that a TestPort is still a TestPort after serialization.
            ComponentSettings.SetSettingsProfile("Bench", "test1");
            InstrumentSettings.Current.ToList();
            TestPortInstrument Instr = new TestPortInstrument();
            InstrumentSettings.Current.Add(Instr);
            RfConnection conn = new RfConnection() { Port1 = Instr.Port1, Port2 = Instr.Port2 };
            ConnectionSettings.Current.Clear();
            ConnectionSettings.Current.Add(conn);
            ComponentSettings.SaveAllCurrentSettings();
            ComponentSettings.SetSettingsProfile("Bench", "test2");
            InstrumentSettings.Current.ToList(); // make componentsettings reload.
            ComponentSettings.SetSettingsProfile("Bench", "test1");
 
            var instr2 = InstrumentSettings.Current.OfType<TestPortInstrument>().First();
            Assert.IsFalse(object.ReferenceEquals(instr2, Instr));
            Instr = instr2;
            conn = ConnectionSettings.Current.OfType<RfConnection>().First(con => (con.Port1 is TestPort));
 
            // Now try to serialize a test plan with a Port inside it.
            try
            {
                TestPlan plan = new TestPlan { };
                plan.ChildTestSteps.Add(new TestPortStep() { SelectedPort = Instr.Port1 });
                TestPlan newplan;
 
                using (var str = new MemoryStream(1000))
                {
                    plan.Save(str);
                    str.Position = 0;
                    newplan = TestPlan.Load(str,plan.Path);
                }
                TestPortStep step = (TestPortStep)newplan.ChildTestSteps.First();
                Assert.IsTrue(step.SelectedPort is TestPort);
            }
            finally
            {
                // Cleanup
                ConnectionSettings.Current.Remove(conn);
                InstrumentSettings.Current.Remove(Instr);
                ComponentSettings.SaveAllCurrentSettings();
 
                ComponentSettings.SetSettingsProfile("Bench", currentSettingsDir);
            }
        }
 
    }
}