1 /******************************************************************************
2 *
3 * Copyright (C) 2016 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 #include <gtest/gtest.h>
20 #include <queue>
21
22 #include "bta/closure/bta_closure_int.h"
23 #include "bta/include/bta_closure_api.h"
24 #include "include/bt_trace.h"
25
26 namespace {
27
28 /* There is no test class, because we talk to C code that accepts plain
29 * functions as arguments.
30 */
31
32 int test_counter = 0;
33 tBTA_SYS_EVT_HDLR* closure_handler = NULL;
34 std::queue<BT_HDR*> msgs;
35
test_plus_one_task()36 void test_plus_one_task() { test_counter++; }
37
test_plus_two_task()38 void test_plus_two_task() { test_counter += 2; }
39
fake_bta_sys_sendmsg(void * p_msg)40 void fake_bta_sys_sendmsg(void* p_msg) { msgs.push((BT_HDR*)p_msg); }
41
fake_bta_sys_register(uint8_t id,const tBTA_SYS_REG * p_reg)42 void fake_bta_sys_register(uint8_t id, const tBTA_SYS_REG* p_reg) {
43 closure_handler = p_reg->evt_hdlr;
44 }
45
fake_bta_sys_sendmsg_execute()46 bool fake_bta_sys_sendmsg_execute() {
47 BT_HDR* p_msg = msgs.front();
48 msgs.pop();
49 return closure_handler(p_msg);
50 }
51
52 } // namespace
53
54 // TODO(jpawlowski): there is some weird dependency issue in tests, and the
55 // tests here fail to compile without this definition.
LogMsg(uint32_t trace_set_mask,const char * fmt_str,...)56 void LogMsg(uint32_t trace_set_mask, const char* fmt_str, ...) {}
57
TEST(ClosureTest,test_post_task)58 TEST(ClosureTest, test_post_task) {
59 test_counter = 0;
60
61 bta_closure_init(fake_bta_sys_register, fake_bta_sys_sendmsg);
62
63 do_in_bta_thread(FROM_HERE, base::Bind(&test_plus_one_task));
64 EXPECT_EQ(1U, msgs.size()) << "Message should not be NULL";
65
66 EXPECT_TRUE(fake_bta_sys_sendmsg_execute());
67 EXPECT_EQ(1, test_counter);
68 }
69
TEST(ClosureTest,test_post_multiple_tasks)70 TEST(ClosureTest, test_post_multiple_tasks) {
71 test_counter = 0;
72
73 bta_closure_init(fake_bta_sys_register, fake_bta_sys_sendmsg);
74
75 do_in_bta_thread(FROM_HERE, base::Bind(&test_plus_one_task));
76 do_in_bta_thread(FROM_HERE, base::Bind(&test_plus_two_task));
77 do_in_bta_thread(FROM_HERE, base::Bind(&test_plus_one_task));
78 do_in_bta_thread(FROM_HERE, base::Bind(&test_plus_two_task));
79 do_in_bta_thread(FROM_HERE, base::Bind(&test_plus_one_task));
80 do_in_bta_thread(FROM_HERE, base::Bind(&test_plus_two_task));
81
82 EXPECT_EQ(6U, msgs.size());
83
84 EXPECT_TRUE(fake_bta_sys_sendmsg_execute());
85 EXPECT_EQ(1, test_counter);
86
87 EXPECT_TRUE(fake_bta_sys_sendmsg_execute());
88 EXPECT_EQ(3, test_counter);
89
90 EXPECT_TRUE(fake_bta_sys_sendmsg_execute());
91 EXPECT_EQ(4, test_counter);
92
93 EXPECT_TRUE(fake_bta_sys_sendmsg_execute());
94 EXPECT_EQ(6, test_counter);
95
96 EXPECT_TRUE(fake_bta_sys_sendmsg_execute());
97 EXPECT_EQ(7, test_counter);
98
99 EXPECT_TRUE(fake_bta_sys_sendmsg_execute());
100 EXPECT_EQ(9, test_counter);
101 }
102