A game framework written with osu! in mind.
at master 6.6 kB view raw
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. 2// See the LICENCE file in the repository root for full licence text. 3 4using System.Linq; 5using NUnit.Framework; 6using osu.Framework.Graphics; 7using osu.Framework.Graphics.Containers; 8using osu.Framework.Graphics.Shapes; 9using osuTK; 10using osuTK.Graphics; 11 12namespace osu.Framework.Tests.Visual.Containers 13{ 14 public class TestSceneVisibilityContainer : FrameworkTestScene 15 { 16 private TestVisibilityContainer testContainer; 17 18 [Test] 19 public void TestShowHide() 20 { 21 AddStep("create container", () => Child = testContainer = new TestVisibilityContainer()); 22 23 checkHidden(true); 24 25 AddStep("show", () => testContainer.Show()); 26 checkVisible(); 27 28 AddStep("hide", () => testContainer.Hide()); 29 checkHidden(); 30 31 AddAssert("fire count is 2", () => testContainer.FireCount == 2); 32 } 33 34 [TestCase(true)] 35 [TestCase(false)] 36 public void TestStartHidden(bool startHidden) 37 { 38 AddStep("create container", () => Child = testContainer = 39 new TestVisibilityContainer(startHidden) { State = { Value = Visibility.Visible } }); 40 41 checkVisible(!startHidden); 42 43 AddStep("hide", () => testContainer.Hide()); 44 checkHidden(); 45 46 AddAssert("fire count is 2", () => testContainer.FireCount == 2); 47 } 48 49 [Test] 50 public void TestNotStartHiddenButHidden() 51 { 52 AddStep("create container", () => Child = testContainer = 53 new TestVisibilityContainer(false) { State = { Value = Visibility.Hidden } }); 54 55 AddAssert("alpha above zero", () => testContainer.Alpha > 0); 56 57 checkHidden(false); 58 } 59 60 [Test] 61 public void TestShowInCtor() 62 { 63 AddStep("create container", () => 64 { 65 var container = new TestVisibilityContainer(null); 66 container.Show(); 67 68 var containingContainer = new Container 69 { 70 RelativeSizeAxes = Axes.Both, 71 Child = testContainer = container 72 }; 73 74 containingContainer.OnLoadComplete += _ => testContainer.Hide(); 75 76 Child = containingContainer; 77 }); 78 79 checkHidden(); 80 } 81 82 [TestCase(true, true)] 83 [TestCase(false, true)] 84 [TestCase(true, false)] 85 [TestCase(false, false)] 86 public void TestStartHiddenNested(bool startHidden, bool immediatelyVisible) 87 { 88 TestNestedVisibilityContainer visibility = null; 89 90 AddStep("create container", () => 91 { 92 Child = testContainer = 93 visibility = new TestNestedVisibilityContainer(startHidden) { State = { Value = immediatelyVisible ? Visibility.Visible : Visibility.Hidden } }; 94 95 if (!immediatelyVisible) testContainer.Show(); 96 }); 97 98 checkVisible(!startHidden); 99 100 AddAssert("box has transforms", () => visibility.BoxHasTransforms); 101 AddStep("hide", () => testContainer.Hide()); 102 103 checkHidden(); 104 AddAssert("box doesn't have transforms", () => !visibility.BoxHasTransforms); 105 106 AddAssert("fire count is 2", () => testContainer.FireCount == 2); 107 } 108 109 private void checkHidden(bool instant = false) 110 { 111 AddAssert("is hidden", () => testContainer.State.Value == Visibility.Hidden); 112 if (instant) 113 AddAssert("alpha zero", () => testContainer.Alpha == 0); 114 else 115 AddUntilStep("alpha zero", () => testContainer.Alpha == 0); 116 } 117 118 private void checkVisible(bool instant = false) 119 { 120 AddAssert("is visible", () => testContainer.State.Value == Visibility.Visible); 121 if (instant) 122 AddAssert("alpha one", () => testContainer.Alpha == 1); 123 else 124 AddUntilStep("wait alpha one", () => testContainer.Alpha == 1); 125 } 126 127 private class TestNestedVisibilityContainer : TestVisibilityContainer 128 { 129 public bool BoxHasTransforms => box.Transforms.Any(); 130 131 private readonly TestVisibilityContainer nested; 132 private readonly Box box; 133 134 public TestNestedVisibilityContainer(bool startHidden = true) 135 : base(startHidden) 136 { 137 Add(nested = new TestVisibilityContainer(true, Color4.Yellow)); 138 139 nested.Add(box = new Box 140 { 141 Colour = Color4.Black, 142 RelativeSizeAxes = Axes.Both, 143 Anchor = Anchor.Centre, 144 Origin = Anchor.Centre, 145 Size = new Vector2(0.5f), 146 }); 147 } 148 149 protected override void PopIn() 150 { 151 base.PopIn(); 152 nested.Show(); 153 box.RotateTo(360, 5000); 154 } 155 156 protected override void PopOut() 157 { 158 base.PopOut(); 159 nested.Hide(); 160 box.RotateTo(0); 161 } 162 } 163 164 private class TestVisibilityContainer : VisibilityContainer 165 { 166 private readonly bool? startHidden; 167 168 protected override bool StartHidden => startHidden ?? base.StartHidden; 169 170 public TestVisibilityContainer(bool? startHidden = true, Color4? colour = null) 171 { 172 this.startHidden = startHidden; 173 174 Size = new Vector2(0.5f); 175 RelativeSizeAxes = Axes.Both; 176 177 Anchor = Anchor.Centre; 178 Origin = Anchor.Centre; 179 180 Children = new Drawable[] 181 { 182 new Box 183 { 184 Colour = colour ?? Color4.Cyan, 185 RelativeSizeAxes = Axes.Both, 186 }, 187 }; 188 189 State.ValueChanged += e => FireCount++; 190 } 191 192 public int FireCount { get; private set; } 193 194 protected override void PopIn() 195 { 196 this.FadeIn(1000, Easing.OutQuint); 197 this.ScaleTo(1, 1000, Easing.OutElastic); 198 } 199 200 protected override void PopOut() 201 { 202 this.FadeOut(1000, Easing.OutQuint); 203 this.ScaleTo(0.4f, 1000, Easing.OutQuint); 204 } 205 } 206 } 207}