/Scenes/Tests/SceneTests.cs
C# | 385 lines | 343 code | 42 blank | 0 comment | 9 complexity | 7cb66f9dc29a67c52e8f49d87019d7bb MD5 | raw file
1using System.Collections.Generic; 2using System.Linq; 3using DeltaEngine.Content; 4using DeltaEngine.Core; 5using DeltaEngine.Datatypes; 6using DeltaEngine.Entities; 7using DeltaEngine.Input; 8using DeltaEngine.Input.Mocks; 9using DeltaEngine.Platforms; 10using DeltaEngine.Rendering2D; 11using DeltaEngine.Scenes.Controls; 12using NUnit.Framework; 13 14namespace DeltaEngine.Scenes.Tests 15{ 16 public class SceneTests : TestWithMocksOrVisually 17 { 18 [SetUp] 19 public void SetUp() 20 { 21 scene = new Scene(); 22 material = new Material(ShaderFlags.Position2DColoredTextured, "DeltaEngineLogo"); 23 window = Resolve<Window>(); 24 } 25 26 private Scene scene; 27 private Material material; 28 private Window window; 29 30 31 [Test, CloseAfterFirstFrame] 32 public void LoadSceneWithoutAnyControls() 33 { 34 var loadedScene = ContentLoader.Load<Scene>("EmptyScene"); 35 Assert.AreEqual("EmptyScene", loadedScene.Name); 36 Assert.AreEqual(0, loadedScene.Controls.Count); 37 } 38 39 [Test] 40 public void LoadSceneWithAButton() 41 { 42 var loadedScene = ContentLoader.Load<Scene>("SceneWithAButton"); 43 Assert.AreEqual("SceneWithAButton", loadedScene.Name); 44 Assert.AreEqual(1, loadedScene.Controls.Count); 45 Assert.AreEqual(typeof(Button), loadedScene.Controls[0].GetType()); 46 } 47 48 [Test, CloseAfterFirstFrame] 49 public void LoadSceneWithAButtonAndChangeTheMaterial() 50 { 51 var loadedScene = ContentLoader.Load<Scene>("SceneWithAButtonWithChangedMaterial"); 52 Assert.AreEqual("SceneWithAButtonWithChangedMaterial", loadedScene.Name); 53 Assert.AreEqual(1, loadedScene.Controls.Count); 54 Assert.AreEqual(typeof(Button), loadedScene.Controls[0].GetType()); 55 } 56 57 [Test, CloseAfterFirstFrame] 58 public void LoadSceneWithAButtonAndChangeTheFontText() 59 { 60 var loadedScene = ContentLoader.Load<Scene>("SceneWithAButtonWithDifferentFontText"); 61 Assert.AreEqual("SceneWithAButtonWithDifferentFontText", loadedScene.Name); 62 Assert.AreEqual(1, loadedScene.Controls.Count); 63 Assert.AreEqual(typeof(Button), loadedScene.Controls[0].GetType()); 64 } 65 66 [Test, CloseAfterFirstFrame] 67 public void AddingControlAddsToListOfControls() 68 { 69 Assert.AreEqual(0, scene.Controls.Count); 70 var control = new EmptyControl(); 71 scene.Add(control); 72 Assert.AreEqual(1, scene.Controls.Count); 73 Assert.AreEqual(control, scene.Controls[0]); 74 } 75 76 [Test, CloseAfterFirstFrame] 77 public void AddingListOfControl() 78 { 79 Assert.AreEqual(0, scene.Controls.Count); 80 var controls = new List<EmptyControl> { new EmptyControl(), new EmptyControl() }; 81 scene.Add(controls); 82 Assert.AreEqual(2, scene.Controls.Count); 83 } 84 85 private class EmptyControl : Entity2D 86 { 87 public EmptyControl() 88 : base(Rectangle.Zero) {} 89 } 90 91 [Test, CloseAfterFirstFrame] 92 public void AddingControlTwiceOnlyAddsItOnce() 93 { 94 var control = new EmptyControl(); 95 scene.Add(control); 96 scene.Add(control); 97 Assert.AreEqual(1, scene.Controls.Count); 98 } 99 100 [Test, CloseAfterFirstFrame] 101 public void AddingControlToActiveSceneActivatesIt() 102 { 103 var label = new Sprite(material, Rectangle.One); 104 scene.Show(); 105 scene.Add(label); 106 Assert.IsTrue(label.IsActive); 107 } 108 109 [Test, CloseAfterFirstFrame] 110 public void AddingControlToInactiveSceneDeactivatesIt() 111 { 112 var label = new Sprite(material, Rectangle.One) { IsActive = true }; 113 scene.Hide(); 114 scene.Add(label); 115 Assert.IsFalse(label.IsActive); 116 } 117 118 [Test, CloseAfterFirstFrame] 119 public void RemovingControlRemovesFromListOfControls() 120 { 121 var label = new Sprite(material, Rectangle.One); 122 scene.Add(label); 123 scene.Remove(label); 124 Assert.AreEqual(0, scene.Controls.Count); 125 } 126 127 [Test, CloseAfterFirstFrame] 128 public void RemovingControlDeactivatesIt() 129 { 130 var label = new Sprite(material, Rectangle.One) { IsActive = true }; 131 scene.Add(label); 132 scene.Remove(label); 133 Assert.IsFalse(label.IsActive); 134 } 135 136 [Test, CloseAfterFirstFrame] 137 public void ClearingControlsDeactivatesThem() 138 { 139 var label = new Sprite(material, Rectangle.One) { IsActive = true }; 140 var control = new EmptyControl { IsActive = true }; 141 scene.Add(label); 142 scene.Add(control); 143 scene.Clear(); 144 Assert.AreEqual(0, scene.Controls.Count); 145 Assert.IsFalse(label.IsActive); 146 Assert.IsFalse(control.IsActive); 147 } 148 149 [Test, CloseAfterFirstFrame] 150 public void HidingSceneHidesControls() 151 { 152 var label = new Sprite(material, Rectangle.One) { IsActive = true }; 153 var control = new EmptyControl { IsActive = true }; 154 scene.Add(label); 155 scene.Add(control); 156 scene.Hide(); 157 scene.Hide(); 158 Assert.AreEqual(2, scene.Controls.Count); 159 Assert.IsFalse(label.IsVisible); 160 Assert.IsFalse(control.IsVisible); 161 } 162 163 [Test, CloseAfterFirstFrame] 164 public void ControlsDoNotRespondToInputWhenSceneIsHidden() 165 { 166 if (!IsMockResolver) 167 return; //ncrunch: no coverage 168 var button = CreateButton(); 169 scene.Add(button); 170 scene.Hide(); 171 SetMouseState(State.Pressing, Vector2D.Half); 172 Assert.AreEqual(NormalColor, button.Color); 173 } 174 175 private static readonly Color NormalColor = Color.LightGray; 176 177 private static Button CreateButton() 178 { 179 var theme = new Theme 180 { 181 Button = 182 new Material(ShaderFlags.Position2DColoredTextured, "DeltaEngineLogo") 183 { 184 DefaultColor = NormalColor 185 }, 186 ButtonMouseover = 187 new Material(ShaderFlags.Position2DColoredTextured, "DeltaEngineLogo") 188 { 189 DefaultColor = MouseoverColor 190 }, 191 ButtonPressed = 192 new Material(ShaderFlags.Position2DColoredTextured, "DeltaEngineLogo") 193 { 194 DefaultColor = PressedColor 195 } 196 }; 197 return new Button(theme, Small); 198 } 199 200 private static readonly Rectangle Small = Rectangle.FromCenter(0.5f, 0.5f, 0.3f, 0.1f); 201 private static readonly Color MouseoverColor = Color.White; 202 private static readonly Color PressedColor = Color.Red; 203 204 private void SetMouseState(State state, Vector2D position) 205 { 206 Resolve<MockMouse>().SetNativePosition(position); 207 Resolve<MockMouse>().SetButtonState(MouseButton.Left, state); 208 AdvanceTimeAndUpdateEntities(); 209 } 210 211 [Test, CloseAfterFirstFrame] 212 public void ControlsDoNotRespondToInputWhenInBackground() 213 { 214 if (!IsMockResolver) 215 return; //ncrunch: no coverage 216 var button = CreateButton(); 217 scene.Add(button); 218 scene.ToBackground(); 219 SetMouseState(State.Pressing, Vector2D.Half); 220 Assert.AreEqual(NormalColor, button.Color); 221 } 222 223 [Test, CloseAfterFirstFrame] 224 public void ControlsRespondToInputWhenBroughtBackToForeground() 225 { 226 if (!IsMockResolver) 227 return; //ncrunch: no coverage 228 var button = CreateButton(); 229 scene.Add(button); 230 scene.ToBackground(); 231 scene.ToForeground(); 232 SetMouseState(State.Pressing, Vector2D.Half); 233 Assert.AreEqual(PressedColor, button.Color); 234 } 235 236 [Test] 237 public void DrawButtonWhichChangesColorAndSize() 238 { 239 if (!IsMockResolver) 240 return; //ncrunch: no coverage 241 var button = CreateButton(); 242 button.Start<ChangeSizeDynamically>(); 243 scene.Add(button); 244 AdvanceTimeAndUpdateEntities(); 245 button.State.IsPressed = true; 246 } 247 248 private class ChangeSizeDynamically : UpdateBehavior 249 { 250 public ChangeSizeDynamically() 251 : base(Priority.Low) {} 252 253 public override void Update(IEnumerable<Entity> entities) 254 { 255 foreach (var entity in entities.OfType<Control>()) 256 if (entity.State.IsInside && !entity.State.IsPressed) 257 entity.Set(Big); 258 else 259 entity.Set(Small); 260 } 261 } 262 263 private static readonly Rectangle Big = Rectangle.FromCenter(0.5f, 0.5f, 0.36f, 0.12f); 264 265 [Test, CloseAfterFirstFrame] 266 public void ChangeBackgroundImage() 267 { 268 Assert.AreEqual(0, scene.Controls.Count); 269 var background = new Material(ShaderFlags.Position2DColoredTextured, 270 "SimpleMainMenuBackground"); 271 scene.SetQuadraticBackground(background); 272 Assert.AreEqual(1, scene.Controls.Count); 273 Assert.AreEqual(background, ((Sprite)scene.Controls[0]).Material); 274 var logo = new Material(ShaderFlags.Position2DColoredTextured, "DeltaEngineLogo"); 275 scene.SetQuadraticBackground(logo); 276 Assert.AreEqual(1, scene.Controls.Count); 277 Assert.AreEqual(logo, ((Sprite)scene.Controls[0]).Material); 278 } 279 280 [Test] 281 public void BackgroundImageChangesWhenButtonClicked() 282 { 283 scene.SetQuadraticBackground("SimpleSubMenuBackground"); 284 var button = CreateButton(); 285 button.Clicked += () => scene.SetQuadraticBackground("SimpleMainMenuBackground"); 286 scene.Add(button); 287 } 288 289 [Test] 290 public void SetQuadraticBackgroundOnSquareWindow() 291 { 292 window.ViewportPixelSize = new Size(512, 512); 293 scene.SetQuadraticBackground("CheckerboardImage512x512"); 294 } 295 296 [Test] 297 public void SetQuadraticBackgroundOnLandscapeWindow() 298 { 299 window.ViewportPixelSize = new Size(512, 288); 300 scene.SetQuadraticBackground("CheckerboardImage512x512"); 301 } 302 303 [Test] 304 public void SetQuadraticBackgroundOnPortraitWindow() 305 { 306 window.ViewportPixelSize = new Size(288, 512); 307 scene.SetQuadraticBackground("CheckerboardImage512x512"); 308 } 309 310 [Test] 311 public void SetLandscapeViewportBackgroundOnLandscapeWindow() 312 { 313 window.ViewportPixelSize = new Size(512, 288); 314 scene.SetViewportBackground("CheckerboardImage512x288"); 315 } 316 317 [Test] 318 public void SetPortraitViewportBackgroundOnPortraitWindow() 319 { 320 window.ViewportPixelSize = new Size(288, 512); 321 scene.SetViewportBackground("CheckerboardImage288x512"); 322 } 323 324 [Test] 325 public void SetChangingViewport() 326 { 327 window.ViewportPixelSize = new Size(288, 512); 328 scene.SetViewportBackground("CheckerboardImage288x512"); 329 window.ViewportPixelSize = new Size(512, 288); 330 scene.SetViewportBackground("CheckerboardImage512x288"); 331 } 332 333 [Test, CloseAfterFirstFrame] 334 public void DisposingSceneClearsIt() 335 { 336 var loadedScene = ContentLoader.Load<Scene>("SceneWithAButton"); 337 var button = loadedScene.Controls[0] as Button; 338 loadedScene.Dispose(); 339 Assert.AreEqual(0, loadedScene.Controls.Count); 340 Assert.IsFalse(button.IsActive); 341 } 342 343 [Test, CloseAfterFirstFrame] 344 public void LoadingDisposedSceneRecreatesIt() 345 { 346 var loadedScene = ContentLoader.Load<Scene>("SceneWithAButton"); 347 loadedScene.Dispose(); 348 loadedScene = ContentLoader.Load<Scene>("SceneWithAButton"); 349 Assert.AreEqual(1, loadedScene.Controls.Count); 350 Assert.IsTrue(loadedScene.Controls[0].IsActive); 351 } 352 353 [Test] 354 public void ClickingFullscreenIconShouldNotCreateInputEvents() 355 { 356 var button = new Button(Rectangle.One); 357 button.Start<ReportMouseState>(); 358 } 359 360 private class ReportMouseState : UpdateBehavior 361 { 362 public ReportMouseState(Mouse mouse) 363 { 364 this.mouse = mouse; 365 } 366 367 private readonly Mouse mouse; 368 369 public override void Update(IEnumerable<Entity> buttons) 370 { 371 foreach (var entity in buttons) 372 { 373 var button = (Button)entity; 374 var state = mouse.GetButtonState(MouseButton.Left); 375 if (!button.Contains<State>()) 376 button.Add(state); 377 if (button.Get<State>() == state) 378 return; 379 button.Text += "\n" + state + " " + mouse.Position; //ncrunch: no coverage start 380 button.Set(state); 381 } //ncrunch: no coverage end 382 } 383 } 384 } 385}