blob: 58db192f117a3fd0b4262f768d26ed7a32797864 [file] [log] [blame]
Makoto Onukicc4bbeb2015-09-17 10:28:24 -07001/*
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
Makoto Onuki1a2cd742015-11-16 13:51:27 -080020import com.android.internal.util.Preconditions;
21import com.android.server.pm.UserRestrictionsUtils;
22
Makoto Onukif76b06a2015-09-22 15:03:44 -070023import android.content.ComponentName;
24import android.content.Intent;
Makoto Onuki1a2cd742015-11-16 13:51:27 -080025import android.os.Bundle;
Makoto Onukicc4bbeb2015-09-17 10:28:24 -070026import 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
Makoto Onukif76b06a2015-09-22 15:03:44 -070053 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 Onuki1a2cd742015-11-16 13:51:27 -080084
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
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 }
Makoto Onukicc4bbeb2015-09-17 10:28:24 -0700117}