[MWF] Add unit tests for DataGridViewRowHeightInfoNeededEventArgs
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / DataGridViewRowHeightInfoNeededEventArgsTests.cs
1 // Copyright (c) 2014 SIL International
2 // This software is licensed under the MIT License (http://opensource.org/licenses/MIT)
3 using System;
4 using System.Windows.Forms;
5 using NUnit.Framework;
6 using CategoryAttribute = NUnit.Framework.CategoryAttribute;
7
8 namespace MonoTests.System.Windows.Forms
9 {
10         [TestFixture]
11         public class DataGridViewRowHeightInfoNeededEventArgsTests
12         {
13                 struct RowInfo
14                 {
15                         public RowInfo (DataGridViewRowHeightInfoNeededEventArgs e)
16                         {
17                                 Height = e.Height;
18                                 MinimumHeight = e.MinimumHeight;
19                         }
20
21                         public int Height;
22                         public int MinimumHeight;
23                 }
24
25                 class DummyDataGridView : DataGridView
26                 {
27                         public RowInfo RowInfo;
28
29                         protected override void OnRowHeightInfoNeeded (DataGridViewRowHeightInfoNeededEventArgs e)
30                         {
31                                 base.OnRowHeightInfoNeeded (e);
32                                 RowInfo = new RowInfo (e);
33                         }
34                 }
35
36                 [Test]
37                 [Category("NotWorking")]
38                 public void HeightCantBeLessThanCurrentMinimumHeight ()
39                 {
40                         using (var dgv = new DummyDataGridView ()) {
41                                 // Setup
42                                 dgv.VirtualMode = true;
43                                 dgv.RowCount = 1;
44                                 dgv.Rows [0].MinimumHeight = 5;
45                                 dgv.Rows [0].Height = 10;
46                                 dgv.RowHeightInfoNeeded += (sender, e) => {
47                                         e.Height = 2;
48                                         e.MinimumHeight = 2;
49                                 };
50                                 dgv.UpdateRowHeightInfo (0, false);
51
52                                 // Execute - this triggers the RowHeightInfoNeeded event
53                                 // This test doesn't work because of different implementation details in .NET
54                                 // and Mono: on .NET RowHeightInfoNeeded gets called for the first time when
55                                 // executing the following line. In Mono RowHeightInfoNeeded got already called
56                                 // while executing dgv.UpdateRowHeightInfo. This means that when RowHeightInfoNeeded
57                                 // gets called now MinimumHeight is already set to 2, allowing Height to become
58                                 // 2 as well.
59                                 // On .NET since it is the first time Height will be set to 5 (the current
60                                 // MinimumHeight value).
61                                 // The .NET behaviour is surprising since the order of setting the information
62                                 // in RowHeightInfoNeeded shouldn't matter, therefore I don't think it's worth
63                                 // changing the behaviour in Mono. Even more so since there is an easy
64                                 // workaround: simply swapping the two lines in the RowHeighInfoNeeded event
65                                 // handler works around the problem.
66                                 var dummy = dgv.Rows [0].Height;
67
68                                 // Verify
69                                 var rowHeightInfo = dgv.RowInfo;
70                                 Assert.AreEqual (5, rowHeightInfo.Height, "#A1"); // 5 because of buggy .NET behaviour
71                                 Assert.AreEqual (2, rowHeightInfo.MinimumHeight, "#A2");
72                         }
73                 }
74
75                 [Test]
76                 public void SettingHeightAfterChangingMinimumHeight ()
77                 {
78                         using (var dgv = new DummyDataGridView ()) {
79                                 // Setup
80                                 dgv.VirtualMode = true;
81                                 dgv.RowCount = 1;
82                                 dgv.Rows [0].MinimumHeight = 5;
83                                 dgv.Rows [0].Height = 10;
84                                 dgv.RowHeightInfoNeeded += (sender, e) => {
85                                         e.MinimumHeight = 2;
86                                         e.Height = 2;
87                                 };
88                                 dgv.UpdateRowHeightInfo (0, false);
89
90                                 // Execute - this triggers the RowHeightInfoNeeded event
91                                 var dummy = dgv.Rows [0].Height;
92
93                                 // Verify
94                                 var rowHeightInfo = dgv.RowInfo;
95                                 Assert.AreEqual (2, rowHeightInfo.Height, "#B1");
96                                 Assert.AreEqual (2, rowHeightInfo.MinimumHeight, "#B2");
97                         }
98                 }
99         }
100 }
101