-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathTreeViewPresenter.cs
More file actions
477 lines (392 loc) · 17.1 KB
/
TreeViewPresenter.cs
File metadata and controls
477 lines (392 loc) · 17.1 KB
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
// ***********************************************************************
// Copyright (c) Charlie Poole and TestCentric contributors.
// Licensed under the MIT License. See LICENSE file in root directory.
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using TestCentric.Common;
namespace TestCentric.Gui.Presenters
{
using Model;
using Views;
using Dialogs;
using System.Xml;
using System.Drawing;
using System.IO;
using TestCentric.Gui.Controls;
/// <summary>
/// TreeViewPresenter is the presenter for the TestTreeView
/// </summary>
public class TreeViewPresenter
{
private ITestTreeView _view;
private ITestModel _model;
private Model.Settings.TestTreeSettings _treeSettings;
private ITreeDisplayStrategyFactory _treeDisplayStrategyFactory;
// Accessed by tests
public ITreeDisplayStrategy Strategy { get; private set; }
#region Constructor
public TreeViewPresenter(ITestTreeView treeView, ITestModel model, ITreeDisplayStrategyFactory factory)
{
_view = treeView;
_model = model;
_treeDisplayStrategyFactory = factory;
_treeSettings = _model.Settings.Gui.TestTree;
_view.ShowCheckBoxes.Checked = _view.CheckBoxes = _treeSettings.ShowCheckBoxes;
_view.ShowTestDuration.Checked = _treeSettings.ShowTestDuration;
_view.AlternateImageSet = _treeSettings.AlternateImageSet;
WireUpEvents();
}
#endregion
#region Private Members
private void WireUpEvents()
{
// Model actions
_model.Events.TestLoaded += (ea) =>
{
EnsureNonRunnableFilesAreVisible(ea.Test);
bool visualStateLoaded = TryLoadVisualState(out VisualState visualState);
if (visualStateLoaded)
UpdateTreeSettingsFromVisualState(visualState);
Strategy = _treeDisplayStrategyFactory.Create(_treeSettings.DisplayFormat, _view, _model);
_view.ShowCheckBoxes.Checked = visualStateLoaded ? visualState.ShowCheckBoxes : _treeSettings.ShowCheckBoxes;
Strategy.OnTestLoaded(ea.Test, visualState);
CheckPropertiesDisplay();
CheckXmlDisplay();
};
_model.Events.TestReloaded += (ea) =>
{
EnsureNonRunnableFilesAreVisible(ea.Test);
Strategy.OnTestLoaded(ea.Test, null);
_view.CheckBoxes = _view.ShowCheckBoxes.Checked; // TODO: View should handle this
};
_model.Events.TestUnloaded += (ea) =>
{
Strategy.OnTestUnloaded();
};
_model.Events.TestsUnloading += ea =>
{
Strategy.SaveVisualState();
ClosePropertiesDisplay();
CloseXmlDisplay();
};
_model.Events.TestsReloading += ea => Strategy.SaveVisualState();
_model.Events.RunStarting += (ea) =>
{
// Save the visual state in case test run causes an exception
// or user terminates cancels the run.
Strategy.SaveVisualState();
_model.ClearResults();
Strategy.OnTestRunStarting();
CheckPropertiesDisplay();
CheckXmlDisplay();
};
_model.Events.RunFinished += (ea) =>
{
Strategy.OnTestRunFinished();
};
_model.Events.TestFilterChanged += (ea) =>
{
Strategy?.Reload();
};
_model.Events.TestFinished += OnTestFinished;
_model.Events.SuiteFinished += OnTestFinished;
_model.Settings.Changed += (s, e) =>
{
switch (e.SettingName)
{
case "TestCentric.Gui.TestTree.AlternateImageSet":
_view.AlternateImageSet = _treeSettings.AlternateImageSet;
break;
case "TestCentric.Gui.TestTree.DisplayFormat":
{
Strategy = _treeDisplayStrategyFactory.Create(_treeSettings.DisplayFormat, _view, _model);
Strategy.Reload();
break;
}
case "TestCentric.Gui.TestTree.TestList.GroupBy":
case "TestCentric.Gui.TestTree.FixtureList.GroupBy":
case "TestCentric.Gui.TestTree.ShowNamespace":
Strategy?.Reload();
break;
case "TestCentric.Gui.TestTree.ShowCheckBoxes":
_view.ShowCheckBoxes.Checked = _treeSettings.ShowCheckBoxes;
break;
case "TestCentric.Gui.GuiLayout":
if (_model.Settings.Gui.GuiLayout == "Full")
ClosePropertiesDisplay();
break;
case "TestCentric.Gui.TestTree.ShowFilter":
_view.SetTestFilterVisibility(_model.Settings.Gui.TestTree.ShowFilter);
break;
}
};
// View context commands
// Test for null is a hack that allows us to avoid
// a problem under Linux creating a ContextMenuStrip
// when no display is present.
_view.ContextMenuOpening += (s, e) => InitializeContextMenu();
_view.CollapseAllCommand.Execute += () => _view.CollapseAll();
_view.ExpandAllCommand.Execute += () => _view.ExpandAll();
_view.CollapseToFixturesCommand.Execute += () => Strategy.CollapseToFixtures();
_view.ShowCheckBoxes.CheckedChanged += () =>
{
_view.CheckBoxes = _view.ShowCheckBoxes.Checked;
};
_view.ShowTestDuration.CheckedChanged += () =>
{
_treeSettings.ShowTestDuration = _view.ShowTestDuration.Checked;
Strategy?.UpdateTreeNodeNames();
};
_view.RunContextCommand.Execute += () =>
{
if (_view.ContextNode != null)
{
if (_view.ContextNode.Tag is TestNode testNode)
_model.RunTests(testNode);
else if (_view.ContextNode.Tag is TestGroup groupNode)
_model.RunTests(groupNode);
}
};
_view.TreeNodeDoubleClick += (treeNode) =>
{
var testNode = treeNode.Tag as TestNode;
if (testNode != null && testNode.Type == "TestCase")
_model.RunTests(testNode);
};
_view.DebugContextCommand.Execute += () =>
{
if (_view.ContextNode != null)
{
var testNode = _view.ContextNode.Tag as TestNode;
if (testNode != null)
_model.DebugTests(testNode);
}
};
_view.TestPropertiesCommand.Execute += () => ShowPropertiesDisplay();
_view.ViewAsXmlCommand.Execute += () => ShowXmlDisplayDialog();
_view.SelectedNodeChanged += (treeNode) =>
{
var testItem = treeNode.Tag as ITestItem;
if (testItem != null)
{
_model.ActiveTestItem = testItem;
// Selected item is either a TestSelection or a TestNode. When
// CheckBoxes are off, the active item is used as the selection.
var selection = testItem as TestSelection;
var node = testItem as TestNode;
if (!_view.CheckBoxes)
{
// If it's a TestNode, make a TestSelection
if (selection == null && node != null)
selection = new TestSelection() { node };
_model.SelectedTests = selection;
}
if (_propertiesDisplay != null)
{
if (_propertiesDisplay.Pinned)
_propertiesDisplay.Display(treeNode);
else
ClosePropertiesDisplay();
}
if (_xmlDisplay != null)
{
if (_xmlDisplay.Pinned)
_xmlDisplay.Display(treeNode);
else
CloseXmlDisplay();
}
}
};
_view.AfterCheck += (treeNode) =>
{
var checkedNodes = _view.CheckedNodes;
var selection = new TestSelection();
foreach (var node in checkedNodes)
selection.Add(node.Tag as ITestItem);
_model.SelectedTests = selection;
};
_view.OutcomeFilter.SelectionChanged += () =>
{
var filter = _view.OutcomeFilter.SelectedItems;
_model.TestCentricTestFilter.OutcomeFilter = filter;
};
_view.TextFilter.Changed += () =>
{
var text = _view.TextFilter.Text;
_model.TestCentricTestFilter.TextFilter = text;
};
// Node selected in tree
//_treeView.SelectedNodesChanged += (nodes) =>
//{
// var selection = new TestSelection();
// foreach (TreeNode tn in nodes)
// {
// var test = tn.Tag as TestNode;
// if (test != null)
// selection.Add(test);
// _model.NotifyTestSelectionChanged(selection);
// }
// if (_propertiesDisplay != null)
// {
// if (_propertiesDisplay.Pinned && nodes.Count == 1)
// _propertiesDisplay.Display(nodes[0]);
// else
// ClosePropertiesDisplay();
// }
// if (_xmlDisplay != null)
// {
// if (_xmlDisplay.Pinned && nodes.Count == 1)
// _xmlDisplay.Display(nodes[0]);
// else
// CloseXmlDisplay();
// }
//};
}
private void UpdateTreeSettingsFromVisualState(VisualState visualState)
{
_treeSettings.DisplayFormat = visualState.DisplayStrategy;
if (visualState.DisplayStrategy == "TEST_LIST")
{
_treeSettings.TestList.GroupBy = visualState.GroupBy;
}
else if (visualState.DisplayStrategy == "FIXTURE_LIST")
{
_treeSettings.FixtureList.GroupBy = visualState.GroupBy;
}
_treeSettings.ShowNamespace = visualState.ShowNamespace;
}
private void EnsureNonRunnableFilesAreVisible(TestNode testNode)
{
// HACK: Temporary fix switches the display strategy if no
// tests are found. Should handle other error situations
// including one non-runnable file out of several files.
if (testNode.TestCount == 0)
Strategy = _treeDisplayStrategyFactory.Create("NUNIT_TREE", _view, _model);
}
private void OnTestFinished(TestResultEventArgs args)
{
Strategy.OnTestFinished(args.Result);
_propertiesDisplay?.OnTestFinished(args.Result);
_xmlDisplay?.OnTestFinished(args.Result);
}
private bool TryLoadVisualState(out VisualState visualState)
{
visualState = null;
if (_model.TestCentricProject.TestFiles.Count > 0)
{
var filename = VisualState.GetVisualStateFileName(_model.TestCentricProject.TestFiles[0]);
if (File.Exists(filename))
visualState = VisualState.LoadFrom(filename);
}
return visualState != null;
}
TestPropertiesDialog _propertiesDisplay;
private void ShowPropertiesDisplay()
{
if (_propertiesDisplay == null)
{
var mainForm = ((Control)_view).FindForm();
_propertiesDisplay = new TestPropertiesDialog(_model, _view)
{
Owner = mainForm,
Font = mainForm.Font,
StartPosition = FormStartPosition.Manual
};
var midScreen = Screen.FromHandle(mainForm.Handle).WorkingArea.Width / 2;
var midForm = (mainForm.Left + mainForm.Right) / 2;
_propertiesDisplay.Left = midForm < midScreen
? mainForm.Right
: Math.Max(0, mainForm.Left - _propertiesDisplay.Width);
_propertiesDisplay.Top = mainForm.Top;
_propertiesDisplay.Closed += (s, e) => _propertiesDisplay = null;
}
_propertiesDisplay.Display(_view.ContextNode);
}
private void ClosePropertiesDisplay()
{
if (_propertiesDisplay != null)
{
_propertiesDisplay.InvokeIfRequired(() => _propertiesDisplay.Close());
_propertiesDisplay = null;
}
}
private void CheckPropertiesDisplay()
{
if (_propertiesDisplay != null && !_propertiesDisplay.Pinned)
ClosePropertiesDisplay();
}
private XmlDisplay _xmlDisplay;
private void ShowXmlDisplayDialog()
{
if (_xmlDisplay == null)
{
var treeView = (Control)_view;
var mainForm = treeView.FindForm();
_xmlDisplay = new XmlDisplay(_model)
{
Owner = mainForm,
Font = mainForm.Font,
StartPosition = FormStartPosition.Manual
};
var midForm = (mainForm.Left + mainForm.Right) / 2;
var screenArea = Screen.FromHandle(mainForm.Handle).WorkingArea;
var midScreen = screenArea.Width / 2;
var myLeft = mainForm.Left;
var myRight = mainForm.Right;
if (_propertiesDisplay != null)
{
myLeft = Math.Min(myLeft, _propertiesDisplay.Left);
myRight = Math.Max(myRight, _propertiesDisplay.Right);
}
_xmlDisplay.Left = myLeft > screenArea.Width - myRight
? Math.Max(0, myLeft - _xmlDisplay.Width)
: Math.Min(myRight, screenArea.Width - _xmlDisplay.Width);
_xmlDisplay.Top = mainForm.Top + (mainForm.Height - _xmlDisplay.Height) / 2;
_xmlDisplay.Closed += (s, e) => _xmlDisplay = null;
}
_xmlDisplay.Display(_view.ContextNode);
}
private void CloseXmlDisplay()
{
_xmlDisplay?.InvokeIfRequired(() => _xmlDisplay?.Close());
}
private void CheckXmlDisplay()
{
if (_xmlDisplay != null && !_xmlDisplay.Pinned)
CloseXmlDisplay();
}
private void InitializeContextMenu()
{
// TODO: Config Menu is hidden until changing the config actually works
bool displayConfigMenu = false;
//var test = _treeView.ContextNode?.Tag as TestNode;
//if (test != null && test.IsProject)
//{
// NUnit.Engine.TestPackage package = _model.GetPackageForTest(test.Id);
// string activeConfig = package.GetActiveConfig();
// string[] configNames = package.GetConfigNames();
// if (configNames.Length > 0)
// {
// _treeView.ActiveConfiguration.MenuItems.Clear();
// foreach (string config in configNames)
// {
// var configEntry = new ToolStripMenuItem(config);
// configEntry.Checked = config == activeConfig;
// configEntry.Click += (sender, e) => _model.ReloadPackage(package, ((ToolStripMenuItem)sender).Text);
// _treeView.ActiveConfiguration.MenuItems.Add(configEntry);
// }
// //displayConfigMenu = true;
// }
//}
_view.ActiveConfiguration.Visible = displayConfigMenu;
var layout = _model.Settings.Gui.GuiLayout;
_view.TestPropertiesCommand.Visible = layout == "Mini";
// If a test is already running, no new test run should be started.
_view.RunContextCommand.Enabled = _model.HasTests && !_model.IsTestRunning;
_view.DebugContextCommand.Enabled = _model.HasTests && !_model.IsTestRunning;
}
#endregion
}
}