1 /*
2  * Copyright (c) 2021-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 
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 #include "mock_common_event_stub.h"
24 #include "singleton.h"
25 
26 using namespace testing::ext;
27 using namespace OHOS;
28 using namespace OHOS::AAFwk;
29 using namespace OHOS::EventFwk;
30 
31 namespace {
32 const std::string STRING_EVENT = "com.ces.event";
33 }  // namespace
34 
35 class CemCommandDumpModuleTest : public ::testing::Test {
36 public:
37     static void SetUpTestCase();
38     static void TearDownTestCase();
39     void SetUp() override;
40     void TearDown() override;
41 
42     void SetMockObjects(const CommonEventCommand &cmd) const;
43 
44     std::string cmd_ = "dump";
45     std::string toolName_ = TOOL_NAME;
46 };
47 
SetUpTestCase()48 void CemCommandDumpModuleTest::SetUpTestCase()
49 {}
50 
TearDownTestCase()51 void CemCommandDumpModuleTest::TearDownTestCase()
52 {}
53 
SetUp()54 void CemCommandDumpModuleTest::SetUp()
55 {
56     // reset optind to 0
57     optind = 0;
58 }
59 
TearDown()60 void CemCommandDumpModuleTest::TearDown()
61 {}
62 
63 
SetMockObjects(const CommonEventCommand & cmd) const64 void CemCommandDumpModuleTest::SetMockObjects(const CommonEventCommand &cmd) const
65 {}
66 
67 class CommonEventSubscriberTest : public CommonEventSubscriber {
68 public:
CommonEventSubscriberTest(const CommonEventSubscribeInfo & subscribeInfo)69     explicit CommonEventSubscriberTest(const CommonEventSubscribeInfo &subscribeInfo)
70         : CommonEventSubscriber(subscribeInfo)
71     {}
72 
~CommonEventSubscriberTest()73     ~CommonEventSubscriberTest()
74     {}
75 
OnReceiveEvent(const CommonEventData & data)76     void OnReceiveEvent(const CommonEventData &data)
77     {}
78 };
79 
80 /**
81  * @tc.number: Cem_Command_Dump_ModuleTest_0100
82  * @tc.name: ExecCommand
83  * @tc.desc: Verify the "cem dump -e <name>" command.
84  */
85 HWTEST_F(CemCommandDumpModuleTest, Cem_Command_Dump_ModuleTest_0100, Function | MediumTest | Level1)
86 {
87     char *argv[] = {
88         (char *)toolName_.c_str(),
89         (char *)cmd_.c_str(),
90         (char *)"-e",
91         (char *)STRING_EVENT.c_str(),
92         (char *)"",
93     };
94     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
95 
96     CommonEventCommand cmd(argc, argv);
97 
98     EXPECT_EQ(cmd.ExecCommand(), "");
99 }
100 
101 /**
102  * @tc.number: Cem_Command_Dump_ModuleTest_0200
103  * @tc.name: ExecCommand
104  * @tc.desc: Verify the "cem dump -e <name>" command with a subscriber.
105  */
106 HWTEST_F(CemCommandDumpModuleTest, Cem_Command_Dump_ModuleTest_0200, Function | MediumTest | Level1)
107 {
108     /* Subscribe */
109 
110     // make matching skills
111     MatchingSkills matchingSkills;
112     matchingSkills.AddEvent(STRING_EVENT);
113 
114     // make subscriber info
115     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
116 
117     // make a subscriber object
118     auto subscriberTestPtr = std::make_shared<CommonEventSubscriberTest>(subscribeInfo);
119     // subscribe a common event
120     CommonEventManager::SubscribeCommonEvent(subscriberTestPtr);
121 
122     char *argv[] = {
123         (char *)toolName_.c_str(),
124         (char *)cmd_.c_str(),
125         (char *)"-e",
126         (char *)STRING_EVENT.c_str(),
127         (char *)"",
128     };
129     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
130 
131     CommonEventCommand cmd(argc, argv);
132 
133     EXPECT_EQ(cmd.ExecCommand(), STRING_EVENT + "\n");
134 }
135