1
// ----------------------------------------------------------------
4
// Copyright 2008, Irénée HOTTIER,
6
// This is free software licensed under the NUnit license, You may
7
// obtain a copy of the license at http://nunit.org/?p=license&r=2.4
8
// ----------------------------------------------------------------
11
using System.Collections.Generic;
13
using NUnit.Framework;
14
using NUnit.UiException;
15
using NUnit.TestUtilities;
16
using System.Windows.Forms;
17
using NUnit.UiException.Tests.data;
19
using NUnit.UiException.Controls;
21
namespace NUnit.UiException.Tests.Controls
24
public class TestCodeBoxComposite
26
TestResource _textCodeFile;
28
private TestingCodeBoxComposite _empty;
29
private TestingCodeBoxComposite _filled;
31
private ExceptionItem _existingItem;
32
private ExceptionItem _unknownItem;
34
private int _leaveNotification;
39
_empty = new TestingCodeBoxComposite();
40
_filled = new TestingCodeBoxComposite();
42
_textCodeFile = new TestResource("TextCode.txt");
43
_existingItem = new ExceptionItem(_textCodeFile.Path, 13);
44
_unknownItem = new ExceptionItem("unknown_file.txt", 1);
46
_filled.ExceptionSource = _existingItem;
48
Assert.That(_empty.CodeBox, Is.Not.Null);
49
Assert.That(_empty.ScrollingDistance, Is.GreaterThan(0));
51
_filled.MouseLeaveWindow += new EventHandler(_filled_CursorLeaveWindow);
57
public void TearDown()
59
if (_textCodeFile != null)
61
_textCodeFile.Dispose();
66
void _filled_CursorLeaveWindow(object sender, EventArgs e)
72
public void Test_Can_Set_ItemSource()
74
Assert.That(_empty.ExceptionSource, Is.Null);
75
Assert.That(_empty.CodeBox.Text, Is.EqualTo(""));
77
_empty.ExceptionSource = _existingItem;
78
Assert.That(_empty.ExceptionSource, Is.EqualTo(_existingItem));
79
Assert.That(_empty.CodeBox.HighlightedLine, Is.EqualTo(13), "bad highlighted line");
80
Assert.That(_empty.ErrorMessage, Is.Null);
82
_empty.ExceptionSource = _unknownItem;
83
Assert.That(_empty.ExceptionSource, Is.EqualTo(_unknownItem));
84
Assert.That(_empty.ErrorMessage, Is.Not.Null);
85
Assert.That(_empty.ErrorMessage, Text.StartsWith("Fail to open file"));
88
_empty.ExceptionSource = null;
89
Assert.That(_empty.CodeBox.CurrentLineNumber, Is.EqualTo(0));
95
public void Test_HandleMouseHoverLeft()
97
Assert.That(_filled.Translation, Is.EqualTo(CodeBoxComposite.TranslationDirection.None));
98
Assert.That(_filled.Timer.Enabled, Is.False);
100
_filled.HandleMouseHoverLeft();
102
Assert.That(_filled.Translation, Is.EqualTo(CodeBoxComposite.TranslationDirection.Left));
103
Assert.That(_filled.Timer.Enabled, Is.True);
109
public void Test_HandleMouseHoverRight()
111
Assert.That(_filled.Translation, Is.EqualTo(CodeBoxComposite.TranslationDirection.None));
112
Assert.That(_filled.Timer.Enabled, Is.False);
114
_filled.HandleMouseHoverRight();
116
Assert.That(_filled.Translation, Is.EqualTo(CodeBoxComposite.TranslationDirection.Right));
117
Assert.That(_filled.Timer.Enabled, Is.True);
123
public void Test_HandleMouseHoverUp()
125
Assert.That(_filled.Translation, Is.EqualTo(CodeBoxComposite.TranslationDirection.None));
126
Assert.That(_filled.Timer.Enabled, Is.False);
128
_filled.HandleMouseHoverUp();
130
Assert.That(_filled.Translation, Is.EqualTo(CodeBoxComposite.TranslationDirection.Up));
131
Assert.That(_filled.Timer.Enabled, Is.True);
137
public void Test_HandleTimerTick_Up()
141
startingLine = _filled.CodeBox.CurrentLineNumber;
142
_filled.ScrollingDistance = _filled.CodeBox.Viewport.CharHeight;
144
_filled.HandleMouseHoverUp();
145
Assert.That(_filled.CodeBox.CurrentLineNumber, Is.EqualTo(startingLine));
147
_filled.HandleTimerTick();
148
Assert.That(_filled.CodeBox.CurrentLineNumber, Is.EqualTo(startingLine - 1));
150
_filled.HandleTimerTick();
151
Assert.That(_filled.CodeBox.CurrentLineNumber, Is.EqualTo(startingLine - 2));
153
for (int i = 0; i < 200; ++i)
154
_filled.HandleTimerTick();
156
Assert.That(_filled.CodeBox.CurrentLineNumber, Is.EqualTo(1));
157
Assert.That(_filled.Timer.Enabled, Is.False);
163
public void Test_ScrollingDistance()
167
startingLine = _filled.CodeBox.CurrentLineNumber;
169
_filled.ScrollingDistance = 2;
170
Assert.That(_filled.ScrollingDistance, Is.EqualTo(2));
172
_filled.ScrollingDistance = _filled.CodeBox.Viewport.CharHeight + 0.5d;
173
Assert.That(_filled.CodeBox.CurrentLineNumber, Is.EqualTo(startingLine));
175
_filled.CodeBox.TranslateView(0, _filled.ScrollingDistance);
176
Assert.That(_filled.CodeBox.CurrentLineNumber, Is.EqualTo(startingLine + 1));
182
public void Test_HandleMouseHoverDown()
184
Assert.That(_filled.Translation, Is.EqualTo(CodeBoxComposite.TranslationDirection.None));
185
Assert.That(_filled.Timer.Enabled, Is.False);
187
_filled.HandleMouseHoverDown();
189
Assert.That(_filled.Translation, Is.EqualTo(CodeBoxComposite.TranslationDirection.Down));
190
Assert.That(_filled.Timer.Enabled, Is.True);
194
public void Test_HandleTimerTick_Left()
196
_filled.CodeBox.Viewport.Location = new PointF(3, 0);
198
_filled.ScrollingDistance = 2;
200
_filled.HandleMouseHoverLeft();
202
_filled.HandleTimerTick();
203
Assert.That(_filled.CodeBox.Viewport.Location, Is.EqualTo(new PointF(1, 0)));
204
Assert.That(_filled.Timer.Enabled, Is.True);
206
_filled.HandleTimerTick();
207
Assert.That(_filled.CodeBox.Viewport.Location, Is.EqualTo(new PointF(0, 0)));
208
Assert.That(_filled.Timer.Enabled, Is.False);
214
public void Test_HandleTimerTick_Right()
220
w = _filled.CodeBox.Viewport.Width;
221
textwidth = _filled.CodeBox.Viewport.TextSource.MaxLength * _filled.CodeBox.Viewport.CharWidth;
223
_filled.CodeBox.Viewport.Location = new PointF(0, 0);
225
_filled.ScrollingDistance = 2;
227
_filled.HandleMouseHoverRight();
229
_filled.HandleTimerTick();
230
Assert.That(_filled.CodeBox.Viewport.Location, Is.EqualTo(new PointF(2, 0)));
231
Assert.That(_filled.Timer.Enabled, Is.True);
233
_filled.HandleTimerTick();
234
Assert.That(_filled.CodeBox.Viewport.Location, Is.EqualTo(new PointF(4, 0)));
236
_filled.ScrollingDistance = 100;
238
for (i = 0; i < 100; ++i)
240
_filled.HandleTimerTick();
243
Assert.That(_filled.CodeBox.Viewport.Location.X, Is.LessThanOrEqualTo(textwidth - w));
244
Assert.That(_filled.Timer.Enabled, Is.False);
250
public void Test_HandleMouseHoverCode()
252
_filled.HandleMouseHoverUp();
254
_filled.HandleMouseHoverCode();
256
Assert.That(_filled.Translation, Is.EqualTo(CodeBoxComposite.TranslationDirection.None));
257
Assert.That(_filled.Timer.Enabled, Is.False);
263
public void Test_HandleTimerTick_Down()
267
startingLine = _filled.CodeBox.CurrentLineNumber;
269
_filled.ScrollingDistance = _filled.CodeBox.Viewport.CharHeight + 0.5d;
271
_filled.HandleMouseHoverDown();
272
Assert.That(_filled.CodeBox.CurrentLineNumber, Is.EqualTo(startingLine));
274
_filled.HandleTimerTick();
275
Assert.That(_filled.CodeBox.CurrentLineNumber, Is.EqualTo(startingLine + 1));
277
_filled.HandleTimerTick();
278
Assert.That(_filled.CodeBox.CurrentLineNumber, Is.EqualTo(startingLine + 2));
280
for (int i = 0; i < 200; ++i)
281
_filled.HandleTimerTick();
283
Assert.That(_filled.CodeBox.CurrentLineNumber, Is.EqualTo(42));
284
Assert.That(_filled.Timer.Enabled, Is.False);
290
public void Test_Yield_Focus_To_CodeBox()
292
_filled.ActiveControl = null;
293
Assert.That(_filled.ActiveControl, Is.Null);
295
_filled.HandleMouseHoverCode();
297
Assert.That(_filled.ActiveControl, Is.EqualTo(_filled.CodeBox));
303
public void Test_Ability_To_Disable_HoverButtons()
309
_filled.CodeBox.Viewport.SetPosition(0, 0);
310
Assert.That(_filled.BtnUp.Enabled, Is.False);
311
Assert.That(_filled.BtnLeft.Enabled, Is.False);
312
Assert.That(_filled.BtnRight.Enabled, Is.True);
313
Assert.That(_filled.BtnDown.Enabled, Is.True);
315
_filled.CodeBox.Viewport.SetPosition(max, 0);
316
Assert.That(_filled.BtnUp.Enabled, Is.False);
317
Assert.That(_filled.BtnLeft.Enabled, Is.True);
318
Assert.That(_filled.BtnRight.Enabled, Is.False);
319
Assert.That(_filled.BtnDown.Enabled, Is.True);
321
_filled.CodeBox.Viewport.SetPosition(0, max);
322
Assert.That(_filled.BtnUp.Enabled, Is.True);
323
Assert.That(_filled.BtnLeft.Enabled, Is.False);
324
Assert.That(_filled.BtnRight.Enabled, Is.True);
325
Assert.That(_filled.BtnDown.Enabled, Is.False);
331
public void Test_HandleLeaveEvent()
333
_filled.HandleMouseHoverDown();
334
Assert.That(_filled.Translation, Is.EqualTo(CodeBoxComposite.TranslationDirection.Down));
335
Assert.That(_filled.Timer.Enabled, Is.True);
337
Assert.That(_leaveNotification, Is.EqualTo(0));
339
_filled.HandleLeaveEvent();
341
Assert.That(_filled.Translation, Is.EqualTo(CodeBoxComposite.TranslationDirection.None));
342
Assert.That(_filled.Timer.Enabled, Is.False);
344
Assert.That(_leaveNotification, Is.EqualTo(1));
351
class TestingCodeBoxComposite :
354
public new string ErrorMessage {
355
get { return (base.ErrorMessage); }
358
public new CodeBox CodeBox {
359
get { return (base.CodeBox); }
362
public new void HandleMouseHoverUp() {
363
base.HandleMouseHoverUp();
366
public new void HandleMouseHoverDown() {
367
base.HandleMouseHoverDown();
370
public new void HandleMouseHoverLeft() {
371
base.HandleMouseHoverLeft();
374
public new void HandleMouseHoverRight() {
375
base.HandleMouseHoverRight();
378
public new void HandleMouseHoverCode() {
379
base.HandleMouseHoverCode();
382
public new TranslationDirection Translation {
383
get { return (base.Translation); }
386
public new void HandleLeaveEvent()
388
base.HandleLeaveEvent();
391
public new Timer Timer {
392
get { return (base.Timer); }
395
public new void HandleTimerTick() {
396
base.HandleTimerTick();
399
public new HoverButton BtnUp {
400
get { return (base.BtnUp); }
403
public new HoverButton BtnLeft {
404
get { return (base.BtnLeft); }
407
public new HoverButton BtnRight {
408
get { return (base.BtnRight); }
411
public new HoverButton BtnDown {
412
get { return (base.BtnDown); }