1 /*
2  * Copyright (c) 2024 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 #define private public
17 #define protected public
18 #include "ime_system_channel.h"
19 #undef private
20 #include <gtest/gtest.h>
21 
22 #include "scope_utils.h"
23 #include "tdd_util.h"
24 using namespace testing;
25 using namespace testing::ext;
26 namespace OHOS {
27 namespace MiscServices {
28 class OnSystemCmdListenerImpl : public OnSystemCmdListener {
ReceivePrivateCommand(const std::unordered_map<std::string,PrivateDataValue> & privateCommand)29     void ReceivePrivateCommand(const std::unordered_map<std::string, PrivateDataValue> &privateCommand) override
30     {
31     }
NotifyPanelStatus(const SysPanelStatus & sysPanelStatus)32     void NotifyPanelStatus(const SysPanelStatus &sysPanelStatus) override
33     {
34     }
35 };
36 class ImeSystemChannelTest : public testing::Test {
37 public:
38     static void SetUpTestCase(void);
39     static void TearDownTestCase(void);
40     void SetUp();
41     void TearDown();
42     static sptr<ImeSystemCmdChannel> imeSystemChannel_;
43     static sptr<OnSystemCmdListener> sysCmdListener_;
44     static uint64_t permissionTokenId_;
45 };
46 sptr<ImeSystemCmdChannel> ImeSystemChannelTest::imeSystemChannel_;
47 sptr<OnSystemCmdListener> ImeSystemChannelTest::sysCmdListener_;
48 uint64_t ImeSystemChannelTest::permissionTokenId_ = 0;
49 
SetUpTestCase(void)50 void ImeSystemChannelTest::SetUpTestCase(void)
51 {
52     TddUtil::StorageSelfTokenID();
53     imeSystemChannel_ = ImeSystemCmdChannel::GetInstance();
54     sysCmdListener_ = new (std::nothrow) OnSystemCmdListenerImpl();
55     permissionTokenId_ =
56         TddUtil::AllocTestTokenID(true, "ohos.inputMethod.test", { "ohos.permission.CONNECT_IME_ABILITY" });
57 }
58 
TearDownTestCase(void)59 void ImeSystemChannelTest::TearDownTestCase(void)
60 {
61     IMSA_HILOGI("ImeSystemChannelTest::TearDownTestCase");
62     imeSystemChannel_->ConnectSystemCmd(nullptr);
63     imeSystemChannel_->ClearSystemCmdAgent();
64     TddUtil::RestoreSelfTokenID();
65 }
66 
SetUp(void)67 void ImeSystemChannelTest::SetUp(void)
68 {
69     IMSA_HILOGI("ImeSystemChannelTest::SetUp");
70 }
71 
TearDown(void)72 void ImeSystemChannelTest::TearDown(void)
73 {
74     IMSA_HILOGI("ImeSystemChannelTest::TearDown");
75 }
76 
77 /**
78  * @tc.name: testConnectSystemCmd001
79  * @tc.desc: SystemCmdChannel ConnectSystemCmd.
80  * @tc.type: FUNC
81  * @tc.require:
82  */
83 HWTEST_F(ImeSystemChannelTest, testConnectSystemCmd001, TestSize.Level0)
84 {
85     IMSA_HILOGI("ImeSystemChannelTest testConnectSystemCmd001 Test START");
86     auto ret = imeSystemChannel_->ConnectSystemCmd(sysCmdListener_);
87     EXPECT_EQ(ret, ErrorCode::ERROR_SYSTEM_CMD_CHANNEL_ERROR);
88 }
89 
90 /**
91  * @tc.name: testConnectSystemCmd002
92  * @tc.desc: SystemCmdChannel ConnectSystemCmd.
93  * @tc.type: FUNC
94  * @tc.require:
95  */
96 HWTEST_F(ImeSystemChannelTest, testConnectSystemCmd002, TestSize.Level0)
97 {
98     IMSA_HILOGI("ImeSystemChannelTest testConnectSystemCmd002 Test START");
99     TokenScope scope(ImeSystemChannelTest::permissionTokenId_);
100     auto ret = imeSystemChannel_->ConnectSystemCmd(sysCmdListener_);
101     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
102 }
103 
104 /**
105  * @tc.name: testSendPrivateCommand001
106  * @tc.desc: SystemCmdChannel SendPrivateCommand.
107  * @tc.type: FUNC
108  * @tc.require:
109  */
110 HWTEST_F(ImeSystemChannelTest, testSendPrivateCommand001, TestSize.Level0)
111 {
112     IMSA_HILOGI("ImeSystemChannelTest testSendPrivateCommand001 Test START");
113     TokenScope scope(ImeSystemChannelTest::permissionTokenId_);
114     std::unordered_map<std::string, PrivateDataValue> privateCommand;
115     auto ret = imeSystemChannel_->SendPrivateCommand(privateCommand);
116     EXPECT_EQ(ret, ErrorCode::ERROR_INVALID_PRIVATE_COMMAND);
117 }
118 
119 /**
120  * @tc.name: testImeSystemChannel_nullptr
121  * @tc.desc: SystemCmdChannel ReceivePrivateCommand.
122  * @tc.type: FUNC
123  * @tc.require:
124  */
125 HWTEST_F(ImeSystemChannelTest, testImeSystemChannel_nullptr, TestSize.Level0)
126 {
127     IMSA_HILOGI("ImeSystemChannelTest testImeSystemChannel_nullptr Test START");
128     std::unordered_map<std::string, PrivateDataValue> privateCommand;
129     PrivateDataValue privateDataValue1 = std::string("stringValue");
130     privateCommand.emplace("value1", privateDataValue1);
131     imeSystemChannel_->systemCmdListener_ = nullptr;
132     imeSystemChannel_->OnConnectCmdReady(nullptr);
133     imeSystemChannel_->GetSmartMenuCfg();
134     int32_t ret = imeSystemChannel_->ReceivePrivateCommand(privateCommand);
135     EXPECT_EQ(ret, ErrorCode::ERROR_EX_NULL_POINTER);
136     ret = imeSystemChannel_->NotifyPanelStatus({ false, 0, 0, 0 });
137     EXPECT_EQ(ret, ErrorCode::ERROR_NULL_POINTER);
138 }
139 } // namespace MiscServices
140 } // namespace OHOS