Skip to content

Commit 5802fd1

Browse files
Fix crash if the LocalKeyboard is in KnownKeyboards (#1217)
This happens only if the LocalKeyboard does not get added to the KeyboardController (e.g. invalid) See: https://jira.sil.org/browse/LT-21112
1 parent e6a4a27 commit 5802fd1

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

SIL.Lexicon.Tests/UserLexiconSettingsWritingSystemDataMapperTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,33 @@ public void Read_ValidXml_SetsAllProperties()
6060
Assert.That(ws3.IsGraphiteEnabled, Is.True);
6161
}
6262

63+
[Test]
64+
public void Read_LocalKeyboardInKnownKeyboards_DoesNotCrash()
65+
{
66+
const string userSettingsXml =
67+
@"<UserLexiconSettings>
68+
<WritingSystems>
69+
<WritingSystem id=""en-US"">
70+
<LocalKeyboard>sil_cameroon_azerty</LocalKeyboard>
71+
<KnownKeyboards>
72+
<KnownKeyboard>sil_cameroon_azerty</KnownKeyboard>
73+
<KnownKeyboard>sil_cameroon_qwerty</KnownKeyboard>
74+
</KnownKeyboards>
75+
<DefaultFontName>Times New Roman</DefaultFontName>
76+
</WritingSystem>
77+
</WritingSystems>
78+
</UserLexiconSettings>";
79+
80+
var userSettingsDataMapper = new UserLexiconSettingsWritingSystemDataMapper(new MemorySettingsStore { SettingsElement = XElement.Parse(userSettingsXml) });
81+
82+
var ws1 = new WritingSystemDefinition("en-US");
83+
userSettingsDataMapper.Read(ws1);
84+
85+
Assert.That(ws1.LocalKeyboard.Id, Is.EqualTo("sil_cameroon_azerty"));
86+
Assert.That(ws1.KnownKeyboards[0].Id, Is.EqualTo("sil_cameroon_azerty"));
87+
Assert.That(ws1.KnownKeyboards[1].Id, Is.EqualTo("sil_cameroon_qwerty"));
88+
}
89+
6390
[Test]
6491
public void Read_EmptyXml_NothingSet()
6592
{

SIL.Lexicon/UserLexiconSettingsWritingSystemDataMapper.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ public virtual void Read(T ws)
4949
IKeyboardDefinition keyboard;
5050
if (!Keyboard.Controller.TryGetKeyboard(id, out keyboard))
5151
keyboard = Keyboard.Controller.CreateKeyboard(id, KeyboardFormat.Unknown, Enumerable.Empty<string>());
52-
if (!ws.KnownKeyboards.Contains(keyboard))
52+
// Check KnownKeyboards for a keyboard with the same identifier, not for the object we just created
53+
if (!ws.KnownKeyboards.Contains(id))
5354
ws.KnownKeyboards.Add(keyboard);
5455
}
5556
}

0 commit comments

Comments
 (0)