Beispiel #1
0
        public void EndDrawCalledOnceTest()
        {
            DelegateBasedEventSource evt = new DelegateBasedEventSource(delegate(DelegateBasedEventSource e) { e.Game.Tick(); });
            TestGame game = new TestGame(evt);

            ((TestGameWindow)game.Window).RaiseVisibleChanged(true);

            game.Run();
            Assert.AreEqual(1, game.CountEndDraw, "EndDraw was called an unexpected number of times.");
        }
Beispiel #2
0
        public void GameRaisesOnDeactivatedEventTest()
        {
            DelegateBasedEventSource evt = new DelegateBasedEventSource(delegate(DelegateBasedEventSource e) { ((TestGameWindow)e.Game.Window).RaiseDeactivating(); });
            Game gd = new TestGame(evt, true);

            bool fired = false;

            gd.Deactivated += delegate { fired = true; };
            gd.Run();

            Assert.IsTrue(fired, "Deactivated event was not raised.");
        }
Beispiel #3
0
        public void EndDrawNotCalledIfBeginDrawReturnsFalseTest()
        {
            DelegateBasedEventSource evt = new DelegateBasedEventSource(delegate(DelegateBasedEventSource e) { e.Game.Tick(); });
            TestGame game = new TestGame(evt);

            ((TestGameWindow)game.Window).RaiseVisibleChanged(true);
            DummyGraphicsDeviceManager gdm = (DummyGraphicsDeviceManager)game.GraphicsDeviceManager;

            gdm.BeginDrawResult = false;

            game.Run();
            Assert.AreEqual(0, game.CountEndDraw, "EndDraw was called an unexpected number of times.");
        }
Beispiel #4
0
        public void DrawCalledOnVisibleIDrawableGameComponent()
        {
            DelegateBasedEventSource evt = new DelegateBasedEventSource(delegate(DelegateBasedEventSource e) { e.Game.Tick(); });
            TestGame game = new TestGame(evt);

            ((TestGameWindow)game.Window).RaiseVisibleChanged(true);

            DrawableGameComponentTestBase gc = new DrawableGameComponentTestBase(game);

            game.Components.Add(gc);

            game.Run();

            Assert.AreEqual(1, gc.CountDraw);
        }
Beispiel #5
0
        public void GameSleepsWhenDeactivatedTest()
        {
            DelegateBasedEventSource evt = new DelegateBasedEventSource(delegate(DelegateBasedEventSource e)
            {
                ((TestGameWindow)e.Game.Window).RaiseDeactivating();
                Assert.IsFalse(e.Game.IsActive, "Game should be inactive.");
                e.Game.Tick();
            });
            TestGame gd = new TestGame(evt, true);

            gd.InactiveSleepTime = TimeSpan.FromMilliseconds(500);

            Stopwatch sw = Stopwatch.StartNew();

            gd.Run();
            sw.Stop();
            Assert.IsTrue(sw.ElapsedMilliseconds >= 500, "Time elapsed should be at least 500ms");
        }
Beispiel #6
0
        public void UpdateCalledOnceForIUpdatableGameComponent()
        {
            DelegateBasedEventSource evt = new DelegateBasedEventSource(delegate(DelegateBasedEventSource e)
            {
                GameComponentTestBase u = (GameComponentTestBase)e.Game.Components[0];
                u.Enabled = false;
                for (int i = 0; i < 5; i++)
                {
                    TestClock.Instance.Step(100);
                    e.Game.Tick();
                }
            });
            TestGame game            = new TestGame(evt);
            GameComponentTestBase gc = new GameComponentTestBase(game);

            game.Components.Add(gc);

            game.Run();

            Assert.AreEqual(1, gc.CountUpdate);
        }
