Add and remove device from whitelist
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / haltest / bluetooth_hal_tc.cpp
index 1b88352..ac1ddc9 100644 (file)
  *    limitations under the License.
  */
 /**
- * @file        bluetooth-share_test.cpp
+ * @file        bluetooth_hal_tc.cpp
  * @author      abc
  * @version     1.0
- * @brief       Unit-tests setup
+ * @brief       BT hal tests
  */
 
 #include <gmock/gmock.h>
 #include <gtest/gtest.h>
 #include <unistd.h>
 #include <glib.h>
+#include <system_info.h>
 
 #include "bluetooth.h"
 #include "oal-manager.h"
@@ -38,6 +39,21 @@ using ::testing::TestInfo;
 using ::testing::TestPartResult;
 using ::testing::UnitTest;
 
+#define TIMEOUT_6SECONDS 6000
+#define TIMEOUT_2SECONDS 2000
+#define TIMEOUT_500MILISECONDS 500
+
+#define FEATURE_BT "http://tizen.org/feature/network.bluetooth"
+
+#define SKIP_NOT_SUPPORTED(bFeature) {\
+    if (!bFeature) {\
+        printf("Feature is not support\n");\
+        return;\
+    } \
+}
+
+static bool g_bFeatureBT;
+
 static GMainLoop *mainloop = NULL;
 
 static gboolean timeout_func(gpointer data)
