1 /* 2 * Copyright (C) 2018 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.textclassifier.testing; 18 19 import android.app.UiAutomation; 20 import android.content.pm.PackageManager; 21 import android.content.pm.PackageManager.NameNotFoundException; 22 import android.provider.DeviceConfig; 23 import android.util.Log; 24 import android.view.textclassifier.TextClassificationManager; 25 import android.view.textclassifier.TextClassifier; 26 import androidx.test.core.app.ApplicationProvider; 27 import androidx.test.platform.app.InstrumentationRegistry; 28 import com.google.common.io.ByteStreams; 29 import java.io.FileInputStream; 30 import java.io.IOException; 31 import org.junit.rules.ExternalResource; 32 33 /** A rule that manages a text classifier that is backed by the ExtServices. */ 34 public final class ExtServicesTextClassifierRule extends ExternalResource { 35 private static final String TAG = "androidtc"; 36 private static final String CONFIG_TEXT_CLASSIFIER_SERVICE_PACKAGE_OVERRIDE = 37 "textclassifier_service_package_override"; 38 private static final String PKG_NAME_GOOGLE_EXTSERVICES = "com.google.android.ext.services"; 39 private static final String PKG_NAME_AOSP_EXTSERVICES = "android.ext.services"; 40 41 private UiAutomation uiAutomation; 42 private DeviceConfig.Properties originalProperties; 43 private DeviceConfig.Properties.Builder newPropertiesBuilder; 44 45 @Override before()46 protected void before() throws Exception { 47 uiAutomation = InstrumentationRegistry.getInstrumentation().getUiAutomation(); 48 uiAutomation.adoptShellPermissionIdentity(); 49 originalProperties = DeviceConfig.getProperties(DeviceConfig.NAMESPACE_TEXTCLASSIFIER); 50 newPropertiesBuilder = 51 new DeviceConfig.Properties.Builder(DeviceConfig.NAMESPACE_TEXTCLASSIFIER) 52 .setString( 53 CONFIG_TEXT_CLASSIFIER_SERVICE_PACKAGE_OVERRIDE, getExtServicesPackageName()); 54 overrideDeviceConfig(); 55 } 56 57 @Override after()58 protected void after() { 59 try { 60 DeviceConfig.setProperties(originalProperties); 61 } catch (Throwable t) { 62 Log.e(TAG, "Failed to reset DeviceConfig", t); 63 } finally { 64 uiAutomation.dropShellPermissionIdentity(); 65 } 66 } 67 addDeviceConfigOverride(String name, String value)68 public void addDeviceConfigOverride(String name, String value) { 69 newPropertiesBuilder.setString(name, value); 70 } 71 72 /** 73 * Overrides the TextClassifier DeviceConfig manually. 74 * 75 * <p>This will clean up all device configs not in newPropertiesBuilder. 76 * 77 * <p>We will need to call this everytime before testing, because DeviceConfig can be synced in 78 * background at anytime. DeviceConfig#setSyncDisabledMode is to disable sync, however it's a 79 * hidden API. 80 */ overrideDeviceConfig()81 public void overrideDeviceConfig() throws Exception { 82 DeviceConfig.setProperties(newPropertiesBuilder.build()); 83 } 84 85 /** Force stop ExtServices. Force-stop-and-start can be helpful to reload some states. */ forceStopExtServices()86 public void forceStopExtServices() { 87 runShellCommand("am force-stop com.google.android.ext.services"); 88 runShellCommand("am force-stop android.ext.services"); 89 } 90 getTextClassifier()91 public TextClassifier getTextClassifier() { 92 TextClassificationManager textClassificationManager = 93 ApplicationProvider.getApplicationContext() 94 .getSystemService(TextClassificationManager.class); 95 textClassificationManager.setTextClassifier(null); // Reset TC overrides 96 return textClassificationManager.getTextClassifier(); 97 } 98 dumpDefaultTextClassifierService()99 public void dumpDefaultTextClassifierService() { 100 runShellCommand( 101 "dumpsys activity service com.google.android.ext.services/" 102 + "com.android.textclassifier.DefaultTextClassifierService"); 103 runShellCommand("cmd device_config list textclassifier"); 104 } 105 enableVerboseLogging()106 public void enableVerboseLogging() { 107 runShellCommand("setprop log.tag.androidtc VERBOSE"); 108 } 109 runShellCommand(String cmd)110 private void runShellCommand(String cmd) { 111 Log.v(TAG, "run shell command: " + cmd); 112 try (FileInputStream output = 113 new FileInputStream(uiAutomation.executeShellCommand(cmd).getFileDescriptor())) { 114 String cmdOutput = new String(ByteStreams.toByteArray(output)); 115 if (!cmdOutput.isEmpty()) { 116 Log.d(TAG, "cmd output: " + cmdOutput); 117 } 118 } catch (IOException ioe) { 119 Log.w(TAG, "failed to get cmd output", ioe); 120 } 121 } 122 getExtServicesPackageName()123 private static String getExtServicesPackageName() { 124 PackageManager packageManager = ApplicationProvider.getApplicationContext().getPackageManager(); 125 try { 126 packageManager.getApplicationInfo(PKG_NAME_GOOGLE_EXTSERVICES, /* flags= */ 0); 127 return PKG_NAME_GOOGLE_EXTSERVICES; 128 } catch (NameNotFoundException e) { 129 return PKG_NAME_AOSP_EXTSERVICES; 130 } 131 } 132 } 133