From 07d25029e43c3750baa101c670c2d1056b370ddc Mon Sep 17 00:00:00 2001
From: Hyundo Moon <hdmoon@google.com>
Date: Tue, 8 Nov 2022 09:53:36 +0900
Subject: [PATCH] Fix failing BluetoothPbapSimVcardManagerTest

testGetSIMPhonebookNameList_orderByAlphabet was failing since
the test does not set the local phone name in the service.

This CL fixes it, and also recovers the static variable
(local phone name) after the test ends.

Bug: 258075245
Test: atest BluetoothPbapSimVcardManagerTest
Change-Id: I4a8f3fd9e2a18ba1e626febef0e0ef3588e5f835
---
 .../BluetoothPbapSimVcardManagerTest.java     | 133 ++++++++++--------
 1 file changed, 72 insertions(+), 61 deletions(-)

diff --git a/android/app/tests/unit/src/com/android/bluetooth/pbap/BluetoothPbapSimVcardManagerTest.java b/android/app/tests/unit/src/com/android/bluetooth/pbap/BluetoothPbapSimVcardManagerTest.java
index 023027542ce..46302cb441b 100644
--- a/android/app/tests/unit/src/com/android/bluetooth/pbap/BluetoothPbapSimVcardManagerTest.java
+++ b/android/app/tests/unit/src/com/android/bluetooth/pbap/BluetoothPbapSimVcardManagerTest.java
@@ -240,71 +240,82 @@ public class BluetoothPbapSimVcardManagerTest {
 
     @Test
     public void testGetSIMPhonebookNameList_orderByIndexed() {
-        final String localPhoneName = "test_local_phone_name";
-        Cursor cursor = initManager();
-        List<String> nameList = Arrays.asList("D", "C", "A", "B");
-
-        // Implement Cursor iteration
-        final int size = nameList.size();
-        AtomicInteger currentPosition = new AtomicInteger(0);
-        when(cursor.moveToFirst()).then((Answer<Boolean>) i -> {
-            currentPosition.set(0);
-            return true;
-        });
-        when(cursor.isAfterLast()).then((Answer<Boolean>) i -> {
-            return currentPosition.get() >= size;
-        });
-        when(cursor.moveToNext()).then((Answer<Boolean>) i -> {
-            int pos = currentPosition.addAndGet(1);
-            return pos < size;
-        });
-        when(cursor.getString(anyInt())).then((Answer<String>) i -> {
-            return nameList.get(currentPosition.get());
-        });
-
-        ArrayList<String> result = mManager.getSIMPhonebookNameList(
-                BluetoothPbapObexServer.ORDER_BY_INDEXED);
-
-        ArrayList<String> expectedResult = new ArrayList<>();
-        expectedResult.add(localPhoneName);
-        expectedResult.addAll(nameList);
-
-        assertThat(result).isEqualTo(expectedResult);
+        String prevLocalPhoneName = BluetoothPbapService.getLocalPhoneName();
+        try {
+            final String localPhoneName = "test_local_phone_name";
+            BluetoothPbapService.setLocalPhoneName(localPhoneName);
+            Cursor cursor = initManager();
+            List<String> nameList = Arrays.asList("D", "C", "A", "B");
+
+            // Implement Cursor iteration
+            final int size = nameList.size();
+            AtomicInteger currentPosition = new AtomicInteger(0);
+            when(cursor.moveToFirst()).then((Answer<Boolean>) i -> {
+                currentPosition.set(0);
+                return true;
+            });
+            when(cursor.isAfterLast()).then((Answer<Boolean>) i -> {
+                return currentPosition.get() >= size;
+            });
+            when(cursor.moveToNext()).then((Answer<Boolean>) i -> {
+                int pos = currentPosition.addAndGet(1);
+                return pos < size;
+            });
+            when(cursor.getString(anyInt())).then((Answer<String>) i -> {
+                return nameList.get(currentPosition.get());
+            });
+
+            ArrayList<String> result = mManager.getSIMPhonebookNameList(
+                    BluetoothPbapObexServer.ORDER_BY_INDEXED);
+
+            ArrayList<String> expectedResult = new ArrayList<>();
+            expectedResult.add(localPhoneName);
+            expectedResult.addAll(nameList);
+
+            assertThat(result).isEqualTo(expectedResult);
+        } finally {
+            BluetoothPbapService.setLocalPhoneName(prevLocalPhoneName);
+        }
     }
 
     @Test
     public void testGetSIMPhonebookNameList_orderByAlphabet() {
-        final String localPhoneName = "test_local_phone_name";
-        BluetoothPbapService.setLocalPhoneName(localPhoneName);
-        Cursor cursor = initManager();
-        List<String> nameList = Arrays.asList("D", "C", "A", "B");
-
-        // Implement Cursor iteration
-        final int size = nameList.size();
-        AtomicInteger currentPosition = new AtomicInteger(0);
-        when(cursor.moveToFirst()).then((Answer<Boolean>) i -> {
-            currentPosition.set(0);
-            return true;
-        });
-        when(cursor.isAfterLast()).then((Answer<Boolean>) i -> {
-            return currentPosition.get() >= size;
-        });
-        when(cursor.moveToNext()).then((Answer<Boolean>) i -> {
-            int pos = currentPosition.addAndGet(1);
-            return pos < size;
-        });
-        when(cursor.getString(anyInt())).then((Answer<String>) i -> {
-            return nameList.get(currentPosition.get());
-        });
-
-        List<String> result = mManager.getSIMPhonebookNameList(
-                BluetoothPbapObexServer.ORDER_BY_ALPHABETICAL);
-
-        List<String> expectedResult = new ArrayList<>(nameList);
-        Collections.sort(expectedResult, String.CASE_INSENSITIVE_ORDER);
-        expectedResult.add(0, localPhoneName);
-
-        assertThat(result).isEqualTo(expectedResult);
+        String prevLocalPhoneName = BluetoothPbapService.getLocalPhoneName();
+        try {
+            final String localPhoneName = "test_local_phone_name";
+            BluetoothPbapService.setLocalPhoneName(localPhoneName);
+            Cursor cursor = initManager();
+            List<String> nameList = Arrays.asList("D", "C", "A", "B");
+
+            // Implement Cursor iteration
+            final int size = nameList.size();
+            AtomicInteger currentPosition = new AtomicInteger(0);
+            when(cursor.moveToFirst()).then((Answer<Boolean>) i -> {
+                currentPosition.set(0);
+                return true;
+            });
+            when(cursor.isAfterLast()).then((Answer<Boolean>) i -> {
+                return currentPosition.get() >= size;
+            });
+            when(cursor.moveToNext()).then((Answer<Boolean>) i -> {
+                int pos = currentPosition.addAndGet(1);
+                return pos < size;
+            });
+            when(cursor.getString(anyInt())).then((Answer<String>) i -> {
+                return nameList.get(currentPosition.get());
+            });
+
+            List<String> result = mManager.getSIMPhonebookNameList(
+                    BluetoothPbapObexServer.ORDER_BY_ALPHABETICAL);
+
+            List<String> expectedResult = new ArrayList<>(nameList);
+            Collections.sort(expectedResult, String.CASE_INSENSITIVE_ORDER);
+            expectedResult.add(0, localPhoneName);
+
+            assertThat(result).isEqualTo(expectedResult);
+        } finally {
+            BluetoothPbapService.setLocalPhoneName(prevLocalPhoneName);
+        }
     }
 
     @Test
-- 
GitLab