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