1 /*
2 * Copyright (c) 2021 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
18 #define private public
19 #include "common_event_command.h"
20 #undef private
21 #include "common_event_manager.h"
22 #include "common_event_subscriber.h"
23
24 using namespace testing::ext;
25 using namespace OHOS;
26 using namespace OHOS::AAFwk;
27 using namespace OHOS::EventFwk;
28
29 class CemCommandTest : public testing::Test {
30 public:
31 static void SetUpTestCase();
32 static void TearDownTestCase();
33 void SetUp() override;
34 void TearDown() override;
35 };
36
SetUpTestCase()37 void CemCommandTest::SetUpTestCase()
38 {}
39
TearDownTestCase()40 void CemCommandTest::TearDownTestCase()
41 {}
42
SetUp()43 void CemCommandTest::SetUp()
44 {
45 // reset optind to 0
46 optind = 0;
47 }
48
TearDown()49 void CemCommandTest::TearDown()
50 {}
51
52 /**
53 * @tc.number: Cem_Command_0100
54 * @tc.name: ExecCommand
55 * @tc.desc: Verify the "cem" command.
56 */
57 HWTEST_F(CemCommandTest, Cem_Command_0100, Function | MediumTest | Level1)
58 {
59 char *argv[] = {
60 (char *)TOOL_NAME.c_str(),
61 (char *)"",
62 };
63 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
64
65 CommonEventCommand cmd(argc, argv);
66 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG);
67 }
68
69 /**
70 * @tc.number: Cem_Command_0200
71 * @tc.name: ExecCommand
72 * @tc.desc: Verify the "cem xxx" command.
73 */
74 HWTEST_F(CemCommandTest, Cem_Command_0200, Function | MediumTest | Level1)
75 {
76 char *argv[] = {
77 (char *)TOOL_NAME.c_str(),
78 (char *)"xxx",
79 (char *)"",
80 };
81 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
82
83 CommonEventCommand cmd(argc, argv);
84 EXPECT_EQ(cmd.ExecCommand(), "cem: 'xxx' is not a valid cem command. See 'cem help'.\n" + HELP_MSG);
85 }
86
87 /**
88 * @tc.number: Cem_Command_0300
89 * @tc.name: ExecCommand
90 * @tc.desc: Verify the "cem -x" command.
91 */
92 HWTEST_F(CemCommandTest, Cem_Command_0300, Function | MediumTest | Level1)
93 {
94 char *argv[] = {
95 (char *)TOOL_NAME.c_str(),
96 (char *)"-x",
97 (char *)"",
98 };
99 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
100
101 CommonEventCommand cmd(argc, argv);
102 EXPECT_EQ(cmd.ExecCommand(), cmd.GetCommandErrorMsg() + HELP_MSG);
103 }
104
105 /**
106 * @tc.number: Cem_Command_0400
107 * @tc.name: ExecCommand
108 * @tc.desc: Verify the "cem -xxx" command.
109 */
110 HWTEST_F(CemCommandTest, Cem_Command_0400, Function | MediumTest | Level1)
111 {
112 char *argv[] = {
113 (char *)TOOL_NAME.c_str(),
114 (char *)"-xxx",
115 (char *)"",
116 };
117 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
118
119 CommonEventCommand cmd(argc, argv);
120 EXPECT_EQ(cmd.ExecCommand(), cmd.GetCommandErrorMsg() + HELP_MSG);
121 }
122
123 /**
124 * @tc.number: Cem_Command_0500
125 * @tc.name: ExecCommand
126 * @tc.desc: Verify the "cem help" command.
127 */
128 HWTEST_F(CemCommandTest, Cem_Command_0500, Function | MediumTest | Level1)
129 {
130 char *argv[] = {
131 (char *)TOOL_NAME.c_str(),
132 (char *)"help",
133 (char *)"",
134 };
135 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
136
137 CommonEventCommand cmd(argc, argv);
138 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG);
139 }
140