mirror of
https://github.com/keymanapp/keyman.git
synced 2026-09-24 00:27:40 +00:00
test(android): add unit tests for getAssociatedLexicalModel
This commit is contained in:
parent
e3ed30a309
commit
0368a7bc56
1 changed files with 51 additions and 0 deletions
|
|
@ -5,12 +5,14 @@ package com.keyman.engine.data;
|
|||
|
||||
import android.content.Context;
|
||||
import androidx.test.core.app.ApplicationProvider;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
|
@ -31,6 +33,12 @@ public class CloudRepositoryTests {
|
|||
return false;
|
||||
}
|
||||
|
||||
private void setMemCachedDataset(Dataset dataset) throws Exception {
|
||||
Field field = CloudRepository.class.getDeclaredField("memCachedDataset");
|
||||
field.setAccessible(true);
|
||||
field.set(repository, dataset);
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
context = ApplicationProvider.getApplicationContext();
|
||||
|
|
@ -38,6 +46,12 @@ public class CloudRepositoryTests {
|
|||
dataset = new Dataset(context);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
// Reset singleton state
|
||||
setMemCachedDataset(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMergeLexicalModels_PreservesUniqueExistingModels() throws Exception {
|
||||
// Setup
|
||||
|
|
@ -95,4 +109,41 @@ public class CloudRepositoryTests {
|
|||
LexicalModel result = dataset.lexicalModels.getItem(0);
|
||||
Assert.assertEquals("1.2", result.getVersion());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAssociatedLexicalModel_ReturnsCorrectModel() throws Exception {
|
||||
// Setup
|
||||
LexicalModel model1 = new LexicalModel("pkg1", "model1", "Model 1", "en", "English", "1.0", "", "");
|
||||
LexicalModel model2 = new LexicalModel("pkg2", "model2", "Model 2", "fr", "French", "1.0", "", "");
|
||||
dataset.lexicalModels.add(model1);
|
||||
dataset.lexicalModels.add(model2);
|
||||
setMemCachedDataset(dataset);
|
||||
|
||||
// Execute & Verify
|
||||
Assert.assertEquals(model1, repository.getAssociatedLexicalModel(context, "en"));
|
||||
Assert.assertEquals(model2, repository.getAssociatedLexicalModel(context, "fr"));
|
||||
Assert.assertEquals(model1, repository.getAssociatedLexicalModel(context, "EN")); // Case insensitivity
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAssociatedLexicalModel_ReturnsNullWhenDatasetIsNull() throws Exception {
|
||||
// Setup
|
||||
setMemCachedDataset(null);
|
||||
|
||||
// Execute & Verify
|
||||
Assert.assertNull(repository.getAssociatedLexicalModel(context, "en"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAssociatedLexicalModel_ReturnsFirstMatch() throws Exception {
|
||||
// Setup
|
||||
LexicalModel model1 = new LexicalModel("pkg1", "model1", "Model 1", "en", "English", "1.0", "", "");
|
||||
LexicalModel model2 = new LexicalModel("pkg2", "model2", "Model 2", "en", "English", "1.0", "", "");
|
||||
dataset.lexicalModels.add(model1);
|
||||
dataset.lexicalModels.add(model2);
|
||||
setMemCachedDataset(dataset);
|
||||
|
||||
// Execute & Verify
|
||||
Assert.assertEquals(model1, repository.getAssociatedLexicalModel(context, "en"));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue