Example #1
0
		public void CanOverrideDefaultLocation()
		{
			RegistrationSettingsProvider.SetProductName("FlowerUnitTest");
			var settingsProvider = new TestCrossPlatformSettingsProvider();
			settingsProvider.Initialize(null, null); // Seems to be what .NET does, despite warnings
			var dirPath = settingsProvider.UserConfigLocation;
			Assert.That(dirPath, Is.StringContaining("FlowerUnitTest"));
			Directory.CreateDirectory(dirPath);
			var filePath = Path.Combine(dirPath, TestCrossPlatformSettingsProvider.UserConfigFileName);
			using (var tempFile = new TempFile(filePath, true))
			{
				File.WriteAllText(filePath,
					@"<?xml version='1.0' encoding='utf-8'?>
<configuration>
    <userSettings>
        <Palaso.UI.WindowsForms.Registration.Registration>
            <setting name='Email' serializeAs='String'>
                <value>[email protected]</value>
            </setting>
        </Palaso.UI.WindowsForms.Registration.Registration>
    </userSettings>
</configuration>");

				var regSettings = Registration.Default;
				var email = regSettings.Email;
				Assert.That(email, Is.EqualTo("*****@*****.**"));
			}
		}
Example #2
0
		public void CanSaveBothRegularAndRegistrationSettings()
		{
			// We need to START with a file that has no Registration settings.
			// (Note that we do NOT set a special location for the registration settings in this test.
			// For this text, we are interested in what happens when the two lots of settings are saved in the SAME file.
			// This can happen, even when the registration location is being customized; for example, all channels of
			// Bloom save Registration settings under the product name "Bloom", but the main stable release build will also
			// save its regular settings there.
			var provider = new TestCrossPlatformSettingsProvider();
			var settingsFilePath = Path.Combine(provider.UserConfigLocation, TestCrossPlatformSettingsProvider.UserConfigFileName);
			if (File.Exists(settingsFilePath))
				File.Delete(settingsFilePath);

			Settings.Default.TestString = "hello world";
			Settings.Default.Save();
			Settings.Default.AnotherTest = "another";
			Settings.Default.Save();
			Registration.Default.FirstName = "John";
			Registration.Default.Save();

			// This line was a problem in one version of the code, where Settings saved a version of the XML without the Registration stuff.
			// The thing this test is mainly about is that this subsequent Save() does not discard the registration settings.
			Settings.Default.Save();

			// Somehow a Reload() at this point does NOT detect that the registration settings are missing (if they are).
			string fileContent = File.ReadAllText(settingsFilePath);
			Assert.That(fileContent, Is.StringContaining("Registration"));
			Assert.That(fileContent, Is.StringContaining("John"));
		}
Example #3
0
		public void SettingsFolderWithNewerConfig_SortsBeforeOneWithOlderConfig()
		{

			using (var tempFolder = new TemporaryFolder())
			{
				var firstDirPath = Path.Combine(tempFolder.Path, "first");
				var secondDirPath = Path.Combine(tempFolder.Path, "second");
				var firstConfigFile = Path.Combine(firstDirPath, TestCrossPlatformSettingsProvider.UserConfigFileName);
				var secondConfigFile = Path.Combine(secondDirPath, TestCrossPlatformSettingsProvider.UserConfigFileName);
				Directory.CreateDirectory(firstDirPath);
				Directory.CreateDirectory(secondDirPath);
				File.WriteAllText(firstConfigFile, @"nonsense");
				Thread.Sleep(1000); // May help ensure write times are sufficiently different on TeamCity.
				File.WriteAllText(secondConfigFile, @"nonsense"); // second is newer

				var result = TestCrossPlatformSettingsProvider.VersionDirectoryComparison(firstDirPath, secondDirPath);
				Assert.That(result, Is.GreaterThan(0));

				Thread.Sleep(1000); // May help ensure write times are sufficiently different on TeamCity.
				File.WriteAllText(firstConfigFile, @"nonsense"); // now first is newer
				result = TestCrossPlatformSettingsProvider.VersionDirectoryComparison(firstDirPath, secondDirPath);
				Assert.That(result, Is.LessThan(0));

				// A final check to make sure it is really working the way we want
				var list = new List<string>();
				list.Add(secondDirPath);
				list.Add(firstDirPath);
				list.Sort(TestCrossPlatformSettingsProvider.VersionDirectoryComparison);
				Assert.That(list[0], Is.EqualTo(firstDirPath));
			}
		}
Example #4
0
        public void CanOverrideDefaultLocation()
        {
            string settingsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "SIL", "SettingsProviderTests");

            Directory.CreateDirectory(settingsPath);
            using (TemporaryFolder.TrackExisting(settingsPath))
            {
                RegistrationSettingsProvider.SetProductName("SettingsProviderTests");
                var settingsProvider = new TestCrossPlatformSettingsProvider();
                settingsProvider.Initialize(null, null);                 // Seems to be what .NET does, despite warnings
                string dirPath = settingsProvider.UserConfigLocation;
                Assert.That(dirPath, Is.StringContaining("SettingsProviderTests"));
                Directory.CreateDirectory(dirPath);
                string filePath = Path.Combine(dirPath, TestCrossPlatformSettingsProvider.UserConfigFileName);
                using (new TempFile(filePath, true))
                {
                    File.WriteAllText(filePath,
                                      @"<?xml version='1.0' encoding='utf-8'?>
<configuration>
	<userSettings>
		<SIL.Windows.Forms.Registration.Registration>
			<setting name='Email' serializeAs='String'>
				<value>[email protected]</value>
			</setting>
		</SIL.Windows.Forms.Registration.Registration>
	</userSettings>
</configuration>");

                    Registration.Registration regSettings = Registration.Registration.Default;
                    Assert.That(regSettings.Email, Is.EqualTo("*****@*****.**"));
                }
            }
        }
Example #5
0
        public void CheckForErrorsInFile_FileCorrupt_ReturnsMessage()
        {
            var settingsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "SIL", "SettingsProviderTests");

            Directory.CreateDirectory(settingsPath);
            using (TemporaryFolder.TrackExisting(settingsPath))
            {
                var settingsProvider = new TestCrossPlatformSettingsProvider();
                settingsProvider.Initialize(null, null);                 // Seems to be what .NET does, despite warnings
                var dirPath = settingsProvider.UserConfigLocation;
                Directory.CreateDirectory(dirPath);
                var filePath = Path.Combine(dirPath, TestCrossPlatformSettingsProvider.UserConfigFileName);
                using (new TempFile(filePath, true))
                {
                    File.WriteAllText(filePath, "hello world");

                    Assert.That(settingsProvider.CheckForErrorsInSettingsFile(), Is.Not.Null);

                    //because we already did the check, we don't expect to see any error now
                    using (new ErrorReport.NoNonFatalErrorReportExpected())
                    {
                        var dummy = Registration.Registration.Default;
                    }
                }
            }
        }
