• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <base/bind.h>
18 #include <base/location.h>
19 #include <gtest/gtest.h>
20 
21 #include "bta/dm/bta_dm_int.h"
22 #include "bta/hf_client/bta_hf_client_int.h"
23 #include "bta/include/bta_hf_client_api.h"
24 #include "bta/test/common/fake_osi.h"
25 #include "common/message_loop_thread.h"
26 
27 std::map<std::string, int> mock_function_count_map;
28 
LogMsg(uint32_t trace_set_mask,const char * fmt_str,...)29 void LogMsg(uint32_t trace_set_mask, const char* fmt_str, ...) {}
30 
31 namespace base {
32 class MessageLoop;
33 }  // namespace base
34 
35 namespace {
36 constexpr uint8_t kUnusedTimer = BTA_ID_MAX;
37 }  // namespace
38 
39 class BtaDmTest : public testing::Test {
40  protected:
SetUp()41   void SetUp() override {
42     mock_function_count_map.clear();
43 
44     bta_dm_init_cb();
45 
46     for (int i = 0; i < BTA_DM_NUM_PM_TIMER; i++) {
47       for (int j = 0; j < BTA_DM_PM_MODE_TIMER_MAX; j++) {
48         bta_dm_cb.pm_timer[i].srvc_id[j] = kUnusedTimer;
49       }
50     }
51   }
TearDown()52   void TearDown() override { bta_dm_deinit_cb(); }
53 };
54 
TEST_F(BtaDmTest,nop)55 TEST_F(BtaDmTest, nop) {
56   bool status = true;
57   ASSERT_EQ(true, status);
58 }
59 
60 extern struct fake_osi_alarm_set_on_mloop fake_osi_alarm_set_on_mloop_;
61 
TEST_F(BtaDmTest,disable_no_acl_links)62 TEST_F(BtaDmTest, disable_no_acl_links) {
63   bta_dm_cb.disabling = true;
64 
65   bta_dm_disable();  // Waiting for all ACL connections to drain
66   ASSERT_EQ(0, mock_function_count_map["btm_remove_acl"]);
67   ASSERT_EQ(1, mock_function_count_map["alarm_set_on_mloop"]);
68 
69   // Execute timer callback
70   fake_osi_alarm_set_on_mloop_.cb(fake_osi_alarm_set_on_mloop_.data);
71   ASSERT_EQ(1, mock_function_count_map["alarm_set_on_mloop"]);
72   ASSERT_EQ(0, mock_function_count_map["BTIF_dm_disable"]);
73   ASSERT_EQ(1, mock_function_count_map["future_ready"]);
74   ASSERT_TRUE(!bta_dm_cb.disabling);
75 }
76 
77 extern uint16_t mock_stack_acl_num_links;
TEST_F(BtaDmTest,disable_first_pass_with_acl_links)78 TEST_F(BtaDmTest, disable_first_pass_with_acl_links) {
79   bta_dm_cb.disabling = true;
80   // ACL link is open
81   mock_stack_acl_num_links = bta_dm_cb.device_list.count = 1;
82 
83   bta_dm_disable();              // Waiting for all ACL connections to drain
84   mock_stack_acl_num_links = 0;  // ACL link has closed
85   ASSERT_EQ(1, mock_function_count_map["alarm_set_on_mloop"]);
86   ASSERT_EQ(0, mock_function_count_map["BTIF_dm_disable"]);
87 
88   // First disable pass
89   fake_osi_alarm_set_on_mloop_.cb(fake_osi_alarm_set_on_mloop_.data);
90   ASSERT_EQ(1, mock_function_count_map["alarm_set_on_mloop"]);
91   ASSERT_EQ(1, mock_function_count_map["BTIF_dm_disable"]);
92   ASSERT_TRUE(!bta_dm_cb.disabling);
93 }
94 
TEST_F(BtaDmTest,disable_second_pass_with_acl_links)95 TEST_F(BtaDmTest, disable_second_pass_with_acl_links) {
96   bta_dm_cb.disabling = true;
97   // ACL link is open
98   mock_stack_acl_num_links = bta_dm_cb.device_list.count = 1;
99 
100   bta_dm_disable();  // Waiting for all ACL connections to drain
101   ASSERT_EQ(1, mock_function_count_map["alarm_set_on_mloop"]);
102   ASSERT_EQ(0, mock_function_count_map["BTIF_dm_disable"]);
103 
104   // First disable pass
105   fake_osi_alarm_set_on_mloop_.cb(fake_osi_alarm_set_on_mloop_.data);
106   ASSERT_EQ(2, mock_function_count_map["alarm_set_on_mloop"]);
107   ASSERT_EQ(0, mock_function_count_map["BTIF_dm_disable"]);
108   ASSERT_EQ(1, mock_function_count_map["btm_remove_acl"]);
109 
110   // Second disable pass
111   fake_osi_alarm_set_on_mloop_.cb(fake_osi_alarm_set_on_mloop_.data);
112   ASSERT_EQ(1, mock_function_count_map["BTIF_dm_disable"]);
113   ASSERT_TRUE(!bta_dm_cb.disabling);
114 }
115