Index: Core/Components/src/Core.Components.DotSpatial.Forms/Core.Components.DotSpatial.Forms.csproj =================================================================== diff -u -r5b2528a7b0f0443681e0fbeba5793d2aa00c98c0 -r6f806998a17fa46d14678596d71c9c935408b948 --- Core/Components/src/Core.Components.DotSpatial.Forms/Core.Components.DotSpatial.Forms.csproj (.../Core.Components.DotSpatial.Forms.csproj) (revision 5b2528a7b0f0443681e0fbeba5793d2aa00c98c0) +++ Core/Components/src/Core.Components.DotSpatial.Forms/Core.Components.DotSpatial.Forms.csproj (.../Core.Components.DotSpatial.Forms.csproj) (revision 6f806998a17fa46d14678596d71c9c935408b948) @@ -85,6 +85,7 @@ True Resources.resx + Index: Core/Components/src/Core.Components.DotSpatial.Forms/WellKnownBackgroundLayerStatus.cs =================================================================== diff -u --- Core/Components/src/Core.Components.DotSpatial.Forms/WellKnownBackgroundLayerStatus.cs (revision 0) +++ Core/Components/src/Core.Components.DotSpatial.Forms/WellKnownBackgroundLayerStatus.cs (revision 6f806998a17fa46d14678596d71c9c935408b948) @@ -0,0 +1,105 @@ +// Copyright (C) Stichting Deltares 2016. All rights reserved. +// +// This file is part of Ringtoets. +// +// Ringtoets is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this program. If not, see . +// +// All names, logos, and references to "Deltares" are registered trademarks of +// Stichting Deltares and remain full property of Stichting Deltares at all times. +// All rights reserved. + +using System; +using Core.Components.DotSpatial.Layer.BruTile; +using Core.Components.Gis.Data; + +namespace Core.Components.DotSpatial.Forms +{ + /// + /// Class responsible for keeping track of various status information related to the + /// used to create a background layer in a map view. + /// + internal class WellKnownBackgroundLayerStatus : IBackgroundLayerStatus + { + public bool PreviousBackgroundLayerCreationFailed { get; private set; } + + public BruTileLayer BackgroundLayer { get; private set; } + + public virtual void Dispose() + { + BackgroundLayer?.Dispose(); + } + + public void SuccessfullyInitializedLayer(BruTileLayer backgroundLayer, ImageBasedMapData dataSource) + { + if (backgroundLayer == null) + { + throw new ArgumentNullException(nameof(backgroundLayer)); + } + if (dataSource == null) + { + throw new ArgumentNullException(nameof(dataSource)); + } + var wellKnownTileSourceMapData = dataSource as WellKnownTileSourceMapData; + if (wellKnownTileSourceMapData == null) + { + PreviousBackgroundLayerCreationFailed = true; + return; + } + + WellKnownTileSource = wellKnownTileSourceMapData.TileSource; + + BackgroundLayer = backgroundLayer; + PreviousBackgroundLayerCreationFailed = false; + } + + public void LayerInitializationFailed() + { + ClearConfiguration(); + PreviousBackgroundLayerCreationFailed = true; + } + + public void ClearConfiguration(bool expectRecreationOfSameBackgroundLayer = false) + { + WellKnownTileSource = null; + + if (BackgroundLayer != null) + { + BackgroundLayer.Dispose(); + BackgroundLayer = null; + } + + if (!expectRecreationOfSameBackgroundLayer) + { + PreviousBackgroundLayerCreationFailed = false; + } + } + + public bool HasSameConfiguration(ImageBasedMapData mapData) + { + if (mapData == null) + { + throw new ArgumentNullException(nameof(mapData)); + } + + var wellKnownTileSourceMapData = mapData as WellKnownTileSourceMapData; + if (wellKnownTileSourceMapData == null) + { + return false; + } + return Equals(wellKnownTileSourceMapData.TileSource, WellKnownTileSource); + } + + private WellKnownTileSource? WellKnownTileSource { get; set; } + } +} \ No newline at end of file Index: Core/Components/test/Core.Components.DotSpatial.Forms.Test/Core.Components.DotSpatial.Forms.Test.csproj =================================================================== diff -u -r99f9004206bfb9de084275d749b7aeccafd6da18 -r6f806998a17fa46d14678596d71c9c935408b948 --- Core/Components/test/Core.Components.DotSpatial.Forms.Test/Core.Components.DotSpatial.Forms.Test.csproj (.../Core.Components.DotSpatial.Forms.Test.csproj) (revision 99f9004206bfb9de084275d749b7aeccafd6da18) +++ Core/Components/test/Core.Components.DotSpatial.Forms.Test/Core.Components.DotSpatial.Forms.Test.csproj (.../Core.Components.DotSpatial.Forms.Test.csproj) (revision 6f806998a17fa46d14678596d71c9c935408b948) @@ -82,6 +82,7 @@ + Index: Core/Components/test/Core.Components.DotSpatial.Forms.Test/WellKnownBackgroundLayerStatusTest.cs =================================================================== diff -u --- Core/Components/test/Core.Components.DotSpatial.Forms.Test/WellKnownBackgroundLayerStatusTest.cs (revision 0) +++ Core/Components/test/Core.Components.DotSpatial.Forms.Test/WellKnownBackgroundLayerStatusTest.cs (revision 6f806998a17fa46d14678596d71c9c935408b948) @@ -0,0 +1,298 @@ +// Copyright (C) Stichting Deltares 2016. All rights reserved. +// +// This file is part of Ringtoets. +// +// Ringtoets is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this program. If not, see . +// +// All names, logos, and references to "Deltares" are registered trademarks of +// Stichting Deltares and remain full property of Stichting Deltares at all times. +// All rights reserved. + +using System; +using BruTile; +using Core.Components.BruTile.Configurations; +using Core.Components.BruTile.IO; +using Core.Components.DotSpatial.Layer.BruTile; +using Core.Components.Gis.Data; +using DotSpatial.Symbology; +using NUnit.Framework; +using Rhino.Mocks; + +namespace Core.Components.DotSpatial.Forms.Test +{ + [TestFixture] + public class WellKnownBackgroundLayerStatusTest + { + [Test] + public void Constructor_ExpectedValues() + { + // Call + using (var layerStatus = new WellKnownBackgroundLayerStatus()) + { + // Assert + Assert.IsInstanceOf(layerStatus); + Assert.IsInstanceOf(layerStatus); + + Assert.IsNull(layerStatus.BackgroundLayer); + Assert.IsFalse(layerStatus.PreviousBackgroundLayerCreationFailed); + } + } + + [Test] + public void HasSameConfiguration_MapDataNull_ThrownArgumentNullException() + { + // Setup + using (var layerStatus = new WellKnownBackgroundLayerStatus()) + { + // Call + TestDelegate call = () => layerStatus.HasSameConfiguration(null); + + // Assert + string paramName = Assert.Throws(call).ParamName; + Assert.AreEqual("mapData", paramName); + } + } + + [Test] + public void HasSameConfiguration_MapDataNotWellKnownMapData_ReturnFalse() + { + // Setup + using (var layerStatus = new WellKnownBackgroundLayerStatus()) + { + var mapData = new SimpleImageBasedMapData(); + + // Call + bool isSame = layerStatus.HasSameConfiguration(mapData); + + // Assert + Assert.IsFalse(isSame); + } + } + + [Test] + public void HasSameConfiguration_ForInitializedLayer_ReturnTrue() + { + // Setup + var mocks = new MockRepository(); + var tileFetcher = mocks.Stub(); + using (var layerStatus = new WellKnownBackgroundLayerStatus()) + { + IConfiguration configuration = CreateStubConfiguration(mocks, tileFetcher); + mocks.ReplayAll(); + + using (var layer = new BruTileLayer(configuration)) + { + var source = new Random(789).NextEnum(); + var mapData1 = new WellKnownTileSourceMapData(source); + var mapData2 = new WellKnownTileSourceMapData(source); + + layerStatus.SuccessfullyInitializedLayer(layer, mapData1); + + // Call + bool isSame = layerStatus.HasSameConfiguration(mapData2); + + // Assert + Assert.IsTrue(isSame, "Should recognize same configuration even if instance is not the same."); + } + } + mocks.VerifyAll(); + } + + [Test] + public void SuccessfullyInitializedLayer_LayerNull_ThrownArgumentNullException() + { + // Setup + using (var layerStatus = new WellKnownBackgroundLayerStatus()) + { + var dataSource = new WellKnownTileSourceMapData(new Random(789).NextEnum()); + + // Call + TestDelegate call = () => layerStatus.SuccessfullyInitializedLayer(null, dataSource); + + // Assert + string paramName = Assert.Throws(call).ParamName; + Assert.AreEqual("backgroundLayer", paramName); + } + } + + [Test] + public void SuccessfullyInitializedLayer_MapDataNull_ThrownArgumentNullException() + { + // Setup + var mocks = new MockRepository(); + var tileFetcher = mocks.Stub(); + IConfiguration configuration = CreateStubConfiguration(mocks, tileFetcher); + mocks.ReplayAll(); + + using (var layer = new BruTileLayer(configuration)) + using (var layerStatus = new WellKnownBackgroundLayerStatus()) + { + // Call + TestDelegate call = () => layerStatus.SuccessfullyInitializedLayer(layer, null); + + // Assert + string paramName = Assert.Throws(call).ParamName; + Assert.AreEqual("dataSource", paramName); + } + } + + [Test] + public void SuccessfullyInitializedLayer_MapDataNotWellKnownMapData_SetCreationFailedTrue() + { + // Setup + var mocks = new MockRepository(); + var tileFetcher = mocks.Stub(); + IConfiguration configuration = CreateStubConfiguration(mocks, tileFetcher); + mocks.ReplayAll(); + + using (var layer = new BruTileLayer(configuration)) + using (var layerStatus = new WellKnownBackgroundLayerStatus()) + { + var mapData = new SimpleImageBasedMapData(); + + // Call + layerStatus.SuccessfullyInitializedLayer(layer, mapData); + + // Assert + Assert.IsTrue(layerStatus.PreviousBackgroundLayerCreationFailed); + } + } + + [Test] + public void SuccessfullyInitializedLayer_InitializationPreviouslyFailed_PreviousBackgroundLayerCreationFailedFalse() + { + // Setup + var mocks = new MockRepository(); + var tileFetcher = mocks.Stub(); + IConfiguration configuration = CreateStubConfiguration(mocks, tileFetcher); + mocks.ReplayAll(); + + using (var layer = new BruTileLayer(configuration)) + using (var layerStatus = new WellKnownBackgroundLayerStatus()) + { + layerStatus.LayerInitializationFailed(); + + var mapData = new WellKnownTileSourceMapData(new Random(789).NextEnum()); + + // Call + layerStatus.SuccessfullyInitializedLayer(layer, mapData); + + // Assert + Assert.IsFalse(layerStatus.PreviousBackgroundLayerCreationFailed); + } + } + + [Test] + public void LayerInitializationFailed_PreviousBackgroundLayerCreationFailedTrue() + { + // Setup + using (var layerStatus = new WellKnownBackgroundLayerStatus()) + { + // Call + layerStatus.LayerInitializationFailed(); + + // Assert + Assert.IsTrue(layerStatus.PreviousBackgroundLayerCreationFailed); + } + } + + [Test] + public void LayerInitializationFailed_HasLayer_ConfigurationCleared() + { + // Setup + var mocks = new MockRepository(); + var tileFetcher = mocks.Stub(); + IConfiguration configuration = CreateStubConfiguration(mocks, tileFetcher); + mocks.ReplayAll(); + + using (var layer = new BruTileLayer(configuration)) + using (var layerStatus = new WellKnownBackgroundLayerStatus()) + { + var mapData = new WellKnownTileSourceMapData(new Random(789).NextEnum()); + layerStatus.SuccessfullyInitializedLayer(layer, mapData); + + // Call + layerStatus.LayerInitializationFailed(); + + // Assert + Assert.IsTrue(layerStatus.PreviousBackgroundLayerCreationFailed); + Assert.IsFalse(layerStatus.HasSameConfiguration(mapData)); + } + } + + [Test] + public void ClearConfiguration_HasLayer_ConfigurationCleared() + { + // Setup + var mocks = new MockRepository(); + var tileFetcher = mocks.Stub(); + IConfiguration configuration = CreateStubConfiguration(mocks, tileFetcher); + mocks.ReplayAll(); + + using (var layer = new BruTileLayer(configuration)) + using (var layerStatus = new WellKnownBackgroundLayerStatus()) + { + var mapData = new WellKnownTileSourceMapData(new Random(789).NextEnum()); + layerStatus.SuccessfullyInitializedLayer(layer, mapData); + + // Call + layerStatus.ClearConfiguration(); + + // Assert + Assert.IsFalse(layerStatus.PreviousBackgroundLayerCreationFailed); + Assert.IsFalse(layerStatus.HasSameConfiguration(mapData)); + } + } + + [Test] + public void ClearConfiguration_WithFailedLayerInitializationAndExpectingRecreation_ConfigurationClearedButKeepFailedFlagSet() + { + // Setup + using (var layerStatus = new WellKnownBackgroundLayerStatus()) + { + var mapData = new WellKnownTileSourceMapData(new Random(465).NextEnum()); + layerStatus.LayerInitializationFailed(); + + // Call + layerStatus.ClearConfiguration(true); + + // Assert + Assert.IsTrue(layerStatus.PreviousBackgroundLayerCreationFailed); + Assert.IsFalse(layerStatus.HasSameConfiguration(mapData)); + } + } + + private static IConfiguration CreateStubConfiguration(MockRepository mocks, ITileFetcher tileFetcher) + { + var schema = mocks.Stub(); + schema.Stub(s => s.Srs).Return("EPSG:28992"); + schema.Stub(s => s.Extent).Return(new Extent()); + + var tileSource = mocks.Stub(); + tileSource.Stub(ts => ts.Schema).Return(schema); + + var configuration = mocks.Stub(); + configuration.Stub(c => c.Initialized).Return(true); + configuration.Stub(c => c.TileSource).Return(tileSource); + configuration.Stub(c => c.TileFetcher).Return(tileFetcher); + configuration.Stub(c => c.Dispose()); + return configuration; + } + + private class SimpleImageBasedMapData : ImageBasedMapData + { + public SimpleImageBasedMapData() : base("SimpleImageBasedMapData") {} + } + } +} \ No newline at end of file Index: Core/Components/test/Core.Components.DotSpatial.Forms.Test/WmtsBackgroundLayerStatusTest.cs =================================================================== diff -u -r5b2528a7b0f0443681e0fbeba5793d2aa00c98c0 -r6f806998a17fa46d14678596d71c9c935408b948 --- Core/Components/test/Core.Components.DotSpatial.Forms.Test/WmtsBackgroundLayerStatusTest.cs (.../WmtsBackgroundLayerStatusTest.cs) (revision 5b2528a7b0f0443681e0fbeba5793d2aa00c98c0) +++ Core/Components/test/Core.Components.DotSpatial.Forms.Test/WmtsBackgroundLayerStatusTest.cs (.../WmtsBackgroundLayerStatusTest.cs) (revision 6f806998a17fa46d14678596d71c9c935408b948) @@ -214,7 +214,7 @@ } [Test] - public void SuccessfullyInitializedLayer_MapDataNotWmtsMapData_ReturnsTrue() + public void SuccessfullyInitializedLayer_MapDataNotWmtsMapData_SetCreationFailedTrue() { // Setup var mocks = new MockRepository();