OSDN Git Service

net_test_bluetooth: Improve set/get name logic
authorAjay Panicker <apanicke@google.com>
Tue, 22 Dec 2015 21:09:48 +0000 (13:09 -0800)
committerAjay Panicker <apanicke@google.com>
Wed, 23 Dec 2015 02:17:57 +0000 (18:17 -0800)
Without this CL semaphore_wait would instantly return due to the fact
that enabling the adapter would cause the properties callback to post
immediatly. Also prevented the test from returning a false positive if the
original device name was the same as the test name.

Change-Id: I987efcb9a5ef58209d37d2fc21f3c149fb3e556c

test/suite/adapter/adapter_unittest.cpp
test/suite/adapter/bluetooth_test.cpp
test/suite/adapter/bluetooth_test.h

index f39c247..14d4de9 100644 (file)
@@ -62,13 +62,17 @@ TEST_F(BluetoothTest, AdapterRepeatedEnableDisable) {
 }
 
 TEST_F(BluetoothTest, AdapterSetGetName) {
-  bt_property_t *name = property_new_name("BluetoothTestName");
+  bt_property_t *new_name = property_new_name("BluetoothTestName1");
 
   EXPECT_EQ(bt_interface()->enable(), BT_STATUS_SUCCESS);
   semaphore_wait(adapter_state_changed_callback_sem_);
   EXPECT_EQ(GetState(), BT_STATE_ON)
     << "Test should be run with Adapter enabled";
 
+  // Enabling the interface will call the properties callback twice before
+  // ever reaching this point.
+  ClearSemaphore(adapter_properties_callback_sem_);
+
   EXPECT_EQ(bt_interface()->get_adapter_property(BT_PROPERTY_BDNAME),
       BT_STATUS_SUCCESS);
   semaphore_wait(adapter_properties_callback_sem_);
@@ -76,36 +80,34 @@ TEST_F(BluetoothTest, AdapterSetGetName) {
     << "Expected at least one adapter property to change";
   bt_property_t *old_name = GetProperty(BT_PROPERTY_BDNAME);
   EXPECT_NE(old_name, nullptr);
-  EXPECT_FALSE(property_equals(name, old_name))
-    << "Current name of the device matches test name. "
-    << "Please change the device name for this test.";
+  if (property_equals(old_name, new_name)) {
+    property_free(new_name);
+    new_name = property_new_name("BluetoothTestName2");
+  }
 
-  EXPECT_EQ(bt_interface()->set_adapter_property(name), BT_STATUS_SUCCESS);
+  EXPECT_EQ(bt_interface()->set_adapter_property(new_name), BT_STATUS_SUCCESS);
   semaphore_wait(adapter_properties_callback_sem_);
   EXPECT_GT(GetPropertiesChangedCount(), 0)
     << "Expected at least one adapter property to change";
   EXPECT_TRUE(GetProperty(BT_PROPERTY_BDNAME))
     << "The Bluetooth name property did not change.";
-  EXPECT_TRUE(property_equals(GetProperty(BT_PROPERTY_BDNAME), name))
-    << "Bluetooth name '"
+  EXPECT_TRUE(property_equals(GetProperty(BT_PROPERTY_BDNAME), new_name))
+    << "Bluetooth name "
     << property_as_name(GetProperty(BT_PROPERTY_BDNAME))->name
-    << "' does not match test value " << name;
+    << " does not match test value " << property_as_name(new_name)->name;
+
 
   EXPECT_EQ(bt_interface()->set_adapter_property(old_name), BT_STATUS_SUCCESS);
   semaphore_wait(adapter_properties_callback_sem_);
-  EXPECT_GT(GetPropertiesChangedCount(), 0)
-    << "Expected at least one adapter property";
-  EXPECT_TRUE(GetProperty(BT_PROPERTY_BDNAME))
-    << "The Bluetooth name property did not change.";
   EXPECT_TRUE(property_equals(GetProperty(BT_PROPERTY_BDNAME), old_name))
-    << "Bluetooth name '"
+    << "Bluetooth name "
     << property_as_name(GetProperty(BT_PROPERTY_BDNAME))->name
-    << "' does not match test value " << old_name;
+    << " does not match original name" << property_as_name(old_name)->name;
 
   EXPECT_EQ(bt_interface()->disable(), BT_STATUS_SUCCESS);
   semaphore_wait(adapter_state_changed_callback_sem_);
   EXPECT_EQ(GetState(), BT_STATE_OFF) << "Adapter did not turn off.";
-  property_free(name);
+  property_free(new_name);
 }
 
 TEST_F(BluetoothTest, AdapterStartDiscovery) {
index 1046d75..91229db 100644 (file)
@@ -66,6 +66,10 @@ void BluetoothTest::TearDown() {
   ASSERT_FALSE(bt_hal_interface->IsInitialized());
 }
 
+void BluetoothTest::ClearSemaphore(semaphore_t* sem) {
+  while (semaphore_try_wait(sem));
+}
+
 const bt_interface_t* BluetoothTest::bt_interface() {
   return bt_interface_;
 }
index 1b02c13..4871d25 100644 (file)
@@ -68,6 +68,9 @@ class BluetoothTest : public ::testing::Test,
   // Get the current Bond State
   bt_bond_state_t GetBondState();
 
+  // Reset a semaphores count to 0
+  void ClearSemaphore(semaphore_t* sem);
+
   // SetUp initializes the Bluetooth interface and registers the callbacks
   // before running every test
   void SetUp() override;