PageRenderTime 480ms CodeModel.GetById 4ms RepoModel.GetById 1ms app.codeStats 0ms

/Scenes/Tests/SceneTests.cs

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