1 /*
2 * Copyright (C) 2022 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 #include <gtest/gtest.h>
16 #include <sys/time.h>
17 #include <unistd.h>
18
19 #include <cstdint>
20 #include <string>
21
22 #include "global.h"
23 #include "securec.h"
24
25 using namespace testing::ext;
26 namespace OHOS {
27 namespace MiscServices {
28 constexpr const uint16_t EACH_LINE_LENGTH = 100;
29 constexpr const uint16_t TOTAL_LENGTH = 1000;
30 constexpr const char *CMD1 = "hidumper -s 3703 -a -a";
31 constexpr const char *CMD2 = "hidumper -s 3703 -a -h";
32 constexpr const char *CMD3 = "hidumper -s 3703 -a -test";
33 class InputMethodDfxTest : public testing::Test {
34 public:
35 static void SetUpTestCase(void);
36 static void TearDownTestCase(void);
37 static bool ExecuteCmd(const std::string &cmd, std::string &result);
38 void SetUp();
39 void TearDown();
40 };
41
SetUpTestCase(void)42 void InputMethodDfxTest::SetUpTestCase(void)
43 {
44 IMSA_HILOGI("InputMethodDfxTest::SetUpTestCase");
45 }
46
TearDownTestCase(void)47 void InputMethodDfxTest::TearDownTestCase(void)
48 {
49 IMSA_HILOGI("InputMethodDfxTest::TearDownTestCase");
50 }
51
SetUp(void)52 void InputMethodDfxTest::SetUp(void)
53 {
54 IMSA_HILOGI("InputMethodDfxTest::SetUp");
55 }
56
TearDown(void)57 void InputMethodDfxTest::TearDown(void)
58 {
59 IMSA_HILOGI("InputMethodDfxTest::TearDown");
60 }
61
ExecuteCmd(const std::string & cmd,std::string & result)62 bool InputMethodDfxTest::ExecuteCmd(const std::string &cmd, std::string &result)
63 {
64 char buff[EACH_LINE_LENGTH] = { 0x00 };
65 char output[TOTAL_LENGTH] = { 0x00 };
66 FILE *ptr = popen(cmd.c_str(), "r");
67 if (ptr != nullptr) {
68 while (fgets(buff, sizeof(buff), ptr) != nullptr) {
69 if (strcat_s(output, sizeof(output), buff) != 0) {
70 pclose(ptr);
71 ptr = nullptr;
72 return false;
73 }
74 }
75 pclose(ptr);
76 ptr = nullptr;
77 } else {
78 return false;
79 }
80 result = std::string(output);
81 return true;
82 }
83
84 /**
85 * @tc.name: InputMethodDfxTest_DumpAllMethod_001
86 * @tc.desc: DumpAllMethod
87 * @tc.type: FUNC
88 * @tc.require: issueI61PMG
89 * @tc.author: chenyu
90 */
91 HWTEST_F(InputMethodDfxTest, InputMethodDfxTest_DumpAllMethod_001, TestSize.Level0)
92 {
93 std::string result;
94 auto ret = InputMethodDfxTest::ExecuteCmd(CMD1, result);
95 EXPECT_TRUE(ret);
96 EXPECT_NE(result.find("imeList"), std::string::npos);
97 EXPECT_NE(result.find("com.example.testIme"), std::string::npos);
98 }
99
100 /**
101 * @tc.name: InputMethodDfxTest_Dump_ShowHelp_001
102 * @tc.desc: Dump ShowHelp.
103 * @tc.type: FUNC
104 * @tc.require: issueI61PMG
105 * @tc.author: chenyu
106 */
107 HWTEST_F(InputMethodDfxTest, InputMethodDfxTest_Dump_ShowHelp_001, TestSize.Level0)
108 {
109 std::string result;
110 auto ret = InputMethodDfxTest::ExecuteCmd(CMD2, result);
111 EXPECT_TRUE(ret);
112 EXPECT_NE(result.find("Description:"), std::string::npos);
113 EXPECT_NE(result.find("-h show help"), std::string::npos);
114 EXPECT_NE(result.find("-a dump all input methods"), std::string::npos);
115 }
116
117 /**
118 * @tc.name: InputMethodDfxTest_Dump_ShowIllealInformation_001
119 * @tc.desc: Dump ShowIllealInformation.
120 * @tc.type: FUNC
121 * @tc.require: issueI61PMG
122 * @tc.author: chenyu
123 */
124 HWTEST_F(InputMethodDfxTest, InputMethodDfxTest_Dump_ShowIllealInformation_001, TestSize.Level0)
125 {
126 std::string result;
127 auto ret = InputMethodDfxTest::ExecuteCmd(CMD3, result);
128 EXPECT_TRUE(ret);
129 EXPECT_NE(result.find("input dump parameter error,enter '-h' for usage."), std::string::npos);
130 }
131 } // namespace MiscServices
132 } // namespace OHOS
133