forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplicationSettingsBaseTests.cs
More file actions
242 lines (208 loc) · 8.16 KB
/
ApplicationSettingsBaseTests.cs
File metadata and controls
242 lines (208 loc) · 8.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.ComponentModel;
using System.Configuration;
using Xunit;
namespace System.ConfigurationTests
{
public class ApplicationSettingsBaseTests
{
private const int DefaultIntPropertyValue = 42;
private class SimpleSettings : ApplicationSettingsBase
{
[ApplicationScopedSetting]
public string StringProperty
{
get
{
return (string) this[nameof(StringProperty)];
}
set
{
this[nameof(StringProperty)] = value;
}
}
[UserScopedSetting]
[DefaultSettingValue("42")]
public int IntProperty
{
get
{
return (int)this[nameof(IntProperty)];
}
set
{
this[nameof(IntProperty)] = value;
}
}
}
private class PersistedSimpleSettings : SimpleSettings
{
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void Context_SimpleSettings_InNotNull(bool isSynchronized)
{
SimpleSettings settings = isSynchronized
? (SimpleSettings)SettingsBase.Synchronized(new SimpleSettings())
: new SimpleSettings();
Assert.NotNull(settings.Context);
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void Providers_SimpleSettings_Empty(bool isSynchronized)
{
SimpleSettings settings = isSynchronized
? (SimpleSettings)SettingsBase.Synchronized(new SimpleSettings())
: new SimpleSettings();
Assert.Equal(1, settings.Providers.Count);
Assert.NotNull(settings.Providers[typeof(LocalFileSettingsProvider).Name]);
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void GetSetStringProperty_SimpleSettings_Ok(bool isSynchronized)
{
SimpleSettings settings = isSynchronized
? (SimpleSettings)SettingsBase.Synchronized(new SimpleSettings())
: new SimpleSettings();
Assert.Equal(default, settings.StringProperty);
settings.StringProperty = "Foo";
Assert.Equal("Foo", settings.StringProperty);
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void GetSetIntProperty_SimpleSettings_Ok(bool isSynchronized)
{
SimpleSettings settings = isSynchronized
? (SimpleSettings)SettingsBase.Synchronized(new SimpleSettings())
: new SimpleSettings();
Assert.Equal(DefaultIntPropertyValue, settings.IntProperty);
settings.IntProperty = 10;
Assert.Equal(10, settings.IntProperty);
}
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsNanoServer)),
InlineData(true),
InlineData(false)]
[ActiveIssue("https://github.com/dotnet/runtime/issues/28833")]
public void Save_SimpleSettings_Ok(bool isSynchronized)
{
PersistedSimpleSettings settings = isSynchronized
? (PersistedSimpleSettings)SettingsBase.Synchronized(new PersistedSimpleSettings())
: new PersistedSimpleSettings();
// Make sure we're clean
settings.Reset();
settings.Save();
Assert.Equal(DefaultIntPropertyValue, settings.IntProperty);
Assert.Equal(default, settings.StringProperty);
// Change settings and save
settings.IntProperty = 12;
settings.StringProperty = "Bar";
Assert.Equal("Bar", settings.StringProperty);
Assert.Equal(12, settings.IntProperty);
settings.Save();
// Create a new instance and validate persisted settings
settings = isSynchronized
? (PersistedSimpleSettings)SettingsBase.Synchronized(new PersistedSimpleSettings())
: new PersistedSimpleSettings();
Assert.Equal(default, settings.StringProperty); // [ApplicationScopedSetting] isn't persisted
Assert.Equal(12, settings.IntProperty);
// Reset and save
settings.Reset();
settings.Save();
Assert.Equal(DefaultIntPropertyValue, settings.IntProperty);
Assert.Equal(default, settings.StringProperty);
// Create a new instance and validate persisted settings
settings = isSynchronized
? (PersistedSimpleSettings)SettingsBase.Synchronized(new PersistedSimpleSettings())
: new PersistedSimpleSettings();
Assert.Equal(default, settings.StringProperty); // [ApplicationScopedSetting] isn't persisted
Assert.Equal(DefaultIntPropertyValue, settings.IntProperty);
}
[Fact]
public void Reload_SimpleSettings_Ok()
{
var settings = new SimpleSettings
{
IntProperty = 10
};
Assert.NotEqual(DefaultIntPropertyValue, settings.IntProperty);
settings.Reload();
Assert.Equal(DefaultIntPropertyValue, settings.IntProperty);
}
[ReadOnly(false)]
[SettingsGroupName("TestGroup")]
[SettingsProvider(typeof(TestProvider))]
[SettingsSerializeAs(SettingsSerializeAs.Binary)]
private class SettingsWithAttributes : ApplicationSettingsBase
{
[ApplicationScopedSetting]
[SettingsProvider(typeof(TestProvider))]
public string StringProperty
{
get
{
return (string)this["StringProperty"];
}
set
{
this["StringProperty"] = value;
}
}
}
private class TestProvider : LocalFileSettingsProvider
{
}
[Fact]
public void SettingsProperty_SettingsWithAttributes_Ok()
{
SettingsWithAttributes settings = new SettingsWithAttributes();
Assert.Equal(1, settings.Properties.Count);
SettingsProperty property = settings.Properties["StringProperty"];
Assert.Equal(typeof(TestProvider), property.Provider.GetType());
Assert.Equal(SettingsSerializeAs.Binary, property.SerializeAs);
}
[Fact]
public void SettingsChanging_Success()
{
SimpleSettings settings = new SimpleSettings();
bool changingFired = false;
int newValue = 1976;
settings.SettingChanging += (object sender, SettingChangingEventArgs e)
=>
{
changingFired = true;
Assert.Equal(nameof(SimpleSettings.IntProperty), e.SettingName);
Assert.Equal(typeof(SimpleSettings).FullName, e.SettingClass);
Assert.Equal(newValue, e.NewValue);
};
settings.IntProperty = newValue;
Assert.True(changingFired);
Assert.Equal(newValue, settings.IntProperty);
}
[Fact]
public void SettingsChanging_Canceled()
{
int oldValue = 1776;
SimpleSettings settings = new SimpleSettings
{
IntProperty = oldValue
};
bool changingFired = false;
int newValue = 1976;
settings.SettingChanging += (object sender, SettingChangingEventArgs e)
=>
{
changingFired = true;
e.Cancel = true;
};
settings.IntProperty = newValue;
Assert.True(changingFired);
Assert.Equal(oldValue, settings.IntProperty);
}
}
}