~nunit-core/nunitv2/2.5

« back to all changes in this revision

Viewing changes to src/GuiException/tests/Controls/TestCodeBoxComposite.cs

  • Committer: charliepoole
  • Date: 2008-12-11 03:54:36 UTC
  • Revision ID: vcs-imports@canonical.com-20081211035436-thnyyjdctgmlxvot
Exception Browser - Initial Checkin

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// ----------------------------------------------------------------
 
2
// ExceptionBrowser
 
3
// Version 1.0.0
 
4
// Copyright 2008, Irénée HOTTIER,
 
5
// 
 
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
// ----------------------------------------------------------------
 
9
 
 
10
using System;
 
11
using System.Collections.Generic;
 
12
using System.Text;
 
13
using NUnit.Framework;
 
14
using NUnit.UiException;
 
15
using NUnit.TestUtilities;
 
16
using System.Windows.Forms;
 
17
using NUnit.UiException.Tests.data;
 
18
using System.Drawing;
 
19
using NUnit.UiException.Controls;
 
20
 
 
21
namespace NUnit.UiException.Tests.Controls
 
22
{
 
23
    [TestFixture]
 
24
    public class TestCodeBoxComposite
 
25
    {
 
26
        TestResource _textCodeFile;
 
27
 
 
28
        private TestingCodeBoxComposite _empty;
 
29
        private TestingCodeBoxComposite _filled;
 
30
 
 
31
        private ExceptionItem _existingItem;
 
32
        private ExceptionItem _unknownItem;
 
33
 
 
34
        private int _leaveNotification;
 
35
 
 
36
        [SetUp]
 
37
        public void SetUp()
 
38
        {
 
39
            _empty = new TestingCodeBoxComposite();
 
40
            _filled = new TestingCodeBoxComposite();
 
41
 
 
42
            _textCodeFile = new TestResource("TextCode.txt");
 
43
            _existingItem = new ExceptionItem(_textCodeFile.Path, 13);
 
44
            _unknownItem = new ExceptionItem("unknown_file.txt", 1);
 
45
 
 
46
            _filled.ExceptionSource = _existingItem;
 
47
 
 
48
            Assert.That(_empty.CodeBox, Is.Not.Null);
 
49
            Assert.That(_empty.ScrollingDistance, Is.GreaterThan(0));
 
50
 
 
51
            _filled.MouseLeaveWindow += new EventHandler(_filled_CursorLeaveWindow);
 
52
 
 
53
            return;
 
54
        }
 
55
 
 
56
        [TearDown]
 
57
        public void TearDown()
 
58
        {
 
59
            if (_textCodeFile != null)
 
60
            {
 
61
                _textCodeFile.Dispose();
 
62
                _textCodeFile = null;
 
63
            }
 
64
        }
 
65
 
 
66
        void _filled_CursorLeaveWindow(object sender, EventArgs e)
 
67
        {
 
68
            _leaveNotification++;
 
69
        }
 
70
 
 
71
        [Test]
 
72
        public void Test_Can_Set_ItemSource()
 
73
        {
 
74
            Assert.That(_empty.ExceptionSource, Is.Null);
 
75
            Assert.That(_empty.CodeBox.Text, Is.EqualTo(""));
 
76
 
 
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);
 
81
 
 
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"));
 
86
            
 
87
 
 
88
            _empty.ExceptionSource = null;
 
89
            Assert.That(_empty.CodeBox.CurrentLineNumber, Is.EqualTo(0));
 
90
 
 
91
            return;
 
92
        }       
 
93
 
 
94
        [Test]
 
95
        public void Test_HandleMouseHoverLeft()
 
96
        {
 
97
            Assert.That(_filled.Translation, Is.EqualTo(CodeBoxComposite.TranslationDirection.None));
 
98
            Assert.That(_filled.Timer.Enabled, Is.False);
 
99
 
 
100
            _filled.HandleMouseHoverLeft();
 
101
 
 
102
            Assert.That(_filled.Translation, Is.EqualTo(CodeBoxComposite.TranslationDirection.Left));
 
103
            Assert.That(_filled.Timer.Enabled, Is.True);
 
104
 
 
105
            return;
 
106
        }
 
107
 
 
108
        [Test]
 
109
        public void Test_HandleMouseHoverRight()
 
110
        {
 
111
            Assert.That(_filled.Translation, Is.EqualTo(CodeBoxComposite.TranslationDirection.None));
 
112
            Assert.That(_filled.Timer.Enabled, Is.False);
 
113
 
 
114
            _filled.HandleMouseHoverRight();
 
115
 
 
116
            Assert.That(_filled.Translation, Is.EqualTo(CodeBoxComposite.TranslationDirection.Right));
 
117
            Assert.That(_filled.Timer.Enabled, Is.True);
 
118
 
 
119
            return;
 
120
        }
 
121
 
 
122
        [Test]
 
123
        public void Test_HandleMouseHoverUp()
 
124
        {
 
125
            Assert.That(_filled.Translation, Is.EqualTo(CodeBoxComposite.TranslationDirection.None));
 
126
            Assert.That(_filled.Timer.Enabled, Is.False);
 
127
 
 
128
            _filled.HandleMouseHoverUp();
 
129
 
 
130
            Assert.That(_filled.Translation, Is.EqualTo(CodeBoxComposite.TranslationDirection.Up));
 
131
            Assert.That(_filled.Timer.Enabled, Is.True);            
 
132
 
 
133
            return;
 
134
        }
 
135
 
 
136
        [Test]
 
137
        public void Test_HandleTimerTick_Up()
 
138
        {
 
139
            int startingLine;
 
140
 
 
141
            startingLine = _filled.CodeBox.CurrentLineNumber;
 
142
            _filled.ScrollingDistance = _filled.CodeBox.Viewport.CharHeight;            
 
143
 
 
144
            _filled.HandleMouseHoverUp();
 
145
            Assert.That(_filled.CodeBox.CurrentLineNumber, Is.EqualTo(startingLine));
 
146
 
 
147
            _filled.HandleTimerTick();
 
148
            Assert.That(_filled.CodeBox.CurrentLineNumber, Is.EqualTo(startingLine - 1));
 
149
 
 
150
            _filled.HandleTimerTick();
 
151
            Assert.That(_filled.CodeBox.CurrentLineNumber, Is.EqualTo(startingLine - 2));
 
152
 
 
153
            for (int i = 0; i < 200; ++i)
 
154
                _filled.HandleTimerTick();
 
155
 
 
156
            Assert.That(_filled.CodeBox.CurrentLineNumber, Is.EqualTo(1));
 
157
            Assert.That(_filled.Timer.Enabled, Is.False);
 
158
           
 
159
            return;
 
160
        }
 
161
 
 
162
        [Test]
 
163
        public void Test_ScrollingDistance()
 
164
        {
 
165
            int startingLine;
 
166
 
 
167
            startingLine = _filled.CodeBox.CurrentLineNumber;
 
168
 
 
169
            _filled.ScrollingDistance = 2;
 
170
            Assert.That(_filled.ScrollingDistance, Is.EqualTo(2));
 
171
 
 
172
            _filled.ScrollingDistance = _filled.CodeBox.Viewport.CharHeight + 0.5d;
 
173
            Assert.That(_filled.CodeBox.CurrentLineNumber, Is.EqualTo(startingLine));
 
174
 
 
175
            _filled.CodeBox.TranslateView(0, _filled.ScrollingDistance);
 
176
            Assert.That(_filled.CodeBox.CurrentLineNumber, Is.EqualTo(startingLine + 1));
 
177
 
 
178
            return;
 
179
        }
 
180
 
 
181
        [Test]
 
182
        public void Test_HandleMouseHoverDown()
 
183
        {            
 
184
            Assert.That(_filled.Translation, Is.EqualTo(CodeBoxComposite.TranslationDirection.None));
 
185
            Assert.That(_filled.Timer.Enabled, Is.False);
 
186
 
 
187
            _filled.HandleMouseHoverDown();
 
188
 
 
189
            Assert.That(_filled.Translation, Is.EqualTo(CodeBoxComposite.TranslationDirection.Down));
 
190
            Assert.That(_filled.Timer.Enabled, Is.True);
 
191
        }
 
192
 
 
193
        [Test]
 
194
        public void Test_HandleTimerTick_Left()
 
195
        {
 
196
            _filled.CodeBox.Viewport.Location = new PointF(3, 0);
 
197
 
 
198
            _filled.ScrollingDistance = 2;
 
199
 
 
200
            _filled.HandleMouseHoverLeft();
 
201
 
 
202
            _filled.HandleTimerTick();
 
203
            Assert.That(_filled.CodeBox.Viewport.Location, Is.EqualTo(new PointF(1, 0)));
 
204
            Assert.That(_filled.Timer.Enabled, Is.True);
 
205
 
 
206
            _filled.HandleTimerTick();
 
207
            Assert.That(_filled.CodeBox.Viewport.Location, Is.EqualTo(new PointF(0, 0)));
 
208
            Assert.That(_filled.Timer.Enabled, Is.False);
 
209
 
 
210
            return;
 
211
        }
 
212
 
 
213
        [Test]
 
214
        public void Test_HandleTimerTick_Right()
 
215
        {
 
216
            double textwidth;
 
217
            double w;
 
218
            int i;
 
219
 
 
220
            w = _filled.CodeBox.Viewport.Width;
 
221
            textwidth = _filled.CodeBox.Viewport.TextSource.MaxLength * _filled.CodeBox.Viewport.CharWidth;
 
222
 
 
223
            _filled.CodeBox.Viewport.Location = new PointF(0, 0);
 
224
 
 
225
            _filled.ScrollingDistance = 2;
 
226
 
 
227
            _filled.HandleMouseHoverRight();
 
228
 
 
229
            _filled.HandleTimerTick();
 
230
            Assert.That(_filled.CodeBox.Viewport.Location, Is.EqualTo(new PointF(2, 0)));
 
231
            Assert.That(_filled.Timer.Enabled, Is.True);
 
232
 
 
233
            _filled.HandleTimerTick();
 
234
            Assert.That(_filled.CodeBox.Viewport.Location, Is.EqualTo(new PointF(4, 0)));
 
235
 
 
236
            _filled.ScrollingDistance = 100;
 
237
 
 
238
            for (i = 0; i < 100; ++i)
 
239
            {
 
240
                _filled.HandleTimerTick();
 
241
            }
 
242
 
 
243
            Assert.That(_filled.CodeBox.Viewport.Location.X, Is.LessThanOrEqualTo(textwidth - w));
 
244
            Assert.That(_filled.Timer.Enabled, Is.False);
 
245
 
 
246
            return;
 
247
        }
 
248
 
 
249
        [Test]
 
250
        public void Test_HandleMouseHoverCode()
 
251
        {
 
252
            _filled.HandleMouseHoverUp();
 
253
 
 
254
            _filled.HandleMouseHoverCode();
 
255
 
 
256
            Assert.That(_filled.Translation, Is.EqualTo(CodeBoxComposite.TranslationDirection.None));
 
257
            Assert.That(_filled.Timer.Enabled, Is.False);
 
258
 
 
259
            return;
 
260
        }
 
261
 
 
262
        [Test]
 
263
        public void Test_HandleTimerTick_Down()
 
264
        {
 
265
            int startingLine;
 
266
 
 
267
            startingLine = _filled.CodeBox.CurrentLineNumber;
 
268
 
 
269
            _filled.ScrollingDistance = _filled.CodeBox.Viewport.CharHeight + 0.5d;
 
270
 
 
271
            _filled.HandleMouseHoverDown();
 
272
            Assert.That(_filled.CodeBox.CurrentLineNumber, Is.EqualTo(startingLine));
 
273
 
 
274
            _filled.HandleTimerTick();
 
275
            Assert.That(_filled.CodeBox.CurrentLineNumber, Is.EqualTo(startingLine + 1));
 
276
 
 
277
            _filled.HandleTimerTick();
 
278
            Assert.That(_filled.CodeBox.CurrentLineNumber, Is.EqualTo(startingLine + 2));
 
279
 
 
280
            for (int i = 0; i < 200; ++i)
 
281
                _filled.HandleTimerTick();
 
282
 
 
283
            Assert.That(_filled.CodeBox.CurrentLineNumber, Is.EqualTo(42));
 
284
            Assert.That(_filled.Timer.Enabled, Is.False);           
 
285
 
 
286
            return;
 
287
        }
 
288
 
 
289
        [Test]
 
290
        public void Test_Yield_Focus_To_CodeBox()
 
291
        {
 
292
            _filled.ActiveControl = null;
 
293
            Assert.That(_filled.ActiveControl, Is.Null);
 
294
 
 
295
            _filled.HandleMouseHoverCode();
 
296
 
 
297
            Assert.That(_filled.ActiveControl, Is.EqualTo(_filled.CodeBox));
 
298
 
 
299
            return;
 
300
        }
 
301
 
 
302
        [Test]
 
303
        public void Test_Ability_To_Disable_HoverButtons()
 
304
        {
 
305
            double max;
 
306
 
 
307
            max = 5000; 
 
308
            
 
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);
 
314
            
 
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);
 
