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 "disable_microphone_plugin.h"
17 
18 #include <gtest/gtest.h>
19 
20 #include "parameters.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 std::string PARAM_EDM_MIC_DISABLE = "persist.edm.mic_disable";
30 class DisableMicrophonePluginTest : public testing::Test {
31 protected:
32     static void SetUpTestSuite(void);
33 
34     static void TearDownTestSuite(void);
35 };
36 
SetUpTestSuite(void)37 void DisableMicrophonePluginTest::SetUpTestSuite(void)
38 {
39     Utils::SetEdmInitialEnv();
40 }
41 
TearDownTestSuite(void)42 void DisableMicrophonePluginTest::TearDownTestSuite(void)
43 {
44     Utils::ResetTokenTypeAndUid();
45     ASSERT_TRUE(Utils::IsOriginalUTEnv());
46     std::cout << "now ut process is orignal ut env : " << Utils::IsOriginalUTEnv() << std::endl;
47 }
48 
49 /**
50  * @tc.name: TestDisableMicrophonePluginTestSetTrue
51  * @tc.desc: Test DisableMicrophonePluginTest::OnSetPolicy function.
52  * @tc.type: FUNC
53  */
54 HWTEST_F(DisableMicrophonePluginTest, TestDisableMicrophonePluginTestSetTrue, TestSize.Level1)
55 {
56     bool isDisallow = true;
57     DisableMicrophonePlugin plugin;
58     ErrCode ret = plugin.OnSetPolicy(isDisallow);
59     ASSERT_TRUE(ret == ERR_OK);
60     ASSERT_TRUE(system::GetBoolParameter(PARAM_EDM_MIC_DISABLE, false));
61 }
62 
63 /**
64  * @tc.name: TestDisableMicrophonePluginTestSetFalse
65  * @tc.desc: Test DisableMicrophonePluginTest::OnSetPolicy function and audioSystemManager::SetMicrophoneMute.
66  * @tc.type: FUNC
67  */
68 HWTEST_F(DisableMicrophonePluginTest, TestDisableMicrophonePluginTestSetFalse, TestSize.Level1)
69 {
70     bool isDisallow = false;
71     DisableMicrophonePlugin plugin;
72     ErrCode ret = plugin.OnSetPolicy(isDisallow);
73     ASSERT_TRUE(ret == ERR_OK);
74     ASSERT_FALSE(system::GetBoolParameter(PARAM_EDM_MIC_DISABLE, true));
75 }
76 
77 /**
78  * @tc.name: TestDisableMicrophonePluginTestGet
79  * @tc.desc: Test DisableMicrophonePluginTest::OnGetPolicy function.
80  * @tc.type: FUNC
81  */
82 HWTEST_F(DisableMicrophonePluginTest, TestDisableMicrophonePluginTestGet, TestSize.Level1)
83 {
84     std::string policyData;
85     MessageParcel data;
86     MessageParcel reply;
87     int32_t userId = 0;
88     DisableMicrophonePlugin plugin;
89     ErrCode ret = plugin.OnGetPolicy(policyData, data, reply, userId);
90     ASSERT_TRUE(ret == ERR_OK);
91     ASSERT_TRUE(reply.ReadInt32() == ERR_OK);
92     ASSERT_FALSE(reply.ReadBool());
93 }
94 } // namespace TEST
95 } // namespace EDM
96 } // namespace OHOS
97