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 #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 CemCommandPublishModuleTest : 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_ = "publish";
46 std::string toolName_ = TOOL_NAME;
47 sptr<ICommonEvent> proxyPtr_;
48 };
49
SetUpTestCase()50 void CemCommandPublishModuleTest::SetUpTestCase()
51 {}
52
TearDownTestCase()53 void CemCommandPublishModuleTest::TearDownTestCase()
54 {}
55
SetUp()56 void CemCommandPublishModuleTest::SetUp()
57 {
58 // reset optind to 0
59 optind = 0;
60
61 // make mock objects
62 MakeMockObjects();
63 }
64
TearDown()65 void CemCommandPublishModuleTest::TearDown()
66 {}
67
MakeMockObjects()68 void CemCommandPublishModuleTest::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 CemCommandPublishModuleTest::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_Publish_ModuleTest_0100
100 * @tc.name: ExecCommand
101 * @tc.desc: Verify the "cem publish -e <name>" command.
102 */
103 HWTEST_F(CemCommandPublishModuleTest, Cem_Command_Publish_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 EXPECT_EQ(cmd.ExecCommand(), STRING_PUBLISH_COMMON_EVENT_OK);
116 }
117