| 1 |
// -*- compile-command: "./test_single_testcase.sh TTestCastleWindow" -*- |
| 2 |
{ |
| 3 |
Copyright 2010-2022 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{ Test CastleWindow. } |
| 18 |
unit TestCastleWindow; |
| 19 |
|
| 20 |
interface |
| 21 |
|
| 22 |
uses CastleTester; |
| 23 |
|
| 24 |
type |
| 25 |
TTestCastleWindow = class(TCastleTestCase) |
| 26 |
published |
| 27 |
procedure Test1; |
| 28 |
procedure TestNotifications; |
| 29 |
procedure TestMenu; |
| 30 |
{ Test TCastleUserInterface.AutoSizeToChildren. |
| 31 |
In this unit, as it requires TCastleWindow (UI container) to make sense. } |
| 32 |
procedure TestAutoSizeToChildren; |
| 33 |
{ Test TCastleContainer.Focus. |
| 34 |
In this unit, as it requires TCastleWindow (UI container) to make sense. } |
| 35 |
procedure TestFocus; |
| 36 |
procedure TestEventLoop; |
| 37 |
procedure TestViewportPositionTo; |
| 38 |
procedure TestStateAutoStop; |
| 39 |
procedure TestStateSize; |
| 40 |
procedure TestStateSize2; |
| 41 |
procedure TestViewportWithoutCamera; |
| 42 |
procedure TestPrepareResourcesWithoutContextOpen; |
| 43 |
end; |
| 44 |
|
| 45 |
implementation |
| 46 |
|
| 47 |
uses SysUtils, Classes, Math, |
| 48 |
CastleWindow, CastleControls, CastleStringUtils, CastleKeysMouse, |
| 49 |
CastleUIControls, CastleRectangles, CastleOnScreenMenu, CastleComponentSerialize, |
| 50 |
CastleCameras, {$ifdef FPC}CastleSceneManager,{$endif} CastleVectors, |
| 51 |
CastleTransform, CastleScene, CastleApplicationProperties, X3DCameraUtils, |
| 52 |
CastleViewport, CastleInternalRenderer, CastleInternalShapesRenderer; |
| 53 |
|
| 54 |
procedure TTestCastleWindow.Test1; |
| 55 |
var |
| 56 |
Window: TCastleWindow; |
| 57 |
begin |
| 58 |
if not CanCreateWindowForTest then |
| 59 |
Exit; |
| 60 |
|
| 61 |
Window := CreateWindowForTest; |
| 62 |
try |
| 63 |
Window.Open; |
| 64 |
Window.Close; |
| 65 |
finally DestroyWindowForTest(Window) end; |
| 66 |
end; |
| 67 |
|
| 68 |
procedure TTestCastleWindow.TestNotifications; |
| 69 |
var |
| 70 |
Window: TCastleWindow; |
| 71 |
C: TCastleButton; |
| 72 |
begin |
| 73 |
if not CanCreateWindowForTest then |
| 74 |
Exit; |
| 75 |
|
| 76 |
Window := CreateWindowForTest; |
| 77 |
try |
| 78 |
C := TCastleButton.Create(Window); |
| 79 |
FreeAndNil(C); |
| 80 |
finally DestroyWindowForTest(Window) end; |
| 81 |
end; |
| 82 |
|
| 83 |
procedure TTestCastleWindow.TestMenu; |
| 84 |
var |
| 85 |
M: TMenuItem; |
| 86 |
begin |
| 87 |
M := TMenuItem.Create('blah', 123, 'a'); |
| 88 |
AssertTrue(M.KeyMatches(keyA, 'a', [])); |
| 89 |
{ Below may be improved in the future, for now our KeyMatches is probably too forgiving. |
| 90 |
Below combination is not even usually possible, with mkCtrl you would get CtrlA |
| 91 |
character usually. } |
| 92 |
AssertTrue(M.KeyMatches(keyA, 'a', [mkCtrl])); |
| 93 |
FreeAndNil(M); |
| 94 |
|
| 95 |
M := TMenuItem.Create('blah', 123, keyF11); |
| 96 |
AssertTrue(M.KeyMatches(keyF11, #0, [])); |
| 97 |
AssertTrue(M.KeyMatches(keyF11, '', [])); // string '' and #0 should be treated equal, for backward compat |
| 98 |
{ below may be improved in the future, for now our KeyMatches is probably too forgiving } |
| 99 |
AssertTrue(M.KeyMatches(keyF11, #0, [mkCtrl])); |
| 100 |
AssertTrue(M.KeyMatches(keyF11, '', [mkCtrl])); // string '' and #0 should be treated equal, for backward compat |
| 101 |
FreeAndNil(M); |
| 102 |
|
| 103 |
M := TMenuItem.Create('blah', 123, keyF11); |
| 104 |
M.Modifiers := [mkCtrl]; |
| 105 |
AssertTrue(not M.KeyMatches(keyF11, #0, [])); |
| 106 |
AssertTrue(M.KeyMatches(keyF11, #0, [mkCtrl])); |
| 107 |
FreeAndNil(M); |
| 108 |
end; |
| 109 |
|
| 110 |
procedure TTestCastleWindow.TestAutoSizeToChildren; |
| 111 |
var |
| 112 |
Window: TCastleWindow; |
| 113 |
Parent, Child1, Child2: TCastleUserInterface; |
| 114 |
begin |
| 115 |
if not CanCreateWindowForTest then |
| 116 |
Exit; |
| 117 |
|
| 118 |
Window := CreateWindowForTest; |
| 119 |
try |
| 120 |
Parent := nil; |
| 121 |
Child1 := nil; |
| 122 |
Child2 := nil; |
| 123 |
|
| 124 |
Window.Width := 500; |
| 125 |
Window.Height := 500; |
| 126 |
Window.ResizeAllowed := raNotAllowed; |
| 127 |
Window.Open; |
| 128 |
|
| 129 |
Parent := TCastleUserInterface.Create(nil); |
| 130 |
Window.Controls.InsertFront(Parent); |
| 131 |
Child1 := TCastleUserInterface.Create(nil); |
| 132 |
Child2 := TCastleUserInterface.Create(nil); |
| 133 |
|
| 134 |
AssertRectsEqual(FloatRectangle(0, 0, 100, 100), Parent.RenderRect); |
| 135 |
|
| 136 |
Parent.Anchor(hpLeft, 50); |
| 137 |
Parent.Anchor(vpTop, -60); |
| 138 |
AssertRectsEqual(FloatRectangle(50, 500 - 60 - 100, 100, 100), Parent.RenderRect); |
| 139 |
|
| 140 |
Parent.AutoSizeToChildren := true; |
| 141 |
AssertRectsEqual(TFloatRectangle.Empty, Parent.RenderRect); |
| 142 |
|
| 143 |
Parent.InsertFront(Child1); |
| 144 |
AssertRectsEqual(FloatRectangle(50, 500 - 60 - 100, 100, 100), Parent.RenderRect); |
| 145 |
|
| 146 |
Child1.Width := 40; |
| 147 |
Child1.Height := 30; |
| 148 |
AssertRectsEqual(FloatRectangle(50, 500 - 60 - 30, 40, 30), Parent.RenderRect); |
| 149 |
|
| 150 |
Child2.Width := 200; |
| 151 |
Child2.Height := 100; |
| 152 |
Child2.Anchor(vpTop, -10); |
| 153 |
Parent.InsertFront(Child2); |
| 154 |
AssertRectsEqual(FloatRectangle(50, 500 - 60 - 110, 200, 110), Parent.RenderRect); |
| 155 |
|
| 156 |
Child1.Width := 10; |
| 157 |
Child1.Height := 10; |
| 158 |
Child1.Anchor(hpLeft, 200); |
| 159 |
Child2.Width := 10; |
| 160 |
Child2.Height := 10; |
| 161 |
Child2.Anchor(vpBottom, 400); |
| 162 |
AssertRectsEqual(FloatRectangle(50, 500 - 60 - 410, 210, 410), Parent.RenderRect); |
| 163 |
|
| 164 |
Child1.Width := 80; |
| 165 |
Child1.Height := 30; |
| 166 |
Child1.Anchor(hpLeft, 30); |
| 167 |
Child1.Anchor(vpBottom, 40); |
| 168 |
Child2.Width := 80; |
| 169 |
Child2.Height := 30; |
| 170 |
Child2.Anchor(hpLeft, 40); |
| 171 |
Child2.Anchor(vpBottom, 60); |
| 172 |
AssertRectsEqual(FloatRectangle(50, 500 - 60 - 90, 120, 90), Parent.RenderRect); |
| 173 |
finally |
| 174 |
DestroyWindowForTest(Window); |
| 175 |
FreeAndNil(Parent); |
| 176 |
FreeAndNil(Child1); |
| 177 |
FreeAndNil(Child2); |
| 178 |
end; |
| 179 |
end; |
| 180 |
|
| 181 |
|
| 182 |
procedure TTestCastleWindow.TestFocus; |
| 183 |
var |
| 184 |
Window: TCastleWindow; |
| 185 |
ManualButton, Button2: TCastleButton; |
| 186 |
OnScreenMenu1: TCastleOnScreenMenu; |
| 187 |
SceneManager1: TCastleSceneManager; |
| 188 |
|
| 189 |
{ Based on examples/lazarus/model_3d_with_2d_controls/ } |
| 190 |
|
| 191 |
procedure AddUserInterfaceDesigned; |
| 192 |
var |
| 193 |
UiRoot: TCastleUserInterface; |
| 194 |
UiOwner: TComponent; |
| 195 |
begin |
| 196 |
UiOwner := TComponent.Create(Window); |
| 197 |
UiRoot := UserInterfaceLoad('castle-data:/focus_test/main.castle-user-interface', UiOwner); |
| 198 |
Window.Controls.InsertFront(UiRoot); |
| 199 |
Button2 := UiOwner.FindRequiredComponent('Button2') as TCastleButton; |
| 200 |
SceneManager1 := UiOwner.FindRequiredComponent('SceneManager1') as TCastleSceneManager; |
| 201 |
SceneManager1.AssignDefaultNavigation; // adds TCastleWalkNavigation |
| 202 |
end; |
| 203 |
|
| 204 |
procedure AddUserInterfaceFromCode; |
| 205 |
begin |
| 206 |
ManualButton := TCastleButton.Create(Window); |
| 207 |
ManualButton.Caption := 'Button added from code'; |
| 208 |
ManualButton.Anchor(hpRight, -10); |
| 209 |
ManualButton.Anchor(vpBottom, 120); |
| 210 |
Window.Controls.InsertFront(ManualButton); |
| 211 |
|
| 212 |
OnScreenMenu1 := TCastleOnScreenMenu.Create(Window); |
| 213 |
OnScreenMenu1.Bottom := 140; |
| 214 |
OnScreenMenu1.Left := 400; |
| 215 |
OnScreenMenu1.Add('one'); |
| 216 |
OnScreenMenu1.Add('two'); |
| 217 |
OnScreenMenu1.Add('three'); |
| 218 |
Window.Controls.InsertFront(OnScreenMenu1); |
| 219 |
end; |
| 220 |
|
| 221 |
procedure MoveMouse(const Pos: TVector2); |
| 222 |
// var |
| 223 |
// C: TCastleUserInterface; |
| 224 |
begin |
| 225 |
Window.InternalFakeMotion(InputMotion(Window.MousePosition, Pos, [], 0)); |
| 226 |
Window.Container.UpdateFocusAndMouseCursor; |
| 227 |
{ Useful to test current Focus value: |
| 228 |
Writeln('Focus now ', Window.Container.Focus.Count); |
| 229 |
for C in Window.Container.Focus do |
| 230 |
Writeln(' ', C.Name, ':', C.ClassName); |
| 231 |
} |
| 232 |
end; |
| 233 |
|
| 234 |
begin |
| 235 |
if not CanCreateWindowForTest then |
| 236 |
Exit; |
| 237 |
|
| 238 |
Window := CreateWindowForTest; |
| 239 |
try |
| 240 |
Window.Width := 800; |
| 241 |
Window.Height := 800; |
| 242 |
Window.ResizeAllowed := raNotAllowed; |
| 243 |
Window.Open; |
| 244 |
|
| 245 |
AddUserInterfaceDesigned; |
| 246 |
AddUserInterfaceFromCode; |
| 247 |
|
| 248 |
MoveMouse(FloatRectangle(Window.Rect).Center); |
| 249 |
AssertEquals(3, Window.Container.Focus.Count); |
| 250 |
AssertTrue(Window.Container.Focus[0].Name = 'Group1'); |
| 251 |
AssertTrue(Window.Container.Focus[1].Name = 'SceneManager1'); |
| 252 |
AssertTrue(Window.Container.Focus[2] is TCastleWalkNavigation); // internal in SceneManager1 |
| 253 |
|
| 254 |
MoveMouse(ManualButton.RenderRect.Center); |
| 255 |
AssertEquals(4, Window.Container.Focus.Count); |
| 256 |
AssertTrue(Window.Container.Focus[0].Name = 'Group1'); |
| 257 |
AssertTrue(Window.Container.Focus[1].Name = 'SceneManager1'); |
| 258 |
AssertTrue(Window.Container.Focus[2] is TCastleWalkNavigation); // internal in SceneManager1 |
| 259 |
AssertTrue(Window.Container.Focus[3] = ManualButton); |
| 260 |
|
| 261 |
MoveMouse(Button2.RenderRect.Center); |
| 262 |
AssertEquals(4, Window.Container.Focus.Count); |
| 263 |
AssertTrue(Window.Container.Focus[0].Name = 'Group1'); |
| 264 |
AssertTrue(Window.Container.Focus[1].Name = 'SceneManager1'); |
| 265 |
AssertTrue(Window.Container.Focus[2] is TCastleWalkNavigation); // internal in SceneManager1 |
| 266 |
AssertTrue(Window.Container.Focus[3] = Button2); |
| 267 |
finally DestroyWindowForTest(Window) end; |
| 268 |
end; |
| 269 |
|
| 270 |
procedure TTestCastleWindow.TestEventLoop; |
| 271 |
var |
| 272 |
Window: TCastleWindow; |
| 273 |
Renderer: TRenderer; |
| 274 |
ShapesCollector: TShapesCollector; |
| 275 |
|
| 276 |
procedure SimulateEventLoop(const T: TCastleTransform); |
| 277 |
var |
| 278 |
RenderParams: TRenderParams; |
| 279 |
RemoveMe: TRemoveType; |
| 280 |
begin |
| 281 |
RenderParams := TBasicRenderParams.Create; |
| 282 |
try |
| 283 |
RenderParams.RenderingCamera := TRenderingCamera.Create; |
| 284 |
try |
| 285 |
RenderParams.RenderingCamera.FromViewVectors( |
| 286 |
DefaultX3DCameraView, TMatrix4.Identity); |
| 287 |
RenderParams.RenderingCamera.Target := rtScreen; |
| 288 |
RenderParams.Frustum := @RenderParams.RenderingCamera.Frustum; |
| 289 |
RenderParams.Collector := ShapesCollector; |
| 290 |
RenderParams.RendererToPrepareShapes := Renderer; |
| 291 |
T.Render(RenderParams); |
| 292 |
finally FreeAndNil(RenderParams.RenderingCamera) end; |
| 293 |
finally FreeAndNil(RenderParams) end; |
| 294 |
|
| 295 |
RemoveMe := rtNone; |
| 296 |
T.Update(1/60, RemoveMe); |
| 297 |
|
| 298 |
//Window.MessageOK('Press OK to finish this event loop run', mtInfo); |
| 299 |
end; |
| 300 |
|
| 301 |
var |
| 302 |
Box: TCastleBox; |
| 303 |
Viewport: TCastleViewport; |
| 304 |
begin |
| 305 |
if not CanCreateWindowForTest then |
| 306 |
Exit; |
| 307 |
|
| 308 |
ApplicationProperties.OnWarning.Add({$ifdef FPC}@{$endif}OnWarningRaiseException); |
| 309 |
try |
| 310 |
Window := CreateWindowForTest; |
| 311 |
try |
| 312 |
// for rendering, OpenGL context must be ready, with GLFeatures initialized |
| 313 |
Window.Visible := false; |
| 314 |
Window.Open; |
| 315 |
|
| 316 |
Viewport := TCastleViewport.Create(nil); |
| 317 |
try |
| 318 |
Viewport.FullSize := true; |
| 319 |
Viewport.AutoCamera := true; |
| 320 |
Window.Controls.InsertFront(Viewport); |
| 321 |
|
| 322 |
// in real applications, Viewport has its internal renderer |
| 323 |
Renderer := TRenderer.Create(nil); |
| 324 |
try |
| 325 |
ShapesCollector := TShapesCollector.Create; |
| 326 |
try |
| 327 |
Box := TCastleBox.Create(nil); |
| 328 |
try |
| 329 |
Viewport.Items.Add(Box); |
| 330 |
|
| 331 |
SimulateEventLoop(Box); |
| 332 |
Box.Material := pmUnlit; |
| 333 |
SimulateEventLoop(Box); |
| 334 |
Box.Material := pmPhysical; |
| 335 |
SimulateEventLoop(Box); |
| 336 |
finally FreeAndNil(Box) end; |
| 337 |
finally FreeAndNil(ShapesCollector) end; |
| 338 |
finally FreeAndNil(Renderer) end; |
| 339 |
finally FreeAndNil(Viewport) end; |
| 340 |
finally DestroyWindowForTest(Window) end; |
| 341 |
finally |
| 342 |
ApplicationProperties.OnWarning.Remove({$ifdef FPC}@{$endif}OnWarningRaiseException); |
| 343 |
end; |
| 344 |
end; |
| 345 |
|
| 346 |
procedure TTestCastleWindow.TestViewportPositionTo; |
| 347 |
var |
| 348 |
Viewport: TCastleViewport; |
| 349 |
|
| 350 |
{ Non-interactive version of a testcase on |
| 351 |
https://gist.github.com/michaliskambi/2b8faee73df9f3a12351736cabcad1ee |
| 352 |
for https://github.com/castle-engine/castle-engine/issues/295 : |
| 353 |
Viewport.PositionToXxx should return correct values even when used before |
| 354 |
and resize/render event. } |
| 355 |
|
| 356 |
procedure TestQueryPosition(const ScreenPos: TVector2; |
| 357 |
const CorrectRayOrigin, CorrectRayDirection, CorrectCameraPlaneResult, CorrectWorldPlaneResult: TVector3; |
| 358 |
const CorrectPos2D: TVector2); |
| 359 |
var |
| 360 |
CameraPlaneResult, RayOrigin, RayDirection, WorldPlaneResult: TVector3; |
| 361 |
Pos2D: TVector2; |
| 362 |
begin |
| 363 |
//WritelnLog('Testing on ', ScreenPos.ToString); |
| 364 |
|
| 365 |
Viewport.PositionToRay(ScreenPos, true, RayOrigin, RayDirection); |
| 366 |
AssertVectorEquals(CorrectRayOrigin, RayOrigin, 0.1); |
| 367 |
AssertVectorEquals(CorrectRayDirection, RayDirection, 0.1); |
| 368 |
|
| 369 |
AssertTrue(Viewport.PositionToCameraPlane(ScreenPos, true, 2, CameraPlaneResult)); |
| 370 |
AssertVectorEquals(CorrectCameraPlaneResult, CameraPlaneResult, 0.1); |
| 371 |
|
| 372 |
AssertTrue(Viewport.PositionToWorldPlane(ScreenPos, true, -10, WorldPlaneResult)); |
| 373 |
AssertVectorEquals(CorrectWorldPlaneResult, WorldPlaneResult, 0.1); |
| 374 |
|
| 375 |
AssertTrue(Viewport.PositionToWorldPlane(ScreenPos, true, 2, -10, WorldPlaneResult)); |
| 376 |
AssertVectorEquals(CorrectWorldPlaneResult, WorldPlaneResult, 0.1); |
| 377 |
|
| 378 |
Pos2D := Viewport.PositionTo2DWorld(ScreenPos, true); |
| 379 |
AssertVectorEquals(CorrectPos2D, Pos2D, 0.1); |
| 380 |
end; |
| 381 |
|
| 382 |
var |
| 383 |
Window: TCastleWindow; |
| 384 |
begin |
| 385 |
if not CanCreateWindowForTest then |
| 386 |
Exit; |
| 387 |
|
| 388 |
Window := CreateWindowForTest; |
| 389 |
try |
| 390 |
Window.Width := 300; |
| 391 |
Window.Height := 300; |
| 392 |
Window.Open; |
| 393 |
|
| 394 |
Viewport := TCastleViewport.Create(Window); |
| 395 |
Viewport.FullSize := true; |
| 396 |
|
| 397 |
// too early to use |
| 398 |
// TestQueryPosition(Vector2(100, 100)); |
| 399 |
// TestQueryPosition(Vector2(Window.Width / 2, Window.Height / 2)); |
| 400 |
|
| 401 |
Window.Controls.InsertFront(Viewport); |
| 402 |
|
| 403 |
// make sure Window and Viewport sizes are as expected |
| 404 |
AssertSameValue(150, Window.Width / 2); |
| 405 |
AssertSameValue(150, Window.Height / 2); |
| 406 |
AssertSameValue(150, Viewport.RenderRect.Width / 2); |
| 407 |
AssertSameValue(150, Viewport.RenderRect.Height / 2); |
| 408 |
|
| 409 |
TestQueryPosition(Vector2(100, 100), |
| 410 |
Vector3(0.00, 0.00, 0.00), |
| 411 |
Vector3(-0.13, -0.13, -0.98), |
| 412 |
Vector3(-0.27, -0.27, -2.00), |
| 413 |
Vector3(-1.37, -1.37, -10.00), |
| 414 |
Vector2(100.00, 100.00) |
| 415 |
); |
| 416 |
|
| 417 |
TestQueryPosition(Vector2(Window.Width / 2, Window.Height / 2), |
| 418 |
Vector3(0.00, 0.00, 0.00), |
| 419 |
Vector3(0.00, 0.00, -1.00), |
| 420 |
Vector3(0.00, 0.00, -2.00), |
| 421 |
Vector3(0.01, 0.01, -10.00), |
| 422 |
Vector2(150.00, 150.00) |
| 423 |
); |
| 424 |
finally DestroyWindowForTest(Window) end; |
| 425 |
end; |
| 426 |
|
| 427 |
procedure TTestCastleWindow.TestStateAutoStop; |
| 428 |
|
| 429 |
{ Test something similar to https://github.com/castle-engine/castle-engine/issues/307 . |
| 430 |
|
| 431 |
However, this was always working, it does *not* reproduce what |
| 432 |
https://github.com/castle-engine/castle-engine/issues/307 did. |
| 433 |
There is TCastleControl that does |
| 434 |
|
| 435 |
procedure TCastleForm.WindowOpen(Sender: TObject); |
| 436 |
begin |
| 437 |
TCastleControl.MainControl := Window; |
| 438 |
CastleApp := TCastleApp.Create(Window); |
| 439 |
TCastleView.Current := CastleApp; |
| 440 |
Window.Container.UIScaling := usNone; |
| 441 |
end; |
| 442 |
} |
| 443 |
|
| 444 |
var |
| 445 |
Window: TCastleWindow; |
| 446 |
SomeState: TCastleView; |
| 447 |
begin |
| 448 |
if not CanCreateWindowForTest then |
| 449 |
Exit; |
| 450 |
|
| 451 |
Window := CreateWindowForTest; |
| 452 |
try |
| 453 |
Window.Open; |
| 454 |
SomeState := TCastleView.Create(Window); |
| 455 |
Window.Container.View := SomeState; |
| 456 |
AssertTrue(TCastleView.Current = SomeState); // deprecated |
| 457 |
AssertTrue(Window.Container.View = SomeState); |
| 458 |
finally |
| 459 |
{ let freeing Window cause everything else: |
| 460 |
- freeing of SomeState |
| 461 |
- stopping of SomeState |
| 462 |
- closing of Window |
| 463 |
} |
| 464 |
DestroyWindowForTest(Window); |
| 465 |
end; |
| 466 |
end; |
| 467 |
|
| 468 |
type |
| 469 |
TStateTestingSize = class(TCastleView) |
| 470 |
public |
| 471 |
W, H: Single; |
| 472 |
TestCase: TCastleTestCase; |
| 473 |
procedure Start; override; |
| 474 |
procedure Resize; override; |
| 475 |
end; |
| 476 |
|
| 477 |
procedure TStateTestingSize.Start; |
| 478 |
begin |
| 479 |
inherited; |
| 480 |
W := EffectiveWidth; |
| 481 |
H := EffectiveHeight; |
| 482 |
TestCase.AssertTrue( |
| 483 |
SameValue(EffectiveWidth, 160) or |
| 484 |
SameValue(EffectiveHeight, 90)); |
| 485 |
TestCase.AssertTrue( |
| 486 |
SameValue(EffectiveRect.Width, 160) or |
| 487 |
SameValue(EffectiveRect.Height, 90)); |
| 488 |
end; |
| 489 |
|
| 490 |
procedure TStateTestingSize.Resize; |
| 491 |
begin |
| 492 |
inherited; |
| 493 |
TestCase.AssertTrue( |
| 494 |
SameValue(EffectiveWidth, 160) or |
| 495 |
SameValue(EffectiveHeight, 90)); |
| 496 |
TestCase.AssertTrue( |
| 497 |
SameValue(EffectiveRect.Width, 160) or |
| 498 |
SameValue(EffectiveRect.Height, 90)); |
| 499 |
// size didn't change from Start to 1st Resize call |
| 500 |
TestCase.AssertSameValue(W, EffectiveWidth); |
| 501 |
TestCase.AssertSameValue(H, EffectiveHeight); |
| 502 |
end; |
| 503 |
|
| 504 |
procedure TTestCastleWindow.TestStateSize; |
| 505 |
var |
| 506 |
Window: TCastleWindow; |
| 507 |
StateTesting: TStateTestingSize; |
| 508 |
begin |
| 509 |
if not CanCreateWindowForTest then |
| 510 |
Exit; |
| 511 |
|
| 512 |
Window := CreateWindowForTest; |
| 513 |
try |
| 514 |
Window.Open; |
| 515 |
Window.Container.UIScaling := usEncloseReferenceSize; |
| 516 |
Window.Container.UIReferenceWidth := 160; |
| 517 |
Window.Container.UIReferenceHeight := 90; |
| 518 |
|
| 519 |
StateTesting := TStateTestingSize.Create(Window); |
| 520 |
StateTesting.TestCase := Self; |
| 521 |
|
| 522 |
// already state EffectiveXxx for size should work |
| 523 |
AssertTrue( |
| 524 |
SameValue(StateTesting.EffectiveWidth, 160) or |
| 525 |
SameValue(StateTesting.EffectiveHeight, 90)); |
| 526 |
AssertTrue( |
| 527 |
SameValue(StateTesting.EffectiveRect.Width, 160) or |
| 528 |
SameValue(StateTesting.EffectiveRect.Height, 90)); |
| 529 |
|
| 530 |
TCastleView.Current := StateTesting; |
| 531 |
finally |
| 532 |
DestroyWindowForTest(Window); |
| 533 |
end; |
| 534 |
end; |
| 535 |
|
| 536 |
type |
| 537 |
TStateTestingSize2 = class(TCastleView) |
| 538 |
public |
| 539 |
W, H: Single; |
| 540 |
TestCase: TCastleTestCase; |
| 541 |
procedure Start; override; |
| 542 |
procedure Resize; override; |
| 543 |
end; |
| 544 |
|
| 545 |
procedure TStateTestingSize2.Start; |
| 546 |
begin |
| 547 |
inherited; |
| 548 |
W := EffectiveWidth; |
| 549 |
H := EffectiveHeight; |
| 550 |
TestCase.AssertTrue( |
| 551 |
SameValue(EffectiveWidth, 200) or |
| 552 |
SameValue(EffectiveHeight, 400)); |
| 553 |
TestCase.AssertTrue( |
| 554 |
SameValue(EffectiveRect.Width, 200) or |
| 555 |
SameValue(EffectiveRect.Height, 400)); |
| 556 |
end; |
| 557 |
|
| 558 |
procedure TStateTestingSize2.Resize; |
| 559 |
begin |
| 560 |
inherited; |
| 561 |
TestCase.AssertTrue( |
| 562 |
SameValue(EffectiveWidth, 200) or |
| 563 |
SameValue(EffectiveHeight, 400)); |
| 564 |
TestCase.AssertTrue( |
| 565 |
SameValue(EffectiveRect.Width, 200) or |
| 566 |
SameValue(EffectiveRect.Height, 400)); |
| 567 |
// size didn't change from Start to 1st Resize call |
| 568 |
TestCase.AssertSameValue(W, EffectiveWidth); |
| 569 |
TestCase.AssertSameValue(H, EffectiveHeight); |
| 570 |
end; |
| 571 |
|
| 572 |
procedure TTestCastleWindow.TestStateSize2; |
| 573 |
var |
| 574 |
Window: TCastleWindow; |
| 575 |
StateTesting: TStateTestingSize2; |
| 576 |
begin |
| 577 |
if not CanCreateWindowForTest then |
| 578 |
Exit; |
| 579 |
|
| 580 |
Window := CreateWindowForTest; |
| 581 |
try |
| 582 |
Window.Width := 200; |
| 583 |
Window.Height := 400; |
| 584 |
Window.Open; |
| 585 |
{ No UI scaling this time. } |
| 586 |
|
| 587 |
StateTesting := TStateTestingSize2.Create(Window); |
| 588 |
StateTesting.TestCase := Self; |
| 589 |
|
| 590 |
// already state EffectiveXxx for size should work |
| 591 |
AssertTrue( |
| 592 |
SameValue(StateTesting.EffectiveWidth, 200) or |
| 593 |
SameValue(StateTesting.EffectiveHeight, 400)); |
| 594 |
AssertTrue( |
| 595 |
SameValue(StateTesting.EffectiveRect.Width, 200) or |
| 596 |
SameValue(StateTesting.EffectiveRect.Height, 400)); |
| 597 |
|
| 598 |
TCastleView.Current := StateTesting; |
| 599 |
finally |
| 600 |
DestroyWindowForTest(Window); |
| 601 |
end; |
| 602 |
end; |
| 603 |
|
| 604 |
procedure TTestCastleWindow.TestViewportWithoutCamera; |
| 605 |
var |
| 606 |
Window: TCastleWindow; |
| 607 |
V: TCastleViewport; |
| 608 |
DummyHandleInput: Boolean; |
| 609 |
begin |
| 610 |
if not CanCreateWindowForTest then |
| 611 |
Exit; |
| 612 |
|
| 613 |
Window := CreateWindowForTest; |
| 614 |
try |
| 615 |
Window.Visible := false; |
| 616 |
Window.Open; |
| 617 |
|
| 618 |
V := TCastleViewport.Create(Window); |
| 619 |
AssertTrue(V.Camera <> nil); |
| 620 |
AssertTrue(V.Camera.Owner = V); // camera created by TCastleViewport.CommonCreate is owned by viewport |
| 621 |
V.Camera.Free; |
| 622 |
AssertTrue(V.Camera = nil); // freeing camera should make it nil also |
| 623 |
|
| 624 |
{ test V.PrepareResources has no problem with Camera=nil } |
| 625 |
V.PrepareResources; |
| 626 |
|
| 627 |
{ test V.Update has no problem with Camera=nil } |
| 628 |
DummyHandleInput := true; |
| 629 |
V.Update(1/30, DummyHandleInput); |
| 630 |
|
| 631 |
{ test V.Render and friends have no problem with Camera=nil } |
| 632 |
V.BeforeRender; |
| 633 |
V.Render; |
| 634 |
V.RenderOverChildren; |
| 635 |
|
| 636 |
FreeAndNil(V); |
| 637 |
finally |
| 638 |
DestroyWindowForTest(Window); |
| 639 |
end; |
| 640 |
end; |
| 641 |
|
| 642 |
procedure TTestCastleWindow.TestPrepareResourcesWithoutContextOpen; |
| 643 |
var |
| 644 |
Window: TCastleWindow; |
| 645 |
V: TCastleViewport; |
| 646 |
Scene, Scene2: TCastleScene; |
| 647 |
begin |
| 648 |
if not CanCreateWindowForTest then |
| 649 |
Exit; |
| 650 |
|
| 651 |
Window := CreateWindowForTest; |
| 652 |
try |
| 653 |
V := TCastleViewport.Create(Window); |
| 654 |
|
| 655 |
{ test V.PrepareResources has no problem called before context open } |
| 656 |
V.PrepareResources; |
| 657 |
|
| 658 |
Scene := TCastleScene.Create(Window); |
| 659 |
Scene.Load('castle-data:/game/scene.x3d'); |
| 660 |
V.Items.Add(Scene); |
| 661 |
|
| 662 |
{ test V.PrepareResources has no problem called before context open, |
| 663 |
when it has some scene } |
| 664 |
V.PrepareResources; |
| 665 |
|
| 666 |
Scene2 := TCastleScene.Create(Window); |
| 667 |
Scene2.Load('castle-data:/knight_resource/knight.gltf'); |
| 668 |
V.Items.Add(Scene2); |
| 669 |
|
| 670 |
{ test V.PrepareResources has no problem called before context open, |
| 671 |
when it has 2 scenes } |
| 672 |
V.PrepareResources; |
| 673 |
|
| 674 |
Window.Visible := false; |
| 675 |
Window.Open; |
| 676 |
|
| 677 |
V.PrepareResources; |
| 678 |
|
| 679 |
FreeAndNil(V); |
| 680 |
finally |
| 681 |
DestroyWindowForTest(Window); |
| 682 |
end; |
| 683 |
end; |
| 684 |
|
| 685 |
initialization |
| 686 |
RegisterTest(TTestCastleWindow); |
| 687 |
end. |