|
| 1 | +unit VTOnDrawTextTests; |
| 2 | + |
| 3 | +interface |
| 4 | + |
| 5 | +uses |
| 6 | + DUnitX.TestFramework, |
| 7 | + Vcl.Forms, |
| 8 | + VirtualTrees, System.Types; |
| 9 | + |
| 10 | +type |
| 11 | + |
| 12 | + [TestFixture] |
| 13 | + TVTOnDrawTextTests = class |
| 14 | + strict private |
| 15 | + fTree: TVirtualStringTree; |
| 16 | + fForm: TForm; |
| 17 | + |
| 18 | + FDrawText1Called: Boolean; |
| 19 | + FDrawTextEx1Called: Boolean; |
| 20 | + |
| 21 | + FDrawText2Called: Boolean; |
| 22 | + FDrawTextEx2Called: Boolean; |
| 23 | + |
| 24 | + FDrawText3Called: Boolean; |
| 25 | + FDrawTextEx3Called: Boolean; |
| 26 | + |
| 27 | + procedure DrawText1Event(Sender: TBaseVirtualTree; TargetCanvas: TCanvas; |
| 28 | + Node: PVirtualNode; Column: TColumnIndex; const Text: string; |
| 29 | + const CellRect: TRect; var DefaultDraw: Boolean); |
| 30 | + |
| 31 | + procedure DrawTextEx2Event(Sender: TBaseVirtualTree; TargetCanvas: TCanvas; |
| 32 | + Node: PVirtualNode; Column: TColumnIndex; const Text: string; |
| 33 | + const CellRect: TRect; var DefaultDraw: Boolean; var DrawFormat: Cardinal); |
| 34 | + |
| 35 | + procedure DrawText3Event(Sender: TBaseVirtualTree; TargetCanvas: TCanvas; |
| 36 | + Node: PVirtualNode; Column: TColumnIndex; const Text: string; |
| 37 | + const CellRect: TRect; var DefaultDraw: Boolean); |
| 38 | + procedure DrawTextEx3Event(Sender: TBaseVirtualTree; TargetCanvas: TCanvas; |
| 39 | + Node: PVirtualNode; Column: TColumnIndex; const Text: string; |
| 40 | + const CellRect: TRect; var DefaultDraw: Boolean; var DrawFormat: Cardinal); |
| 41 | + |
| 42 | + procedure GetTextEvent(Sender: TBaseVirtualTree; Node: PVirtualNode; |
| 43 | + Column: TColumnIndex; TextType: TVSTTextType; var CellText: string); |
| 44 | + public |
| 45 | + [Setup] |
| 46 | + procedure Setup; |
| 47 | + [TearDown] |
| 48 | + procedure TearDown; |
| 49 | + |
| 50 | + [Test] |
| 51 | + procedure TestOnDrawText; |
| 52 | + |
| 53 | + [Test] |
| 54 | + procedure TestOnDrawTextOnDrawTextEx; |
| 55 | + |
| 56 | + [Test] |
| 57 | + procedure TestOnDrawTextEx; |
| 58 | + end; |
| 59 | + |
| 60 | +implementation |
| 61 | + |
| 62 | +uses |
| 63 | + System.SysUtils, VirtualTrees.Types; |
| 64 | + |
| 65 | +const |
| 66 | + colCaption = 0; |
| 67 | + colData = 1; |
| 68 | + |
| 69 | +procedure TVTOnDrawTextTests.DrawText1Event(Sender: TBaseVirtualTree; |
| 70 | + TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex; |
| 71 | + const Text: string; const CellRect: TRect; var DefaultDraw: Boolean); |
| 72 | +begin |
| 73 | + FDrawText1Called := True; |
| 74 | +end; |
| 75 | + |
| 76 | +procedure TVTOnDrawTextTests.DrawText3Event(Sender: TBaseVirtualTree; |
| 77 | + TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex; |
| 78 | + const Text: string; const CellRect: TRect; var DefaultDraw: Boolean); |
| 79 | +begin |
| 80 | + FDrawText3Called := True; |
| 81 | +end; |
| 82 | + |
| 83 | +procedure TVTOnDrawTextTests.DrawTextEx2Event(Sender: TBaseVirtualTree; |
| 84 | + TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex; |
| 85 | + const Text: string; const CellRect: TRect; var DefaultDraw: Boolean; |
| 86 | + var DrawFormat: Cardinal); |
| 87 | +begin |
| 88 | + FDrawTextEx2Called := True; |
| 89 | +end; |
| 90 | + |
| 91 | +procedure TVTOnDrawTextTests.DrawTextEx3Event(Sender: TBaseVirtualTree; |
| 92 | + TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex; |
| 93 | + const Text: string; const CellRect: TRect; var DefaultDraw: Boolean; |
| 94 | + var DrawFormat: Cardinal); |
| 95 | +begin |
| 96 | + FDrawTextEx3Called := True; |
| 97 | +end; |
| 98 | + |
| 99 | +procedure TVTOnDrawTextTests.GetTextEvent(Sender: TBaseVirtualTree; |
| 100 | + Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType; |
| 101 | + var CellText: string); |
| 102 | +begin |
| 103 | + case Column of |
| 104 | + colCaption: begin |
| 105 | + CellText := 'Caption'; |
| 106 | + end; |
| 107 | + colData: begin |
| 108 | + CellText := 'Data'; |
| 109 | + end; |
| 110 | + end; |
| 111 | +end; |
| 112 | + |
| 113 | +procedure TVTOnDrawTextTests.Setup; |
| 114 | +begin |
| 115 | + FDrawText1Called := False; |
| 116 | + FDrawTextEx1Called := False; |
| 117 | + |
| 118 | + FDrawText2Called := False; |
| 119 | + FDrawTextEx2Called := False; |
| 120 | + |
| 121 | + FDrawText3Called := False; |
| 122 | + FDrawTextEx3Called := False; |
| 123 | + |
| 124 | + fForm := TForm.Create(nil); |
| 125 | + fTree := TVirtualStringTree.Create(fForm); |
| 126 | + fForm.InsertControl(fTree); |
| 127 | + |
| 128 | + fTree.OnGetText := GetTextEvent; |
| 129 | + |
| 130 | + var LCol1 := fTree.Header.Columns.Add; |
| 131 | + var LCol2 := fTree.Header.Columns.Add; |
| 132 | + LCol1.Text := 'Caption'; |
| 133 | + LCol2.Text := 'Data'; |
| 134 | + |
| 135 | + fTree.AddChild(fTree.RootNode); |
| 136 | + fTree.AddChild(fTree.RootNode); |
| 137 | + fForm.Show; |
| 138 | +end; |
| 139 | + |
| 140 | +procedure TVTOnDrawTextTests.TearDown; |
| 141 | +begin |
| 142 | + FreeAndNil(fForm); |
| 143 | +end; |
| 144 | + |
| 145 | +procedure TVTOnDrawTextTests.TestOnDrawText; |
| 146 | +begin |
| 147 | + // This test ensures that OnDrawText event is called when OnDrawText is assigned |
| 148 | + fTree.OnDrawText := DrawText1Event; |
| 149 | + fTree.OnDrawTextEx := nil; |
| 150 | + fTree.Update; |
| 151 | + |
| 152 | + Assert.IsTrue(FDrawText1Called and not FDrawTextEx1Called); |
| 153 | +end; |
| 154 | + |
| 155 | +procedure TVTOnDrawTextTests.TestOnDrawTextEx; |
| 156 | +begin |
| 157 | + // This test ensures that OnDrawTextEx event is called when OnDrawTextEx is assigned |
| 158 | + // and that OnDrawText is not called |
| 159 | + fTree.OnDrawText := nil; |
| 160 | + fTree.OnDrawTextEx := DrawTextEx2Event; |
| 161 | + fTree.Update; |
| 162 | + |
| 163 | + Assert.IsTrue(not FDrawText2Called and FDrawTextEx2Called); |
| 164 | +end; |
| 165 | + |
| 166 | +procedure TVTOnDrawTextTests.TestOnDrawTextOnDrawTextEx; |
| 167 | +begin |
| 168 | + // This test ensures that only the OnDrawTextEx event is called when both |
| 169 | + // OnDrawText and OnDrawTextEx are assigned and that OnDrawText is not called |
| 170 | + fTree.OnDrawText := DrawText3Event; |
| 171 | + fTree.OnDrawTextEx := DrawTextEx3Event; |
| 172 | + fTree.Update; |
| 173 | + |
| 174 | + Assert.IsTrue(not FDrawText3Called and FDrawTextEx3Called); |
| 175 | +end; |
| 176 | + |
| 177 | +initialization |
| 178 | + TDUnitX.RegisterTestFixture(TVTOnDrawTextTests); |
| 179 | +end. |
0 commit comments