******************************************************************************/
#include <gtest/gtest.h>
+#include <queue>
#include "bta/closure/bta_closure_int.h"
#include "bta/include/bta_closure_api.h"
*/
int test_counter = 0;
-int msg_send_counter = 0;
tBTA_SYS_EVT_HDLR* closure_handler = NULL;
-void* msg = NULL;
+std::queue<BT_HDR*> msgs;
void test_plus_one_task() { test_counter++; }
void test_plus_two_task() { test_counter += 2; }
-void fake_bta_sys_sendmsg(void* p_msg) {
- msg_send_counter++;
- msg = p_msg;
-}
+void fake_bta_sys_sendmsg(void* p_msg) { msgs.push((BT_HDR*)p_msg); }
void fake_bta_sys_register(uint8_t id, const tBTA_SYS_REG* p_reg) {
closure_handler = p_reg->evt_hdlr;
}
-bool fake_bta_sys_sendmsg_execute() { return closure_handler((BT_HDR*)msg); }
+bool fake_bta_sys_sendmsg_execute() {
+ BT_HDR* p_msg = msgs.front();
+ msgs.pop();
+ return closure_handler(p_msg);
+}
} // namespace
void LogMsg(uint32_t trace_set_mask, const char* fmt_str, ...) {}
TEST(ClosureTest, test_post_task) {
- msg_send_counter = 0;
test_counter = 0;
bta_closure_init(fake_bta_sys_register, fake_bta_sys_sendmsg);
- msg_send_counter = 0;
do_in_bta_thread(FROM_HERE, base::Bind(&test_plus_one_task));
- EXPECT_EQ(1, msg_send_counter);
- EXPECT_TRUE(msg != NULL) << "Message should not be NULL";
+ EXPECT_EQ(1U, msgs.size()) << "Message should not be NULL";
EXPECT_TRUE(fake_bta_sys_sendmsg_execute());
EXPECT_EQ(1, test_counter);
-
- // We sent only one task for execution, attempt to execute non-exiting
- // task should fail, counter shouldn't be updated
- EXPECT_FALSE(fake_bta_sys_sendmsg_execute());
- EXPECT_EQ(1, test_counter);
}
TEST(ClosureTest, test_post_multiple_tasks) {
- msg_send_counter = 0;
test_counter = 0;
bta_closure_init(fake_bta_sys_register, fake_bta_sys_sendmsg);
do_in_bta_thread(FROM_HERE, base::Bind(&test_plus_one_task));
do_in_bta_thread(FROM_HERE, base::Bind(&test_plus_two_task));
- EXPECT_EQ(6, msg_send_counter);
- EXPECT_TRUE(msg != NULL) << "Message should not be NULL";
+ EXPECT_EQ(6U, msgs.size());
EXPECT_TRUE(fake_bta_sys_sendmsg_execute());
EXPECT_EQ(1, test_counter);
EXPECT_TRUE(fake_bta_sys_sendmsg_execute());
EXPECT_EQ(9, test_counter);
-
- // We sent only one task for execution, attempt to execute non-exiting
- // task should fail, counter shouldn't be updated
- EXPECT_FALSE(fake_bta_sys_sendmsg_execute());
- EXPECT_EQ(9, test_counter);
}