MockUtils.java revision 972e236e84fd4073f7ecc40f2de326b388203dfb
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.google.common.base.Objects; 19 20import com.android.internal.util.Preconditions; 21import com.android.server.pm.UserRestrictionsUtils; 22 23import android.content.ComponentName; 24import android.content.Intent; 25import android.os.Bundle; 26import android.os.UserHandle; 27 28import org.hamcrest.BaseMatcher; 29import org.hamcrest.Description; 30import org.hamcrest.Matcher; 31import org.mockito.Mockito; 32 33public class MockUtils { 34 private MockUtils() { 35 } 36 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 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 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 85 public static Intent checkIntent(final Intent intent) { 86 final Matcher<Intent> m = new BaseMatcher<Intent>() { 87 @Override 88 public boolean matches(Object item) { 89 if (item == null) return false; 90 return intent.filterEquals((Intent) item); 91 } 92 @Override 93 public void describeTo(Description description) { 94 description.appendText(intent.toString()); 95 } 96 }; 97 return Mockito.argThat(m); 98 } 99 100 public static Bundle checkUserRestrictions(String... keys) { 101 final Bundle expected = DpmTestUtils.newRestrictions(Preconditions.checkNotNull(keys)); 102 final Matcher<Bundle> m = new BaseMatcher<Bundle>() { 103 @Override 104 public boolean matches(Object item) { 105 if (item == null) return false; 106 return UserRestrictionsUtils.areEqual((Bundle) item, expected); 107 } 108 109 @Override 110 public void describeTo(Description description) { 111 description.appendText("User restrictions=" + getRestrictionsAsString(expected)); 112 } 113 }; 114 return Mockito.argThat(m); 115 } 116 117 private static String getRestrictionsAsString(Bundle b) { 118 final StringBuilder sb = new StringBuilder(); 119 sb.append("["); 120 121 if (b != null) { 122 String sep = ""; 123 for (String key : b.keySet()) { 124 sb.append(sep); 125 sep = ","; 126 sb.append(key); 127 } 128 } 129 sb.append("]"); 130 return sb.toString(); 131 } 132} 133