-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathDataTreeTests.cs
More file actions
488 lines (432 loc) · 17.6 KB
/
DataTreeTests.cs
File metadata and controls
488 lines (432 loc) · 17.6 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
478
479
480
481
482
483
484
485
486
487
488
// Copyright (c) 2016 SIL International
// This software is licensed under the LGPL, version 2.1 or later
// (http://www.gnu.org/licenses/lgpl-2.1.html)
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using System.Xml;
using NUnit.Framework;
using SIL.LCModel.Core.Text;
using SIL.FieldWorks.Common.FwUtils;
using SIL.LCModel;
using SIL.LCModel.Core.Cellar;
using SIL.LCModel.Infrastructure;
using SIL.LCModel.Utils;
using XCore;
namespace SIL.FieldWorks.Common.Framework.DetailControls
{
/// <summary></summary>
[TestFixture]
public class DataTreeTests : MemoryOnlyBackendProviderRestoredForEachTestTestBase
{
private Inventory m_parts;
private Inventory m_layouts;
private ILexEntry m_entry; // test object.
private Mediator m_mediator;
private PropertyTable m_propertyTable;
private DataTree m_dtree;
private Form m_parent;
private sealed class ScrollTestDataTree : DataTree
{
protected override void OnPaint(PaintEventArgs e)
{
}
}
private CustomFieldForTest m_customField;
#region Fixture Setup and Teardown
internal static Inventory GenerateParts()
{
string partDirectory = Path.Combine(FwDirectoryFinder.SourceDirectory,
@"Common/Controls/DetailControls/DetailControlsTests");
var keyAttrs = new Dictionary<string, string[]>();
keyAttrs["part"] = new[] {"id"};
var parts = new Inventory(new string[] {partDirectory},
"*Parts.xml", "/PartInventory/bin/*", keyAttrs, "DetailTreeTests", Path.GetTempPath());
return parts;
}
internal static Inventory GenerateLayouts()
{
string partDirectory = Path.Combine(FwDirectoryFinder.SourceDirectory,
@"Common/Controls/DetailControls/DetailControlsTests");
Dictionary<string, string[]> keyAttrs = new Dictionary<string, string[]>();
keyAttrs["layout"] = new[] {"class", "type", "name" };
keyAttrs["group"] = new[] {"label"};
keyAttrs["part"] = new[] {"ref"};
var layouts = new Inventory(new[] {partDirectory},
"*.fwlayout", "/LayoutInventory/*", keyAttrs, "DetailTreeTests", Path.GetTempPath());
return layouts;
}
/// ------------------------------------------------------------------------------------
/// <summary>
/// Setups this instance.
/// </summary>
/// ------------------------------------------------------------------------------------
public override void FixtureSetup()
{
base.FixtureSetup();
m_layouts = GenerateLayouts();
m_parts = GenerateParts();
}
#endregion
private static DataTree CreateScrollableDataTree(Form parent)
{
var dataTree = new ScrollTestDataTree();
parent.Size = new Size(400, 200);
dataTree.Dock = DockStyle.Fill;
parent.Controls.Add(dataTree);
for (int i = 0; i < 12; i++)
{
var slice = new Slice(new Panel { Dock = DockStyle.Fill })
{
Visible = true,
Size = new Size(360, 50),
Location = new Point(0, i * 50)
};
dataTree.Controls.Add(slice);
slice.Install(dataTree);
}
parent.Show();
Application.DoEvents();
return dataTree;
}
#region Test setup and teardown
/// ------------------------------------------------------------------------------------
/// <summary>
/// Create DataTree and parent form
/// </summary>
/// ------------------------------------------------------------------------------------
public override void TestSetup()
{
base.TestSetup();
m_customField = new CustomFieldForTest(Cache, "testField", "testField", LexEntryTags.kClassId,
CellarPropertyType.String, Guid.Empty);
// base.TestSetup() may already start a unit-of-work; avoid nesting NonUndoable tasks.
m_entry = Cache.ServiceLocator.GetInstance<ILexEntryFactory>().Create();
m_entry.CitationForm.VernacularDefaultWritingSystem = TsStringUtils.MakeString("rubbish", Cache.DefaultVernWs);
// We set both alternatives because currently the default part for Bibliography uses vernacular,
// but I think this will probably get fixed. Anyway, this way the test is robust.
m_entry.Bibliography.SetAnalysisDefaultWritingSystem("My rubbishy bibliography");
m_entry.Bibliography.SetVernacularDefaultWritingSystem("My rubbishy bibliography");
m_dtree = new DataTree();
m_mediator = new Mediator();
m_propertyTable = new PropertyTable(m_mediator);
m_dtree.Init(m_mediator, m_propertyTable, null);
m_parent = new Form();
m_parent.Controls.Add(m_dtree);
}
/// ------------------------------------------------------------------------------------
/// <summary>
/// Close and dispose DataTree and parent form
/// </summary>
/// ------------------------------------------------------------------------------------
public override void TestTearDown()
{
if (m_customField != null && Cache != null && Cache.MainCacheAccessor.MetaDataCache != null)
{
m_customField.Dispose();
m_customField = null;
}
// m_dtree gets disposed from m_parent because it's part of its Controls
if (m_parent != null)
{
m_parent.Close();
m_parent.Dispose();
}
if (m_propertyTable != null)
{
m_propertyTable.Dispose();
m_propertyTable = null;
}
if (m_mediator != null)
{
m_mediator.Dispose();
m_mediator = null;
}
base.TestTearDown();
}
#endregion
/// <summary></summary>
[Test]
public void OneStringAttr()
{
m_dtree.Initialize(Cache, false, m_layouts, m_parts);
m_dtree.ShowObject(m_entry, "CfOnly", null, m_entry, false);
Assert.That(m_dtree.Controls.Count, Is.EqualTo(1));
Assert.That((m_dtree.Controls[0] as Slice).Label, Is.EqualTo("CitationForm"));
// Enhance JohnT: there are more things we could test about this slice,
// such as the presence and contents and initial selection of the view,
// but this round of tests is mainly aimed at the process of interpreting
// layouts and parts to get slices.
}
/// <summary></summary>
[Test]
public void TwoStringAttr()
{
m_dtree.Initialize(Cache, false, m_layouts, m_parts);
m_dtree.ShowObject(m_entry, "CfAndBib", null, m_entry, false);
Assert.That(m_dtree.Controls.Count, Is.EqualTo(2));
Assert.That((m_dtree.Controls[0] as Slice).Label, Is.EqualTo("CitationForm"));
Assert.That((m_dtree.Controls[1] as Slice).Label, Is.EqualTo("Bibliography"));
}
/// <summary></summary>
[Test]
public void LabelAbbreviations()
{
m_dtree.Initialize(Cache, false, m_layouts, m_parts);
m_dtree.ShowObject(m_entry, "Abbrs", null, m_entry, false);
Assert.That(m_dtree.Controls.Count, Is.EqualTo(3));
// 1) Test that labels that are not in "LabelAbbreviations" stringTable
// are abbreviated by being truncated to 4 characters.
Assert.That((m_dtree.Controls[0] as Slice).Label, Is.EqualTo("CitationForm"));
string abbr1 = StringTable.Table.GetString((m_dtree.Controls[0] as Slice).Label, "LabelAbbreviations");
Assert.That(abbr1, Is.EqualTo("*" + (m_dtree.Controls[0] as Slice).Label + "*")); // verify it's not in the table.
Assert.That((m_dtree.Controls[0] as Slice).Abbreviation, Is.EqualTo("Cita")); // verify truncation took place.
// 2) Test that a label in "LabelAbbreviations" defaults to its string table entry.
Assert.That((m_dtree.Controls[1] as Slice).Label, Is.EqualTo("Citation Form"));
string abbr2 = StringTable.Table.GetString((m_dtree.Controls[1] as Slice).Label, "LabelAbbreviations");
Assert.That(abbr2 == "*" + (m_dtree.Controls[1] as Slice).Label + "*", Is.False); // verify it IS in the table
Assert.That((m_dtree.Controls[1] as Slice).Abbreviation, Is.EqualTo(abbr2)); // should be identical
// 3) Test that a label with an "abbr" attribute overrides default abbreviation.
Assert.That((m_dtree.Controls[2] as Slice).Label, Is.EqualTo("Citation Form"));
Assert.That((m_dtree.Controls[2] as Slice).Abbreviation, Is.EqualTo("!?"));
Assert.That(abbr2 == (m_dtree.Controls[2] as Slice).Abbreviation, Is.False);
}
/// <summary></summary>
[Test]
public void IfDataEmpty()
{
string anaWsText = m_entry.Bibliography.AnalysisDefaultWritingSystem.Text;
string vernWsText = m_entry.Bibliography.VernacularDefaultWritingSystem.Text;
try
{
m_entry.Bibliography.SetAnalysisDefaultWritingSystem("");
m_entry.Bibliography.SetVernacularDefaultWritingSystem("");
m_dtree.Initialize(Cache, false, m_layouts, m_parts);
m_dtree.ShowObject(m_entry, "CfAndBib", null, m_entry, false);
Assert.That(m_dtree.Controls.Count, Is.EqualTo(1));
Assert.That((m_dtree.Controls[0] as Slice).Label, Is.EqualTo("CitationForm"));
}
finally
{
m_entry.Bibliography.SetAnalysisDefaultWritingSystem(anaWsText);
m_entry.Bibliography.SetVernacularDefaultWritingSystem(vernWsText);
}
}
/// <summary></summary>
[Test]
public void NestedExpandedPart()
{
m_dtree.Initialize(Cache, false, m_layouts, m_parts);
m_dtree.ShowObject(m_entry, "Nested-Expanded", null, m_entry, false);
Assert.That(m_dtree.Controls.Count, Is.EqualTo(3));
Assert.That((m_dtree.Controls[0] as Slice).Label, Is.EqualTo("Header"));
Assert.That((m_dtree.Controls[1] as Slice).Label, Is.EqualTo("Citation form"));
Assert.That((m_dtree.Controls[2] as Slice).Label, Is.EqualTo("Bibliography"));
Assert.That((m_dtree.Controls[1] as Slice).Indent, Is.EqualTo(0)); // was 1, but indent currently suppressed.
}
/// <summary>Remove duplicate custom field placeholder parts</summary>
[Test]
public void RemoveDuplicateCustomFields()
{
m_dtree.Initialize(Cache, false, m_layouts, m_parts);
m_dtree.ShowObject(m_entry, "Normal", null, m_entry, false);
var template = m_dtree.GetTemplateForObjLayout(m_entry, "Normal", null);
var expected = "<layout class=\"LexEntry\" type=\"detail\" name=\"Normal\"><part ref=\"_CustomFieldPlaceholder\" customFields=\"here\" /><part ref=\"Custom\" param=\"testField\" /></layout>";
Assert.That(expected, Is.EqualTo(template.OuterXml), "Exactly one part with a _CustomFieldPlaceholder ref attribute should exist.");
}
[Test]
public void BadCustomFieldPlaceHoldersAreCorrected()
{
m_dtree.Initialize(Cache, false, m_layouts, m_parts);
m_dtree.ShowObject(m_entry, "NoRef", null, m_entry, false);
var template = m_dtree.GetTemplateForObjLayout(m_entry, "NoRef", null);
var expected = "<layout class=\"LexEntry\" type=\"detail\" name=\"NoRef\"><part customFields=\"here\" ref=\"_CustomFieldPlaceholder\" /><part ref=\"Custom\" param=\"testField\" /></layout>";
Assert.That(expected, Is.EqualTo(template.OuterXml), "The previously empty ref on the customFields=\"here\" part should be _CustomFieldPlaceholder.");
}
/// <summary></summary>
[Test]
[Ignore("Collapsed nodes are currently not implemented")]
public void NestedCollapsedPart()
{
m_dtree.Initialize(Cache, false, m_layouts, m_parts);
m_dtree.ShowObject(m_entry, "Nested-Collapsed", null, m_entry, false);
Assert.That(m_dtree.Controls.Count, Is.EqualTo(1));
Assert.That((m_dtree.Controls[0] as Slice).Label, Is.EqualTo("Header"));
}
[Test]
public void GetGuidForJumpToTool_UsesRootObject_WhenNoCurrentSlice()
{
m_dtree.Initialize(Cache, false, m_layouts, m_parts);
m_dtree.ShowObject(m_entry, "OptSensesEty", null, m_entry, false);
Assert.That(m_dtree.CurrentSlice == null, "may be OK to automatically select a slice...but this test will need rewriting");
var doc = new XmlDocument();
doc.LoadXml(
@"<command id='CmdRootEntryJumpToConcordance' label='Show Entry in Concordance' message='JumpToTool'>
<parameters tool='concordance' className='LexEntry'/>
</command>");
using (var cmd = new Command(null, doc.DocumentElement))
{
string tool;
var guid = m_dtree.GetGuidForJumpToTool(cmd, true, out tool);
Assert.That(guid, Is.EqualTo(m_dtree.Root.Guid));
}
}
[Test]
public void GetWheelScrollPixels_UsesSystemWheelSettings()
{
m_dtree.Bounds = new Rectangle(0, 0, 200, 100);
int delta = SystemInformation.MouseWheelScrollDelta;
int scrollLines = SystemInformation.MouseWheelScrollLines;
int expectedPixels;
if (scrollLines == 0)
{
expectedPixels = 0;
}
else if (scrollLines == int.MaxValue)
{
expectedPixels = m_dtree.ClientRectangle.Height;
}
else
{
expectedPixels = (int)Math.Round((double)scrollLines * m_dtree.Font.Height,
MidpointRounding.AwayFromZero);
}
Assert.That(DataTree.GetWheelScrollPixels(m_dtree, delta), Is.EqualTo(expectedPixels));
Assert.That(DataTree.GetWheelScrollPixels(m_dtree, -delta), Is.EqualTo(-expectedPixels));
}
[Test]
public void TryGetWheelScrollPosition_ReturnsFalse_WhenAlreadyAtTop()
{
m_dtree.Bounds = new Rectangle(0, 0, 200, 100);
m_dtree.AutoScrollMinSize = new Size(200, 1000);
m_dtree.AutoScrollPosition = new Point(0, 0);
int newY;
bool handled = DataTree.TryGetWheelScrollPosition(m_dtree,
SystemInformation.MouseWheelScrollDelta, out newY);
Assert.That(handled, Is.False);
Assert.That(newY, Is.EqualTo(0));
}
[Test]
public void CanRedirectWheelMessage_ReturnsFalse_WhenDataTreeHidden()
{
m_parent.Show();
m_dtree.Show();
Assert.That(m_dtree.IsHandleCreated, Is.True);
m_dtree.Hide();
Assert.That(DataTree.CanRedirectWheelMessage(m_dtree), Is.False);
}
[Test]
public void TryHandleWheelScroll_UpdatesScrollPosition_WhenScrollingIsPossible()
{
using (var parent = new Form())
{
var dataTree = CreateScrollableDataTree(parent);
dataTree.AutoScrollPosition = new Point(0, 0);
bool handled = DataTree.TryHandleWheelScroll(dataTree, -SystemInformation.MouseWheelScrollDelta);
Assert.That(handled, Is.True);
Assert.That(-dataTree.AutoScrollPosition.Y, Is.GreaterThan(0));
}
}
[Test]
public void TryScrollOwningDataTree_ScrollsContainingDataTree_FromDateSliceHostedControl()
{
using (var parent = new Form())
{
var dataTree = CreateScrollableDataTree(parent);
dataTree.AutoScrollPosition = new Point(0, 0);
using (var host = new Panel())
using (var control = new RichTextBox())
{
host.Bounds = new Rectangle(0, 0, 150, 20);
control.Dock = DockStyle.Fill;
host.Controls.Add(control);
dataTree.Controls.Add(host);
host.Show();
control.Show();
Application.DoEvents();
bool handled = DateSlice.TryScrollOwningDataTree(control,
-SystemInformation.MouseWheelScrollDelta);
Assert.That(handled, Is.True);
Assert.That(-dataTree.AutoScrollPosition.Y, Is.GreaterThan(0));
}
}
}
/// <summary></summary>
[Test]
public void OwnedObjects()
{
m_dtree.Initialize(Cache, false, m_layouts, m_parts);
m_dtree.ShowObject(m_entry, "OptSensesEty", null, m_entry, false);
// With no etymology or senses, this view contains nothing at all.
Assert.That(m_dtree.Controls.Count, Is.EqualTo(0));
m_parent.Close();
m_parent.Dispose();
m_parent = null;
ILexSense sense1 = Cache.ServiceLocator.GetInstance<ILexSenseFactory>().Create();
m_entry.SensesOS.Add(sense1);
ILexSense sense2 = Cache.ServiceLocator.GetInstance<ILexSenseFactory>().Create();
m_entry.SensesOS.Add(sense2);
Cache.MainCacheAccessor.SetString(sense2.Hvo,
LexSenseTags.kflidScientificName,
TsStringUtils.MakeString("blah blah", Cache.DefaultAnalWs));
m_mediator.Dispose();
m_mediator = new Mediator();
m_propertyTable.Dispose();
m_propertyTable = new PropertyTable(m_mediator);
m_parent = new Form();
m_dtree = new DataTree();
m_dtree.Init(m_mediator, m_propertyTable, null);
m_parent.Controls.Add(m_dtree);
m_dtree.Initialize(Cache, false, m_layouts, m_parts);
m_dtree.ShowObject(m_entry, "OptSensesEty", null, m_entry, false);
// With two senses, we get a header slice, a gloss slice for
// sense 1 (not optional), and both gloss and Scientific name
// slices for sense 2.
Assert.That(m_dtree.Controls.Count, Is.EqualTo(3));
//Assert.That((m_dtree.Controls[0] as Slice).Label, Is.EqualTo("Senses"));
Assert.That((m_dtree.Controls[0] as Slice).Label, Is.EqualTo("Gloss"));
Assert.That((m_dtree.Controls[1] as Slice).Label, Is.EqualTo("Gloss"));
Assert.That((m_dtree.Controls[2] as Slice).Label, Is.EqualTo("ScientificName"));
m_parent.Close();
m_parent.Dispose();
m_parent = null;
var etymology = Cache.ServiceLocator.GetInstance<ILexEtymologyFactory>().Create();
m_entry.EtymologyOS.Add(etymology);
m_mediator.Dispose();
m_mediator = new Mediator();
m_propertyTable.Dispose();
m_propertyTable = new PropertyTable(m_mediator);
m_parent = new Form();
m_dtree = new DataTree();
m_dtree.Init(m_mediator, m_propertyTable, null);
m_parent.Controls.Add(m_dtree);
m_dtree.Initialize(Cache, false, m_layouts, m_parts);
m_dtree.ShowObject(m_entry, "OptSensesEty", null, m_entry, false);
// Adding an etymology gets us just no more slices so far,
// because it doesn't have a form or source
Assert.That(m_dtree.Controls.Count, Is.EqualTo(3));
m_parent.Close();
m_parent.Dispose();
m_parent = null;
etymology.LanguageNotes.AnalysisDefaultWritingSystem = TsStringUtils.MakeString("source language", Cache.DefaultAnalWs);
etymology.Form.VernacularDefaultWritingSystem = TsStringUtils.MakeString("rubbish", Cache.DefaultVernWs);
m_mediator.Dispose();
m_mediator = new Mediator();
m_propertyTable.Dispose();
m_propertyTable = new PropertyTable(m_mediator);
m_parent = new Form();
m_dtree = new DataTree();
m_dtree.Init(m_mediator, m_propertyTable, null);
m_parent.Controls.Add(m_dtree);
m_dtree.Initialize(Cache, false, m_layouts, m_parts);
m_dtree.ShowObject(m_entry, "OptSensesEty", null, m_entry, false);
// When the etymology has something we get two more.
Assert.That(m_dtree.Controls.Count, Is.EqualTo(5));
Assert.That((m_dtree.Controls[3] as Slice).Label, Is.EqualTo("Form"));
Assert.That((m_dtree.Controls[4] as Slice).Label, Is.EqualTo("Source Language Notes"));
}
}
}