Fix a bug in InlinePresentationStyleUtils#bundleEquals
* before the fix, the method would return false if the two bundles both
contains a nested bundle for the same key, and the nested bundles are
equal. i.e. the test case
InlinePresentationStyleUtilsTest#testBundleEquals_nestedBundle_equal
would fail.
Test: atest InlinePresentationStyleUtilsTest
Bug: 154957669
Change-Id: Ia2cf682619fa245d846972a8683d03333551df78
diff --git a/core/java/com/android/internal/widget/InlinePresentationStyleUtils.java b/core/java/com/android/internal/widget/InlinePresentationStyleUtils.java
index db1725f..9c1aa28 100644
--- a/core/java/com/android/internal/widget/InlinePresentationStyleUtils.java
+++ b/core/java/com/android/internal/widget/InlinePresentationStyleUtils.java
@@ -52,10 +52,10 @@
for (String key : keys) {
Object value1 = bundle1.get(key);
Object value2 = bundle2.get(key);
- if (value1 instanceof Bundle && value2 instanceof Bundle
- && !bundleEquals((Bundle) value1, (Bundle) value2)) {
- return false;
- } else if (!Objects.equals(value1, value2)) {
+ final boolean equal = value1 instanceof Bundle && value2 instanceof Bundle
+ ? bundleEquals((Bundle) value1, (Bundle) value2)
+ : Objects.equals(value1, value2);
+ if (!equal) {
return false;
}
}
diff --git a/core/tests/utiltests/src/com/android/internal/util/InlinePresentationStyleUtilsTest.java b/core/tests/utiltests/src/com/android/internal/util/InlinePresentationStyleUtilsTest.java
new file mode 100644
index 0000000..8e4f38e
--- /dev/null
+++ b/core/tests/utiltests/src/com/android/internal/util/InlinePresentationStyleUtilsTest.java
@@ -0,0 +1,113 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.util;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import android.os.Bundle;
+
+import androidx.test.filters.SmallTest;
+import androidx.test.runner.AndroidJUnit4;
+
+import com.android.internal.widget.InlinePresentationStyleUtils;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+@RunWith(AndroidJUnit4.class)
+@SmallTest
+public class InlinePresentationStyleUtilsTest {
+ @Test
+ public void testBundleEquals_empty() {
+ Bundle bundle1 = new Bundle();
+ Bundle bundle2 = new Bundle();
+
+ assertTrue(InlinePresentationStyleUtils.bundleEquals(bundle1, bundle2));
+
+ bundle1 = Bundle.EMPTY;
+ assertTrue(InlinePresentationStyleUtils.bundleEquals(bundle1, bundle2));
+
+ bundle2 = Bundle.EMPTY;
+ assertTrue(InlinePresentationStyleUtils.bundleEquals(bundle1, bundle2));
+ }
+
+ @Test
+ public void testBundleEquals_oneIsEmpty() {
+ Bundle bundle1 = Bundle.EMPTY;
+ Bundle bundle2 = new Bundle();
+ bundle2.putString("KEY", "value");
+ assertFalse(InlinePresentationStyleUtils.bundleEquals(bundle1, bundle2));
+ }
+
+ @Test
+ public void testBundleEquals_nestedBundle_equal() {
+ Bundle bundle1 = new Bundle();
+ Bundle bundle11 = new Bundle();
+ bundle11.putString("KEY", "VALUE");
+ bundle1.putBundle("KEY_B", bundle11);
+
+ Bundle bundle2 = new Bundle();
+ Bundle bundle21 = new Bundle();
+ bundle21.putString("KEY", "VALUE");
+ bundle2.putBundle("KEY_B", bundle21);
+
+ assertTrue(InlinePresentationStyleUtils.bundleEquals(bundle1, bundle2));
+ }
+
+ @Test
+ public void testBundleEquals_nestedBundle_unequal() {
+ Bundle bundle1 = new Bundle();
+ Bundle bundle11 = new Bundle();
+ bundle11.putString("KEY", "VALUE");
+ bundle1.putBundle("KEY_B", bundle11);
+
+ Bundle bundle2 = new Bundle();
+ bundle2.putBundle("KEY_B", new Bundle());
+
+ assertFalse(InlinePresentationStyleUtils.bundleEquals(bundle1, bundle2));
+ }
+
+ @Test
+ public void testBundleEquals_sameKeyDifferentType() {
+ Bundle bundle1 = new Bundle();
+ bundle1.putBundle("KEY_B", new Bundle());
+
+ Bundle bundle2 = new Bundle();
+ bundle2.putInt("KEY_B", 12);
+
+ assertFalse(InlinePresentationStyleUtils.bundleEquals(bundle1, bundle2));
+ }
+
+ @Test
+ public void testBundleEquals_primitiveValue_equal() {
+ Bundle bundle1 = new Bundle();
+ bundle1.putInt("KEY", 11);
+ Bundle bundle2 = new Bundle();
+ bundle2.putInt("KEY", 11);
+ assertTrue(InlinePresentationStyleUtils.bundleEquals(bundle1, bundle2));
+ }
+
+ @Test
+ public void testBundleEquals_primitiveValue_unequal() {
+ Bundle bundle1 = new Bundle();
+ bundle1.putInt("KEY", 11);
+ Bundle bundle2 = new Bundle();
+ bundle2.putInt("KEY", 22);
+ assertFalse(InlinePresentationStyleUtils.bundleEquals(bundle1, bundle2));
+ }
+}