<commit_before>// Copyright (c) 2013, SIL International.
// Distributable under the terms of the MIT license (http://opensource.org/licenses/MIT).

using System;
using System.Globalization;
using System.Linq;
using System.Windows.Forms;
using NUnit.Framework;
using SIL.WritingSystems.WindowsForms.Keyboarding;

namespace SIL.WritingSystems.WindowsForms.Tests.Keyboarding
{
	[TestFixture]
	public class KeyboardControllerTests
	{
		[SetUp]
		public void Setup()
		{
			KeyboardController.Initialize();
		}

		[TearDown]
		public void TearDown()
		{
			KeyboardController.Shutdown();
		}

		[Test]
		public void CreateKeyboardDefinition_ExistingKeyboard_ReturnsReference()
		{
			var expectedKeyboard = new KeyboardDescription("en-US_foo", "foo - English (US)", "foo", "en-US", true,
				KeyboardController.Instance.Adaptors[0]);
			KeyboardController.Instance.Keyboards.Add(expectedKeyboard);
			IKeyboardDefinition keyboard = Keyboard.Controller.CreateKeyboardDefinition("en-US_foo", KeyboardFormat.Unknown, null);
			Assert.That(keyboard, Is.SameAs(expectedKeyboard));
			Assert.That(keyboard, Is.TypeOf<KeyboardDescription>());
			Assert.That(keyboard.Name, Is.EqualTo("foo - English (US)"));
		}

		[Test]
		public void GetKeyboard_FromInputLanguage_ExistingKeyboardReturnsKeyboard()
		{
			var keyboard = Keyboard.Controller.CreateKeyboardDefinition("en-US_foo", KeyboardFormat.Unknown, null);
			KeyboardController.Instance.Keyboards.Add(keyboard);

			var inputLanguage = new InputLanguageWrapper(new CultureInfo("en-US"), IntPtr.Zero, "foo");
			Assert.That(Keyboard.Controller.GetKeyboard(inputLanguage), Is.EqualTo(keyboard));
		}

		[Test]
		public void GetKeyboard_FromNewPalasoId_ExistingKeyboardReturnsKeyboard()
		{
			var keyboard = Keyboard.Controller.CreateKeyboardDefinition("en-US_foo", KeyboardFormat.Unknown, null);
			KeyboardController.Instance.Keyboards.Add(keyboard);

			Assert.That(Keyboard.Controller.GetKeyboard("en-US_foo"), Is.EqualTo(keyboard));
		}

		[Test]
		public void GetKeyboard_FromNewPalasoId_NonExistingKeyboard()
		{
			var keyboard = Keyboard.Controller.CreateKeyboardDefinition("en-US_foo", KeyboardFormat.Unknown, null);
			KeyboardController.Instance.Keyboards.Add(keyboard);

			Assert.That(Keyboard.Controller.GetKeyboard("en-US_glop"), Is.EqualTo(KeyboardDescription.Zero));
		}

		[Test]
		public void GetKeyboard_FromOldParatextId_ExistingKeyboardReturnsKeyboard()
		{
			// This is the case of a keyboard that the old Palaso system was incapable of supporting.
			var keyboardFooBoo = Keyboard.Controller.CreateKeyboardDefinition("az-Latn-AZ_foo", KeyboardFormat.Unknown, null);
			KeyboardController.Instance.Keyboards.Add(keyboardFooBoo);

			var keyboard = Keyboard.Controller.CreateKeyboardDefinition("en-US_foo", KeyboardFormat.Unknown, null);
			KeyboardController.Instance.Keyboards.Add(keyboard);

			Assert.That(Keyboard.Controller.GetKeyboard("foo|en-US"), Is.EqualTo(keyboard));
			Assert.That(Keyboard.Controller.GetKeyboard("foo|az-Latn-AZ"), Is.EqualTo(keyboardFooBoo));
		}

		[Test]
		public void GetKeyboard_FromOldParatextId_NonExistingKeyboard()
		{
			var keyboard = Keyboard.Controller.CreateKeyboardDefinition("en-US_foo", KeyboardFormat.Unknown, null);
			KeyboardController.Instance.Keyboards.Add(keyboard);

			Assert.That(Keyboard.Controller.GetKeyboard("glop|en-US"), Is.EqualTo(KeyboardDescription.Zero));
		}

