DevicePolicyManagerServiceTestable.java revision 3f3657a61b54d495bf2e692289eb92a48fe5a0b2
1/*
2 * Copyright (C) 2015 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 */
16package com.android.server.devicepolicy;
17
18import com.android.internal.widget.LockPatternUtils;
19
20import android.app.IActivityManager;
21import android.app.NotificationManager;
22import android.app.backup.IBackupManager;
23import android.content.pm.IPackageManager;
24import android.content.pm.PackageManagerInternal;
25import android.media.IAudioService;
26import android.os.Looper;
27import android.os.PowerManagerInternal;
28import android.os.UserHandle;
29import android.os.UserManager;
30import android.os.UserManagerInternal;
31import android.view.IWindowManager;
32
33import java.io.File;
34
35import static org.mockito.Matchers.eq;
36import static org.mockito.Mockito.when;
37
38/**
39 * Overrides {@link #DevicePolicyManagerService} for dependency injection.
40 */
41public class DevicePolicyManagerServiceTestable extends DevicePolicyManagerService {
42    /**
43     * Overrides {@link #Owners} for dependency injection.
44     */
45    public static class OwnersTestable extends Owners {
46        public static final String LEGACY_FILE = "legacy.xml";
47        public static final String DEVICE_OWNER_FILE = "device_owner2.xml";
48        public static final String PROFILE_OWNER_FILE_BASE = "profile_owner.xml";
49
50        private final File mLegacyFile;
51        private final File mDeviceOwnerFile;
52        private final File mProfileOwnerBase;
53
54        public OwnersTestable(DpmMockContext context) {
55            super(context, context.userManager, context.userManagerInternal);
56            mLegacyFile = new File(context.dataDir, LEGACY_FILE);
57            mDeviceOwnerFile = new File(context.dataDir, DEVICE_OWNER_FILE);
58            mProfileOwnerBase = new File(context.dataDir, PROFILE_OWNER_FILE_BASE);
59        }
60
61        @Override
62        File getLegacyConfigFileWithTestOverride() {
63            return mLegacyFile;
64        }
65
66        @Override
67        File getDeviceOwnerFileWithTestOverride() {
68            return mDeviceOwnerFile;
69        }
70
71        @Override
72        File getProfileOwnerFileWithTestOverride(int userId) {
73            return new File(mDeviceOwnerFile.getAbsoluteFile() + "-" + userId);
74        }
75    }
76
77    public final DpmMockContext context;
78
79    public DevicePolicyManagerServiceTestable(DpmMockContext context, File dataDir) {
80        this(new MockInjector(context, dataDir));
81    }
82
83    private DevicePolicyManagerServiceTestable(MockInjector injector) {
84        super(injector);
85        this.context = injector.context;
86    }
87
88    private static class MockInjector extends Injector {
89
90        public final DpmMockContext context;
91
92        public final File dataDir;
93
94        private MockInjector(DpmMockContext context, File dataDir) {
95            super(context);
96            this.context = context;
97            this.dataDir = dataDir;
98        }
99
100        @Override
101        Owners newOwners() {
102            return new OwnersTestable(context);
103        }
104
105        @Override
106        UserManager getUserManager() {
107            return context.userManager;
108        }
109
110        @Override
111        UserManagerInternal getUserManagerInternal() {
112            return context.userManagerInternal;
113        }
114
115        @Override
116        PackageManagerInternal getPackageManagerInternal() {
117            return context.packageManagerInternal;
118        }
119
120        @Override
121        PowerManagerInternal getPowerManagerInternal() {
122            return context.powerManagerInternal;
123        }
124
125        @Override
126        NotificationManager getNotificationManager() {
127            return context.notificationManager;
128        }
129
130        @Override
131        IWindowManager getIWindowManager() {
132            return context.iwindowManager;
133        }
134
135        @Override
136        IActivityManager getIActivityManager() {
137            return context.iactivityManager;
138        }
139
140        @Override
141        IPackageManager getIPackageManager() {
142            return context.ipackageManager;
143        }
144
145        @Override
146        IBackupManager getIBackupManager() {
147            return context.ibackupManager;
148        }
149
150        @Override
151        IAudioService getIAudioService() {
152            return context.iaudioService;
153        }
154
155        @Override
156        Looper getMyLooper() {
157            return Looper.getMainLooper();
158        }
159
160        @Override
161        LockPatternUtils newLockPatternUtils() {
162            return context.lockPatternUtils;
163        }
164
165        @Override
166        String getDevicePolicyFilePathForSystemUser() {
167            return context.systemUserDataDir.getAbsolutePath() + "/";
168        }
169
170        @Override
171        long binderClearCallingIdentity() {
172            return context.binder.clearCallingIdentity();
173        }
174
175        @Override
176        void binderRestoreCallingIdentity(long token) {
177            context.binder.restoreCallingIdentity(token);
178        }
179
180        @Override
181        int binderGetCallingUid() {
182            return context.binder.getCallingUid();
183        }
184
185        @Override
186        int binderGetCallingPid() {
187            return context.binder.getCallingPid();
188        }
189
190        @Override
191        UserHandle binderGetCallingUserHandle() {
192            return context.binder.getCallingUserHandle();
193        }
194
195        @Override
196        boolean binderIsCallingUidMyUid() {
197            return context.binder.isCallerUidMyUid();
198        }
199
200        @Override
201        File environmentGetUserSystemDirectory(int userId) {
202            return context.environment.getUserSystemDirectory(userId);
203        }
204
205        @Override
206        void powerManagerGoToSleep(long time, int reason, int flags) {
207            context.powerManager.goToSleep(time, reason, flags);
208        }
209
210        @Override
211        void powerManagerReboot(String reason) {
212            context.powerManager.reboot(reason);
213        }
214
215        @Override
216        boolean systemPropertiesGetBoolean(String key, boolean def) {
217            return context.systemProperties.getBoolean(key, def);
218        }
219
220        @Override
221        long systemPropertiesGetLong(String key, long def) {
222            return context.systemProperties.getLong(key, def);
223        }
224
225        @Override
226        String systemPropertiesGet(String key, String def) {
227            return context.systemProperties.get(key, def);
228        }
229
230        @Override
231        String systemPropertiesGet(String key) {
232            return context.systemProperties.get(key);
233        }
234
235        @Override
236        void systemPropertiesSet(String key, String value) {
237            context.systemProperties.set(key, value);
238        }
239
240        @Override
241        boolean userManagerIsSplitSystemUser() {
242            return context.userManagerForMock.isSplitSystemUser();
243        }
244
245        @Override
246        int settingsSecureGetIntForUser(String name, int def, int userHandle) {
247            return context.settings.settingsSecureGetIntForUser(name, def, userHandle);
248        }
249
250        @Override
251        void settingsSecurePutIntForUser(String name, int value, int userHandle) {
252            context.settings.settingsSecurePutIntForUser(name, value, userHandle);
253        }
254
255        @Override
256        void settingsSecurePutStringForUser(String name, String value, int userHandle) {
257            context.settings.settingsSecurePutStringForUser(name, value, userHandle);
258        }
259
260        @Override
261        void settingsGlobalPutStringForUser(String name, String value, int userHandle) {
262            context.settings.settingsGlobalPutStringForUser(name, value, userHandle);
263        }
264
265        @Override
266        void settingsSecurePutInt(String name, int value) {
267            context.settings.settingsSecurePutInt(name, value);
268        }
269
270        @Override
271        void settingsGlobalPutInt(String name, int value) {
272            context.settings.settingsGlobalPutInt(name, value);
273        }
274
275        @Override
276        void settingsSecurePutString(String name, String value) {
277            context.settings.settingsSecurePutString(name, value);
278        }
279
280        @Override
281        void settingsGlobalPutString(String name, String value) {
282            context.settings.settingsGlobalPutString(name, value);
283        }
284
285        @Override
286        int settingsGlobalGetInt(String name, int def) {
287            return context.settings.settingsGlobalGetInt(name, def);
288        }
289
290        @Override
291        void securityLogSetLoggingEnabledProperty(boolean enabled) {
292            context.settings.securityLogSetLoggingEnabledProperty(enabled);
293        }
294    }
295}
296