| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.compat.testing; |
| 18 |
|
| 19 |
import android.Manifest; |
| 20 |
import android.app.Instrumentation; |
| 21 |
import android.app.UiAutomation; |
| 22 |
import android.compat.Compatibility; |
| 23 |
import android.compat.Compatibility.ChangeConfig; |
| 24 |
import android.content.Context; |
| 25 |
import android.os.RemoteException; |
| 26 |
import android.os.ServiceManager; |
| 27 |
|
| 28 |
import androidx.test.platform.app.InstrumentationRegistry; |
| 29 |
|
| 30 |
|
| 31 |
import com.android.internal.compat.CompatibilityChangeConfig; |
| 32 |
import com.android.internal.compat.IPlatformCompat; |
| 33 |
|
| 34 |
import libcore.junit.util.compat.CoreCompatChangeRule; |
| 35 |
|
| 36 |
import org.junit.runners.model.Statement; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public class PlatformCompatChangeRule extends CoreCompatChangeRule { |
| 71 |
|
| 72 |
@Override |
| 73 |
protected Statement createStatementForConfig(final Statement statement, ChangeConfig config) { |
| 74 |
return new CompatChangeStatement(statement, config); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
private static class CompatChangeStatement extends Statement { |
| 79 |
private final Statement mTestStatement; |
| 80 |
private final ChangeConfig mConfig; |
| 81 |
|
| 82 |
private CompatChangeStatement(Statement testStatement, ChangeConfig config) { |
| 83 |
this.mTestStatement = testStatement; |
| 84 |
this.mConfig = config; |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
public void evaluate() throws Throwable { |
| 89 |
Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); |
| 90 |
UiAutomation uiAutomation = instrumentation.getUiAutomation(); |
| 91 |
String packageName = instrumentation.getTargetContext().getPackageName(); |
| 92 |
IPlatformCompat platformCompat = IPlatformCompat.Stub |
| 93 |
.asInterface(ServiceManager.getService(Context.PLATFORM_COMPAT_SERVICE)); |
| 94 |
if (platformCompat == null) { |
| 95 |
throw new IllegalStateException("Could not get IPlatformCompat service!"); |
| 96 |
} |
| 97 |
adoptShellPermissions(uiAutomation); |
| 98 |
Compatibility.setOverrides(mConfig); |
| 99 |
try { |
| 100 |
platformCompat.setOverridesForTest(new CompatibilityChangeConfig(mConfig), |
| 101 |
packageName); |
| 102 |
try { |
| 103 |
mTestStatement.evaluate(); |
| 104 |
} finally { |
| 105 |
adoptShellPermissions(uiAutomation); |
| 106 |
platformCompat.clearOverridesForTest(packageName); |
| 107 |
} |
| 108 |
} catch (RemoteException e) { |
| 109 |
throw new RuntimeException("Could not call IPlatformCompat binder method!", e); |
| 110 |
} finally { |
| 111 |
uiAutomation.dropShellPermissionIdentity(); |
| 112 |
Compatibility.clearOverrides(); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
private static void adoptShellPermissions(UiAutomation uiAutomation) { |
| 117 |
uiAutomation.adoptShellPermissionIdentity( |
| 118 |
Manifest.permission.LOG_COMPAT_CHANGE, |
| 119 |
Manifest.permission.OVERRIDE_COMPAT_CHANGE_CONFIG, |
| 120 |
Manifest.permission.OVERRIDE_COMPAT_CHANGE_CONFIG_ON_RELEASE_BUILD, |
| 121 |
Manifest.permission.READ_COMPAT_CHANGE_CONFIG, |
| 122 |
Manifest.permission.INTERACT_ACROSS_USERS_FULL); |
| 123 |
} |
| 124 |
} |
| 125 |
} |