		[Test]
		public void GetKeyboard_FromOldPalasoId_ExistingKeyboardReturnsKeyboard()
		{
			// This demonstrates the case of a keyboard that the old Palaso system was incapable of supporting.
			// There's no way to reference this keyboard, or is there???
			IKeyboardDefinition keyboardFooBoo = Keyboard.Controller.CreateKeyboardDefinition("az-Latn-AZ_foo", KeyboardFormat.Unknown, null);
			KeyboardController.Instance.Keyboards.Add(keyboardFooBoo);

			IKeyboardDefinition keyboardFooF = Keyboard.Controller.CreateKeyboardDefinition("en-US_foo-az", KeyboardFormat.Unknown, null);
			KeyboardController.Instance.Keyboards.Add(keyboardFooF);
			IKeyboardDefinition keyboardFoo = Keyboard.Controller.CreateKeyboardDefinition("en-US_foo", KeyboardFormat.Unknown, null);
			KeyboardController.Instance.Keyboards.Add(keyboardFoo);

			Assert.That(Keyboard.Controller.GetKeyboard("foo-az-en-US"), Is.EqualTo(keyboardFooF));
			Assert.That(Keyboard.Controller.GetKeyboard("foo-en-US"), Is.EqualTo(keyboardFoo));
			Assert.That(Keyboard.Controller.GetKeyboard("foo-az-Latn-AZ"), Is.EqualTo(keyboardFooBoo));
		}

		[Test]
		public void GetKeyboard_FromOldPalasoId_NonExistingKeyboard()
		{
			var keyboard = Keyboard.Controller.CreateKeyboardDefinition("en-US_foo", KeyboardFormat.Unknown, null);
			KeyboardController.Instance.Keyboards.Add(keyboard);

			Assert.That(Keyboard.Controller.GetKeyboard("glop-en-US"), Is.EqualTo(KeyboardDescription.Zero));
		}

		[Test]
		public void GetKeyboard_FromInputLanguage_NonExistingKeyboard()
		{
			var inputLanguage = new InputLanguageWrapper(new CultureInfo("en-US"), IntPtr.Zero, "foo");
			Assert.That(Keyboard.Controller.GetKeyboard(inputLanguage), Is.EqualTo(KeyboardDescription.Zero));
		}

		[Test]
		public void DefaultForWritingSystem_NullInput_ReturnsSystemDefault()
		{
			Assert.That(Keyboard.Controller.DefaultForWritingSystem(null),
				Is.EqualTo(KeyboardController.Instance.Adaptors[0].DefaultKeyboard));
		}

		[Test]
		public void DefaultForWritingSystem_NoLegacyKeyboardSet_ReturnsSystemDefault()
		{
			var ws = new WritingSystemDefinition();
			Assert.That(Keyboard.Controller.DefaultForWritingSystem(ws),
				Is.EqualTo(KeyboardController.Instance.Adaptors[0].DefaultKeyboard));
		}

		[Test]
		public void DefaultForWritingSystem_OldPalasoWinIMEKeyboard()
		{
			var expectedKeyboard = new KeyboardDescription("en-US_foo", "foo - English (US)", "foo", "en-US", true,
				KeyboardController.Instance.Adaptors[0]);
			KeyboardController.Instance.Keyboards.Add(expectedKeyboard);

			// Palaso sets the keyboard property for Windows system keyboards to <layoutname>-<locale>
			var ws = new WritingSystemDefinition {Keyboard = "foo-en-US"};
			Assert.That(Keyboard.Controller.DefaultForWritingSystem(ws), Is.EqualTo(expectedKeyboard));
		}

		[Test]
		public void DefaultForWritingSystem_OldPalasoKeymanKeyboard()
		{
			IKeyboardDefinition expectedKeyboard;
			if (!KeyboardController.Instance.Keyboards.TryGetKeyboardDefinition("IPA Unicode 1.1.1", out expectedKeyboard))
			{
				expectedKeyboard = new KeyboardDescription("IPA Unicode 1.1.1", "IPA Unicode 1.1.1 - English (US)", "IPA Unicode 1.1.1", string.Empty, true,
					KeyboardController.Instance.Adaptors[0]);
				KeyboardController.Instance.Keyboards.Add(expectedKeyboard);
			}

			// Palaso sets the keyboard property for Keyman keyboards to <layoutname>
			var ws = new WritingSystemDefinition {Keyboard = "IPA Unicode 1.1.1"};
			Assert.That(Keyboard.Controller.DefaultForWritingSystem(ws), Is.EqualTo(expectedKeyboard));
		}

