aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Modules/Tango.FSE.Stubs/ViewModels/TestRunnerViewVM.cs
blob: 54e3d0f6da39f3958ca7f0e21646a1eaa1481e55 (plain)
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
238
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.BL.Entities;
using Tango.Core.Commands;
using Tango.FSE.Common;
using Tango.FSE.Stubs.Dialogs;
using Tango.FSE.Stubs.Messages;
using Tango.FSE.Stubs.Views;

namespace Tango.FSE.Stubs.ViewModels
{
    public class TestRunnerViewVM : FSEViewModel, ITestLogger
    {
        public enum RunnerView
        {
            TestRunnerCatalogView,
            TestRunnerExecutionView
        }

        private bool _requiresReloadingOfProjects;

        private RunnerView _selectedView;
        public RunnerView SelectedView
        {
            get { return _selectedView; }
            set { _selectedView = value; RaisePropertyChangedAuto(); }
        }

        private List<PublishedTestProject> _publishedTestProjects;
        public List<PublishedTestProject> PublishedTestProjects
        {
            get { return _publishedTestProjects; }
            set { _publishedTestProjects = value; RaisePropertyChangedAuto(); }
        }

        private TestProject _runningTestProject;
        public TestProject RunningTestProject
        {
            get { return _runningTestProject; }
            set { _runningTestProject = value; RaisePropertyChangedAuto(); }
        }

        private ProjectRunner _projectRunner;
        public ProjectRunner ProjectRunner
        {
            get { return _projectRunner; }
            set { _projectRunner = value; RaisePropertyChangedAuto(); }
        }

        private bool _isLoadingProjects;
        public bool IsLoadingProjects
        {
            get { return _isLoadingProjects; }
            set { _isLoadingProjects = value; RaisePropertyChangedAuto(); }
        }

        public bool HasTestInputs
        {
            get { return RunningTestProject != null && RunningTestProject.Inputs.Count > 0; }
        }

        private List<Result> _results;
        public List<Result> Results
        {
            get { return _results; }
            set { _results = value; RaisePropertyChangedAuto(); }
        }

        private String _status;
        public String Status
        {
            get { return _status; }
            set { _status = value; RaisePropertyChangedAuto(); }
        }

        private String _failedError;
        public String FailedError
        {
            get { return _failedError; }
            set { _failedError = value; RaisePropertyChangedAuto(); }
        }

        private bool _isRunning;
        public bool IsRunning
        {
            get { return _isRunning; }
            set { _isRunning = value; RaisePropertyChangedAuto(); }
        }

        public RelayCommand<PublishedTestProject> EditProjectCommand { get; set; }
        public RelayCommand<PublishedTestProject> RunProjectCommand { get; set; }
        public RelayCommand StartProjectCommand { get; set; }
        public RelayCommand StopProjectCommand { get; set; }
        public RelayCommand<Result> DisplayResultGridCommand { get; set; }

        public TestRunnerViewVM()
        {
            EditProjectCommand = new RelayCommand<PublishedTestProject>(EditProject);
            RunProjectCommand = new RelayCommand<PublishedTestProject>(RunProject);
            StartProjectCommand = new RelayCommand(StartProject, () => ProjectRunner != null && ProjectRunner.CanRun);
            StopProjectCommand = new RelayCommand(StopProject, () => ProjectRunner != null && ProjectRunner.IsRunning);
            DisplayResultGridCommand = new RelayCommand<Result>(DisplayResultGrid);

            _requiresReloadingOfProjects = true;
            RegisterForMessage<TestProjectPublishedOrSuppressed>((x) => _requiresReloadingOfProjects = true);
        }

        private async void StartProject()
        {
            try
            {
                IsRunning = true;
                FailedError = null;
                Results = new List<Result>();
                Status = "Running...";
                var context = new TestContext(RunningTestProject, this);
                await ProjectRunner.Run(context);
                Status = "Completed";
                Results = context.Results.ToList();
            }
            catch (OperationCanceledException)
            {
                Status = "Stopped";
            }
            catch (Exception ex)
            {
                Status = "Failed";
                FailedError = ex.FlattenMessage();
            }
            finally
            {
                IsRunning = false;
            }
        }

        private void StopProject()
        {
            ProjectRunner.Stop();
        }

        private void EditProject(PublishedTestProject project)
        {
            if (project != null)
            {
                NavigationManager.NavigateWithObject<
                    StubsModule,
                    TestDesignerView,
                    TestDesignerViewVM.NavigationObject>(
                    new TestDesignerViewVM.NavigationObject()
                    {
                        Project = project
                    });
            }
        }

        public override void OnNavigatedTo()
        {
            base.OnNavigatedTo();

            if (_requiresReloadingOfProjects)
            {
                LoadPublishedTestProjects();
            }
        }

        private async void LoadPublishedTestProjects()
        {
            try
            {
                IsFree = false;
                IsLoadingProjects = true;
                PublishedTestProjects = new List<PublishedTestProject>();
                PublishedTestProjects = await Services.PublishedTestProjectsService.GetPublishedTestProjects();
                _requiresReloadingOfProjects = false;
                IsFree = true;
            }
            catch (Exception ex)
            {
                IsFree = false;
                LogManager.Log(ex, "Error retrieving published test projects.");
                await NotificationProvider.ShowError($"Error occurred while trying to retrieve the tests collection.\n{ex.FlattenMessage()}");
            }
            finally
            {
                IsLoadingProjects = false;
            }
        }

        private void RunProject(PublishedTestProject project)
        {
            RunningTestProject = TestProject.FromJson(project.CurrentVersion.ProjectJsonString);
            RaisePropertyChanged(nameof(HasTestInputs));
            ProjectRunner = new ProjectRunner(RunningTestProject);
            ProjectRunner.StateChanged += (x, e) => InvalidateRelayCommands();
            Status = "Ready";
            InvalidateRelayCommands();
            SelectedView = RunnerView.TestRunnerExecutionView;
        }

        public override Task<bool> OnNavigateBackRequest()
        {
            if (SelectedView == RunnerView.TestRunnerExecutionView && !IsRunning)
            {
                SelectedView = RunnerView.TestRunnerCatalogView;
                return Task.FromResult(false);
            }

            return base.OnNavigateBackRequest();
        }

        public void WriteLine(string text)
        {
            Status = text;
        }

        public void Write(string text)
        {
            Status = text;
        }

        public void Clear()
        {
            //Do nothing
        }

        private void DisplayResultGrid(Result result)
        {
            if (result != null)
            {
                ResultGridViewVM vm = new ResultGridViewVM(result);
                NotificationProvider.ShowDialog(vm);
            }
        }
    }
}