Beispiel #7
0
        public void UpdateLoopCatchupForFixedTimeStepGameTest()
        {
            const int LOOP_COUNT         = 5;
            const int STEP_RATE          = 200; // milliseconds per step
            const int TARGET_UPDATE_RATE = 100; // update called every 100 ms

            DelegateBasedEventSource evt = new DelegateBasedEventSource(delegate(DelegateBasedEventSource e)
            {
                int i = LOOP_COUNT;
                while (i-- > 0)
                {
                    TestClock.Instance.Step(STEP_RATE);
                    e.Game.Tick();
                }
            });
            TestGame gd = new TestGame(evt, true);

            gd.TargetElapsedTime = TimeSpan.FromMilliseconds(TARGET_UPDATE_RATE);
            gd.Run();

            Assert.AreEqual(1 + LOOP_COUNT * STEP_RATE / TARGET_UPDATE_RATE, gd.CountUpdate);
        }
Beispiel #8
0
        public void DrawCalledWhenIDrawableGameComponentIsVisibleTest()
        {
            DelegateBasedEventSource evt = new DelegateBasedEventSource(delegate(DelegateBasedEventSource e)
            {
                DrawableGameComponentTestBase u = (DrawableGameComponentTestBase)e.Game.Components[0];
                for (int i = 1; i < 6; i++)
                {
                    TestClock.Instance.Step(100);
                    e.Game.Tick();
                    u.Visible = i % 2 == 0;
                }
            });
            TestGame game = new TestGame(evt);

            ((TestGameWindow)game.Window).RaiseVisibleChanged(true);
            DrawableGameComponentTestBase gc = new DrawableGameComponentTestBase(game);

            game.Components.Add(gc);

            game.Run();

            Assert.AreEqual(3, gc.CountDraw, "Unexpected draw count");
            Assert.AreEqual(6, gc.CountUpdate, "Unexpected update count");
        }
Beispiel #9
0
        public void UpdateLoopForVariableTimeStepGameTest()
        {
            const int LOOP_COUNT         = 5;
            const int STEP_RATE          = 50;  // milliseconds per step
            const int TARGET_UPDATE_RATE = 100; // update called every 100 ms

            DelegateBasedEventSource evt = new DelegateBasedEventSource(delegate(DelegateBasedEventSource e)
            {
                int i = LOOP_COUNT;
                while (i-- > 0)
                {
                    TestClock.Instance.Step(STEP_RATE);
                    e.Game.Tick();
                }
            });
            TestGame gd = new TestGame(evt, true);

            gd.TargetElapsedTime = TimeSpan.FromMilliseconds(TARGET_UPDATE_RATE);
            gd.IsFixedTimeStep   = false;
            ((TestGameWindow)gd.Window).RaiseVisibleChanged(true);

            gd.Run();
            Assert.AreEqual(gd.CountUpdate, gd.CountDraw + 1);
        }
Beispiel #10
0
        public void UpdateLoopCatchupForFixedTimeStepGameWhereStepRateExceedsMAX_ELAPSEDTest()
        {
            const int LOOP_COUNT         = 5;
            const int STEP_RATE          = 1000; // milliseconds per step
            const int TARGET_UPDATE_RATE = 100;  // update called every 100 ms

            Assert.Greater((double)STEP_RATE, (double)TestClock.MAX_ELAPSED, "STEP_RATE should be greater than TestClock.MAX_ELAPSED for this test");

            DelegateBasedEventSource evt = new DelegateBasedEventSource(delegate(DelegateBasedEventSource e)
            {
                int i = LOOP_COUNT;
                while (i-- > 0)
                {
                    TestClock.Instance.Step(STEP_RATE);
                    e.Game.Tick();
                }
            });
            TestGame gd = new TestGame(evt, true);

            gd.TargetElapsedTime = TimeSpan.FromMilliseconds(TARGET_UPDATE_RATE);
            gd.Run();

            Assert.AreEqual(1 + LOOP_COUNT * TestClock.MAX_ELAPSED / TARGET_UPDATE_RATE, gd.CountUpdate);
        }