		[Test]
		public void DefaultForWritingSystem_OldPalasoIbusKeyboard()
		{
			#if __MonoCS__
			// For this test on Linux we only use the XkbKeyboardAdaptor and simulate an available
			// IBus keyboard. This is necessary because otherwise the test might return an
			// installed Danish IBus keyboard (m17n:da:post) instead of our expected dummy one.
			KeyboardController.Instance.SetKeyboardAdaptors(new XkbKeyboardAdaptor());
			#endif
			var expectedKeyboard = new KeyboardDescription("m17n:da:post", "m17n:da:post - English (US)", "m17n:da:post", string.Empty, true,
				KeyboardController.Instance.Adaptors[0]);
			KeyboardController.Instance.Keyboards.Add(expectedKeyboard);

			// Palaso sets the keyboard property for Ibus keyboards to <ibus longname>
			var ws = new WritingSystemDefinition {Keyboard = "m17n:da:post"};
			Assert.That(Keyboard.Controller.DefaultForWritingSystem(ws), Is.EqualTo(expectedKeyboard));
		}

		[Test]
		public void DefaultForWritingSystem_OldFwSystemKeyboard()
		{
			// 0x001C is Albanian (see http://msdn.microsoft.com/en-us/goglobal/bb896001.aspx).
			// Make sure it's not installed on current system.
			if (InputLanguage.InstalledInputLanguages.Cast<InputLanguage>().Any(lang => lang.Culture.LCID == 0x041C))
				Assert.Ignore("Input language 'Albanian (Albania)' is installed on current system. Can't run this test.");

			var inputLanguage = new InputLanguageWrapper("sq-AL", IntPtr.Zero, "US");
			var expectedKeyboard = new KeyboardDescription("sq-AL_US", "US - Albanian (Albania)", "US", "sq-AL", true,
				KeyboardController.Instance.Adaptors[0]) {InputLanguage = inputLanguage};
			KeyboardController.Instance.Keyboards.Add(expectedKeyboard);

			// FieldWorks sets the WindowsLcid property for System keyboards to <lcid>
			var ws = new WritingSystemDefinition {WindowsLcid = 0x041C.ToString(CultureInfo.InvariantCulture)};
			Assert.That(Keyboard.Controller.DefaultForWritingSystem(ws), Is.EqualTo(expectedKeyboard));
		}<commit_msg>Fix keyboard controller tests on Linux<commit_after>// Copyright (c) 2013, SIL International.
// Distributable under the terms of the MIT license (http://opensource.org/licenses/MIT).

using System;
using System.Globalization;
using System.Linq;
using System.Windows.Forms;
using NUnit.Framework;
using SIL.WritingSystems.WindowsForms.Keyboarding;
#if __MonoCS__
using SIL.WritingSystems.WindowsForms.Keyboarding.Linux;
#endif

namespace SIL.WritingSystems.WindowsForms.Tests.Keyboarding
{
	[TestFixture]
	public class KeyboardControllerTests
	{
		[SetUp]
		public void Setup()
		{
			KeyboardController.Initialize();
		}

		[TearDown]
		public void TearDown()
		{
			KeyboardController.Shutdown();
		}

		[Test]
		public void CreateKeyboardDefinition_ExistingKeyboard_ReturnsReference()
		{
			var expectedKeyboard = new KeyboardDescription("en-US_foo", "foo - English (US)", "foo", "en-US", true,
				KeyboardController.Instance.Adaptors[0]);
			KeyboardController.Instance.Keyboards.Add(expectedKeyboard);
			IKeyboardDefinition keyboard = Keyboard.Controller.CreateKeyboardDefinition("en-US_foo", KeyboardFormat.Unknown, null);
			Assert.That(keyboard, Is.SameAs(expectedKeyboard));
			Assert.That(keyboard, Is.TypeOf<KeyboardDescription>());
			Assert.That(keyboard.Name, Is.EqualTo("foo - English (US)"));
		}

		[Test]
		public void GetKeyboard_FromInputLanguage_ExistingKeyboardReturnsKeyboard()
		{
			var keyboard = Keyboard.Controller.CreateKeyboardDefinition("en-US_foo", KeyboardFormat.Unknown, null);
			KeyboardController.Instance.Keyboards.Add(keyboard);

			var inputLanguage = new InputLanguageWrapper(new CultureInfo("en-US"), IntPtr.Zero, "foo");
			Assert.That(Keyboard.Controller.GetKeyboard(inputLanguage), Is.EqualTo(keyboard));
		}

		[Test]
		public void GetKeyboard_FromNewPalasoId_ExistingKeyboardReturnsKeyboard()
		{
			var keyboard = Keyboard.Controller.CreateKeyboardDefinition("en-US_foo", KeyboardFormat.Unknown, null);
			KeyboardController.Instance.Keyboards.Add(keyboard);

			Assert.That(Keyboard.Controller.GetKeyboard("en-US_foo"), Is.EqualTo(keyboard));
		}

