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