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 | |
| 17 | package com.android.server.devicepolicy; |
| 18 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 19 | import com.google.android.collect.Lists; |
| 20 | import com.google.android.collect.Sets; |
| 21 | |
| 22 | import android.content.Context; |
| 23 | import android.os.Bundle; |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 24 | import android.os.FileUtils; |
Makoto Onuki | d932f76 | 2015-09-29 16:53:38 -0700 | [diff] [blame] | 25 | import android.os.Parcel; |
| 26 | import android.os.Parcelable; |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 27 | import android.test.AndroidTestCase; |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 28 | import android.util.Log; |
| 29 | import android.util.Printer; |
| 30 | |
| 31 | import org.junit.Assert; |
| 32 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 33 | import java.io.BufferedReader; |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 34 | import java.io.File; |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 35 | import java.io.FileWriter; |
| 36 | import java.io.IOException; |
| 37 | import java.io.InputStreamReader; |
| 38 | import java.io.PrintWriter; |
| 39 | import java.util.ArrayList; |
| 40 | import java.util.Collections; |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 41 | import java.util.List; |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 42 | import java.util.Objects; |
| 43 | import java.util.Set; |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 44 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 45 | import junit.framework.AssertionFailedError; |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 46 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 47 | public class DpmTestUtils extends AndroidTestCase { |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 48 | 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 Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 53 | Log.i(DpmTestBase.TAG, "Created " + dir); |
| 54 | } |
| 55 | |
| 56 | public static int getListSizeAllowingNull(List<?> list) { |
| 57 | return list == null ? 0 : list.size(); |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 60 | 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 Onuki | d932f76 | 2015-09-29 16:53:38 -0700 | [diff] [blame] | 98 | 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 Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 107 | public static Printer LOG_PRINTER = new Printer() { |
| 108 | @Override |
| 109 | public void println(String x) { |
| 110 | Log.i(DpmTestBase.TAG, x); |
| 111 | } |
| 112 | }; |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 113 | |
| 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 Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 166 | } |