1 /*
2 * Copyright (c) 2025 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 "switch_bluetooth_plugin_test.h"
17 #include "bluetooth_host.h"
18 #include "edm_ipc_interface_code.h"
19 #include "plugin_singleton.h"
20 #include "utils.h"
21
22 using namespace testing::ext;
23
24 namespace OHOS {
25 namespace EDM {
26 namespace TEST {
SetUpTestSuite(void)27 void SwitchBluetoothTest::SetUpTestSuite(void)
28 {
29 Utils::SetEdmInitialEnv();
30 }
31
TearDownTestSuite(void)32 void SwitchBluetoothTest::TearDownTestSuite(void)
33 {
34 Utils::ResetTokenTypeAndUid();
35 ASSERT_TRUE(Utils::IsOriginalUTEnv());
36 Bluetooth::BluetoothHost::GetDefaultHost().Close();
37 std::cout << "now ut process is orignal ut env : " << Utils::IsOriginalUTEnv() << std::endl;
38 }
39
40 /**
41 * @tc.name: TestOpenBluetoothSuccess
42 * @tc.desc: Test SwitchBluetoothPlugin::OnSetPolicy function sucess.
43 * @tc.type: FUNC
44 */
45 HWTEST_F(SwitchBluetoothTest, TestOpenBluetoothSuccess, TestSize.Level1)
46 {
47 uint64_t selfTokenId = GetSelfTokenID();
48 SetSelfTokenID(0);
49 std::shared_ptr<IPlugin> plugin = SwitchBluetoothPlugin::GetPlugin();
50 uint32_t code = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::SWITCH_BLUETOOTH);
51 HandlePolicyData handlePolicyData{"false", "", false};
52 MessageParcel data;
53 MessageParcel reply;
54 data.WriteBool(true);
55 ErrCode ret = plugin->OnHandlePolicy(code, data, reply, handlePolicyData, DEFAULT_USER_ID);
56 ASSERT_TRUE(ret == ERR_OK);
57 SetSelfTokenID(selfTokenId);
58 }
59
60 /**
61 * @tc.name: TestCloseBluetoothSuccess
62 * @tc.desc: Test SwitchBluetoothPlugin::OnSetPolicy function sucess.
63 * @tc.type: FUNC
64 */
65 HWTEST_F(SwitchBluetoothTest, TestCloseBluetoothSuccess, TestSize.Level1)
66 {
67 uint64_t selfTokenId = GetSelfTokenID();
68 SetSelfTokenID(0);
69 std::shared_ptr<IPlugin> plugin = SwitchBluetoothPlugin::GetPlugin();
70 uint32_t code = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::SWITCH_BLUETOOTH);
71 HandlePolicyData handlePolicyData{"false", "", false};
72 MessageParcel data;
73 MessageParcel reply;
74 data.WriteBool(false);
75 ErrCode ret = plugin->OnHandlePolicy(code, data, reply, handlePolicyData, DEFAULT_USER_ID);
76 ASSERT_TRUE(ret == ERR_OK);
77 SetSelfTokenID(selfTokenId);
78 }
79 } // namespace TEST
80 } // namespace EDM
81 } // namespace OHOS