wenxiangchao
4 天以前 559890c482f5973f06aafdfec9550fee5ba3c7f2
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
228
229
230
231
232
233
234
235
236
237
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> UIMessageEvent;
        public event EventHandler<ExecutionEndEvent> 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<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()
        {
            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();
            }
        }
    }
}