public void Configuration_ReceiverLocation_Returns_ReceiverLocation_From_ReceiverLocations()
        {
            var home = new ReceiverLocation() { UniqueId = 1, Name = "HOME" };
            var away = new ReceiverLocation() { UniqueId = 2, Name = "AWAY" };
            _Implementation.ReceiverLocations.AddRange(new ReceiverLocation[] { home, away });

            var location = _Implementation.ReceiverLocation(2);
            Assert.AreSame(away, location);
        }
        public void Configuration_ReceiverLocation_Returns_Null_If_Receiver_Cannot_Be_Found()
        {
            var home = new ReceiverLocation() { UniqueId = 1, Name = "HOME" };
            var away = new ReceiverLocation() { UniqueId = 2, Name = "AWAY" };
            _Implementation.ReceiverLocations.AddRange(new ReceiverLocation[] { home, away });

            var location = _Implementation.ReceiverLocation(3);
            Assert.IsNull(location);
        }
        public void ReceiverLocation_Initialises_To_Known_State_And_Properties_Work()
        {
            var location = new ReceiverLocation();

            TestUtilities.TestProperty(location, r => r.IsBaseStationLocation, false);
            TestUtilities.TestProperty(location, r => r.Latitude, 0.0, 123.567);
            TestUtilities.TestProperty(location, r => r.Longitude, 0.0, -123.456);
            TestUtilities.TestProperty(location, r => r.Name, null, "Home");
            TestUtilities.TestProperty(location, r => r.UniqueId, 0, 123);
        }
        public void ReceiverLocation_Clone_Creates_Copy()
        {
            var original = new ReceiverLocation() {
                IsBaseStationLocation = true,
                Latitude = 1.0,
                Longitude = 2.0,
                Name = "A",
                UniqueId = 1,
            };

            var copy = (ReceiverLocation)original.Clone();
            Assert.AreNotSame(original, copy);

            foreach(var property in typeof(ReceiverLocation).GetProperties()) {
                switch(property.Name) {
                    case "IsBaseStationLocation":   Assert.AreEqual(true, copy.IsBaseStationLocation); break;
                    case "Latitude":                Assert.AreEqual(1.0, copy.Latitude); break;
                    case "Longitude":               Assert.AreEqual(2.0, copy.Longitude); break;
                    case "Name":                    Assert.AreEqual("A", copy.Name); break;
                    case "UniqueId":                Assert.AreEqual(1, copy.UniqueId); break;
                    default:                        throw new NotImplementedException();
                }
            }
        }
        /// <summary>
        /// Fills the locations list view.
        /// </summary>
        private void PopulateLocations()
        {
            var currentSuppressState = _SuppressItemSelectedEventHandler;

            try {
                _SuppressItemSelectedEventHandler = true;
                var selected = SelectedReceiverLocation;

                listViewReceiverLocations.Items.Clear();
                foreach(var location in ReceiverLocations) {
                    var item = new ListViewItem() { Tag = location };
                    PopulateListViewItem(item);
                    listViewReceiverLocations.Items.Add(item);
                }

                if(ReceiverLocations.Contains(selected)) SelectedReceiverLocation = selected;
            } finally {
                _SuppressItemSelectedEventHandler = currentSuppressState;
            }
        }
        /// <summary>
        /// Called after the view has been created but before the user sees anything.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            Localise.Form(this);
            PopulateLocations();

            _Presenter = Factory.Singleton.Resolve<IReceiverLocationsPresenter>();
            _Presenter.Initialise(this);

            _OnlineHelp = new OnlineHelpHelper(this, OnlineHelpAddress.WinFormsReceiverLocationsView);

            SelectedReceiverLocation = _SelectedReceiverLocation;
        }
        public void OptionsPresenter_ResetToDefaultsClicked_Does_Not_Delete_ReceiverLocations()
        {
            // Personally I would be more than a little upset to enter half a dozen locations and then find that the button
            // had wiped them all out! :) We should preserve the receiver locations when the user clicks reset to default.
            var line1 = new ReceiverLocation() { UniqueId = 1, Name = "A", Latitude = 1.2, Longitude = 3.4 };
            var line2 = new ReceiverLocation() { UniqueId = 2, Name = "B", Latitude = 5.6, Longitude = 7.8 };
            _Configuration.ReceiverLocations.AddRange(new ReceiverLocation[] { line1, line2 });

            _Presenter.Initialise(_View.Object);
            _View.Raise(v => v.ResetToDefaultsClicked += null, EventArgs.Empty);

            Assert.AreEqual(2, _View.Object.RawDecodingReceiverLocations.Count);
            Assert.IsTrue(_View.Object.RawDecodingReceiverLocations.Contains(line1));
            Assert.IsTrue(_View.Object.RawDecodingReceiverLocations.Contains(line2));
        }
        public void OptionsPresenter_SaveClicked_Copies_ReceiverLocations_From_BaseStation_Options_UI_To_Configuration_Before_Save()
        {
            _Configuration.ReceiverLocations.Add(new ReceiverLocation() { UniqueId = 3, Name = "Old garbage, should be cleared", Latitude = 1, Longitude = 2 });
            _Presenter.Initialise(_View.Object);

            var line1 = new ReceiverLocation() { UniqueId = 1, Name = "A", Latitude = 1.2, Longitude = 3.4 };
            var line2 = new ReceiverLocation() { UniqueId = 2, Name = "B", Latitude = 5.6, Longitude = 7.8 };
            _View.Object.RawDecodingReceiverLocations.Clear();
            _View.Object.RawDecodingReceiverLocations.AddRange(new ReceiverLocation[] { line1, line2 });

            _ConfigurationStorage.Setup(c => c.Save(_Configuration)).Callback(() => {
                Assert.AreEqual(2, _Configuration.ReceiverLocations.Count);
                Assert.IsTrue(_Configuration.ReceiverLocations.Contains(line1));
                Assert.IsTrue(_Configuration.ReceiverLocations.Contains(line2));
            });

            _View.Raise(v => v.SaveClicked += null, EventArgs.Empty);

            _ConfigurationStorage.Verify(c => c.Save(_Configuration), Times.Once());
        }
        public void OptionsPresenter_Initialise_Copies_ReceiverLocations_From_Configuration_To_BaseStation_Options_UI()
        {
            var line1 = new ReceiverLocation() { UniqueId = 1, Name = "A", Latitude = 1.2, Longitude = 3.4 };
            var line2 = new ReceiverLocation() { UniqueId = 2, Name = "B", Latitude = 5.6, Longitude = 7.8 };
            _Configuration.ReceiverLocations.AddRange(new ReceiverLocation[] { line1, line2 });

            _Presenter.Initialise(_View.Object);

            Assert.AreEqual(2, _View.Object.RawDecodingReceiverLocations.Count);
            Assert.IsTrue(_View.Object.RawDecodingReceiverLocations.Contains(line1));
            Assert.IsTrue(_View.Object.RawDecodingReceiverLocations.Contains(line2));
        }
 private ReceiverLocation SetupSelectedLocation(ReceiverLocation location)
 {
     _View.Setup(v => v.SelectedReceiverLocation).Returns(location);
     return location;
 }
        private void CheckValidation_Displays_Validation_Message_When_Name_Duplicates_Existing_Name(Action trigger)
        {
            _Presenter.Initialise(_View.Object);

            var line1 = new ReceiverLocation() { UniqueId = 1, Name = "ABC" };
            var line2 = new ReceiverLocation() { UniqueId = 2, Name = "XYZ" };
            _View.Object.ReceiverLocations.AddRange(new ReceiverLocation[] { line1, line2 });
            SetupSelectedLocation(line2);
            _View.Raise(v => v.SelectedLocationChanged += null, EventArgs.Empty);

            SetupExpectedValidationFields(new ValidationResult[] { new ValidationResult(ValidationField.Location, Strings.PleaseEnterUniqueNameForLocation) });

            _View.Object.Location = "ABC";
            _View.Object.Latitude = (0.01).ToString();
            trigger();

            _View.Verify(v => v.ShowValidationResults(It.IsAny<IEnumerable<ValidationResult>>()), Times.Exactly(2));
            _View.Verify(v => v.RefreshSelectedLocation(), Times.Never());
            Assert.AreEqual("XYZ", line2.Name);
        }
        private void CheckValidation_Updates_Selected_Line_With_Name(Action trigger)
        {
            _Presenter.Initialise(_View.Object);

            var line1 = new ReceiverLocation() { UniqueId = 1, Name = "ABC", Latitude = 1.0, Longitude = 2.0 };
            var line2 = new ReceiverLocation() { UniqueId = 2, Name = "XYZ", Latitude = 1.0, Longitude = 2.0 };
            _View.Object.ReceiverLocations.AddRange(new ReceiverLocation[] { line1, line2 });
            SetupSelectedLocation(line2);
            _View.Raise(v => v.SelectedLocationChanged += null, EventArgs.Empty);

            SetupExpectedValidationFields(new ValidationResult[] { });

            _View.Object.Location = "New name";
            trigger();

            _View.Verify(v => v.ShowValidationResults(It.IsAny<IEnumerable<ValidationResult>>()), Times.Exactly(2));
            Assert.AreEqual("New name", line2.Name);
            _View.Verify(v => v.RefreshSelectedLocation(), Times.Once());
        }
        private void CheckValidation_Updates_Selected_Line_With_New_Longitude(Action trigger)
        {
            foreach(var longitude in new double[] { -180.0, -179.9999, -178, -1, 0, 1, 178, 179.9999, 180.0 }) {
                TestCleanup();
                TestInitialise();

                _Presenter.Initialise(_View.Object);

                var line1 = new ReceiverLocation() { UniqueId = 1, Name = "ABC", Latitude = 1.0, Longitude = 2.0 };
                var line2 = new ReceiverLocation() { UniqueId = 2, Name = "XYZ", Latitude = 1.0, Longitude = 2.0 };
                _View.Object.ReceiverLocations.AddRange(new ReceiverLocation[] { line1, line2 });
                SetupSelectedLocation(line2);
                _View.Raise(v => v.SelectedLocationChanged += null, EventArgs.Empty);

                SetupExpectedValidationFields(new ValidationResult[] { });

                _View.Object.Longitude = longitude.ToString();
                trigger();

                _View.Verify(v => v.ShowValidationResults(It.IsAny<IEnumerable<ValidationResult>>()), Times.Exactly(2));
                Assert.AreEqual(longitude, line2.Longitude);
                _View.Verify(v => v.RefreshSelectedLocation(), Times.Once());
            }
        }
        private void CheckValidation_Displays_Validation_Message_When_Longitude_Is_Out_Of_Range(Action trigger)
        {
            foreach(var badLongitude in new double[] { -182.0, -181.0, -180.00001, 180.00001, 181.0, 182.0 }) {
                TestCleanup();
                TestInitialise();

                _Presenter.Initialise(_View.Object);

                var line1 = new ReceiverLocation() { UniqueId = 1, Name = "ABC", Latitude = 1.0, Longitude = 2.0 };
                var line2 = new ReceiverLocation() { UniqueId = 2, Name = "XYZ", Latitude = 1.0, Longitude = 2.0 };
                _View.Object.ReceiverLocations.AddRange(new ReceiverLocation[] { line1, line2 });
                SetupSelectedLocation(line2);
                _View.Raise(v => v.SelectedLocationChanged += null, EventArgs.Empty);

                SetupExpectedValidationFields(new ValidationResult[] { new ValidationResult(ValidationField.Longitude, Strings.LongitudeOutOfBounds) });

                _View.Object.Longitude = badLongitude.ToString();
                trigger();

                _View.Verify(v => v.ShowValidationResults(It.IsAny<IEnumerable<ValidationResult>>()), Times.Exactly(2));
                _View.Verify(v => v.RefreshSelectedLocation(), Times.Never());
                Assert.AreEqual(2.0, line2.Longitude);
            }
        }
        private void CheckValidation_Displays_Validation_Message_When_Latitude_Is_Zero(Action trigger)
        {
            _Presenter.Initialise(_View.Object);

            var line1 = new ReceiverLocation() { UniqueId = 1, Name = "ABC", Latitude = 1.0, Longitude = 2.0 };
            var line2 = new ReceiverLocation() { UniqueId = 2, Name = "XYZ", Latitude = 1.0, Longitude = 2.0 };
            _View.Object.ReceiverLocations.AddRange(new ReceiverLocation[] { line1, line2 });
            SetupSelectedLocation(line2);
            _View.Raise(v => v.SelectedLocationChanged += null, EventArgs.Empty);

            SetupExpectedValidationFields(new ValidationResult[] { new ValidationResult(ValidationField.Latitude, Strings.LatitudeCannotBeZero) });

            _View.Object.Latitude = (0.0).ToString("N1");
            trigger();

            _View.Verify(v => v.ShowValidationResults(It.IsAny<IEnumerable<ValidationResult>>()), Times.Exactly(2));
            _View.Verify(v => v.RefreshSelectedLocation(), Times.Never());
            Assert.AreEqual(1.0, line2.Latitude);
        }
        private void CheckValidation_Does_Not_Display_Validation_Message_When_Name_Duplicates_Own_Name(Action trigger)
        {
            _Presenter.Initialise(_View.Object);

            var line1 = new ReceiverLocation() { UniqueId = 1, Name = "ABC" };
            var line2 = new ReceiverLocation() { UniqueId = 2, Name = "XYZ" };
            _View.Object.ReceiverLocations.AddRange(new ReceiverLocation[] { line1, line2 });
            SetupSelectedLocation(line2);
            _View.Raise(v => v.SelectedLocationChanged += null, EventArgs.Empty);

            SetupExpectedValidationFields(new ValidationResult[] { });

            _View.Object.Location = "XYZ";
            _View.Object.Latitude = (0.01).ToString();
            trigger();

            _View.Verify(v => v.ShowValidationResults(It.IsAny<IEnumerable<ValidationResult>>()), Times.Exactly(2));
        }
        /// <summary>
        /// Raised when the user asks for a new location to be created.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void View_NewLocationClicked(object sender, EventArgs args)
        {
            var location = new ReceiverLocation() {
                Name = SelectUniqueName("New Location"),
                UniqueId = _NextUniqueId++,
            };

            _View.ReceiverLocations.Add(location);
            _View.RefreshLocations();
            _View.SelectedReceiverLocation = location;

            CopySelectedLocationToFields();
            _View.FocusOnEditFields();
        }
 private ReceiverLocation SetupSelectedLocation()
 {
     var result = new ReceiverLocation() { UniqueId = 1, Name = "SELECTED", Latitude = 1, Longitude = 2 };
     return SetupSelectedLocation(result);
 }
        private void CheckValidation_Does_Nothing_If_No_Location_Is_Selected(Action trigger)
        {
            _Presenter.Initialise(_View.Object);

            var line1 = new ReceiverLocation() { UniqueId = 1, Name = "ABC", Latitude = 1.0, Longitude = 2.0 };
            var line2 = new ReceiverLocation() { UniqueId = 2, Name = "XYZ", Latitude = 1.0, Longitude = 2.0 };
            _View.Object.ReceiverLocations.AddRange(new ReceiverLocation[] { line1, line2 });
            SetupSelectedLocation(null);
            _View.Raise(v => v.SelectedLocationChanged += null, EventArgs.Empty);

            _View.Object.Location = "New name";
            _View.Object.Latitude = (4.0).ToString();
            _View.Object.Longitude = (5.0).ToString();

            trigger();

            _View.Verify(v => v.RefreshSelectedLocation(), Times.Never());
        }
        /// <summary>
        /// Raised when the user wants to copy locations from the BaseStation database.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void View_UpdateFromBaseStationDatabaseClicked(object sender, EventArgs args)
        {
            var entriesPreviouslyCopiedFromBaseStationDatabase = _View.ReceiverLocations.Where(r => r.IsBaseStationLocation).ToList();
            _View.ReceiverLocations.RemoveAll(r => r.IsBaseStationLocation);

            var database = Factory.Singleton.Resolve<IAutoConfigBaseStationDatabase>().Singleton.Database;
            foreach(var location in database.GetLocations()) {
                var previousEntry = entriesPreviouslyCopiedFromBaseStationDatabase.Where(r => r.Name == location.LocationName).FirstOrDefault();
                var newLocation = new ReceiverLocation() {
                    Name = SelectUniqueName(location.LocationName),
                    UniqueId = previousEntry == null ? _NextUniqueId++ : previousEntry.UniqueId,
                    Latitude = location.Latitude,
                    Longitude = location.Longitude,
                    IsBaseStationLocation = true,
                };
                _View.ReceiverLocations.Add(newLocation);
            }

            _View.RefreshLocations();
        }