A game framework written with osu! in mind.
at master 105 lines 3.2 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 NUnit.Framework; 5using osu.Framework.Graphics; 6using osu.Framework.Graphics.Containers; 7using osu.Framework.Graphics.Shapes; 8using osuTK; 9 10namespace osu.Framework.Tests.Visual.Drawables 11{ 12 public class TestSceneProxyDrawableLifetime : FrameworkTestScene 13 { 14 [SetUp] 15 public void Setup() => Schedule(Clear); 16 17 [Test] 18 public void TestProxyAliveWhenOriginalAlive() 19 { 20 Box box = null; 21 Drawable proxy = null; 22 23 AddStep("add proxy", () => 24 { 25 Add(box = new Box 26 { 27 Anchor = Anchor.Centre, 28 Origin = Anchor.Centre, 29 Size = new Vector2(100) 30 }); 31 32 Add(proxy = box.CreateProxy()); 33 }); 34 35 AddAssert("proxy should be alive", () => proxy.ShouldBeAlive); 36 AddAssert("proxy is alive", () => proxy.IsAlive); 37 38 AddStep("expire box", () => box.Expire()); 39 40 AddAssert("proxy should not be alive", () => !proxy.ShouldBeAlive); 41 AddAssert("proxy is not alive", () => !proxy.IsAlive); 42 } 43 44 [Test] 45 public void TestLifetimeTransferred() 46 { 47 Box box = null; 48 Drawable proxy = null; 49 bool lifetimeChanged = false; 50 51 AddStep("add proxy", () => 52 { 53 lifetimeChanged = false; 54 55 Add(box = new Box 56 { 57 Anchor = Anchor.Centre, 58 Origin = Anchor.Centre, 59 Size = new Vector2(100) 60 }); 61 62 Add(proxy = box.CreateProxy().With(d => d.LifetimeChanged += _ => lifetimeChanged = true)); 63 }); 64 65 AddStep("set lifetimes", () => 66 { 67 box.LifetimeStart = Time.Current - 5000; 68 box.LifetimeEnd = Time.Current + 5000; 69 }); 70 71 AddAssert("lifetime changed", () => lifetimeChanged); 72 AddAssert("lifetime transferred from box", () => proxy.LifetimeStart == box.LifetimeStart && proxy.LifetimeEnd == box.LifetimeEnd); 73 } 74 75 [Test] 76 public void TestRemovedWhenOriginalRemoved() 77 { 78 Container container = null; 79 Box box = null; 80 Drawable proxy = null; 81 82 AddStep("add proxy", () => 83 { 84 Add(container = new Container 85 { 86 RelativeSizeAxes = Axes.Both 87 }); 88 89 container.Add(box = new Box 90 { 91 Anchor = Anchor.Centre, 92 Origin = Anchor.Centre, 93 Size = new Vector2(100) 94 }); 95 96 container.Add(proxy = box.CreateProxy()); 97 }); 98 99 AddStep("expire box", () => box.Expire(true)); 100 101 AddAssert("box removed", () => !container.Contains(box)); 102 AddAssert("proxy removed", () => !container.Contains(proxy)); 103 } 104 } 105}