Carkit test refactors

Created a base test class for common code
regarding HFP tests.
* Test class calls BluetoothBaseTest setup
functions where necessary.
* Sets phone attributes based off a sim config
file to accomodate all types of carriers. This
also makes it easier to use regularly tested
tel_test_utils.
* Pre-pairs the phone and carkit.
* Sets up hf, ag, re, and re2 depending on
how many andorid devices are connected.
* Establishes common setup, teardown, and
onfail routines.

Fix BluetoothProfile.PBAP enum to be named
appropriately to PBAP_SERVER.

Fixed logging in bt_contact_utils to use
the built in logging.

Used built in adb functions for android devices
instead of command line python executions in
bt_contacts_utils.

Added BluetoothProfile.Map_MCE to be blocked when
setting car profile priorities for more stable
test results.

Fixed infinite while loop in car_telecom_utils.

Fixed SL4A api change of telecomGetGalls to
telecomCallGetCallIds in car_telecom_utils.

Removed un-needed cmdr function from utils.py.

Since connetion broadcasts are flacky, added
profile connected devices checks after
connection broadcast checks in order to stabalize
tests.

Added needed timeouts throught HFP tests in order
for the stack to update to avoid test race conditions.
Not all carriers update the stack fast enough in areas
like waiting for a ringing call or accepting calls.

Used tel_test_util fincutions instead of telecom
utils where it was applicable.

Made tests more readible by changing var names.

Added bt_test_wrap to all code for logging consistency.

Added media path check. BtCarMediaPassthroughTest now
requires "local_media_path" added to the config file
that corresponds to the path of where the music dir
is where you want to push code.

Added mandatory "sim_conf_file" to HFP related tests
since not all carriers will update phone numbers that
Java apis expect.

Removed passing the logger to connect_pri_to_sec and
disconnect_pri_to_sec to be consistent with the
rest of bt_test_utils.

Ran YAPF on all code so it is formatted cosistently.

Test: Running automation against all tests modified
Change-Id: I8fe44d033980cff70bbd7aa4250c6a40be7c9418
diff --git a/acts/tests/google/bt/car_bt/BtCarHfpTest.py b/acts/tests/google/bt/car_bt/BtCarHfpTest.py
index 92af2f2..1548f71 100644
--- a/acts/tests/google/bt/car_bt/BtCarHfpTest.py
+++ b/acts/tests/google/bt/car_bt/BtCarHfpTest.py
@@ -17,80 +17,46 @@
 Test the HFP profile for basic calling functionality.
 """
 
-import os
 import time
-
 from acts.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest
+from acts.test_utils.bt.BluetoothCarHfpBaseTest import BluetoothCarHfpBaseTest
 from acts.test_utils.bt import BtEnum
 from acts.test_utils.bt import bt_test_utils
+from acts.test_utils.car import car_telecom_utils
 from acts.test_utils.car import tel_telecom_utils
 from acts.test_utils.tel import tel_defines
-from acts.test_utils.tel.tel_test_utils import get_phone_number
-from acts.test_utils.tel.tel_test_utils import setup_droid_properties
 
 BLUETOOTH_PKG_NAME = "com.android.bluetooth"
 CALL_TYPE_OUTGOING = "CALL_TYPE_OUTGOING"
 CALL_TYPE_INCOMING = "CALL_TYPE_INCOMING"
-default_timeout = 20
+SHORT_TIMEOUT = 5
 
 
-class BtCarHfpTest(BluetoothBaseTest):
+class BtCarHfpTest(BluetoothCarHfpBaseTest):
     def setup_class(self):
-        self.hf = self.android_devices[0]
-        self.ag = self.android_devices[1]
-        self.re = self.android_devices[2]
-        if not "sim_conf_file" in self.user_params.keys():
-            self.log.error("Missing mandatory user config \"sim_conf_file\"!")
+        if not super(BtCarHfpTest, self).setup_class():
             return False
-        sim_conf_file = self.user_params["sim_conf_file"]
-        if not os.path.isfile(sim_conf_file):
-            sim_conf_file = os.path.join(
-                self.user_params[Config.key_config_path], sim_conf_file)
-            if not os.path.isfile(sim_conf_file):
-                self.log.error("Unable to load user config " + sim_conf_file +
-                               " from test config file.")
-                return False
-        setup_droid_properties(self.log, self.ag, sim_conf_file)
-        setup_droid_properties(self.log, self.re, sim_conf_file)
-        self.ag_phone_number = get_phone_number(self.log, self.ag)
-        self.re_phone_number = get_phone_number(self.log, self.re)
-        self.log.info("ag tel: {} re tel: {}".format(self.ag_phone_number,
-                                                     self.re_phone_number))
-
-        # Setup includes pairing and connecting the devices.
-        bt_test_utils.setup_multiple_devices_for_bt_test([self.hf, self.ag])
-        bt_test_utils.reset_bluetooth([self.hf, self.ag])
-
-        # Pair and connect the devices.
-        if not bt_test_utils.pair_pri_to_sec(self.hf.droid, self.ag.droid):
-            self.log.error("Failed to pair")
-            return False
-
         # Disable the A2DP profile.
         bt_test_utils.set_profile_priority(
             self.hf, self.ag, [BtEnum.BluetoothProfile.PBAP_CLIENT.value,
                                BtEnum.BluetoothProfile.A2DP_SINK.value],
             BtEnum.BluetoothPriorityLevel.PRIORITY_OFF)
+        bt_test_utils.set_profile_priority(
+            self.hf, self.ag, [BtEnum.BluetoothProfile.HEADSET_CLIENT.value],
+            BtEnum.BluetoothPriorityLevel.PRIORITY_ON)
 
-        if not bt_test_utils.connect_pri_to_sec(
-                self.log, self.hf, self.ag.droid,
-                set([BtEnum.BluetoothProfile.HEADSET_CLIENT.value])):
+        if not bt_test_utils.connect_pri_to_sec(self.hf, self.ag, set(
+            [BtEnum.BluetoothProfile.HEADSET_CLIENT.value])):
             self.log.error("Failed to connect")
-            return False
-
-    def setup_test(self):
-        # Reset the devices.
-        for d in self.android_devices:
-            d.ed.clear_all_events()
-
-    def on_fail(self, test_name, begin_time):
-        self.log.debug("Test {} failed.".format(test_name))
-        bt_test_utils.take_btsnoop_logs(self.android_devices, self, test_name, begin_time)
-        bt_test_utils.take_bugreport_logs(self.android_devices, self, test_name, begin_time)
-
-    def teardown_test(self):
-        self.log.debug(bt_test_utils.log_energy_info(self.android_devices,
-                                                     "End"))
+            # Additional profile connection check
+            if bt_test_utils.is_hfp_client_device_connected(
+                    self.hf, self.ag.droid.bluetoothGetLocalAddress()):
+                self.log.info(
+                    "HFP Client connected even though connection state changed "
+                    + " event not found")
+            else:
+                return False
+        return True
 
     #@BluetoothTest(UUID=4ce2195a-b70a-4584-912e-cbd20d20e19d)
     @BluetoothBaseTest.bt_test_wrap
@@ -129,6 +95,7 @@
             self.log.info("Component name does not start with pkg name {}".
                           format(selected_acc))
             return False
+        return True
 
     #@BluetoothTest(UUID=e579009d-05f3-4236-a698-5de8c11d73a9)
     @BluetoothBaseTest.bt_test_wrap
@@ -250,7 +217,7 @@
         """
         return self.dial_a_hangup_b(self.re, self.re, self.ag_phone_number)
 
