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 static com.google.common.truth.Truth.assertWithMessage;
20 
21 import android.content.ComponentName;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.os.BaseBundle;
25 import android.os.Bundle;
26 import android.os.PersistableBundle;
27 import android.os.RemoteException;
28 import android.test.AndroidTestCase;
29 
30 import androidx.test.filters.SmallTest;
31 import androidx.test.uiautomator.UiDevice;
32 
33 import com.android.managedprovisioning.preprovisioning.EncryptionController;
34 import com.android.managedprovisioning.preprovisioning.PostEncryptionActivity;
35 
36 import java.lang.reflect.Array;
37 import java.util.Objects;
38 import java.util.Set;
39 
40 public class TestUtils extends AndroidTestCase {
41     @SmallTest
testIntentWithActionEquals()42     public void testIntentWithActionEquals() {
43         Intent i = new Intent("aa");
44         assertTrue(intentEquals(i, i));
45     }
46 
47     @SmallTest
testIntentWithExtraEquals()48     public void testIntentWithExtraEquals() {
49         Intent i = new Intent().putExtra("bb", "cc");
50         assertTrue(intentEquals(i, i));
51     }
52 
53     @SmallTest
testIntentActionNotEqual()54     public void testIntentActionNotEqual() {
55         Intent i1 = new Intent("aa");
56         Intent i2 = new Intent("bb");
57         assertFalse(intentEquals(i1, i2));
58     }
59 
60     @SmallTest
testIntentExtraNotEqual()61     public void testIntentExtraNotEqual() {
62         Intent i1 = new Intent().putExtra("aa", "bb");
63         Intent i2 = new Intent().putExtra("aa", "cc");
64         assertFalse(intentEquals(i1, i2));
65     }
66 
67     @SmallTest
testIntentNotSameExtra()68     public void testIntentNotSameExtra() {
69         Intent i1 = new Intent().putExtra("aa", "bb");
70         Intent i2 = new Intent().putExtra("dd", "cc");
71         assertFalse(intentEquals(i1, i2));
72     }
73 
74     @SmallTest
testIntentMultipleExtrasOfArray()75     public void testIntentMultipleExtrasOfArray() {
76         Intent i1 = new Intent()
77                 .putExtra("aa", new int[] {1, 2})
78                 .putExtra("aaa", new int[] {3, 4});
79         Intent i2 = new Intent(i1);
80         assertTrue(intentEquals(i1, i2));
81 
82         i2.putExtra("aaa", new int[] {5, 6});
83         assertFalse(intentEquals(i1, i2));
84     }
85 
86     @SmallTest
testIntentMultipleExtrasOfBundle()87     public void testIntentMultipleExtrasOfBundle() {
88         Bundle b1 = new Bundle();
89         b1.putString("b1", "11");
90         Bundle b2 = new Bundle();
91         b2.putString("b2", "22");
92         Intent i1 = new Intent()
93                 .putExtra("aa", b1)
94                 .putExtra("aaa", b2);
95         Intent i2 = new Intent(i1);
96         assertTrue(intentEquals(i1, i2));
97 
98         Bundle b3 = new Bundle();
99         b3.putString("b3", "33");
100         i2.putExtra("aaa", b3);
101         assertFalse(intentEquals(i1, i2));
102     }
103 
104     /**
105      * This method uses Object.equals to compare the extras.
106      * Which means that it will always return false if one of the intents has an extra with an
107      * embedded bundle.
108      */
intentEquals(Intent intent1, Intent intent2)109     public static boolean intentEquals(Intent intent1, Intent intent2) {
110         // both are null? return true
111         if (intent1 == null && intent2 == null) {
112             return true;
113         }
114         // Only one is null? return false
115         if (intent1 == null || intent2 == null) {
116             return false;
117         }
118         return intent1.filterEquals(intent2) && bundleEquals(intent1.getExtras(),
119                 intent2.getExtras());
120     }
121 
bundleEquals(BaseBundle bundle1, BaseBundle bundle2)122     public static boolean bundleEquals(BaseBundle bundle1, BaseBundle bundle2) {
123         // both are null? return true
124         if (bundle1 == null && bundle2 == null) {
125             return true;
126         }
127         // Only one is null? return false
128         if (bundle1 == null || bundle2 == null) {
129             return false;
130         }
131         if (bundle1.size() != bundle2.size()) {
132             return false;
133         }
134         Set<String> keys = bundle1.keySet();
135         for (String key : keys) {
136             Object value1 = bundle1.get(key);
137             Object value2 = bundle2.get(key);
138             if (value1 != null && value1.getClass().isArray()
139                     && value2 != null && value2.getClass().isArray()) {
140                 if (!arrayEquals(value1, value2)) {
141                     return false;
142                 }
143             } else if (value1 instanceof BaseBundle && value2 instanceof BaseBundle) {
144                 if (!bundleEquals((BaseBundle) value1, (BaseBundle) value2)) {
145                     return false;
146                 }
147             } else if (!Objects.equals(value1, value2)) {
148                 return false;
149             }
150         }
151         return true;
152     }
153 
arrayEquals(Object value1, Object value2)154     private static boolean arrayEquals(Object value1, Object value2) {
155         final int length = Array.getLength(value1);
156         if (length != Array.getLength(value2)) {
157             return false;
158         }
159         for (int i = 0; i < length; i++) {
160             if (!Objects.equals(Array.get(value1, i), Array.get(value2, i))) {
161                 return false;
162             }
163         }
164         return true;
165     }
166 
assertIntentEquals(Intent i1, Intent i2)167     public static void assertIntentEquals(Intent i1, Intent i2) {
168         if (!intentEquals(i1, i2)) {
169             failIntentsNotEqual(i1, i2);
170         }
171     }
172 
failIntentsNotEqual(Intent i1, Intent i2)173     public static void failIntentsNotEqual(Intent i1, Intent i2) {
174         fail("Intent " + intentToString(i1) + " is not equal to " + intentToString(i2));
175     }
176 
intentToString(Intent i)177     public static String intentToString(Intent i) {
178         return i.toString() + " with extras " + i.getExtras();
179     }
180 
createTestAdminExtras()181     public static PersistableBundle createTestAdminExtras() {
182         PersistableBundle adminExtras = new PersistableBundle();
183         adminExtras.putBoolean("boolean", true);
184         adminExtras.putBooleanArray("boolean_array", new boolean[] { true, false });
185         adminExtras.putDouble("double", 1.1);
186         adminExtras.putDoubleArray("double_array", new double[] { 1.1, 2.2 });
187         adminExtras.putInt("int", 1);
188         adminExtras.putIntArray("int_array", new int[] { 1, 2 } );
189         adminExtras.putLong("long", 1L);
190         adminExtras.putLongArray("long_array", new long[] { 1L, 2L });
191         adminExtras.putString("string", "Hello");
192         adminExtras.putStringArray("string_array", new String[] { "Hello", "World" } );
193 
194         PersistableBundle nestedBundle = new PersistableBundle();
195         nestedBundle.putInt("int", 1);
196         nestedBundle.putStringArray("string_array", new String[] { "Hello", "World" } );
197         adminExtras.putPersistableBundle("persistable_bundle", nestedBundle);
198         return adminExtras;
199     }
200 
wakeupDeviceAndPressHome(UiDevice uiDevice)201     public static void wakeupDeviceAndPressHome(UiDevice uiDevice) throws RemoteException {
202         uiDevice.wakeUp();
203         uiDevice.pressMenu();
204         uiDevice.pressHome();
205     }
206 
createEncryptionController( Context context)207     public static EncryptionController createEncryptionController(
208             Context context) {
209         return EncryptionController.getInstance(
210                 context,
211                 new ComponentName(context, PostEncryptionActivity.class));
212     }
213 
assertIntentsEqual(Intent intent1, Intent intent2)214     public static void assertIntentsEqual(Intent intent1, Intent intent2) {
215         assertWithMessage("Intent actions are not equal")
216                 .that(intent1.getAction())
217                 .isEqualTo(intent2.getAction());
218         assertWithMessage("Package names are not equal")
219                 .that(intent1.getPackage())
220                 .isEqualTo(intent2.getPackage());
221         assertBundlesEqual(intent1.getExtras(), intent2.getExtras());
222     }
223 
assertBundlesEqual(BaseBundle bundle1, BaseBundle bundle2)224     public static void assertBundlesEqual(BaseBundle bundle1, BaseBundle bundle2) {
225         if (bundle1 != null) {
226             assertWithMessage("Intent bundles are not equal, " + bundle1 + " " + bundle2)
227                     .that(bundle2).isNotNull();
228             assertWithMessage("Intent bundles are not equal, " + bundle1 + " " + bundle2)
229                     .that(bundle1.keySet().size()).isEqualTo(bundle2.keySet().size());
230             for (String key : bundle1.keySet()) {
231                 assertWithMessage("Intent bundles are not equal, " + bundle1 + " " + bundle2)
232                         .that(bundle1.get(key))
233                         .isEqualTo(bundle2.get(key));
234             }
235         } else {
236             assertWithMessage("Intent bundles are not equal").that(bundle2).isNull();
237         }
238     }
239 }
240