Skip to content

Commit

Permalink
Merge branch 'task/cht-1092-refactor-tests' into 'develop'
Browse files Browse the repository at this point in the history
CHT-1092. Refactor AuxVarsBool & AuxVarsMCHandle

See merge request megachat/MEGAchat!1790
  • Loading branch information
jgandres committed Dec 22, 2023
2 parents 9afafa1 + 1f5d477 commit fa8b8ce
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 53 deletions.
112 changes: 66 additions & 46 deletions tests/sdk_test/sdk_test.cpp
Expand Up @@ -933,7 +933,7 @@ TEST_F(MegaChatApiTest, WaitingRoomsJoiningOrder)
std::unique_ptr<MegaChatCall> call(megaChatApi[i]->getChatCall(mData.mChatid));
ASSERT_TRUE(call) << "Can't get call for a1. Callid: " << getChatIdStrB64(mData.mChatid);

MegaChatHandle* callId = getHandleVars().get(i, "CallIdInProgress");
MegaChatHandle* callId = handleVars().getVar(i, "CallIdInProgress");
ASSERT_TRUE(callId) << "Can't get CallInProgress var for a1";
ASSERT_NE(*callId, MEGACHAT_INVALID_HANDLE) << "Invalid callid received at onChatCallUpdate for a1";
ASSERT_NE(call->getCallId(), MEGACHAT_INVALID_HANDLE) << "Invalid callid in MegaChatCall for a1";
Expand All @@ -945,7 +945,7 @@ TEST_F(MegaChatApiTest, WaitingRoomsJoiningOrder)
CleanupFunction testCleanup = [this] () -> void
{
ExitBoolFlags eF;
addBoolExitFlag(mData.mOpIdx, eF, "CallDestroyed", false); // mOpIdx - onChatCallUpdate(CALL_STATUS_DESTROYED)
addBoolVarAndExitFlag(mData.mOpIdx, eF, "CallDestroyed", false); // mOpIdx - onChatCallUpdate(CALL_STATUS_DESTROYED)
endChatCallTestCleanup(mData.mOpIdx, eF, mData.mChatid);
closeOpenedChatrooms();
cleanChatVideoListeners();
Expand Down Expand Up @@ -1026,31 +1026,34 @@ TEST_F(MegaChatApiTest, WaitingRoomsJoiningOrder)
// ensure that users in WR are ordered by joining time.
ExitBoolFlags eF;
MegaChatHandle invalHandle = MEGACHAT_INVALID_HANDLE;
addHandleFlag(a1, "CallIdInProgress", invalHandle); // a1 - callid received at onChatCallUpdate(CALL_STATUS_IN_PROGRESS)
addBoolExitFlag(a1, eF, "CallReceived" , false); // a1 - onChatCallUpdate(CALL_STATUS_INITIAL)
addBoolExitFlag(a2, eF, "CallReceived" , false); // a2 - onChatCallUpdate(CALL_STATUS_INITIAL)
addBoolExitFlag(a3, eF, "CallReceived" , false); // a3 - onChatCallUpdate(CALL_STATUS_INITIAL)
addBoolExitFlag(a1, eF, "CallInProgress", false); // a1 - onChatCallUpdate(CALL_STATUS_IN_PROGRESS)
addHandleVar(a1, "CallIdInProgress", invalHandle); // a1 - callid received at onChatCallUpdate(CALL_STATUS_IN_PROGRESS)
addBoolVarAndExitFlag(a1, eF, "CallReceived" , false); // a1 - onChatCallUpdate(CALL_STATUS_INITIAL)
addBoolVarAndExitFlag(a2, eF, "CallReceived" , false); // a2 - onChatCallUpdate(CALL_STATUS_INITIAL)
addBoolVarAndExitFlag(a3, eF, "CallReceived" , false); // a3 - onChatCallUpdate(CALL_STATUS_INITIAL)
addBoolVarAndExitFlag(a1, eF, "CallInProgress", false); // a1 - onChatCallUpdate(CALL_STATUS_IN_PROGRESS)
startWaitingRoomCall(a1, eF, mData.mChatid, schedId,
false /*audio*/, false /*video*/);
checkCallIdInProgress(a1); // check received callid for caller(a1)
getBoolVars().cleanAll(); // clean all bool vars (this prevents conflicts in following tests)
checkCallIdInProgress(a1); // check received callid for caller(a1)

clearTemporalVars(); // important: this prevents conflicts in following test cases of this integration test

// a2 answers call
ExitBoolFlags eF1;
addBoolExitFlag(a2, eF1, "CallWR" , false); // a2 - onChatCallUpdate(CALL_STATUS_WAITING_ROOM)
addBoolExitFlag(a1, eF1, "CallWrChanged" , false); // a1 - onChatCallUpdate(CHANGE_TYPE_WR_USERS_ENTERED)
addBoolVarAndExitFlag(a2, eF1, "CallWR" , false); // a2 - onChatCallUpdate(CALL_STATUS_WAITING_ROOM)
addBoolVarAndExitFlag(a1, eF1, "CallWrChanged" , false); // a1 - onChatCallUpdate(CHANGE_TYPE_WR_USERS_ENTERED)
answerChatCall(a2, eF1, mData.mChatid, false /*video*/,
false /*audio*/);
getBoolVars().cleanAll(); // clean all bool vars (this prevents conflicts in following tests)

clearTemporalVars(); // clean-up for next test cases

// a3 answers call
ExitBoolFlags eF2;
addBoolExitFlag(a3, eF2, "CallWR" , false); // a3 - onChatCallUpdate(CALL_STATUS_WAITING_ROOM)
addBoolExitFlag(a1, eF2, "CallWrChanged" , false); // a1 - onChatCallUpdate(CHANGE_TYPE_WR_USERS_ENTERED)
addBoolVarAndExitFlag(a3, eF2, "CallWR" , false); // a3 - onChatCallUpdate(CALL_STATUS_WAITING_ROOM)
addBoolVarAndExitFlag(a1, eF2, "CallWrChanged" , false); // a1 - onChatCallUpdate(CHANGE_TYPE_WR_USERS_ENTERED)
answerChatCall(a3, eF2, mData.mChatid, false /*video*/,
false /*audio*/);
getBoolVars().cleanAll(); // clean all bool vars (this prevents conflicts in following tests)

clearTemporalVars(); // clean-up for next test cases

// a1 checks waiting room participants order
std::unique_ptr<MegaChatCall>call(megaChatApi[a1]->getChatCall(mData.mChatid));
Expand All @@ -1077,7 +1080,7 @@ TEST_F(MegaChatApiTest, RejectCall)
std::unique_ptr<MegaChatCall> call(megaChatApi[i]->getChatCall(mData.mChatid));
ASSERT_TRUE(call) << "Can't get call for " << std::to_string(i) <<". CallId: " << getChatIdStrB64(mData.mChatid);

MegaChatHandle* callId = getHandleVars().get(i, "CallIdInProgress");
MegaChatHandle* callId = handleVars().getVar(i, "CallIdInProgress");
ASSERT_TRUE(callId) << "Can't get CallInProgress var for " << std::to_string(i);
ASSERT_NE(*callId, MEGACHAT_INVALID_HANDLE) << "Invalid callId received at onChatCallUpdate for: " << std::to_string(i);
ASSERT_NE(call->getCallId(), MEGACHAT_INVALID_HANDLE) << "Invalid callId in MegaChatCall for: " << std::to_string(i);
Expand All @@ -1089,7 +1092,7 @@ TEST_F(MegaChatApiTest, RejectCall)
CleanupFunction testCleanup = [this]() -> void
{
ExitBoolFlags eF;
addBoolExitFlag(mData.mOpIdx, eF, "CallDestroyed", false); // mOpIdx - onChatCallUpdate(CALL_STATUS_DESTROYED)
addBoolVarAndExitFlag(mData.mOpIdx, eF, "CallDestroyed", false); // mOpIdx - onChatCallUpdate(CALL_STATUS_DESTROYED)
endChatCallTestCleanup(mData.mOpIdx, eF, mData.mChatid);
closeOpenedChatrooms();
cleanChatVideoListeners();
Expand Down Expand Up @@ -1146,19 +1149,19 @@ TEST_F(MegaChatApiTest, RejectCall)
// ensure that users in WR are ordered by joining time.
ExitBoolFlags eF;
MegaChatHandle invalHandle = MEGACHAT_INVALID_HANDLE;
addHandleFlag(a1, "CallIdInProgress", invalHandle); // a1 - callId received at onChatCallUpdate(CALL_STATUS_IN_PROGRESS)
addBoolExitFlag(a1, eF, "CallReceived" , false); // a1 - onChatCallUpdate(CALL_STATUS_INITIAL)
addBoolExitFlag(a2, eF, "CallReceived" , false); // a2 - onChatCallUpdate(CALL_STATUS_INITIAL)
addBoolExitFlag(a1, eF, "CallInProgress", false); // a1 - onChatCallUpdate(CALL_STATUS_IN_PROGRESS)
addHandleVar(a1, "CallIdInProgress", invalHandle); // a1 - callId received at onChatCallUpdate(CALL_STATUS_IN_PROGRESS)
addBoolVarAndExitFlag(a1, eF, "CallReceived" , false); // a1 - onChatCallUpdate(CALL_STATUS_INITIAL)
addBoolVarAndExitFlag(a2, eF, "CallReceived" , false); // a2 - onChatCallUpdate(CALL_STATUS_INITIAL)
addBoolVarAndExitFlag(a1, eF, "CallInProgress", false); // a1 - onChatCallUpdate(CALL_STATUS_IN_PROGRESS)
startCallInChat(a1, eF, mData.mChatid, false /*audio*/,
false /*video*/, false /*notRinging*/);

checkCallIdInProgress(a1); // check received callId for caller(a1)
getBoolVars().cleanAll(); // clean all bool vars (this prevents conflicts in following tests)

clearTemporalVars(); // clean-up for next test cases

LOG_debug << "#### Test2: B rejects chat call ####";
ExitBoolFlags eF1;
addBoolExitFlag(a2, eF1, "CallStopsRinging" , false); // a2 - onChatCallUpdate(CHANGE_TYPE_RINGING_STATUS)
addBoolVarAndExitFlag(a2, eF1, "CallStopsRinging" , false); // a2 - onChatCallUpdate(CHANGE_TYPE_RINGING_STATUS)
rejectCall(a2, eF1, mData.mChatid);
}
#endif
Expand Down Expand Up @@ -7212,17 +7215,36 @@ void MegaChatApiTest::initLocalSchedMeeting(const MegaChatHandle chatId, const M
rulesByMonthWeekDay));
}

