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 #include "plugin_factory_test.h"
16 
17 #include <memory>
18 
19 #include "plugin_example.h"
20 #include "plugin_factory.h"
21 
22 using namespace testing::ext;
23 using namespace OHOS::HiviewDFX;
24 /**
25  * @tc.name: PluginFactoryRegisterTest001
26  * @tc.desc: create PluginExample with pre-registed constructor method
27  * @tc.type: FUNC
28  * @tc.require: AR000DPTT6
29  */
30 HWTEST_F(PluginFactoryTest, PluginFactoryRegisterTest001, TestSize.Level3)
31 {
32     /**
33      * @tc.steps: step1. create PluginExample by plugin factory
34      */
35     printf("PluginFactoryTest.\n");
36     auto plugin = PluginFactory::GetGlobalPluginInfo("PluginExample");
37     ASSERT_NE(plugin.get(), nullptr);
38     auto examplePlugin = std::static_pointer_cast<PluginExample>(plugin->getPluginObject());
39     ASSERT_EQ(examplePlugin->isConstructed_, true);
40 }
41 
42 /**
43  * @tc.name: PluginFactoryRegisterTest002
44  * @tc.desc: register plugin dynamically and create instance after registration
45  * @tc.type: FUNC
46  * @tc.require: AR000DPTT6
47  */
48 HWTEST_F(PluginFactoryTest, PluginFactoryRegisterTest002, TestSize.Level3)
49 {
50     /**
51      * @tc.steps: step1. create PluginExample by plugin factory
52      */
53     printf("PluginFactoryTest.\n");
54     auto plugin = PluginFactory::GetGlobalPluginInfo("PluginExample2");
55     ASSERT_EQ(plugin, nullptr);
56     PluginFactory::RegisterPlugin("PluginExample2",
57         std::make_shared<PluginRegistInfo>(RegisterPluginExample2::GetObject, false, false));
58     plugin = PluginFactory::GetGlobalPluginInfo("PluginExample2");
59     ASSERT_NE(plugin, nullptr);
60     auto examplePlugin = std::static_pointer_cast<PluginExample>(plugin->getPluginObject());
61     ASSERT_EQ(examplePlugin->isConstructed_, true);
62     ASSERT_TRUE(PluginFactory::GetPlugin("PluginExample2") != nullptr);
63     PluginFactory::UnregisterPlugin("PluginExample2");
64     ASSERT_TRUE(PluginFactory::GetPlugin("PluginExample2") == nullptr);
65 }
66