Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 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 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 20 | import com.android.internal.util.Preconditions; |
| 21 | import com.android.server.pm.UserRestrictionsUtils; |
| 22 | |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 23 | import android.content.ComponentName; |
| 24 | import android.content.Intent; |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 25 | import android.os.Bundle; |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 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 { |
| 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 | |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 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 | } |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 84 | |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 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 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 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 | } |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 132 | } |