Beispiel #1
0
        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;

            SettingsGroup.LoadSettingsProfile(TempPath("TestSettingsValueChangedOnLoadText.txt"), true);
            SettingsGroup.CurrentProfile.ValidateSettingsChanges();

            Assert.AreEqual(1, intValueChangeCount);
            Assert.AreEqual(0, doubleValueChangeCount);
            Assert.AreEqual(0, stringValueChangeCount);
            Assert.AreEqual(1, intListChangeCount);
            Assert.AreEqual(0, doubleListChangeCount);
            Assert.AreEqual(0, stringListChangeCount);
        }
Beispiel #2
0
        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");
            SettingsGroup.CurrentProfile.ValidateSettingsChanges();
            Assert.AreEqual(3, settingsChangedCount[0]);
            settingsChangedCount[0] = 0;
        }
Beispiel #3
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);
                    }
                    SettingsGroup.LoadSettingsProfile(TempPath("TestSettingsFileModified.txt"), true);
                    SettingsGroup.CurrentProfile.MonitorFileModification = true;
                    SettingsGroup.CurrentProfile.FileModified           += settingsModified;
                    ValueSettingsKeys.Initialize();
                    ListSettingsKeys.Initialize();
                    Assert.AreEqual(55, ValueSettingsKeys.IntValue.GetValue());

                    SettingsGroup.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.AreEqual(75, ValueSettingsKeys.IntValue.GetValue());
                    SettingsGroup.SettingsFileLoaded -= settingsLoaded;
                }
                catch
                {
                    SettingsGroup.SettingsFileLoaded -= settingsLoaded;
                }
            });

            // Block while the task has not ended to ensure that no other test will start before this one ends.
            task.Wait();
        }
Beispiel #4
0
        public void TestSettingsLoadWrongType()
        {
            using (var writer = new StreamWriter(TempPath("TestSettingsLoadWrongType.txt")))
            {
                writer.Write(TestSettingsLoadWrongTypeFileText);
            }
            SettingsGroup.LoadSettingsProfile(TempPath("TestSettingsLoadWrongType.txt"), true);

            ValueSettingsKeys.Initialize();
            ListSettingsKeys.Initialize();
            Assert.AreEqual(ValueSettingsKeys.IntValue.DefaultValue, ValueSettingsKeys.IntValue.GetValue());
            Assert.AreEqual(ValueSettingsKeys.DoubleValue.DefaultValue, ValueSettingsKeys.DoubleValue.GetValue());
            Assert.AreEqual(ValueSettingsKeys.StringValue.DefaultValue, ValueSettingsKeys.StringValue.GetValue());
            var intList = ListSettingsKeys.IntList.GetValue();

            Assert.That(intList, Is.EquivalentTo(ListSettingsKeys.IntList.DefaultValue));
            var doubleList = ListSettingsKeys.DoubleList.GetValue();

            Assert.That(doubleList, Is.EquivalentTo(ListSettingsKeys.DoubleList.DefaultValue));
        }
Beispiel #5
0
        public void TestSettingsLoad()
        {
            using (var writer = new StreamWriter(TempPath("TestSettingsLoad.txt")))
            {
                writer.Write(TestSettingsLoadFileText);
            }
            SettingsGroup.LoadSettingsProfile(TempPath("TestSettingsLoad.txt"), true);

            ValueSettingsKeys.Initialize();
            ListSettingsKeys.Initialize();
            Assert.AreEqual(45, ValueSettingsKeys.IntValue.GetValue());
            Assert.AreEqual(25.0, ValueSettingsKeys.DoubleValue.GetValue());
            Assert.AreEqual(new DateTime(2004, 7, 25, 18, 18, 00).ToString(CultureInfo.InvariantCulture), ValueSettingsKeys.StringValue.GetValue());
            var intList = ListSettingsKeys.IntList.GetValue();

            Assert.That(intList, Is.EquivalentTo(new[] { 1, 3 }));
            var doubleList = ListSettingsKeys.DoubleList.GetValue();

            Assert.That(doubleList, Is.EquivalentTo(new[] { 9.0 }));
            var stringList = ListSettingsKeys.StringList.GetValue();

            Assert.That(stringList, Is.EquivalentTo(new[] { "String 1", "String 1.5", "String 2", "String 3" }));
        }
Beispiel #6
0
        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.That(intList, Is.EquivalentTo(new[] { 1, 3 }));
            doubleList = ListSettingsKeys.DoubleList.GetValue();
            Assert.That(doubleList, Is.EquivalentTo(new[] { 9.0 }));
            stringList = ListSettingsKeys.StringList.GetValue();
            Assert.That(stringList, Is.EquivalentTo(new[] { "String 1", "String 1.5", "String 2.0", "String 3" }));
        }