ShadowActivityManager.java revision 82029aedff5c6ec11619a558bf7424718ef22403
1/*
2 * Copyright (C) 2018 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
17package com.android.settingslib.testutils.shadow;
18
19import android.app.ActivityManager;
20
21import org.robolectric.RuntimeEnvironment;
22import org.robolectric.annotation.Implementation;
23import org.robolectric.annotation.Implements;
24import org.robolectric.annotation.Resetter;
25import org.robolectric.shadow.api.Shadow;
26
27@Implements(ActivityManager.class)
28public class ShadowActivityManager {
29    private static int sCurrentUserId = 0;
30    private int mUserSwitchedTo = -1;
31
32    @Resetter
33    public void reset() {
34        sCurrentUserId = 0;
35        mUserSwitchedTo = 0;
36    }
37
38    @Implementation
39    public static int getCurrentUser() {
40        return sCurrentUserId;
41    }
42
43    @Implementation
44    public boolean switchUser(int userId) {
45        mUserSwitchedTo = userId;
46        return true;
47    }
48
49    public boolean getSwitchUserCalled() {
50        return mUserSwitchedTo != -1;
51    }
52
53    public int getUserSwitchedTo() {
54        return mUserSwitchedTo;
55    }
56
57    public static void setCurrentUser(int userId) {
58        sCurrentUserId = userId;
59    }
60
61    public static ShadowActivityManager getShadow() {
62        return (ShadowActivityManager) Shadow.extract(
63                RuntimeEnvironment.application.getSystemService(ActivityManager.class));
64    }
65}
66