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 return checkUserRestrictions(expected); 113 } 114 checkUserRestrictions(Bundle expected)115 public static Bundle checkUserRestrictions(Bundle expected) { 116 return createUserRestrictionsBundleMatcher(expected); 117 } 118 createUserRestrictionsBundleMatcher(Bundle expected)119 private static Bundle createUserRestrictionsBundleMatcher(Bundle expected) { 120 final Matcher<Bundle> m = new BaseMatcher<Bundle>() { 121 @Override 122 public boolean matches(Object item) { 123 if (item == null) { 124 return false; 125 } 126 return UserRestrictionsUtils.areEqual((Bundle) item, expected); 127 } 128 129 @Override 130 public void describeTo(Description description) { 131 description.appendText("User restrictions=" + getRestrictionsAsString(expected)); 132 } 133 }; 134 return MockitoHamcrest.argThat(m); 135 } 136 checkUserRestrictions(int userId, String... keys)137 public static RestrictionsSet checkUserRestrictions(int userId, String... keys) { 138 final RestrictionsSet expected = DpmTestUtils.newRestrictions(userId, 139 java.util.Objects.requireNonNull(keys)); 140 return checkUserRestrictions(userId, expected); 141 } 142 checkUserRestrictions(int userId, RestrictionsSet expected)143 public static RestrictionsSet checkUserRestrictions(int userId, RestrictionsSet expected) { 144 return createUserRestrictionsSetMatcher(userId, expected); 145 } 146 createUserRestrictionsSetMatcher( int userId, RestrictionsSet expected)147 private static RestrictionsSet createUserRestrictionsSetMatcher( 148 int userId, RestrictionsSet expected) { 149 final Matcher<RestrictionsSet> m = new BaseMatcher<RestrictionsSet>() { 150 @Override 151 public boolean matches(Object item) { 152 if (item == null) return false; 153 RestrictionsSet actual = (RestrictionsSet) item; 154 return UserRestrictionsUtils.areEqual(expected.getRestrictions(userId), 155 actual.getRestrictions(userId)); 156 } 157 158 @Override 159 public void describeTo(Description description) { 160 description.appendText("User restrictions=" + getRestrictionsAsString(expected)); 161 } 162 }; 163 return MockitoHamcrest.argThat(m); 164 } 165 checkApps(String... adminApps)166 public static Set<String> checkApps(String... adminApps) { 167 final Matcher<Set<String>> m = new BaseMatcher<Set<String>>() { 168 @Override 169 public boolean matches(Object item) { 170 if (item == null) return false; 171 final Set<String> actualApps = (Set<String>) item; 172 if (adminApps.length != actualApps.size()) { 173 return false; 174 } 175 final Set<String> copyOfApps = new ArraySet<>(actualApps); 176 for (String adminApp : adminApps) { 177 copyOfApps.remove(adminApp); 178 } 179 return copyOfApps.isEmpty(); 180 } 181 182 @Override 183 public void describeTo(Description description) { 184 description.appendText("Apps=" + Arrays.toString(adminApps)); 185 } 186 }; 187 return MockitoHamcrest.argThat(m); 188 } 189 getRestrictionsAsString(RestrictionsSet r)190 private static String getRestrictionsAsString(RestrictionsSet r) { 191 final StringBuilder sb = new StringBuilder(); 192 sb.append("{"); 193 194 if (r != null) { 195 String sep = ""; 196 for (int i = 0; i < r.size(); i++) { 197 sb.append(sep); 198 sep = ","; 199 sb.append( 200 String.format("%s= %s", r.keyAt(i), getRestrictionsAsString(r.valueAt(i)))); 201 } 202 } 203 sb.append("}"); 204 return sb.toString(); 205 } 206 getRestrictionsAsString(Bundle b)207 private static String getRestrictionsAsString(Bundle b) { 208 final StringBuilder sb = new StringBuilder(); 209 sb.append("["); 210 211 if (b != null) { 212 String sep = ""; 213 for (String key : b.keySet()) { 214 sb.append(sep); 215 sep = ","; 216 sb.append(key); 217 } 218 } 219 sb.append("]"); 220 return sb.toString(); 221 } 222 } 223