1 /*
2  * Copyright (c) 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 "disable_hdc_plugin.h"
18 #include "edm_ipc_interface_code.h"
19 #include "iplugin_manager.h"
20 #include "plugin_singleton.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 class DisableHdcPluginTest : public testing::Test {
30 protected:
31     static void SetUpTestSuite(void);
32 
33     static void TearDownTestSuite(void);
34 };
35 
SetUpTestSuite(void)36 void DisableHdcPluginTest::SetUpTestSuite(void)
37 {
38     Utils::SetEdmInitialEnv();
39 }
40 
TearDownTestSuite(void)41 void DisableHdcPluginTest::TearDownTestSuite(void)
42 {
43     Utils::ResetTokenTypeAndUid();
44     ASSERT_TRUE(Utils::IsOriginalUTEnv());
45     std::cout << "now ut process is orignal ut env : " << Utils::IsOriginalUTEnv() << std::endl;
46 }
47 
48 /**
49  * @tc.name: TestDisableHdcPluginTestSet
50  * @tc.desc: Test DisableHdcPluginTest::OnSetPolicy function.
51  * @tc.type: FUNC
52  */
53 HWTEST_F(DisableHdcPluginTest, TestDisableHdcPluginTestSet, TestSize.Level1)
54 {
55     MessageParcel data;
56     MessageParcel reply;
57     data.WriteBool(false);
58     std::shared_ptr<IPlugin> plugin = DisableHdcPlugin::GetPlugin();
59     HandlePolicyData handlePolicyData{"false", false};
60     std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::DISABLED_HDC);
61     ErrCode ret = plugin->OnHandlePolicy(funcCode, data, reply, handlePolicyData, DEFAULT_USER_ID);
62     ASSERT_TRUE(ret == ERR_OK);
63     ASSERT_TRUE(handlePolicyData.isChanged);
64 }
65 
66 /**
67  * @tc.name: TestDisableHdcPluginTestGet
68  * @tc.desc: Test DisableHdcPluginTest::OnGetPolicy function.
69  * @tc.type: FUNC
70  */
71 HWTEST_F(DisableHdcPluginTest, TestDisableHdcPluginTestGet, TestSize.Level1)
72 {
73     std::shared_ptr<IPlugin> plugin = DisableHdcPlugin::GetPlugin();
74     std::string policyData{"false"};
75     MessageParcel data;
76     MessageParcel reply;
77     ErrCode ret = plugin->OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
78     int32_t flag = ERR_INVALID_VALUE;
79     ASSERT_TRUE(reply.ReadInt32(flag) && (flag == ERR_OK));
80     bool result = false;
81     reply.ReadBool(result);
82     ASSERT_TRUE(ret == ERR_OK);
83     ASSERT_FALSE(result);
84 }
85 } // namespace TEST
86 } // namespace EDM
87 } // namespace OHOS