Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2022 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <jpegrecoverymap/jpegencoder.h> |
| 18 | #include <gtest/gtest.h> |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include <fcntl.h> |
| 22 | |
| 23 | namespace android::recoverymap { |
| 24 | |
| 25 | #define VALID_IMAGE "/sdcard/Documents/minnie-320x240.yu12" |
| 26 | #define VALID_IMAGE_WIDTH 320 |
| 27 | #define VALID_IMAGE_HEIGHT 240 |
| 28 | #define INVALID_SIZE_IMAGE "/sdcard/Documents/minnie-318x240.yu12" |
| 29 | #define INVALID_SIZE_IMAGE_WIDTH 318 |
| 30 | #define INVALID_SIZE_IMAGE_HEIGHT 240 |
| 31 | #define JPEG_QUALITY 90 |
| 32 | |
| 33 | class JpegEncoderTest : public testing::Test { |
| 34 | public: |
| 35 | struct Image { |
| 36 | std::unique_ptr<uint8_t[]> buffer; |
| 37 | size_t width; |
| 38 | size_t height; |
| 39 | }; |
| 40 | JpegEncoderTest(); |
| 41 | ~JpegEncoderTest(); |
| 42 | protected: |
| 43 | virtual void SetUp(); |
| 44 | virtual void TearDown(); |
| 45 | |
| 46 | Image mValidImage, mInvalidSizeImage; |
| 47 | }; |
| 48 | |
| 49 | JpegEncoderTest::JpegEncoderTest() {} |
| 50 | |
| 51 | JpegEncoderTest::~JpegEncoderTest() {} |
| 52 | |
| 53 | static size_t getFileSize(int fd) { |
| 54 | struct stat st; |
| 55 | if (fstat(fd, &st) < 0) { |
| 56 | ALOGW("%s : fstat failed", __func__); |
| 57 | return 0; |
| 58 | } |
| 59 | return st.st_size; // bytes |
| 60 | } |
| 61 | |
| 62 | static bool loadFile(const char filename[], JpegEncoderTest::Image* result) { |
| 63 | int fd = open(filename, O_CLOEXEC); |
| 64 | if (fd < 0) { |
| 65 | return false; |
| 66 | } |
| 67 | int length = getFileSize(fd); |
| 68 | if (length == 0) { |
| 69 | close(fd); |
| 70 | return false; |
| 71 | } |
| 72 | result->buffer.reset(new uint8_t[length]); |
| 73 | if (read(fd, result->buffer.get(), length) != static_cast<ssize_t>(length)) { |
| 74 | close(fd); |
| 75 | return false; |
| 76 | } |
| 77 | close(fd); |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | void JpegEncoderTest::SetUp() { |
| 82 | if (!loadFile(VALID_IMAGE, &mValidImage)) { |
| 83 | FAIL() << "Load file " << VALID_IMAGE << " failed"; |
| 84 | } |
| 85 | mValidImage.width = VALID_IMAGE_WIDTH; |
| 86 | mValidImage.height = VALID_IMAGE_HEIGHT; |
| 87 | if (!loadFile(INVALID_SIZE_IMAGE, &mInvalidSizeImage)) { |
| 88 | FAIL() << "Load file " << INVALID_SIZE_IMAGE << " failed"; |
| 89 | } |
| 90 | mInvalidSizeImage.width = INVALID_SIZE_IMAGE_WIDTH; |
| 91 | mInvalidSizeImage.height = INVALID_SIZE_IMAGE_HEIGHT; |
| 92 | } |
| 93 | |
| 94 | void JpegEncoderTest::TearDown() {} |
| 95 | |
| 96 | TEST_F(JpegEncoderTest, validImage) { |
| 97 | JpegEncoder encoder; |
| 98 | EXPECT_TRUE(encoder.compressImage(mValidImage.buffer.get(), mValidImage.width, |
| 99 | mValidImage.height, JPEG_QUALITY, NULL, 0)); |
| 100 | ASSERT_GT(encoder.getCompressedImageSize(), static_cast<uint32_t>(0)); |
| 101 | } |
| 102 | |
| 103 | TEST_F(JpegEncoderTest, invalidSizeImage) { |
| 104 | JpegEncoder encoder; |
| 105 | EXPECT_FALSE(encoder.compressImage(mInvalidSizeImage.buffer.get(), mInvalidSizeImage.width, |
| 106 | mInvalidSizeImage.height, JPEG_QUALITY, NULL, 0)); |
| 107 | } |
| 108 | |
| 109 | } |
| 110 | |