tests: better stuff

This commit is contained in:
Vaxry 2024-04-06 21:15:43 +01:00
parent 35411a6184
commit b868c2e11d
4 changed files with 131 additions and 28 deletions

View file

@ -65,12 +65,17 @@ target_link_libraries(hyprcursor-util PkgConfig::deps hyprcursor)
# tests # tests
add_custom_target(tests) add_custom_target(tests)
add_executable(hyprcursor_test "tests/test.cpp") add_executable(hyprcursor_test1 "tests/full_rendering.cpp")
target_link_libraries(hyprcursor_test PRIVATE hyprcursor) target_link_libraries(hyprcursor_test1 PRIVATE hyprcursor)
add_test(NAME "Test libhyprcursor in C++" WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests COMMAND hyprcursor_test) add_test(NAME "Test libhyprcursor in C++ (full rendering)" WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests COMMAND hyprcursor_test1)
add_dependencies(tests hyprcursor_test) add_dependencies(tests hyprcursor_test1)
add_executable(hyprcursor_test_c "tests/test.c") add_executable(hyprcursor_test2 "tests/only_metadata.cpp")
target_link_libraries(hyprcursor_test2 PRIVATE hyprcursor)
add_test(NAME "Test libhyprcursor in C++ (only metadata)" WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests COMMAND hyprcursor_test2)
add_dependencies(tests hyprcursor_test2)
add_executable(hyprcursor_test_c "tests/c_test.c")
target_link_libraries(hyprcursor_test_c PRIVATE hyprcursor) target_link_libraries(hyprcursor_test_c PRIVATE hyprcursor)
add_test(NAME "Test libhyprcursor in C" WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests COMMAND hyprcursor_test_c) add_test(NAME "Test libhyprcursor in C" WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests COMMAND hyprcursor_test_c)
add_dependencies(tests hyprcursor_test_c) add_dependencies(tests hyprcursor_test_c)

View file