Example #6
0
        public void LoadSettings_FileCorrupt_ShowsErrorAndSelfHeals()
        {
            var settingsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "SIL", "SettingsProviderTests");

            Directory.CreateDirectory(settingsPath);
            using (TemporaryFolder.TrackExisting(settingsPath))
            {
                var settingsProvider = new TestCrossPlatformSettingsProvider();
                settingsProvider.Initialize(null, null);                 // Seems to be what .NET does, despite warnings
                var dirPath = settingsProvider.UserConfigLocation;
                Directory.CreateDirectory(dirPath);
                var filePath = Path.Combine(dirPath, TestCrossPlatformSettingsProvider.UserConfigFileName);
                File.Delete(filePath);
                using (new TempFile(filePath, true))
                {
                    File.WriteAllText(filePath, "hello world");
                    using (new ErrorReport.NonFatalErrorReportExpected())
                    {
                        var dummy = Registration.Registration.Default;
                        //by this point, we expect that the file has been fixed and we can continue normally
                        dummy.Email = "*****@*****.**";
                    }
                }

                //next time, it should be healed
                using (new ErrorReport.NoNonFatalErrorReportExpected())
                {
                    var settingsProvider2 = new TestCrossPlatformSettingsProvider();
                    settingsProvider2.Initialize(null, null);                     // Seems to be what .NET does, despite warnings
                    Assert.That(Registration.Registration.Default.Email, Is.EqualTo("*****@*****.**"));
                }
            }
        }
		public void CanOverrideDefaultLocation()
		{
			string settingsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "SIL", "SettingsProviderTests");
			Directory.CreateDirectory(settingsPath);
			using (TemporaryFolder.TrackExisting(settingsPath))
			{
				RegistrationSettingsProvider.SetProductName("SettingsProviderTests");
				var settingsProvider = new TestCrossPlatformSettingsProvider();
				settingsProvider.Initialize(null, null); // Seems to be what .NET does, despite warnings
				string dirPath = settingsProvider.UserConfigLocation;
				Assert.That(dirPath, Is.StringContaining("SettingsProviderTests"));
				Directory.CreateDirectory(dirPath);
				string filePath = Path.Combine(dirPath, TestCrossPlatformSettingsProvider.UserConfigFileName);
				using (new TempFile(filePath, true))
				{
					File.WriteAllText(filePath,
						@"<?xml version='1.0' encoding='utf-8'?>
<configuration>
	<userSettings>
		<SIL.Windows.Forms.Registration.Registration>
			<setting name='Email' serializeAs='String'>
				<value>[email protected]</value>
			</setting>
		</SIL.Windows.Forms.Registration.Registration>
	</userSettings>
</configuration>");

					Registration.Registration regSettings = Registration.Registration.Default;
					Assert.That(regSettings.Email, Is.EqualTo("*****@*****.**"));
				}
			}
		}
		public void CanSaveBothRegularAndRegistrationSettings()
		{
			// We need to START with a file that has no Registration settings.
			// (Note that we do NOT set a special location for the registration settings in this test.
			// For this text, we are interested in what happens when the two lots of settings are saved in the SAME file.
			// This can happen, even when the registration location is being customized; for example, all channels of
			// Bloom save Registration settings under the product name "Bloom", but the main stable release build will also
			// save its regular settings there.
			var provider = new TestCrossPlatformSettingsProvider();
			var settingsFilePath = Path.Combine(provider.UserConfigLocation, TestCrossPlatformSettingsProvider.UserConfigFileName);
			if (File.Exists(settingsFilePath))
				File.Delete(settingsFilePath);

			Properties.Settings.Default.TestString = "hello world";
			Properties.Settings.Default.Save();
			Properties.Settings.Default.AnotherTest = "another";
			Properties.Settings.Default.Save();
			Registration.Registration.Default.FirstName = "John";
			Registration.Registration.Default.Save();

			// This line was a problem in one version of the code, where Settings saved a version of the XML without the Registration stuff.
			// The thing this test is mainly about is that this subsequent Save() does not discard the registration settings.
			Properties.Settings.Default.Save();

			// Somehow a Reload() at this point does NOT detect that the registration settings are missing (if they are).
			string fileContent = File.ReadAllText(settingsFilePath);
			Assert.That(fileContent, Is.StringContaining("Registration"));
			Assert.That(fileContent, Is.StringContaining("John"));
		}
		public void CheckForErrorsInFile_FileCorrupt_ReturnsMessage()
		{
			var settingsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "SIL", "SettingsProviderTests");
			Directory.CreateDirectory(settingsPath);
			using (TemporaryFolder.TrackExisting(settingsPath))
			{
				var settingsProvider = new TestCrossPlatformSettingsProvider();
				settingsProvider.Initialize(null, null); // Seems to be what .NET does, despite warnings
				var dirPath = settingsProvider.UserConfigLocation;
				Directory.CreateDirectory(dirPath);
				var filePath = Path.Combine(dirPath, TestCrossPlatformSettingsProvider.UserConfigFileName);
				using (new TempFile(filePath, true))
				{
					File.WriteAllText(filePath, "hello world");

					Assert.That(settingsProvider.CheckForErrorsInSettingsFile(), Is.Not.Null);

					//because we already did the check, we don't expect to see any error now
					using (new ErrorReport.NoNonFatalErrorReportExpected())
					{
						var dummy = Registration.Registration.Default;
					}
				}
			}
		}
		public void LoadSettings_FileCorrupt_ShowsErrorAndSelfHeals()
		{
			var settingsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "SIL", "SettingsProviderTests");
			Directory.CreateDirectory(settingsPath);
			using (TemporaryFolder.TrackExisting(settingsPath))
			{
				var settingsProvider = new TestCrossPlatformSettingsProvider();
				settingsProvider.Initialize(null, null); // Seems to be what .NET does, despite warnings
				var dirPath = settingsProvider.UserConfigLocation;
				Directory.CreateDirectory(dirPath);
				var filePath = Path.Combine(dirPath, TestCrossPlatformSettingsProvider.UserConfigFileName);
				File.Delete(filePath);
				using (new TempFile(filePath, true))
				{
					File.WriteAllText(filePath,"hello world");
					using (new ErrorReport.NonFatalErrorReportExpected())
					{
						var dummy = Registration.Registration.Default;
						//by this point, we expect that the file has been fixed and we can continue normally
						dummy.Email = "*****@*****.**";
					}
				}

				//next time, it should be healed
				using (new ErrorReport.NoNonFatalErrorReportExpected())
				{
					var settingsProvider2 = new TestCrossPlatformSettingsProvider();
					settingsProvider2.Initialize(null, null); // Seems to be what .NET does, despite warnings
					Assert.That(Registration.Registration.Default.Email, Is.EqualTo("*****@*****.**"));
				}
			}
		}