diff options
author | Ruslan Tkhakokhov <rthakohov@google.com> | 2019-08-05 09:13:49 +0100 |
---|---|---|
committer | Ruslan Tkhakokhov <rthakohov@google.com> | 2019-08-07 19:05:34 +0000 |
commit | cb28da9cb18f1a889ca16b3ba090cb3f1be1f544 (patch) | |
tree | 59b42314622859a02270b3f5c514aa77010472f8 /services/robotests/src | |
parent | 0dd1be75c3ff9be8075b2ad93185c29e2eb96af6 (diff) |
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
Diffstat (limited to 'services/robotests/src')
-rw-r--r-- | services/robotests/src/com/android/server/testing/shadows/ShadowSystemServiceRegistry.java | 48 |
1 files changed, 48 insertions, 0 deletions
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); + } + } +} |