CrossClipboardTests.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. // Round 190nz (issue #232 stage 2): the cross-window clipboard's text and documents (src/engine/CrossClipboard.h) read
  2. // back exactly, inline and through the side file, and a pasted fragment merges as core/CrossPaste.h says.
  3. #include "engine/CrossClipboard.h"
  4. #include "engine/ProjectFile.h"
  5. #include "core/CrossPaste.h"
  6. #include "core/Groove.h"
  7. #include <cstdio>
  8. #include <stdexcept>
  9. using namespace lynx;
  10. namespace
  11. {
  12. int checks = 0, failures = 0;
  13. #define EXPECT(condition) \
  14. do \
  15. { \
  16. ++checks; \
  17. if (! (condition)) \
  18. { \
  19. ++failures; \
  20. std::fprintf (stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #condition); \
  21. } \
  22. } while (false)
  23. juce::File sideFolder()
  24. {
  25. return juce::File::getSpecialLocation (juce::File::tempDirectory).getChildFile ("lynx-crossclip-tests");
  26. }
  27. crossclipboard::Payload roundTrip (const crossclipboard::Payload& p, std::size_t limit = crossclipboard::inlineLimit)
  28. {
  29. const auto text = crossclipboard::clipboardText (p, sideFolder(), limit);
  30. EXPECT (text.isNotEmpty());
  31. const auto envelope = crossclipboard::envelopeOf (text);
  32. EXPECT (envelope.has_value());
  33. if (! envelope)
  34. return {};
  35. EXPECT (envelope->kind == p.kind && envelope->stamp == p.stamp && envelope->pid == p.pid);
  36. auto back = crossclipboard::open (*envelope);
  37. EXPECT (back.has_value());
  38. return back ? *back : crossclipboard::Payload {};
  39. }
  40. crossclipboard::Payload payload (crossclipboard::Kind kind)
  41. {
  42. crossclipboard::Payload p;
  43. p.kind = kind;
  44. p.stamp = juce::Uuid().toString();
  45. p.pid = 4242;
  46. p.from = juce::String::fromUTF8 ("Álbum – faixa 2");
  47. return p;
  48. }
  49. void notes()
  50. {
  51. auto p = payload (crossclipboard::Kind::notes);
  52. Note a;
  53. a.start = 96;
  54. a.length = 48;
  55. a.channel = 5; // (the channel does not travel: the paste puts the notes on the channel being edited)
  56. a.key = 67;
  57. a.velocity = 0.61f;
  58. a.pan = -0.25f;
  59. a.finePitch = 33.0f;
  60. a.slide = true;
  61. a.release = 0.2f;
  62. a.modX = 0.3f;
  63. a.modY = 0.9f;
  64. a.colour = 7;
  65. a.group = 2;
  66. a.repeat = 3;
  67. a.chance = 0.5f;
  68. a.velocityRange = 0.25f;
  69. a.chanceGroup = 1;
  70. a.chanceOne = true;
  71. NoteCurves curves;
  72. curves.lanes[0] = { { 0.0f, 0.5f }, { 0.5f, 0.75f } };
  73. a.curves = curves;
  74. Note b;
  75. b.start = 0;
  76. b.key = 48;
  77. b.muted = true;
  78. p.notes = { a, b };
  79. const auto back = roundTrip (p);
  80. EXPECT (back.from == p.from);
  81. EXPECT (back.notes.size() == 2u);
  82. if (back.notes.size() == 2u)
  83. {
  84. // sorted by start; every field but the channel kept
  85. auto expectA = a;
  86. expectA.channel = 0;
  87. auto expectB = b;
  88. expectB.channel = 0;
  89. EXPECT (back.notes[0].start == 0 && back.notes[0].key == 48 && back.notes[0].muted && back.notes[0].channel == 0);
  90. EXPECT (back.notes[1].key == 67 && back.notes[1].curves == expectA.curves);
  91. EXPECT (std::abs (back.notes[1].velocity - a.velocity) < 1.0e-4f && back.notes[1].chanceOne && back.notes[1].repeat == 3);
  92. EXPECT (back.notes[1].colour == 7 && back.notes[1].group == 2 && back.notes[1].slide);
  93. }
  94. }
  95. Song source()
  96. {
  97. auto song = Song::makeDefault();
  98. ChannelDef lead;
  99. lead.name = "Lead";
  100. lead.instrument = InstrumentKind::plugin;
  101. lead.plugin.id = "VST3-Lead";
  102. lead.plugin.name = "Lead";
  103. lead.plugin.state = juce::Base64::toBase64 ("lead state", 10).toStdString();
  104. song.channels.push_back (lead);
  105. ChannelDef vox;
  106. vox.name = "Vox";
  107. vox.instrument = InstrumentKind::audioClip;
  108. vox.samplePath = juce::File::getSpecialLocation (juce::File::tempDirectory).getChildFile ("vox take.wav").getFullPathName().toStdString();
  109. song.channels.push_back (vox);
  110. auto& pattern = song.patterns[0];
  111. pattern.notes.push_back ({ 0, 96, 5, 72, 0.5f });
  112. pattern.sortNotes();
  113. Automation a;
  114. a.name = "Lead volume";
  115. a.target = { AutomationTargetKind::channelVolume, 5, 0, 0 };
  116. a.points = { { 0, 0.1f, 0.2f, 0 }, { 192, 0.8f, 0.0f, 0 } };
  117. song.automations.push_back (a);
  118. Groove g;
  119. g.name = "Shuffle";
  120. g.timing = 0.7f;
  121. const auto id = groove::add (song.grooves, g);
  122. song.clipEnvelopes.push_back ({});
  123. song.clipEnvelopes.back().pan = { { 0, 0.2f, 0.0f, 0 } };
  124. song.playlist.clear();
  125. Clip c;
  126. c.pattern = 0;
  127. c.track = 0;
  128. c.start = 384;
  129. c.length = 384;
  130. c.groove = id;
  131. song.playlist.push_back (c);
  132. Clip audio;
  133. audio.kind = ClipKind::audio;
  134. audio.channel = 6;
  135. audio.track = 1;
  136. audio.start = 0;
  137. audio.length = 192;
  138. audio.gain = 0.5f;
  139. audio.envelope = 0;
  140. song.playlist.push_back (audio);
  141. Clip automation;
  142. automation.kind = ClipKind::automation;
  143. automation.automation = 0;
  144. automation.track = 2;
  145. automation.length = 192;
  146. song.playlist.push_back (automation);
  147. return song;
  148. }
  149. void clips()
  150. {
  151. const auto song = source();
  152. auto p = payload (crossclipboard::Kind::clips);
  153. p.fragment = crosspaste::clipsFragment (song, song.playlist);
  154. const auto back = roundTrip (p);
  155. const auto& f = back.fragment;
  156. EXPECT (f.channels.size() == p.fragment.channels.size());
  157. EXPECT (f.playlist.size() == 3u);
  158. for (std::size_t i = 0; i < juce::jmin (f.channels.size(), p.fragment.channels.size()); ++i)
  159. {
  160. EXPECT (f.channels[i].name == p.fragment.channels[i].name);
  161. EXPECT (f.channels[i].plugin.state == p.fragment.channels[i].plugin.state);
  162. EXPECT (f.channels[i].samplePath == p.fragment.channels[i].samplePath);
  163. EXPECT (f.channels[i].instrument == p.fragment.channels[i].instrument);
  164. }
  165. if (f.playlist.size() == 3u)
  166. {
  167. EXPECT (f.playlist[0].pattern >= 0 && f.patterns[(std::size_t) f.playlist[0].pattern].notes == p.fragment.patterns[0].notes);
  168. EXPECT (f.playlist[1].gain == 0.5f && f.playlist[1].envelope >= 0);
  169. EXPECT (f.automations.size() == 1u && f.automations[0].points == p.fragment.automations[0].points);
  170. EXPECT (f.grooves.grooves.size() == 1u && f.grooves.grooves[0].name == "Shuffle");
  171. }
  172. // pasted into another project: Kick..Synth are its own, Lead and Vox come in
  173. auto other = Song::makeDefault();
  174. const auto pasted = crosspaste::importClips (other, f);
  175. EXPECT (pasted.size() == 3u);
  176. EXPECT (other.channels.size() == 7u && other.channels[5].name == "Lead" && other.channels[6].name == "Vox");
  177. if (pasted.size() == 3u)
  178. {
  179. EXPECT (other.automations[(std::size_t) pasted[2].automation].target.index == 5);
  180. EXPECT (pasted[1].channel == 6);
  181. EXPECT (pasted[0].groove != 0);
  182. // the same paste again (a second Ctrl+V): nothing new in the project
  183. const auto patterns = other.patterns.size();
  184. crosspaste::importClips (other, f);
  185. EXPECT (other.patterns.size() == patterns && other.channels.size() == 7u);
  186. }
  187. // a fragment saved through the project's own code never brings a phantom pattern along
  188. Clip onlyAudio = song.playlist[1];
  189. auto q = payload (crossclipboard::Kind::clips);
  190. q.fragment = crosspaste::clipsFragment (song, { onlyAudio });
  191. const auto audioBack = roundTrip (q);
  192. auto third = Song::makeDefault();
  193. const auto patterns = third.patterns.size();
  194. EXPECT (crosspaste::importClips (third, audioBack.fragment).size() == 1u);
  195. EXPECT (third.patterns.size() == patterns && third.channels.size() == 6u);
  196. }
  197. void channelsThroughTheSideFile()
  198. {
  199. auto song = source();
  200. // a plugin state larger than the inline limit: the clipboard holds a path, the file holds the document
  201. std::string big (200 * 1024, 'A');
  202. song.channels[5].plugin.state = big;
  203. auto p = payload (crossclipboard::Kind::channels);
  204. p.fragment = crosspaste::channelsFragment (song, { 5, 6 });
  205. const auto text = crossclipboard::clipboardText (p, sideFolder());
  206. EXPECT (text.length() < 1000);
  207. const auto envelope = crossclipboard::envelopeOf (text);
  208. EXPECT (envelope.has_value() && envelope->file.existsAsFile() && envelope->document.isEmpty());
  209. const auto back = envelope ? crossclipboard::open (*envelope) : std::nullopt;
  210. EXPECT (back.has_value());
  211. if (back)
  212. {
  213. EXPECT (back->fragment.channels.size() == 2u);
  214. EXPECT (back->fragment.channels.size() == 2u && back->fragment.channels[0].plugin.state == big);
  215. auto other = Song::makeDefault();
  216. const auto added = crosspaste::importChannels (other, back->fragment, 2);
  217. EXPECT (added == (std::vector<int> { 2, 3 }) && other.channels[2].name == "Lead" && other.channels[3].name == "Vox");
  218. }
  219. // the file gone (another window pruned it, the temp folder was emptied): nothing to paste, no crash
  220. if (envelope)
  221. {
  222. envelope->file.deleteFile();
  223. EXPECT (! crossclipboard::open (*envelope).has_value());
  224. }
  225. // only the newest side files stay
  226. for (int i = 0; i < 7; ++i)
  227. {
  228. auto q = payload (crossclipboard::Kind::channels);
  229. q.fragment = p.fragment;
  230. crossclipboard::clipboardText (q, sideFolder());
  231. juce::Thread::sleep (15);
  232. }
  233. EXPECT (sideFolder().findChildFiles (juce::File::findFiles, false, "*.json").size() <= 4);
  234. EXPECT (sideFolder().findChildFiles (juce::File::findFiles, false, "*.part").isEmpty());
  235. }
  236. void stepsMixerAndPoints()
  237. {
  238. auto p = payload (crossclipboard::Kind::steps);
  239. ChannelStepsClipboard::Slot a;
  240. a.copied = true;
  241. a.notes = { { 0, 24, 0, 60, 0.7f }, { 96, 24, 0, 60, 0.9f } };
  242. a.loops = true;
  243. a.loopSetting = ChannelLoop::nextBeat;
  244. ChannelStepsClipboard::Slot gap; // (a channel that was not selected)
  245. ChannelStepsClipboard::Slot empty;
  246. empty.copied = true; // (a selected channel without notes: a paste clears that channel)
  247. p.steps.slots = { a, gap, empty };
  248. const auto steps = roundTrip (p).steps;
  249. EXPECT (steps.slots.size() == 3u);
  250. if (steps.slots.size() == 3u)
  251. {
  252. EXPECT (steps.slots[0].copied && steps.slots[0].notes.size() == 2u && steps.slots[0].loops && steps.slots[0].loopSetting == ChannelLoop::nextBeat);
  253. EXPECT (! steps.slots[1].copied && steps.slots[1].notes.empty());
  254. EXPECT (steps.slots[2].copied && steps.slots[2].notes.empty() && ! steps.slots[2].loops);
  255. }
  256. auto m = payload (crossclipboard::Kind::mixerTracks);
  257. MixerTrack t;
  258. t.name = "Vocal bus";
  259. t.colour = 0xff3080c0;
  260. t.volume = 0.62f;
  261. t.pan = -0.2f;
  262. EffectSlot eq;
  263. eq.kind = EffectKind::limiter;
  264. eq.params = { 0.5f, 0.25f, 0.75f };
  265. t.effects.push_back (eq);
  266. t.sends.push_back ({ 3, 0.5f }); // (routing belongs to the project: it does not travel)
  267. m.mixerTracks = { t, t };
  268. const auto tracks = roundTrip (m).mixerTracks;
  269. EXPECT (tracks.size() == 2u);
  270. if (! tracks.empty())
  271. {
  272. EXPECT (tracks[0].name == "Vocal bus" && tracks[0].colour == 0xff3080c0u);
  273. EXPECT (std::abs (tracks[0].volume - 0.62f) < 1.0e-4f && std::abs (tracks[0].pan + 0.2f) < 1.0e-4f);
  274. EXPECT (tracks[0].effects.size() == 1u && tracks[0].effects[0].kind == EffectKind::limiter && tracks[0].effects[0].params.size() >= 3u);
  275. if (! tracks[0].effects.empty() && tracks[0].effects[0].params.size() >= 3u) // (the effect's own parameter count; the three set kept)
  276. EXPECT (std::abs (tracks[0].effects[0].params[0] - 0.5f) < 1.0e-4f && std::abs (tracks[0].effects[0].params[2] - 0.75f) < 1.0e-4f);
  277. EXPECT (tracks[0].sends.empty());
  278. }
  279. auto pts = payload (crossclipboard::Kind::points);
  280. pts.points.points = { { 0, 0.25f, 0.5f, 2 }, { 96, 1.0f, -0.3f, 0 } };
  281. pts.points.span = 96;
  282. const auto points = roundTrip (pts).points;
  283. EXPECT (points.span == 96 && points.points.size() == 2u);
  284. if (points.points.size() == 2u)
  285. EXPECT (points.points[0].mode == 2 && std::abs (points.points[1].tension + 0.3f) < 1.0e-5f && points.points[1].tick == 96);
  286. }
  287. void notOurs()
  288. {
  289. EXPECT (! crossclipboard::envelopeOf ("hello").has_value());
  290. EXPECT (! crossclipboard::envelopeOf ("").has_value());
  291. EXPECT (! crossclipboard::envelopeOf ("LYNX SHARE clipboard 9\nnotes x 1\n{}").has_value()); // (a newer format)
  292. EXPECT (! crossclipboard::envelopeOf ("LYNX SHARE clipboard 1\nsomething x 1\n{}").has_value());
  293. // an envelope whose document is not the one it names (half written, edited by hand)
  294. auto p = payload (crossclipboard::Kind::notes);
  295. p.notes = { Note {} };
  296. auto text = crossclipboard::clipboardText (p, sideFolder());
  297. auto envelope = crossclipboard::envelopeOf (text);
  298. EXPECT (envelope.has_value());
  299. if (envelope)
  300. {
  301. envelope->stamp = "other";
  302. EXPECT (! crossclipboard::open (*envelope).has_value());
  303. envelope->stamp = p.stamp;
  304. envelope->document = envelope->document.substring (0, envelope->document.length() / 2);
  305. EXPECT (! crossclipboard::open (*envelope).has_value());
  306. }
  307. EXPECT (! crossclipboard::fromJson ("{\"lynxClipboard\":2,\"kind\":\"notes\"}").has_value());
  308. EXPECT (crossclipboard::kindFromName ("mixerTracks") == crossclipboard::Kind::mixerTracks);
  309. }
  310. // Round 190qt (owner's option A): a project from an FL trial save (Song::flTrialSaved) keeps the mark through a .lynx
  311. // save and reopen; a project without it writes no key (older .lynx files and readers unchanged); clips and channels
  312. // copied to another window carry it in their fragment, mixer track states on the payload
  313. void trialMark190qt()
  314. {
  315. auto song = source();
  316. EXPECT (! ProjectFile::toJson (song).contains ("flTrialSaved"));
  317. juce::String error;
  318. auto back = ProjectFile::fromJson (ProjectFile::toJson (song), error);
  319. EXPECT (back.has_value() && ! back->flTrialSaved);
  320. song.flTrialSaved = true;
  321. back = ProjectFile::fromJson (ProjectFile::toJson (song), error);
  322. EXPECT (back.has_value() && back->flTrialSaved);
  323. if (back) // saved again after the reopen
  324. {
  325. const auto twice = ProjectFile::fromJson (ProjectFile::toJson (*back), error);
  326. EXPECT (twice.has_value() && twice->flTrialSaved);
  327. }
  328. auto c = payload (crossclipboard::Kind::clips);
  329. c.fragment = crosspaste::clipsFragment (song, song.playlist);
  330. const auto clipsBack = roundTrip (c);
  331. EXPECT (clipsBack.fragment.flTrialSaved);
  332. auto other = Song::makeDefault();
  333. EXPECT (! crosspaste::importClips (other, clipsBack.fragment).empty() && other.flTrialSaved);
  334. auto ch = payload (crossclipboard::Kind::channels);
  335. ch.fragment = crosspaste::channelsFragment (song, { 0 });
  336. const auto channelsBack = roundTrip (ch, 16); // (through the side file)
  337. EXPECT (channelsBack.fragment.flTrialSaved);
  338. auto m = payload (crossclipboard::Kind::mixerTracks);
  339. m.mixerTracks = { song.mixer.size() > 1 ? song.mixer[1] : MixerTrack {} };
  340. EXPECT (! roundTrip (m).flTrialSaved);
  341. m.flTrialSaved = true;
  342. EXPECT (roundTrip (m).flTrialSaved);
  343. }
  344. } // namespace
  345. int main()
  346. {
  347. sideFolder().deleteRecursively();
  348. notes();
  349. clips();
  350. channelsThroughTheSideFile();
  351. stepsMixerAndPoints();
  352. notOurs();
  353. trialMark190qt();
  354. sideFolder().deleteRecursively();
  355. std::printf ("cross-window clipboard tests: %d checks, %d failures\n", checks, failures);
  356. return failures == 0 ? 0 : 1;
  357. }