[longomatch] Simplify API for Timer and complete unit tests



commit 64bb6d62a8f8dc5dfe610fea7e807b16fd2bf9a5
Author: Andoni Morales Alastruey <ylatuya gmail com>
Date:   Fri Mar 6 17:46:05 2015 +0100

    Simplify API for Timer and complete unit tests

 LongoMatch.Core/Store/Timer.cs                  |   27 ++-------
 LongoMatch.Drawing/CanvasObjects/TimerObject.cs |    4 +-
 LongoMatch.GUI.Multimedia/Gui/CapturerBin.cs    |    8 +-
 LongoMatch.GUI/Gui/Component/ProjectPeriods.cs  |    4 +-
 Tests/Core/Store/TestTimer.cs                   |   70 ++++++++++++++++++++++-
 5 files changed, 82 insertions(+), 31 deletions(-)
---
diff --git a/LongoMatch.Core/Store/Timer.cs b/LongoMatch.Core/Store/Timer.cs
index 4c63973..59e8ac8 100644
--- a/LongoMatch.Core/Store/Timer.cs
+++ b/LongoMatch.Core/Store/Timer.cs
@@ -51,39 +51,24 @@ namespace LongoMatch.Core.Store
                [JsonIgnore]
                public Time TotalTime {
                        get {
-                               return new Time (Nodes.Sum (tn => tn.Duration.MSeconds));
+                               return new Time (Nodes.Where (tn=>tn.Start != null && tn.Stop != null)
+                                       .Sum (tn => tn.Duration.MSeconds));
                        }
                }
 
-               public TimeNode StartTimer (Time start, string name = null)
+               public TimeNode Start (Time start, string name = null)
                {
                        TimeNode tn;
 
                        if (name == null)
                                name = Name;
-                       StopTimer (start);
+                       Stop (start);
                        tn = new TimeNode { Name = name, Start = start };
                        Nodes.Add (tn);
                        return tn;
                }
 
-               public void PauseTimer (Time stop)
-               {
-                       TimeNode node = Nodes.LastOrDefault ();
-                       if (node == null) {
-                               throw new TimerNotRunningException ();
-                       }
-                       node.Stop = stop;
-               }
-
-               public TimeNode Resume (Time start)
-               {
-                       TimeNode tn = new TimeNode { Name = Name, Start = start };
-                       Nodes.Add (tn);
-                       return tn;
-               }
-
-               public void StopTimer (Time stop)
+               public void Stop (Time stop)
                {
                        if (Nodes.Count > 0) {
                                TimeNode last = Nodes.Last ();
@@ -94,7 +79,7 @@ namespace LongoMatch.Core.Store
                        Nodes.OrderBy (tn => tn.Start.MSeconds);
                }
 
-               public void CancelTimer ()
+               public void CancelCurrent ()
                {
                        if (Nodes.Count > 0) {
                                TimeNode last = Nodes.Last ();
diff --git a/LongoMatch.Drawing/CanvasObjects/TimerObject.cs b/LongoMatch.Drawing/CanvasObjects/TimerObject.cs
index fd30e2a..d939d70 100644
--- a/LongoMatch.Drawing/CanvasObjects/TimerObject.cs
+++ b/LongoMatch.Drawing/CanvasObjects/TimerObject.cs
@@ -103,8 +103,8 @@ namespace LongoMatch.Drawing.CanvasObjects
                        } else {
                                Log.Debug ("Stop timer at " + CurrentTime.ToMSecondsString ());
                                if (StartTime.MSeconds != CurrentTime.MSeconds) {
-                                       var tn = Button.Timer.StartTimer (StartTime);
-                                       Button.Timer.StopTimer (CurrentTime);
+                                       var tn = Button.Timer.Start (StartTime);
+                                       Button.Timer.Stop (CurrentTime);
                                        Config.EventsBroker.EmitTimerNodeAddedEvent (Button.Timer, tn);
                                }
                                StartTime = null;
diff --git a/LongoMatch.GUI.Multimedia/Gui/CapturerBin.cs b/LongoMatch.GUI.Multimedia/Gui/CapturerBin.cs
index ebe8a7c..8fafcb1 100644
--- a/LongoMatch.GUI.Multimedia/Gui/CapturerBin.cs
+++ b/LongoMatch.GUI.Multimedia/Gui/CapturerBin.cs
@@ -201,7 +201,7 @@ namespace LongoMatch.Gui
                        }
                        currentPeriod = new Period { Name = periodName };
                        
-                       currentTimeNode = currentPeriod.StartTimer (accumTime, periodName);
+                       currentTimeNode = currentPeriod.Start (accumTime, periodName);
                        currentTimeNode.Stop = currentTimeNode.Start;
                        currentPeriodStart = DateTime.UtcNow;
                        timeoutID = GLib.Timeout.Add (20, UpdateTime);
@@ -227,7 +227,7 @@ namespace LongoMatch.Gui
                        }
                        
                        GLib.Source.Remove (timeoutID);
-                       currentPeriod.StopTimer (CurrentCaptureTime);
+                       currentPeriod.Stop (CurrentCaptureTime);
                        accumTime = CurrentCaptureTime;
                        Log.Debug ("Stop period stop=", accumTime.ToMSecondsString ());
                        currentTimeNode = null;
@@ -248,7 +248,7 @@ namespace LongoMatch.Gui
                                return;
                        }
                        Log.Debug ("Pause period at currentTime=", CurrentCaptureTime.ToMSecondsString ());
