blob: cceb2d2761fe4a1915a19925a1646251c90dc3f4 [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 */
16
17package com.android.server.devicepolicy;
18
Makoto Onuki068c54a2015-10-13 14:34:03 -070019import com.google.android.collect.Lists;
20import com.google.android.collect.Sets;
21
22import android.content.Context;
23import android.os.Bundle;
Makoto Onukicc4bbeb2015-09-17 10:28:24 -070024import android.os.FileUtils;
Makoto Onukid932f762015-09-29 16:53:38 -070025import android.os.Parcel;
26import android.os.Parcelable;
Makoto Onuki068c54a2015-10-13 14:34:03 -070027import android.test.AndroidTestCase;
Makoto Onukicc4bbeb2015-09-17 10:28:24 -070028import android.util.Log;
29import android.util.Printer;
30
31import org.junit.Assert;
32
Makoto Onuki068c54a2015-10-13 14:34:03 -070033import java.io.BufferedReader;
Makoto Onukicc4bbeb2015-09-17 10:28:24 -070034import java.io.File;
Makoto Onuki068c54a2015-10-13 14:34:03 -070035import java.io.FileWriter;
36import java.io.IOException;
37import java.io.InputStreamReader;
38import java.io.PrintWriter;
39import java.util.ArrayList;
40import java.util.Collections;
Makoto Onukif76b06a2015-09-22 15:03:44 -070041import java.util.List;
Makoto Onuki068c54a2015-10-13 14:34:03 -070042import java.util.Objects;
43import java.util.Set;
Makoto Onukicc4bbeb2015-09-17 10:28:24 -070044
Makoto Onuki068c54a2015-10-13 14:34:03 -070045import junit.framework.AssertionFailedError;
Makoto Onukicc4bbeb2015-09-17 10:28:24 -070046
Makoto Onuki068c54a2015-10-13 14:34:03 -070047public class DpmTestUtils extends AndroidTestCase {
Makoto Onukicc4bbeb2015-09-17 10:28:24 -070048 public static void clearDir(File dir) {
49 if (dir.exists()) {
50 Assert.assertTrue("failed to delete dir", FileUtils.deleteContents(dir));
51 }
52 dir.mkdirs();
Makoto Onukif76b06a2015-09-22 15:03:44 -070053 Log.i(DpmTestBase.TAG, "Created " + dir);
54 }
55
56 public static int getListSizeAllowingNull(List<?> list) {
57 return list == null ? 0 : list.size();
Makoto Onukicc4bbeb2015-09-17 10:28:24 -070058 }
59
Makoto Onuki068c54a2015-10-13 14:34:03 -070060 public static Bundle newRestrictions(String... restrictions) {
61 final Bundle ret = new Bundle();
62 for (String restriction : restrictions) {
63 ret.putBoolean(restriction, true);
64 }
65 return ret;
66 }
67
68 public static void assertRestrictions(Bundle expected, Bundle actual) {
69 final ArrayList<String> elist;
70 if (expected == null) {
71 elist = null;
72 } else {
73 elist = Lists.newArrayList();
74 for (String key : expected.keySet()) {
75 if (expected.getBoolean(key)) {
76 elist.add(key);
77 }
78 }
79 Collections.sort(elist);
80 }
81
82 final ArrayList<String> alist;
83 if (actual == null) {
84 alist = null;
85 } else {
86 alist = Lists.newArrayList();
87 for (String key : actual.keySet()) {
88 if (actual.getBoolean(key)) {
89 alist.add(key);
90 }
91 }
92 Collections.sort(alist);
93 }
94
95 assertEquals(elist, alist);
96 }
97
Makoto Onukid932f762015-09-29 16:53:38 -070098 public static <T extends Parcelable> T cloneParcelable(T source) {
99 Parcel p = Parcel.obtain();
100 p.writeParcelable(source, 0);
101 p.setDataPosition(0);
102 final T clone = p.readParcelable(DpmTestUtils.class.getClassLoader());
103 p.recycle();
104 return clone;
105 }
106
Makoto Onukicc4bbeb2015-09-17 10:28:24 -0700107 public static Printer LOG_PRINTER = new Printer() {
108 @Override
109 public void println(String x) {
110 Log.i(DpmTestBase.TAG, x);
111 }
112 };
Makoto Onuki068c54a2015-10-13 14:34:03 -0700113
114 public static String readAsset(Context context, String assetPath) throws IOException {
115 final StringBuilder sb = new StringBuilder();
116 try (BufferedReader br = new BufferedReader(
117 new InputStreamReader(
118 context.getResources().getAssets().open(assetPath)))) {
119 String line;
120 while ((line = br.readLine()) != null) {
121 sb.append(line);
122 sb.append(System.lineSeparator());
123 }
124 }
125 return sb.toString();
126 }
127
128 public static void writeToFile(File path, String content)
129 throws IOException {
130 path.getParentFile().mkdirs();
131
132 try (FileWriter writer = new FileWriter(path)) {
133 Log.i(DpmTestBase.TAG, "Writing to " + path);
134 Log.i(DpmTestBase.TAG, content);
135 writer.write(content);
136 }
137 }
138
139 private static boolean checkAssertRestrictions(Bundle a, Bundle b) {
140 try {
141 assertRestrictions(a, b);
142 return true;
143 } catch (AssertionFailedError e) {
144 return false;
145 }
146 }
147
148 public void testAssertRestrictions() {
149 final Bundle a = newRestrictions();
150 final Bundle b = newRestrictions("a");
151 final Bundle c = newRestrictions("a");
152 final Bundle d = newRestrictions("b", "c");
153 final Bundle e = newRestrictions("b", "c");
154
155 assertTrue(checkAssertRestrictions(null, null));
156 assertFalse(checkAssertRestrictions(null, a));
157 assertFalse(checkAssertRestrictions(a, null));
158 assertTrue(checkAssertRestrictions(a, a));
159
160 assertFalse(checkAssertRestrictions(a, b));
161 assertTrue(checkAssertRestrictions(b, c));
162
163 assertFalse(checkAssertRestrictions(c, d));
164 assertTrue(checkAssertRestrictions(d, e));
165 }
Makoto Onukicc4bbeb2015-09-17 10:28:24 -0700166}