public void TestSettingsValueChangedOnLoad() { ValueSettingsKeys.Initialize(); ListSettingsKeys.Initialize(); using (var writer = new StreamWriter(TempPath("TestSettingsValueChangedOnLoadText.txt"))) { writer.Write(TestSettingsValueChangedOnLoadText); } int intValueChangeCount = 0; int doubleValueChangeCount = 0; int stringValueChangeCount = 0; int intListChangeCount = 0; int doubleListChangeCount = 0; int stringListChangeCount = 0; ValueSettingsKeys.IntValue.ChangesValidated += (s, e) => ++ intValueChangeCount; ValueSettingsKeys.DoubleValue.ChangesValidated += (s, e) => ++ doubleValueChangeCount; ValueSettingsKeys.StringValue.ChangesValidated += (s, e) => ++ stringValueChangeCount; ListSettingsKeys.IntList.ChangesValidated += (s, e) => ++ intListChangeCount; ListSettingsKeys.DoubleList.ChangesValidated += (s, e) => ++ doubleListChangeCount; ListSettingsKeys.StringList.ChangesValidated += (s, e) => ++ stringListChangeCount; SettingsContainer.LoadSettingsProfile(TempPath("TestSettingsValueChangedOnLoadText.txt"), true); SettingsContainer.CurrentProfile.ValidateSettingsChanges(); Assert.Equal(1, intValueChangeCount); Assert.Equal(0, doubleValueChangeCount); Assert.Equal(0, stringValueChangeCount); Assert.Equal(1, intListChangeCount); Assert.Equal(0, doubleListChangeCount); Assert.Equal(0, stringListChangeCount); }
public void TestSettingsValueChanged() { // We use an array to avoid a closure issue (resharper) int[] settingsChangedCount = { 0 }; ValueSettingsKeys.Initialize(); ListSettingsKeys.Initialize(); ValueSettingsKeys.IntValue.ChangesValidated += (s, e) => ++ settingsChangedCount[0]; ValueSettingsKeys.DoubleValue.ChangesValidated += (s, e) => ++ settingsChangedCount[0]; ValueSettingsKeys.StringValue.ChangesValidated += (s, e) => ++ settingsChangedCount[0]; ValueSettingsKeys.IntValue.SetValue(20); ValueSettingsKeys.DoubleValue.SetValue(6.5); ValueSettingsKeys.StringValue.SetValue("New string"); SettingsContainer.CurrentProfile.ValidateSettingsChanges(); Assert.Equal(3, settingsChangedCount[0]); settingsChangedCount[0] = 0; }
public void TestSettingsFileModified() { // NUnit does not support async tests so lets wrap this task into a synchronous operation var task = Task.Run(async() => { var tcs = new TaskCompletionSource <int>(); EventHandler <FileModifiedEventArgs> settingsModified = (s, e) => e.ReloadFile = true; EventHandler <SettingsFileLoadedEventArgs> settingsLoaded = (s, e) => tcs.SetResult(0); try { using (var writer = new StreamWriter(TempPath("TestSettingsFileModified.txt"))) { writer.Write(TestSettingsFileModifiedText1); } SettingsContainer.LoadSettingsProfile(TempPath("TestSettingsFileModified.txt"), true); SettingsContainer.CurrentProfile.MonitorFileModification = true; SettingsContainer.CurrentProfile.FileModified += settingsModified; ValueSettingsKeys.Initialize(); ListSettingsKeys.Initialize(); Assert.Equal(55, ValueSettingsKeys.IntValue.GetValue()); SettingsContainer.SettingsFileLoaded += settingsLoaded; using (var writer = new StreamWriter(TempPath("TestSettingsFileModified.txt"))) { writer.Write(TestSettingsFileModifiedText2); } // Gives some time to the file watcher to awake. await tcs.Task; Assert.Equal(75, ValueSettingsKeys.IntValue.GetValue()); SettingsContainer.SettingsFileLoaded -= settingsLoaded; } catch { SettingsContainer.SettingsFileLoaded -= settingsLoaded; } }); // Block while the task has not ended to ensure that no other test will start before this one ends. task.Wait(); }
public void TestSettingsLoadWrongType() { using (var writer = new StreamWriter(TempPath("TestSettingsLoadWrongType.txt"))) { writer.Write(TestSettingsLoadWrongTypeFileText); } SettingsContainer.LoadSettingsProfile(TempPath("TestSettingsLoadWrongType.txt"), true); ValueSettingsKeys.Initialize(); ListSettingsKeys.Initialize(); Assert.Equal(ValueSettingsKeys.IntValue.DefaultValue, ValueSettingsKeys.IntValue.GetValue()); Assert.Equal(ValueSettingsKeys.DoubleValue.DefaultValue, ValueSettingsKeys.DoubleValue.GetValue()); Assert.Equal(ValueSettingsKeys.StringValue.DefaultValue, ValueSettingsKeys.StringValue.GetValue()); var intList = ListSettingsKeys.IntList.GetValue(); Assert.Equal(intList, ListSettingsKeys.IntList.DefaultValue); var doubleList = ListSettingsKeys.DoubleList.GetValue(); Assert.Equal(doubleList, ListSettingsKeys.DoubleList.DefaultValue); }
public void TestSettingsLoad() { using (var writer = new StreamWriter(TempPath("TestSettingsLoad.txt"))) { writer.Write(TestSettingsLoadFileText); } SettingsContainer.LoadSettingsProfile(TempPath("TestSettingsLoad.txt"), true); ValueSettingsKeys.Initialize(); ListSettingsKeys.Initialize(); Assert.Equal(45, ValueSettingsKeys.IntValue.GetValue()); Assert.Equal(25.0, ValueSettingsKeys.DoubleValue.GetValue()); Assert.Equal(new DateTime(2004, 7, 25, 18, 18, 00).ToString(CultureInfo.InvariantCulture), ValueSettingsKeys.StringValue.GetValue()); var intList = ListSettingsKeys.IntList.GetValue(); Assert.Equal(intList, new[] { 1, 3 }); var doubleList = ListSettingsKeys.DoubleList.GetValue(); Assert.Equal(doubleList, new[] { 9.0 }); var stringList = ListSettingsKeys.StringList.GetValue(); Assert.Equal(stringList, new[] { "String 1", "String 1.5", "String 2", "String 3" }); }
public void TestSettingsList() { ListSettingsKeys.Initialize(); var intList = ListSettingsKeys.IntList.GetValue(); intList.Add(1); intList.Add(3); var doubleList = ListSettingsKeys.DoubleList.GetValue(); doubleList.Remove(2.0); doubleList.RemoveAt(0); var stringList = ListSettingsKeys.StringList.GetValue(); stringList.Insert(1, "String 1.5"); stringList[2] = "String 2.0"; intList = ListSettingsKeys.IntList.GetValue(); Assert.Equal(intList, new[] { 1, 3 }); doubleList = ListSettingsKeys.DoubleList.GetValue(); Assert.Equal(doubleList, new[] { 9.0 }); stringList = ListSettingsKeys.StringList.GetValue(); Assert.Equal(stringList, new[] { "String 1", "String 1.5", "String 2.0", "String 3" }); }