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