• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.internal.util.test;
18 
19 import com.android.server.LocalServices;
20 
21 import org.junit.rules.TestRule;
22 import org.junit.runner.Description;
23 import org.junit.runners.model.Statement;
24 
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29 
30 /**
31  * JUnit Rule helps override /restore {@link LocalServices} state.
32  */
33 public class LocalServiceKeeperRule implements TestRule {
34 
35     private final Map<Class<?>, Object> mOverriddenServices = new HashMap<>();
36     private final List<Class<?>> mAddedServices = new ArrayList<>();
37 
38     private volatile boolean mRuleApplied = false;
39 
40     /**
41      * Overrides service in LocalServices. Service will be restored to original after test run.
42      */
overrideLocalService(Class<T> type, T service)43     public <T> void overrideLocalService(Class<T> type, T service) {
44         if (!mRuleApplied) {
45             throw new IllegalStateException("Can't override service without applying rule");
46         }
47         if (mOverriddenServices.containsKey(type) || mAddedServices.contains(type)) {
48             throw new IllegalArgumentException("Type already overridden: " + type);
49         }
50 
51         T currentService = LocalServices.getService(type);
52         if (currentService != null) {
53             mOverriddenServices.put(type, currentService);
54             LocalServices.removeServiceForTest(type);
55         } else {
56             mAddedServices.add(type);
57         }
58         LocalServices.addService(type, service);
59     }
60 
61     @Override
apply(Statement base, Description description)62     public Statement apply(Statement base, Description description) {
63         return new Statement() {
64             public void evaluate() throws Throwable {
65                 try {
66                     mRuleApplied = true;
67                     base.evaluate();
68                 } finally {
69                     mRuleApplied = false;
70                     mAddedServices.forEach(LocalServices::removeServiceForTest);
71                     mOverriddenServices.forEach((clazz, service) -> {
72                         LocalServices.removeServiceForTest(clazz);
73                         LocalServices.addService((Class) clazz, service);
74                     });
75                     mAddedServices.clear();
76                     mOverriddenServices.clear();
77                 }
78             }
79         };
80     }
81 }
82