320
 
 
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);
 
326
 
 
327
            return;
 
328
        }
 
329
 
 
330
        [Test]
 
331
        public void Test_HandleLeaveEvent()
 
332
        {
 
333
            _filled.HandleMouseHoverDown();
 
334
            Assert.That(_filled.Translation, Is.EqualTo(CodeBoxComposite.TranslationDirection.Down));
 
335
            Assert.That(_filled.Timer.Enabled, Is.True);
 
336
 
 
337
            Assert.That(_leaveNotification, Is.EqualTo(0));
 
338
 
 
339
            _filled.HandleLeaveEvent();
 
340
 
 
341
            Assert.That(_filled.Translation, Is.EqualTo(CodeBoxComposite.TranslationDirection.None));
 
342
            Assert.That(_filled.Timer.Enabled, Is.False);
 
343
 
 
344
            Assert.That(_leaveNotification, Is.EqualTo(1));
 
345
 
 
346
            return;
 
347
        }
 
348
        
 
349
        #region CodeControl
 
350
 
 
351
        class TestingCodeBoxComposite :
 
352
            CodeBoxComposite
 
353
        {
 
354
            public new string ErrorMessage {
 
355
                get { return (base.ErrorMessage); }
 
356
            }
 
357
 
 
358
            public new CodeBox CodeBox {
 
359
                get { return (base.CodeBox); }
 
360
            }
 
361
 
 
362
            public new void HandleMouseHoverUp() {
 
363
                base.HandleMouseHoverUp();
 
364
            }
 
365
 
 
366
            public new void HandleMouseHoverDown() {
 
367
                base.HandleMouseHoverDown();
 
368
            }
 
369
 
 
370
            public new void HandleMouseHoverLeft() {
 
371
                base.HandleMouseHoverLeft();
 
372
            }
 
373
 
 
374
            public new void HandleMouseHoverRight() {
 
375
                base.HandleMouseHoverRight();
 
376
            }
 
377
 
 
378
            public new void HandleMouseHoverCode() {
 
379
                base.HandleMouseHoverCode();
 
380
            }
 
381
 
 
382
            public new TranslationDirection Translation {
 
383
                get { return (base.Translation); }
 
384
            }
 
385
 
 
386
            public new void HandleLeaveEvent()
 
387
            {
 
388
                base.HandleLeaveEvent();
 
389
            }
 
390
 
 
391
            public new Timer Timer {
 
392
                get { return (base.Timer); }
 
393
            }
 
394
 
 
395
            public new void HandleTimerTick() {
 
396
                base.HandleTimerTick();
 
397
            }
 
398
 
 
399
            public new HoverButton BtnUp {
 
400
                get { return (base.BtnUp); }
 
401
            }
 
402
 
 
403
            public new HoverButton BtnLeft {
 
404
                get { return (base.BtnLeft); }
 
405
            }
 
406
 
 
407
            public new HoverButton BtnRight {
 
408
                get { return (base.BtnRight); }
 
409
            }
 
410
 
 
411
            public new HoverButton BtnDown {
 
412
                get { return (base.BtnDown); }
 
413
            }
 
414
        }
 
415
 
 
416
        #endregion
 
417
    }
 
418
}