1 /*
2  * Copyright (c) 2023-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <gtest/gtest.h>
17 #include "edm_data_ability_utils_mock.h"
18 #include "edm_ipc_interface_code.h"
19 #include "get_bluetooth_info_plugin.h"
20 #include "iplugin_manager.h"
21 #include "utils.h"
22 
23 using namespace testing::ext;
24 using namespace testing;
25 
26 namespace OHOS {
27 namespace EDM {
28 namespace TEST {
29 const int BLUETOOTH_TURN_ON = 2;
30 const int32_t INVALID_BLUETOOTH_STATE = -1;
31 
32 enum BluetoothState {
33     TURN_OFF,
34     TURNING_ON,
35     TURN_ON,
36     TURNING_OFF,
37 };
38 
39 enum BluetoothConnectionState {
40     DISCONNECTED,
41     CONNECTING,
42     CONNECTED,
43     DISCONNECTING,
44 };
45 
46 class GetBluetoothInfoPluginTest : public testing::Test {
47 protected:
48     static void SetUpTestSuite(void);
49 
50     static void TearDownTestSuite(void);
51 };
52 
SetUpTestSuite(void)53 void GetBluetoothInfoPluginTest::SetUpTestSuite(void)
54 {
55     Utils::SetEdmServiceEnable();
56     Utils::SetEdmInitialEnv();
57 }
58 
TearDownTestSuite(void)59 void GetBluetoothInfoPluginTest::TearDownTestSuite(void)
60 {
61     Utils::SetEdmServiceDisable();
62     Utils::ResetTokenTypeAndUid();
63     ASSERT_TRUE(Utils::IsOriginalUTEnv());
64     std::cout << "now ut process is orignal ut env : " << Utils::IsOriginalUTEnv() << std::endl;
65 }
66 
67 /**
68  * @tc.name: GetBluetoothInfoSuc
69  * @tc.desc: Test get bluetooth info function.
70  * @tc.type: FUNC
71  */
72 HWTEST_F(GetBluetoothInfoPluginTest, GetBluetoothInfoSuc, TestSize.Level1)
73 {
74     GetBluetoothInfoPlugin plugin;
75     std::string policyValue{"GetBluetoothInfo"};
76     MessageParcel data;
77     MessageParcel reply;
78     plugin.OnGetPolicy(policyValue, data, reply, DEFAULT_USER_ID);
79     ASSERT_TRUE(reply.ReadInt32() == ERR_OK);
80 
81     std::string name = reply.ReadString();
82     int state = reply.ReadInt32();
83     int connectionState = reply.ReadInt32();
84     ASSERT_TRUE(((state == 0) && (connectionState == 0)) ||
85         ((name != "") && (state == BLUETOOTH_TURN_ON)));
86 }
87 
88 /**
89  * @tc.name: TransformBluetoothStateSuc
90  * @tc.desc: Test TransformBluetoothState function.
91  * @tc.type: FUNC
92  */
93 HWTEST_F(GetBluetoothInfoPluginTest, TransformBluetoothStateSuc, TestSize.Level1)
94 {
95     GetBluetoothInfoPlugin plugin;
96     int32_t realState = plugin.TransformBluetoothState(static_cast<int32_t>(Bluetooth::BTStateID::STATE_TURNING_ON));
97     ASSERT_TRUE(realState == BluetoothState::TURNING_ON);
98     realState = plugin.TransformBluetoothState(static_cast<int32_t>(Bluetooth::BTStateID::STATE_TURN_ON));
99     ASSERT_TRUE(realState == BluetoothState::TURN_ON);
100     realState = plugin.TransformBluetoothState(static_cast<int32_t>(Bluetooth::BTStateID::STATE_TURNING_OFF));
101     ASSERT_TRUE(realState == BluetoothState::TURNING_OFF);
102     realState = plugin.TransformBluetoothState(static_cast<int32_t>(Bluetooth::BTStateID::STATE_TURN_OFF));
103     ASSERT_TRUE(realState == BluetoothState::TURN_OFF);
104     realState = plugin.TransformBluetoothState(INVALID_BLUETOOTH_STATE);
105     ASSERT_TRUE(realState == INVALID_BLUETOOTH_STATE);
106 }
107 
108 /**
109  * @tc.name: TransformBluetoothConnectionStateSuc
110  * @tc.desc: Test TransformBluetoothConnectionState function.
111  * @tc.type: FUNC
112  */
113 HWTEST_F(GetBluetoothInfoPluginTest, TransformBluetoothConnectionStateSuc, TestSize.Level1)
114 {
115     GetBluetoothInfoPlugin plugin;
116     int32_t realConnectionState =
117         plugin.TransformBluetoothConnectionState(static_cast<int32_t>(Bluetooth::BTConnectState::CONNECTING));
118     ASSERT_TRUE(realConnectionState == BluetoothConnectionState::CONNECTING);
119     realConnectionState =
120         plugin.TransformBluetoothConnectionState(static_cast<int32_t>(Bluetooth::BTConnectState::CONNECTED));
121     ASSERT_TRUE(realConnectionState == BluetoothConnectionState::CONNECTED);
122     realConnectionState =
123         plugin.TransformBluetoothConnectionState(static_cast<int32_t>(Bluetooth::BTConnectState::DISCONNECTING));
124     ASSERT_TRUE(realConnectionState == BluetoothConnectionState::DISCONNECTING);
125     realConnectionState =
126         plugin.TransformBluetoothConnectionState(static_cast<int32_t>(Bluetooth::BTConnectState::DISCONNECTED));
127     ASSERT_TRUE(realConnectionState == BluetoothConnectionState::DISCONNECTED);
128     realConnectionState =
129         plugin.TransformBluetoothConnectionState(INVALID_BLUETOOTH_STATE);
130     ASSERT_TRUE(realConnectionState == INVALID_BLUETOOTH_STATE);
131 }
132 } // namespace TEST
133 } // namespace EDM
134 } // namespace OHOS