Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 49cd6191 authored by Ajay Panicker's avatar Ajay Panicker
Browse files

net_test_bluetooth: fix set and get name tests

Fixed the get and set name tests so that they are more consistent and robust.

Change-Id: I4b76357eff05531c4e52cd6de03eb3b8b969062b
parent a88923ed
Loading
Loading
Loading
Loading
+20 −27
Original line number Diff line number Diff line
@@ -61,18 +61,29 @@ TEST_F(BluetoothTest, AdapterRepeatedEnableDisable) {
  }
}

TEST_F(BluetoothTest, AdapterSetName) {
  bt_property_t *name = property_new_name("get_name");
TEST_F(BluetoothTest, AdapterSetGetName) {
  bt_property_t *name = property_new_name("BluetoothTestName");

  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";

  EXPECT_EQ(bt_interface()->get_adapter_property(BT_PROPERTY_BDNAME),
      BT_STATUS_SUCCESS);
  semaphore_wait(adapter_properties_callback_sem_);
  EXPECT_GT(GetPropertiesChangedCount(), 0)
    << "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.";

  EXPECT_EQ(bt_interface()->set_adapter_property(name), BT_STATUS_SUCCESS);
  semaphore_wait(adapter_properties_callback_sem_);
  EXPECT_GT(GetPropertyCount(), 1)
    << "Expected at least one adapter property";
  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))
@@ -80,34 +91,16 @@ TEST_F(BluetoothTest, AdapterSetName) {
    << property_as_name(GetProperty(BT_PROPERTY_BDNAME))->name
    << "' does not match test value " << 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);
}

TEST_F(BluetoothTest, AdapterGetName) {
  bt_property_t *name = property_new_name("get_name");

  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";

  EXPECT_EQ(bt_interface()->set_adapter_property(name), BT_STATUS_SUCCESS);
  EXPECT_EQ(bt_interface()->set_adapter_property(old_name), BT_STATUS_SUCCESS);
  semaphore_wait(adapter_properties_callback_sem_);
  EXPECT_EQ(bt_interface()->get_adapter_property(BT_PROPERTY_BDNAME),
      BT_STATUS_SUCCESS);
  semaphore_wait(adapter_properties_callback_sem_);
  EXPECT_TRUE(GetPropertyCount() >= 1)
    << "Expected 1 adapter property change, found " << GetPropertyCount()
    << " instead.";
  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), name))
  EXPECT_TRUE(property_equals(GetProperty(BT_PROPERTY_BDNAME), old_name))
    << "Bluetooth name '"
    << property_as_name(GetProperty(BT_PROPERTY_BDNAME))->name
    << "' does not match test value";
    << "' does not match test value " << old_name;

  EXPECT_EQ(bt_interface()->disable(), BT_STATUS_SUCCESS);
  semaphore_wait(adapter_state_changed_callback_sem_);
+6 −6
Original line number Diff line number Diff line
@@ -37,7 +37,7 @@ namespace bttest {
void BluetoothTest::SetUp() {
  bt_interface_ = nullptr;
  state_ = BT_STATE_OFF;
  property_count_ = 0;
  properties_changed_count_ = 0;
  last_changed_properties_ = nullptr;
  discovery_state_ = BT_DISCOVERY_STOPPED;
  acl_state_ = BT_ACL_STATE_DISCONNECTED;
@@ -74,12 +74,12 @@ bt_state_t BluetoothTest::GetState() {
  return state_;
}

int BluetoothTest::GetPropertyCount() {
  return property_count_;
int BluetoothTest::GetPropertiesChangedCount() {
  return properties_changed_count_;
}

bt_property_t* BluetoothTest::GetProperty(bt_property_type_t type) {
  for (int i = 0; i < property_count_; ++i) {
  for (int i = 0; i < properties_changed_count_; ++i) {
    if (last_changed_properties_[i].type == type) {
      return &last_changed_properties_[i];
    }
@@ -111,9 +111,9 @@ void BluetoothTest::AdapterPropertiesCallback(
    bt_status_t status,
    int num_properties,
    bt_property_t* new_properties) {
  property_free_array(last_changed_properties_, property_count_);
  property_free_array(last_changed_properties_, properties_changed_count_);
  last_changed_properties_ = property_copy_array(new_properties, num_properties);
  property_count_ = num_properties;
  properties_changed_count_ = num_properties;
  semaphore_post(adapter_properties_callback_sem_);
}

+4 −3
Original line number Diff line number Diff line
@@ -52,8 +52,9 @@ class BluetoothTest : public ::testing::Test,
  // Gets the current state of the Bluetooth Interface
  bt_state_t GetState();

  // Get the number of properties set
  int GetPropertyCount();
  // Get the number of properties that have changed on the
  // Adapter Properties callback
  int GetPropertiesChangedCount();

  // Get the value of a specific property
  bt_property_t* GetProperty(bt_property_type_t type);
@@ -97,7 +98,7 @@ class BluetoothTest : public ::testing::Test,
  const bt_interface_t* bt_interface_;

  bt_state_t state_;
  int property_count_;
  int properties_changed_count_;
  bt_property_t *last_changed_properties_;
  bt_discovery_state_t discovery_state_;
  bt_acl_state_t acl_state_;