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 <event_dump.h>
18
19 #include "event_log_helper.h"
20
21 #include "input_manager_util.h"
22 #include "multimodal_event_handler.h"
23 #include "system_info.h"
24 #include "input_manager.h"
25 #include <fcntl.h>
26 #include <cstdio>
27 #include <cerrno>
28
29 namespace OHOS {
30 namespace MMI {
31 namespace {
32 using namespace testing::ext;
33 const char *testFileName = "/data/log.log";
34 } // namespace
35
36 class EventDumpTest : public testing::Test {
37 public:
SetUp()38 void SetUp() override
39 {
40 fd_ = open(testFileName, O_WRONLY);
41 }
42
TearDown()43 void TearDown() override
44 {
45 close(fd_);
46 fd_ = -1;
47 }
48
49 int32_t fd_;
50 };
51
52 /**
53 * @tc.name: EventDumpTest_CheckCount_001
54 * @tc.desc: Event dump CheckCount
55 * @tc.type: FUNC
56 * @tc.require:AR000GJG6G
57 */
58 HWTEST_F(EventDumpTest, EventDumpTest001, TestSize.Level1)
59 {
60 std::vector<std::string> args;
61 size_t count = 0;
62 MMIEventDump->CheckCount(fd_, args, count);
63 EXPECT_EQ(count, 0);
64 }
65
66 HWTEST_F(EventDumpTest, EventDumpTest002, TestSize.Level1)
67 {
68 std::vector<std::string> args = {"--help"};
69 size_t count = 0;
70 MMIEventDump->CheckCount(fd_, args, count);
71 MMIEventDump->ParseCommand(fd_, args);
72 EXPECT_EQ(count, 1);
73 }
74
75 HWTEST_F(EventDumpTest, EventDumpTest003, TestSize.Level1)
76 {
77 std::vector<std::string> args = {"-h"};
78 size_t count = 0;
79 MMIEventDump->CheckCount(fd_, args, count);
80 MMIEventDump->ParseCommand(fd_, args);
81 EXPECT_EQ(count, 1);
82 }
83
84 HWTEST_F(EventDumpTest, EventDumpTest004, TestSize.Level1) {
85 std::vector<std::string> args = {"-abc"};
86 size_t count = 0;
87 MMIEventDump->CheckCount(fd_, args, count);
88 MMIEventDump->ParseCommand(fd_, args);
89 EXPECT_EQ(count, 3);
90 }
91
92 // 定义一个测试用例,名为MixedOptions,用于测试混合的选项和非选项的参数列表
93 HWTEST_F(EventDumpTest, EventDumpTest005, TestSize.Level1) {
94 // 定义一个包含混合的选项和非选项的参数列表
95 std::vector<std::string> args = {"-a", "--help", "foo", "-bc", "bar"};
96 // 定义一个选项个数的变量,初始值为0
97 size_t count = 0;
98 // 调用CheckCount方法,传入文件描述符,参数列表和选项个数
99 MMIEventDump->CheckCount(fd_, args, count);
100 // 调用Dump方法,传入文件描述符,参数列表
101 MMIEventDump->ParseCommand(fd_, args);
102 // 调用Dump方法,传入文件描述符,参数列表
103 MMIEventDump->DumpEventHelp(fd_, args);
104 // 使用Gtest提供的断言函数,判断选项个数是否为4
105 EXPECT_EQ(count, 4);
106 }
107 } // namespace MMI
108 } // namespace OHOS