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