Merge pull request #10484 from akallabeth/clip_unit_test

Clip unit test
This commit is contained in:
akallabeth 2024-08-19 12:06:16 +02:00 committed by GitHub
commit 82835026d3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 167 additions and 9 deletions

View File

@ -8,6 +8,20 @@ set(${MODULE_PREFIX}_TESTS
TestUri.c
TestClipboardFormats.c)
set(TEST_CLIP_PNG "${CMAKE_SOURCE_DIR}/resources/FreeRDP_Icon.png")
file(TO_NATIVE_PATH "${TEST_CLIP_PNG}" TEST_CLIP_PNG)
set(TEST_CLIP_BMP "${CMAKE_SOURCE_DIR}/resources/FreeRDP_Install.bmp")
file(TO_NATIVE_PATH "${TEST_CLIP_BMP}" TEST_CLIP_BMP)
if (WIN32)
string(REPLACE "\\" "\\\\" TEST_CLIP_PNG "${TEST_CLIP_PNG}")
string(REPLACE "\\" "\\\\" TEST_CLIP_BMP "${TEST_CLIP_BMP}")
endif()
add_definitions(-DTEST_CLIP_BMP="${TEST_CLIP_BMP}")
add_definitions(-DTEST_CLIP_PNG="${TEST_CLIP_PNG}")
create_test_sourcelist(${MODULE_PREFIX}_SRCS
${${MODULE_PREFIX}_DRIVER}
${${MODULE_PREFIX}_TESTS})

View File

@ -1,10 +1,12 @@
#include <winpr/crt.h>
#include <winpr/print.h>
#include <winpr/image.h>
#include <winpr/clipboard.h>
int TestClipboardFormats(int argc, char* argv[])
{
int rc = -1;
UINT32 count = 0;
UINT32* pFormatIds = NULL;
const char* formatName = NULL;
@ -18,9 +20,16 @@ int TestClipboardFormats(int argc, char* argv[])
if (!clipboard)
return -1;
ClipboardRegisterFormat(clipboard, "text/html");
ClipboardRegisterFormat(clipboard, "image/bmp");
ClipboardRegisterFormat(clipboard, "image/png");
const char* mime_types[] = { "text/html", "text/html", "image/bmp",
"image/png", "image/webp", "image/jpeg" };
for (size_t x = 0; x < ARRAYSIZE(mime_types); x++)
{
const char* mime = mime_types[x];
UINT32 id = ClipboardRegisterFormat(clipboard, mime);
fprintf(stderr, "ClipboardRegisterFormat(%s) -> 0x%08" PRIx32 "\n", mime, id);
if (id == 0)
goto fail;
}
utf8StringFormatId = ClipboardRegisterFormat(clipboard, "UTF8_STRING");
pFormatIds = NULL;
@ -76,7 +85,140 @@ int TestClipboardFormats(int argc, char* argv[])
fprintf(stderr, "Format: 0x%08" PRIX32 " %s\n", formatId, formatName);
}
if (1)
{
const char* name = TEST_CLIP_BMP;
BOOL bSuccess = FALSE;
UINT32 idBmp = ClipboardRegisterFormat(clipboard, "image/bmp");
wImage* img = winpr_image_new();
if (!img)
goto fail;
if (winpr_image_read(img, name) <= 0)
{
winpr_image_free(img, TRUE);
goto fail;
}
size_t bmpsize = 0;
void* data = winpr_image_write_buffer(img, WINPR_IMAGE_BITMAP, &bmpsize);
bSuccess = ClipboardSetData(clipboard, idBmp, data, bmpsize);
fprintf(stderr, "ClipboardSetData: %" PRId32 "\n", bSuccess);
free(data);
winpr_image_free(img, TRUE);
if (!bSuccess)
goto fail;
{
UINT32 id = CF_DIB;
UINT32 DstSize = 0;
void* pDstData = ClipboardGetData(clipboard, id, &DstSize);
fprintf(stderr, "ClipboardGetData: [CF_DIB] %p [%" PRIu32 "]\n", pDstData, DstSize);
if (!pDstData)
goto fail;
bSuccess = ClipboardSetData(clipboard, id, pDstData, DstSize);
free(pDstData);
if (!bSuccess)
goto fail;
}
{
UINT32 id = ClipboardRegisterFormat(clipboard, "image/bmp");
UINT32 DstSize = 0;
void* pDstData = ClipboardGetData(clipboard, id, &DstSize);
fprintf(stderr, "ClipboardGetData: [image/bmp] %p [%" PRIu32 "]\n", pDstData, DstSize);
if (!pDstData)
goto fail;
free(pDstData);
if (DstSize != bmpsize)
goto fail;
}
#if defined(WINPR_UTILS_IMAGE_PNG)
{
UINT32 id = ClipboardRegisterFormat(clipboard, "image/png");
UINT32 DstSize = 0;
void* pDstData = ClipboardGetData(clipboard, id, &DstSize);
fprintf(stderr, "ClipboardGetData: [image/png] %p\n", pDstData);
if (!pDstData)
goto fail;
free(pDstData);
}
{
const char* name = TEST_CLIP_PNG;
BOOL bSuccess = FALSE;
UINT32 idBmp = ClipboardRegisterFormat(clipboard, "image/png");
wImage* img = winpr_image_new();
if (!img)
goto fail;
if (winpr_image_read(img, name) <= 0)
{
winpr_image_free(img, TRUE);
goto fail;
}
size_t bmpsize = 0;
void* data = winpr_image_write_buffer(img, WINPR_IMAGE_PNG, &bmpsize);
bSuccess = ClipboardSetData(clipboard, idBmp, data, bmpsize);
fprintf(stderr, "ClipboardSetData: %" PRId32 "\n", bSuccess);
free(data);
winpr_image_free(img, TRUE);
if (!bSuccess)
goto fail;
}
{
UINT32 id = CF_DIB;
UINT32 DstSize = 0;
void* pDstData = ClipboardGetData(clipboard, id, &DstSize);
fprintf(stderr, "ClipboardGetData: [CF_DIB] %p [%" PRIu32 "]\n", pDstData, DstSize);
if (!pDstData)
goto fail;
bSuccess = ClipboardSetData(clipboard, id, pDstData, DstSize);
free(pDstData);
if (!bSuccess)
goto fail;
}
#endif
#if defined(WINPR_UTILS_IMAGE_WEBP)
{
UINT32 id = ClipboardRegisterFormat(clipboard, "image/webp");
UINT32 DstSize = 0;
void* pDstData = ClipboardGetData(clipboard, id, &DstSize);
fprintf(stderr, "ClipboardGetData: [image/webp] %p\n", pDstData);
if (!pDstData)
goto fail;
free(pDstData);
}
#endif
#if defined(WINPR_UTILS_IMAGE_JPEG)
{
UINT32 id = ClipboardRegisterFormat(clipboard, "image/jpeg");
UINT32 DstSize = 0;
void* pDstData = ClipboardGetData(clipboard, id, &DstSize);
fprintf(stderr, "ClipboardGetData: [image/jpeg] %p\n", pDstData);
if (!pDstData)
goto fail;
free(pDstData);
}
#endif
}
rc = 0;
fail:
free(pFormatIds);
ClipboardDestroy(clipboard);
return 0;
return rc;
}

