| 1 |
{ |
| 2 |
Copyright 2022-2023 Andrzej Kilijański, Dean Zobec, Michael Van Canneyt, Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ Main view, where most of the application logic takes place. } |
| 17 |
unit GameViewMain; |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses Classes, |
| 22 |
CastleVectors, CastleComponentSerialize, |
| 23 |
CastleUIControls, CastleControls, CastleKeysMouse, CastleTester; |
| 24 |
|
| 25 |
type |
| 26 |
{ Main view, where most of the application logic takes place. } |
| 27 |
TViewMain = class(TCastleView) |
| 28 |
published |
| 29 |
{ Components designed using CGE editor. |
| 30 |
These fields will be automatically initialized at Start. } |
| 31 |
LabelMessage: TCastleLabel; |
| 32 |
LabelCurrentTest: TCastleLabel; |
| 33 |
LabelTestPassed: TCastleLabel; |
| 34 |
LabelTestFailed: TCastleLabel; |
| 35 |
LabelFailedTests: TCastleLabel; |
| 36 |
LabelTestsCount: TCastleLabel; |
| 37 |
CheckboxStopOnFail: TCastleCheckbox; |
| 38 |
ButtonStartTests: TCastleButton; |
| 39 |
ButtonStopTests: TCastleButton; |
| 40 |
ButtonSelectTests: TCastleButton; |
| 41 |
private |
| 42 |
Tester: TCastleTester; |
| 43 |
RunTests: Boolean; |
| 44 |
|
| 45 |
procedure ClickStartTests(Sender: TObject); |
| 46 |
procedure ClickStopTests(Sender: TObject); |
| 47 |
|
| 48 |
procedure TestPassedCountChanged(const TestCount: Integer); |
| 49 |
procedure TestFailedCountChanged(const TestCount: Integer); |
| 50 |
procedure EnabledTestCountChanged(Sender: TObject); |
| 51 |
procedure TestExecuted(const AName: String); |
| 52 |
procedure TestFailed(const TestName, Msg: String); |
| 53 |
procedure LogFailure(const AMessage: String); |
| 54 |
|
| 55 |
procedure StartTesting; |
| 56 |
procedure StopTesting(const AMessage: String; |
| 57 |
const Exception: Boolean = false); |
| 58 |
|
| 59 |
public |
| 60 |
constructor Create(AOwner: TComponent); override; |
| 61 |
procedure Start; override; |
| 62 |
procedure Update(const SecondsPassed: Single; |
| 63 |
var HandleInput: Boolean); override; |
| 64 |
end; |
| 65 |
|
| 66 |
var |
| 67 |
ViewMain: TViewMain; |
| 68 |
|
| 69 |
implementation |
| 70 |
|
| 71 |
uses SysUtils, |
| 72 |
CastleColors, CastleUtils, CastleTesterParameters, CastleLog; |
| 73 |
|
| 74 |
{ TViewMain ----------------------------------------------------------------- } |
| 75 |
|
| 76 |
procedure TViewMain.TestFailed(const TestName, Msg: String); |
| 77 |
begin |
| 78 |
LogFailure(TestName + ': ' + Msg); |
| 79 |
end; |
| 80 |
|
| 81 |
procedure TViewMain.ClickStartTests(Sender: TObject); |
| 82 |
begin |
| 83 |
Tester.StopOnFirstFail := CheckboxStopOnFail.Checked; |
| 84 |
Tester.PrepareTestListToRun; |
| 85 |
StartTesting; |
| 86 |
end; |
| 87 |
|
| 88 |
procedure TViewMain.ClickStopTests(Sender: TObject); |
| 89 |
begin |
| 90 |
StopTesting('Testing aborted by user', false); |
| 91 |
end; |
| 92 |
|
| 93 |
constructor TViewMain.Create(AOwner: TComponent); |
| 94 |
begin |
| 95 |
inherited; |
| 96 |
DesignUrl := 'castle-data:/gameviewmain.castle-user-interface'; |
| 97 |
end; |
| 98 |
|
| 99 |
procedure TViewMain.EnabledTestCountChanged(Sender: TObject); |
| 100 |
begin |
| 101 |
LabelTestsCount.Caption := Format('Tests: %d / %d', [ |
| 102 |
Tester.EnabledTestCount, |
| 103 |
Tester.TestsCount |
| 104 |
]); |
| 105 |
end; |
| 106 |
|
| 107 |
procedure TViewMain.LogFailure(const AMessage: String); |
| 108 |
begin |
| 109 |
if LabelFailedTests.Caption = '' then |
| 110 |
LabelFailedTests.Caption := AMessage |
| 111 |
else |
| 112 |
LabelFailedTests.Caption := LabelFailedTests.Caption + NL + AMessage; |
| 113 |
end; |
| 114 |
|
| 115 |
procedure TViewMain.Start; |
| 116 |
begin |
| 117 |
inherited; |
| 118 |
|
| 119 |
ButtonStartTests.OnClick := {$ifdef FPC}@{$endif}ClickStartTests; |
| 120 |
|
| 121 |
ButtonStopTests.OnClick := {$ifdef FPC}@{$endif}ClickStopTests; |
| 122 |
ButtonStopTests.Enabled := false; |
| 123 |
|
| 124 |
ButtonSelectTests.Enabled := true; |
| 125 |
ButtonSelectTests.Exists := false; // TODO: ButtonSelectTests functionality not implemented yet |
| 126 |
|
| 127 |
{ Make sure the tests are not running } |
| 128 |
RunTests := false; |
| 129 |
|
| 130 |
Tester := TCastleTester.Create(FreeAtStop); |
| 131 |
{ We can just set values in Update but I think callbacks interface is more |
| 132 |
flexible in a variety of applications } |
| 133 |
Tester.NotifyTestPassedChanged := {$ifdef FPC}@{$endif}TestPassedCountChanged; |
| 134 |
Tester.NotifyTestFailedChanged := {$ifdef FPC}@{$endif}TestFailedCountChanged; |
| 135 |
Tester.NotifyEnabledTestCountChanged := {$ifdef FPC}@{$endif}EnabledTestCountChanged; |
| 136 |
Tester.NotifyTestCaseExecuted := {$ifdef FPC}@{$endif}TestExecuted; |
| 137 |
Tester.NotifyTestFail := {$ifdef FPC}@{$endif}TestFailed; |
| 138 |
|
| 139 |
{ Add all Registered tests by calling AddRegisteredTestCases } |
| 140 |
Tester.AddRegisteredTestCases; |
| 141 |
|
| 142 |
{ Note: One can also add only one test case by code, like this: } |
| 143 |
// Tester.AddTestCase(TTestCameras.Create); |
| 144 |
|
| 145 |
{ Scans all tests } |
| 146 |
Tester.Scan; |
| 147 |
if ParamFilter <> '' then |
| 148 |
Tester.EnableFilter(ParamFilter); |
| 149 |
{ First prepare to count acctualy selected tests } |
| 150 |
Tester.PrepareTestListToRun; |
| 151 |
end; |
| 152 |
|
| 153 |
procedure TViewMain.StartTesting; |
| 154 |
begin |
| 155 |
RunTests := true; |
| 156 |
LabelMessage.Caption := 'Processing...'; |
| 157 |
LabelMessage.Color := HexToColor('00CE00'); |
| 158 |
ButtonStartTests.Enabled := false; |
| 159 |
ButtonStopTests.Enabled := true; |
| 160 |
ButtonSelectTests.Enabled := false; |
| 161 |
end; |
| 162 |
|
| 163 |
procedure TViewMain.StopTesting(const AMessage: String; const Exception: Boolean = false); |
| 164 |
begin |
| 165 |
RunTests := false; |
| 166 |
|
| 167 |
LabelMessage.Caption := AMessage; |
| 168 |
|
| 169 |
{ If some test ends with unhandled exception we want it on our error list } |
| 170 |
if Exception then |
| 171 |
LogFailure(AMessage); |
| 172 |
|
| 173 |
if (Tester.TestFailedCount > 0) or (Exception) then |
| 174 |
LabelMessage.Color := HexToColor('C60D0D') |
| 175 |
else |
| 176 |
LabelMessage.Color := HexToColor('00CE00'); |
| 177 |
|
| 178 |
ButtonStartTests.Enabled := true; |
| 179 |
ButtonStopTests.Enabled := false; |
| 180 |
ButtonSelectTests.Enabled := true; |
| 181 |
end; |
| 182 |
|
| 183 |
procedure TViewMain.TestExecuted(const AName: String); |
| 184 |
begin |
| 185 |
LabelCurrentTest.Caption := AName; |
| 186 |
WritelnLog('Running test ' + AName); |
| 187 |
end; |
| 188 |
|
| 189 |
procedure TViewMain.TestFailedCountChanged(const TestCount: Integer); |
| 190 |
begin |
| 191 |
LabelTestFailed.Caption := IntToStr(TestCount); |
| 192 |
end; |
| 193 |
|
| 194 |
procedure TViewMain.TestPassedCountChanged(const TestCount: Integer); |
| 195 |
begin |
| 196 |
LabelTestPassed.Caption := IntToStr(TestCount); |
| 197 |
end; |
| 198 |
|
| 199 |
procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: Boolean); |
| 200 |
begin |
| 201 |
if RunTests then |
| 202 |
begin |
| 203 |
if Tester.IsNextTestToRun then |
| 204 |
begin |
| 205 |
try |
| 206 |
Tester.RunNextTest; |
| 207 |
except |
| 208 |
on E:Exception do |
| 209 |
begin |
| 210 |
{ In case of UI application we don't want any unhandled exceptions } |
| 211 |
StopTesting('Unhandled exception: ' + E.Message, true); |
| 212 |
end; |
| 213 |
end; |
| 214 |
end else |
| 215 |
begin |
| 216 |
StopTesting('Testing finished'); |
| 217 |
end; |
| 218 |
end; |
| 219 |
|
| 220 |
inherited; |
| 221 |
end; |
| 222 |
|
| 223 |
end. |