Ejemplo n.º 1
0
 public void TestSettingsInitialization()
 {
     ValueSettingsKeys.Initialize();
     Assert.Equal(10, ValueSettingsKeys.IntValue.GetValue());
     Assert.Equal(3.14, ValueSettingsKeys.DoubleValue.GetValue());
     Assert.Equal("Test string", ValueSettingsKeys.StringValue.GetValue());
 }
Ejemplo n.º 2
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;

            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);
        }
Ejemplo n.º 3
0
        public void TestSettingsWrite()
        {
            ValueSettingsKeys.Initialize();
            ValueSettingsKeys.IntValue.SetValue(20);
            ValueSettingsKeys.DoubleValue.SetValue(6.5);
            ValueSettingsKeys.StringValue.SetValue("New string");
            Assert.Equal(20, ValueSettingsKeys.IntValue.GetValue());
            Assert.Equal(6.5, ValueSettingsKeys.DoubleValue.GetValue());
            Assert.Equal("New string", ValueSettingsKeys.StringValue.GetValue());

            ValueSettingsKeys.IntValue.SetValue(30);
            ValueSettingsKeys.DoubleValue.SetValue(9.1);
            ValueSettingsKeys.StringValue.SetValue("Another string");
            Assert.Equal(30, ValueSettingsKeys.IntValue.GetValue());
            Assert.Equal(9.1, ValueSettingsKeys.DoubleValue.GetValue());
            Assert.Equal("Another string", ValueSettingsKeys.StringValue.GetValue());
        }
Ejemplo n.º 4
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");
            SettingsContainer.CurrentProfile.ValidateSettingsChanges();
            Assert.Equal(3, settingsChangedCount[0]);
            settingsChangedCount[0] = 0;
        }
Ejemplo n.º 5
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();
        }
Ejemplo n.º 6
0
        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);
        }
Ejemplo n.º 7
0
        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" });
        }