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 com.google.common.base.Objects; 19 20 import com.android.internal.util.Preconditions; 21 import com.android.server.pm.UserRestrictionsUtils; 22 23 import android.content.ComponentName; 24 import android.content.Intent; 25 import android.os.Bundle; 26 import android.os.UserHandle; 27 28 import org.hamcrest.BaseMatcher; 29 import org.hamcrest.Description; 30 import org.hamcrest.Matcher; 31 import org.mockito.Mockito; 32 33 public class MockUtils { MockUtils()34 private MockUtils() { 35 } 36 checkUserHandle(final int userId)37 public static UserHandle checkUserHandle(final int userId) { 38 final Matcher<UserHandle> m = new BaseMatcher<UserHandle>() { 39 @Override 40 public boolean matches(Object item) { 41 if (item == null) return false; 42 return Objects.equal(((UserHandle) item).getIdentifier(), userId); 43 } 44 45 @Override 46 public void describeTo(Description description) { 47 description.appendText("UserHandle: user-id= \"" + userId + "\""); 48 } 49 }; 50 return Mockito.argThat(m); 51 } 52 checkIntentComponent(final ComponentName component)53 public static Intent checkIntentComponent(final ComponentName component) { 54 final Matcher<Intent> m = new BaseMatcher<Intent>() { 55 @Override 56 public boolean matches(Object item) { 57 if (item == null) return false; 58 return Objects.equal(((Intent) item).getComponent(), component); 59 } 60 61 @Override 62 public void describeTo(Description description) { 63 description.appendText("Intent: component=\"" + component + "\""); 64 } 65 }; 66 return Mockito.argThat(m); 67 } 68 checkIntentAction(final String action)69 public static Intent checkIntentAction(final String action) { 70 final Matcher<Intent> m = new BaseMatcher<Intent>() { 71 @Override 72 public boolean matches(Object item) { 73 if (item == null) return false; 74 return Objects.equal(((Intent) item).getAction(), action); 75 } 76 77 @Override 78 public void describeTo(Description description) { 79 description.appendText("Intent: action=\"" + action + "\""); 80 } 81 }; 82 return Mockito.argThat(m); 83 } 84 checkUserRestrictions(String... keys)85 public static Bundle checkUserRestrictions(String... keys) { 86 final Bundle expected = DpmTestUtils.newRestrictions(Preconditions.checkNotNull(keys)); 87 final Matcher<Bundle> m = new BaseMatcher<Bundle>() { 88 @Override 89 public boolean matches(Object item) { 90 if (item == null) return false; 91 return UserRestrictionsUtils.areEqual((Bundle) item, expected); 92 } 93 94 @Override 95 public void describeTo(Description description) { 96 description.appendText("User restrictions=" + getRestrictionsAsString(expected)); 97 } 98 }; 99 return Mockito.argThat(m); 100 } 101 getRestrictionsAsString(Bundle b)102 private static String getRestrictionsAsString(Bundle b) { 103 final StringBuilder sb = new StringBuilder(); 104 sb.append("["); 105 106 if (b != null) { 107 String sep = ""; 108 for (String key : b.keySet()) { 109 sb.append(sep); 110 sep = ","; 111 sb.append(key); 112 } 113 } 114 sb.append("]"); 115 return sb.toString(); 116 } 117 } 118