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