		[Test]
		public void GetKeyboard_FromNewPalasoId_NonExistingKeyboard()
		{
			var keyboard = Keyboard.Controller.CreateKeyboardDefinition("en-US_foo", KeyboardFormat.Unknown, null);
			KeyboardController.Instance.Keyboards.Add(keyboard);

			Assert.That(Keyboard.Controller.GetKeyboard("en-US_glop"), Is.EqualTo(KeyboardDescription.Zero));
		}

		[Test]
		public void GetKeyboard_FromOldParatextId_ExistingKeyboardReturnsKeyboard()
		{
			// This is the case of a keyboard that the old Palaso system was incapable of supporting.
			var keyboardFooBoo = Keyboard.Controller.CreateKeyboardDefinition("az-Latn-AZ_foo", KeyboardFormat.Unknown, null);
			KeyboardController.Instance.Keyboards.Add(keyboardFooBoo);

			var keyboard = Keyboard.Controller.CreateKeyboardDefinition("en-US_foo", KeyboardFormat.Unknown, null);
			KeyboardController.Instance.Keyboards.Add(keyboard);

			Assert.That(Keyboard.Controller.GetKeyboard("foo|en-US"), Is.EqualTo(keyboard));
			Assert.That(Keyboard.Controller.GetKeyboard("foo|az-Latn-AZ"), Is.EqualTo(keyboardFooBoo));
		}

		[Test]
		public void GetKeyboard_FromOldParatextId_NonExistingKeyboard()
		{
			var keyboard = Keyboard.Controller.CreateKeyboardDefinition("en-US_foo", KeyboardFormat.Unknown, null);
			KeyboardController.Instance.Keyboards.Add(keyboard);

			Assert.That(Keyboard.Controller.GetKeyboard("glop|en-US"), Is.EqualTo(KeyboardDescription.Zero));
		}

		[Test]
		public void GetKeyboard_FromOldPalasoId_ExistingKeyboardReturnsKeyboard()
		{
			// This demonstrates the case of a keyboard that the old Palaso system was incapable of supporting.
			// There's no way to reference this keyboard, or is there???
			IKeyboardDefinition keyboardFooBoo = Keyboard.Controller.CreateKeyboardDefinition("az-Latn-AZ_foo", KeyboardFormat.Unknown, null);
			KeyboardController.Instance.Keyboards.Add(keyboardFooBoo);

			IKeyboardDefinition keyboardFooF = Keyboard.Controller.CreateKeyboardDefinition("en-US_foo-az", KeyboardFormat.Unknown, null);
			KeyboardController.Instance.Keyboards.Add(keyboardFooF);
			IKeyboardDefinition keyboardFoo = Keyboard.Controller.CreateKeyboardDefinition("en-US_foo", KeyboardFormat.Unknown, null);
			KeyboardController.Instance.Keyboards.Add(keyboardFoo);

			Assert.That(Keyboard.Controller.GetKeyboard("foo-az-en-US"), Is.EqualTo(keyboardFooF));
			Assert.That(Keyboard.Controller.GetKeyboard("foo-en-US"), Is.EqualTo(keyboardFoo));
			Assert.That(Keyboard.Controller.GetKeyboard("foo-az-Latn-AZ"), Is.EqualTo(keyboardFooBoo));
		}

		[Test]
		public void GetKeyboard_FromOldPalasoId_NonExistingKeyboard()
		{
			var keyboard = Keyboard.Controller.CreateKeyboardDefinition("en-US_foo", KeyboardFormat.Unknown, null);
			KeyboardController.Instance.Keyboards.Add(keyboard);

			Assert.That(Keyboard.Controller.GetKeyboard("glop-en-US"), Is.EqualTo(KeyboardDescription.Zero));
		}

		[Test]
		public void GetKeyboard_FromInputLanguage_NonExistingKeyboard()
		{
			var inputLanguage = new InputLanguageWrapper(new CultureInfo("en-US"), IntPtr.Zero, "foo");
			Assert.That(Keyboard.Controller.GetKeyboard(inputLanguage), Is.EqualTo(KeyboardDescription.Zero));
		}

		[Test]
		public void DefaultForWritingSystem_NullInput_ReturnsSystemDefault()
		{
			Assert.That(Keyboard.Controller.DefaultForWritingSystem(null),
				Is.EqualTo(KeyboardController.Instance.Adaptors[0].DefaultKeyboard));
		}

