public void Tests() { ManualTimer manualTimer = new ManualTimer(); int fireCount = 0; // Can't start without an action. Assert.Throws <InvalidOperationException>(() => manualTimer.Start()); // Can't fire without an action. Assert.Throws <InvalidOperationException>(() => manualTimer.Fire()); // Can't set an empty action. Assert.Throws <ArgumentNullException>(() => manualTimer.SetAction(null)); manualTimer.SetAction(() => fireCount++); // Can't set action twice. Assert.Throws <InvalidOperationException>(() => manualTimer.SetAction(() => fireCount++)); // Can't fire before starting. Assert.Throws <InvalidOperationException>(() => manualTimer.Fire()); manualTimer.Start(); Assert.AreEqual(0, fireCount); manualTimer.Fire(); Assert.AreEqual(1, fireCount); // Can't fire again before re-starting. Assert.Throws <InvalidOperationException>(() => manualTimer.Fire()); manualTimer.Start(); Assert.AreEqual(1, fireCount); manualTimer.Fire(); Assert.AreEqual(2, fireCount); }
public void PlayTest() { ManualTimer manualTimer = new ManualTimer(); using (GameViewModel gameViewModel = CreateGameViewModel(manualTimer)) { Assert.IsTrue(gameViewModel.StartCommand.CanExecute(null)); Assert.IsFalse(gameViewModel.HitCommand.CanExecute(null)); Assert.IsTrue(gameViewModel.OverlayIsVisible); // Should not be able to Hit if a game is not in progress. Assert.Throws <InvalidOperationException>(() => gameViewModel.HitCommand.Execute(0)); // Start a game. gameViewModel.StartCommand.Execute(null); // Can't start or hit during countdown. Assert.IsFalse(gameViewModel.StartCommand.CanExecute(null)); Assert.IsFalse(gameViewModel.HitCommand.CanExecute(null)); Assert.IsTrue(gameViewModel.OverlayIsVisible); // Should not be able to Start if a game is already in progress. Assert.Throws <InvalidOperationException>(() => gameViewModel.StartCommand.Execute(null)); // Do countdown. for (int i = 0; i < Constants.CountdownSteps - 1; i++) { manualTimer.Fire(); Assert.IsFalse(gameViewModel.HitCommand.CanExecute(null)); } manualTimer.Fire(); Assert.IsTrue(gameViewModel.HitCommand.CanExecute(0)); Assert.IsFalse(gameViewModel.OverlayIsVisible); int score = 0; for (int i = 0; i < Constants.TargetCount; i++) { gameViewModel.HitCommand.Execute(i); Assert.AreNotEqual(score, gameViewModel.Score); score = gameViewModel.Score; } Assert.Throws <ArgumentOutOfRangeException>(() => gameViewModel.HitCommand.Execute(Constants.TargetCount)); double timeLeft = gameViewModel.TimeLeft; for (int i = 0; i < Constants.PlaySeconds; i++) { Thread.Sleep(1000); manualTimer.Fire(); Assert.AreNotEqual(timeLeft, gameViewModel.TimeLeft); timeLeft = gameViewModel.TimeLeft; } Assert.AreEqual(0, timeLeft); Assert.IsTrue(gameViewModel.StartCommand.CanExecute(null)); Assert.IsFalse(gameViewModel.HitCommand.CanExecute(null)); Assert.IsTrue(gameViewModel.OverlayIsVisible); } }