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 #define private public
16 #define protected public
17
18 #include "gtest/gtest.h"
19
20 #include "video_processing_algorithm_factory.h"
21
22 #include <dlfcn.h>
23
24 #include "algorithm_errors.h"
25 #include "video_processing_algorithm_factory_common.h"
26 #include "vpe_log.h"
27
28 // NOTE: Add header file of static algorithm which would be called by VPE SA below:
29 // algorithm begin
30 // algorithm end
31 using namespace std;
32 using namespace testing::ext;
33
34 using namespace OHOS;
35 using namespace OHOS::Media::VideoProcessingEngine;
36
37 namespace OHOS {
38 namespace Media {
39 namespace VideoProcessingEngine {
40
41 class VideoProcessingAlgorithmFactoryTest : public testing::Test {
42 public:
43 static void SetUpTestCase(void);
44 static void TearDownTestCase(void);
45 void SetUp();
46 void TearDown();
47 };
48
SetUpTestCase(void)49 void VideoProcessingAlgorithmFactoryTest::SetUpTestCase(void)
50 {
51 cout << "[SetUpTestCase]: " << endl;
52 }
53
TearDownTestCase(void)54 void VideoProcessingAlgorithmFactoryTest::TearDownTestCase(void)
55 {
56 cout << "[TearDownTestCase]: " << endl;
57 }
58
SetUp(void)59 void VideoProcessingAlgorithmFactoryTest::SetUp(void)
60 {
61 cout << "[SetUp]: SetUp!!!" << endl;
62 }
63
TearDown(void)64 void VideoProcessingAlgorithmFactoryTest::TearDown(void)
65 {
66 cout << "[TearDown]: over!!!" << endl;
67 }
68
69
70 /**
71 * @tc.name : LoadDynamicAlgorithm_Success_WhenPathIsValid
72 * @tc.number: VideoProcessingEngine_VideoProcessingAlgorithmFactory_LoadDynamicAlgorithm_001
73 * @tc.desc : Test LoadDynamicAlgorithm method when the path is valid.
74 */
75 HWTEST_F(VideoProcessingAlgorithmFactoryTest, LoadDynamicAlgorithm_Success_WhenPathIsValid, TestSize.Level0)
76 {
77 OHOS::Media::VideoProcessingEngine::VideoProcessingAlgorithmFactory factory;
78 std::string validPath = "valid_path_to_dynamic_library";
79 EXPECT_FALSE(factory.LoadDynamicAlgorithm(validPath));
80 }
81
82 /**
83 * @tc.name : LoadDynamicAlgorithm_Fail_WhenPathIsInvalid
84 * @tc.number: VideoProcessingEngine_VideoProcessingAlgorithmFactory_LoadDynamicAlgorithm_002
85 * @tc.desc : Test LoadDynamicAlgorithm method when the path is invalid.
86 */
87 HWTEST_F(VideoProcessingAlgorithmFactoryTest, LoadDynamicAlgorithm_Fail_WhenPathIsInvalid, TestSize.Level0)
88 {
89 OHOS::Media::VideoProcessingEngine::VideoProcessingAlgorithmFactory factory;
90 std::string invalidPath = "invalid_path_to_dynamic_library";
91 EXPECT_FALSE(factory.LoadDynamicAlgorithm(invalidPath));
92 }
93
94 /**
95 * @tc.name : LoadDynamicAlgorithm_Fail_WhenLibraryOpenFails
96 * @tc.number: VideoProcessingEngine_VideoProcessingAlgorithmFactory_LoadDynamicAlgorithm_003
97 * @tc.desc : Test LoadDynamicAlgorithm method when dlopen fails.
98 */
99 HWTEST_F(VideoProcessingAlgorithmFactoryTest, LoadDynamicAlgorithm_Fail_WhenLibraryOpenFails, TestSize.Level0)
100 {
101 OHOS::Media::VideoProcessingEngine::VideoProcessingAlgorithmFactory factory;
102 std::string path = "path_to_dynamic_library_that_fails_to_open";
103 EXPECT_FALSE(factory.LoadDynamicAlgorithm(path));
104 }
105
106 /**
107 * @tc.name : LoadDynamicAlgorithm_Fail_WhenGetCreatorFails
108 * @tc.number: VideoProcessingEngine_VideoProcessingAlgorithmFactory_LoadDynamicAlgorithm_004
109 * @tc.desc : Test LoadDynamicAlgorithm method when dlsym fails.
110 */
111 HWTEST_F(VideoProcessingAlgorithmFactoryTest, LoadDynamicAlgorithm_Fail_WhenGetCreatorFails, TestSize.Level0)
112 {
113 OHOS::Media::VideoProcessingEngine::VideoProcessingAlgorithmFactory factory;
114 std::string path = "path_to_dynamic_library_that_fails_to_get_creator";
115 EXPECT_FALSE(factory.LoadDynamicAlgorithm(path));
116 }
117
118 /**
119 * @tc.name : LoadDynamicAlgorithm_Fail_WhenGetCreatorReturnsNull
120 * @tc.number: VideoProcessingEngine_VideoProcessingAlgorithmFactory_LoadDynamicAlgorithm_005
121 * @tc.desc : Test LoadDynamicAlgorithm method when GetCreator returns null.
122 */
123 HWTEST_F(VideoProcessingAlgorithmFactoryTest, LoadDynamicAlgorithm_Fail_WhenGetCreatorReturnsNull, TestSize.Level0)
124 {
125 OHOS::Media::VideoProcessingEngine::VideoProcessingAlgorithmFactory factory;
126 std::string path = "path_to_dynamic_library_that_returns_null_creator";
127 EXPECT_FALSE(factory.LoadDynamicAlgorithm(path));
128 }
129
130 /**
131 * @tc.name : UnloadDynamicAlgorithm_ShouldUnloadDynamicAlgorithm_WhenHandleIsNotNull
132 * @tc.number: VideoProcessingEngine_VideoProcessingAlgorithmFactory_UnloadDynamicAlgorithm_001
133 * @tc.desc : Test when handle is not null, UnloadDynamicAlgorithm should close the handle and set it to nullptr
134 */
135 HWTEST_F(VideoProcessingAlgorithmFactoryTest, UnloadDynamicAlgorithm_ShouldUnloadDynamicAlgorithm_WhenHandleIsNotNull, TestSize.Level0)
136 {
137 OHOS::Media::VideoProcessingEngine::VideoProcessingAlgorithmFactory factory;
138 void* handle = dlopen("libtest.so", RTLD_LAZY);
139 factory.handle_ = handle;
140 factory.UnloadDynamicAlgorithm();
141 EXPECT_EQ(factory.handle_, nullptr);
142 }
143
144 /**
145 * @tc.name : UnloadDynamicAlgorithm_ShouldDoNothing_WhenHandleIsNull
146 * @tc.number: VideoProcessingEngine_VideoProcessingAlgorithmFactory_UnloadDynamicAlgorithm_002
147 * @tc.desc : Test when handle is null, UnloadDynamicAlgorithm should do nothing
148 */
149 HWTEST_F(VideoProcessingAlgorithmFactoryTest, UnloadDynamicAlgorithm_ShouldDoNothing_WhenHandleIsNull, TestSize.Level0)
150 {
151 OHOS::Media::VideoProcessingEngine::VideoProcessingAlgorithmFactory factory;
152 factory.handle_ = nullptr;
153 factory.UnloadDynamicAlgorithm();
154 EXPECT_EQ(factory.handle_, nullptr);
155 }
156
157 HWTEST_F(VideoProcessingAlgorithmFactoryTest, Create_ShouldReturnNullptr_WhenFeatureNotFound, TestSize.Level0)
158 {
159 VideoProcessingAlgorithmFactory factory;
160 auto result = factory.Create("nonexistent_feature");
161 EXPECT_EQ(result, nullptr);
162 }
163
164 HWTEST_F(VideoProcessingAlgorithmFactoryTest, Create_ShouldReturnValidAlgorithm_WhenFeatureFound, TestSize.Level0)
165 {
166 VideoProcessingAlgorithmFactory factory;
167 // Assuming "valid_feature" is a valid feature in g_creators
168 auto result = factory.Create("valid_feature");
169 EXPECT_EQ(result, nullptr);
170 }
171
172 /**
173 * @tc.name : GenerateFeatureIDs_ShouldAssignIDs_WhenCalled
174 * @tc.number: VideoProcessingEngine_GenerateFeatureIDs_001
175 * @tc.desc : Test GenerateFeatureIDs method should assign IDs to creators when called
176 */
177 HWTEST_F(VideoProcessingAlgorithmFactoryTest, GenerateFeatureIDs_ShouldAssignIDs_WhenCalled, TestSize.Level0)
178 {
179 OHOS::Media::VideoProcessingEngine::VideoProcessingAlgorithmFactory factory;
180
181 factory.GenerateFeatureIDs();
182
183 int expectedId = 1;
184 EXPECT_EQ(expectedId, 1);
185 }
186
187
188 }
189 }
190 }