Beispiel #11
0
        public void EndDrawNotCalledIfBeginDrawReturnsFalseTest()
        {
            DelegateBasedEventSource evt = new DelegateBasedEventSource(delegate(DelegateBasedEventSource e) { e.Game.Tick(); });
            TestGame game = new TestGame(evt);
            ((TestGameWindow)game.Window).RaiseVisibleChanged(true);
            DummyGraphicsDeviceManager gdm = (DummyGraphicsDeviceManager)game.GraphicsDeviceManager;
            gdm.BeginDrawResult = false;

            game.Run();
            Assert.AreEqual(0, game.CountEndDraw, "EndDraw was called an unexpected number of times.");
        }
Beispiel #12
0
        public void UpdateLoopForVariableTimeStepGameTest()
        {
            const int LOOP_COUNT = 5;
            const int STEP_RATE = 50; // milliseconds per step
            const int TARGET_UPDATE_RATE = 100; // update called every 100 ms

            DelegateBasedEventSource evt = new DelegateBasedEventSource(delegate(DelegateBasedEventSource e)
                                                                            {
                                                                                int i = LOOP_COUNT;
                                                                                while (i-- > 0)
                                                                                {
                                                                                    TestClock.Instance.Step(STEP_RATE);
                                                                                    e.Game.Tick();
                                                                                }
                                                                            });
            TestGame gd = new TestGame(evt, true);
            gd.TargetElapsedTime = TimeSpan.FromMilliseconds(TARGET_UPDATE_RATE);
            gd.IsFixedTimeStep = false;
            ((TestGameWindow)gd.Window).RaiseVisibleChanged(true);

            gd.Run();
            Assert.AreEqual(gd.CountUpdate, gd.CountDraw + 1);
        }
Beispiel #13
0
        public void UpdateLoopCatchupForFixedTimeStepGameTest()
        {
            const int LOOP_COUNT = 5;
            const int STEP_RATE = 200; // milliseconds per step
            const int TARGET_UPDATE_RATE = 100; // update called every 100 ms

            DelegateBasedEventSource evt = new DelegateBasedEventSource(delegate(DelegateBasedEventSource e)
                                                                            {
                                                                                int i = LOOP_COUNT;
                                                                                while (i-- > 0)
                                                                                {
                                                                                    TestClock.Instance.Step(STEP_RATE);
                                                                                    e.Game.Tick();
                                                                                }
                                                                            });
            TestGame gd = new TestGame(evt, true);
            gd.TargetElapsedTime = TimeSpan.FromMilliseconds(TARGET_UPDATE_RATE);
            gd.Run();

            Assert.AreEqual(1 + LOOP_COUNT*STEP_RATE/TARGET_UPDATE_RATE, gd.CountUpdate);
        }
Beispiel #14
0
        public void UpdateLoopCatchupForFixedTimeStepGameWhereStepRateExceedsMAX_ELAPSEDTest()
        {
            const int LOOP_COUNT = 5;
            const int STEP_RATE = 1000; // milliseconds per step
            const int TARGET_UPDATE_RATE = 100; // update called every 100 ms

            Assert.Greater((double)STEP_RATE, (double)TestClock.MAX_ELAPSED, "STEP_RATE should be greater than TestClock.MAX_ELAPSED for this test");

            DelegateBasedEventSource evt = new DelegateBasedEventSource(delegate(DelegateBasedEventSource e)
                                                                            {
                                                                                int i = LOOP_COUNT;
                                                                                while (i-- > 0)
                                                                                {
                                                                                    TestClock.Instance.Step(STEP_RATE);
                                                                                    e.Game.Tick();
                                                                                }
                                                                            });
            TestGame gd = new TestGame(evt, true);
            gd.TargetElapsedTime = TimeSpan.FromMilliseconds(TARGET_UPDATE_RATE);
            gd.Run();

            Assert.AreEqual(1 + LOOP_COUNT*TestClock.MAX_ELAPSED/TARGET_UPDATE_RATE, gd.CountUpdate);
        }
