Use tip-of-tree robolectric

By adopting robolectric built-from-source, the backup tests are no
longer grafting ToT backup classes on top of SDK 26 framework classes.

Instead, all tests are executed against the SDK built from current
master which includes the backup classes under test.

Bug: 117904612
Test: make -j56 RunFrameworksServicesRoboTests
Change-Id: Iaf911db93f318df46e47b765f94701b36bacfe59
This commit is contained in:
James Lemieux
2018-10-30 16:50:39 -07:00
parent fddeab3668
commit 38d86c2dc5
55 changed files with 291 additions and 813 deletions

View File

@@ -12,10 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.
##############################################################
# FrameworksServicesLib app just for Robolectric test target #
##############################################################
###################################################################
# FrameworksServicesLib app just for Robolectric test target #
###################################################################
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
@@ -32,81 +31,51 @@ LOCAL_STATIC_JAVA_LIBRARIES := \
include $(BUILD_PACKAGE)
##############################################
# FrameworksServices Robolectric test target #
##############################################
###################################################################
# FrameworksServicesLib Robolectric test target. #
###################################################################
include $(CLEAR_VARS)
# Dependency platform-robolectric-android-all-stubs below contains a bunch of Android classes as
# stubs that throw RuntimeExceptions when we use them. The goal is to include hidden APIs that
# weren't included in Robolectric's Android jar files. However, we are testing the framework itself
# here, so if we write stuff that is being used in the tests and exist in
# platform-robolectric-android-all-stubs, the class loader is going to pick up the latter, and thus
# we are going to test what we don't want. To solve this:
#
# 1. If the class being used should be visible to bundled apps:
# => Bypass the stubs target by including them in LOCAL_SRC_FILES and LOCAL_AIDL_INCLUDES
# (if aidl).
#
# 2. If it's not visible:
# => Remove the class from the stubs jar (common/robolectric/android-all/android-all-stubs.jar)
# and add the class path to
# common/robolectric/android-all/android-all-stubs_removed_classes.txt.
#
LOCAL_MODULE := FrameworksServicesRoboTests
INTERNAL_BACKUP := ../../core/java/com/android/internal/backup
LOCAL_SRC_FILES := $(call all-java-files-under, src)
LOCAL_SRC_FILES := \
$(call all-java-files-under, src) \
$(call all-Iaidl-files-under, $(INTERNAL_BACKUP)) \
$(call all-java-files-under, ../../core/java/android/app/backup) \
$(call all-Iaidl-files-under, ../../core/java/android/app/backup) \
$(call all-java-files-under, ../../core/java/android/util/proto) \
../../core/java/android/content/pm/PackageInfo.java \
../../core/java/android/app/IBackupAgent.aidl \
../../core/java/android/util/KeyValueSettingObserver.java \
../../core/java/android/content/pm/PackageParser.java \
../../core/java/android/content/pm/SigningInfo.java
LOCAL_RESOURCE_DIR := \
$(LOCAL_PATH)/res
LOCAL_AIDL_INCLUDES := \
$(call all-Iaidl-files-under, $(INTERNAL_BACKUP)) \
$(call all-Iaidl-files-under, ../../core/java/android/app/backup) \
../../core/java/android/app/IBackupAgent.aidl
LOCAL_JAVA_RESOURCE_DIRS := config
LOCAL_STATIC_JAVA_LIBRARIES := \
platform-robolectric-android-all-stubs \
android-support-test \
guava \
mockito-robolectric-prebuilt \
# Include the testing libraries
LOCAL_JAVA_LIBRARIES := \
platform-test-annotations \
robolectric_android-all-stub \
Robolectric_all-target \
mockito-robolectric-prebuilt \
truth-prebuilt \
testng
LOCAL_JAVA_LIBRARIES := \
junit \
platform-robolectric-3.6.2-prebuilt
LOCAL_INSTRUMENTATION_FOR := FrameworksServicesLib
LOCAL_MODULE := FrameworksServicesRoboTests
LOCAL_MODULE_TAGS := optional
include $(BUILD_STATIC_JAVA_LIBRARY)
###############################################################
# FrameworksServices runner target to run the previous target #
###############################################################
###################################################################
# FrameworksServicesLib runner target to run the previous target. #
###################################################################
include $(CLEAR_VARS)
LOCAL_MODULE := RunFrameworksServicesRoboTests
LOCAL_SDK_VERSION := current
LOCAL_STATIC_JAVA_LIBRARIES := \
FrameworksServicesRoboTests
LOCAL_JAVA_LIBRARIES := \
FrameworksServicesRoboTests \
platform-test-annotations \
robolectric_android-all-stub \
Robolectric_all-target \
mockito-robolectric-prebuilt \
truth-prebuilt \
testng
LOCAL_TEST_PACKAGE := FrameworksServicesLib
LOCAL_INSTRUMENT_SOURCE_DIRS := $(dir $(LOCAL_PATH))backup/java
include prebuilts/misc/common/robolectric/3.6.2/run_robotests.mk
include external/robolectric-shadows/run_robotests.mk

View File

@@ -0,0 +1 @@
sdk=NEWEST_SDK

View File