-                       currentPeriod.PauseTimer (CurrentCaptureTime);
+                       currentPeriod.Stop (CurrentCaptureTime);
                        currentTimeNode = null;
                        pausebutton.Visible = false;
                        resumebutton.Visible = true;
@@ -263,7 +263,7 @@ namespace LongoMatch.Gui
                                return;
                        }
                        Log.Debug ("Resume period at currentTime=", CurrentCaptureTime.ToMSecondsString ());
-                       currentTimeNode = currentPeriod.Resume (CurrentCaptureTime);
+                       currentTimeNode = currentPeriod.Start (CurrentCaptureTime);
                        pausebutton.Visible = true;
                        resumebutton.Visible = false;
                        Capturing = true;
diff --git a/LongoMatch.GUI/Gui/Component/ProjectPeriods.cs b/LongoMatch.GUI/Gui/Component/ProjectPeriods.cs
index c5a45f0..6c18524 100644
--- a/LongoMatch.GUI/Gui/Component/ProjectPeriods.cs
+++ b/LongoMatch.GUI/Gui/Component/ProjectPeriods.cs
@@ -118,8 +118,8 @@ namespace LongoMatch.Gui.Component
                                        gamePeriods = value.Dashboard.GamePeriods;
                                        foreach (string s in gamePeriods) {
                                                Period period = new Period { Name = s };
-                                               period.StartTimer (start);
-                                               period.StopTimer (start + pDuration);
+                                               period.Start (start);
+                                               period.Stop (start + pDuration);
                                                periods.Add (period);
                                                start += pDuration;
                                        }
diff --git a/Tests/Core/Store/TestTimer.cs b/Tests/Core/Store/TestTimer.cs
index f0ca144..810c20c 100644
--- a/Tests/Core/Store/TestTimer.cs
+++ b/Tests/Core/Store/TestTimer.cs
@@ -17,6 +17,8 @@
 //
 using NUnit.Framework;
 using System;
+using LongoMatch.Core.Store;
+using LongoMatch.Core.Common;
 
 namespace Tests.Core.Store
 {
@@ -24,9 +26,73 @@ namespace Tests.Core.Store
        public class TestTimer
        {
                [Test ()]
-               [Ignore ("Not implemented")]
-               public void TestCase ()
+               public void TestSerialization ()
                {
+                       Timer timer = new Timer ();
+                       Utils.CheckSerialization (timer);
+
+                       timer.Name = "test";
+                       timer.Team = LongoMatch.Core.Common.Team.LOCAL;
+                       Timer timer2 = Utils.SerializeDeserialize (timer);
+                       Assert.AreEqual (timer.Name, timer2.Name);
+                       Assert.AreEqual (timer.Nodes, timer2.Nodes);
+                       Assert.AreEqual (timer.Team, timer2.Team);
+               }
+
+               [Test ()]
+               public void TestTotalTime ()
+               {
+                       Timer timer = new Timer { Name = "Test" };
+                       timer.Start (new Time (1000));
+                       timer.Stop (new Time (2000));
+                       Assert.AreEqual (1000, timer.TotalTime.MSeconds);
+                       timer.Start (new Time (3000));
+                       Assert.AreEqual (1000, timer.TotalTime.MSeconds);
+                       timer.Stop (new Time (4000));
+                       Assert.AreEqual (2000, timer.TotalTime.MSeconds);
+               }
+
+               [Test ()]
+               public void TestStartTimer ()
+               {
+                       Timer timer = new Timer { Name = "test" };
+
+                       timer.Start (new Time (1000));
+                       Assert.AreEqual (1, timer.Nodes.Count);
+                       Assert.AreEqual ("test", timer.Nodes [0].Name);
+                       Assert.AreEqual (1000, timer.Nodes [0].Start.MSeconds);
+                       Assert.IsNull (timer.Nodes [0].Stop);
+
+                       timer.Start (new Time (5000), "new");
+                       Assert.AreEqual (2, timer.Nodes.Count);
+                       /* Starting a time should stop the previous period */
+                       Assert.AreEqual (5000, timer.Nodes [0].Stop.MSeconds);
+                       Assert.AreEqual ("new", timer.Nodes [1].Name);
+                       Assert.AreEqual (5000, timer.Nodes [1].Start.MSeconds);
+               }
+
+               [Test ()]
+               public void TestStopTimer ()
+               {
+                       Timer timer = new Timer { Name = "Test" };
+                       timer.Start (new Time (1000));
+                       Assert.IsNull (timer.Nodes [0].Stop);
+                       timer.Stop (new Time (1200));
+                       Assert.AreEqual (1200, timer.Nodes [0].Stop.MSeconds);
+               }
+
+               [Test ()]
+               public void TestCancelTimer ()
+               {
+                       Timer timer = new Timer { Name = "Test" };
+                       timer.Start (new Time (1000));
+                       timer.Stop (new Time (2000));
+                       timer.CancelCurrent ();
+                       Assert.AreEqual (1, timer.Nodes.Count);
+                       timer.Start (new Time (3000));
+                       Assert.AreEqual (2, timer.Nodes.Count);
+                       timer.CancelCurrent ();
+                       Assert.AreEqual (1, timer.Nodes.Count);
                }
        }
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]