| 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 | 
| Dichen Zhang | a481914 | 2022-10-21 04:12:30 +0000 | [diff] [blame] | 28 | #define SINGLE_CHANNEL_IMAGE "/sdcard/Documents/minnie-320x240.y" | 
|  | 29 | #define SINGLE_CHANNEL_IMAGE_WIDTH VALID_IMAGE_WIDTH | 
|  | 30 | #define SINGLE_CHANNEL_IMAGE_HEIGHT VALID_IMAGE_HEIGHT | 
| Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 31 | #define INVALID_SIZE_IMAGE "/sdcard/Documents/minnie-318x240.yu12" | 
|  | 32 | #define INVALID_SIZE_IMAGE_WIDTH 318 | 
|  | 33 | #define INVALID_SIZE_IMAGE_HEIGHT 240 | 
|  | 34 | #define JPEG_QUALITY 90 | 
|  | 35 |  | 
|  | 36 | class JpegEncoderTest : public testing::Test { | 
|  | 37 | public: | 
|  | 38 | struct Image { | 
|  | 39 | std::unique_ptr<uint8_t[]> buffer; | 
|  | 40 | size_t width; | 
|  | 41 | size_t height; | 
|  | 42 | }; | 
|  | 43 | JpegEncoderTest(); | 
|  | 44 | ~JpegEncoderTest(); | 
|  | 45 | protected: | 
|  | 46 | virtual void SetUp(); | 
|  | 47 | virtual void TearDown(); | 
|  | 48 |  | 
| Dichen Zhang | a481914 | 2022-10-21 04:12:30 +0000 | [diff] [blame] | 49 | Image mValidImage, mInvalidSizeImage, mSingleChannelImage; | 
| Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 50 | }; | 
|  | 51 |  | 
|  | 52 | JpegEncoderTest::JpegEncoderTest() {} | 
|  | 53 |  | 
|  | 54 | JpegEncoderTest::~JpegEncoderTest() {} | 
|  | 55 |  | 
|  | 56 | static size_t getFileSize(int fd) { | 
|  | 57 | struct stat st; | 
|  | 58 | if (fstat(fd, &st) < 0) { | 
|  | 59 | ALOGW("%s : fstat failed", __func__); | 
|  | 60 | return 0; | 
|  | 61 | } | 
|  | 62 | return st.st_size; // bytes | 
|  | 63 | } | 
|  | 64 |  | 
|  | 65 | static bool loadFile(const char filename[], JpegEncoderTest::Image* result) { | 
|  | 66 | int fd = open(filename, O_CLOEXEC); | 
|  | 67 | if (fd < 0) { | 
|  | 68 | return false; | 
|  | 69 | } | 
|  | 70 | int length = getFileSize(fd); | 
|  | 71 | if (length == 0) { | 
|  | 72 | close(fd); | 
|  | 73 | return false; | 
|  | 74 | } | 
|  | 75 | result->buffer.reset(new uint8_t[length]); | 
|  | 76 | if (read(fd, result->buffer.get(), length) != static_cast<ssize_t>(length)) { | 
|  | 77 | close(fd); | 
|  | 78 | return false; | 
|  | 79 | } | 
|  | 80 | close(fd); | 
|  | 81 | return true; | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 | void JpegEncoderTest::SetUp() { | 
|  | 85 | if (!loadFile(VALID_IMAGE, &mValidImage)) { | 
|  | 86 | FAIL() << "Load file " << VALID_IMAGE << " failed"; | 
|  | 87 | } | 
|  | 88 | mValidImage.width = VALID_IMAGE_WIDTH; | 
|  | 89 | mValidImage.height = VALID_IMAGE_HEIGHT; | 
|  | 90 | if (!loadFile(INVALID_SIZE_IMAGE, &mInvalidSizeImage)) { | 
|  | 91 | FAIL() << "Load file " << INVALID_SIZE_IMAGE << " failed"; | 
|  | 92 | } | 
|  | 93 | mInvalidSizeImage.width = INVALID_SIZE_IMAGE_WIDTH; | 
|  | 94 | mInvalidSizeImage.height = INVALID_SIZE_IMAGE_HEIGHT; | 
| Dichen Zhang | a481914 | 2022-10-21 04:12:30 +0000 | [diff] [blame] | 95 | if (!loadFile(SINGLE_CHANNEL_IMAGE, &mSingleChannelImage)) { | 
|  | 96 | FAIL() << "Load file " << SINGLE_CHANNEL_IMAGE << " failed"; | 
|  | 97 | } | 
|  | 98 | mSingleChannelImage.width = SINGLE_CHANNEL_IMAGE_WIDTH; | 
|  | 99 | mSingleChannelImage.height = SINGLE_CHANNEL_IMAGE_HEIGHT; | 
| Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 100 | } | 
|  | 101 |  | 
|  | 102 | void JpegEncoderTest::TearDown() {} | 
|  | 103 |  | 
|  | 104 | TEST_F(JpegEncoderTest, validImage) { | 
|  | 105 | JpegEncoder encoder; | 
|  | 106 | EXPECT_TRUE(encoder.compressImage(mValidImage.buffer.get(), mValidImage.width, | 
|  | 107 | mValidImage.height, JPEG_QUALITY, NULL, 0)); | 
|  | 108 | ASSERT_GT(encoder.getCompressedImageSize(), static_cast<uint32_t>(0)); | 
|  | 109 | } | 
|  | 110 |  | 
|  | 111 | TEST_F(JpegEncoderTest, invalidSizeImage) { | 
|  | 112 | JpegEncoder encoder; | 
|  | 113 | EXPECT_FALSE(encoder.compressImage(mInvalidSizeImage.buffer.get(), mInvalidSizeImage.width, | 
|  | 114 | mInvalidSizeImage.height, JPEG_QUALITY, NULL, 0)); | 
|  | 115 | } | 
|  | 116 |  | 
| Dichen Zhang | a481914 | 2022-10-21 04:12:30 +0000 | [diff] [blame] | 117 | TEST_F(JpegEncoderTest, singleChannelImage) { | 
|  | 118 | JpegEncoder encoder; | 
|  | 119 | EXPECT_TRUE(encoder.compressImage(mSingleChannelImage.buffer.get(), mSingleChannelImage.width, | 
|  | 120 | mSingleChannelImage.height, JPEG_QUALITY, NULL, 0, true)); | 
|  | 121 | ASSERT_GT(encoder.getCompressedImageSize(), static_cast<uint32_t>(0)); | 
|  | 122 | } | 
|  | 123 |  | 
| Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 124 | } | 
|  | 125 |  |