Index: Ringtoets/Integration/test/Ringtoets.Integration.Forms.Test/BackgroundMapDataSelectionDialogTest.cs =================================================================== diff -u -r5be1ab23e8cdc4eacc23d0ba5b4f921d6be4a9c1 -r0e50500bb017c821970b286992f14b03dca0e69d --- Ringtoets/Integration/test/Ringtoets.Integration.Forms.Test/BackgroundMapDataSelectionDialogTest.cs (.../BackgroundMapDataSelectionDialogTest.cs) (revision 5be1ab23e8cdc4eacc23d0ba5b4f921d6be4a9c1) +++ Ringtoets/Integration/test/Ringtoets.Integration.Forms.Test/BackgroundMapDataSelectionDialogTest.cs (.../BackgroundMapDataSelectionDialogTest.cs) (revision 0e50500bb017c821970b286992f14b03dca0e69d) @@ -20,18 +20,20 @@ // All rights reserved. using System; -using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Threading; using System.Windows.Forms; using BruTile; +using Core.Common.Controls.DataGrid; using Core.Common.Controls.Dialogs; +using Core.Common.Gui.TestUtil.Settings; using Core.Common.TestUtil; using Core.Components.BruTile.Configurations; using Core.Components.BruTile.TestUtil; using Core.Components.Gis.Data; using Core.Components.Gis.Forms.Views; +using Core.Components.Gis.TestUtil; using NUnit.Extensions.Forms; using NUnit.Framework; using Rhino.Mocks; @@ -64,10 +66,10 @@ { // Setup mockRepository.ReplayAll(); - WmtsMapData mapData = WmtsMapData.CreateDefaultPdokMapData(); + var imageBasedMapData = new TestImageBasedMapData("someMapData", true); // Call - TestDelegate test = () => new BackgroundMapDataSelectionDialog(null, mapData); + TestDelegate test = () => new BackgroundMapDataSelectionDialog(null, imageBasedMapData); // Assert string parameter = Assert.Throws(test).ParamName; @@ -85,7 +87,28 @@ using (var dialog = new BackgroundMapDataSelectionDialog(dialogParent, null)) { // Assert + Assert.IsInstanceOf(dialog); + Assert.AreEqual("Selecteer achtergrondkaart", dialog.Text); Assert.IsNull(dialog.SelectedMapData); + + Icon icon = BitmapToIcon(RingtoetsCommonFormsResources.SelectionDialogIcon); + Bitmap expectedImage = icon.ToBitmap(); + Bitmap actualImage = dialog.Icon.ToBitmap(); + TestHelper.AssertImagesAreEqual(expectedImage, actualImage); + + var mapLayers = (ComboBox) new ComboBoxTester("mapLayerComboBox", dialog).TheObject; + Assert.AreEqual(ComboBoxStyle.DropDownList, mapLayers.DropDownStyle); + Assert.AreEqual("DisplayName", mapLayers.DisplayMember); + Assert.IsTrue(mapLayers.Sorted); + + var backgroundMapDataSelectionControls = (IBackgroundMapDataSelectionControl[]) mapLayers.DataSource; + Assert.AreEqual(2, backgroundMapDataSelectionControls.Length); + Assert.IsInstanceOf(backgroundMapDataSelectionControls[0]); + Assert.IsInstanceOf(backgroundMapDataSelectionControls[1]); + + var groupBoxProperties = (GroupBox) new ControlTester("propertiesGroupBox", dialog).TheObject; + Assert.AreEqual(1, groupBoxProperties.Controls.Count); + Assert.AreSame(backgroundMapDataSelectionControls[0], groupBoxProperties.Controls[0]); } } @@ -94,45 +117,69 @@ { // Setup mockRepository.ReplayAll(); - WmtsMapData mapData = WmtsMapData.CreateDefaultPdokMapData(); + var imageBasedMapData = new TestImageBasedMapData("someMapData", true); // Call - TestDelegate test = () => new BackgroundMapDataSelectionDialog(null, mapData); + TestDelegate test = () => new BackgroundMapDataSelectionDialog(null, imageBasedMapData); // Assert string parameter = Assert.Throws(test).ParamName; Assert.AreEqual("dialogParent", parameter); } [Test] - public void MapDataConstructor_WithParents_DefaultProperties() + public void MapDataConstructor_WithWmtsMapData_DefaultProperties() { // Setup + var dialogParent = mockRepository.Stub(); WmtsMapData mapData = WmtsMapData.CreateDefaultPdokMapData(); tileFactory.Expect(tf => tf.GetWmtsTileSources(mapData.SourceCapabilitiesUrl)).Return(Enumerable.Empty()); mockRepository.ReplayAll(); using (new UseCustomTileSourceFactoryConfig(tileFactory)) - using (var dialogParent = new Form()) { // Call using (var dialog = new BackgroundMapDataSelectionDialog(dialogParent, mapData)) { // Assert - Assert.IsInstanceOf(dialog); - Assert.AreEqual("Selecteer achtergrondkaart", dialog.Text); Assert.AreSame(mapData, dialog.SelectedMapData); + var mapLayers = (ComboBox) new ComboBoxTester("mapLayerComboBox", dialog).TheObject; + Assert.IsInstanceOf(mapLayers.SelectedItem); - Icon icon = BitmapToIcon(RingtoetsCommonFormsResources.SelectionDialogIcon); - Bitmap expectedImage = icon.ToBitmap(); - Bitmap actualImage = dialog.Icon.ToBitmap(); - TestHelper.AssertImagesAreEqual(expectedImage, actualImage); + var groupBoxProperties = (GroupBox) new ControlTester("propertiesGroupBox", dialog).TheObject; + Assert.AreEqual(1, groupBoxProperties.Controls.Count); + Assert.AreSame(mapLayers.SelectedItem, groupBoxProperties.Controls[0]); + } + } + } + [Test] + public void MapDataConstructor_WithWellKnownMapData_DefaultProperties() + { + // Setup + var dialogParent = mockRepository.Stub(); + mockRepository.ReplayAll(); + + var random = new Random(124); + var mapData = new WellKnownTileSourceMapData(random.NextEnumValue()); + + using (new UseCustomTileSourceFactoryConfig(tileFactory)) + { + // Call + using (var dialog = new BackgroundMapDataSelectionDialog(dialogParent, mapData)) + { + // Assert + Assert.AreSame(mapData, dialog.SelectedMapData); var mapLayers = (ComboBox) new ComboBoxTester("mapLayerComboBox", dialog).TheObject; Assert.AreEqual(ComboBoxStyle.DropDownList, mapLayers.DropDownStyle); - Assert.IsInstanceOf>(mapLayers.DataSource); + Assert.IsInstanceOf(mapLayers.DataSource); Assert.AreEqual("DisplayName", mapLayers.DisplayMember); Assert.IsTrue(mapLayers.Sorted); + Assert.IsInstanceOf(mapLayers.SelectedItem); + + var groupBoxProperties = (GroupBox) new ControlTester("propertiesGroupBox", dialog).TheObject; + Assert.AreEqual(1, groupBoxProperties.Controls.Count); + Assert.AreSame(mapLayers.SelectedItem, groupBoxProperties.Controls[0]); } } } @@ -159,14 +206,14 @@ Assert.AreEqual("Type kaartlaag", mapLayerLabel.Text); var mapLayers = (ComboBox) new ComboBoxTester("mapLayerComboBox", dialog).TheObject; - Assert.IsFalse(mapLayers.Enabled); + Assert.IsTrue(mapLayers.Enabled); var groupBoxProperties = new ControlTester("propertiesGroupBox", dialog); Assert.AreEqual("Eigenschappen", groupBoxProperties.Text); Button buttonSelect = (Button) new ButtonTester("selectButton", dialog).TheObject; Assert.AreEqual("Selecteren", buttonSelect.Text); - Assert.IsFalse(buttonSelect.Enabled); + Assert.IsTrue(buttonSelect.Enabled); Button buttonCancel = (Button) new ButtonTester("cancelButton", dialog).TheObject; Assert.AreEqual("Annuleren", buttonCancel.Text); @@ -198,15 +245,135 @@ using (var dialog = new BackgroundMapDataSelectionDialog(dialogParent, null)) { // When - dialog.ShowDialog(); + DialogResult dialogResult = dialog.ShowDialog(); // Then + Assert.AreEqual(DialogResult.Cancel, dialogResult); Assert.IsNull(dialog.SelectedMapData); Assert.AreEqual(dialog.CancelButton, cancelButton); } } [Test] + [Apartment(ApartmentState.STA)] + public void GivenValidDialog_WhenSelectPressed_ThenSelectedMapDataSet() + { + // Given + mockRepository.ReplayAll(); + + DialogBoxHandler = (name, wnd) => + { + using (new FormTester(name)) + { + var button = new ButtonTester("selectButton", name); + button.Click(); + } + }; + + using (var dialogParent = new Form()) + using (var dialog = new BackgroundMapDataSelectionDialog(dialogParent, null)) + { + // When + DialogResult dialogResult = dialog.ShowDialog(); + + // Then + Assert.AreEqual(DialogResult.OK, dialogResult); + Assert.IsNotNull(dialog.SelectedMapData); + } + } + + [Test] + public void GivenValidDialog_WhenControlSwitched_ThenDoesNotListenToEventOfOldControl() + { + // Given + WmtsMapData activeWmtsMapData = WmtsMapData.CreateDefaultPdokMapData(); + + var capabilities = new[] + { + new TestWmtsTileSource(WmtsMapData.CreateAlternativePdokMapData()), + new TestWmtsTileSource(activeWmtsMapData) + }; + + tileFactory.Expect(tf => tf.GetWmtsTileSources(activeWmtsMapData.SourceCapabilitiesUrl)).Return(capabilities); + mockRepository.ReplayAll(); + + var wmtsLocationControlSelectedMapDataChanged = 0; + + using (new UseCustomSettingsHelper(new TestSettingsHelper + { + ApplicationLocalUserSettingsDirectory = TestHelper.GetTestDataPath(TestDataPath.Core.Components.Gis.IO, "noConfig") + })) + using (new UseCustomTileSourceFactoryConfig(tileFactory)) + using (var dialogParent = new Form()) + using (var dialog = new BackgroundMapDataSelectionDialog(dialogParent, activeWmtsMapData)) + { + dialog.Show(); + + var comboBox = (ComboBox) new ComboBoxTester("mapLayerComboBox", dialog).TheObject; + var wmtsLocationControl = (WmtsLocationControl) comboBox.SelectedItem; + var wmtsDataGridViewControl = (DataGridViewControl) new ControlTester("dataGridViewControl", dialog).TheObject; + wmtsLocationControl.SelectedMapDataChanged += (sender, args) => { wmtsLocationControlSelectedMapDataChanged++; }; + + comboBox.SelectedItem = GetComboBoxItem(comboBox); + + // When + wmtsDataGridViewControl.SetCurrentCell(wmtsDataGridViewControl.GetCell(0, 0)); + var button = new ButtonTester("selectButton", dialog); + button.Click(); + + // Then + Assert.IsInstanceOf(dialog.SelectedMapData); + Assert.AreEqual(1, wmtsLocationControlSelectedMapDataChanged); + } + } + + [Test] + public void GivenValidDialog_WhenControlSwitched_ThenListenToEventOfNewControl() + { + // Given + WmtsMapData activeWmtsMapData = WmtsMapData.CreateDefaultPdokMapData(); + + var capabilities = new[] + { + new TestWmtsTileSource(WmtsMapData.CreateAlternativePdokMapData()), + new TestWmtsTileSource(activeWmtsMapData) + }; + + tileFactory.Expect(tf => tf.GetWmtsTileSources(activeWmtsMapData.SourceCapabilitiesUrl)).Return(capabilities); + mockRepository.ReplayAll(); + + var wellKnownSelectedMapDataChanged = 0; + + using (new UseCustomSettingsHelper(new TestSettingsHelper + { + ApplicationLocalUserSettingsDirectory = TestHelper.GetTestDataPath(TestDataPath.Core.Components.Gis.IO, "noConfig") + })) + using (new UseCustomTileSourceFactoryConfig(tileFactory)) + using (var dialogParent = new Form()) + using (var dialog = new BackgroundMapDataSelectionDialog(dialogParent, activeWmtsMapData)) + { + dialog.Show(); + + var comboBox = (ComboBox) new ComboBoxTester("mapLayerComboBox", dialog).TheObject; + var wellKnownControl = GetComboBoxItem(comboBox); + + comboBox.SelectedItem = wellKnownControl; + + var wellKnownDataGridViewControl = (DataGridViewControl) new ControlTester("dataGridViewControl", dialog).TheObject; + wellKnownControl.SelectedMapDataChanged += (sender, args) => { wellKnownSelectedMapDataChanged++; }; + + // When + wellKnownDataGridViewControl.SetCurrentCell(wellKnownDataGridViewControl.GetCell(4, 0)); + var button = new ButtonTester("selectButton", dialog); + button.Click(); + + // Then + Assert.IsInstanceOf(dialog.SelectedMapData); + Assert.AreEqual(1, wellKnownSelectedMapDataChanged); + } + } + + [Test] public void Dispose_DisposedAlreadyCalled_DoesNotThrowException() { // Setup @@ -226,6 +393,11 @@ Assert.DoesNotThrow(call); } + private static T GetComboBoxItem(ComboBox comboBox) + { + return ((IBackgroundMapDataSelectionControl[]) comboBox.DataSource).OfType().First(); + } + private static Icon BitmapToIcon(Bitmap icon) { return Icon.FromHandle(icon.GetHicon());