@ -1,3 +1,10 @@
/*
hyprlang-test in C.
Renders a cursor shape to /tmp at 48px
For better explanations, see the cpp tests.
*/
#include <hyprcursor/hyprcursor.h> #include <hyprcursor/hyprcursor.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -6,11 +13,6 @@ void logFunction(enum eHyprcursorLogLevel level, char* message) {
printf("[hc] %s\n", message); printf("[hc] %s\n", message);
} }
/*
hyprlang-test in C.
Renders a cursor shape to /tmp at 48px
*/
int main(int argc, char** argv) { int main(int argc, char** argv) {
struct hyprcursor_manager_t* mgr = hyprcursor_manager_create_with_logger(NULL, logFunction); struct hyprcursor_manager_t* mgr = hyprcursor_manager_create_with_logger(NULL, logFunction);

View file

@ -1,3 +1,11 @@
/*
full_rendering.cpp
This example shows probably what you want to do.
Hyprcursor will render a left_ptr shape at 48x48px to a file called /tmp/arrow.png
*/
#include <iostream> #include <iostream>
#include <hyprcursor/hyprcursor.hpp> #include <hyprcursor/hyprcursor.hpp>
@ -5,40 +13,43 @@ void logFunction(enum eHyprcursorLogLevel level, char* message) {
std::cout << "[hc] " << message << "\n"; std::cout << "[hc] " << message << "\n";
} }
/*
hyprlang-test in C++.
Renders a cursor shape to /tmp at 48px
*/
int main(int argc, char** argv) { int main(int argc, char** argv) {
/*
Create a manager. You can optionally pass a logger function.
*/
Hyprcursor::CHyprcursorManager mgr(nullptr, logFunction); Hyprcursor::CHyprcursorManager mgr(nullptr, logFunction);
/*
Manager could be invalid if no themes were found, or
a specified theme was invalid.
*/
if (!mgr.valid()) { if (!mgr.valid()) {
std::cout << "mgr is invalid\n"; std::cout << "mgr is invalid\n";
return 1; return 1;
} }
// test raw data /*
const auto RAWDATA = mgr.getRawShapeData("left_ptr"); Style describes what pixel size you want your cursor
if (RAWDATA.images.empty()) { images to be.
std::cout << "failed querying left_ptr\n";
return 1;
}
std::cout << "left_ptr images: " << RAWDATA.images.size() << "\n";
for (auto& i : RAWDATA.images)
std::cout << "left_ptr data size: " << i.data.size() << "\n";
Remember to free styles once you're done with them
(e.g. the user requested to change the cursor size to something else)
*/
Hyprcursor::SCursorStyleInfo style{.size = 48}; Hyprcursor::SCursorStyleInfo style{.size = 48};
// preload size 48 for testing
if (!mgr.loadThemeStyle(style)) { if (!mgr.loadThemeStyle(style)) {
std::cout << "failed loading style\n"; std::cout << "failed loading style\n";
return 1; return 1;
} }
// get cursor for left_ptr /*
Get a shape. This will return the data about available image(s),
their delay, hotspot, etc.
*/
const auto SHAPEDATA = mgr.getShape("left_ptr", style); const auto SHAPEDATA = mgr.getShape("left_ptr", style);
/*
If the size doesn't exist, images will be empty.
*/
if (SHAPEDATA.images.empty()) { if (SHAPEDATA.images.empty()) {
std::cout << "no images\n"; std::cout << "no images\n";
return 1; return 1;
@ -46,11 +57,16 @@ int main(int argc, char** argv) {
std::cout << "hyprcursor returned " << SHAPEDATA.images.size() << " images\n"; std::cout << "hyprcursor returned " << SHAPEDATA.images.size() << " images\n";
// save to disk /*
Save to disk with cairo
*/
const auto RET = cairo_surface_write_to_png(SHAPEDATA.images[0].surface, "/tmp/arrow.png"); const auto RET = cairo_surface_write_to_png(SHAPEDATA.images[0].surface, "/tmp/arrow.png");
std::cout << "Cairo returned for write: " << RET << "\n"; std::cout << "Cairo returned for write: " << RET << "\n";
/*
As mentioned before, clean up by releasing the style.
*/
mgr.cursorSurfaceStyleDone(style); mgr.cursorSurfaceStyleDone(style);
if (RET) if (RET)

80
tests/only_metadata.cpp Normal file
View file

@ -0,0 +1,80 @@
/*
only_metadata.cpp
This is a mode in which you probably do NOT want to operate,
but major DEs might want their own renderer for
cursor shapes.
Prefer full_rendering.cpp for consistency and simplicity.
*/
#include <iostream>
#include <hyprcursor/hyprcursor.hpp>
void logFunction(enum eHyprcursorLogLevel level, char* message) {
std::cout << "[hc] " << message << "\n";
}
int main(int argc, char** argv) {
/*
Create a manager. You can optionally pass a logger function.
*/
Hyprcursor::CHyprcursorManager mgr(nullptr, logFunction);
/*
Manager could be invalid if no themes were found, or
a specified theme was invalid.
*/
if (!mgr.valid()) {
std::cout << "mgr is invalid\n";
return 1;
}
/*
If you are planning on using your own renderer,
you do not want to load in any styles, as those
are rendered once you make your call.
Instead, let's request left_ptr's metadata
*/
auto RAWDATA = mgr.getRawShapeData("left_ptr");
/*
if images are empty, check overridenBy
*/
if (RAWDATA.images.empty()) {
/*
if overridenBy is empty, the current theme doesn't have this shape.
*/
if (RAWDATA.overridenBy.empty())
return false;
/*
load what it's overriden by.
*/
RAWDATA = mgr.getRawShapeData(RAWDATA.overridenBy.c_str());
}
/*
If we still have no images, the theme seems broken.
*/
if (RAWDATA.images.empty()) {
std::cout << "failed querying left_ptr\n";
return 1;
}
/*
You can query the images (animation frames)
or their properties.
Every image has .data and .type for you to handle.
*/
std::cout << "left_ptr images: " << RAWDATA.images.size() << "\n";
for (auto& i : RAWDATA.images)
std::cout << "left_ptr data size: " << i.data.size() << "\n";
return 0;
}