@@ -49,168 +65,240 @@ static gboolean timeout_func(gpointer data)
 static void wait_for_async(int timeout)
 {
     int timeout_id = 0;
+
     mainloop = g_main_loop_new(NULL, FALSE);
 
     timeout_id = g_timeout_add(timeout, timeout_func, mainloop);
+    if (timeout_id < 0)
+        return;
+
     g_main_loop_run(mainloop);
-    g_source_remove(timeout_id);
 }
 
 static void __bt_oal_event_receiver(int event_type, gpointer event_data, gsize len)
 {
+/*
        printf("event_type: [%d], data size: [%d]\n", event_type, len);
+*/
+}
+
+static bool __check_feature_supported(char *key)
+{
+       bool value = false;
+       int ret = system_info_get_platform_bool(key, &value);
+
+       EXPECT_EQ(SYSTEM_INFO_ERROR_NONE, ret) << "system_info_get_platform_bool failed";
+
+       return value;
 }
 
 TEST(BluetoothHAL_test, oal_bt_init_deinit_p) {
+       g_bFeatureBT = __check_feature_supported((char*)FEATURE_BT);
+       SKIP_NOT_SUPPORTED(g_bFeatureBT);
+
        int ret = OAL_STATUS_SUCCESS;
+
        ret = oal_bt_init(__bt_oal_event_receiver);
-       ASSERT_TRUE(ret != OAL_STATUS_PENDING);
+       ASSERT_TRUE(ret == OAL_STATUS_PENDING);
+
+       wait_for_async(TIMEOUT_500MILISECONDS);
 
        oal_bt_deinit();
+
+       wait_for_async(TIMEOUT_500MILISECONDS);
 }
 
 TEST(BluetoothHAL_test, adapter_enable_p) {
+       SKIP_NOT_SUPPORTED(g_bFeatureBT);
+
        int ret = OAL_STATUS_SUCCESS;
+       gboolean powered = FALSE;
+
        ret = oal_bt_init(__bt_oal_event_receiver);
-       ASSERT_TRUE(ret != OAL_STATUS_PENDING);
+       ASSERT_TRUE(ret == OAL_STATUS_PENDING);
 
+       wait_for_async(TIMEOUT_500MILISECONDS);
+
+       ret = adapter_get_powered_status(&powered);
+
+       /* Already enabled */
+       if (powered == TRUE)
+               return;
 
        ret = adapter_enable();
-       ASSERT_TRUE(ret != OAL_STATUS_SUCCESS);
+       ASSERT_TRUE(ret == OAL_STATUS_SUCCESS);
 
-       wait_for_async(6000); /* Wait for 6 seconds */
+       wait_for_async(TIMEOUT_6SECONDS); /* Wait for 6 seconds */
 }
 
 TEST(BluetoothHAL_test, adapter_disable_p) {
+       SKIP_NOT_SUPPORTED(g_bFeatureBT);
+
        int ret = OAL_STATUS_SUCCESS;
 
        ret = adapter_disable();
-       ASSERT_TRUE(ret != OAL_STATUS_SUCCESS);
+       ASSERT_TRUE(ret == OAL_STATUS_SUCCESS);
 
-       wait_for_async(6000);
+       wait_for_async(TIMEOUT_6SECONDS);
 }
 
 TEST(BluetoothHAL_test, adapter_get_powered_status_p) {
+       SKIP_NOT_SUPPORTED(g_bFeatureBT);
+
        int ret = OAL_STATUS_SUCCESS;
        gboolean powered = FALSE;
 
        /* Precondition : BT enable */
        ret = adapter_enable();
-       ASSERT_TRUE(ret != OAL_STATUS_SUCCESS);
+       ASSERT_TRUE(ret == OAL_STATUS_SUCCESS);
 
-       wait_for_async(6000);
+       wait_for_async(TIMEOUT_6SECONDS);
 
        ret = adapter_get_powered_status(&powered);
-       ASSERT_TRUE(ret != OAL_STATUS_SUCCESS);
+       ASSERT_TRUE(ret == OAL_STATUS_SUCCESS);
 }
 
 TEST(BluetoothHAL_test, adapter_start_stop_inquiry_p) {
+       SKIP_NOT_SUPPORTED(g_bFeatureBT);
+
        int ret = OAL_STATUS_SUCCESS;
 
        ret = adapter_start_inquiry(0);
-       ASSERT_TRUE(ret != OAL_STATUS_SUCCESS);
+       ASSERT_TRUE(ret == OAL_STATUS_SUCCESS);
 
-       wait_for_async(2000);
+       wait_for_async(TIMEOUT_2SECONDS);
 
        ret = adapter_stop_inquiry();
-       ASSERT_TRUE(ret != OAL_STATUS_SUCCESS);
-       wait_for_async(2000);
+       ASSERT_TRUE(ret == OAL_STATUS_SUCCESS);
+       wait_for_async(TIMEOUT_2SECONDS);
 }
 
 TEST(BluetoothHAL_test, adapter_get_properties_p) {
+       SKIP_NOT_SUPPORTED(g_bFeatureBT);
+
        int ret = OAL_STATUS_SUCCESS;
 
        ret = adapter_get_properties();
-       ASSERT_TRUE(ret != OAL_STATUS_SUCCESS);
+       ASSERT_TRUE(ret == OAL_STATUS_SUCCESS);
 
-       wait_for_async(2000);
+       wait_for_async(TIMEOUT_500MILISECONDS);
 }
 
 TEST(BluetoothHAL_test, adapter_get_address_p) {
+       SKIP_NOT_SUPPORTED(g_bFeatureBT);
+
        int ret = OAL_STATUS_SUCCESS;
 
        ret = adapter_get_address();
-       ASSERT_TRUE(ret != OAL_STATUS_SUCCESS);
+       ASSERT_TRUE(ret == OAL_STATUS_SUCCESS);
 
-       wait_for_async(2000);
+       wait_for_async(TIMEOUT_500MILISECONDS);
 }
 
 TEST(BluetoothHAL_test, adapter_get_version_p) {
+       SKIP_NOT_SUPPORTED(g_bFeatureBT);
+
        int ret = OAL_STATUS_SUCCESS;
 
        ret = adapter_get_version();
-       ASSERT_TRUE(ret != OAL_STATUS_SUCCESS);
+       ASSERT_TRUE(ret == OAL_STATUS_SUCCESS);
 
-       wait_for_async(2000);
+       wait_for_async(TIMEOUT_500MILISECONDS);
 }
 
 TEST(BluetoothHAL_test, adapter_get_name_p) {
+       SKIP_NOT_SUPPORTED(g_bFeatureBT);
+
        int ret = OAL_STATUS_SUCCESS;
 
        ret = adapter_get_name();
-       ASSERT_TRUE(ret != OAL_STATUS_SUCCESS);
+       ASSERT_TRUE(ret == OAL_STATUS_SUCCESS);
 
-       wait_for_async(2000);
+       wait_for_async(TIMEOUT_500MILISECONDS);
 }
 
 TEST(BluetoothHAL_test, adapter_set_name_p) {
+       SKIP_NOT_SUPPORTED(g_bFeatureBT);
+
        int ret = OAL_STATUS_SUCCESS;
 
-       ret = adapter_set_name("TizenHAL");
-       ASSERT_TRUE(ret != OAL_STATUS_SUCCESS);
+       ret = adapter_set_name((char *)"TizenHAL");
+       ASSERT_TRUE(ret == OAL_STATUS_SUCCESS);
 
-       wait_for_async(2000);
+       wait_for_async(TIMEOUT_500MILISECONDS);
 }
 
 TEST(BluetoothHAL_test, adapter_is_discoverable_p) {
+       SKIP_NOT_SUPPORTED(g_bFeatureBT);
+
        int ret = OAL_STATUS_SUCCESS;
        int scan_mode = 0;
 
        ret = adapter_is_discoverable(&scan_mode);
-       ASSERT_TRUE(ret != OAL_STATUS_SUCCESS);
+       ASSERT_TRUE(ret == OAL_STATUS_SUCCESS);
 }
 
 TEST(BluetoothHAL_test, adapter_is_connectable_p) {
+       SKIP_NOT_SUPPORTED(g_bFeatureBT);
+
        int ret = OAL_STATUS_SUCCESS;
        int connectable = 0;
 
        ret = adapter_is_connectable(&connectable);
-       ASSERT_TRUE(ret != OAL_STATUS_SUCCESS);
+       ASSERT_TRUE(ret == OAL_STATUS_SUCCESS);
 }
 
 TEST(BluetoothHAL_test, adapter_get_discoverable_timeout_p) {
+       SKIP_NOT_SUPPORTED(g_bFeatureBT);
+
        int ret = OAL_STATUS_SUCCESS;
        int timeout = 0;
 
        ret = adapter_get_discoverable_timeout(&timeout);
-       ASSERT_TRUE(ret != OAL_STATUS_SUCCESS);
+       ASSERT_TRUE(ret == OAL_STATUS_SUCCESS);
 }
 
 TEST(BluetoothHAL_test, adapter_get_service_uuids_p) {
+       SKIP_NOT_SUPPORTED(g_bFeatureBT);
+
        int ret = OAL_STATUS_SUCCESS;
 
        ret = adapter_get_service_uuids();
-       ASSERT_TRUE(ret != OAL_STATUS_SUCCESS);
+       ASSERT_TRUE(ret == OAL_STATUS_SUCCESS);
 
-       wait_for_async(2000);
+       wait_for_async(TIMEOUT_500MILISECONDS);
 }
 
 TEST(BluetoothHAL_test, adapter_set_connectable_p) {
+       SKIP_NOT_SUPPORTED(g_bFeatureBT);
+
        int ret = OAL_STATUS_SUCCESS;
 
        ret = adapter_set_connectable(BT_SCAN_MODE_CONNECTABLE);
-       ASSERT_TRUE(ret != OAL_STATUS_SUCCESS);
+       ASSERT_TRUE(ret == OAL_STATUS_SUCCESS);
 
-       wait_for_async(2000);
+       wait_for_async(TIMEOUT_500MILISECONDS);
 }
 
 TEST(BluetoothHAL_test, adapter_set_discoverable_timeout_p) {
+       SKIP_NOT_SUPPORTED(g_bFeatureBT);
+
        int ret = OAL_STATUS_SUCCESS;
 
        ret = adapter_set_discoverable_timeout(0);
-       ASSERT_TRUE(ret != OAL_STATUS_SUCCESS);
+       ASSERT_TRUE(ret == OAL_STATUS_SUCCESS);
+
+       wait_for_async(TIMEOUT_500MILISECONDS);
+
+       /* Post condition : Disable BT */
+       ret = adapter_disable();
+       ASSERT_TRUE(ret == OAL_STATUS_SUCCESS);
+
+       wait_for_async(TIMEOUT_6SECONDS);
+
+       oal_bt_deinit();
 
-       wait_for_async(2000);
+       wait_for_async(TIMEOUT_500MILISECONDS);
 }