1/*
2 * Copyright (c) 2023 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#include "plugin_test.h"
16
17#include "plugin.h"
18#include "pipeline.h"
19
20namespace OHOS {
21namespace HiviewDFX {
22void PluginTest::SetUpTestCase()
23{
24}
25
26void PluginTest::TearDownTestCase()
27{
28}
29
30void PluginTest::SetUp()
31{
32}
33
34void PluginTest::TearDown()
35{
36}
37
38/**
39 * @tc.name: PluginTest001
40 * @tc.desc: Test the api of Plugin.
41 * @tc.type: FUNC
42 * @tc.require:
43 */
44HWTEST_F(PluginTest, PluginTest001, testing::ext::TestSize.Level0)
45{
46    /**
47     * @tc.steps: step1. create Plugin object.
48     * @tc.steps: step2. invoke the function of the plugin object.
49     */
50    printf("PluginTest001 start\n");
51    Plugin plugin;
52    ASSERT_FALSE(plugin.HasLoaded());
53
54    plugin.OnLoad();
55    auto event = plugin.GetEvent(Event::SYS_EVENT);
56    ASSERT_NE(event, nullptr);
57    ASSERT_TRUE(plugin.OnEvent(event));
58
59    /* default null function test */
60    // Dump
61    plugin.Dump(0, {});
62    // OnEventListeningCallback
63    plugin.OnEventListeningCallback(*(event.get()));
64    // OnConfigUpdate
65    std::string localCfgPath = "test/local/config";
66    std::string cloudCfgPath = "test/cloud/config";
67    plugin.OnConfigUpdate(localCfgPath, cloudCfgPath);
68
69    // delay to handle event
70    auto pipelineEvent = std::make_shared<PipelineEvent>(*(event.get()));
71    ASSERT_FALSE(pipelineEvent->hasPending_);
72    auto workLoop = std::make_shared<EventLoop>("testLoop");
73    plugin.BindWorkLoop(workLoop);
74    ASSERT_NE(plugin.GetWorkLoop(), nullptr);
75    plugin.DelayProcessEvent(pipelineEvent, 1); // 1s delay
76    ASSERT_TRUE(pipelineEvent->hasPending_);
77    sleep(3);
78
79    // udpate the active time
80    auto lastActiveTime1 = plugin.GetLastActiveTime();
81    ASSERT_GT(lastActiveTime1, 0);
82    plugin.UpdateTimeByDelay(1000); // delay 1s
83    auto lastActiveTime2 = plugin.GetLastActiveTime();
84    ASSERT_GT(lastActiveTime2, lastActiveTime1);
85
86    // set/get function
87    std::string version = "1.0";
88    plugin.SetVersion(version);
89    ASSERT_EQ(version, plugin.GetVersion());
90
91    // bundle test
92    ASSERT_FALSE(plugin.IsBundlePlugin());
93    std::string bundleName = "testBundle";
94    plugin.SetBundleName(bundleName);
95    ASSERT_TRUE(plugin.IsBundlePlugin());
96
97    ASSERT_EQ(plugin.GetUseCount(), 0);
98    plugin.AddUseCount();
99    ASSERT_EQ(plugin.GetUseCount(), 1);
100    plugin.SubUseCount();
101    ASSERT_EQ(plugin.GetUseCount(), 0);
102
103    plugin.OnUnload();
104    printf("PluginTest001 end\n");
105}
106
107/**
108 * @tc.name: HiviewContextTest001
109 * @tc.desc: Test the api of HiviewContext.
110 * @tc.type: FUNC
111 * @tc.require:
112 */
113HWTEST_F(PluginTest, HiviewContextTest001, testing::ext::TestSize.Level0)
114{
115    /**
116     * @tc.steps: step1. create HiviewContext object.
117     * @tc.steps: step2. invoke the function of the HiviewContext object.
118     */
119    printf("HiviewContextTest001 start\n");
120    HiviewContext context;
121
122    /* default null function test */
123    // PostUnorderedEvent
124    context.PostUnorderedEvent(nullptr, nullptr);
125    // RegisterUnorderedEventListener
126    std::weak_ptr<EventListener> eventListenerPtr;
127    context.RegisterUnorderedEventListener(eventListenerPtr);
128    // PostAsyncEventToTarget
129    context.PostAsyncEventToTarget(nullptr, "", nullptr);
130    // RequestUnloadPlugin
131    context.RequestUnloadPlugin(nullptr);
132    // AppendPluginToPipeline
133    context.AppendPluginToPipeline("", "");
134    // RequestLoadBundle
135    context.RequestLoadBundle("");
136    // RequestUnloadBundle
137    context.RequestUnloadBundle("");
138    // AddListenerInfo
139    context.AddListenerInfo(0, "", {}, {});
140    context.AddListenerInfo(0, "");
141    // AddDispatchInfo
142    std::weak_ptr<Plugin> pluginPtr;
143    context.AddDispatchInfo(pluginPtr, {}, {}, {}, {});
144
145    /* default null function test with return value */
146    // PostSyncEventToTarget
147    ASSERT_TRUE(context.PostSyncEventToTarget(nullptr, "", nullptr));
148    // GetSharedWorkLoop
149    ASSERT_EQ(context.GetSharedWorkLoop(), nullptr);
150    // GetPipelineSequenceByName
151    ASSERT_TRUE(context.GetPipelineSequenceByName("").empty());
152    // IsReady
153    ASSERT_FALSE(context.IsReady());
154    // GetHiViewDirectory
155    ASSERT_TRUE(context.GetHiViewDirectory(HiviewContext::DirectoryType::CONFIG_DIRECTORY).empty());
156    ASSERT_TRUE(context.GetHiViewDirectory(HiviewContext::DirectoryType::WORK_DIRECTORY).empty());
157    ASSERT_TRUE(context.GetHiViewDirectory(HiviewContext::DirectoryType::PERSIST_DIR).empty());
158    // GetHiviewProperty
159    ASSERT_EQ(context.GetHiviewProperty("test_key", "test_value"), "test_value");
160    // SetHiviewProperty
161    ASSERT_TRUE(context.SetHiviewProperty("test_key", "test_value", true));
162    // InstancePluginByProxy
163    ASSERT_EQ(context.InstancePluginByProxy(nullptr), nullptr);
164    // GetPluginByName
165    ASSERT_EQ(context.GetPluginByName(""), nullptr);
166    // GetListenerInfo
167    ASSERT_TRUE(context.GetListenerInfo(0, "", "").empty());
168    // GetDisPatcherInfo
169    ASSERT_TRUE(context.GetDisPatcherInfo(0, "", "", "").empty());
170
171    printf("HiviewContextTest001 end\n");
172}
173} // namespace HiviewDFX
174} // namespace OHOS
175