1 /* 2 * Copyright (C) 2022 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 android.appenumeration.cts; 18 19 import static android.appenumeration.cts.Constants.ACTION_GET_ENABLED_SPELL_CHECKER_INFOS; 20 import static android.appenumeration.cts.Constants.CTS_MOCK_SPELL_CHECKER_APK; 21 import static android.appenumeration.cts.Constants.MOCK_SPELL_CHECKER_PKG; 22 import static android.appenumeration.cts.Constants.QUERIES_NOTHING; 23 import static android.appenumeration.cts.Constants.QUERIES_PACKAGE; 24 import static android.appenumeration.cts.Utils.installPackage; 25 import static android.appenumeration.cts.Utils.uninstallPackage; 26 import static android.content.Intent.EXTRA_RETURN_RESULT; 27 28 import static org.junit.Assume.assumeTrue; 29 30 import android.os.Bundle; 31 import android.view.textservice.SpellCheckerInfo; 32 import android.view.textservice.TextServicesManager; 33 34 import androidx.test.ext.junit.runners.AndroidJUnit4; 35 36 import com.android.compatibility.common.util.PollingCheck; 37 38 import org.junit.AfterClass; 39 import org.junit.BeforeClass; 40 import org.junit.Test; 41 import org.junit.runner.RunWith; 42 43 import java.util.List; 44 45 @RunWith(AndroidJUnit4.class) 46 public class TextServicesEnumerationTests extends AppEnumerationTestsBase { 47 48 @BeforeClass prepareSpellChecker()49 public static void prepareSpellChecker() throws Exception { 50 if (!hasTextServicesManager()) { 51 return; 52 } 53 54 installPackage(CTS_MOCK_SPELL_CHECKER_APK); 55 56 PollingCheck.check("Failed to wait for " + MOCK_SPELL_CHECKER_PKG 57 + " getting ready", DEFAULT_TIMEOUT_MS, 58 () -> hasSpellChecker(MOCK_SPELL_CHECKER_PKG)); 59 } 60 61 @AfterClass cleanUp()62 public static void cleanUp() throws Exception { 63 uninstallPackage(MOCK_SPELL_CHECKER_PKG); 64 } 65 66 @Test queriesNothing_getEnabledSpellCheckerInfos_cannotSeeMockSpellChecker()67 public void queriesNothing_getEnabledSpellCheckerInfos_cannotSeeMockSpellChecker() 68 throws Exception { 69 assumeTrue(hasTextServicesManager()); 70 assertNotVisible( 71 QUERIES_NOTHING, MOCK_SPELL_CHECKER_PKG, this::getEnabledSpellCheckerInfos); 72 } 73 74 @Test queriesPackage_getEnabledSpellCheckerInfos_canSeeMockSpellChecker()75 public void queriesPackage_getEnabledSpellCheckerInfos_canSeeMockSpellChecker() 76 throws Exception { 77 assumeTrue(hasTextServicesManager()); 78 assertVisible( 79 QUERIES_PACKAGE, MOCK_SPELL_CHECKER_PKG, this::getEnabledSpellCheckerInfos); 80 } 81 getEnabledSpellCheckerInfos(String sourcePackage)82 private String[] getEnabledSpellCheckerInfos(String sourcePackage) 83 throws Exception { 84 final Bundle response = sendCommandBlocking(sourcePackage, null /* targetPackageName */, 85 null /* intentExtra */, ACTION_GET_ENABLED_SPELL_CHECKER_INFOS); 86 final List<SpellCheckerInfo> infos = 87 response.getParcelableArrayList(EXTRA_RETURN_RESULT, SpellCheckerInfo.class); 88 return infos.stream() 89 .map(info -> info.getPackageName()) 90 .distinct() 91 .toArray(String[]::new); 92 } 93 hasTextServicesManager()94 private static boolean hasTextServicesManager() { 95 return sContext.getSystemService(TextServicesManager.class) != null; 96 } 97 hasSpellChecker(String packageName)98 private static boolean hasSpellChecker(String packageName) { 99 final List<SpellCheckerInfo> spellCheckerInfos = sContext.getSystemService( 100 TextServicesManager.class).getEnabledSpellCheckerInfos(); 101 return spellCheckerInfos.stream() 102 .anyMatch(info -> info.getPackageName().equals(packageName)); 103 } 104 } 105