void MegaChatApiTest::addHandleFlag(const unsigned int i, const std::string& n, const MegaChatHandle val)
void MegaChatApiTest::clearTemporalVars()
{
ASSERT_TRUE(getHandleVars().add(i, n, val)) << n << " couldn't be added to mAuxHandles for account " << std::to_string(i);
boolVars().cleanAll();
handleVars().cleanAll();
}

void MegaChatApiTest::addHandleVar(const unsigned int i, const std::string& n, const MegaChatHandle val)
{
// adds a handle var to mAuxHandles <var_name, MegaChatHandle>[NUM_ACCOUNTS]
ASSERT_TRUE(handleVars().add(i, n, val)) << n << " couldn't be added to mAuxHandles for account " << std::to_string(i);
}

void MegaChatApiTest::addBoolExitFlag(const unsigned int i, ExitBoolFlags &eF, const std::string& n, const bool val)
bool* MegaChatApiTest::addBoolVar(const unsigned int i, const std::string& n, const bool val)
{
bool* f = getBoolVars().add(i, n, val);
return boolVars().add(i, n, val);
}

void MegaChatApiTest::addBoolVarAndExitFlag(const unsigned int i, ExitBoolFlags &eF, const std::string& n, const bool val)
{
// adds a boolean var to mAuxBool <var_name, boolVal>[NUM_ACCOUNTS]
bool* f = addBoolVar(i, n, val);
ASSERT_TRUE(f) << n << " couldn't be added to mAuxBool for account " << std::to_string(i);
// add idx to differentiate this var from the other accounts

// add entry above, to temporal ExitBoolFlags <var_name(i), *boolVal>
// we may want to preserve all mAuxBool entries, but store a subset of them in ExitBoolFlags,
// used as an array of conditions that need to be met to exit from a waitForAction call
//
// (i) is added to var_name to differentiate this var from the other accounts.
// i.e: { {CallReceived0, *boolVal0}, {CallReceived1, *boolVal1} }
//
ASSERT_TRUE(eF.add(n + std::to_string(i), f)) << n << " couldn't be added to eF for account " << std::to_string(i);
}

Expand Down Expand Up @@ -7343,11 +7365,11 @@ void MegaChatApiTest::setChatTitle(const std::string& title, const unsigned int
{
auto idx = it.first;
// add flag to wait for onChatListItemUpdate(CHANGE_TYPE_TITLE)
ASSERT_TRUE(getBoolVars().add(idx, "titleItemChanged", false /*val*/)) << "titleItemChanged couldn't be added for account "
ASSERT_TRUE(addBoolVar(idx, "titleItemChanged", false /*val*/)) << "titleItemChanged couldn't be added for account "
<< std::to_string(idx);

// add flag to wait for onChatRoomUpdate(CHANGE_TYPE_TITLE)
ASSERT_TRUE(getBoolVars().add(idx, "titleChanged", false /*val*/)) << "titleChanged couldn't be added for account "
ASSERT_TRUE(addBoolVar(idx, "titleChanged", false /*val*/)) << "titleChanged couldn't be added for account "
<< std::to_string(idx);
});

Expand All @@ -7362,15 +7384,13 @@ void MegaChatApiTest::setChatTitle(const std::string& title, const unsigned int
std::for_each(crlisteners.begin(), crlisteners.end(), [this](const auto& it)
{
auto idx = it.first;
auto f1 = getBoolVars().get(idx, "titleItemChanged");
bool* f1 = boolVars().getVar(idx, "titleItemChanged");
ASSERT_TRUE(f1) << "titleItemChanged wait flag not found for account: " << idx;
ASSERT_TRUE(waitForResponse(f1)) << "Timeout expired for receiving chat list item update";
getBoolVars().remove(idx, "titleItemChanged");

auto f2 = getBoolVars().get(idx, "titleChanged");
bool* f2 = boolVars().getVar(idx, "titleChanged");
ASSERT_TRUE(f2) << "titleChanged wait flag not found for account: " << idx;
ASSERT_TRUE(waitForResponse(f2)) << "Timeout expired for receiving chatroom update";
getBoolVars().remove(idx, "titleChanged");
});
};