View File

@ -430,7 +430,8 @@ static int winpr_image_bitmap_read_buffer(wImage* image, const BYTE* buffer, siz
image->bitsPerPixel = bi.biBitCount;
image->bytesPerPixel = (image->bitsPerPixel / 8);
image->scanline = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) >> 3);
const size_t bpp = (bi.biBitCount + 7) / 8;
image->scanline = bi.biWidth * bpp;
const size_t bmpsize = 1ull * image->scanline * image->height;
if (bmpsize != bi.biSizeImage)
WLog_WARN(TAG, "bmpsize=%" PRIuz " != bi.biSizeImage=%" PRIu32, bmpsize, bi.biSizeImage);
@ -939,14 +940,15 @@ static void* winpr_read_png_from_buffer(const void* data, size_t SrcSize, size_t
NULL, NULL) != 1)
goto fail;
size_t bpp = PNG_IMAGE_PIXEL_SIZE(color_type);
const png_byte channelcount = png_get_channels(png_ptr, info_ptr);
const size_t bpp = channelcount * bit_depth;
row_pointers = png_get_rows(png_ptr, info_ptr);
if (row_pointers)
{
const size_t stride = width * bpp;
const size_t stride = width * bpp / 8ull;
const size_t png_stride = png_get_rowbytes(png_ptr, info_ptr);
const size_t size = width * height * bpp;
const size_t size = width * height * bpp / 8ull;
const size_t copybytes = stride > png_stride ? png_stride : stride;
rc = malloc(size);
@ -961,7 +963,7 @@ static void* winpr_read_png_from_buffer(const void* data, size_t SrcSize, size_t
*pSize = size;
*pWidth = width;
*pHeight = height;
*pBpp = bpp * 8;
*pBpp = bpp;
}
}
fail: