Code development platform for open source projects from the European Union institutions :large_blue_circle: EU Login authentication by SMS has been phased out. To see alternatives please check here

Skip to content
Snippets Groups Projects
Select Git revision
  • 8a56aae6fb75fc03bb5043899a55bae777d16127
  • stable default
  • feat-fchv-bus
  • fix-h2-ice-bus
  • powertrains-multiple-axles
  • amdm3/develop
  • issue-1039
  • amdm3/main
  • test/nuget_publish
  • IEPC-experiments
  • amdm2/main
  • amdm2/develop
  • aptngearbox-not-auto
  • playground
  • official/main
  • official/develop
  • issue-templates
  • pdf-reports
  • HEV-timeruns-dev
  • timerun-empower-hybrids
  • timerun-pwheel-hybrids
  • Release/v5.0.3
  • Release/v5.0.1
  • Release/5.0.0-RC
  • Nuget/v0.11.4-DEV
  • Release/v0.11.4-DEV
  • Release/4.3.4-DEV
  • Release/4.3.3
  • Release/4.3.2-RC
  • Release/v4.3.0-DEV
  • Release/4.2.7
  • XMLConverterTool/4.2.6.0
  • Release/4.2.6-RC
  • Release/v4.2.5
  • Release/v4.2.3
  • Release/v4.2.2.3539-RC
  • Release/v4.2.1.3469
  • Release/v0.11.2.3456-DEV
  • Release/v4.2.0.3448-RC
  • Release/v4.1.3.3415
  • Release/v4.1.1.3413
41 results

BackingStorageTest.cs

