Merge "Revert "Revert "Example for disabling changes at test time""" am: f72b72b126

am: b5d55fad69

Change-Id: Iefc4693e6341a37d5718dda26801c22bfc6acbe9
This commit is contained in:
Andrei-Valentin Onea
2019-11-07 10:24:53 -08:00
committed by android-build-merger
5 changed files with 244 additions and 0 deletions

17
tests/Gating/Android.bp Normal file
View File

@@ -0,0 +1,17 @@
android_test {
name: "Gating",
// Only compile source java files in this apk.
srcs: ["src/**/*.java"],
certificate: "platform",
libs: [
"android.test.runner",
"android.test.base",
],
static_libs: [
"junit",
"android-support-test",
"mockito-target-minus-junit4",
"truth-prebuilt",
"platform_compat-test-rules"
],
}

View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.tests.gating">
<application android:label="GatingTest">
<uses-library android:name="android.test.runner" />
</application>
<instrumentation android:name="android.support.test.runner.AndroidJUnitRunner"
android:targetPackage="com.android.tests.gating"/>
</manifest>

View File

@@ -0,0 +1,30 @@
<!-- Copyright (C) 2018 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<configuration description="Test compatibility change gating.">
<target_preparer class="com.android.tradefed.targetprep.TestFilePushSetup"/>
<target_preparer class="com.android.tradefed.targetprep.TestAppInstallSetup">
<option name="test-file-name" value="Gating.apk"/>
</target_preparer>
<target_preparer class="com.android.tradefed.targetprep.PushFilePreparer"/>
<target_preparer class="com.android.tradefed.targetprep.RunCommandTargetPreparer"/>
<option name="test-suite-tag" value="apct"/>
<option name="test-tag" value="Gating"/>
<test class="com.android.tradefed.testtype.AndroidJUnitTest">
<option name="package" value="com.android.tests.gating"/>
<option name="runner" value="android.support.test.runner.AndroidJUnitRunner"/>
<option name="hidden-api-checks" value="false"/>
</test>
</configuration>

View File

@@ -0,0 +1,91 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.compat.testing;
import android.compat.Compatibility;
import android.content.Context;
import android.os.RemoteException;
import android.os.ServiceManager;
import com.android.internal.compat.IPlatformCompat;
/**
* This is a dummy API to test gating
*
* @hide
*/
public class DummyApi {
public static final long CHANGE_ID = 666013;
public static final long CHANGE_ID_1 = 666014;
public static final long CHANGE_ID_2 = 666015;
public static final long CHANGE_SYSTEM_SERVER = 666016;
/**
* Dummy method
* @return "A" if change is enabled, "B" otherwise.
*/
public static String dummyFunc() {
if (Compatibility.isChangeEnabled(CHANGE_ID)) {
return "A";
}
return "B";
}
/**
* Dummy combined method
* @return "0" if {@link CHANGE_ID_1} is disabled and {@link CHANGE_ID_2} is disabled,
"1" if {@link CHANGE_ID_1} is disabled and {@link CHANGE_ID_2} is enabled,
"2" if {@link CHANGE_ID_1} is enabled and {@link CHANGE_ID_2} is disabled,
"3" if {@link CHANGE_ID_1} is enabled and {@link CHANGE_ID_2} is enabled.
*/
public static String dummyCombinedFunc() {
if (!Compatibility.isChangeEnabled(CHANGE_ID_1)
&& !Compatibility.isChangeEnabled(CHANGE_ID_2)) {
return "0";
} else if (!Compatibility.isChangeEnabled(CHANGE_ID_1)
&& Compatibility.isChangeEnabled(CHANGE_ID_2)) {
return "1";
} else if (Compatibility.isChangeEnabled(CHANGE_ID_1)
&& !Compatibility.isChangeEnabled(CHANGE_ID_2)) {
return "2";
}
return "3";
}
/**
* Dummy api using system server API.
*/
public static String dummySystemServer(Context context) {
IPlatformCompat platformCompat = IPlatformCompat.Stub
.asInterface(ServiceManager.getService(Context.PLATFORM_COMPAT_SERVICE));
if (platformCompat == null) {
throw new RuntimeException("Could not obtain IPlatformCompat instance!");
}
String packageName = context.getPackageName();
try {
if (platformCompat.isChangeEnabledByPackageName(CHANGE_SYSTEM_SERVER, packageName,
context.getUserId())) {
return "True";
} else {
return "False";
}
} catch (RemoteException e) {
throw new RuntimeException("Could not get change value!", e);
}
}
}

View File

@@ -0,0 +1,95 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.tests.gating;
import static com.google.common.truth.Truth.assertThat;
import android.compat.testing.CompatChangeRule;
import android.compat.testing.CompatChangeRule.DisableCompatChanges;
import android.compat.testing.CompatChangeRule.EnableCompatChanges;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import com.android.compat.testing.DummyApi;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;
import org.junit.runner.RunWith;
/**
* Tests for platform compatibility change gating.
*/
@RunWith(AndroidJUnit4.class)
public class GatingTest {
@Rule
public TestRule compatChangeRule = new CompatChangeRule();
@Test
@EnableCompatChanges({DummyApi.CHANGE_ID})
public void testDummyGatingPositive() {
assertThat(DummyApi.dummyFunc()).isEqualTo("A");
}
@Test
@DisableCompatChanges({DummyApi.CHANGE_ID})
public void testDummyGatingNegative() {
assertThat(DummyApi.dummyFunc()).isEqualTo("B");
}
@Test
@DisableCompatChanges({DummyApi.CHANGE_ID_1, DummyApi.CHANGE_ID_2})
public void testDummyGatingCombined0() {
assertThat(DummyApi.dummyCombinedFunc()).isEqualTo("0");
}
@Test
@DisableCompatChanges({DummyApi.CHANGE_ID_1})
@EnableCompatChanges({DummyApi.CHANGE_ID_2})
public void testDummyGatingCombined1() {
assertThat(DummyApi.dummyCombinedFunc()).isEqualTo("1");
}
@Test
@EnableCompatChanges({DummyApi.CHANGE_ID_1})
@DisableCompatChanges({DummyApi.CHANGE_ID_2})
public void testDummyGatingCombined2() {
assertThat(DummyApi.dummyCombinedFunc()).isEqualTo("2");
}
@Test
@EnableCompatChanges({DummyApi.CHANGE_ID_1, DummyApi.CHANGE_ID_2})
public void testDummyGatingCombined3() {
assertThat(DummyApi.dummyCombinedFunc()).isEqualTo("3");
}
@Test
@EnableCompatChanges({DummyApi.CHANGE_SYSTEM_SERVER})
public void testDummyGatingPositiveSystemServer() {
assertThat(
DummyApi.dummySystemServer(InstrumentationRegistry.getTargetContext())).isEqualTo(
"True");
}
@Test
@DisableCompatChanges({DummyApi.CHANGE_SYSTEM_SERVER})
public void testDummyGatingNegativeSystemServer() {
assertThat(
DummyApi.dummySystemServer(InstrumentationRegistry.getTargetContext())).isEqualTo(
"False");
}
}