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 "bluetooth_host.h"
21 #include "iplugin_manager.h"
22 #include "utils.h"
23
24 using namespace testing::ext;
25 using namespace testing;
26
27 namespace OHOS {
28 namespace EDM {
29 namespace TEST {
30 const int BLUETOOTH_TURN_ON = 2;
31 const int32_t INVALID_BLUETOOTH_STATE = -1;
32
33 enum BluetoothState {
34 TURN_OFF,
35 TURNING_ON,
36 TURN_ON,
37 TURNING_OFF,
38 };
39
40 enum BluetoothConnectionState {
41 DISCONNECTED,
42 CONNECTING,
43 CONNECTED,
44 DISCONNECTING,
45 };
46
47 class GetBluetoothInfoPluginTest : public testing::Test {
48 protected:
49 static void SetUpTestSuite(void);
50
51 static void TearDownTestSuite(void);
52 };
53
SetUpTestSuite(void)54 void GetBluetoothInfoPluginTest::SetUpTestSuite(void)
55 {
56 Utils::SetEdmServiceEnable();
57 Utils::SetEdmInitialEnv();
58 }
59
TearDownTestSuite(void)60 void GetBluetoothInfoPluginTest::TearDownTestSuite(void)
61 {
62 Utils::SetEdmServiceDisable();
63 Utils::ResetTokenTypeAndUid();
64 ASSERT_TRUE(Utils::IsOriginalUTEnv());
65 Bluetooth::BluetoothHost::GetDefaultHost().Close();
66 std::cout << "now ut process is orignal ut env : " << Utils::IsOriginalUTEnv() << std::endl;
67 }
68
69 /**
70 * @tc.name: GetBluetoothInfoSuc
71 * @tc.desc: Test get bluetooth info function.
72 * @tc.type: FUNC
73 */
74 HWTEST_F(GetBluetoothInfoPluginTest, GetBluetoothInfoSuc, TestSize.Level1)
75 {
76 GetBluetoothInfoPlugin plugin;
77 std::string policyValue{"GetBluetoothInfo"};
78 MessageParcel data;
79 MessageParcel reply;
80 plugin.OnGetPolicy(policyValue, data, reply, DEFAULT_USER_ID);
81 ASSERT_TRUE(reply.ReadInt32() == ERR_OK);
82
83 std::string name = reply.ReadString();
84 int state = reply.ReadInt32();
85 int connectionState = reply.ReadInt32();
86 ASSERT_TRUE(((state == 0) && (connectionState == 0)) ||
87 ((name != "") && (state == BLUETOOTH_TURN_ON)));
88 }
89
90 /**
91 * @tc.name: TransformBluetoothStateSuc
92 * @tc.desc: Test TransformBluetoothState function.
93 * @tc.type: FUNC
94 */
95 HWTEST_F(GetBluetoothInfoPluginTest, TransformBluetoothStateSuc, TestSize.Level1)
96 {
97 GetBluetoothInfoPlugin plugin;
98 int32_t realState = plugin.TransformBluetoothState(static_cast<int32_t>(Bluetooth::BTStateID::STATE_TURNING_ON));
99 ASSERT_TRUE(realState == BluetoothState::TURNING_ON);
100 realState = plugin.TransformBluetoothState(static_cast<int32_t>(Bluetooth::BTStateID::STATE_TURN_ON));
101 ASSERT_TRUE(realState == BluetoothState::TURN_ON);
102 realState = plugin.TransformBluetoothState(static_cast<int32_t>(Bluetooth::BTStateID::STATE_TURNING_OFF));
103 ASSERT_TRUE(realState == BluetoothState::TURNING_OFF);
104 realState = plugin.TransformBluetoothState(static_cast<int32_t>(Bluetooth::BTStateID::STATE_TURN_OFF));
105 ASSERT_TRUE(realState == BluetoothState::TURN_OFF);
106 realState = plugin.TransformBluetoothState(INVALID_BLUETOOTH_STATE);
107 ASSERT_TRUE(realState == INVALID_BLUETOOTH_STATE);
108 }
109
110 /**
111 * @tc.name: TransformBluetoothConnectionStateSuc
112 * @tc.desc: Test TransformBluetoothConnectionState function.
113 * @tc.type: FUNC
114 */
115 HWTEST_F(GetBluetoothInfoPluginTest, TransformBluetoothConnectionStateSuc, TestSize.Level1)
116 {
117 GetBluetoothInfoPlugin plugin;
118 int32_t realConnectionState =
119 plugin.TransformBluetoothConnectionState(static_cast<int32_t>(Bluetooth::BTConnectState::CONNECTING));
120 ASSERT_TRUE(realConnectionState == BluetoothConnectionState::CONNECTING);
121 realConnectionState =
122 plugin.TransformBluetoothConnectionState(static_cast<int32_t>(Bluetooth::BTConnectState::CONNECTED));
123 ASSERT_TRUE(realConnectionState == BluetoothConnectionState::CONNECTED);
124 realConnectionState =
125 plugin.TransformBluetoothConnectionState(static_cast<int32_t>(Bluetooth::BTConnectState::DISCONNECTING));
126 ASSERT_TRUE(realConnectionState == BluetoothConnectionState::DISCONNECTING);
127 realConnectionState =
128 plugin.TransformBluetoothConnectionState(static_cast<int32_t>(Bluetooth::BTConnectState::DISCONNECTED));
129 ASSERT_TRUE(realConnectionState == BluetoothConnectionState::DISCONNECTED);
130 realConnectionState =
131 plugin.TransformBluetoothConnectionState(INVALID_BLUETOOTH_STATE);
132 ASSERT_TRUE(realConnectionState == INVALID_BLUETOOTH_STATE);
133 }
134 } // namespace TEST
135 } // namespace EDM
136 } // namespace OHOS