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