PrefsTests.inc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // --- Settings (General / Audio / MIDI pages), Plugin manager and Shortcut manager logic (core/Preferences.h,
  2. // core/KeyMap.h) - round 82 ----------------------------------------------------------------------------------------
  3. TEST (prefs_key_values)
  4. {
  5. const auto kv = lynx::prefs::parseKeyValues ("a=1\n b = two words \r\nnoequals\n=x\na=3\nc=\n");
  6. CHECK (kv.size() == 3);
  7. CHECK (kv.at ("a") == "3" && kv.at ("b") == "two words" && kv.at ("c").empty());
  8. }
  9. // A folder whose name ends in a space (".../lunx ") is a different folder than ".../lunx": the New project window saved
  10. // the path in new-project.txt, parseKeyValues trimmed the space, and the next project went to another folder (a second
  11. // "lunx" in Finder). encodeSettingsPath/decodeSettingsPath keep the path exactly as it is.
  12. TEST (prefs_paths_keep_a_trailing_space)
  13. {
  14. using namespace lynx::prefs;
  15. const std::string spaced = "/Users/me/Desktop/all in mac ssd/lunx ";
  16. CHECK (encodeSettingsPath (spaced) == spaced + "/");
  17. CHECK (decodeSettingsPath (encodeSettingsPath (spaced)) == spaced);
  18. // through a settings line, as the New project window writes it
  19. const auto kv = parseKeyValues ("show=1\nfolder=" + encodeSettingsPath (spaced) + "\nownFolder=1\n");
  20. CHECK (decodeSettingsPath (kv.at ("folder")) == spaced);
  21. // every other path is written and read as it is (a drive root included)
  22. CHECK (encodeSettingsPath ("/Users/me/Music") == "/Users/me/Music");
  23. CHECK (decodeSettingsPath ("/Users/me/Music") == "/Users/me/Music");
  24. CHECK (encodeSettingsPath ("C:\\") == "C:\\" && decodeSettingsPath ("C:\\") == "C:\\");
  25. }
  26. TEST (prefs_autosave_choices_and_backup_rotation)
  27. {
  28. using namespace lynx::prefs;
  29. CHECK (clampAutosaveMinutes (0) == 0 && clampAutosaveMinutes (-5) == 0);
  30. CHECK (clampAutosaveMinutes (2) == 2 && clampAutosaveMinutes (4) == 5 && clampAutosaveMinutes (999) == 30 && clampAutosaveMinutes (3) == 2);
  31. CHECK (clampAutosaveKeep (0) == 1 && clampAutosaveKeep (500) == 100 && clampAutosaveKeep (7) == 7);
  32. const auto a = backupName ("Night Drive", 2026, 10, 16, 9, 5, 3);
  33. CHECK (a == "Night Drive (2026-10-16 09.05.03)");
  34. CHECK (isBackupOf (a, "Night Drive"));
  35. CHECK (isBackupOf ("Night Drive", "Night Drive")); // the single backup of older versions
  36. CHECK (! isBackupOf ("Night Drive 2 (2026-10-16 09.05.03)", "Night Drive"));
  37. CHECK (! isBackupOf ("Night Drive (2026-10-16 09.05.0x)", "Night Drive"));
  38. CHECK (! isBackupOf ("Night (2026-10-16 09.05.03)", "Night Drive"));
  39. const std::vector<std::string> names { backupName ("Song", 2026, 10, 16, 10, 0, 0), backupName ("Song", 2026, 10, 15, 23, 59, 0), "Song",
  40. backupName ("Other", 2026, 1, 1, 0, 0, 0), backupName ("Song", 2026, 10, 16, 11, 0, 0) };
  41. const auto gone = backupsToRemove (names, "Song", 2);
  42. CHECK (gone.size() == 2);
  43. CHECK (gone.size() == 2 && gone[0] == "Song" && gone[1] == backupName ("Song", 2026, 10, 15, 23, 59, 0));
  44. CHECK (backupsToRemove (names, "Song", 10).empty());
  45. CHECK (backupsToRemove (names, "Other", 1).empty());
  46. CHECK (clampUndoLevels (1) == 10 && clampUndoLevels (5000) == 1000 && clampUndoLevels (250) == 250);
  47. CHECK (std::abs (bufferLatencyMs (480, 48000.0) - 10.0) < 1e-9 && bufferLatencyMs (256, 0.0) == 0.0);
  48. }
  49. TEST (prefs_midi_velocity_curves_and_device_settings)
  50. {
  51. using namespace lynx::prefs;
  52. CHECK (mapVelocity (0.5f, VelocityCurve::linear) == 0.5f);
  53. CHECK (mapVelocity (0.5f, VelocityCurve::soft) > 0.6f && mapVelocity (0.5f, VelocityCurve::hard) < 0.35f);
  54. CHECK (mapVelocity (0.2f, VelocityCurve::fixed) == 1.0f && mapVelocity (0.0f, VelocityCurve::fixed) == 0.0f);
  55. CHECK (mapVelocity (1.0f, VelocityCurve::soft) == 1.0f && mapVelocity (1.0f, VelocityCurve::hard) == 1.0f);
  56. CHECK (mapVelocity (2.0f, VelocityCurve::linear) == 1.0f);
  57. MidiPrefs p;
  58. CHECK (p.accepts ("Keystation 49", 3)); // unknown devices: on, any channel
  59. MidiInputPrefs k;
  60. k.name = "Keystation 49";
  61. k.midiChannel = 2;
  62. k.target = 4;
  63. p.setInput (k);
  64. MidiInputPrefs pads;
  65. pads.name = "Pads | MPD=218";
  66. pads.enabled = false;
  67. p.setInput (pads);
  68. p.velocity = VelocityCurve::soft;
  69. p.pickup = true;
  70. CHECK (p.inputs.size() == 2);
  71. CHECK (p.accepts ("Keystation 49", 2) && ! p.accepts ("Keystation 49", 3) && ! p.accepts ("Pads | MPD=218", 1));
  72. CHECK (p.input ("Keystation 49").target == 4 && p.input ("Other").target == -1);
  73. const auto back = MidiPrefs::fromText (p.toText());
  74. CHECK (back == p);
  75. CHECK (back.input ("Pads | MPD=218").enabled == false);
  76. // setting a device back to the defaults drops it from the list
  77. k.midiChannel = 0;
  78. k.target = -1;
  79. p.setInput (k);
  80. CHECK (p.inputs.size() == 1);
  81. CHECK (MidiPrefs::fromText ("input=1|99|-7|X\ninput=junk\nvelocity=hard\n").input ("X").midiChannel == 16);
  82. CHECK (MidiPrefs::fromText ("velocity=hard").velocity == VelocityCurve::hard);
  83. }
  84. TEST (prefs_midi_pickup)
  85. {
  86. using namespace lynx::prefs;
  87. CHECK (! pickupReached (0.5f, -1.0f, 0.1f)); // first value, far away
  88. CHECK (pickupReached (0.5f, -1.0f, 0.5f)); // lands on it
  89. CHECK (! pickupReached (0.5f, 0.1f, 0.3f)); // still below
  90. CHECK (pickupReached (0.5f, 0.3f, 0.7f)); // moved past it (up)
  91. CHECK (pickupReached (0.5f, 0.9f, 0.2f)); // moved past it (down)
  92. CHECK (pickupReached (0.5f, 0.2f, 0.505f)); // within the tolerance
  93. PickupState st;
  94. CHECK (! pickupUpdate (st, 0.5f, 0.1f)); // the knob is elsewhere: nothing changes
  95. CHECK (! pickupUpdate (st, 0.5f, 0.3f));
  96. CHECK (pickupUpdate (st, 0.5f, 0.6f)); // crossed the value: caught
  97. CHECK (pickupUpdate (st, 0.6f, 0.9f)); // then follows even in big steps
  98. CHECK (pickupUpdate (st, 0.9f, 0.2f));
  99. CHECK (! pickupUpdate (st, 0.8f, 0.25f)); // the parameter was moved by the mouse: lost again
  100. CHECK (pickupUpdate (st, 0.8f, 0.85f));
  101. }
  102. TEST (prefs_plugin_manager_filter_and_favourites)
  103. {
  104. using namespace lynx::prefs;
  105. std::vector<PluginRow> rows (4);
  106. rows[0] = { "Serum", "Xfer Records", "VST3", "Synth", "C:\\VST3\\Serum.vst3", "s", true, PluginStatus::ok, true };
  107. rows[1] = { "Pro-Q 3", "FabFilter", "VST3", "EQ", "C:\\VST3\\Pro-Q 3.vst3", "q", false, PluginStatus::ok, false };
  108. rows[2] = { "Broken", "", "CLAP", "", "C:\\CLAP\\Broken.clap", "", false, PluginStatus::failed, false };
  109. rows[3] = { "Analog Lab", "Arturia", "CLAP", "Synth", "C:\\CLAP\\Analog Lab.clap", "a", true, PluginStatus::missingFile, false };
  110. CHECK ((visiblePlugins (rows, "", PluginFilter::all, PluginSort::name) == std::vector<int> { 3, 2, 1, 0 }));
  111. CHECK ((visiblePlugins (rows, "", PluginFilter::instruments, PluginSort::name) == std::vector<int> { 3, 0 }));
  112. CHECK ((visiblePlugins (rows, "", PluginFilter::effects, PluginSort::name) == std::vector<int> { 1 })); // failed ones are not effects
  113. CHECK ((visiblePlugins (rows, "", PluginFilter::favourites, PluginSort::name) == std::vector<int> { 0 }));
  114. CHECK ((visiblePlugins (rows, "", PluginFilter::failed, PluginSort::name) == std::vector<int> { 3, 2 }));
  115. CHECK ((visiblePlugins (rows, "fab vst3", PluginFilter::all, PluginSort::name) == std::vector<int> { 1 }));
  116. CHECK ((visiblePlugins (rows, "SYNTH", PluginFilter::all, PluginSort::vendor) == std::vector<int> { 3, 0 }));
  117. CHECK ((visiblePlugins (rows, "", PluginFilter::all, PluginSort::format) == std::vector<int> { 3, 2, 1, 0 }));
  118. CHECK ((visiblePlugins (rows, "", PluginFilter::all, PluginSort::name, true) == std::vector<int> { 0, 1, 2, 3 }));
  119. CHECK ((visiblePlugins (rows, "", PluginFilter::all, PluginSort::status) == std::vector<int> { 1, 0, 2, 3 }));
  120. const std::set<std::string> fav { "VST3-Serum-1", "CLAP-x" };
  121. CHECK (parseFavourites (formatFavourites (fav)) == fav);
  122. CHECK (parseFavourites ("\n a \r\n\nb\n").size() == 2);
  123. }
  124. TEST (keymap_shortcut_manager_helpers)
  125. {
  126. lynx::keys::KeyMap map;
  127. CHECK (map.overridesJson() == "{\n}\n"); // nothing changed
  128. CHECK (map.isDefault ("pattern.next"));
  129. // the chord of an existing global action, asked for another global action: reported as taken
  130. const auto& play = map.chordsFor ("transport.playStop");
  131. CHECK (! play.empty());
  132. const auto users = map.actionsWith (play.front(), lynx::keys::Context::global, "transport.pause");
  133. CHECK (users.size() == 1 && users[0] == "transport.playStop");
  134. CHECK (map.actionsWith (play.front(), lynx::keys::Context::global, "transport.playStop").empty());
  135. lynx::keys::Chord f9 { lynx::keys::f9, lynx::keys::ctrl | lynx::keys::shift };
  136. CHECK (map.actionsWith (f9, lynx::keys::Context::global).empty());
  137. map.setChords ("pattern.next", { f9 });
  138. CHECK (! map.isDefault ("pattern.next"));
  139. const auto json = map.overridesJson();
  140. CHECK (json == "{\n \"pattern.next\": [\"Ctrl+Shift+F9\"]\n}\n");
  141. CHECK (map.actionsWith (f9, lynx::keys::Context::global).size() == 1);
  142. // the overrides read back give the same map
  143. lynx::keys::KeyMap back;
  144. std::vector<std::string> errors;
  145. CHECK (back.loadJson (json, &errors) && errors.empty());
  146. CHECK (back.chordsFor ("pattern.next") == map.chordsFor ("pattern.next"));
  147. CHECK (back.toJson() == map.toJson());
  148. map.setChords ("transport.playStop", {});
  149. CHECK (map.overridesJson().find ("\"transport.playStop\": []") != std::string::npos);
  150. }