Skip to content

Commit

Permalink
Fix crash if the LocalKeyboard is in KnownKeyboards (#1217)
Browse files Browse the repository at this point in the history
This happens only if the LocalKeyboard does not get added to the KeyboardController (e.g. invalid)

See: https://jira.sil.org/browse/LT-21112
  • Loading branch information
jasonleenaylor authored Aug 26, 2022
1 parent e6a4a27 commit 5802fd1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,33 @@ public void Read_ValidXml_SetsAllProperties()
Assert.That(ws3.IsGraphiteEnabled, Is.True);
}

[Test]
public void Read_LocalKeyboardInKnownKeyboards_DoesNotCrash()
{
const string userSettingsXml =
@"<UserLexiconSettings>
<WritingSystems>
<WritingSystem id=""en-US"">
<LocalKeyboard>sil_cameroon_azerty</LocalKeyboard>
<KnownKeyboards>
<KnownKeyboard>sil_cameroon_azerty</KnownKeyboard>
<KnownKeyboard>sil_cameroon_qwerty</KnownKeyboard>
</KnownKeyboards>
<DefaultFontName>Times New Roman</DefaultFontName>
</WritingSystem>
</WritingSystems>
</UserLexiconSettings>";

var userSettingsDataMapper = new UserLexiconSettingsWritingSystemDataMapper(new MemorySettingsStore { SettingsElement = XElement.Parse(userSettingsXml) });

var ws1 = new WritingSystemDefinition("en-US");
userSettingsDataMapper.Read(ws1);

Assert.That(ws1.LocalKeyboard.Id, Is.EqualTo("sil_cameroon_azerty"));
Assert.That(ws1.KnownKeyboards[0].Id, Is.EqualTo("sil_cameroon_azerty"));
Assert.That(ws1.KnownKeyboards[1].Id, Is.EqualTo("sil_cameroon_qwerty"));
}

[Test]
public void Read_EmptyXml_NothingSet()
{
Expand Down
3 changes: 2 additions & 1 deletion SIL.Lexicon/UserLexiconSettingsWritingSystemDataMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public virtual void Read(T ws)
IKeyboardDefinition keyboard;
if (!Keyboard.Controller.TryGetKeyboard(id, out keyboard))
keyboard = Keyboard.Controller.CreateKeyboard(id, KeyboardFormat.Unknown, Enumerable.Empty<string>());
if (!ws.KnownKeyboards.Contains(keyboard))
// Check KnownKeyboards for a keyboard with the same identifier, not for the object we just created
if (!ws.KnownKeyboards.Contains(id))
ws.KnownKeyboards.Add(keyboard);
}
}
Expand Down

0 comments on commit 5802fd1

Please sign in to comment.