public void TestDeserializeSTTargetExtensions()
        {
            ILogbook logbook = PluginMain.GetApplication().Logbook;
            Workout deserializedWorkout = new Workout("TestWorkout", logbook.ActivityCategories[0]);
            XmlDocument testDocument = new XmlDocument();
            RegularStep step;

            testDocument.LoadXml(workoutSTExtensionsResult);
            Assert.AreEqual("TrainingCenterDatabase", testDocument.LastChild.Name, "Cannot find database node");
            Assert.AreEqual("Workouts", testDocument.LastChild.FirstChild.Name, "Cannot find workouts node");
            Assert.AreEqual("Workout", testDocument.LastChild.FirstChild.FirstChild.Name, "Cannot find workout node");
            deserializedWorkout.Deserialize(testDocument.LastChild.FirstChild.FirstChild);

            Assert.AreEqual("TestSTExtension", deserializedWorkout.Name, "Invalid workout name deserialized");
            Assert.AreEqual(4, deserializedWorkout.StepCount, "Invalid step count deserialized");

            // Speed ST zone
            BaseSpeedTarget speedTarget;
            SpeedZoneSTTarget speedSTTarget;

            Assert.IsTrue(deserializedWorkout.Steps[0] is RegularStep, "First step is of invalid type");
            step = deserializedWorkout.Steps[0] as RegularStep;
            Assert.IsTrue(step.Target is BaseSpeedTarget, "First step target is of invalid type");
            speedTarget = step.Target as BaseSpeedTarget;
            Assert.IsTrue(speedTarget.ConcreteTarget is SpeedZoneSTTarget, "Speed concrete target is of invalid type");
            speedSTTarget = speedTarget.ConcreteTarget as SpeedZoneSTTarget;
            Assert.AreEqual(logbook.SpeedZones[0].Zones[3], speedSTTarget.Zone, "Invalid speed ST zone deserialized");

            // Cadence ST zone
            BaseCadenceTarget cadenceTarget;
            CadenceZoneSTTarget cadenceSTTarget;

            Assert.IsTrue(deserializedWorkout.Steps[1] is RegularStep, "Second step is of invalid type");
            step = deserializedWorkout.Steps[1] as RegularStep;
            Assert.IsTrue(step.Target is BaseCadenceTarget, "Second step target is of invalid type");
            cadenceTarget = step.Target as BaseCadenceTarget;
            Assert.IsTrue(cadenceTarget.ConcreteTarget is CadenceZoneSTTarget, "Cadence concrete target is of invalid type");
            cadenceSTTarget = cadenceTarget.ConcreteTarget as CadenceZoneSTTarget;
            Assert.AreEqual(logbook.CadenceZones[0].Zones[4], cadenceSTTarget.Zone, "Invalid cadence ST zone deserialized");

            // HR ST zone
            BaseHeartRateTarget hrTarget;
            HeartRateZoneSTTarget hrSTTarget;

            Assert.IsTrue(deserializedWorkout.Steps[2] is RegularStep, "Third step is of invalid type");
            step = deserializedWorkout.Steps[2] as RegularStep;
            Assert.IsTrue(step.Target is BaseHeartRateTarget, "Third step target is of invalid type");
            hrTarget = step.Target as BaseHeartRateTarget;
            Assert.IsTrue(hrTarget.ConcreteTarget is HeartRateZoneSTTarget, "HR concrete target is of invalid type");
            hrSTTarget = hrTarget.ConcreteTarget as HeartRateZoneSTTarget;
            Assert.AreEqual(logbook.HeartRateZones[0].Zones[3], hrSTTarget.Zone, "Invalid HR ST zone deserialized");

            // Power ST zone
            BasePowerTarget powerTarget;
            PowerZoneSTTarget powerSTTarget;

            Assert.IsTrue(deserializedWorkout.Steps[3] is RegularStep, "Fourth step is of invalid type");
            step = deserializedWorkout.Steps[3] as RegularStep;
            Assert.IsTrue(step.Target is BasePowerTarget, "Fourth step target is of invalid type");
            powerTarget = step.Target as BasePowerTarget;
            Assert.IsTrue(powerTarget.ConcreteTarget is PowerZoneSTTarget, "Power concrete target is of invalid type");
            powerSTTarget = powerTarget.ConcreteTarget as PowerZoneSTTarget;
            Assert.AreEqual(logbook.PowerZones[0].Zones[0], powerSTTarget.Zone, "Invalid power ST zone deserialized");
        }
        public void TestTCXDeserialization()
        {
            XmlDocument testDocument = new XmlDocument();
            XmlNode readNode;
            XmlNode database;
            XmlAttribute attribute;
            Workout workout = new Workout("Test", PluginMain.GetApplication().Logbook.ActivityCategories[0]);
            ILogbook logbook = PluginMain.GetApplication().Logbook;

            // Setup document
            testDocument.AppendChild(testDocument.CreateXmlDeclaration("1.0", "UTF-8", "no"));
            database = testDocument.CreateNode(XmlNodeType.Element, "TrainingCenterDatabase", null);
            testDocument.AppendChild(database);
            attribute = testDocument.CreateAttribute("xmlns", "xsi", GarminFitnessPlugin.Constants.xmlns);
            attribute.Value = "http://www.w3.org/2001/XMLSchema-instance";
            database.Attributes.Append(attribute);
            readNode = testDocument.CreateElement("TestNode");
            database.AppendChild(readNode);

            // Workout name
            readNode.InnerXml = workoutTestResult1;
            workout.Deserialize(readNode.FirstChild);
            Assert.AreEqual("WorkoutTest1", workout.Name, "Invalid workout name in TCX deserialization");

            readNode.InnerXml = workoutTestResult2;
            workout.Deserialize(readNode.FirstChild);
            Assert.AreEqual("WorkoutTest2", workout.Name, "Invalid workout name in TCX deserialization");

            // Category/Sport
            readNode.InnerXml = workoutTestResult3;
            workout.Deserialize(readNode.FirstChild);
            Assert.AreEqual("WorkoutTest3", workout.Name, "Invalid workout name in TCX deserialization");
            Assert.AreEqual(logbook.ActivityCategories[0].SubCategories[5], workout.Category, "Invalid SportTracks category in TCX deserialization");

            readNode.InnerXml = workoutTestResult4;
            workout.Deserialize(readNode.FirstChild);
            Assert.AreEqual("WorkoutTest4", workout.Name, "Invalid workout name in TCX deserialization");
            Assert.AreEqual(logbook.ActivityCategories[0].SubCategories[6], workout.Category, "Invalid SportTracks category in TCX deserialization");

            // Scheduled dates
            readNode.InnerXml = workoutTestResult5;
            workout.Deserialize(readNode.FirstChild);
            Assert.AreEqual("WorkoutTest5", workout.Name, "Invalid workout name in TCX deserialization");
            Assert.AreEqual(1, workout.ScheduledDates.Count, "Invalid number of scheduled dates in TCX deserialization");
            Assert.LessOrEqual(0, (DateTime.Now - workout.ScheduledDates[0]).TotalDays, "Invalid scheduled date in TCX deserialization");

            readNode.InnerXml = workoutTestResult6;
            workout.Deserialize(readNode.FirstChild);
            Assert.AreEqual("WorkoutTest6", workout.Name, "Invalid workout name in TCX deserialization");
            Assert.AreEqual(2, workout.ScheduledDates.Count, "Invalid number of scheduled dates in TCX deserialization");
            Assert.LessOrEqual(0, (DateTime.Now - workout.ScheduledDates[0]).TotalDays, "Invalid scheduled date in TCX deserialization");
            Assert.LessOrEqual(0, (DateTime.Now.AddDays(1) - workout.ScheduledDates[0]).TotalDays, "Invalid future scheduled date in TCX deserialization");

            // New Zealand time (UST+12H) is a case where we had problems because the offset changes the day
            String currentZoneName = Time.CurrentTimeZone.standardName;
            Time.SetTimeZone("New Zealand Standard Time");
            readNode.InnerXml = workoutTestResult7;
            workout.Deserialize(readNode.FirstChild);
            Time.SetTimeZone(currentZoneName);
            Assert.AreEqual("WorkoutTest7", workout.Name, "Invalid workout name in TCX deserialization");
            Assert.AreEqual(1, workout.ScheduledDates.Count, "Invalid number of scheduled dates in TCX deserialization");
            Assert.LessOrEqual(0, (DateTime.Now - workout.ScheduledDates[0]).TotalDays, "Invalid scheduled date in TCX deserialization");

            readNode.InnerXml = workoutTestResult8;
            workout.Deserialize(readNode.FirstChild);
            Assert.AreEqual("WorkoutTest8", workout.Name, "Invalid workout name in TCX deserialization");
            Assert.AreEqual("Notes test 1", workout.Notes, "Invalid workout notes in TCX deserialization");

            readNode.InnerXml = workoutTestResult9;
            workout.Deserialize(readNode.FirstChild);
            Assert.AreEqual("WorkoutTest9", workout.Name, "Invalid workout name in TCX deserialization");
            Assert.AreEqual("Notes test 2", workout.Notes, "Invalid workout notes in TCX deserialization");

            // Erase old value for successive deserializations
            Assert.AreNotEqual(logbook.ActivityCategories[1], workout.Category, "No SportTracks category TCX deserialization setup failure");
            readNode.InnerXml = workoutTestResult10;
            workout.Category = logbook.ActivityCategories[1];
            workout.Deserialize(readNode.FirstChild);
            Assert.AreEqual("WorkoutTest10", workout.Name, "Invalid workout name in TCX deserialization");
            Assert.AreEqual(logbook.ActivityCategories[1], workout.Category, "No SportTracks category TCX deserialization won't request category");
            Assert.IsTrue(String.IsNullOrEmpty(workout.Notes), "TCXDeserialization without notes didn't erase old notes");
        }
        public void TestStepNotesTCXDeserialization()
        {
            Workout deserializedWorkout = new Workout("TestWorkout", PluginMain.GetApplication().Logbook.ActivityCategories[0]);
            XmlDocument testDocument = new XmlDocument();
            RegularStep regularStep;
            RepeatStep repeatStep;

            testDocument.LoadXml(workoutStepNotesExtensionsResult);
            Assert.AreEqual("TrainingCenterDatabase", testDocument.LastChild.Name, "Cannot find database node");
            Assert.AreEqual("Workouts", testDocument.LastChild.FirstChild.Name, "Cannot find workouts node");
            Assert.AreEqual("Workout", testDocument.LastChild.FirstChild.FirstChild.Name, "Cannot find workout node");
            deserializedWorkout.Deserialize(testDocument.LastChild.FirstChild.FirstChild);

            Assert.AreEqual("TestStepNoteExt", deserializedWorkout.Name, "Invalid workout name deserialized");
            Assert.AreEqual(4, deserializedWorkout.StepCount, "Invalid step count deserialized");

            // Regular step
            regularStep = deserializedWorkout.Steps[0] as RegularStep;
            Assert.AreEqual("Test Note1", regularStep.Notes, "Invalid deserialized step note for regular step");

            regularStep = deserializedWorkout.Steps[1] as RegularStep;
            Assert.AreEqual("Test Note2", regularStep.Notes, "Invalid deserialized step note for regular step");

            // Repeat step
            repeatStep = deserializedWorkout.Steps[2] as RepeatStep;
            Assert.AreEqual("Test Repeat Note", repeatStep.Notes, "Invalid deserialized step note for repeat step");

            regularStep = repeatStep.StepsToRepeat[0] as RegularStep;
            Assert.AreEqual("", regularStep.Notes, "Invalid deserialized step note for nested regular step");
        }