// 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 System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Core.Common.Base;
using Core.Common.Controls.TreeView;
using Core.Common.Gui.Commands;
using Core.Common.Gui.ContextMenu;
using Core.Common.TestUtil;
using Core.Common.Utils.Reflection;
using Core.Components.Gis.Data;
using Core.Plugins.Map.Legend;
using Core.Plugins.Map.Properties;
using NUnit.Extensions.Forms;
using NUnit.Framework;
using Rhino.Mocks;
using GuiResources = Core.Common.Gui.Properties.Resources;
namespace Core.Plugins.Map.Test.Legend
{
[TestFixture]
public class MapDataCollectionTreeNodeInfoTest : NUnitFormTest
{
private MockRepository mocks;
private MapLegendView mapLegendView;
private TreeNodeInfo info;
private IContextMenuBuilderProvider contextMenuBuilderProvider;
[SetUp]
public void SetUp()
{
mocks = new MockRepository();
contextMenuBuilderProvider = mocks.Stub();
mapLegendView = new MapLegendView(contextMenuBuilderProvider);
TreeViewControl treeViewControl = TypeUtils.GetField(mapLegendView, "treeViewControl");
Dictionary treeNodeInfoLookup = TypeUtils.GetField>(treeViewControl, "tagTypeTreeNodeInfoLookup");
info = treeNodeInfoLookup[typeof(MapDataCollection)];
}
[Test]
public void Initialized_Always_ExpectedPropertiesSet()
{
// Setup
mocks.ReplayAll();
// Assert
Assert.AreEqual(typeof(MapDataCollection), info.TagType);
Assert.IsNull(info.ForeColor);
Assert.IsNull(info.EnsureVisibleOnCreate);
Assert.IsNull(info.CanRename);
Assert.IsNull(info.OnNodeRenamed);
Assert.IsNull(info.CanRemove);
Assert.IsNull(info.OnNodeRemoved);
Assert.IsNull(info.CanCheck);
Assert.IsNull(info.IsChecked);
Assert.IsNull(info.OnNodeChecked);
Assert.IsNull(info.CanDrag);
}
[Test]
public void Text_Always_ReturnsNameFromMapData()
{
// Setup
mocks.ReplayAll();
var mapDataCollection = new MapDataCollection("Collectie");
// Call
var text = info.Text(mapDataCollection);
// Assert
Assert.AreEqual(mapDataCollection.Name, text);
}
[Test]
public void Image_Always_ReturnsImageFromResource()
{
// Setup
mocks.ReplayAll();
// Call
var image = info.Image(null);
// Assert
TestHelper.AssertImagesAreEqual(GuiResources.folder, image);
}
[Test]
public void ChildNodeObjects_Always_ReturnsChildrenOfDataReversed()
{
// Setup
var mapData1 = mocks.StrictMock("test data");
var mapData2 = mocks.StrictMock("test data");
var mapData3 = mocks.StrictMock("test data");
var mapDataCollection = new MapDataCollection("test data");
mapDataCollection.Add(mapData1);
mapDataCollection.Add(mapData2);
mapDataCollection.Add(mapData3);
mocks.ReplayAll();
// Call
var objects = info.ChildNodeObjects(mapDataCollection);
// Assert
CollectionAssert.AreEqual(new[]
{
mapData3,
mapData2,
mapData1
}, objects);
}
[Test]
public void CanDrop_SourceNodeTagIsNoMapData_ReturnsFalse()
{
// Setup
mocks.ReplayAll();
var mapDataCollection = new MapDataCollection("test data");
// Call
var canDrop = info.CanDrop(new object(), mapDataCollection);
// Assert
Assert.IsFalse(canDrop);
}
[Test]
public void CanDrop_SourceNodeTagIsMapData_ReturnsTrue()
{
// Setup
var mapData = mocks.StrictMock("test data");
var mapDataCollection = new MapDataCollection("test data");
mocks.ReplayAll();
// Call
var canDrop = info.CanDrop(mapData, mapDataCollection);
// Assert
Assert.IsTrue(canDrop);
}
[Test]
public void CanInsert_SourceNodeTagIsNoMapData_ReturnsFalse()
{
// Setup
mocks.ReplayAll();
var mapDataCollection = new MapDataCollection("test data");
// Call
var canInsert = info.CanInsert(new object(), mapDataCollection);
// Assert
Assert.IsFalse(canInsert);
}
[Test]
public void CanInsert_SourceNodeTagIsMapData_ReturnsTrue()
{
// Setup
var mapData = mocks.StrictMock("test data");
var mapDataCollection = new MapDataCollection("test data");
mocks.ReplayAll();
// Call
var canInsert = info.CanInsert(mapData, mapDataCollection);
// Assert
Assert.IsTrue(canInsert);
}
[Test]
[TestCase(0)]
[TestCase(1)]
[TestCase(2)]
public void OnDrop_MapDataMovedToPositionInsideRange_SetsNewReverseOrder(int position)
{
// Setup
var mapData1 = mocks.StrictMock("test data");
var mapData2 = mocks.StrictMock("test data");
var mapData3 = mocks.StrictMock("test data");
var mapDataCollection = new MapDataCollection("test data");
mapDataCollection.Add(mapData1);
mapDataCollection.Add(mapData2);
mapDataCollection.Add(mapData3);
var observer = mocks.StrictMock();
observer.Expect(o => o.UpdateObserver());
mocks.ReplayAll();
using (var treeViewControl = new TreeViewControl())
{
mapDataCollection.Attach(observer);
// Call
info.OnDrop(mapData1, mapDataCollection, mapDataCollection, position, treeViewControl);
// Assert
var reversedIndex = 2 - position;
Assert.AreSame(mapData1, mapDataCollection.Collection.ElementAt(reversedIndex));
}
}
[Test]
[TestCase(-50)]
[TestCase(-1)]
[TestCase(3)]
[TestCase(50)]
public void OnDrop_MapDataMovedToPositionOutsideRange_SetsNewReverseOrder(int position)
{
// Setup
var observer = mocks.StrictMock();
var mapData1 = mocks.StrictMock("test data");
var mapData2 = mocks.StrictMock("test data");
var mapData3 = mocks.StrictMock("test data");
var mapDataCollection = new MapDataCollection("test data");
mapDataCollection.Add(mapData1);
mapDataCollection.Add(mapData2);
mapDataCollection.Add(mapData3);
mapDataCollection.Attach(observer);
mocks.ReplayAll();
using (var treeViewControl = new TreeViewControl())
{
// Call
TestDelegate test = () => info.OnDrop(mapData1, mapDataCollection, mapDataCollection, position, treeViewControl);
// Assert
Assert.Throws(test);
}
}
[Test]
public void ContextMenuStrip_Always_CallsContextMenuBuilderMethods()
{
// Setup
var mapDataCollection = new MapDataCollection("test data");
var applicationFeatureCommandsMockMock = mocks.Stub();
var importCommandHandlerMock = mocks.Stub();
importCommandHandlerMock.Stub(ich => ich.CanImportOn(null)).IgnoreArguments().Return(true);
var exportCommandHandlerMock = mocks.Stub();
var viewCommandsMock = mocks.Stub();
using (var treeViewControl = new TreeViewControl())
{
// Call
var builder = new ContextMenuBuilder(applicationFeatureCommandsMockMock,
importCommandHandlerMock,
exportCommandHandlerMock,
viewCommandsMock,
mapDataCollection,
treeViewControl);
contextMenuBuilderProvider.Expect(cmbp => cmbp.Get(mapDataCollection, treeViewControl)).Return(builder);
mocks.ReplayAll();
// Call
using (ContextMenuStrip contextMenu = info.ContextMenuStrip(mapDataCollection, null, treeViewControl))
{
// Assert
Assert.AreEqual(3, contextMenu.Items.Count);
TestHelper.AssertContextMenuStripContainsItem(contextMenu, 0,
"&Voeg kaartlaag toe...",
"Importeer een nieuwe kaartlaag en voeg deze toe.",
Resources.MapPlusIcon);
TestHelper.AssertContextMenuStripContainsItem(contextMenu, 2,
"Ei&genschappen",
"Toon de eigenschappen in het Eigenschappenpaneel.",
GuiResources.PropertiesHS,
false);
CollectionAssert.AllItemsAreInstancesOfType(new[]
{
contextMenu.Items[1]
}, typeof(ToolStripSeparator));
}
}
// Assert
// Expectancies will be asserted in TearDown()
}
public override void TearDown()
{
mapLegendView.Dispose();
mocks.VerifyAll();
}
}
}