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

Commit eb2fa846 authored by William Escande's avatar William Escande Committed by Chris Manton
Browse files

Mock static initialization order 03

cpp static order initialization is not guarantee.
In order to avoid the mess, we need to have a lazy init schema.
Replacing all custom getter with the function call:
```
sed -i 's|mock_function_count_map\[\("[A-Za-z_]*"\)\]|get_func_call_count(\1)|' $(rg mock_function_count_map -l)
```

Bug: 265217208
Test: atest --host
Change-Id: I849ab6c212d39cb0f2f03caa94320d5988b760b0
parent fdb0f415
Loading
Loading
Loading
Loading
+8 −10
Original line number Diff line number Diff line
@@ -1033,23 +1033,21 @@ TEST_F(CsisMultiClientTest, test_discover_multiple_instances) {
TEST_F(CsisClientTest, test_storage_calls) {
  SetSampleDatabaseCsis(1, 1);

  ASSERT_EQ(0,
            mock_function_count_map["btif_storage_load_bonded_csis_devices"]);
  ASSERT_EQ(0, get_func_call_count("btif_storage_load_bonded_csis_devices"));
  TestAppRegister();
  ASSERT_EQ(1,
            mock_function_count_map["btif_storage_load_bonded_csis_devices"]);
  ASSERT_EQ(1, get_func_call_count("btif_storage_load_bonded_csis_devices"));

  ASSERT_EQ(0, mock_function_count_map["btif_storage_update_csis_info"]);
  ASSERT_EQ(0, mock_function_count_map["btif_storage_set_csis_autoconnect"]);
  ASSERT_EQ(0, get_func_call_count("btif_storage_update_csis_info"));
  ASSERT_EQ(0, get_func_call_count("btif_storage_set_csis_autoconnect"));
  TestConnect(test_address);
  InjectConnectedEvent(test_address, 1);
  GetSearchCompleteEvent(1);
  ASSERT_EQ(1, mock_function_count_map["btif_storage_set_csis_autoconnect"]);
  ASSERT_EQ(1, mock_function_count_map["btif_storage_update_csis_info"]);
  ASSERT_EQ(1, get_func_call_count("btif_storage_set_csis_autoconnect"));
  ASSERT_EQ(1, get_func_call_count("btif_storage_update_csis_info"));

  ASSERT_EQ(0, mock_function_count_map["btif_storage_remove_csis_device"]);
  ASSERT_EQ(0, get_func_call_count("btif_storage_remove_csis_device"));
  CsisClient::Get()->RemoveDevice(test_address);
  ASSERT_EQ(1, mock_function_count_map["btif_storage_remove_csis_device"]);
  ASSERT_EQ(1, get_func_call_count("btif_storage_remove_csis_device"));

  TestAppUnregister();
}
+7 −7
Original line number Diff line number Diff line
@@ -246,24 +246,24 @@ TEST_F(GroupsTest, test_group_id_assign) {
}

TEST_F(GroupsTest, test_storage_calls) {
  ASSERT_EQ(0, mock_function_count_map["btif_storage_load_bonded_groups"]);
  ASSERT_EQ(0, get_func_call_count("btif_storage_load_bonded_groups"));
  DeviceGroups::Initialize(callbacks.get());
  ASSERT_EQ(1, mock_function_count_map["btif_storage_load_bonded_groups"]);
  ASSERT_EQ(1, get_func_call_count("btif_storage_load_bonded_groups"));

  ASSERT_EQ(0, mock_function_count_map["btif_storage_add_groups"]);
  ASSERT_EQ(0, get_func_call_count("btif_storage_add_groups"));
  DeviceGroups::Get()->AddDevice(GetTestAddress(1), Uuid::kEmpty, 7);
  DeviceGroups::Get()->AddDevice(GetTestAddress(1), Uuid::kEmpty, 8);
  ASSERT_EQ(2, mock_function_count_map["btif_storage_add_groups"]);
  ASSERT_EQ(2, get_func_call_count("btif_storage_add_groups"));

  DeviceGroups::Get()->AddDevice(GetTestAddress(2), Uuid::kEmpty, 7);
  DeviceGroups::Get()->AddDevice(GetTestAddress(3), Uuid::kEmpty, 7);
  ASSERT_EQ(4, mock_function_count_map["btif_storage_add_groups"]);
  ASSERT_EQ(4, get_func_call_count("btif_storage_add_groups"));

  ASSERT_EQ(0, mock_function_count_map["btif_storage_remove_groups"]);
  ASSERT_EQ(0, get_func_call_count("btif_storage_remove_groups"));
  DeviceGroups::Get()->RemoveDevice(GetTestAddress(1));
  DeviceGroups::Get()->RemoveDevice(GetTestAddress(2));
  DeviceGroups::Get()->RemoveDevice(GetTestAddress(3));
  ASSERT_EQ(3, mock_function_count_map["btif_storage_remove_groups"]);
  ASSERT_EQ(3, get_func_call_count("btif_storage_remove_groups"));

  DeviceGroups::CleanUp(callbacks.get());
}
+1 −1
Original line number Diff line number Diff line
@@ -2696,7 +2696,7 @@ TEST_F(StateMachineTest, testStateTransitionTimeout) {

  // simulate timeout seconds passed, alarm executing
  fake_osi_alarm_set_on_mloop_.cb(fake_osi_alarm_set_on_mloop_.data);
  ASSERT_EQ(1, mock_function_count_map["alarm_set_on_mloop"]);
  ASSERT_EQ(1, get_func_call_count("alarm_set_on_mloop"));
}

MATCHER_P(dataPathIsEq, expected, "") { return (arg.data_path_id == expected); }
+21 −21
Original line number Diff line number Diff line
@@ -137,14 +137,14 @@ TEST_F(BtaDmTest, disable_no_acl_links) {
      };

  bta_dm_disable();  // Waiting for all ACL connections to drain
  ASSERT_EQ(0, mock_function_count_map["btm_remove_acl"]);
  ASSERT_EQ(1, mock_function_count_map["alarm_set_on_mloop"]);
  ASSERT_EQ(0, get_func_call_count("btm_remove_acl"));
  ASSERT_EQ(1, get_func_call_count("alarm_set_on_mloop"));

  // Execute timer callback
  alarm_callback(alarm_data);
  ASSERT_EQ(1, mock_function_count_map["alarm_set_on_mloop"]);
  ASSERT_EQ(0, mock_function_count_map["BTIF_dm_disable"]);
  ASSERT_EQ(1, mock_function_count_map["future_ready"]);
  ASSERT_EQ(1, get_func_call_count("alarm_set_on_mloop"));
  ASSERT_EQ(0, get_func_call_count("BTIF_dm_disable"));
  ASSERT_EQ(1, get_func_call_count("future_ready"));
  ASSERT_TRUE(!bta_dm_cb.disabling);

  test::mock::osi_alarm::alarm_set_on_mloop = {};
@@ -170,14 +170,14 @@ TEST_F(BtaDmTest, disable_first_pass_with_acl_links) {
      };

  bta_dm_disable();              // Waiting for all ACL connections to drain
  ASSERT_EQ(1, mock_function_count_map["alarm_set_on_mloop"]);
  ASSERT_EQ(0, mock_function_count_map["BTIF_dm_disable"]);
  ASSERT_EQ(1, get_func_call_count("alarm_set_on_mloop"));
  ASSERT_EQ(0, get_func_call_count("BTIF_dm_disable"));

  links_up = 0;
  // First disable pass
  alarm_callback(alarm_data);
  ASSERT_EQ(1, mock_function_count_map["alarm_set_on_mloop"]);
  ASSERT_EQ(1, mock_function_count_map["BTIF_dm_disable"]);
  ASSERT_EQ(1, get_func_call_count("alarm_set_on_mloop"));
  ASSERT_EQ(1, get_func_call_count("BTIF_dm_disable"));
  ASSERT_TRUE(!bta_dm_cb.disabling);

  test::mock::stack_acl::BTM_GetNumAclLinks = {};
@@ -204,18 +204,18 @@ TEST_F(BtaDmTest, disable_second_pass_with_acl_links) {
      };

  bta_dm_disable();  // Waiting for all ACL connections to drain
  ASSERT_EQ(1, mock_function_count_map["alarm_set_on_mloop"]);
  ASSERT_EQ(0, mock_function_count_map["BTIF_dm_disable"]);
  ASSERT_EQ(1, get_func_call_count("alarm_set_on_mloop"));
  ASSERT_EQ(0, get_func_call_count("BTIF_dm_disable"));

  // First disable pass
  alarm_callback(alarm_data);
  ASSERT_EQ(2, mock_function_count_map["alarm_set_on_mloop"]);
  ASSERT_EQ(0, mock_function_count_map["BTIF_dm_disable"]);
  ASSERT_EQ(1, mock_function_count_map["btm_remove_acl"]);
  ASSERT_EQ(2, get_func_call_count("alarm_set_on_mloop"));
  ASSERT_EQ(0, get_func_call_count("BTIF_dm_disable"));
  ASSERT_EQ(1, get_func_call_count("btm_remove_acl"));

  // Second disable pass
  alarm_callback(alarm_data);
  ASSERT_EQ(1, mock_function_count_map["BTIF_dm_disable"]);
  ASSERT_EQ(1, get_func_call_count("BTIF_dm_disable"));
  ASSERT_TRUE(!bta_dm_cb.disabling);

  test::mock::stack_acl::BTM_GetNumAclLinks = {};
@@ -273,7 +273,7 @@ TEST_F(BtaDmTest, bta_dm_set_encryption) {
  // Fake indication that the encryption is in progress with non-null callback
  device->p_encrypt_cback = BTA_DM_ENCRYPT_CBACK;
  bta_dm_set_encryption(bd_addr, transport, BTA_DM_ENCRYPT_CBACK, sec_act);
  ASSERT_EQ(0, mock_function_count_map["BTM_SetEncryption"]);
  ASSERT_EQ(0, get_func_call_count("BTM_SetEncryption"));
  ASSERT_EQ(1UL, BTA_DM_ENCRYPT_CBACK_queue.size());
  auto params = BTA_DM_ENCRYPT_CBACK_queue.front();
  BTA_DM_ENCRYPT_CBACK_queue.pop();
@@ -289,7 +289,7 @@ TEST_F(BtaDmTest, bta_dm_set_encryption) {
  };

  bta_dm_set_encryption(bd_addr, transport, BTA_DM_ENCRYPT_CBACK, sec_act);
  ASSERT_EQ(1, mock_function_count_map["BTM_SetEncryption"]);
  ASSERT_EQ(1, get_func_call_count("BTM_SetEncryption"));
  ASSERT_EQ(0UL, BTA_DM_ENCRYPT_CBACK_queue.size());
  device->p_encrypt_cback = nullptr;

@@ -300,7 +300,7 @@ TEST_F(BtaDmTest, bta_dm_set_encryption) {
         tBTM_BLE_SEC_ACT sec_act) -> tBTM_STATUS { return BTM_CMD_STARTED; };

  bta_dm_set_encryption(bd_addr, transport, BTA_DM_ENCRYPT_CBACK, sec_act);
  ASSERT_EQ(2, mock_function_count_map["BTM_SetEncryption"]);
  ASSERT_EQ(2, get_func_call_count("BTM_SetEncryption"));
  ASSERT_EQ(0UL, BTA_DM_ENCRYPT_CBACK_queue.size());
  ASSERT_NE(nullptr, device->p_encrypt_cback);

@@ -420,7 +420,7 @@ TEST_F(BtaDmTest, bta_dm_remname_cback__typical) {

  sync_main_handler();

  ASSERT_EQ(1, mock_function_count_map["BTM_SecDeleteRmtNameNotifyCallback"]);
  ASSERT_EQ(1, get_func_call_count("BTM_SecDeleteRmtNameNotifyCallback"));
  ASSERT_TRUE(bta_dm_search_cb.name_discover_done);
}

@@ -444,7 +444,7 @@ TEST_F(BtaDmTest, bta_dm_remname_cback__wrong_address) {

  sync_main_handler();

  ASSERT_EQ(0, mock_function_count_map["BTM_SecDeleteRmtNameNotifyCallback"]);
  ASSERT_EQ(0, get_func_call_count("BTM_SecDeleteRmtNameNotifyCallback"));
  ASSERT_FALSE(bta_dm_search_cb.name_discover_done);
}

@@ -468,6 +468,6 @@ TEST_F(BtaDmTest, bta_dm_remname_cback__HCI_ERR_CONNECTION_EXISTS) {

  sync_main_handler();

  ASSERT_EQ(1, mock_function_count_map["BTM_SecDeleteRmtNameNotifyCallback"]);
  ASSERT_EQ(1, get_func_call_count("BTM_SecDeleteRmtNameNotifyCallback"));
  ASSERT_TRUE(bta_dm_search_cb.name_discover_done);
}
+5 −7
Original line number Diff line number Diff line
@@ -26,9 +26,7 @@
#include "common/message_loop_thread.h"
#include "osi/include/allocator.h"
#include "stack/gatt/gatt_int.h"

// TODO put this in common place
extern std::map<std::string, int> mock_function_count_map;
#include "test/common/mock_functions.h"

namespace param {
struct {
@@ -168,7 +166,7 @@ TEST_F(BtaGattTest, bta_gattc_op_cmpl_read) {
  };

  bta_gattc_op_cmpl(&client_channel_control_block, &data);
  ASSERT_EQ(1, mock_function_count_map["osi_free_and_reset"]);
  ASSERT_EQ(1, get_func_call_count("osi_free_and_reset"));
  ASSERT_EQ(456, param::bta_gatt_read_complete_callback.conn_id);
  ASSERT_EQ(GATT_OUT_OF_RANGE, param::bta_gatt_read_complete_callback.status);
  ASSERT_EQ(123, param::bta_gatt_read_complete_callback.handle);
@@ -203,7 +201,7 @@ TEST_F(BtaGattTest, bta_gattc_op_cmpl_write) {
  };

  bta_gattc_op_cmpl(&client_channel_control_block, &data);
  ASSERT_EQ(1, mock_function_count_map["osi_free_and_reset"]);
  ASSERT_EQ(1, get_func_call_count("osi_free_and_reset"));
  ASSERT_EQ(456, param::bta_gatt_write_complete_callback.conn_id);
  ASSERT_EQ(2, param::bta_gatt_write_complete_callback.handle);
  ASSERT_EQ(GATT_OUT_OF_RANGE, param::bta_gatt_write_complete_callback.status);
@@ -234,7 +232,7 @@ TEST_F(BtaGattTest, bta_gattc_op_cmpl_config) {
  };

  bta_gattc_op_cmpl(&client_channel_control_block, &data);
  ASSERT_EQ(1, mock_function_count_map["osi_free_and_reset"]);
  ASSERT_EQ(1, get_func_call_count("osi_free_and_reset"));
  ASSERT_EQ(456, param::bta_gatt_configure_mtu_complete_callback.conn_id);

  ASSERT_EQ(GATT_PRC_IN_PROGRESS,
@@ -264,7 +262,7 @@ TEST_F(BtaGattTest, bta_gattc_op_cmpl_execute) {

  bta_gattc_op_cmpl(&client_channel_control_block, &data);
  ASSERT_EQ(BTA_GATTC_EXEC_EVT, param::bta_gattc_event_complete_callback.event);
  ASSERT_EQ(1, mock_function_count_map["osi_free_and_reset"]);
  ASSERT_EQ(1, get_func_call_count("osi_free_and_reset"));
}

TEST_F(BtaGattTest, bta_gattc_op_cmpl_read_interrupted) {
Loading