		[Test]
		public void DefaultForWritingSystem_NoLegacyKeyboardSet_ReturnsSystemDefault()
		{
			var ws = new WritingSystemDefinition();
			Assert.That(Keyboard.Controller.DefaultForWritingSystem(ws),
				Is.EqualTo(KeyboardController.Instance.Adaptors[0].DefaultKeyboard));
		}

		[Test]
		public void DefaultForWritingSystem_OldPalasoWinIMEKeyboard()
		{
			var expectedKeyboard = new KeyboardDescription("en-US_foo", "foo - English (US)", "foo", "en-US", true,
				KeyboardController.Instance.Adaptors[0]);
			KeyboardController.Instance.Keyboards.Add(expectedKeyboard);

			// Palaso sets the keyboard property for Windows system keyboards to <layoutname>-<locale>
			var ws = new WritingSystemDefinition {Keyboard = "foo-en-US"};
			Assert.That(Keyboard.Controller.DefaultForWritingSystem(ws), Is.EqualTo(expectedKeyboard));
		}

		[Test]
		public void DefaultForWritingSystem_OldPalasoKeymanKeyboard()
		{
			IKeyboardDefinition expectedKeyboard;
			if (!KeyboardController.Instance.Keyboards.TryGetKeyboardDefinition("IPA Unicode 1.1.1", out expectedKeyboard))
			{
				expectedKeyboard = new KeyboardDescription("IPA Unicode 1.1.1", "IPA Unicode 1.1.1 - English (US)", "IPA Unicode 1.1.1", string.Empty, true,
					KeyboardController.Instance.Adaptors[0]);
				KeyboardController.Instance.Keyboards.Add(expectedKeyboard);
			}

			// Palaso sets the keyboard property for Keyman keyboards to <layoutname>
			var ws = new WritingSystemDefinition {Keyboard = "IPA Unicode 1.1.1"};
			Assert.That(Keyboard.Controller.DefaultForWritingSystem(ws), Is.EqualTo(expectedKeyboard));
		}

		[Test]
		public void DefaultForWritingSystem_OldPalasoIbusKeyboard()
		{
#if __MonoCS__
			// For this test on Linux we only use the XkbKeyboardAdaptor and simulate an available
			// IBus keyboard. This is necessary because otherwise the test might return an
			// installed Danish IBus keyboard (m17n:da:post) instead of our expected dummy one.
			KeyboardController.Instance.SetKeyboardAdaptors(new XkbKeyboardAdaptor());
#endif
			var expectedKeyboard = new KeyboardDescription("m17n:da:post", "m17n:da:post - English (US)", "m17n:da:post", string.Empty, true,
				KeyboardController.Instance.Adaptors[0]);
			KeyboardController.Instance.Keyboards.Add(expectedKeyboard);

			// Palaso sets the keyboard property for Ibus keyboards to <ibus longname>
			var ws = new WritingSystemDefinition {Keyboard = "m17n:da:post"};
			Assert.That(Keyboard.Controller.DefaultForWritingSystem(ws), Is.EqualTo(expectedKeyboard));
		}

		[Test]
		public void DefaultForWritingSystem_OldFwSystemKeyboard()
		{
			// 0x001C is Albanian (see http://msdn.microsoft.com/en-us/goglobal/bb896001.aspx).
			// Make sure it's not installed on current system.
			if (InputLanguage.InstalledInputLanguages.Cast<InputLanguage>().Any(lang => lang.Culture.LCID == 0x041C))
				Assert.Ignore("Input language 'Albanian (Albania)' is installed on current system. Can't run this test.");

			var inputLanguage = new InputLanguageWrapper("sq-AL", IntPtr.Zero, "US");
			var expectedKeyboard = new KeyboardDescription("sq-AL_US", "US - Albanian (Albania)", "US", "sq-AL", true,
				KeyboardController.Instance.Adaptors[0]) {InputLanguage = inputLanguage};
			KeyboardController.Instance.Keyboards.Add(expectedKeyboard);

			// FieldWorks sets the WindowsLcid property for System keyboards to <lcid>
			var ws = new WritingSystemDefinition {WindowsLcid = 0x041C.ToString(CultureInfo.InvariantCulture)};
			Assert.That(Keyboard.Controller.DefaultForWritingSystem(ws), Is.EqualTo(expectedKeyboard));
		}
{'fraction_non_alphanumeric': 0.1118596881959911, 'fraction_numerical': 0.0060133630289532294, 'mean_word_length': 5.669513553657631, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '535916', 'n_tokens_mistral': 5900, 'n_tokens_neox': 5448, 'n_words': 992}