Expand Down Expand Up @@ -8593,7 +8613,7 @@ void MegaChatApiTest::onChatListItemUpdate(MegaChatApi *api, MegaChatListItem *i
}
if (item->hasChanged(MegaChatListItem::CHANGE_TYPE_TITLE))
{
getBoolVars().updateIfExists(apiIndex, "titleItemChanged", true);
boolVars().updateIfExists(apiIndex, "titleItemChanged", true);
titleUpdated[apiIndex] = true;
}
if (item->hasChanged(MegaChatListItem::CHANGE_TYPE_ARCHIVE))
Expand Down Expand Up @@ -8688,23 +8708,23 @@ void MegaChatApiTest::onChatCallUpdate(MegaChatApi *api, MegaChatCall *call)
mCallReceivedRinging[apiIndex] = true;
mChatIdRingInCall[apiIndex] = call->getChatid();
mCallIdRingIn[apiIndex] = call->getCallId();
getBoolVars().updateIfExists(apiIndex, "CallIsRinging", true);
boolVars().updateIfExists(apiIndex, "CallIsRinging", true);
}
else
{
mCallStopRinging[apiIndex] = true;
mCallIdStopRingIn[apiIndex] = call->getCallId();
mChatIdStopRingInCall[apiIndex] = call->getChatid();
getBoolVars().updateIfExists(apiIndex, "CallStopsRinging", true);
boolVars().updateIfExists(apiIndex, "CallStopsRinging", true);
}
}
}

if (call->hasChanged(MegaChatCall::CHANGE_TYPE_WR_USERS_ENTERED)
|| call->hasChanged(MegaChatCall::CHANGE_TYPE_WR_COMPOSITION))
{
getBoolVars().updateIfExists(apiIndex, "CallWrChanged", true);
mCallWrChanged[apiIndex] = true;
boolVars().updateIfExists(apiIndex, "CallWrChanged", true);
mCallWrChanged[apiIndex] = true;
}

if (call->hasChanged(MegaChatCall::CHANGE_TYPE_WR_ALLOW))
Expand Down Expand Up @@ -8752,13 +8772,13 @@ void MegaChatApiTest::onChatCallUpdate(MegaChatApi *api, MegaChatCall *call)
* we receive multiple onChatCallUpdate like a login */
mCallWithIdReceived[apiIndex] = true;
}
getBoolVars().updateIfExists(apiIndex, "CallReceived", true);
boolVars().updateIfExists(apiIndex, "CallReceived", true);
mCallReceived[apiIndex] = true;
break;

case MegaChatCall::CALL_STATUS_IN_PROGRESS:
getHandleVars().updateIfExists(apiIndex, "CallIdInProgress", call->getCallId());
getBoolVars().updateIfExists(apiIndex, "CallInProgress", true);
handleVars().updateIfExists(apiIndex, "CallIdInProgress", call->getCallId());
boolVars().updateIfExists(apiIndex, "CallInProgress", true);
mCallInProgress[apiIndex] = true;
mChatIdInProgressCall[apiIndex] = call->getChatid();
break;
Expand All @@ -8775,7 +8795,7 @@ void MegaChatApiTest::onChatCallUpdate(MegaChatApi *api, MegaChatCall *call)
}

case MegaChatCall::CALL_STATUS_DESTROYED:
getBoolVars().updateIfExists(apiIndex, "CallDestroyed", true);
boolVars().updateIfExists(apiIndex, "CallDestroyed", true);
mCallDestroyed[apiIndex] = true;
break;

