Directly update DEFAULT_INPUT_METHOD in UiAutomatorTestCase

This CL updates UiAutomatorTestCase so as not to rely on a special
undocumented rule that
  IInputMethodManager#setInputMethod(IBinder token, String id)
can accept null token when the caller has WRITE_SECURE_SETTINGS is
going to be deprecated.

Fix: 114481043
Test: make -j uiautomator.core
Change-Id: If338da51b2c9d62ca4528e6ead16ea711639b775
This commit is contained in:
Yohei Yukawa
2018-12-24 20:15:08 -08:00
parent 2232e86186
commit dc1c5157ae

View File

@@ -16,14 +16,16 @@
package com.android.uiautomator.testrunner;
import android.app.ActivityThread;
import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.SystemClock;
import android.provider.Settings;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager;
import com.android.internal.view.IInputMethodManager;
import com.android.uiautomator.core.UiDevice;
import junit.framework.TestCase;
@@ -44,6 +46,8 @@ public class UiAutomatorTestCase extends TestCase {
private static final String DISABLE_IME = "disable_ime";
private static final String DUMMY_IME_PACKAGE = "com.android.testing.dummyime";
private static final int NOT_A_SUBTYPE_ID = -1;
private UiDevice mUiDevice;
private Bundle mParams;
private IAutomationSupport mAutomationSupport;
@@ -124,9 +128,13 @@ public class UiAutomatorTestCase extends TestCase {
SystemClock.sleep(ms);
}
private void setDummyIme() throws RemoteException {
IInputMethodManager im = IInputMethodManager.Stub.asInterface(ServiceManager
.getService(Context.INPUT_METHOD_SERVICE));
private void setDummyIme() {
Context context = ActivityThread.currentApplication();
if (context == null) {
throw new RuntimeException("ActivityThread.currentApplication() is null.");
}
InputMethodManager im = (InputMethodManager) context.getSystemService(
Context.INPUT_METHOD_SERVICE);
List<InputMethodInfo> infos = im.getInputMethodList();
String id = null;
for (InputMethodInfo info : infos) {
@@ -138,10 +146,17 @@ public class UiAutomatorTestCase extends TestCase {
throw new RuntimeException(String.format(
"Required testing fixture missing: IME package (%s)", DUMMY_IME_PACKAGE));
}
im.setInputMethod(null, id);
if (context.checkSelfPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS)
!= PackageManager.PERMISSION_GRANTED) {
return;
}
ContentResolver resolver = context.getContentResolver();
Settings.Secure.putInt(resolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE,
NOT_A_SUBTYPE_ID);
Settings.Secure.putString(resolver, Settings.Secure.DEFAULT_INPUT_METHOD, id);
}
private void restoreActiveIme() throws RemoteException {
private void restoreActiveIme() {
// TODO: figure out a way to restore active IME
// Currently retrieving active IME requires querying secure settings provider, which is hard
// to do without a Context; so the caveat here is that to make the post test device usable,