wenxiangchao
2026-11-02 9d1660270272180cd71147d6ddd53d1382643d2e
Plugin.TsSupport/TapWrapper.cs
@@ -1,39 +1,188 @@
using System;
//using AddInPlugin;
//using AddInPlugin.Util;
//using Microsoft.Win32;
//using OpenTap;
//using OpenTap.Addin;
//using OpenTap.Addin.Util;
//using System.ComponentModel;
//using System.IO;
using AddInPlugin;
using AddInPlugin.Util;
using Microsoft.Win32;
using OpenTap;
using OpenTap.Addin;
using OpenTap.Addin.Listener;
using OpenTap.Addin.Reader;
using OpenTap.Addin.Util;
using OpenTap.Plugins.BasicSteps;
using System;
using System.ComponentModel;
using System.IO;
using System.Windows;
using System.Windows.Threading;
namespace Plugin.TsSupport
{
    public class TapWrapper : ITestExecutionWrapper
    public class Net47Getter : IRegGetter
    {
        public string GetAppPath(string keyPath)
        {
            using (RegistryKey key = Registry.CurrentUser.OpenSubKey(keyPath))
            {
                if (key == null)
                {
                    throw new Exception("注册表不存在,请先运行一次编辑器");
                }
                string value = key.GetValue("Location")?.ToString();
                if (string.IsNullOrEmpty(value))
                {
                    throw new Exception("注册表不存在,请先运行一次编辑器");
                }
                return value;
            }
        }
    }
    public class TapWrapper : ITestExecutionWrapper, IResultListener, IUserInputInterface
    {
        TapThread testPlanThread;
        TestPlan Plan;
        TestPlanRun PlanRun;
        public event PropertyChangedEventHandler PropertyChanged;
        public event EventHandler<UIMessageEvent> UIMessageEvent;
        public event EventHandler<ExecutionEndEvent> EndExecutionEvent;
        public void CommitToDisk(bool option)
        {
        private PostUIMessageHandler uIMessageHandler;
        }
        public string Name { get; set; }
        public T GetFileGlobalValue<T>(string key, int option)
        {
            return default(T);
        }
        public bool IsConnected => true;
        public T GetStationGlobalValue<T>(string key, int option)
        static TapWrapper()
        {
            return default(T);
            RegUtil.Register(new Net47Getter());
        }
        public object Init()
        {
            StationGlobalsManager.Register(new DefaultStationGlobalsManager());
            VariableResolverFactory.Register(new DefaultVariableResolverFactory());
            ResultSettings.Current.Add(this);
            UserInput.SetInterface(this);
            LvlibpLoader.PreLoad();
            PropertyInfoCache.Refresh(typeof(MethodVariable));
            uIMessageHandler = PostUIMessageHandler.Get();
            uIMessageHandler.OnPostUIMsg += PostUIMsgHandler_OnPostUIMsg;
            return null;
        }
        private void PostUIMsgHandler_OnPostUIMsg(object sender, PostUIMsg e)
        {
            UIMessageEvent?.Invoke(this, new UIMessageEvent()
            {
                Origin = e,
                EventCode = e.EventCode,
                BoolData = e.BoolData,
                NumberData = e.NumberData,
                StrData = e.StringData
            });
        }
        public object StartSequence(string filePath, string modelPath)
        {
            Plan = TestPlanReader.ReadTestPlan(filePath);
            //Plan = TestPlan.Load(filePath);
            testPlanThread = TapThread.Start(() =>
            {
                var res = ResultSettings.Current;
                res.Add(uIMessageHandler);
                Plan.Execute(ResultSettings.Current);
            });
            return Plan;
        }
        public T GetFileGlobalValue<T>(string key, int option)
        {
            return PlanRun.Resolve<T>(null, $"FileGlobals.{key}");
        }
        public T GetStationGlobalValue<T>(string key, int option)
        {
            if (PlanRun == null)
            {
                return StationGlobalsManager.GetValue<T>(key);
            }
            return PlanRun.Resolve<T>(null, $"StationGlobals.{key}");
        }
        public T GetValue<T>(string key, int option)
        {
            return default(T);
        }
        public void SetValue<T>(string key, T value, int option)
        {
        }
        public void SetFileGlobalValue<T>(string key, int option, T value)
        {
            PlanRun.Update(null, $"FileGlobals.{key}", value);
        }
        public void SetStationGlobalValue<T>(string key, int option, T value, bool inContext = false)
        {
            if (PlanRun == null)
            {
                StationGlobalsManager.SetValue(key, value);
            }
            else
            {
                PlanRun.Update(null, $"StationGlobals.{key}", value);
            }
        }
        public void CommitToDisk(bool option)
        {
            StationGlobalsManager.WriteToLocal(null);
        }
        public void OnTestPlanRunStart(TestPlanRun planRun)
        {
            PlanRun = planRun;
        }
        public void OnTestPlanRunCompleted(TestPlanRun planRun, Stream logStream)
        {
            PlanRun = null;
        }
        public void OnTestStepRunStart(TestStepRun stepRun)
        {
        }
        public void OnTestStepRunCompleted(TestStepRun stepRun)
        {
        }
        public void OnResultPublished(Guid stepRunID, ResultTable result)
        {
        }
        public void Open()
        {
        }
        public void Close()
        {
            EndExecutionEvent?.Invoke(this, new ExecutionEndEvent());
            Plan = null;
            PlanRun = null;
            testPlanThread = null;
        }
        public bool WaitForExit()
        {
            return true;
        }
        public bool ReleaseSeqFile()
@@ -41,198 +190,48 @@
            return true;
        }
        public void Dispose()
        {
        }
        public void SetContext(object context)
        {
        }
        public void SetFileGlobalValue<T>(string key, int option, T value)
        {
        public DispatcherObject ui { get; private set; }
        public void RegisteUserInput(DispatcherObject ui)
        {
            this.ui = ui;
        }
        public void SetStationGlobalValue<T>(string key, int option, T value, bool inContext = false)
        public void RequestUserInput(object dataObject, TimeSpan Timeout, bool modal)
        {
        }
        public object StartSequence(string filePath, string modelPath)
        {
            return null;
        }
        public bool WaitForExit()
        {
            return true;
            if (dataObject is DialogRequest req)
            {
                this.ui?.Dispatcher.BeginInvoke(new Action(() =>
                {
                    if (req.Buttons == InputButtons.OkCancel)
                    {
                        var res = MessageBox.Show(req.Message, req.Name, MessageBoxButton.OKCancel);
                        req.Input2 = res == MessageBoxResult.OK ? WaitForInputResult2.Ok : WaitForInputResult2.Cancel;
                    }
                    else
                    {
                        var res = MessageBox.Show(req.Message, req.Name, MessageBoxButton.YesNo);
                        req.Input1 = res == MessageBoxResult.Yes ? WaitForInputResult1.Yes : WaitForInputResult1.No;
                    }
                })).Wait();
            }
            else if (dataObject is OpenTap.TestPlan.PlanLoadError err)
            {
                this.ui?.Dispatcher.BeginInvoke(new Action(() =>
                {
                    var res = MessageBox.Show(err.Message, err.Name, MessageBoxButton.OKCancel);
                    err.Response = res == MessageBoxResult.OK ? TestPlan.PlanLoadErrorResponse.Ignore : TestPlan.PlanLoadErrorResponse.Abort;
                })).Wait();
            }
        }
    }
    //public class Net47Getter : IRegGetter
    //{
    //    public string GetAppPath(string keyPath)
    //    {
    //        using (RegistryKey key = Registry.CurrentUser.OpenSubKey(keyPath))
    //        {
    //            if (key == null)
    //            {
    //                throw new Exception("注册表不存在,请先运行一次编辑器");
    //            }
    //            string value = key.GetValue("Location")?.ToString();
    //            if (string.IsNullOrEmpty(value))
    //            {
    //                throw new Exception("注册表不存在,请先运行一次编辑器");
    //            }
    //            return value;
    //        }
    //    }
    //}
    //public class TapWrapper : ITestExecutionWrapper, IResultListener
    //{
    //    TapThread testPlanThread;
    //    TestPlan Plan;
    //    TestPlanRun PlanRun;
    //    public event PropertyChangedEventHandler PropertyChanged;
    //    public event EventHandler<UIMessageEvent> UIMessageEvent;
    //    public event EventHandler<ExecutionEndEvent> EndExecutionEvent;
    //    public string Name { get; set; }
    //    public bool IsConnected => true;
    //    public object Init()
    //    {
    //        RegUtil.Register(new Net47Getter());
    //        StationGlobalsManager.Register(new DefaultStationGlobalsManager());
    //        VariableResolverFactory.Register(new DefaultVariableResolverFactory());
    //        ResultSettings.Current.Add(this);
    //        return null;
    //    }
    //    private void PostUIMsgHandler_OnPostUIMsg(object sender, PostUIMsg e)
    //    {
    //        UIMessageEvent?.Invoke(this, new UIMessageEvent()
    //        {
    //            Origin = e,
    //            EventCode = e.EventCode,
    //            BoolData = e.BoolData,
    //            NumberData = e.NumberData,
    //            StrData = e.StringData
    //        });
    //    }
    //    public object StartSequence(string filePath, string modelPath)
    //    {
    //        Plan = TestPlan.Load(filePath);
    //        testPlanThread = TapThread.Start(() =>
    //        {
    //            var res = ResultSettings.Current;
    //            Plan.Execute(ResultSettings.Current, null);
    //        });
    //        return Plan;
    //    }
    //    public T GetFileGlobalValue<T>(string key, int option)
    //    {
    //        return PlanRun.Resolve<T>(null, $"FileGlobals.{key}");
    //    }
    //    public T GetStationGlobalValue<T>(string key, int option)
    //    {
    //        if (PlanRun == null)
    //        {
    //            return StationGlobalsManager.GetValue<T>(key);
    //        }
    //        return PlanRun.Resolve<T>(null, $"StationGlobals.{key}");
    //    }
    //    public T GetValue<T>(string key, int option)
    //    {
    //        return default(T);
    //    }
    //    public void SetValue<T>(string key, T value, int option)
    //    {
    //    }
    //    public void SetFileGlobalValue<T>(string key, int option, T value)
    //    {
    //        PlanRun.Update(null, $"FileGlobals.{key}", value);
    //    }
    //    public void SetStationGlobalValue<T>(string key, int option, T value, bool inContext = false)
    //    {
    //        if (PlanRun == null)
    //        {
    //            StationGlobalsManager.SetValue(key, value);
    //        }
    //        else
    //        {
    //            PlanRun.Update(null, $"StationGlobals.{key}", value);
    //        }
    //    }
    //    public void CommitToDisk(bool option)
    //    {
    //        StationGlobalsManager.WriteToLocal(null);
    //    }
    //    public void OnTestPlanRunStart(TestPlanRun planRun)
    //    {
    //        PlanRun = planRun;
    //    }
    //    public void OnTestPlanRunCompleted(TestPlanRun planRun, Stream logStream)
    //    {
    //    }
    //    public void OnTestStepRunStart(TestStepRun stepRun)
    //    {
    //    }
    //    public void OnTestStepRunCompleted(TestStepRun stepRun)
    //    {
    //    }
    //    public void OnResultPublished(Guid stepRunID, ResultTable result)
    //    {
    //    }
    //    public void Open()
    //    {
    //        PostUIMsgHandler.OnPostUIMsg += PostUIMsgHandler_OnPostUIMsg;
    //    }
    //    public void Close()
    //    {
    //        PostUIMsgHandler.OnPostUIMsg -= PostUIMsgHandler_OnPostUIMsg;
    //        EndExecutionEvent?.Invoke(this, new ExecutionEndEvent());
    //        PlanRun = null;
    //    }
    //    public bool WaitForExit()
    //    {
    //        return true;
    //    }
    //    public bool ReleaseSeqFile()
    //    {
    //        return true;
    //    }
    //    public void SetContext(object context)
    //    {
    //    }
    //}
}