Fix Robolectric tests that use JobScheduler

In ag/8881405 registering JobScheduler with SystemRegistryService has moved int JobSchedulerFrameworkInitializer. Make sure that class is loaded before the tests are run.

Bug: 138840929
Test: atest RunBackupFrameworksServicesRoboTest
Change-Id: Idacab9f8597b57df5b61a5c6619090a242a76264
This commit is contained in:
Ruslan Tkhakokhov
2019-08-05 09:13:49 +01:00
parent 0dd1be75c3
commit cb28da9cb1
7 changed files with 80 additions and 6 deletions

View File

@@ -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";

View File

@@ -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;

View File

@@ -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;

View File

@@ -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";

View File

@@ -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";

View File

@@ -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 =

View File

@@ -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);
}
}
}