• 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 #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 MakeMockObjects();
43     void SetMockObjects(const CommonEventCommand &cmd) const;
44 
45     std::string cmd_ = "dump";
46     std::string toolName_ = TOOL_NAME;
47     sptr<ICommonEvent> proxyPtr_;
48 };
49 
SetUpTestCase()50 void CemCommandDumpModuleTest::SetUpTestCase()
51 {}
52 
TearDownTestCase()53 void CemCommandDumpModuleTest::TearDownTestCase()
54 {}
55 
SetUp()56 void CemCommandDumpModuleTest::SetUp()
57 {
58     // reset optind to 0
59     optind = 0;
60 
61     // make mock objects
62     MakeMockObjects();
63 }
64 
TearDown()65 void CemCommandDumpModuleTest::TearDown()
66 {}
67 
MakeMockObjects()68 void CemCommandDumpModuleTest::MakeMockObjects()
69 {
70     // mock a stub
71     auto stubPtr = sptr<IRemoteObject>(new MockCommonEventStub());
72 
73     // mock a proxy
74     proxyPtr_ = iface_cast<ICommonEvent>(stubPtr);
75 
76     // set the mock proxy
77     auto commonEventPtr = DelayedSingleton<CommonEvent>::GetInstance();
78     commonEventPtr->isProxyValid_ = true;
79     commonEventPtr->commonEventProxy_ = proxyPtr_;
80 }
81 
SetMockObjects(const CommonEventCommand & cmd) const82 void CemCommandDumpModuleTest::SetMockObjects(const CommonEventCommand &cmd) const
83 {}
84 
85 class CommonEventSubscriberTest : public CommonEventSubscriber {
86 public:
CommonEventSubscriberTest(const CommonEventSubscribeInfo & subscribeInfo)87     explicit CommonEventSubscriberTest(const CommonEventSubscribeInfo &subscribeInfo)
88         : CommonEventSubscriber(subscribeInfo)
89     {}
90 
~CommonEventSubscriberTest()91     ~CommonEventSubscriberTest()
92     {}
93 
OnReceiveEvent(const CommonEventData & data)94     void OnReceiveEvent(const CommonEventData &data)
95     {}
96 };
97 
98 /**
99  * @tc.number: Cem_Command_Dump_ModuleTest_0100
100  * @tc.name: ExecCommand
101  * @tc.desc: Verify the "cem dump -e <name>" command.
102  */
103 HWTEST_F(CemCommandDumpModuleTest, Cem_Command_Dump_ModuleTest_0100, Function | MediumTest | Level1)
104 {
105     char *argv[] = {
106         (char *)toolName_.c_str(),
107         (char *)cmd_.c_str(),
108         (char *)"-e",
109         (char *)STRING_EVENT.c_str(),
110         (char *)"",
111     };
112     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
113 
114     CommonEventCommand cmd(argc, argv);
115 
116     // set the mock objects
117     SetMockObjects(cmd);
118 
119     EXPECT_EQ(cmd.ExecCommand(), "");
120 }
121 
122 /**
123  * @tc.number: Cem_Command_Dump_ModuleTest_0200
124  * @tc.name: ExecCommand
125  * @tc.desc: Verify the "cem dump -e <name>" command with a subscriber.
126  */
127 HWTEST_F(CemCommandDumpModuleTest, Cem_Command_Dump_ModuleTest_0200, Function | MediumTest | Level1)
128 {
129     /* Subscribe */
130 
131     // make matching skills
132     MatchingSkills matchingSkills;
133     matchingSkills.AddEvent(STRING_EVENT);
134 
135     // make subscriber info
136     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
137 
138     // make a subscriber object
139     auto subscriberTestPtr = std::make_shared<CommonEventSubscriberTest>(subscribeInfo);
140     // subscribe a common event
141     CommonEventManager::SubscribeCommonEvent(subscriberTestPtr);
142 
143     char *argv[] = {
144         (char *)toolName_.c_str(),
145         (char *)cmd_.c_str(),
146         (char *)"-e",
147         (char *)STRING_EVENT.c_str(),
148         (char *)"",
149     };
150     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
151 
152     CommonEventCommand cmd(argc, argv);
153 
154     // set the mock objects
155     SetMockObjects(cmd);
156 
157     EXPECT_EQ(cmd.ExecCommand(), STRING_EVENT + "\n");
158 }
159