Blame
  • Forked from VECTO / VECTO Sim
    4731 commits behind the upstream repository.
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    BackingStorageTest.cs 6.28 KiB
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Reflection;
    using System.Runtime.CompilerServices;
    using Castle.Components.DictionaryAdapter;
    using NUnit.Framework;
    using VECTO3GUI2020;
    using VECTO3GUI2020.ViewModel.Implementation.Common;
    using VECTO3GUI2020.ViewModel.MultiStage.Implementation;
    
    namespace Vecto3GUI2020Test.HelperTests
    {
    	[TestFixture]
    	public class BackingStorageTest
    	{
    		private TestVm _testVm;
    		private Dictionary<string, object> _currentValues;
    		private IReadOnlyDictionary<string, object> _savedValues;
    		private IReadOnlyDictionary<string, object> _unsavedChanges;
    		private bool _unsavedChangesNotified = false;
    
    		public class TestVm : ObservableObject
    		{
    			private string _stringTestProperty;
    
    			public string StringTestProperty
    			{
    				get => _stringTestProperty;
    				set => SetProperty(ref _stringTestProperty, value);
    			}
    
    			private string _notMonitoredProperty;
    
    			public string NotMonitoredProperty
    			{
    				get => _notMonitoredProperty;
    				set => SetProperty(ref _notMonitoredProperty, value);
    			}
    
    
    			public BackingStorage<TestVm> BackingStorage { get; set; }
    			public TestVm()
    			{
    				BackingStorage = new BackingStorage<TestVm>(this, nameof(StringTestProperty));
    			}
    		}
    
    		#region Setup and Property implementation
    		private IReadOnlyDictionary<string, object> SavedValues
    		{
    			get
    			{
    				return _savedValues ?? (_savedValues = (IReadOnlyDictionary<string, object>)_testVm.BackingStorage.GetType()
    					.GetField("_savedValues", BindingFlags.NonPublic | BindingFlags.Instance)
    					.GetValue(_testVm.BackingStorage));
    			}
    		}
    
    
    		[SetUp]
    		public void SetUp()
    		{
    			_unsavedChangesNotified = false;
    			_testVm = new TestVm();
    			_currentValues = (Dictionary<string, object>)_testVm.BackingStorage.
    				GetType().GetField("_currentValues", BindingFlags.NonPublic | BindingFlags.Instance).
    				GetValue(_testVm.BackingStorage);
    			_unsavedChanges = (Dictionary<string, object>)_testVm.BackingStorage.
    				GetType().GetField("_unsavedChanges", BindingFlags.NonPublic | BindingFlags.Instance).
    				GetValue(_testVm.BackingStorage);
    
    			_testVm.BackingStorage.PropertyChanged += BackingStorage_PropertyChanged;
    		}
    
    		[TearDown]
    		public void TearDown()
    		{
    			_unsavedChangesNotified = false;
    			
    			_currentValues = null;
    			_unsavedChanges = null;
    			_savedValues = null;
    			_testVm.BackingStorage.PropertyChanged -= BackingStorage_PropertyChanged;
    			_testVm = null;
    		}
    
            private void BackingStorage_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
            {
    			switch (e.PropertyName) {
    				case nameof(TestVm.BackingStorage.UnsavedChanges):
    					_unsavedChangesNotified = true;
    					break;
    				default:
    					break;
    			}
            }
    
    #endregion
    		[Test]
    		public void ValueUpdatedOnChange()
    		{
    
    			var stopWatch = Stopwatch.StartNew();
    			_testVm.StringTestProperty = "Hi i am the new value";
    			stopWatch.Stop();
    			TestContext.WriteLine($"Ticks ellapsed first update: {stopWatch.ElapsedTicks} ({stopWatch.Elapsed.TotalMilliseconds} ms)");
    
    			
    			
    
    			Assert.AreEqual(1, _currentValues.Count);
    			Assert.AreEqual(_testVm.StringTestProperty, _currentValues[nameof(_testVm.StringTestProperty)]);
    
    
    
    			stopWatch = Stopwatch.StartNew();
    			_testVm.StringTestProperty = "Hi i am ANOTHER value";
    			stopWatch.Stop();
    			TestContext.WriteLine($"Ticks ellapsed second update: {stopWatch.ElapsedTicks}  ({stopWatch.Elapsed.TotalMilliseconds} ms)");
    
    			Assert.AreEqual(1, _currentValues.Count);
    			Assert.AreEqual(_testVm.StringTestProperty, _currentValues[nameof(_testVm.StringTestProperty)]);
    		}
    
    		[Test]
    		public void NotMonitoredProperty()
    		{
    			_testVm.NotMonitoredProperty = "Hi i should not get stored in the backing storage";
    			Assert.IsFalse(_currentValues.ContainsKey(nameof(TestVm.NotMonitoredProperty)));
    		}
    
    		[Test]
    		public void SaveChanges()
    		{
    			_testVm.StringTestProperty = "hi";
    			Assert.IsTrue(checkNotified(ref _unsavedChangesNotified));
    			Assert.IsTrue(_testVm.BackingStorage.UnsavedChanges);
    
    			_testVm.BackingStorage.SaveChanges();
    			Assert.IsTrue(checkNotified(ref _unsavedChangesNotified));
    			Assert.IsFalse(_testVm.BackingStorage.UnsavedChanges);
    		}
    
    
    		[Test]
    		public void ChangeAndUndo()
    		{
    			var originalString = "original";
    			var newValue = "new Value";
    			//Set to initial value
    			_testVm.StringTestProperty = originalString;
    			Assert.IsTrue(checkNotified(ref _unsavedChangesNotified));
    			Assert.IsTrue(_testVm.BackingStorage.UnsavedChanges);
    
    			//Save
    			_testVm.BackingStorage.SaveChanges();
    			Assert.GreaterOrEqual(SavedValues.Count, 1);
    			Assert.IsTrue(checkNotified(ref _unsavedChangesNotified));
    			Assert.IsFalse(_testVm.BackingStorage.UnsavedChanges);
    
    			//Set to new value
    			_testVm.StringTestProperty = newValue;
    			Assert.IsTrue(checkNotified(ref _unsavedChangesNotified));
    			Assert.IsTrue(_testVm.BackingStorage.UnsavedChanges);
    
    			//Set to initial value
    			_testVm.StringTestProperty = originalString;
    			Assert.IsTrue(checkNotified(ref _unsavedChangesNotified));
    			Assert.IsFalse(_testVm.BackingStorage.UnsavedChanges);
    		}
    
    		[Test]
    		public void UnsavedTrueBeforeFirstSave()
    		{
    			Assert.IsTrue(_testVm.BackingStorage.UnsavedChanges);
    			_testVm.BackingStorage.SaveChanges();
    			Assert.IsFalse(_testVm.BackingStorage.UnsavedChanges);
    		}
    
    		[Test]
    		public void SavedChangesNotModified()
    		{
    			string initialValue = "hello world";
    
    			_testVm.StringTestProperty = initialValue;
    
    			_testVm.BackingStorage.SaveChanges();
    			
    			Assert.AreEqual(SavedValues.Count, _currentValues.Count);
    			Assert.AreEqual( initialValue, SavedValues[nameof(_testVm.StringTestProperty)]);
    
    			_testVm.StringTestProperty = "New value";
    			Assert.AreEqual(initialValue, SavedValues[nameof(_testVm.StringTestProperty)]);
    		}
    
    		[Test]
    		public void createEqualityComparer()
    		{
    			var stringEqualityComparer = EqualityComparer<string>.Default;
    			var CreateEqualityComparererMethodInfo = _testVm.BackingStorage.GetType()
    				.GetMethod("CreateEqualityComparer", BindingFlags.NonPublic | BindingFlags.Static);
    
    			var generatedEqualityComparer = CreateEqualityComparererMethodInfo.Invoke(null, new []{ typeof(String) });
    			Assert.AreEqual(stringEqualityComparer.GetType(), generatedEqualityComparer.GetType());
    
    
    		}
    
    		public bool checkNotified(ref bool value)
    		{
    			var result = value;
    			value = false;
    			return result;
    		}
    
    		
    	}
    }