HapClient: stop calling NativeInterface in test

Bug: 295237486
Test: atest HapClientTest
Change-Id: Id139b96ddcf16ccc2abb2770b6a087f12ce16d0a
diff --git a/android/app/src/com/android/bluetooth/hap/HapClientNativeInterface.java b/android/app/src/com/android/bluetooth/hap/HapClientNativeInterface.java
index 5fab43a..32d6ac0 100644
--- a/android/app/src/com/android/bluetooth/hap/HapClientNativeInterface.java
+++ b/android/app/src/com/android/bluetooth/hap/HapClientNativeInterface.java
@@ -33,16 +33,23 @@
  * Hearing Access Profile Client Native Interface to/from JNI.
  */
 public class HapClientNativeInterface {
-    private static final String TAG = "HapClientNativeInterface";
-    private static final boolean DBG = true;
+    private static final String TAG =
+            HapClientNativeInterface.class.getSimpleName().substring(0, 23);
+    private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG);
+
     private final BluetoothAdapter mAdapter;
 
     @GuardedBy("INSTANCE_LOCK")
     private static HapClientNativeInterface sInstance;
+
     private static final Object INSTANCE_LOCK = new Object();
 
     static {
-        classInitNative();
+        if (Utils.isInstrumentationTestMode()) {
+            Log.w(TAG, "App is instrumented. Skip loading the native");
+        } else {
+            classInitNative();
+        }
     }
 
     private HapClientNativeInterface() {
@@ -64,6 +71,14 @@
         }
     }
 
+    /** Set singleton instance. */
+    @VisibleForTesting
+    public static void setInstance(HapClientNativeInterface instance) {
+        synchronized (INSTANCE_LOCK) {
+            sInstance = instance;
+        }
+    }
+
     /**
      * Initiates HapClientService connection to a remote device.
      *
@@ -346,7 +361,7 @@
         event.valueList = new ArrayList<>(Arrays.asList(presets));
 
         if (DBG) {
-            Log.d(TAG, "onPresetInfo: " + event);
+            Log.d(TAG, "onGroupPresetInfo: " + event);
         }
         sendMessageToService(event);
     }
@@ -360,7 +375,7 @@
         event.valueInt2 = presetIndex;
 
         if (DBG) {
-            Log.d(TAG, "OnPresetNameSetError: " + event);
+            Log.d(TAG, "onPresetNameSetError: " + event);
         }
         sendMessageToService(event);
     }
@@ -374,7 +389,7 @@
         event.valueInt3 = groupId;
 
         if (DBG) {
-            Log.d(TAG, "OnPresetNameSetError: " + event);
+            Log.d(TAG, "onGroupPresetNameSetError: " + event);
         }
         sendMessageToService(event);
     }
@@ -402,7 +417,7 @@
         event.valueInt3 = groupId;
 
         if (DBG) {
-            Log.d(TAG, "onPresetInfoError: " + event);
+            Log.d(TAG, "onGroupPresetInfoError: " + event);
         }
         sendMessageToService(event);
     }
diff --git a/android/app/tests/unit/src/com/android/bluetooth/hap/HapClientTest.java b/android/app/tests/unit/src/com/android/bluetooth/hap/HapClientTest.java
index 32e6435..898a05d 100644
--- a/android/app/tests/unit/src/com/android/bluetooth/hap/HapClientTest.java
+++ b/android/app/tests/unit/src/com/android/bluetooth/hap/HapClientTest.java
@@ -129,8 +129,8 @@
         mAdapter = BluetoothAdapter.getDefaultAdapter();
         mAttributionSource = mAdapter.getAttributionSource();
 
+        HapClientNativeInterface.setInstance(mNativeInterface);
         startService();
-        mService.mHapClientNativeInterface = mNativeInterface;
         mService.mFactory = mServiceFactory;
         doReturn(mCsipService).when(mServiceFactory).getCsipSetCoordinatorService();
         mServiceBinder = (HapClientService.BluetoothHapClientBinder) mService.initBinder();
@@ -217,6 +217,7 @@
         mService.mCallbacks.unregister(mCallback);
 
         stopService();
+        HapClientNativeInterface.setInstance(null);
 
         if (mHasIntentReceiver != null) {
             mTargetContext.unregisterReceiver(mHasIntentReceiver);