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 #include <fcntl.h> 18 #include <fstream> 19 #include "image_utils.h" 20 #include "image_trace.h" 21 #include "source_stream.h" 22 #include "istream_source_stream.h" 23 24 using namespace testing::ext; 25 using namespace OHOS::Media; 26 using namespace OHOS::MultimediaPlugin; 27 namespace OHOS { 28 namespace Multimedia { 29 static const std::string IMAGE_INPUT_JPEG_PATH = "/data/local/tmp/image/test.jpg"; 30 static constexpr int32_t LENGTH = 8; 31 constexpr int32_t RGB_888_PIXEL_BYTES = 3; 32 constexpr int32_t RGBA_F16_PIXEL_BYTES = 8; 33 constexpr int32_t NV12_PIXEL_BYTES = 2; 34 class ImageUtilsTest : public testing::Test { 35 public: ImageUtilsTest()36 ImageUtilsTest() {} ~ImageUtilsTest()37 ~ImageUtilsTest() {} 38 }; 39 40 /** 41 * @tc.name: ImageTraceTest001 42 * @tc.desc: test SetData and ClearData data type is bool 43 * @tc.type: FUNC 44 */ 45 HWTEST_F(ImageUtilsTest, ImageTraceTest001, TestSize.Level3) 46 { 47 GTEST_LOG_(INFO) << "ImageUtilsTest: ImageTraceTest001 start"; 48 const std::string title = "title"; 49 ImageTrace imagetrace(title); 50 ASSERT_NE(&imagetrace, nullptr); 51 GTEST_LOG_(INFO) << "ImageUtilsTest: ImageTraceTest001 end"; 52 } 53 54 /** 55 * @tc.name: ImageTraceTest002 56 * @tc.desc: test SetData and ClearData data type is bool 57 * @tc.type: FUNC 58 */ 59 HWTEST_F(ImageUtilsTest, ImageTraceTest002, TestSize.Level3) 60 { 61 GTEST_LOG_(INFO) << "ImageUtilsTest: ImageTraceTest002 start"; 62 const char *fmt = nullptr; 63 ImageTrace imagetrace(fmt); 64 ASSERT_NE(&imagetrace, nullptr); 65 GTEST_LOG_(INFO) << "ImageUtilsTest: ImageTraceTest002 end"; 66 } 67 68 /** 69 * @tc.name: ImageTraceTest003 70 * @tc.desc: test SetData and ClearData data type is bool 71 * @tc.type: FUNC 72 */ 73 HWTEST_F(ImageUtilsTest, ImageTraceTest003, TestSize.Level3) 74 { 75 GTEST_LOG_(INFO) << "ImageUtilsTest: ImageTraceTest003 start"; 76 const char *fmt = "mytrace"; 77 ImageTrace imagetrace(fmt); 78 ASSERT_NE(&imagetrace, nullptr); 79 GTEST_LOG_(INFO) << "ImageUtilsTest: ImageTraceTest003 end"; 80 } 81 82 /** 83 * @tc.name: GetFileSize001 84 * @tc.desc: ImageUtils::GetFileSize(const string &pathName, size_t &size) 85 * @tc.type: FUNC 86 */ 87 HWTEST_F(ImageUtilsTest, GetFileSize001, TestSize.Level3) 88 { 89 GTEST_LOG_(INFO) << "ImageUtilsTest: GetFileSize001 start"; 90 ImageUtils imageUtils; 91 size_t size; 92 bool res = imageUtils.GetFileSize(IMAGE_INPUT_JPEG_PATH, size); 93 ASSERT_EQ(res, true); 94 GTEST_LOG_(INFO) << "ImageUtilsTest: GetFileSize001 end"; 95 } 96 97 /** 98 * @tc.name: GetFileSize002 99 * @tc.desc: ImageUtils::GetFileSize(const string &pathName, size_t &size) 100 * @tc.type: FUNC 101 */ 102 HWTEST_F(ImageUtilsTest, GetFileSize002, TestSize.Level3) 103 { 104 GTEST_LOG_(INFO) << "ImageUtilsTest: GetFileSize002 start"; 105 ImageUtils imageUtils; 106 size_t size; 107 const std::string path = ""; 108 bool res = imageUtils.GetFileSize(path, size); 109 ASSERT_EQ(res, false); 110 GTEST_LOG_(INFO) << "ImageUtilsTest: GetFileSize002 end"; 111 } 112 113 /** 114 * @tc.name: GetFileSize003 115 * @tc.desc: ImageUtils::GetFileSize(const string &pathName, size_t &size) 116 * @tc.type: FUNC 117 */ 118 HWTEST_F(ImageUtilsTest, GetFileSize003, TestSize.Level3) 119 { 120 GTEST_LOG_(INFO) << "ImageUtilsTest: GetFileSize003 start"; 121 ImageUtils imageUtils; 122 size_t size; 123 const std::string path = "test/aaa"; 124 bool res = imageUtils.GetFileSize(path, size); 125 ASSERT_EQ(res, false); 126 GTEST_LOG_(INFO) << "ImageUtilsTest: GetFileSize003 end"; 127 } 128 129 /** 130 * @tc.name: GetFileSize004 131 * @tc.desc: bool ImageUtils::GetFileSize(const int fd, size_t &size) 132 * @tc.type: FUNC 133 */ 134 HWTEST_F(ImageUtilsTest, GetFileSize004, TestSize.Level3) 135 { 136 GTEST_LOG_(INFO) << "ImageUtilsTest: GetFileSize004 start"; 137 const int fd = open("/data/local/tmp/image/test.jpg", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); 138 ImageUtils imageUtils; 139 size_t size; 140 bool res = imageUtils.GetFileSize(fd, size); 141 ASSERT_EQ(res, true); 142 GTEST_LOG_(INFO) << "ImageUtilsTest: GetFileSize004 end"; 143 } 144 145 /** 146 * @tc.name: GetFileSize005 147 * @tc.desc: bool ImageUtils::GetFileSize(const int fd, size_t &size) 148 * @tc.type: FUNC 149 */ 150 HWTEST_F(ImageUtilsTest, GetFileSize005, TestSize.Level3) 151 { 152 GTEST_LOG_(INFO) << "ImageUtilsTest: GetFileSize005 start"; 153 const int fd = -1; 154 ImageUtils imageUtils; 155 size_t size; 156 bool res = imageUtils.GetFileSize(fd, size); 157 ASSERT_EQ(res, false); 158 GTEST_LOG_(INFO) << "ImageUtilsTest: GetFileSize005 end"; 159 } 160 161 /** 162 * @tc.name: GetFileSize006 163 * @tc.desc: bool ImageUtils::GetFileSize(const int fd, size_t &size) 164 * @tc.type: FUNC 165 */ 166 HWTEST_F(ImageUtilsTest, GetFileSize006, TestSize.Level3) 167 { 168 GTEST_LOG_(INFO) << "ImageUtilsTest: GetFileSize006 start"; 169 const int fd = 100; 170 ImageUtils imageUtils; 171 size_t size; 172 bool res = imageUtils.GetFileSize(fd, size); 173 ASSERT_EQ(res, false); 174 GTEST_LOG_(INFO) << "ImageUtilsTest: GetFileSize006 end"; 175 } 176 177 /** 178 * @tc.name: GetPixelBytes001 179 * @tc.desc: bool ImageUtils::GetInputStreamSize(istream &inputStream, size_t &size) 180 * @tc.type: FUNC 181 */ 182 HWTEST_F(ImageUtilsTest, GetPixelBytes001, TestSize.Level3) 183 { 184 GTEST_LOG_(INFO) << "ImageUtilsTest: GetPixelBytes001 start"; 185 int32_t res1 = ImageUtils::GetPixelBytes(PixelFormat::RGB_888); 186 ASSERT_EQ(res1, RGB_888_PIXEL_BYTES); 187 int32_t res2 = ImageUtils::GetPixelBytes(PixelFormat::RGBA_F16); 188 ASSERT_EQ(res2, RGBA_F16_PIXEL_BYTES); 189 int32_t res3 = ImageUtils::GetPixelBytes(PixelFormat::NV12); 190 ASSERT_EQ(res3, NV12_PIXEL_BYTES); 191 GTEST_LOG_(INFO) << "ImageUtilsTest: GetPixelBytes001 end"; 192 } 193 194 /** 195 * @tc.name: PathToRealPath001 196 * @tc.desc: PathToRealPath 197 * @tc.type: FUNC 198 */ 199 HWTEST_F(ImageUtilsTest, PathToRealPath001, TestSize.Level3) 200 { 201 GTEST_LOG_(INFO) << "ImageUtilsTest: PathToRealPath001 start"; 202 const string path = ""; 203 string realPath; 204 bool res = ImageUtils::PathToRealPath(path, realPath); 205 ASSERT_EQ(res, false); 206 GTEST_LOG_(INFO) << "ImageUtilsTest: PathToRealPath001 end"; 207 } 208 209 /** 210 * @tc.name: PathToRealPath002 211 * @tc.desc: PathToRealPath 212 * @tc.type: FUNC 213 */ 214 HWTEST_F(ImageUtilsTest, PathToRealPath002, TestSize.Level3) 215 { 216 GTEST_LOG_(INFO) << "ImageUtilsTest: PathToRealPath002 start"; 217 char buffer[PATH_MAX+1] = {'\0'}; 218 for (int i = 0; i <= PATH_MAX; i++) { 219 buffer[i] = i; 220 } 221 const string path = buffer; 222 string realPath; 223 bool res = ImageUtils::PathToRealPath(path, realPath); 224 ASSERT_EQ(res, false); 225 GTEST_LOG_(INFO) << "ImageUtilsTest: PathToRealPath002 end"; 226 } 227 228 /** 229 * @tc.name: IsValidImageInfo001 230 * @tc.desc: IsValidImageInfo 231 * @tc.type: FUNC 232 */ 233 HWTEST_F(ImageUtilsTest, IsValidImageInfo001, TestSize.Level3) 234 { 235 GTEST_LOG_(INFO) << "ImageUtilsTest: IsValidImageInfo001 start"; 236 ImageInfo info; 237 bool res1 = ImageUtils::IsValidImageInfo(info); 238 ASSERT_EQ(res1, false); 239 info.size.width = 0; 240 info.size.height = 0; 241 bool res2 = ImageUtils::IsValidImageInfo(info); 242 ASSERT_EQ(res2, false); 243 info.size.width = 100; 244 info.size.height = 10; 245 bool res3 = ImageUtils::IsValidImageInfo(info); 246 ASSERT_EQ(res3, false); 247 GTEST_LOG_(INFO) << "ImageUtilsTest: IsValidImageInfo001 end"; 248 } 249 250 /** 251 * @tc.name: CheckMulOverflow001 252 * @tc.desc: CheckMulOverflow 253 * @tc.type: FUNC 254 */ 255 HWTEST_F(ImageUtilsTest, CheckMulOverflow001, TestSize.Level3) 256 { 257 GTEST_LOG_(INFO) << "ImageUtilsTest: CheckMulOverflow001 start"; 258 int32_t width = 0; 259 int32_t height = 0; 260 int32_t bytesPerPixel = 0; 261 bool res = ImageUtils::CheckMulOverflow(width, height, bytesPerPixel); 262 ASSERT_EQ(res, true); 263 GTEST_LOG_(INFO) << "ImageUtilsTest: CheckMulOverflow001 end"; 264 } 265 266 /** 267 * @tc.name: BGRAToARGB001 268 * @tc.desc: BGRAToARGB 269 * @tc.type: FUNC 270 */ 271 HWTEST_F(ImageUtilsTest, BGRAToARGB001, TestSize.Level3) 272 { 273 GTEST_LOG_(INFO) << "ImageUtilsTest: BGRAToARGB001 start"; 274 uint8_t src[LENGTH] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}; 275 uint8_t dst[LENGTH] = {0}; 276 ImageUtils::BGRAToARGB(src, dst, LENGTH); 277 for (int i = 0; i < LENGTH; i++) { 278 GTEST_LOG_(INFO) << "BGRAToARGB" << "i:" << i << " " \ 279 << static_cast<int>(src[i]) << "," << static_cast<int>(dst[i]); 280 } 281 282 uint8_t src2[LENGTH] = {0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00}; 283 uint8_t dst2[LENGTH] = {0}; 284 ImageUtils::ARGBToBGRA(src2, dst2, LENGTH); 285 for (int i = 0; i < LENGTH; i++) { 286 GTEST_LOG_(INFO) << "ARGBToBGRA" << "i:" << i << " " \ 287 << static_cast<int>(src2[i]) << "," << static_cast<int>(dst2[i]); 288 } 289 EXPECT_NE(dst2, nullptr); 290 uint8_t src3[] = {0x01, 0x02}; 291 ImageUtils::ARGBToBGRA(src3, dst2, LENGTH); 292 ImageUtils::BGRAToARGB(src3, dst, LENGTH); 293 GTEST_LOG_(INFO) << "ImageUtilsTest: BGRAToARGB001 end"; 294 } 295 296 /** 297 * @tc.name: SurfaceBuffer_Reference001 298 * @tc.desc: SurfaceBuffer_Reference 299 * @tc.type: FUNC 300 */ 301 HWTEST_F(ImageUtilsTest, SurfaceBuffer_Reference001, TestSize.Level3) 302 { 303 GTEST_LOG_(INFO) << "ImageUtilsTest: SurfaceBuffer_Reference001 start"; 304 void* buffer = nullptr; 305 int32_t res = ImageUtils::SurfaceBuffer_Reference(buffer); 306 ASSERT_NE(res, SUCCESS); 307 308 int32_t ret = ImageUtils::SurfaceBuffer_Unreference(buffer); 309 ASSERT_NE(ret, SUCCESS); 310 311 GTEST_LOG_(INFO) << "ImageUtilsTest: SurfaceBuffer_Reference001 end"; 312 } 313 } 314 }