Expand All @@ -8785,7 +8805,7 @@ void MegaChatApiTest::onChatCallUpdate(MegaChatApi *api, MegaChatCall *call)

case MegaChatCall::CALL_STATUS_WAITING_ROOM:
{
getBoolVars().updateIfExists(apiIndex, "CallWR", true);
boolVars().updateIfExists(apiIndex, "CallWR", true);
mCallWR[apiIndex] = true;
break;
}
Expand Down Expand Up @@ -8979,7 +8999,7 @@ void TestChatRoomListener::onChatRoomUpdate(MegaChatApi *api, MegaChatRoom *chat
}
else if (chat->hasChanged(MegaChatRoom::CHANGE_TYPE_TITLE))
{
t->getBoolVars().updateIfExists(apiIndex, "titleChanged", true);
t->boolVars().updateIfExists(apiIndex, "titleChanged", true);
titleUpdated[apiIndex] = true;
}
else if (chat->hasChanged(MegaChatRoom::CHANGE_TYPE_ARCHIVE))
Expand Down
26 changes: 19 additions & 7 deletions tests/sdk_test/sdk_test.h
Expand Up @@ -378,8 +378,8 @@ class MegaChatApiTest :
return res.second ? &res.first->second : nullptr;
}

// returns value<T> mapped by key n
T* get(const unsigned int i, const std::string_view n)
// returns pointer to value<T> mapped by key n
T* getVar(const unsigned int i, const std::string_view n)
{
if (!validInput(i, n)) { return nullptr; }
if (!exists(i, n)) { return nullptr; }
Expand Down Expand Up @@ -540,8 +540,8 @@ class MegaChatApiTest :
using AuxVarsBool = AuxVars<bool>;
using AuxVarsMCHandle = AuxVars<megachat::MegaChatHandle>;

AuxVarsBool& getBoolVars() { return mAuxBool; };
AuxVarsMCHandle& getHandleVars() { return mAuxHandles; };
AuxVarsBool& boolVars() { return mAuxBool; };
AuxVarsMCHandle& handleVars() { return mAuxHandles; };

protected:
static Account& account(unsigned i) { return getEnv().account(i); }
Expand Down Expand Up @@ -659,11 +659,17 @@ class MegaChatApiTest :
const ::megachat::MegaChatPeerList* peerlist, const ::mega::MegaIntegerList* rulesByWeekDay,
const ::mega::MegaIntegerList* rulesByMonthDay, const ::mega::MegaIntegerMap* rulesByMonthWeekDay);

// Adds a temporal MegaChatHandle variable, to MegaChatApiTest::mAuxHandles
void addHandleFlag(const unsigned int i, const std::string& n, const ::megachat::MegaChatHandle val);
// Clears all temporal variables from MegaChatApiTest::mAuxHandles and MegaChatApiTest::mAuxBool
void clearTemporalVars();

// Adds a temporal MegaChatHandle variable to MegaChatApiTest::mAuxHandles
void addHandleVar(const unsigned int i, const std::string& n, const ::megachat::MegaChatHandle val);

// Adds a temporal boolean variable to MegaChatApiTest::mAuxBool
bool* addBoolVar(const unsigned int i, const std::string& n, const bool val);

// Adds a temporal boolean variable, to ExitBoolFlags param, and also to MegaChatApiTest::mAuxBool
void addBoolExitFlag(const unsigned int i, ExitBoolFlags &eF, const std::string& n, const bool val);
void addBoolVarAndExitFlag(const unsigned int i, ExitBoolFlags &eF, const std::string& n, const bool val);

#ifndef KARERE_DISABLE_WEBRTC

Expand Down Expand Up @@ -900,13 +906,19 @@ class MegaChatApiTest :

// Aux vars maps: these maps can be used to add temporal variables that needs to be updated by any callback or code path,
// this avoids defining amounts of vars in MegaChatApiTest class
//
// The lifetime of these maps is the lifetime of MegaChatApiTest, so these maps will be automatically released after
// each test execution. To prevent conflicts between different test cases at any particular automated test, it's recommended to remove
// unused variables after it's use, or clear maps (just in case we don't need any variable already defined).

// maps a var name to boolean.
// It can be used to "register" temporal boolean variables that will be used to wait for async events.
// <var_name, boolVal>[NUM_ACCOUNTS]
AuxVarsBool mAuxBool;

// maps a var name to MegaChatHandle
// It can be used to "register" temporal variables that will be used to store received handles on MegaChat callbacks
// <var_name, MegaChatHandle>[NUM_ACCOUNTS]
AuxVarsMCHandle mAuxHandles;

class TestEnv
Expand Down

0 comments on commit fa8b8ce

Please sign in to comment.