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 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; public event EventHandler EndExecutionEvent; private PostUIMessageHandler uIMessageHandler; public string Name { get; set; } public bool IsConnected => true; static TapWrapper() { 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(string key, int option) { return PlanRun.Resolve(null, $"FileGlobals.{key}"); } public T GetStationGlobalValue(string key, int option) { if (PlanRun == null) { return StationGlobalsManager.GetValue(key); } return PlanRun.Resolve(null, $"StationGlobals.{key}"); } public T GetValue(string key, int option) { return default(T); } public void SetValue(string key, T value, int option) { } public void SetFileGlobalValue(string key, int option, T value) { PlanRun.Update(null, $"FileGlobals.{key}", value); } public void SetStationGlobalValue(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() { return true; } public void Dispose() { } public void SetContext(object context) { } public DispatcherObject ui { get; private set; } public void RegisteUserInput(DispatcherObject ui) { this.ui = ui; } public void RequestUserInput(object dataObject, TimeSpan Timeout, bool modal) { 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(); } } } }