-    def dial_a_hangup_b(self, a, b, ph=""):
+    def dial_a_hangup_b(self, caller, callee, ph=""):
         """
         a, b and c can be either of AG, HF or Remote.
         1. Make a call from 'a' on a fixed number.
@@ -265,23 +232,25 @@
 
         # Determine if this is outgoing or incoming call.
         call_type = None
-        if a == self.ag or a == self.hf:
+        if caller == self.ag or caller == self.hf:
             call_type = CALL_TYPE_OUTGOING
-            if b != self.ag and b != self.hf:
+            if callee != self.ag and callee != self.hf:
                 self.log.info("outgoing call should terminate at AG or HF")
                 return False
-        elif a == self.re:
+        elif caller == self.re:
             call_type = CALL_TYPE_INCOMING
-            if b != self.re:
+            if callee != self.re:
                 self.log.info("Incoming call should terminate at Re")
                 return False
 
         self.log.info("Call type is {}".format(call_type))
 
         # make a call on 'a'
-        if not tel_telecom_utils.dial_number(self.log, a, ph):
+        if not tel_telecom_utils.dial_number(self.log, caller, ph):
             return False
 
+        # Give time for state to update due to carrier limitations
+        time.sleep(SHORT_TIMEOUT)
         # Check that everyone is in dialing/ringing state.
         ret = True
         if call_type == CALL_TYPE_OUTGOING:
@@ -295,6 +264,8 @@
         if not ret:
             return False
 
+        # Give time for state to update due to carrier limitations
+        time.sleep(SHORT_TIMEOUT)
         # Check if we have any calls with dialing or active state on 'b'.
         # We assume we never disconnect from 'ringing' state since it will lead
         # to voicemail.
@@ -304,7 +275,7 @@
              tel_defines.CALL_STATE_ACTIVE]
 
         calls_in_dialing_or_active = tel_telecom_utils.get_calls_in_states(
-            self.log, b, call_state_dialing_or_active)
+            self.log, callee, call_state_dialing_or_active)
 
         # Make sure there is only one!
         if len(calls_in_dialing_or_active) != 1:
@@ -313,7 +284,7 @@
             return False
 
         # Hangup the *only* call on 'b'
-        if not car_telecom_utils.hangup_call(self.log, b,
+        if not car_telecom_utils.hangup_call(self.log, caller,
                                              calls_in_dialing_or_active[0]):
             return False