@@ -22,14 +22,11 @@ import android.app.backup.FullBackup.BackupScheme.PathWithRequiredFlags;
import android.content.Context;
import android.platform.test.annotations.Presubmit;
import com.android.server.testing.FrameworkRobolectricTestRunner;
import com.android.server.testing.SystemLoaderPackages;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.internal.DoNotInstrument;
import java.io.File;
@@ -38,9 +35,7 @@ import java.util.Collection;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@RunWith(FrameworkRobolectricTestRunner.class)
@Config(manifest = Config.NONE, sdk = 26)
@SystemLoaderPackages({"android.app.backup"})
@RunWith(RobolectricTestRunner.class)
@Presubmit
@DoNotInstrument
public class BackupUtilsTest {

View File

@@ -25,22 +25,16 @@ import android.content.Context;
import android.os.Handler;
import android.platform.test.annotations.Presubmit;
import android.provider.Settings;
import android.util.KeyValueSettingObserver;
import com.android.server.testing.FrameworkRobolectricTestRunner;
import com.android.server.testing.SystemLoaderClasses;
import com.android.server.testing.SystemLoaderPackages;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
/** Tests for {@link BackupAgentTimeoutParameters}. */
@RunWith(FrameworkRobolectricTestRunner.class)
@Config(manifest = Config.NONE, sdk = 26)
@SystemLoaderPackages({"com.android.server.backup"})
@SystemLoaderClasses({KeyValueSettingObserver.class})
@RunWith(RobolectricTestRunner.class)
@Presubmit
public class BackupAgentTimeoutParametersTest {
private ContentResolver mContentResolver;

View File

@@ -23,21 +23,15 @@ import android.content.Context;
import android.os.Handler;
import android.platform.test.annotations.Presubmit;
import android.provider.Settings;
import android.util.KeyValueSettingObserver;
import com.android.server.testing.FrameworkRobolectricTestRunner;
import com.android.server.testing.SystemLoaderClasses;
import com.android.server.testing.SystemLoaderPackages;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
@RunWith(FrameworkRobolectricTestRunner.class)
@Config(manifest = Config.NONE, sdk = 26)
@SystemLoaderPackages({"com.android.server.backup"})
@SystemLoaderClasses({KeyValueSettingObserver.class})
@RunWith(RobolectricTestRunner.class)
@Presubmit
public class BackupManagerConstantsTest {
private static final String PACKAGE_NAME = "some.package.name";

View File

@@ -22,7 +22,9 @@ import static com.android.server.backup.testing.TransportData.d2dTransport;
import static com.android.server.backup.testing.TransportData.localTransport;
import static com.android.server.backup.testing.TransportTestUtils.setUpCurrentTransport;
import static com.android.server.backup.testing.TransportTestUtils.setUpTransports;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
@@ -45,24 +47,23 @@ import android.os.PowerManager;
import android.os.PowerSaveState;
import android.platform.test.annotations.Presubmit;
import android.provider.Settings;
import com.android.server.backup.testing.BackupManagerServiceTestUtils;
import com.android.server.backup.testing.TransportData;
import com.android.server.backup.testing.TransportTestUtils.TransportMock;
import com.android.server.backup.transport.TransportNotRegisteredException;
import com.android.server.testing.FrameworkRobolectricTestRunner;
import com.android.server.testing.SystemLoaderPackages;
import com.android.server.testing.shadows.ShadowAppBackupUtils;
import com.android.server.testing.shadows.ShadowBinder;
import com.android.server.testing.shadows.ShadowKeyValueBackupJob;
import com.android.server.testing.shadows.ShadowKeyValueBackupTask;
import java.io.File;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.Implements;
@@ -71,9 +72,11 @@ import org.robolectric.shadows.ShadowLooper;
import org.robolectric.shadows.ShadowPackageManager;
import org.robolectric.shadows.ShadowSettings;
@RunWith(FrameworkRobolectricTestRunner.class)
@Config(manifest = Config.NONE, sdk = 26, shadows = {ShadowAppBackupUtils.class})
@SystemLoaderPackages({"com.android.server.backup"})
import java.io.File;
import java.util.List;
@RunWith(RobolectricTestRunner.class)
@Config(shadows = {ShadowAppBackupUtils.class})
@Presubmit
public class BackupManagerServiceTest {
private static final String TAG = "BMSTest";

View File

@@ -22,19 +22,14 @@ import android.content.Context;
import android.os.Handler;
import android.platform.test.annotations.Presubmit;
import com.android.server.testing.FrameworkRobolectricTestRunner;
import com.android.server.testing.SystemLoaderPackages;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
@RunWith(FrameworkRobolectricTestRunner.class)
@Config(manifest = Config.NONE, sdk = 26)
@SystemLoaderPackages({"com.android.server.backup"})
@RunWith(RobolectricTestRunner.class)
@Presubmit
public class KeyValueBackupJobTest {
private Context mContext;

View File

@@ -56,17 +56,14 @@ import com.android.server.backup.transport.OnTransportRegisteredListener;
import com.android.server.backup.transport.TransportClient;
import com.android.server.backup.transport.TransportClientManager;
import com.android.server.backup.transport.TransportNotRegisteredException;
import com.android.server.testing.FrameworkRobolectricTestRunner;
import com.android.server.testing.SystemLoaderPackages;
import com.android.server.testing.shadows.FrameworkShadowContextImpl;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowPackageManager;
import java.util.ArrayList;
@@ -75,12 +72,7 @@ import java.util.Objects;
import java.util.Set;
import java.util.stream.Stream;
@RunWith(FrameworkRobolectricTestRunner.class)
@Config(
manifest = Config.NONE,
sdk = 26,
shadows = {FrameworkShadowContextImpl.class})
@SystemLoaderPackages({"com.android.server.backup"})
@RunWith(RobolectricTestRunner.class)
@Presubmit
public class TransportManagerTest {
private static final String PACKAGE_A = "some.package.a";
@@ -682,7 +674,7 @@ public class TransportManagerTest {
transport.getTransportComponent().getPackageName(),
ApplicationInfo.PRIVATE_FLAG_PRIVILEGED);
}
setUpTransports(transportSet.toArray(new TransportData[transportSet.size()]));
setUpTransports(transportSet.toArray(new TransportData[0]));
TransportManager transportManager = createTransportManager(selectedTransport, transports);
transportManager.registerTransports();
return transportManager;

View File

@@ -19,17 +19,16 @@ package com.android.server.backup.encryption.chunk;
import static com.google.common.truth.Truth.assertThat;
import android.platform.test.annotations.Presubmit;
import com.android.server.testing.FrameworkRobolectricTestRunner;
import com.android.server.testing.SystemLoaderPackages;
import com.google.common.primitives.Bytes;
import java.util.Arrays;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
import org.robolectric.RobolectricTestRunner;
@RunWith(FrameworkRobolectricTestRunner.class)
@Config(manifest = Config.NONE, sdk = 26)
@SystemLoaderPackages({"com.android.server.backup"})
import java.util.Arrays;
@RunWith(RobolectricTestRunner.class)
@Presubmit
public class ChunkHashTest {
private static final int HASH_LENGTH_BYTES = 256 / 8;

View File

@@ -21,22 +21,20 @@ import static com.google.common.truth.Truth.assertThat;
import android.platform.test.annotations.Presubmit;
import android.util.proto.ProtoInputStream;
import android.util.proto.ProtoOutputStream;
import com.android.internal.util.Preconditions;
import com.android.server.testing.FrameworkRobolectricTestRunner;
import com.android.server.testing.SystemLoaderPackages;
import com.google.common.base.Charsets;
import java.io.ByteArrayInputStream;
import java.util.Arrays;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
import org.robolectric.RobolectricTestRunner;
@RunWith(FrameworkRobolectricTestRunner.class)
@Config(manifest = Config.NONE, sdk = 26)
// Include android.util.proto in addition to classes under test because the latest versions of
// android.util.proto.Proto{Input|Output}Stream are not part of Robolectric.
@SystemLoaderPackages({"com.android.server.backup", "android.util.proto"})
import java.io.ByteArrayInputStream;
import java.util.Arrays;
@RunWith(RobolectricTestRunner.class)
@Presubmit
public class ChunkListingTest {
private static final String CHUNK_A = "CHUNK_A";

View File

@@ -21,21 +21,18 @@ import static com.google.common.truth.Truth.assertThat;
import android.platform.test.annotations.Presubmit;
import android.util.proto.ProtoInputStream;
import android.util.proto.ProtoOutputStream;
import com.android.server.testing.FrameworkRobolectricTestRunner;
import com.android.server.testing.SystemLoaderPackages;
import com.google.common.base.Charsets;
import java.io.ByteArrayInputStream;
import java.util.Arrays;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
import org.robolectric.RobolectricTestRunner;
@RunWith(FrameworkRobolectricTestRunner.class)
@Config(manifest = Config.NONE, sdk = 26)
// Include android.util.proto in addition to classes under test because the latest versions of
// android.util.proto.Proto{Input|Output}Stream are not part of Robolectric.
@SystemLoaderPackages({"com.android.server.backup", "android.util.proto"})
import java.io.ByteArrayInputStream;
import java.util.Arrays;
@RunWith(RobolectricTestRunner.class)
@Presubmit
public class ChunkTest {
private static final String CHUNK_A = "CHUNK_A";

View File

@@ -19,16 +19,14 @@ package com.android.server.backup.encryption.chunk;
import static com.google.common.truth.Truth.assertThat;
import android.platform.test.annotations.Presubmit;
import com.android.server.testing.FrameworkRobolectricTestRunner;
import com.android.server.testing.SystemLoaderPackages;
import com.google.common.primitives.Bytes;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
import org.robolectric.RobolectricTestRunner;
@RunWith(FrameworkRobolectricTestRunner.class)
@Config(manifest = Config.NONE, sdk = 26)
@SystemLoaderPackages({"com.android.server.backup"})
@RunWith(RobolectricTestRunner.class)
@Presubmit
public class EncryptedChunkOrderingTest {
private static final byte[] TEST_BYTE_ARRAY_1 = new byte[] {1, 2, 3, 4, 5};

View File

@@ -17,32 +17,35 @@
package com.android.server.backup.encryption.chunking;
import static com.android.server.backup.testing.CryptoTestUtils.generateAesKey;
import static com.google.common.truth.Truth.assertThat;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static java.nio.charset.StandardCharsets.UTF_8;
import android.platform.test.annotations.Presubmit;
import com.android.server.backup.encryption.chunk.ChunkHash;
import com.android.server.testing.FrameworkRobolectricTestRunner;
import com.android.server.testing.SystemLoaderPackages;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.GCMParameterSpec;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.robolectric.annotation.Config;
import org.robolectric.RobolectricTestRunner;
@RunWith(FrameworkRobolectricTestRunner.class)
@Config(manifest = Config.NONE, sdk = 26)
@SystemLoaderPackages({"com.android.server.backup"})
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.GCMParameterSpec;
@RunWith(RobolectricTestRunner.class)
@Presubmit
public class ChunkEncryptorTest {
private static final String MAC_ALGORITHM = "HmacSHA256";

View File

@@ -19,20 +19,19 @@ package com.android.server.backup.encryption.chunking;
import static com.google.common.truth.Truth.assertThat;
import android.platform.test.annotations.Presubmit;
import com.android.server.backup.encryption.chunk.ChunkHash;
import com.android.server.testing.FrameworkRobolectricTestRunner;
import com.android.server.testing.SystemLoaderPackages;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
import org.robolectric.RobolectricTestRunner;
@RunWith(FrameworkRobolectricTestRunner.class)
@Config(manifest = Config.NONE, sdk = 26)
@SystemLoaderPackages({"com.android.server.backup"})
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
@RunWith(RobolectricTestRunner.class)
@Presubmit
public class ChunkHasherTest {
private static final String KEY_ALGORITHM = "AES";

View File

@@ -17,21 +17,22 @@
package com.android.server.backup.encryption.chunking;
import static com.google.common.truth.Truth.assertThat;
import static org.testng.Assert.assertThrows;
import android.platform.test.annotations.Presubmit;
import com.android.server.backup.encryption.chunk.ChunkHash;
import com.android.server.testing.FrameworkRobolectricTestRunner;
import com.android.server.testing.SystemLoaderPackages;
import com.google.common.primitives.Bytes;
import java.util.Arrays;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
import org.robolectric.RobolectricTestRunner;
@RunWith(FrameworkRobolectricTestRunner.class)
@Config(manifest = Config.NONE, sdk = 26)
@SystemLoaderPackages({"com.android.server.backup"})
import java.util.Arrays;
@RunWith(RobolectricTestRunner.class)
@Presubmit
public class EncryptedChunkTest {
private static final byte[] CHUNK_HASH_1_BYTES =

View File

@@ -17,25 +17,25 @@
package com.android.server.backup.encryption.chunking;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import android.platform.test.annotations.Presubmit;
import com.android.server.backup.encryption.chunk.ChunkHash;
import com.android.server.backup.encryption.chunk.ChunksMetadataProto;
import com.android.server.testing.FrameworkRobolectricTestRunner;
import com.android.server.testing.SystemLoaderPackages;
import java.util.Arrays;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.robolectric.annotation.Config;
import org.robolectric.RobolectricTestRunner;
@RunWith(FrameworkRobolectricTestRunner.class)
@Config(manifest = Config.NONE, sdk = 26)
@SystemLoaderPackages({"com.android.server.backup"})
import java.util.Arrays;
@RunWith(RobolectricTestRunner.class)
@Presubmit
public class InlineLengthsEncryptedChunkEncoderTest {

View File

@@ -17,25 +17,25 @@
package com.android.server.backup.encryption.chunking;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import android.platform.test.annotations.Presubmit;
import com.android.server.backup.encryption.chunk.ChunkHash;
import com.android.server.backup.encryption.chunk.ChunksMetadataProto;
import com.android.server.testing.FrameworkRobolectricTestRunner;
import com.android.server.testing.SystemLoaderPackages;
import java.util.Arrays;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.robolectric.annotation.Config;
import org.robolectric.RobolectricTestRunner;
@RunWith(FrameworkRobolectricTestRunner.class)
@Config(manifest = Config.NONE, sdk = 26)
@SystemLoaderPackages({"com.android.server.backup"})
import java.util.Arrays;
@RunWith(RobolectricTestRunner.class)
@Presubmit
public class LengthlessEncryptedChunkEncoderTest {
private static final byte[] TEST_NONCE =
@@ -74,7 +74,7 @@ public class LengthlessEncryptedChunkEncoderTest {
}
@Test
public void getChunkOrderingType_returnsExplicitStartsType() throws Exception {
public void getChunkOrderingType_returnsExplicitStartsType() {
assertThat(mEncoder.getChunkOrderingType()).isEqualTo(ChunksMetadataProto.EXPLICIT_STARTS);
}
}

View File

@@ -17,23 +17,23 @@
package com.android.server.backup.encryption.chunking;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.testng.Assert.assertThrows;
import android.platform.test.annotations.Presubmit;
import com.android.server.testing.FrameworkRobolectricTestRunner;
import com.android.server.testing.SystemLoaderPackages;
import com.google.common.primitives.Bytes;
import java.io.ByteArrayOutputStream;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
import org.robolectric.RobolectricTestRunner;
@RunWith(FrameworkRobolectricTestRunner.class)
@Config(manifest = Config.NONE, sdk = 26)
@SystemLoaderPackages({"com.android.server.backup"})
import java.io.ByteArrayOutputStream;
@RunWith(RobolectricTestRunner.class)
@Presubmit
public class RawBackupWriterTest {
private static final byte[] TEST_BYTES = {1, 2, 3, 4, 5, 6};

View File

@@ -7,6 +7,7 @@ import static com.android.server.backup.BackupManagerService.BACKUP_METADATA_VER
import static com.android.server.backup.BackupManagerService.BACKUP_WIDGET_METADATA_TOKEN;
import static com.google.common.truth.Truth.assertThat;
import static org.robolectric.Shadows.shadowOf;
import static org.testng.Assert.expectThrows;
@@ -21,17 +22,24 @@ import android.content.pm.PackageParser.SigningDetails;
import android.content.pm.Signature;
import android.content.pm.SigningInfo;
import android.os.Build;
import android.os.Build.VERSION_CODES;
import android.os.Environment;
import android.os.ParcelFileDescriptor;
import android.os.UserHandle;
import com.android.server.testing.FrameworkRobolectricTestRunner;
import com.android.server.testing.SystemLoaderClasses;
import com.android.server.testing.SystemLoaderPackages;
import com.android.server.testing.shadows.ShadowBackupDataInput;
import com.android.server.testing.shadows.ShadowBackupDataOutput;
import com.android.server.testing.shadows.ShadowFullBackup;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplicationPackageManager;
import org.robolectric.shadows.ShadowEnvironment;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
@@ -41,32 +49,20 @@ import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.attribute.FileTime;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplicationPackageManager;
import org.robolectric.shadows.ShadowEnvironment;
@RunWith(FrameworkRobolectricTestRunner.class)
@RunWith(RobolectricTestRunner.class)
@Config(
manifest = Config.NONE,
sdk = 26,
shadows = {
ShadowBackupDataInput.class,
ShadowBackupDataOutput.class,
ShadowEnvironment.class,
ShadowFullBackup.class,
})
@SystemLoaderPackages({"com.android.server.backup", "android.app.backup"})
@SystemLoaderClasses({PackageInfo.class, SigningInfo.class})
public class AppMetadataBackupWriterTest {
private static final String TEST_PACKAGE = "com.test.package";
private static final String TEST_PACKAGE_INSTALLER = "com.test.package.installer";
private static final Long TEST_PACKAGE_VERSION_CODE = 100L;
private PackageManager mPackageManager;
private ShadowApplicationPackageManager mShadowPackageManager;
private File mFilesDir;
private File mBackupDataOutputFile;
@@ -76,8 +72,8 @@ public class AppMetadataBackupWriterTest {
public void setUp() throws Exception {
Application application = RuntimeEnvironment.application;
PackageManager packageManager = application.getPackageManager();
mShadowPackageManager = (ShadowApplicationPackageManager) shadowOf(packageManager);
mPackageManager = application.getPackageManager();
mShadowPackageManager = (ShadowApplicationPackageManager) shadowOf(mPackageManager);
mFilesDir = RuntimeEnvironment.application.getFilesDir();
mBackupDataOutputFile = new File(mFilesDir, "output");
@@ -87,7 +83,7 @@ public class AppMetadataBackupWriterTest {
mBackupDataOutputFile, ParcelFileDescriptor.MODE_READ_WRITE);
FullBackupDataOutput output =
new FullBackupDataOutput(pfd, /* quota */ -1, /* transportFlags */ 0);
mBackupWriter = new AppMetadataBackupWriter(output, packageManager);
mBackupWriter = new AppMetadataBackupWriter(output, mPackageManager);
}
@After
@@ -197,36 +193,6 @@ public class AppMetadataBackupWriterTest {
manifestFile.delete();
}
/**
* The manifest format is:
*
* <pre>
* BACKUP_MANIFEST_VERSION
* package name
* package version code
* platform version code
* installer package name (can be empty)
* boolean (1 if archive includes .apk, otherwise 0)
* # of signatures N
* N* (signature byte array in ascii format per Signature.toCharsString())
* </pre>
*/
@Config(sdk = VERSION_CODES.O)
@Test
public void testBackupManifest_whenApiO_writesCorrectApi() throws Exception {
PackageInfo packageInfo =
createPackageInfo(TEST_PACKAGE, TEST_PACKAGE_INSTALLER, TEST_PACKAGE_VERSION_CODE);
File manifestFile = createFile(BACKUP_MANIFEST_FILENAME);
mBackupWriter.backupManifest(packageInfo, manifestFile, mFilesDir, /* withApk */ false);
byte[] manifestBytes = getWrittenBytes(mBackupDataOutputFile, /* includeTarHeader */ false);
String[] manifest = new String(manifestBytes, StandardCharsets.UTF_8).split("\n");
assertThat(manifest.length).isEqualTo(7);
assertThat(manifest[3]).isEqualTo(Integer.toString(VERSION_CODES.O)); // platform version
manifestFile.delete();
}
/**
* The manifest format is:
*
@@ -394,7 +360,7 @@ public class AppMetadataBackupWriterTest {
}
@Test
public void testBackupObb_withNoObbData_doesNotWriteBytesToOutput() throws Exception {
public void testBackupObb_withNoObbData_doesNotWriteBytesToOutput() {
PackageInfo packageInfo =
createPackageInfo(TEST_PACKAGE, TEST_PACKAGE_INSTALLER, TEST_PACKAGE_VERSION_CODE);
File obbDir = createObbDirForPackage(packageInfo.packageName);
@@ -416,7 +382,7 @@ public class AppMetadataBackupWriterTest {
packageInfo.setLongVersionCode(versionCode);
mShadowPackageManager.addPackage(packageInfo);
if (installerPackageName != null) {
mShadowPackageManager.setInstallerPackageName(packageName, installerPackageName);
mPackageManager.setInstallerPackageName(packageName, installerPackageName);
}
return packageInfo;
}

View File

@@ -46,20 +46,18 @@ import android.util.Log;
import com.android.internal.backup.IBackupTransport;
import com.android.server.backup.BackupManagerService;
import com.android.server.backup.TransportManager;
import com.android.server.backup.testing.TransportTestUtils;
import com.android.server.backup.testing.TransportData;
import com.android.server.backup.testing.TransportTestUtils;
import com.android.server.backup.testing.TransportTestUtils.TransportMock;
import com.android.server.backup.transport.TransportClient;
import com.android.server.testing.FrameworkRobolectricTestRunner;
import com.android.server.testing.SystemLoaderPackages;
import com.android.server.testing.shadows.ShadowSlog;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
@@ -69,9 +67,8 @@ import java.util.Iterator;
import java.util.List;
import java.util.stream.Stream;
@RunWith(FrameworkRobolectricTestRunner.class)
@Config(manifest = Config.NONE, sdk = 26, shadows = ShadowSlog.class)
@SystemLoaderPackages({"com.android.server.backup"})
@RunWith(RobolectricTestRunner.class)
@Config(shadows = ShadowSlog.class)
@Presubmit
public class PerformInitializeTaskTest {
@Mock private BackupManagerService mBackupManagerService;

View File

@@ -20,29 +20,24 @@ import static com.google.common.truth.Truth.assertThat;
import android.platform.test.annotations.Presubmit;
import com.android.server.testing.FrameworkRobolectricTestRunner;
import com.android.server.testing.SystemLoaderPackages;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
import org.robolectric.RobolectricTestRunner;
import java.io.IOException;
@RunWith(FrameworkRobolectricTestRunner.class)
@Config(manifest = Config.NONE, sdk = 26)
@SystemLoaderPackages({"com.android.server.backup"})
@RunWith(RobolectricTestRunner.class)
@Presubmit
public class AgentExceptionTest {
@Test
public void testTransitory_isTransitory() throws Exception {
public void testTransitory_isTransitory() {
AgentException exception = AgentException.transitory();
assertThat(exception.isTransitory()).isTrue();
}
@Test
public void testTransitory_withCause() throws Exception {
public void testTransitory_withCause() {
Exception cause = new IOException();
AgentException exception = AgentException.transitory(cause);
@@ -52,14 +47,14 @@ public class AgentExceptionTest {
}
@Test
public void testPermanent_isNotTransitory() throws Exception {
public void testPermanent_isNotTransitory() {
AgentException exception = AgentException.permanent();
assertThat(exception.isTransitory()).isFalse();
}
@Test
public void testPermanent_withCause() throws Exception {
public void testPermanent_withCause() {
Exception cause = new IOException();
AgentException exception = AgentException.permanent(cause);

View File

@@ -20,18 +20,13 @@ import static com.google.common.truth.Truth.assertThat;
import android.platform.test.annotations.Presubmit;
import com.android.server.testing.FrameworkRobolectricTestRunner;
import com.android.server.testing.SystemLoaderPackages;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
import org.robolectric.RobolectricTestRunner;
import java.io.IOException;
@RunWith(FrameworkRobolectricTestRunner.class)
@Config(manifest = Config.NONE, sdk = 26)
@SystemLoaderPackages({"com.android.server.backup"})
@RunWith(RobolectricTestRunner.class)
@Presubmit
public class BackupExceptionTest {
@Test

View File

@@ -27,9 +27,6 @@ import android.platform.test.annotations.Presubmit;
import android.util.Log;
import com.android.server.backup.BackupManagerService;
import com.android.server.backup.remote.RemoteResult;
import com.android.server.testing.FrameworkRobolectricTestRunner;
import com.android.server.testing.SystemLoaderPackages;
import com.android.server.testing.shadows.ShadowEventLog;
import com.android.server.testing.shadows.ShadowSlog;
@@ -37,17 +34,11 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowLog;
import java.lang.reflect.Field;
@RunWith(FrameworkRobolectricTestRunner.class)
@Config(
manifest = Config.NONE,
sdk = 26,
shadows = {ShadowEventLog.class, ShadowSlog.class})
@SystemLoaderPackages({"com.android.server.backup"})
@RunWith(RobolectricTestRunner.class)
@Config(shadows = {ShadowEventLog.class, ShadowSlog.class})
@Presubmit
public class KeyValueBackupReporterTest {
@Mock private BackupManagerService mBackupManagerService;
@@ -57,33 +48,31 @@ public class KeyValueBackupReporterTest {
private KeyValueBackupReporter mReporter;
@Before
public void setUp() throws Exception {
public void setUp() {
mReporter = new KeyValueBackupReporter(mBackupManagerService, mObserver, mMonitor);
}
@Test
public void testMoreDebug_isFalse() throws Exception {
boolean moreDebug = KeyValueBackupReporter.MORE_DEBUG;
assertThat(moreDebug).isFalse();
public void testMoreDebug_isFalse() {
assertThat(KeyValueBackupReporter.MORE_DEBUG).isFalse();
}
@Test
public void testOnNewThread_logsCorrectly() throws Exception {
public void testOnNewThread_logsCorrectly() {
KeyValueBackupReporter.onNewThread("foo");
assertLogcat(TAG, Log.DEBUG);
}
@Test
public void testGetMonitor_returnsMonitor() throws Exception {
public void testGetMonitor_returnsMonitor() {
IBackupManagerMonitor monitor = mReporter.getMonitor();
assertThat(monitor).isEqualTo(mMonitor);
}
@Test
public void testGetObserver_returnsObserver() throws Exception {
public void testGetObserver_returnsObserver() {
IBackupObserver observer = mReporter.getObserver();
assertThat(observer).isEqualTo(mObserver);

View File

@@ -62,8 +62,8 @@ import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import static org.robolectric.Shadows.shadowOf;
import static org.robolectric.shadow.api.Shadow.extract;
import static org.testng.Assert.fail;
import static org.testng.Assert.expectThrows;
import static org.testng.Assert.fail;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import static java.util.Collections.emptyList;
@@ -114,9 +114,6 @@ import com.android.server.backup.testing.TestUtils.ThrowingRunnable;
import com.android.server.backup.testing.TransportData;
import com.android.server.backup.testing.TransportTestUtils;
import com.android.server.backup.testing.TransportTestUtils.TransportMock;
import com.android.server.testing.FrameworkRobolectricTestRunner;
import com.android.server.testing.SystemLoaderClasses;
import com.android.server.testing.SystemLoaderPackages;
import com.android.server.testing.shadows.FrameworkShadowLooper;
import com.android.server.testing.shadows.ShadowBackupDataInput;
import com.android.server.testing.shadows.ShadowBackupDataOutput;
@@ -134,6 +131,7 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication;
@@ -156,10 +154,8 @@ import java.util.concurrent.TimeoutException;
import java.util.stream.Stream;
// TODO: Test agents timing out
@RunWith(FrameworkRobolectricTestRunner.class)
@RunWith(RobolectricTestRunner.class)
@Config(
manifest = Config.NONE,
sdk = 26,
shadows = {
FrameworkShadowLooper.class,
ShadowBackupDataInput.class,
@@ -167,8 +163,6 @@ import java.util.stream.Stream;
ShadowEventLog.class,
ShadowQueuedWork.class,
})
@SystemLoaderPackages({"com.android.server.backup", "android.app.backup"})
@SystemLoaderClasses({IBackupTransport.class, IBackupAgent.class, PackageInfo.class})
@Presubmit
public class KeyValueBackupTaskTest {
private static final PackageData PACKAGE_1 = keyValuePackage(1);
@@ -187,6 +181,7 @@ public class KeyValueBackupTaskTest {
private Handler mBackupHandler;
private PowerManager.WakeLock mWakeLock;
private KeyValueBackupReporter mReporter;
private PackageManager mPackageManager;
private ShadowPackageManager mShadowPackageManager;
private FakeIBackupManager mBackupManager;
private File mBaseStateDir;
@@ -219,8 +214,8 @@ public class KeyValueBackupTaskTest {
mDataDir.mkdirs();
assertThat(mDataDir.isDirectory()).isTrue();
PackageManager packageManager = mApplication.getPackageManager();
mShadowPackageManager = shadowOf(packageManager);
mPackageManager = mApplication.getPackageManager();
mShadowPackageManager = shadowOf(mPackageManager);
mWakeLock = createBackupWakeLock(mApplication);
mBackupManager = spy(FakeIBackupManager.class);
@@ -235,7 +230,7 @@ public class KeyValueBackupTaskTest {
mBackupManagerService,
mApplication,
mTransportManager,
packageManager,
mPackageManager,
mBackupManagerService.getBackupHandler(),
mWakeLock,
mBackupManagerService.getAgentTimeoutParameters());
@@ -2406,7 +2401,7 @@ public class KeyValueBackupTaskTest {
private AgentMock setUpAgent(PackageData packageData) {
try {
mShadowPackageManager.setApplicationEnabledSetting(
mPackageManager.setApplicationEnabledSetting(
packageData.packageName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 0);
PackageInfo packageInfo = getPackageInfo(packageData);
mShadowPackageManager.addPackage(packageInfo);

View File

@@ -23,18 +23,13 @@ import static org.testng.Assert.expectThrows;
import android.app.backup.BackupTransport;
import android.platform.test.annotations.Presubmit;
import com.android.server.testing.FrameworkRobolectricTestRunner;
import com.android.server.testing.SystemLoaderPackages;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
import org.robolectric.RobolectricTestRunner;
import java.io.IOException;
@RunWith(FrameworkRobolectricTestRunner.class)
@Config(manifest = Config.NONE, sdk = 26)
@SystemLoaderPackages({"com.android.server.backup"})
@RunWith(RobolectricTestRunner.class)
@Presubmit
public class TaskExceptionTest {
@Test

View File

@@ -20,18 +20,13 @@ import static com.google.common.truth.Truth.assertThat;
import android.platform.test.annotations.Presubmit;
import com.android.server.testing.FrameworkRobolectricTestRunner;
import com.android.server.testing.SystemLoaderPackages;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
import org.robolectric.RobolectricTestRunner;
import java.util.concurrent.CompletableFuture;
@RunWith(FrameworkRobolectricTestRunner.class)
@Config(manifest = Config.NONE, sdk = 26)
@SystemLoaderPackages({"com.android.server.backup"})
@RunWith(RobolectricTestRunner.class)
@Presubmit
public class FutureBackupCallbackTest {
@Test

View File

@@ -32,23 +32,19 @@ import android.os.Looper;
import android.platform.test.annotations.Presubmit;
import com.android.server.backup.testing.TestUtils.ThrowingRunnable;
import com.android.server.testing.FrameworkRobolectricTestRunner;
import com.android.server.testing.SystemLoaderPackages;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import org.robolectric.RobolectricTestRunner;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
@RunWith(FrameworkRobolectricTestRunner.class)
@Config(manifest = Config.NONE, sdk = 26)
@SystemLoaderPackages({"com.android.server.backup"})
@RunWith(RobolectricTestRunner.class)
@Presubmit
public class RemoteCallTest {
/** A {@link RemoteCallable} that calls the callback immediately. */
@@ -267,9 +263,4 @@ public class RemoteCallTest {
private static void postDelayed(Handler handler, ThrowingRunnable runnable, long delayMillis) {
handler.postDelayed(() -> uncheck(runnable), delayMillis);
}
/** Unchecked version of {@link Handler#post(Runnable)}. */
private static void post(Handler handler, ThrowingRunnable runnable) {
handler.post(() -> uncheck(runnable));
}
}

View File

@@ -22,16 +22,11 @@ import static org.testng.Assert.expectThrows;
import android.platform.test.annotations.Presubmit;
import com.android.server.testing.FrameworkRobolectricTestRunner;
import com.android.server.testing.SystemLoaderPackages;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
import org.robolectric.RobolectricTestRunner;
@RunWith(FrameworkRobolectricTestRunner.class)
@Config(manifest = Config.NONE, sdk = 26)
@SystemLoaderPackages({"com.android.server.backup"})
@RunWith(RobolectricTestRunner.class)
@Presubmit
public class RemoteResultTest {
@Test

View File

@@ -22,16 +22,11 @@ import static org.mockito.Mockito.verify;
import android.app.backup.IBackupManager;
import android.platform.test.annotations.Presubmit;
import com.android.server.testing.FrameworkRobolectricTestRunner;
import com.android.server.testing.SystemLoaderPackages;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
import org.robolectric.RobolectricTestRunner;
@RunWith(FrameworkRobolectricTestRunner.class)
@Config(manifest = Config.NONE, sdk = 26)
@SystemLoaderPackages({"com.android.server.backup"})
@RunWith(RobolectricTestRunner.class)
@Presubmit
public class ServiceBackupCallbackTest {
@Test

View File

@@ -19,7 +19,6 @@ package com.android.server.backup.restore;
import static com.android.server.backup.testing.BackupManagerServiceTestUtils.createBackupWakeLock;
import static com.android.server.backup.testing.BackupManagerServiceTestUtils.setUpBackupManagerServiceBasics;
import static com.android.server.backup.testing.BackupManagerServiceTestUtils.startBackupThreadAndGetLooper;
import static com.android.server.backup.testing.TestUtils.assertEventLogged;
import static com.android.server.backup.testing.TestUtils.assertEventNotLogged;
import static com.android.server.backup.testing.TransportData.backupTransport;
@@ -58,8 +57,6 @@ import com.android.server.backup.internal.BackupHandler;
import com.android.server.backup.testing.TransportData;
import com.android.server.backup.testing.TransportTestUtils;
import com.android.server.backup.testing.TransportTestUtils.TransportMock;
import com.android.server.testing.FrameworkRobolectricTestRunner;
import com.android.server.testing.SystemLoaderPackages;
import com.android.server.testing.shadows.ShadowEventLog;
import com.android.server.testing.shadows.ShadowPerformUnifiedRestoreTask;
@@ -69,6 +66,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication;
@@ -78,18 +76,14 @@ import org.robolectric.shadows.ShadowPackageManager;
import java.util.ArrayDeque;
@RunWith(FrameworkRobolectricTestRunner.class)
@Config(
manifest = Config.NONE,
sdk = 26,
shadows = {ShadowEventLog.class, ShadowPerformUnifiedRestoreTask.class, ShadowBinder.class})
@SystemLoaderPackages({"com.android.server.backup"})
@RunWith(RobolectricTestRunner.class)
@Config(shadows = {ShadowEventLog.class, ShadowPerformUnifiedRestoreTask.class, ShadowBinder.class})
@Presubmit
public class ActiveRestoreSessionTest {
private static final String PACKAGE_1 = "com.example.package1";
private static final String PACKAGE_2 = "com.example.package2";
public static final long TOKEN_1 = 1L;
public static final long TOKEN_2 = 2L;
private static final long TOKEN_1 = 1L;
private static final long TOKEN_2 = 2L;
@Mock private BackupManagerService mBackupManagerService;
@Mock private TransportManager mTransportManager;

View File

@@ -17,6 +17,7 @@
package com.android.server.backup.transport;
import static com.android.server.backup.TransportManager.SERVICE_ACTION_TRANSPORT_HOST;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.argThat;
@@ -32,19 +33,15 @@ import android.os.Bundle;
import android.os.UserHandle;
import android.platform.test.annotations.Presubmit;
import com.android.server.testing.FrameworkRobolectricTestRunner;
import com.android.server.testing.SystemLoaderPackages;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatcher;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import org.robolectric.RobolectricTestRunner;
@RunWith(FrameworkRobolectricTestRunner.class)
@Config(manifest = Config.NONE, sdk = 26)
@SystemLoaderPackages({"com.android.server.backup"})
@RunWith(RobolectricTestRunner.class)
@Presubmit
public class TransportClientManagerTest {
private static final String PACKAGE_NAME = "random.package.name";

View File

@@ -49,8 +49,6 @@ import android.util.Log;
import com.android.internal.backup.IBackupTransport;
import com.android.server.EventLogTags;
import com.android.server.testing.FrameworkRobolectricTestRunner;
import com.android.server.testing.SystemLoaderPackages;
import com.android.server.testing.shadows.FrameworkShadowLooper;
import com.android.server.testing.shadows.ShadowCloseGuard;
import com.android.server.testing.shadows.ShadowEventLog;
@@ -62,6 +60,7 @@ import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowLog;
import org.robolectric.shadows.ShadowLooper;
@@ -69,17 +68,13 @@ import org.robolectric.shadows.ShadowLooper;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;
@RunWith(FrameworkRobolectricTestRunner.class)
@Config(
manifest = Config.NONE,
sdk = 26,
shadows = {
@RunWith(RobolectricTestRunner.class)
@Config(shadows = {
ShadowEventLog.class,
ShadowCloseGuard.class,
ShadowSlog.class,
FrameworkShadowLooper.class
})
@SystemLoaderPackages({"com.android.server.backup"})
@Presubmit
public class TransportClientTest {
private static final String PACKAGE_NAME = "some.package.name";
@@ -137,7 +132,7 @@ public class TransportClientTest {
}
@Test
public void testConnectAsync_callsBindService() throws Exception {
public void testConnectAsync_callsBindService() {
mTransportClient.connectAsync(mTransportConnectionListener, "caller");
verify(mContext)
@@ -149,7 +144,7 @@ public class TransportClientTest {
}
@Test
public void testConnectAsync_callsListenerWhenConnected() throws Exception {
public void testConnectAsync_callsListenerWhenConnected() {
mTransportClient.connectAsync(mTransportConnectionListener, "caller");
ServiceConnection connection = verifyBindServiceAsUserAndCaptureServiceConnection(mContext);
@@ -161,8 +156,7 @@ public class TransportClientTest {
}
@Test
public void testConnectAsync_whenPendingConnection_callsAllListenersWhenConnected()
throws Exception {
public void testConnectAsync_whenPendingConnection_callsAllListenersWhenConnected() {
mTransportClient.connectAsync(mTransportConnectionListener, "caller1");
ServiceConnection connection = verifyBindServiceAsUserAndCaptureServiceConnection(mContext);
@@ -177,7 +171,7 @@ public class TransportClientTest {
}
@Test
public void testConnectAsync_whenAlreadyConnected_callsListener() throws Exception {
public void testConnectAsync_whenAlreadyConnected_callsListener() {
mTransportClient.connectAsync(mTransportConnectionListener, "caller1");
ServiceConnection connection = verifyBindServiceAsUserAndCaptureServiceConnection(mContext);
connection.onServiceConnected(mTransportComponent, mTransportBinder);
@@ -190,7 +184,7 @@ public class TransportClientTest {
}
@Test
public void testConnectAsync_whenFrameworkDoesntBind_callsListener() throws Exception {
public void testConnectAsync_whenFrameworkDoesntBind_callsListener() {
when(mContext.bindServiceAsUser(
eq(mBindIntent),
any(ServiceConnection.class),
@@ -206,7 +200,7 @@ public class TransportClientTest {
}
@Test
public void testConnectAsync_whenFrameworkDoesNotBind_releasesConnection() throws Exception {
public void testConnectAsync_whenFrameworkDoesNotBind_releasesConnection() {
when(mContext.bindServiceAsUser(
eq(mBindIntent),
any(ServiceConnection.class),
@@ -221,8 +215,7 @@ public class TransportClientTest {
}
@Test
public void testConnectAsync_afterOnServiceDisconnectedBeforeNewConnection_callsListener()
throws Exception {
public void testConnectAsync_afterOnServiceDisconnectedBeforeNewConnection_callsListener() {
mTransportClient.connectAsync(mTransportConnectionListener, "caller1");
ServiceConnection connection = verifyBindServiceAsUserAndCaptureServiceConnection(mContext);
connection.onServiceConnected(mTransportComponent, mTransportBinder);
@@ -235,8 +228,7 @@ public class TransportClientTest {
}
@Test
public void testConnectAsync_afterOnServiceDisconnectedAfterNewConnection_callsListener()
throws Exception {
public void testConnectAsync_afterOnServiceDisconnectedAfterNewConnection_callsListener() {
mTransportClient.connectAsync(mTransportConnectionListener, "caller1");
ServiceConnection connection = verifyBindServiceAsUserAndCaptureServiceConnection(mContext);
connection.onServiceConnected(mTransportComponent, mTransportBinder);
@@ -251,7 +243,7 @@ public class TransportClientTest {
}
@Test
public void testConnectAsync_callsListenerIfBindingDies() throws Exception {
public void testConnectAsync_callsListenerIfBindingDies() {
mTransportClient.connectAsync(mTransportConnectionListener, "caller");
ServiceConnection connection = verifyBindServiceAsUserAndCaptureServiceConnection(mContext);
@@ -263,8 +255,7 @@ public class TransportClientTest {
}
@Test
public void testConnectAsync_whenPendingConnection_callsListenersIfBindingDies()
throws Exception {
public void testConnectAsync_whenPendingConnection_callsListenersIfBindingDies() {
mTransportClient.connectAsync(mTransportConnectionListener, "caller1");
ServiceConnection connection = verifyBindServiceAsUserAndCaptureServiceConnection(mContext);
@@ -412,14 +403,14 @@ public class TransportClientTest {
}
@Test
public void testMarkAsDisposed_whenCreated() throws Throwable {
public void testMarkAsDisposed_whenCreated() {
mTransportClient.markAsDisposed();
// No exception thrown
}
@Test
public void testMarkAsDisposed_afterOnBindingDied() throws Throwable {
public void testMarkAsDisposed_afterOnBindingDied() {
mTransportClient.connectAsync(mTransportConnectionListener, "caller1");
ServiceConnection connection = verifyBindServiceAsUserAndCaptureServiceConnection(mContext);
connection.onBindingDied(mTransportComponent);
@@ -430,7 +421,7 @@ public class TransportClientTest {
}
@Test
public void testMarkAsDisposed_whenConnectedAndUnbound() throws Throwable {
public void testMarkAsDisposed_whenConnectedAndUnbound() {
mTransportClient.connectAsync(mTransportConnectionListener, "caller1");
ServiceConnection connection = verifyBindServiceAsUserAndCaptureServiceConnection(mContext);
connection.onServiceConnected(mTransportComponent, mTransportBinder);
@@ -442,7 +433,7 @@ public class TransportClientTest {
}
@Test
public void testMarkAsDisposed_afterOnServiceDisconnected() throws Throwable {
public void testMarkAsDisposed_afterOnServiceDisconnected() {
mTransportClient.connectAsync(mTransportConnectionListener, "caller1");
ServiceConnection connection = verifyBindServiceAsUserAndCaptureServiceConnection(mContext);
connection.onServiceConnected(mTransportComponent, mTransportBinder);
@@ -454,14 +445,14 @@ public class TransportClientTest {
}
@Test
public void testMarkAsDisposed_whenBound() throws Throwable {
public void testMarkAsDisposed_whenBound() {
mTransportClient.connectAsync(mTransportConnectionListener, "caller1");
expectThrows(RuntimeException.class, mTransportClient::markAsDisposed);
}
@Test
public void testMarkAsDisposed_whenConnected() throws Throwable {
public void testMarkAsDisposed_whenConnected() {
mTransportClient.connectAsync(mTransportConnectionListener, "caller1");
ServiceConnection connection = verifyBindServiceAsUserAndCaptureServiceConnection(mContext);
connection.onServiceConnected(mTransportComponent, mTransportBinder);

View File

@@ -25,17 +25,13 @@ import android.content.ComponentName;
import android.platform.test.annotations.Presubmit;
import com.android.server.backup.transport.TransportStats.Stats;
import com.android.server.testing.FrameworkRobolectricTestRunner;
import com.android.server.testing.SystemLoaderPackages;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
import org.robolectric.RobolectricTestRunner;
@RunWith(FrameworkRobolectricTestRunner.class)
@Config(manifest = Config.NONE, sdk = 26)
@SystemLoaderPackages({"com.android.server.backup"})
@RunWith(RobolectricTestRunner.class)
@Presubmit
public class TransportStatsTest {
private static final double TOLERANCE = 0.0001;

View File

@@ -1,9 +1,10 @@
package com.android.server.location;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Matchers.eq;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -11,27 +12,18 @@ import static org.mockito.Mockito.when;
import android.platform.test.annotations.Presubmit;
import com.android.server.location.GnssBatchingProvider.GnssBatchingProviderNative;
import com.android.server.testing.FrameworkRobolectricTestRunner;
import com.android.server.testing.SystemLoaderPackages;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import org.robolectric.RobolectricTestRunner;
/**
* Unit tests for {@link GnssBatchingProvider}.
*/
@RunWith(FrameworkRobolectricTestRunner.class)
@Config(
manifest = Config.NONE,
shadows = {
},
sdk = 27
)
@SystemLoaderPackages({"com.android.server.location"})
@RunWith(RobolectricTestRunner.class)
@Presubmit
public class GnssBatchingProviderTest {

View File

@@ -1,8 +1,8 @@
package com.android.server.location;
import static org.mockito.Matchers.anyDouble;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.eq;
import static org.mockito.ArgumentMatchers.anyDouble;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -11,25 +11,17 @@ import android.os.Looper;
import android.os.RemoteException;
import android.platform.test.annotations.Presubmit;
import com.android.server.testing.FrameworkRobolectricTestRunner;
import com.android.server.testing.SystemLoaderPackages;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import org.robolectric.RobolectricTestRunner;
/**
* Unit tests for {@link GnssGeofenceProvider}.
*/
@RunWith(FrameworkRobolectricTestRunner.class)
@Config(
manifest = Config.NONE,
sdk = 27
)
@SystemLoaderPackages({"com.android.server.location"})
@RunWith(RobolectricTestRunner.class)
@Presubmit
public class GnssGeofenceProviderTest {
private static final int GEOFENCE_ID = 12345;

View File

@@ -1,6 +1,7 @@
package com.android.server.location;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
@@ -10,26 +11,18 @@ import android.os.Handler;
import android.os.Looper;
import android.platform.test.annotations.Presubmit;
import com.android.server.testing.FrameworkRobolectricTestRunner;
import com.android.server.testing.SystemLoaderPackages;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
/**
* Unit tests for {@link GnssMeasurementsProvider}.
*/
@RunWith(FrameworkRobolectricTestRunner.class)
@Config(
manifest = Config.NONE,
sdk = 27
)
@SystemLoaderPackages({"com.android.server.location"})
@RunWith(RobolectricTestRunner.class)
@Presubmit
public class GnssMeasurementsProviderTest {
@Mock

View File

@@ -10,25 +10,17 @@ import android.os.Handler;
import android.os.Looper;
import android.platform.test.annotations.Presubmit;
import com.android.server.testing.FrameworkRobolectricTestRunner;
import com.android.server.testing.SystemLoaderPackages;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import org.robolectric.RobolectricTestRunner;
/**
* Unit tests for {@link GnssNavigationMessageProvider}.
*/
@RunWith(FrameworkRobolectricTestRunner.class)
@Config(
manifest = Config.NONE,
sdk = 27
)
@SystemLoaderPackages({"com.android.server.location"})
@RunWith(RobolectricTestRunner.class)
@Presubmit
public class GnssNavigationMessageProviderTest {
@Mock

View File

@@ -4,24 +4,16 @@ import static com.google.common.truth.Truth.assertThat;
import android.platform.test.annotations.Presubmit;
import com.android.server.testing.FrameworkRobolectricTestRunner;
import com.android.server.testing.SystemLoaderPackages;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
import org.robolectric.RobolectricTestRunner;
import java.util.HashSet;
/**
* Unit tests for {@link GnssPositionMode}.
*/
@RunWith(FrameworkRobolectricTestRunner.class)
@Config(
manifest = Config.NONE,
sdk = 27
)
@SystemLoaderPackages({"com.android.server.location"})
@RunWith(RobolectricTestRunner.class)
@Presubmit
public class GnssPositionModeTest {

View File

@@ -2,7 +2,7 @@ package com.android.server.location;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
@@ -13,18 +13,15 @@ import android.os.Looper;
import android.platform.test.annotations.Presubmit;
import android.provider.Settings;
import com.android.server.testing.FrameworkRobolectricTestRunner;
import com.android.server.testing.SystemLoaderPackages;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.Shadows;
import org.robolectric.annotation.Config;
import java.util.Collection;
import java.util.List;
@@ -32,18 +29,10 @@ import java.util.List;
/**
* Unit tests for {@link GnssSatelliteBlacklistHelper}.
*/
@RunWith(FrameworkRobolectricTestRunner.class)
@Config(
manifest = Config.NONE,
shadows = {
},
sdk = 27
)
@SystemLoaderPackages({"com.android.server.location"})
@RunWith(RobolectricTestRunner.class)
@Presubmit
public class GnssSatelliteBlacklistHelperTest {
private Context mContext;
private ContentResolver mContentResolver;
@Mock
private GnssSatelliteBlacklistHelper.GnssSatelliteBlacklistCallback mCallback;
@@ -51,9 +40,9 @@ public class GnssSatelliteBlacklistHelperTest {
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mContentResolver = mContext.getContentResolver();
new GnssSatelliteBlacklistHelper(mContext, Looper.myLooper(), mCallback);
Context context = RuntimeEnvironment.application;
mContentResolver = context.getContentResolver();
new GnssSatelliteBlacklistHelper(context, Looper.myLooper(), mCallback);
}
@Test

View File

@@ -9,16 +9,14 @@ import android.platform.test.annotations.Presubmit;
import android.util.NtpTrustedTime;
import com.android.server.location.NtpTimeHelper.InjectNtpTimeCallback;
import com.android.server.testing.FrameworkRobolectricTestRunner;
import com.android.server.testing.SystemLoaderPackages;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowLooper;
import org.robolectric.shadows.ShadowSystemClock;
@@ -28,12 +26,7 @@ import java.util.concurrent.TimeUnit;
/**
* Unit tests for {@link NtpTimeHelper}.
*/
@RunWith(FrameworkRobolectricTestRunner.class)
@Config(
manifest = Config.NONE,
sdk = 27
)
@SystemLoaderPackages({"com.android.server.location"})
@RunWith(RobolectricTestRunner.class)
@Presubmit
public class NtpTimeHelperTest {

View File

@@ -1,186 +0,0 @@
/*
* Copyright (C) 2017 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.server.testing;
import static java.util.Arrays.asList;
import com.google.common.collect.ImmutableSet;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.internal.SandboxFactory;
import org.robolectric.internal.SdkEnvironment;
import org.robolectric.internal.bytecode.InstrumentationConfiguration;
import org.robolectric.internal.bytecode.SandboxClassLoader;
import org.robolectric.util.Util;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Stream;
import javax.annotation.Nonnull;
/**
* HACK
* Robolectric loads up Android environment from prebuilt android jars before running a method.
* These jars are versioned according to the SDK level configured for the method (or class). The
* jars represent a snapshot of the Android APIs in that SDK level. For Robolectric tests that are
* testing Android components themselves we don't want certain classes (usually the
* class-under-test) to be loaded from the prebuilt jar, we want it instead to be loaded from the
* dependencies of our test target, i.e. the system class loader. That way we can write tests
* against the actual classes that are in the tree, not a past version of them. Ideally we would
* have a locally built jar referenced by Robolectric, but until that happens one can use this
* class.
* This class reads the {@link SystemLoaderClasses} or {@link SystemLoaderPackages} annotations on
* test classes, for classes that match the annotations it will bypass the android jar and load it
* from the system class loader. Allowing the test to test the actual class in the tree.
*
* Implementation note: One could think about overriding
* {@link RobolectricTestRunner#createClassLoaderConfig(FrameworkMethod)} method and putting the
* classes in the annotation in the {@link InstrumentationConfiguration} list of classes not to
* acquire. Unfortunately, this will not work because we will not be instrumenting the class.
* Instead, we have to load the class bytes from the system class loader but still instrument it, we
* do this by overriding {@link SandboxClassLoader#getByteCode(String)} and loading the class bytes
* from the system class loader if it in the {@link SystemLoaderClasses} annotation. This way the
* {@link SandboxClassLoader} still instruments the class, but it's not loaded from the android jar.
* Finally, we inject the custom class loader in place of the default one.
*
* TODO: Remove this when we are using locally built android jars in the method's environment.
*/
public class FrameworkRobolectricTestRunner extends RobolectricTestRunner {
private final SandboxFactory mSandboxFactory;
public FrameworkRobolectricTestRunner(Class<?> testClass) throws InitializationError {
super(testClass);
Set<String> classPrefixes = getSystemLoaderClassPrefixes(testClass);
mSandboxFactory = new FrameworkSandboxFactory(classPrefixes);
}
private Set<String> getSystemLoaderClassPrefixes(Class<?> testClass) {
Set<String> classPrefixes = new HashSet<>();
SystemLoaderClasses byClass = testClass.getAnnotation(SystemLoaderClasses.class);
if (byClass != null) {
Stream.of(byClass.value()).map(Class::getName).forEach(classPrefixes::add);
}
SystemLoaderPackages byPackage = testClass.getAnnotation(SystemLoaderPackages.class);
if (byPackage != null) {
classPrefixes.addAll(asList(byPackage.value()));
}
return classPrefixes;
}
@Nonnull
@Override
protected SdkEnvironment getSandbox(FrameworkMethod method) {
// HACK: Calling super just to get SdkConfig via sandbox.getSdkConfig(), because
// RobolectricFrameworkMethod, the runtime class of method, is package-protected
SdkEnvironment sandbox = super.getSandbox(method);
return mSandboxFactory.getSdkEnvironment(
createClassLoaderConfig(method),
getJarResolver(),
sandbox.getSdkConfig());
}
private static class FrameworkClassLoader extends SandboxClassLoader {
private final Set<String> mSystemLoaderClassPrefixes;
private FrameworkClassLoader(
Set<String> systemLoaderClassPrefixes,
ClassLoader systemClassLoader,
InstrumentationConfiguration instrumentationConfig,
URL... urls) {
super(systemClassLoader, instrumentationConfig, urls);
mSystemLoaderClassPrefixes = systemLoaderClassPrefixes;
}
@Override
protected byte[] getByteCode(String className) throws ClassNotFoundException {
String classFileName = className.replace('.', '/') + ".class";
if (shouldLoadFromSystemLoader(className)) {
try (InputStream classByteStream = getResourceAsStream(classFileName)) {
if (classByteStream == null) {
throw new ClassNotFoundException(className);
}
return Util.readBytes(classByteStream);
} catch (IOException e) {
throw new ClassNotFoundException(
"Couldn't load " + className + " from system class loader", e);
}
}
return super.getByteCode(className);
}
/**
* HACK^2
* The framework Robolectric run configuration puts a prebuilt in front of us, so we try not
* to load the class from there, if possible.
*/
@Override
public InputStream getResourceAsStream(String resource) {
try {
Enumeration<URL> urls = getResources(resource);
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
if (!url.toString().toLowerCase().contains("prebuilt")) {
return url.openStream();
}
}
} catch (IOException e) {
// Fall through
}
return super.getResourceAsStream(resource);
}
/**
* Classes like com.package.ClassName$InnerClass should also be loaded from the system class
* loader, so we test if the classes in the annotation are prefixes of the class to load.
*/
private boolean shouldLoadFromSystemLoader(String className) {
for (String classPrefix : mSystemLoaderClassPrefixes) {
if (className.startsWith(classPrefix)) {
return true;
}
}
return false;
}
}
private static class FrameworkSandboxFactory extends SandboxFactory {
private final Set<String> mSystemLoaderClassPrefixes;
private FrameworkSandboxFactory(Set<String> systemLoaderClassPrefixes) {
mSystemLoaderClassPrefixes = systemLoaderClassPrefixes;
}
@Nonnull
@Override
public ClassLoader createClassLoader(
InstrumentationConfiguration instrumentationConfig, URL... urls) {
return new FrameworkClassLoader(
mSystemLoaderClassPrefixes,
ClassLoader.getSystemClassLoader(),
instrumentationConfig,
urls);
}
}
}

View File

@@ -1,35 +0,0 @@
/*
* Copyright (C) 2017 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.server.testing;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Annotation to be used in test classes that run with {@link FrameworkRobolectricTestRunner}.
* This will make the classes specified be loaded from the system class loader, NOT from the
* Robolectric android jar.
*
* @see FrameworkRobolectricTestRunner
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface SystemLoaderClasses {
Class<?>[] value() default {};
}

View File

@@ -1,35 +0,0 @@
/*
* 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.server.testing;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Annotation to be used in test classes that run with {@link FrameworkRobolectricTestRunner}.
* This will make the classes under the specified packages be loaded from the system class loader,
* NOT from the Robolectric android jar.
*
* @see FrameworkRobolectricTestRunner
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface SystemLoaderPackages {
String[] value() default {};
}

View File

@@ -1,37 +0,0 @@
/*
* 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.server.testing.shadows;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.UserHandle;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.shadows.ShadowContextImpl;
@Implements(className = ShadowContextImpl.CLASS_NAME, inheritImplementationMethods = true)
public class FrameworkShadowContextImpl extends ShadowContextImpl {
@Implementation
public boolean bindServiceAsUser(
Intent service,
ServiceConnection connection,
int flags,
UserHandle user) {
return bindService(service, connection, flags);
}
}

View File

@@ -25,7 +25,7 @@ import org.robolectric.shadows.ShadowLooper;
import java.util.Optional;
@Implements(value = Looper.class, inheritImplementationMethods = true)
@Implements(value = Looper.class)
public class FrameworkShadowLooper extends ShadowLooper {
@RealObject private Looper mLooper;
private Optional<Boolean> mIsCurrentThread = Optional.empty();
@@ -39,7 +39,7 @@ public class FrameworkShadowLooper extends ShadowLooper {
}
@Implementation
public boolean isCurrentThread() {
protected boolean isCurrentThread() {
if (mIsCurrentThread.isPresent()) {
return mIsCurrentThread.get();
}

View File

@@ -26,6 +26,7 @@ import com.android.server.backup.utils.AppBackupUtils;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.Resetter;
import java.util.HashSet;
import java.util.Set;
@@ -52,21 +53,22 @@ public class ShadowAppBackupUtils {
}
@Implementation
public static boolean appIsRunningAndEligibleForBackupWithTransport(
protected static boolean appIsRunningAndEligibleForBackupWithTransport(
@Nullable TransportClient transportClient, String packageName, PackageManager pm) {
return sAppsRunningAndEligibleForBackupWithTransport.contains(packageName);
}
@Implementation
public static boolean appIsEligibleForBackup(ApplicationInfo app, PackageManager pm) {
protected static boolean appIsEligibleForBackup(ApplicationInfo app, PackageManager pm) {
return sAppsEligibleForBackup.contains(app.packageName);
}
@Implementation
public static boolean appGetsFullBackup(PackageInfo packageInfo) {
protected static boolean appGetsFullBackup(PackageInfo packageInfo) {
return sAppsGetFullBackup.contains(packageInfo.packageName);
}
@Resetter
public static void reset() {
sAppsRunningAndEligibleForBackupWithTransport.clear();
sAppsEligibleForBackup.clear();

View File

@@ -52,12 +52,12 @@ public class ShadowBackupDataInput {
private boolean mHeaderReady;
@Implementation
public void __constructor__(FileDescriptor fd) {
protected void __constructor__(FileDescriptor fd) {
mFileDescriptor = fd;
}
@Implementation
public boolean readNextHeader() throws IOException {
protected boolean readNextHeader() throws IOException {
if (sReadNextHeaderThrow) {
sReadNextHeaderThrow = false;
throw new IOException("Fake exception");
@@ -75,19 +75,19 @@ public class ShadowBackupDataInput {
}
@Implementation
public String getKey() {
protected String getKey() {
checkHeaderReady();
return mKey;
}
@Implementation
public int getDataSize() {
protected int getDataSize() {
checkHeaderReady();
return mSize;
}
@Implementation
public int readEntityData(byte[] data, int offset, int size) throws IOException {
protected int readEntityData(byte[] data, int offset, int size) throws IOException {
checkHeaderReady();
int result = mInput.read(data, offset, size);
if (result < 0) {
@@ -97,7 +97,7 @@ public class ShadowBackupDataInput {
}
@Implementation
public void skipEntityData() throws IOException {
protected void skipEntityData() throws IOException {
checkHeaderReady();
mInput.read(new byte[mSize], 0, mSize);
}

View File

@@ -39,19 +39,19 @@ public class ShadowBackupDataOutput {
private int mTransportFlags;
@Implementation
public void __constructor__(FileDescriptor fd, long quota, int transportFlags) {
protected void __constructor__(FileDescriptor fd, long quota, int transportFlags) {
mFileDescriptor = fd;
mQuota = quota;
mTransportFlags = transportFlags;
}
@Implementation
public long getQuota() {
protected long getQuota() {
return mQuota;
}
@Implementation
public int getTransportFlags() {
protected int getTransportFlags() {
return mTransportFlags;
}
@@ -61,7 +61,7 @@ public class ShadowBackupDataOutput {
}
@Implementation
public int writeEntityHeader(String key, int dataSize) throws IOException {
protected int writeEntityHeader(String key, int dataSize) throws IOException {
ensureOutput();
final int size;
try (ByteArrayOutputStream byteStream = new ByteArrayOutputStream()) {
@@ -81,7 +81,7 @@ public class ShadowBackupDataOutput {
}
@Implementation
public int writeEntityData(byte[] data, int size) throws IOException {
protected int writeEntityData(byte[] data, int size) throws IOException {
ensureOutput();
mOutput.write(data, 0, size);
mOutput.flush();

View File

@@ -17,6 +17,7 @@
package com.android.server.testing.shadows;
import android.os.Binder;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
@@ -31,14 +32,14 @@ public class ShadowBinder extends org.robolectric.shadows.ShadowBinder {
private static Integer originalCallingUid;
@Implementation
public static long clearCallingIdentity() {
protected static long clearCallingIdentity() {
originalCallingUid = getCallingUid();
setCallingUid(LOCAL_UID);
return 1L;
}
@Implementation
public static void restoreCallingIdentity(long token) {
protected static void restoreCallingIdentity(long token) {
setCallingUid(originalCallingUid);
}
}

View File

@@ -32,14 +32,14 @@ public class ShadowEventLog {
private static final LinkedHashSet<Entry> ENTRIES = new LinkedHashSet<>();
@Implementation
public static int writeEvent(int tag, Object... values) {
protected static int writeEvent(int tag, Object... values) {
ENTRIES.add(new Entry(tag, Arrays.asList(values)));
// Currently we don't care about the return value, if we do, estimate it correctly
return 0;
}
@Implementation
public static int writeEvent(int tag, String string) {
protected static int writeEvent(int tag, String string) {
return writeEvent(tag, (Object) string);
}

View File

@@ -31,7 +31,7 @@ public class ShadowFullBackup {
* implementation.
*/
@Implementation
public static int backupToTar(
protected static int backupToTar(
String packageName,
String domain,
String linkdomain,

View File

@@ -18,8 +18,10 @@ package com.android.server.testing.shadows;
import android.content.Context;
import android.os.Binder;
import com.android.server.backup.BackupManagerConstants;
import com.android.server.backup.KeyValueBackupJob;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
@@ -32,7 +34,7 @@ public class ShadowKeyValueBackupJob {
}
@Implementation
public static void schedule(Context ctx, long delay, BackupManagerConstants constants) {
protected static void schedule(Context ctx, long delay, BackupManagerConstants constants) {
callingUid = Binder.getCallingUid();
}
}

View File

@@ -53,7 +53,7 @@ public class ShadowKeyValueBackupTask {
private List<String> mPendingFullBackups;
@Implementation
public void __constructor__(
protected void __constructor__(
BackupManagerService backupManagerService,
TransportClient transportClient,
String transportDirName,
@@ -71,7 +71,7 @@ public class ShadowKeyValueBackupTask {
}
@Implementation
public void execute() {
protected void execute() {
mListener.onFinished("ShadowKeyValueBackupTask.execute()");
}

View File

@@ -54,7 +54,7 @@ public class ShadowPerformUnifiedRestoreTask {
private OnTaskFinishedListener mListener;
@Implementation
public void __constructor__(
protected void __constructor__(
BackupManagerService backupManagerService,
TransportClient transportClient,
IRestoreObserver observer,
@@ -74,7 +74,7 @@ public class ShadowPerformUnifiedRestoreTask {
}
@Implementation
public void execute() {
protected void execute() {
mBackupManagerService.setRestoreInProgress(false);
mListener.onFinished("ShadowPerformUnifiedRestoreTask.execute()");
}

View File

@@ -21,92 +21,91 @@ import android.util.Slog;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.shadows.ShadowLog;
@Implements(Slog.class)
public class ShadowSlog {
@Implementation
public static int v(String tag, String msg) {
protected static int v(String tag, String msg) {
return Log.v(tag, msg);
}
@Implementation
public static int v(String tag, String msg, Throwable tr) {
protected static int v(String tag, String msg, Throwable tr) {
return Log.v(tag, msg, tr);
}
@Implementation
public static int d(String tag, String msg) {
protected static int d(String tag, String msg) {
return Log.d(tag, msg);
}
@Implementation
public static int d(String tag, String msg, Throwable tr) {
protected static int d(String tag, String msg, Throwable tr) {
return Log.d(tag, msg, tr);
}
@Implementation
public static int i(String tag, String msg) {
protected static int i(String tag, String msg) {
return Log.i(tag, msg);
}
@Implementation
public static int i(String tag, String msg, Throwable tr) {
protected static int i(String tag, String msg, Throwable tr) {
return Log.i(tag, msg, tr);
}
@Implementation
public static int w(String tag, String msg) {
protected static int w(String tag, String msg) {
return Log.w(tag, msg);
}
@Implementation
public static int w(String tag, String msg, Throwable tr) {
protected static int w(String tag, String msg, Throwable tr) {
return Log.w(tag, msg, tr);
}
@Implementation
public static int w(String tag, Throwable tr) {
protected static int w(String tag, Throwable tr) {
return Log.w(tag, tr);
}
@Implementation
public static int e(String tag, String msg) {
protected static int e(String tag, String msg) {
return Log.e(tag, msg);
}
@Implementation
public static int e(String tag, String msg, Throwable tr) {
protected static int e(String tag, String msg, Throwable tr) {
return Log.e(tag, msg, tr);
}
@Implementation
public static int wtf(String tag, String msg) {
protected static int wtf(String tag, String msg) {
return Log.wtf(tag, msg);
}
@Implementation
public static void wtfQuiet(String tag, String msg) {
protected static void wtfQuiet(String tag, String msg) {
Log.wtf(tag, msg);
}
@Implementation
public static int wtfStack(String tag, String msg) {
protected static int wtfStack(String tag, String msg) {
return Log.wtf(tag, msg);
}
@Implementation
public static int wtf(String tag, Throwable tr) {
protected static int wtf(String tag, Throwable tr) {
return Log.wtf(tag, tr);
}
@Implementation
public static int wtf(String tag, String msg, Throwable tr) {
protected static int wtf(String tag, String msg, Throwable tr) {
return Log.wtf(tag, msg, tr);
}
@Implementation
public static int println(int priority, String tag, String msg) {
protected static int println(int priority, String tag, String msg) {
return Log.println(priority, tag, msg);
}
}