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 #include <thread>
19 #include "common_event_manager.h"
20 #include "common_event_subscriber.h"
21
22 using namespace testing::ext;
23 using namespace OHOS;
24 using namespace OHOS::EventFwk;
25
26 namespace {
27 const std::string STRING_EVENT = "com.ces.event";
28 const std::string STRING_NO_SUBSCRIBERS =
29 "Subscribers:\tNo information\n"
30 "Sticky Events:\tNo information\n"
31 "Pending Events:\tNo information\n"
32 "History Events:\tNo information\n";
33 const int32_t TIME_DELAY_FOR_SERVICES = 2;
34
ExecuteCommand(const std::string & command)35 std::string ExecuteCommand(const std::string &command)
36 {
37 std::string result = "";
38 FILE *file = popen(command.c_str(), "r");
39
40 // wait for services
41 std::this_thread::sleep_for(std::chrono::seconds(TIME_DELAY_FOR_SERVICES));
42
43 if (file != nullptr) {
44 char commandResult[1024] = {0};
45 while ((fgets(commandResult, sizeof(commandResult), file)) != nullptr) {
46 result.append(commandResult);
47 }
48 pclose(file);
49 file = nullptr;
50 }
51
52 return result;
53 }
54 } // namespace
55
56 class CemCommandDumpSystemTest : public ::testing::Test {
57 public:
58 static void SetUpTestCase();
59 static void TearDownTestCase();
60 void SetUp() override;
61 void TearDown() override;
62 };
63
SetUpTestCase()64 void CemCommandDumpSystemTest::SetUpTestCase()
65 {}
66
TearDownTestCase()67 void CemCommandDumpSystemTest::TearDownTestCase()
68 {}
69
SetUp()70 void CemCommandDumpSystemTest::SetUp()
71 {
72 // reset optind to 0
73 optind = 0;
74 }
75
TearDown()76 void CemCommandDumpSystemTest::TearDown()
77 {}
78
79 class CommonEventSubscriberTest : public CommonEventSubscriber {
80 public:
CommonEventSubscriberTest(const CommonEventSubscribeInfo & subscribeInfo)81 explicit CommonEventSubscriberTest(const CommonEventSubscribeInfo &subscribeInfo)
82 : CommonEventSubscriber(subscribeInfo)
83 {}
84
~CommonEventSubscriberTest()85 ~CommonEventSubscriberTest()
86 {}
87
OnReceiveEvent(const CommonEventData & data)88 void OnReceiveEvent(const CommonEventData &data)
89 {}
90 };
91
92 /**
93 * @tc.number: Cem_Command_Dump_SystemTest_0100
94 * @tc.name: ExecCommand
95 * @tc.desc: Verify the "cem dump -a" command with a subscriber.
96 */
97 HWTEST_F(CemCommandDumpSystemTest, Cem_Command_Dump_SystemTest_0100, Function | MediumTest | Level1)
98 {
99 /* Subscribe */
100
101 // make matching skills
102 MatchingSkills matchingSkills;
103 matchingSkills.AddEvent(STRING_EVENT);
104
105 // make subscriber info
106 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
107
108 // make a subscriber object
109 auto subscriberTestPtr = std::make_shared<CommonEventSubscriberTest>(subscribeInfo);
110 // subscribe a common event
111 CommonEventManager::SubscribeCommonEvent(subscriberTestPtr);
112
113 // dump all subscribers
114 std::string command = "cem dump -a";
115 std::string commandResult = ExecuteCommand(command);
116
117 EXPECT_NE(commandResult, "");
118
119 // unsubscribe a common event
120 CommonEventManager::UnSubscribeCommonEvent(subscriberTestPtr);
121 }
122
123 /**
124 * @tc.number: Cem_Command_Dump_SystemTest_0200
125 * @tc.name: ExecCommand
126 * @tc.desc: Verify the "cem dump -e <name>" command with no subscriber.
127 */
128 HWTEST_F(CemCommandDumpSystemTest, Cem_Command_Dump_SystemTest_0200, Function | MediumTest | Level1)
129 {
130 // dump all subscribers for an event
131 std::string command = "cem dump -e " + STRING_EVENT + ".test";
132 std::string commandResult = ExecuteCommand(command);
133
134 EXPECT_EQ(commandResult, STRING_NO_SUBSCRIBERS);
135 }
136
137 /**
138 * @tc.number: Cem_Command_Dump_SystemTest_0300
139 * @tc.name: ExecCommand
140 * @tc.desc: Verify the "cem dump -e <name>" command with a subscriber.
141 */
142 HWTEST_F(CemCommandDumpSystemTest, Cem_Command_Dump_SystemTest_0300, Function | MediumTest | Level1)
143 {
144 /* Subscribe */
145
146 // make matching skills
147 MatchingSkills matchingSkills;
148 matchingSkills.AddEvent(STRING_EVENT);
149
150 // make subscriber info
151 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
152
153 // make a subscriber object
154 auto subscriberTestPtr = std::make_shared<CommonEventSubscriberTest>(subscribeInfo);
155 // subscribe a common event
156 CommonEventManager::SubscribeCommonEvent(subscriberTestPtr);
157
158 // dump all subscribers for an event
159 std::string command = "cem dump -e " + STRING_EVENT;
160 std::string commandResult = ExecuteCommand(command);
161
162 EXPECT_NE(commandResult, "");
163
164 // unsubscribe a common event
165 CommonEventManager::UnSubscribeCommonEvent(subscriberTestPtr);
166 }
167