• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
16 package com.android.server.devicepolicy;
17 
18 import android.content.ComponentName;
19 import android.content.Intent;
20 import android.os.BaseBundle;
21 import android.os.Bundle;
22 import android.os.UserHandle;
23 import android.util.ArraySet;
24 
25 import com.android.server.pm.RestrictionsSet;
26 import com.android.server.pm.UserRestrictionsUtils;
27 
28 import com.google.common.base.Objects;
29 
30 import org.hamcrest.BaseMatcher;
31 import org.hamcrest.Description;
32 import org.hamcrest.Matcher;
33 import org.mockito.hamcrest.MockitoHamcrest;
34 
35 import java.util.Arrays;
36 import java.util.Set;
37 
38 public class MockUtils {
MockUtils()39     private MockUtils() {
40     }
41 
checkUserHandle(final int userId)42     public static UserHandle checkUserHandle(final int userId) {
43         final Matcher<UserHandle> m = new BaseMatcher<UserHandle>() {
44             @Override
45             public boolean matches(Object item) {
46                 if (item == null) return false;
47                 return Objects.equal(((UserHandle) item).getIdentifier(), userId);
48             }
49 
50             @Override
51             public void describeTo(Description description) {
52                 description.appendText("UserHandle: user-id= \"" + userId + "\"");
53             }
54         };
55         return MockitoHamcrest.argThat(m);
56     }
57 
checkIntentComponent(final ComponentName component)58     public static Intent checkIntentComponent(final ComponentName component) {
59         final Matcher<Intent> m = new BaseMatcher<Intent>() {
60             @Override
61             public boolean matches(Object item) {
62                 if (item == null) return false;
63                 return Objects.equal(((Intent) item).getComponent(), component);
64             }
65 
66             @Override
67             public void describeTo(Description description) {
68                 description.appendText("Intent: component=\"" + component + "\"");
69             }
70         };
71         return MockitoHamcrest.argThat(m);
72     }
73 
checkIntentAction(final String action)74     public static Intent checkIntentAction(final String action) {
75         final Matcher<Intent> m = new BaseMatcher<Intent>() {
76             @Override
77             public boolean matches(Object item) {
78                 if (item == null) return false;
79                 return Objects.equal(((Intent) item).getAction(), action);
80             }
81 
82             @Override
83             public void describeTo(Description description) {
84                 description.appendText("Intent: action=\"" + action + "\"");
85             }
86         };
87         return MockitoHamcrest.argThat(m);
88     }
89 
checkIntent(final Intent intent)90     public static Intent checkIntent(final Intent intent) {
91         final Matcher<Intent> m = new BaseMatcher<Intent>() {
92             @Override
93             public boolean matches(Object item) {
94                 if (item == null) return false;
95                 if (!intent.filterEquals((Intent) item)) return false;
96                 BaseBundle extras = intent.getExtras();
97                 BaseBundle itemExtras = ((Intent) item).getExtras();
98                 return (extras == itemExtras) || (extras != null &&
99                         extras.kindofEquals(itemExtras));
100             }
101             @Override
102             public void describeTo(Description description) {
103                 description.appendText(intent.toString());
104             }
105         };
106         return MockitoHamcrest.argThat(m);
107     }
108 
checkUserRestrictions(String... keys)109     public static Bundle checkUserRestrictions(String... keys) {
110         final Bundle expected = DpmTestUtils.newRestrictions(
111                 java.util.Objects.requireNonNull(keys));
112         final Matcher<Bundle> m = new BaseMatcher<Bundle>() {
113             @Override
114             public boolean matches(Object item) {
115                 if (item == null) {
116                     return false;
117                 }
118                 return UserRestrictionsUtils.areEqual((Bundle) item, expected);
119             }
120 
121             @Override
122             public void describeTo(Description description) {
123                 description.appendText("User restrictions=" + getRestrictionsAsString(expected));
124             }
125         };
126         return MockitoHamcrest.argThat(m);
127     }
128 
checkUserRestrictions(int userId, String... keys)129     public static RestrictionsSet checkUserRestrictions(int userId, String... keys) {
130         final RestrictionsSet expected = DpmTestUtils.newRestrictions(userId,
131                 java.util.Objects.requireNonNull(keys));
132         final Matcher<RestrictionsSet> m = new BaseMatcher<RestrictionsSet>() {
133             @Override
134             public boolean matches(Object item) {
135                 if (item == null) return false;
136                 RestrictionsSet actual = (RestrictionsSet) item;
137                 return UserRestrictionsUtils.areEqual(expected.getRestrictions(userId),
138                         actual.getRestrictions(userId));
139             }
140 
141             @Override
142             public void describeTo(Description description) {
143                 description.appendText("User restrictions=" + getRestrictionsAsString(expected));
144             }
145         };
146         return MockitoHamcrest.argThat(m);
147     }
148 
checkApps(String... adminApps)149     public static Set<String> checkApps(String... adminApps) {
150         final Matcher<Set<String>> m = new BaseMatcher<Set<String>>() {
151             @Override
152             public boolean matches(Object item) {
153                 if (item == null) return false;
154                 final Set<String> actualApps = (Set<String>) item;
155                 if (adminApps.length != actualApps.size()) {
156                     return false;
157                 }
158                 final Set<String> copyOfApps = new ArraySet<>(actualApps);
159                 for (String adminApp : adminApps) {
160                     copyOfApps.remove(adminApp);
161                 }
162                 return copyOfApps.isEmpty();
163             }
164 
165             @Override
166             public void describeTo(Description description) {
167                 description.appendText("Apps=" + Arrays.toString(adminApps));
168             }
169         };
170         return MockitoHamcrest.argThat(m);
171     }
172 
getRestrictionsAsString(RestrictionsSet r)173     private static String getRestrictionsAsString(RestrictionsSet r) {
174         final StringBuilder sb = new StringBuilder();
175         sb.append("{");
176 
177         if (r != null) {
178             String sep = "";
179             for (int i = 0; i < r.size(); i++) {
180                 sb.append(sep);
181                 sep = ",";
182                 sb.append(
183                         String.format("%s= %s", r.keyAt(i), getRestrictionsAsString(r.valueAt(i))));
184             }
185         }
186         sb.append("}");
187         return sb.toString();
188     }
189 
getRestrictionsAsString(Bundle b)190     private static String getRestrictionsAsString(Bundle b) {
191         final StringBuilder sb = new StringBuilder();
192         sb.append("[");
193 
194         if (b != null) {
195             String sep = "";
196             for (String key : b.keySet()) {
197                 sb.append(sep);
198                 sep = ",";
199                 sb.append(key);
200             }
201         }
202         sb.append("]");
203         return sb.toString();
204     }
205 }
206