Beispiel #15
0
        public void GameRaisesOnDeactivatedEventTest()
        {
            DelegateBasedEventSource evt = new DelegateBasedEventSource(delegate(DelegateBasedEventSource e) { ((TestGameWindow)e.Game.Window).RaiseDeactivating(); });
            Game gd = new TestGame(evt, true);

            bool fired = false;
            gd.Deactivated += delegate { fired = true; };
            gd.Run();

            Assert.IsTrue(fired, "Deactivated event was not raised.");
        }
Beispiel #16
0
        public void GameSleepsWhenDeactivatedTest()
        {
            DelegateBasedEventSource evt = new DelegateBasedEventSource(delegate(DelegateBasedEventSource e)
                                                                            {
                                                                                ((TestGameWindow)e.Game.Window).RaiseDeactivating();
                                                                                Assert.IsFalse(e.Game.IsActive, "Game should be inactive.");
                                                                                e.Game.Tick();
                                                                            });
            TestGame gd = new TestGame(evt, true);
            gd.InactiveSleepTime = TimeSpan.FromMilliseconds(500);

            Stopwatch sw = Stopwatch.StartNew();
            gd.Run();
            sw.Stop();
            Assert.IsTrue(sw.ElapsedMilliseconds >= 500, "Time elapsed should be at least 500ms");
        }
Beispiel #17
0
        public void UpdateCalledOnceForIUpdatableGameComponent()
        {
            DelegateBasedEventSource evt = new DelegateBasedEventSource(delegate(DelegateBasedEventSource e)
                                                                            {
                                                                                GameComponentTestBase u = (GameComponentTestBase)e.Game.Components[0];
                                                                                u.Enabled = false;
                                                                                for (int i = 0; i < 5; i++)
                                                                                {
                                                                                    TestClock.Instance.Step(100);
                                                                                    e.Game.Tick();
                                                                                }
                                                                            });
            TestGame game = new TestGame(evt);
            GameComponentTestBase gc = new GameComponentTestBase(game);
            game.Components.Add(gc);

            game.Run();

            Assert.AreEqual(1, gc.CountUpdate);
        }
Beispiel #18
0
        public void EndDrawCalledOnceTest()
        {
            DelegateBasedEventSource evt = new DelegateBasedEventSource(delegate(DelegateBasedEventSource e) { e.Game.Tick(); });
            TestGame game = new TestGame(evt);
            ((TestGameWindow)game.Window).RaiseVisibleChanged(true);

            game.Run();
            Assert.AreEqual(1, game.CountEndDraw, "EndDraw was called an unexpected number of times.");
        }
Beispiel #19
0
        public void DrawCalledWhenIDrawableGameComponentIsVisibleTest()
        {
            DelegateBasedEventSource evt = new DelegateBasedEventSource(delegate(DelegateBasedEventSource e)
                                                                            {
                                                                                DrawableGameComponentTestBase u = (DrawableGameComponentTestBase)e.Game.Components[0];
                                                                                for (int i = 1; i < 6; i++)
                                                                                {
                                                                                    TestClock.Instance.Step(100);
                                                                                    e.Game.Tick();
                                                                                    u.Visible = i%2 == 0;
                                                                                }
                                                                            });
            TestGame game = new TestGame(evt);
            ((TestGameWindow)game.Window).RaiseVisibleChanged(true);
            DrawableGameComponentTestBase gc = new DrawableGameComponentTestBase(game);
            game.Components.Add(gc);

            game.Run();

            Assert.AreEqual(3, gc.CountDraw, "Unexpected draw count");
            Assert.AreEqual(6, gc.CountUpdate, "Unexpected update count");
        }
Beispiel #20
0
        public void DrawNotCalledOnNonVisibleIDrawableGameComponent()
        {
            DelegateBasedEventSource evt = new DelegateBasedEventSource(delegate(DelegateBasedEventSource e) { e.Game.Tick(); });
            TestGame game = new TestGame(evt);
            ((TestGameWindow)game.Window).RaiseVisibleChanged(true);

            DrawableGameComponentTestBase gc = new DrawableGameComponentTestBase(game);
            game.Components.Add(gc);
            gc.Visible = false;

            game.Run();

            Assert.AreEqual(0, gc.CountDraw);
        }