PropertyParser.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include <catch2/catch_test_macros.hpp>
  2. #include "WallpaperEngine/Data/Model/Property.h"
  3. #include "WallpaperEngine/Data/Parsers/PropertyParser.h"
  4. using WallpaperEngine::Data::JSON::JSON;
  5. using WallpaperEngine::Data::Model::DynamicValue;
  6. using WallpaperEngine::Data::Parsers::PropertyParser;
  7. TEST_CASE ("Bool properties without a value default to false") {
  8. const JSON propertyData = {
  9. { "type", "bool" },
  10. { "text", "Enabled" },
  11. };
  12. const auto property = PropertyParser::parse (propertyData, "enabled");
  13. REQUIRE (property != nullptr);
  14. CHECK (property->getType () == DynamicValue::Boolean);
  15. CHECK_FALSE (property->getBool ());
  16. }
  17. TEST_CASE ("Directory properties are parsed as file-like properties") {
  18. const JSON propertyData = {
  19. { "type", "directory" },
  20. { "text", "Folder" },
  21. };
  22. const auto property = PropertyParser::parse (propertyData, "folder");
  23. REQUIRE (property != nullptr);
  24. CHECK (property->dump ().find ("folder - file") != std::string::npos);
  25. }
  26. TEST_CASE ("Directory properties remember they are directories and what file type they hold") {
  27. const auto directory = PropertyParser::parse (
  28. JSON { { "type", "directory" }, { "text", "Videos" }, { "fileType", "video" }, { "mode", "fetchall" } }, "vidDir"
  29. );
  30. const auto file = PropertyParser::parse (JSON { { "type", "file" }, { "text", "Image" } }, "image");
  31. const auto* asDirectory = dynamic_cast<const WallpaperEngine::Data::Model::PropertyFile*> (directory.get ());
  32. const auto* asFile = dynamic_cast<const WallpaperEngine::Data::Model::PropertyFile*> (file.get ());
  33. REQUIRE (asDirectory != nullptr);
  34. REQUIRE (asFile != nullptr);
  35. CHECK (asDirectory->isDirectory ());
  36. CHECK (asDirectory->getFileType () == "video");
  37. CHECK_FALSE (asFile->isDirectory ());
  38. CHECK (asFile->getFileType ().empty ());
  39. }
  40. TEST_CASE ("UI-only description lines with made up types are parsed as plain text") {
  41. for (const char* type : { "txt", "test", "label" }) {
  42. const JSON propertyData = {
  43. { "type", type },
  44. { "text", "ui_some_tip" },
  45. { "value", "1 0 0" },
  46. };
  47. CHECK (PropertyParser::parse (propertyData, "tip") != nullptr);
  48. }
  49. }
  50. TEST_CASE ("Combo properties can be given a value the wallpaper did not list") {
  51. const JSON propertyData = {
  52. { "type", "combo" },
  53. { "text", "Interval" },
  54. { "value", 1 },
  55. { "options", JSON::array ({ { { "label", "1 min" }, { "value", 1 } }, { { "label", "5 min" }, { "value", 5 } } }) },
  56. };
  57. const auto property = PropertyParser::parse (propertyData, "interval");
  58. auto* combo = dynamic_cast<WallpaperEngine::Data::Model::PropertyCombo*> (property.get ());
  59. REQUIRE (combo != nullptr);
  60. combo->update (std::string ("0.5"), DynamicValue::UpdateSource::User);
  61. CHECK (combo->getString () == "1");
  62. combo->allowValue ("0.5");
  63. combo->update (std::string ("0.5"), DynamicValue::UpdateSource::User);
  64. CHECK (combo->getString () == "0.5");
  65. }