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