diff options
7 files changed, 80 insertions, 6 deletions
diff --git a/services/robotests/backup/src/com/android/server/backup/BackupManagerServiceTest.java b/services/robotests/backup/src/com/android/server/backup/BackupManagerServiceTest.java index e55c4a677dbc..a65d1dca2a57 100644 --- a/services/robotests/backup/src/com/android/server/backup/BackupManagerServiceTest.java +++ b/services/robotests/backup/src/com/android/server/backup/BackupManagerServiceTest.java @@ -53,6 +53,7 @@ import android.util.SparseArray; import com.android.server.backup.testing.TransportData; import com.android.server.testing.shadows.ShadowApplicationPackageManager; import com.android.server.testing.shadows.ShadowBinder; +import com.android.server.testing.shadows.ShadowSystemServiceRegistry; import org.junit.After; import org.junit.Before; @@ -73,7 +74,12 @@ import java.io.StringWriter; /** Tests for the user-aware backup/restore system service {@link BackupManagerService}. */ @RunWith(RobolectricTestRunner.class) -@Config(shadows = {ShadowApplicationPackageManager.class, ShadowBinder.class}) +@Config( + shadows = { + ShadowApplicationPackageManager.class, + ShadowBinder.class, + ShadowSystemServiceRegistry.class + }) @Presubmit public class BackupManagerServiceTest { private static final String TEST_PACKAGE = "package"; diff --git a/services/robotests/backup/src/com/android/server/backup/FullBackupJobTest.java b/services/robotests/backup/src/com/android/server/backup/FullBackupJobTest.java index 9a78d0b3f456..dbc0da707477 100644 --- a/services/robotests/backup/src/com/android/server/backup/FullBackupJobTest.java +++ b/services/robotests/backup/src/com/android/server/backup/FullBackupJobTest.java @@ -25,6 +25,8 @@ import android.os.Handler; import android.os.UserHandle; import android.platform.test.annotations.Presubmit; +import com.android.server.testing.shadows.ShadowSystemServiceRegistry; + import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -36,7 +38,7 @@ import org.robolectric.annotation.Config; import org.robolectric.shadows.ShadowJobScheduler; @RunWith(RobolectricTestRunner.class) -@Config(shadows = {ShadowJobScheduler.class}) +@Config(shadows = {ShadowJobScheduler.class, ShadowSystemServiceRegistry.class}) @Presubmit public class FullBackupJobTest { private Context mContext; diff --git a/services/robotests/backup/src/com/android/server/backup/KeyValueBackupJobTest.java b/services/robotests/backup/src/com/android/server/backup/KeyValueBackupJobTest.java index 8d9e44fbf1ad..1c5fac28de3c 100644 --- a/services/robotests/backup/src/com/android/server/backup/KeyValueBackupJobTest.java +++ b/services/robotests/backup/src/com/android/server/backup/KeyValueBackupJobTest.java @@ -24,14 +24,18 @@ import android.os.Handler; import android.os.UserHandle; import android.platform.test.annotations.Presubmit; +import com.android.server.testing.shadows.ShadowSystemServiceRegistry; + 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(RobolectricTestRunner.class) +@Config(shadows = {ShadowSystemServiceRegistry.class}) @Presubmit public class KeyValueBackupJobTest { private Context mContext; diff --git a/services/robotests/backup/src/com/android/server/backup/UserBackupManagerServiceTest.java b/services/robotests/backup/src/com/android/server/backup/UserBackupManagerServiceTest.java index 74fe81c6f68e..84e810dbb325 100644 --- a/services/robotests/backup/src/com/android/server/backup/UserBackupManagerServiceTest.java +++ b/services/robotests/backup/src/com/android/server/backup/UserBackupManagerServiceTest.java @@ -63,6 +63,7 @@ import com.android.server.testing.shadows.ShadowApplicationPackageManager; import com.android.server.testing.shadows.ShadowBinder; import com.android.server.testing.shadows.ShadowKeyValueBackupJob; import com.android.server.testing.shadows.ShadowKeyValueBackupTask; +import com.android.server.testing.shadows.ShadowSystemServiceRegistry; import org.junit.After; import org.junit.Before; @@ -88,7 +89,12 @@ import java.util.List; * UserBackupManagerService} that performs operations for its target user. */ @RunWith(RobolectricTestRunner.class) -@Config(shadows = {ShadowAppBackupUtils.class, ShadowApplicationPackageManager.class}) +@Config( + shadows = { + ShadowAppBackupUtils.class, + ShadowApplicationPackageManager.class, + ShadowSystemServiceRegistry.class + }) @Presubmit public class UserBackupManagerServiceTest { private static final String TAG = "BMSTest"; diff --git a/services/robotests/backup/src/com/android/server/backup/internal/SetupObserverTest.java b/services/robotests/backup/src/com/android/server/backup/internal/SetupObserverTest.java index 44e9e6aecacd..e49425b00322 100644 --- a/services/robotests/backup/src/com/android/server/backup/internal/SetupObserverTest.java +++ b/services/robotests/backup/src/com/android/server/backup/internal/SetupObserverTest.java @@ -32,6 +32,7 @@ import com.android.server.backup.TransportManager; import com.android.server.backup.UserBackupManagerService; import com.android.server.backup.testing.BackupManagerServiceTestUtils; import com.android.server.testing.shadows.ShadowApplicationPackageManager; +import com.android.server.testing.shadows.ShadowSystemServiceRegistry; import org.junit.Before; import org.junit.Test; @@ -51,7 +52,12 @@ import java.io.File; * UserBackupManagerService}. */ @RunWith(RobolectricTestRunner.class) -@Config(shadows = {ShadowApplicationPackageManager.class, ShadowJobScheduler.class}) +@Config( + shadows = { + ShadowApplicationPackageManager.class, + ShadowJobScheduler.class, + ShadowSystemServiceRegistry.class + }) @Presubmit public class SetupObserverTest { private static final String TAG = "SetupObserverTest"; diff --git a/services/robotests/backup/src/com/android/server/backup/keyvalue/KeyValueBackupTaskTest.java b/services/robotests/backup/src/com/android/server/backup/keyvalue/KeyValueBackupTaskTest.java index 4aba1f487f49..6a90d0bbd77f 100644 --- a/services/robotests/backup/src/com/android/server/backup/keyvalue/KeyValueBackupTaskTest.java +++ b/services/robotests/backup/src/com/android/server/backup/keyvalue/KeyValueBackupTaskTest.java @@ -120,6 +120,7 @@ import com.android.server.testing.shadows.ShadowApplicationPackageManager; import com.android.server.testing.shadows.ShadowBackupDataInput; import com.android.server.testing.shadows.ShadowBackupDataOutput; import com.android.server.testing.shadows.ShadowEventLog; +import com.android.server.testing.shadows.ShadowSystemServiceRegistry; import com.google.common.truth.IterableSubject; @@ -163,10 +164,11 @@ import java.util.stream.Stream; ShadowBackupDataInput.class, ShadowBackupDataOutput.class, ShadowEventLog.class, - ShadowQueuedWork.class + ShadowQueuedWork.class, + ShadowSystemServiceRegistry.class }) @Presubmit -public class KeyValueBackupTaskTest { +public class KeyValueBackupTaskTest { private static final PackageData PACKAGE_1 = keyValuePackage(1); private static final PackageData PACKAGE_2 = keyValuePackage(2); private static final String BACKUP_AGENT_SHARED_PREFS_SYNCHRONIZER_CLASS = diff --git a/services/robotests/src/com/android/server/testing/shadows/ShadowSystemServiceRegistry.java b/services/robotests/src/com/android/server/testing/shadows/ShadowSystemServiceRegistry.java new file mode 100644 index 000000000000..c59798fc92fc --- /dev/null +++ b/services/robotests/src/com/android/server/testing/shadows/ShadowSystemServiceRegistry.java @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2019 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.app.SystemServiceRegistry; +import android.app.job.JobSchedulerFrameworkInitializer; + +import org.robolectric.annotation.Implementation; +import org.robolectric.annotation.Implements; +import org.robolectric.util.ReflectionHelpers; + +/** + * Shadow of {@link SystemServiceRegistry} + * + * <p>JobSchedulerFrameworkInitializer contains a static initializer registering JobScheduler as a + * system service. We need to make sure the initializer is run before the tests that use + * JobScheduler. And we're putting this on the static initializer of SystemServiceRegistry since + * other services are registered here. + */ +@Implements(className = "android.app.SystemServiceRegistry") +public class ShadowSystemServiceRegistry { + @Implementation + protected static void __staticInitializer__() { + // Make sure the static init in the real class is still executed. + ReflectionHelpers.callStaticMethod(SystemServiceRegistry.class, "__staticInitializer__"); + try { + Class.forName(JobSchedulerFrameworkInitializer.class.getCanonicalName()); + } catch (ClassNotFoundException e) { + // Rethrowing as an unchecked exception because checked exceptions are not allowed in + // static blocks. + throw new ExceptionInInitializerError(e); + } + } +} |