1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.server.inputmethod;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 
22 import android.icu.util.ULocale;
23 import android.util.ArrayMap;
24 import android.util.AtomicFile;
25 import android.view.inputmethod.InputMethodInfo;
26 import android.view.inputmethod.InputMethodSubtype;
27 
28 import androidx.test.platform.app.InstrumentationRegistry;
29 
30 import org.junit.Test;
31 
32 import java.io.File;
33 import java.util.List;
34 
35 public final class AdditionalSubtypeUtilsTest {
36 
37     @Test
testSaveAndLoad()38     public void testSaveAndLoad() throws Exception {
39         // Prepares the data to be saved.
40         InputMethodSubtype subtype1 = new InputMethodSubtype.InputMethodSubtypeBuilder()
41                 .setSubtypeNameOverride("Subtype1")
42                 .setLanguageTag("en-US")
43                 .build();
44         InputMethodSubtype subtype2 = new InputMethodSubtype.InputMethodSubtypeBuilder()
45                 .setSubtypeNameOverride("Subtype2")
46                 .setLanguageTag("zh-CN")
47                 .setPhysicalKeyboardHint(new ULocale("en_US"), "qwerty")
48                 .build();
49         String fakeImeId = "fakeImeId";
50         ArrayMap<String, InputMethodInfo> methodMap = new ArrayMap<>();
51         methodMap.put(fakeImeId, new InputMethodInfo("", "", "", ""));
52         ArrayMap<String, List<InputMethodSubtype>> allSubtypes = new ArrayMap<>();
53         allSubtypes.put(fakeImeId, List.of(subtype1, subtype2));
54 
55         // Save & load.
56         AtomicFile atomicFile = new AtomicFile(
57                 new File(InstrumentationRegistry.getInstrumentation().getContext().getCacheDir(),
58                         "subtypes.xml"));
59         AdditionalSubtypeUtils.saveToFile(AdditionalSubtypeMap.of(allSubtypes),
60                 InputMethodMap.of(methodMap), atomicFile);
61         AdditionalSubtypeMap loadedSubtypes = AdditionalSubtypeUtils.loadFromFile(atomicFile);
62 
63         // Verifies the loaded data.
64         assertEquals(1, loadedSubtypes.size());
65         List<InputMethodSubtype> subtypes = loadedSubtypes.get(fakeImeId);
66         assertNotNull(subtypes);
67         assertEquals(2, subtypes.size());
68 
69         verifySubtype(subtypes.get(0), subtype1);
70         verifySubtype(subtypes.get(1), subtype2);
71     }
72 
verifySubtype(InputMethodSubtype subtype, InputMethodSubtype expectedSubtype)73     private void verifySubtype(InputMethodSubtype subtype, InputMethodSubtype expectedSubtype) {
74         assertEquals(expectedSubtype.getLanguageTag(), subtype.getLanguageTag());
75         assertEquals(expectedSubtype.getPhysicalKeyboardHintLanguageTag(),
76                 subtype.getPhysicalKeyboardHintLanguageTag());
77         assertEquals(expectedSubtype.getPhysicalKeyboardHintLayoutType(),
78                 subtype.getPhysicalKeyboardHintLayoutType());
79     }
80 }
81