public void WhenParentAndChildNonDefault_ThenOverlayByReturnsCorrectValues()
        {
            using (var key = this.hkcu.CreateSubKey(TestKeyPath))
            {
                var parent = RegistryQwordSetting.FromKey(
                    "test",
                    "title",
                    "description",
                    "category",
                    10L,
                    key,
                    0L, 100L);
                parent.Value = 42L;
                Assert.IsFalse(parent.IsDefault);

                var child = RegistryQwordSetting.FromKey(
                    "test",
                    "title",
                    "description",
                    "category",
                    10L,
                    key,
                    0L, 100L);
                child.Value = 1L;
                Assert.IsFalse(child.IsDefault);

                var effective = parent.OverlayBy(child);
                Assert.AreNotSame(effective, parent);
                Assert.AreNotSame(effective, child);

                Assert.AreEqual(1, effective.Value);
                Assert.AreEqual(42, effective.DefaultValue);
                Assert.IsFalse(effective.IsDefault);
            }
        }
        public void WhenParentIsNonDefaultAndChildSetToOriginalDefault_ThenIsDefaultReturnsFalse()
        {
            using (var key = this.hkcu.CreateSubKey(TestKeyPath))
            {
                var parent = RegistryQwordSetting.FromKey(
                    "test",
                    "title",
                    "description",
                    "category",
                    10L,
                    key,
                    0L, 100L);
                parent.Value = 42L;
                Assert.IsFalse(parent.IsDefault);

                var intermediate = RegistryQwordSetting.FromKey(
                    "test",
                    "title",
                    "description",
                    "category",
                    10L,
                    key,
                    0L, 100L);
                Assert.IsTrue(intermediate.IsDefault);

                var child = RegistryQwordSetting.FromKey(
                    "test",
                    "title",
                    "description",
                    "category",
                    10L,
                    key,
                    0L, 100L);

                var effective = parent
                                .OverlayBy(intermediate)
                                .OverlayBy(child);
                Assert.AreNotSame(effective, parent);
                Assert.AreNotSame(effective, intermediate);
                Assert.AreNotSame(effective, child);

                effective.Value = 10L;

                Assert.AreEqual(10, effective.Value);
                Assert.AreEqual(42, effective.DefaultValue);
                Assert.IsFalse(effective.IsDefault);
            }
        }
        public void WhenValueIsUnparsable_ThenSetValueRaisesFormatException()
        {
            using (var key = this.hkcu.CreateSubKey(TestKeyPath))
            {
                var setting = RegistryQwordSetting.FromKey(
                    "test",
                    "title",
                    "description",
                    "category",
                    17L,
                    key,
                    0L, 100L);

                Assert.Throws <FormatException>(() => setting.Value = "test");
            }
        }
        public void WhenValueIsOfWrongType_ThenSetValueRaisesInvalidCastException()
        {
            using (var key = this.hkcu.CreateSubKey(TestKeyPath))
            {
                var setting = RegistryQwordSetting.FromKey(
                    "test",
                    "title",
                    "description",
                    "category",
                    17L,
                    key,
                    0L, 100L);

                Assert.Throws <InvalidCastException>(() => setting.Value = false);
            }
        }
        public void WhenValueIsInvalid_ThenSetValueRaisesArgumentOutOfRangeException()
        {
            using (var key = this.hkcu.CreateSubKey(TestKeyPath))
            {
                var setting = RegistryQwordSetting.FromKey(
                    "test",
                    "title",
                    "description",
                    "category",
                    17L,
                    key,
                    0L, 100L);

                Assert.Throws <ArgumentOutOfRangeException>(() => setting.Value = -1L);
            }
        }
        public void WhenValueIsString_ThenSetValueParsesValue()
        {
            using (var key = this.hkcu.CreateSubKey(TestKeyPath))
            {
                var setting = RegistryQwordSetting.FromKey(
                    "test",
                    "title",
                    "description",
                    "category",
                    17L,
                    key,
                    0L, long.MaxValue);

                setting.Value = "120000000000000001";

                Assert.AreEqual(120000000000000001L, setting.Value);
            }
        }
        public void WhenValueDiffersFromDefault_ThenSetValueSucceedsAndSettingIsDirty()
        {
            using (var key = this.hkcu.CreateSubKey(TestKeyPath))
            {
                var setting = RegistryQwordSetting.FromKey(
                    "test",
                    "title",
                    "description",
                    "category",
                    17L,
                    key,
                    0L, 100L);

                setting.Value = 0L;

                Assert.IsFalse(setting.IsDefault);
                Assert.IsTrue(setting.IsDirty);
            }
        }
        public void WhenSettingIsNonNull_ThenSaveUpdatesRegistry()
        {
            using (var key = this.hkcu.CreateSubKey(TestKeyPath))
            {
                var setting = RegistryQwordSetting.FromKey(
                    "test",
                    "title",
                    "description",
                    "category",
                    17L,
                    key,
                    0L, 100L);

                setting.Value = 1L;
                setting.Save(key);

                Assert.AreEqual(1, key.GetValue("test"));
            }
        }
        public void WhenValueIsNull_ThenSetValueResetsToDefault()
        {
            using (var key = this.hkcu.CreateSubKey(TestKeyPath))
            {
                var setting = RegistryQwordSetting.FromKey(
                    "test",
                    "title",
                    "description",
                    "category",
                    17L,
                    key,
                    0L, 100L);

                setting.Value = 1L;
                setting.Value = null;

                Assert.AreEqual(17L, setting.Value);
                Assert.IsTrue(setting.IsDefault);
            }
        }
        public void WhenSettingIsNull_ThenSaveResetsRegistry()
        {
            using (var key = this.hkcu.CreateSubKey(TestKeyPath))
            {
                key.SetValue("test", 42L, RegistryValueKind.QWord);

                var setting = RegistryQwordSetting.FromKey(
                    "test",
                    "title",
                    "description",
                    "category",
                    17L,
                    key,
                    0L, 100L);

                setting.Value = null;
                setting.Save(key);

                Assert.IsNull(key.GetValue("test"));
            }
        }
        public void WhenRegistryKeyIsNull_ThenFromKeyUsesDefaults()
        {
            using (var key = this.hkcu.CreateSubKey(TestKeyPath))
            {
                var setting = RegistryQwordSetting.FromKey(
                    "test",
                    "title",
                    "description",
                    "category",
                    17L,
                    null,
                    0L, 100L);

                Assert.AreEqual("test", setting.Key);
                Assert.AreEqual("title", setting.Title);
                Assert.AreEqual("description", setting.Description);
                Assert.AreEqual("category", setting.Category);
                Assert.AreEqual(17L, setting.Value);
                Assert.IsTrue(setting.IsDefault);
                Assert.IsFalse(setting.IsDirty);
            }
        }
        public void WhenRegistryValueExists_ThenFromKeyUsesValue()
        {
            using (var key = this.hkcu.CreateSubKey(TestKeyPath))
            {
                key.SetValue("test", 420000000000001L, RegistryValueKind.QWord);

                var setting = RegistryQwordSetting.FromKey(
                    "test",
                    "title",
                    "description",
                    "category",
                    17L,
                    key,
                    0L, long.MaxValue);

                Assert.AreEqual("test", setting.Key);
                Assert.AreEqual("title", setting.Title);
                Assert.AreEqual("description", setting.Description);
                Assert.AreEqual("category", setting.Category);
                Assert.AreEqual(420000000000001, setting.Value);
                Assert.IsFalse(setting.IsDefault);
                Assert.IsFalse(setting.IsDirty);
            }
        }
 public static ApplicationSettings FromKey(RegistryKey registryKey)
 {
     return(new ApplicationSettings()
     {
         IsMainWindowMaximized = RegistryBoolSetting.FromKey(
             "IsMainWindowMaximized",
             "IsMainWindowMaximized",
             null,
             null,
             false,
             registryKey),
         MainWindowHeight = RegistryDwordSetting.FromKey(
             "MainWindowHeight",
             "MainWindowHeight",
             null,
             null,
             0,
             registryKey,
             0,
             ushort.MaxValue),
         MainWindowWidth = RegistryDwordSetting.FromKey(
             "WindowWidth",
             "WindowWidth",
             null,
             null,
             0,
             registryKey,
             0,
             ushort.MaxValue),
         IsUpdateCheckEnabled = RegistryBoolSetting.FromKey(
             "IsUpdateCheckEnabled",
             "IsUpdateCheckEnabled",
             null,
             null,
             true,
             registryKey),
         LastUpdateCheck = RegistryQwordSetting.FromKey(
             "LastUpdateCheck",
             "LastUpdateCheck",
             null,
             null,
             0,
             registryKey,
             0,
             long.MaxValue),
         IsPreviewFeatureSetEnabled = RegistryBoolSetting.FromKey(
             "IsPreviewFeatureSetEnabled",
             "IsPreviewFeatureSetEnabled",
             null,
             null,
             false,
             registryKey),
         ProxyUrl = RegistryStringSetting.FromKey(
             "ProxyUrl",
             "ProxyUrl",
             null,
             null,
             null,
             registryKey,
             url => url == null || Uri.TryCreate(url, UriKind.Absolute, out Uri _)),
     });
 public static ApplicationSettings FromKey(RegistryKey registryKey)
 {
     return(new ApplicationSettings()
     {
         IsMainWindowMaximized = RegistryBoolSetting.FromKey(
             "IsMainWindowMaximized",
             "IsMainWindowMaximized",
             null,
             null,
             false,
             registryKey),
         MainWindowHeight = RegistryDwordSetting.FromKey(
             "MainWindowHeight",
             "MainWindowHeight",
             null,
             null,
             0,
             registryKey,
             0,
             ushort.MaxValue),
         MainWindowWidth = RegistryDwordSetting.FromKey(
             "WindowWidth",
             "WindowWidth",
             null,
             null,
             0,
             registryKey,
             0,
             ushort.MaxValue),
         IsUpdateCheckEnabled = RegistryBoolSetting.FromKey(
             "IsUpdateCheckEnabled",
             "IsUpdateCheckEnabled",
             null,
             null,
             true,
             registryKey),
         LastUpdateCheck = RegistryQwordSetting.FromKey(
             "LastUpdateCheck",
             "LastUpdateCheck",
             null,
             null,
             0,
             registryKey,
             0,
             long.MaxValue),
         IsPreviewFeatureSetEnabled = RegistryBoolSetting.FromKey(
             "IsPreviewFeatureSetEnabled",
             "IsPreviewFeatureSetEnabled",
             null,
             null,
             false,
             registryKey),
         ProxyUrl = RegistryStringSetting.FromKey(
             "ProxyUrl",
             "ProxyUrl",
             null,
             null,
             null,
             registryKey,
             url => url == null || Uri.TryCreate(url, UriKind.Absolute, out Uri _)),
         ProxyPacUrl = RegistryStringSetting.FromKey(
             "ProxyPacUrl",
             "ProxyPacUrl",
             null,
             null,
             null,
             registryKey,
             url => url == null || Uri.TryCreate(url, UriKind.Absolute, out Uri _)),
         ProxyUsername = RegistryStringSetting.FromKey(
             "ProxyUsername",
             "ProxyUsername",
             null,
             null,
             null,
             registryKey,
             _ => true),
         ProxyPassword = RegistrySecureStringSetting.FromKey(
             "ProxyPassword",
             "ProxyPassword",
             null,
             null,
             registryKey,
             DataProtectionScope.CurrentUser),
         IsDeviceCertificateAuthenticationEnabled = RegistryBoolSetting.FromKey(
             "IsDeviceCertificateAuthenticationEnabled",
             "IsDeviceCertificateAuthenticationEnabled",
             null,
             null,
             false,
             registryKey),
         FullScreenDevices = RegistryStringSetting.FromKey(
             "FullScreenDevices",
             "FullScreenDevices",
             null,
             null,
             null,
             registryKey,
             _ => true),
         IncludeOperatingSystems = RegistryEnumSetting <OperatingSystems> .FromKey(
             "IncludeOperatingSystems",
             "IncludeOperatingSystems",
             null,
             null,
             OperatingSystems.Windows,
             registryKey)
     });