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.managedprovisioning; 18 19 import android.content.ComponentName; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.os.BaseBundle; 23 import android.os.PersistableBundle; 24 import android.os.RemoteException; 25 import android.support.test.uiautomator.UiDevice; 26 import android.test.AndroidTestCase; 27 import android.test.suitebuilder.annotation.SmallTest; 28 29 import com.android.managedprovisioning.preprovisioning.EncryptionController; 30 import com.android.managedprovisioning.preprovisioning.PostEncryptionActivity; 31 32 import java.lang.reflect.Array; 33 import java.util.Objects; 34 import java.util.Set; 35 36 public class TestUtils extends AndroidTestCase { 37 @SmallTest testIntentWithActionEquals()38 public void testIntentWithActionEquals() { 39 Intent i = new Intent("aa"); 40 assertTrue(intentEquals(i, i)); 41 } 42 43 @SmallTest testIntentWithExtraEquals()44 public void testIntentWithExtraEquals() { 45 Intent i = new Intent().putExtra("bb", "cc"); 46 assertTrue(intentEquals(i, i)); 47 } 48 49 @SmallTest testIntentActionNotEqual()50 public void testIntentActionNotEqual() { 51 Intent i1 = new Intent("aa"); 52 Intent i2 = new Intent("bb"); 53 assertFalse(intentEquals(i1, i2)); 54 } 55 56 @SmallTest testIntentExtraNotEqual()57 public void testIntentExtraNotEqual() { 58 Intent i1 = new Intent().putExtra("aa", "bb"); 59 Intent i2 = new Intent().putExtra("aa", "cc"); 60 assertFalse(intentEquals(i1, i2)); 61 } 62 63 @SmallTest testIntentNotSameExtra()64 public void testIntentNotSameExtra() { 65 Intent i1 = new Intent().putExtra("aa", "bb"); 66 Intent i2 = new Intent().putExtra("dd", "cc"); 67 assertFalse(intentEquals(i1, i2)); 68 } 69 70 /** 71 * This method uses Object.equals to compare the extras. 72 * Which means that it will always return false if one of the intents has an extra with an 73 * embedded bundle. 74 */ intentEquals(Intent intent1, Intent intent2)75 public static boolean intentEquals(Intent intent1, Intent intent2) { 76 // both are null? return true 77 if (intent1 == null && intent2 == null) { 78 return true; 79 } 80 // Only one is null? return false 81 if (intent1 == null || intent2 == null) { 82 return false; 83 } 84 return intent1.filterEquals(intent2) && bundleEquals(intent1.getExtras(), 85 intent2.getExtras()); 86 } 87 bundleEquals(BaseBundle bundle1, BaseBundle bundle2)88 public static boolean bundleEquals(BaseBundle bundle1, BaseBundle bundle2) { 89 // both are null? return true 90 if (bundle1 == null && bundle2 == null) { 91 return true; 92 } 93 // Only one is null? return false 94 if (bundle1 == null || bundle2 == null) { 95 return false; 96 } 97 if (bundle1.size() != bundle2.size()) { 98 return false; 99 } 100 Set<String> keys = bundle1.keySet(); 101 for (String key : keys) { 102 Object value1 = bundle1.get(key); 103 Object value2 = bundle2.get(key); 104 if (value1 != null && value1.getClass().isArray() 105 && value2 != null && value2.getClass().isArray()) { 106 return arrayEquals(value1, value2); 107 } else if (value1 instanceof BaseBundle && value2 instanceof BaseBundle) { 108 return bundleEquals((BaseBundle) value1, (BaseBundle) value2); 109 } else if (!Objects.equals(value1, value2)) { 110 return false; 111 } 112 } 113 return true; 114 } 115 arrayEquals(Object value1, Object value2)116 private static boolean arrayEquals(Object value1, Object value2) { 117 final int length = Array.getLength(value1); 118 if (length != Array.getLength(value2)) { 119 return false; 120 } 121 for (int i = 0; i < length; i++) { 122 if (!Objects.equals(Array.get(value1, i), Array.get(value2, i))) { 123 return false; 124 } 125 } 126 return true; 127 } 128 assertIntentEquals(Intent i1, Intent i2)129 public static void assertIntentEquals(Intent i1, Intent i2) { 130 if (!intentEquals(i1, i2)) { 131 failIntentsNotEqual(i1, i2); 132 } 133 } 134 failIntentsNotEqual(Intent i1, Intent i2)135 public static void failIntentsNotEqual(Intent i1, Intent i2) { 136 fail("Intent " + intentToString(i1) + " is not equal to " + intentToString(i2)); 137 } 138 intentToString(Intent i)139 public static String intentToString(Intent i) { 140 return i.toString() + " with extras " + i.getExtras(); 141 } 142 createTestAdminExtras()143 public static PersistableBundle createTestAdminExtras() { 144 PersistableBundle adminExtras = new PersistableBundle(); 145 adminExtras.putBoolean("boolean", true); 146 adminExtras.putBooleanArray("boolean_array", new boolean[] { true, false }); 147 adminExtras.putDouble("double", 1.1); 148 adminExtras.putDoubleArray("double_array", new double[] { 1.1, 2.2 }); 149 adminExtras.putInt("int", 1); 150 adminExtras.putIntArray("int_array", new int[] { 1, 2 } ); 151 adminExtras.putLong("long", 1L); 152 adminExtras.putLongArray("long_array", new long[] { 1L, 2L }); 153 adminExtras.putString("string", "Hello"); 154 adminExtras.putStringArray("string_array", new String[] { "Hello", "World" } ); 155 156 PersistableBundle nestedBundle = new PersistableBundle(); 157 nestedBundle.putInt("int", 1); 158 nestedBundle.putStringArray("string_array", new String[] { "Hello", "World" } ); 159 adminExtras.putPersistableBundle("persistable_bundle", nestedBundle); 160 return adminExtras; 161 } 162 wakeupDeviceAndPressHome(UiDevice uiDevice)163 public static void wakeupDeviceAndPressHome(UiDevice uiDevice) throws RemoteException { 164 uiDevice.wakeUp(); 165 uiDevice.pressMenu(); 166 uiDevice.pressHome(); 167 } 168 createEncryptionController( Context context)169 public static EncryptionController createEncryptionController( 170 Context context) { 171 return EncryptionController.getInstance( 172 context, 173 new ComponentName(context, PostEncryptionActivity.class)); 174 } 175 } 176