MenuWindows190skCheck.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. // Round 190sk (Discord norenkae 11-28: "export to DAWproject does nothing in lynx"): File › Export › "DAWproject
  2. // (.dawproject)..." opened nothing in 0.1.31.x and the 0.1.32 rollup: its chooser was a local std::shared_ptr its own
  3. // callback did not keep, so it died (and its dialog with it) when the menu action returned. "Save a copy as FL Studio
  4. // Mobile project (.flm)..." had the same fault behind its first window. The selftests wrote both files without the
  5. // menu, so nothing saw it. This check drives the real menu (MainComponent::getMenuForIndex, the items' own actions)
  6. // with JUCE's built-in chooser (LYNX_FILE_DIALOG=builtin):
  7. // 1. every item of File, File › New from template, Open in new window, Import and Export whose name ends in "..."
  8. // (FL's sign for "opens a window") opens one: a modal component, a window on the desktop or a panel in the main
  9. // window that was not there before, still there after the message loop ran; then it is cancelled;
  10. // 2. the export items whose first window leads to a chooser (FLM's losses window, Export to FL Studio, Ableton Live
  11. // set, Save a copy for an older LYNX) reach their chooser;
  12. // 3. DAWproject end to end: the menu item, the chooser given a file name, the file written on the worker (a zip with
  13. // project.xml and metadata.xml) and the report window; FLM the same way.
  14. // Included in the app class of Main.cpp; --menuwindows190sktest <report> and --selftest "menuwindows190sk". The windows
  15. // show on the desktop for a moment: on Windows it runs only on CI or with LYNX_MENUSWEEP=1 (the owner's screen).
  16. juce::String menuWindows190skCheck (juce::String& log)
  17. {
  18. using namespace lynx;
  19. juce::String result = "OK";
  20. const auto check = [&] (const juce::String& what, bool ok, const juce::String& detail = {}) {
  21. log << (ok ? "ok " : "FAIL ") << what << (detail.isNotEmpty() ? " (" + detail + ")" : juce::String()) << "\n";
  22. if (! ok && result == "OK")
  23. result = "ERROR " + what;
  24. };
  25. #if JUCE_MAC
  26. log << "skipped on macOS (the selftest has no message pump there)\n";
  27. return result;
  28. #else
  29. #if JUCE_WINDOWS
  30. if (juce::SystemStats::getEnvironmentVariable ("CI", {}).isEmpty() && juce::SystemStats::getEnvironmentVariable ("GITHUB_ACTIONS", {}).isEmpty()
  31. && juce::SystemStats::getEnvironmentVariable ("LYNX_MENUSWEEP", {}) != "1")
  32. {
  33. log << "skipped on this Windows desktop (windows would show on the owner's screen): LYNX_MENUSWEEP=1 runs it\n";
  34. return result;
  35. }
  36. #endif
  37. const auto setEnv = [] (const char* name, const juce::String& value) {
  38. #if JUCE_WINDOWS
  39. _putenv_s (name, value.toRawUTF8());
  40. #else
  41. if (value.isEmpty())
  42. unsetenv (name);
  43. else
  44. setenv (name, value.toRawUTF8(), 1);
  45. #endif
  46. };
  47. const auto oldDialog = juce::SystemStats::getEnvironmentVariable ("LYNX_FILE_DIALOG", {});
  48. setEnv ("LYNX_FILE_DIALOG", "builtin");
  49. const auto root = juce::File::getSpecialLocation (juce::File::tempDirectory).getChildFile ("lynx-menu190sk-" + juce::String (juce::Random::getSystemRandom().nextInt64()));
  50. root.createDirectory();
  51. {
  52. AudioEngine offline (false);
  53. MainComponent ui (offline, false);
  54. ui.setSize (1600, 900);
  55. ui.setVisible (true);
  56. const auto settle = [&offline] {
  57. for (int k = 0; k < 4; ++k)
  58. offline.dispatchPendingMessages();
  59. for (int i = 0; i < 256 && juce::detail::dispatchNextMessageOnSystemQueue (true); ++i) {}
  60. };
  61. const auto pump = [&settle] (int ms) {
  62. const auto end = juce::Time::getMillisecondCounter() + (juce::uint32) ms;
  63. while (juce::Time::getMillisecondCounter() < end)
  64. {
  65. settle();
  66. juce::Thread::sleep (10);
  67. }
  68. };
  69. // a short song: a pattern of four notes on the first channel, once on the playlist
  70. offline.editSong ([] (Song& s) {
  71. if (s.channels.empty())
  72. return;
  73. Pattern p;
  74. p.name = "Pattern 1";
  75. for (int i = 0; i < 4; ++i)
  76. {
  77. Note n;
  78. n.channel = 0;
  79. n.key = 60 + i * 2;
  80. n.start = i * ppq;
  81. n.length = ppq / 2;
  82. p.notes.push_back (n);
  83. }
  84. s.patterns = { p };
  85. Clip c;
  86. c.kind = ClipKind::pattern;
  87. c.pattern = 0;
  88. c.track = 0;
  89. c.start = 0;
  90. c.length = 4 * ppq;
  91. s.playlist = { c };
  92. s.mode = PlayMode::song;
  93. });
  94. pump (300);
  95. auto* modal = juce::ModalComponentManager::getInstance();
  96. const auto desktop = [] {
  97. std::set<juce::Component*> s;
  98. auto& d = juce::Desktop::getInstance();
  99. for (int i = 0; i < d.getNumComponents(); ++i)
  100. if (auto* c = d.getComponent (i); c != nullptr && c->isVisible())
  101. s.insert (c);
  102. return s;
  103. };
  104. const auto panels = [&ui] {
  105. std::set<juce::Component*> s;
  106. for (auto* c : ui.getChildren())
  107. if (c->isVisible() && c->getWidth() >= 120 && c->getHeight() >= 60)
  108. s.insert (c);
  109. return s;
  110. };
  111. const auto nameOf = [] (juce::Component* c) {
  112. if (auto* w = dynamic_cast<juce::TopLevelWindow*> (c); w != nullptr && w->getName().isNotEmpty())
  113. return w->getName();
  114. return c->getName().isNotEmpty() ? c->getName() : juce::String ("unnamed");
  115. };
  116. std::function<juce::Component* (juce::Component*, const std::function<bool (juce::Component*)>&)> find;
  117. find = [&find] (juce::Component* in, const std::function<bool (juce::Component*)>& pred) -> juce::Component* {
  118. if (in == nullptr)
  119. return nullptr;
  120. if (pred (in))
  121. return in;
  122. for (auto* c : in->getChildren())
  123. if (auto* f = find (c, pred))
  124. return f;
  125. return nullptr;
  126. };
  127. const auto chooserBox = [modal]() -> juce::FileChooserDialogBox* {
  128. for (int i = 0; i < modal->getNumModalComponents(); ++i)
  129. if (auto* b = dynamic_cast<juce::FileChooserDialogBox*> (modal->getModalComponent (i)); b != nullptr && b->isVisible())
  130. return b;
  131. return nullptr;
  132. };
  133. const auto baseModal = modal->getNumModalComponents();
  134. const auto baseDesktop = desktop();
  135. const auto basePanels = panels();
  136. // closes whatever the item opened: Cancel (result 0) on every modal window, the close button of new windows,
  137. // new panels of the main window hidden
  138. const auto closeAll = [&] {
  139. for (int round = 0; round < 6; ++round)
  140. {
  141. modal->cancelAllModalComponents();
  142. pump (120);
  143. for (auto* c : desktop())
  144. if (baseDesktop.count (c) == 0)
  145. {
  146. if (auto* w = dynamic_cast<juce::DocumentWindow*> (c))
  147. w->closeButtonPressed();
  148. else
  149. c->setVisible (false);
  150. }
  151. for (auto* c : panels())
  152. if (basePanels.count (c) == 0)
  153. c->setVisible (false);
  154. pump (80);
  155. if (modal->getNumModalComponents() <= baseModal && desktop().size() <= baseDesktop.size())
  156. return true;
  157. }
  158. return false;
  159. };
  160. // what opened since the baseline ("" = nothing)
  161. const auto opened = [&]() -> juce::String {
  162. juce::StringArray what;
  163. if (modal->getNumModalComponents() > baseModal)
  164. for (int i = 0; i < modal->getNumModalComponents() - baseModal; ++i)
  165. if (auto* c = modal->getModalComponent (i))
  166. what.add ("modal " + nameOf (c));
  167. for (auto* c : desktop())
  168. if (baseDesktop.count (c) == 0)
  169. what.addIfNotAlreadyThere ("window " + nameOf (c));
  170. for (auto* c : panels())
  171. if (basePanels.count (c) == 0)
  172. what.add ("panel " + nameOf (c));
  173. return what.joinIntoString (", ");
  174. };
  175. // the menu's items: (path, action) for every "..." item of the submenus named here
  176. struct Entry
  177. {
  178. juce::String path;
  179. std::function<void()> action;
  180. };
  181. const auto bare = [] (const juce::String& text) { return text.upToFirstOccurrenceOf ("\t", false, false).trim(); };
  182. const auto collect = [&] {
  183. std::vector<Entry> out;
  184. const auto menu = ui.menuForCheck (0, "File");
  185. const juce::StringArray walk { "New from template", "Open in new window", "Import", "Export" };
  186. std::function<void (const juce::PopupMenu&, const juce::String&)> add = [&] (const juce::PopupMenu& m, const juce::String& prefix) {
  187. for (juce::PopupMenu::MenuItemIterator it (m); it.next();)
  188. {
  189. const auto& item = it.getItem();
  190. const auto text = bare (item.text);
  191. if (item.subMenu != nullptr)
  192. {
  193. if (walk.contains (text))
  194. add (*item.subMenu, prefix + text + " > ");
  195. continue;
  196. }
  197. if (item.isEnabled && item.action && (text.endsWith ("...") || text.endsWith (juce::String::fromUTF8 ("\xe2\x80\xa6"))))
  198. out.push_back ({ prefix + text, item.action });
  199. }
  200. };
  201. add (menu, "File > ");
  202. return out;
  203. };
  204. const auto entries = collect();
  205. check ("the File menu lists its '...' items", entries.size() >= 25, juce::String ((int) entries.size()));
  206. for (const auto* must : { "File > Export > DAWproject (.dawproject)...", "File > Save a copy as FL Studio Mobile project (.flm)...",
  207. "File > Export > Ableton Live set (.als)...", "File > Export > FL Studio project...", "File > Export > Wave file...",
  208. "File > Open...", "File > Save as..." })
  209. check (juce::String ("listed: ") + must, std::any_of (entries.begin(), entries.end(), [must] (const Entry& e) { return e.path == must; }));
  210. // 1. every "..." item opens its window, and it is still open after the message loop ran
  211. // (the song counts as saved: "New project..." and "Open..." then open their own window, not "save changes?")
  212. for (const auto& e : entries)
  213. {
  214. offline.markSaved();
  215. e.action();
  216. pump (250);
  217. const auto what = opened();
  218. check ("opens its window: " + e.path, what.isNotEmpty(), what);
  219. check ("and it closes again: " + e.path, closeAll());
  220. }
  221. // 2. the items whose first window leads to a chooser reach it (Export... / OK)
  222. const auto byPath = [&entries] (const juce::String& path) -> std::function<void()> {
  223. for (const auto& e : entries)
  224. if (e.path == path)
  225. return e.action;
  226. return {};
  227. };
  228. for (const auto* path : { "File > Save a copy as FL Studio Mobile project (.flm)...", "File > Export > FL Studio project...",
  229. "File > Export > Ableton Live set (.als)...", "File > Save a copy for an older LYNX..." })
  230. {
  231. const auto action = byPath (path);
  232. if (! action)
  233. continue;
  234. action();
  235. pump (250);
  236. juce::Component* first = modal->getNumModalComponents() > baseModal ? modal->getModalComponent (0) : nullptr;
  237. auto* exportButton = dynamic_cast<juce::Button*> (find (first, [] (juce::Component* c) {
  238. auto* b = dynamic_cast<juce::TextButton*> (c);
  239. return b != nullptr && b->isEnabled() && b->getButtonText().startsWith ("Export");
  240. }));
  241. if (exportButton != nullptr)
  242. exportButton->triggerClick();
  243. else if (first != nullptr)
  244. first->exitModalState (1); // (an AlertWindow: its OK / Save button)
  245. pump (300);
  246. check (juce::String ("reaches its chooser: ") + path, chooserBox() != nullptr, opened());
  247. closeAll();
  248. }
  249. // 3. end to end through the menu: the chooser given a name, the file written, the report shown
  250. const auto throughChooser = [&] (const juce::String& path, const juce::String& fileName, bool alertFirst, const juce::String& reportTitle) -> juce::File {
  251. const auto action = byPath (path);
  252. if (! action)
  253. return {};
  254. action();
  255. pump (250);
  256. if (alertFirst && modal->getNumModalComponents() > baseModal)
  257. {
  258. modal->getModalComponent (0)->exitModalState (1);
  259. pump (250);
  260. }
  261. auto* box = chooserBox();
  262. check ("the chooser is open: " + path, box != nullptr, opened());
  263. auto* browser = dynamic_cast<juce::FileBrowserComponent*> (find (box, [] (juce::Component* c) { return dynamic_cast<juce::FileBrowserComponent*> (c) != nullptr; }));
  264. if (box == nullptr || browser == nullptr)
  265. {
  266. closeAll();
  267. return {};
  268. }
  269. browser->setRoot (root);
  270. browser->setFileName (fileName);
  271. pump (100);
  272. box->exitModalState (1); // (the chooser's Save button)
  273. const auto target = root.getChildFile (fileName);
  274. const auto end = juce::Time::getMillisecondCounter() + 60000u;
  275. // done: the file is there and nothing is still being written (DAWproject: its report window, "Export
  276. // DAWproject"; FLM: the saved-file hint, no window)
  277. bool done = false;
  278. juce::String report;
  279. while (juce::Time::getMillisecondCounter() < end && ! done)
  280. {
  281. pump (100);
  282. for (int i = 0; i < modal->getNumModalComponents(); ++i)
  283. if (auto* a = dynamic_cast<juce::AlertWindow*> (modal->getModalComponent (i)))
  284. report = a->getName();
  285. done = target.existsAsFile() && (reportTitle.isEmpty() || report == reportTitle);
  286. }
  287. check ("done through the menu: " + path, done, report.isNotEmpty() ? "report window: " + report : opened());
  288. closeAll();
  289. return target;
  290. };
  291. const auto daw = throughChooser ("File > Export > DAWproject (.dawproject)...", "menu190sk.dawproject", false, tr ("Export DAWproject"));
  292. bool zipOk = false;
  293. if (daw.existsAsFile())
  294. {
  295. juce::ZipFile zip (daw);
  296. zipOk = zip.getIndexOfFileName ("project.xml") >= 0 && zip.getIndexOfFileName ("metadata.xml") >= 0;
  297. if (zipOk)
  298. {
  299. std::unique_ptr<juce::InputStream> in (zip.createStreamForEntry (zip.getIndexOfFileName ("project.xml")));
  300. const auto xml = in != nullptr ? in->readEntireStreamAsString() : juce::String();
  301. zipOk = xml.contains ("<Project") && xml.contains ("<Notes") && xml.contains ("<Note ");
  302. }
  303. }
  304. check ("File > Export > DAWproject writes the file through the menu (a zip: project.xml with the notes, metadata.xml)", zipOk,
  305. daw.getFullPathName() + (daw.existsAsFile() ? " " + juce::String (daw.getSize()) + " bytes" : " missing"));
  306. const auto flm = throughChooser ("File > Save a copy as FL Studio Mobile project (.flm)...", "menu190sk.flm", true, {});
  307. check ("File > Save a copy as FL Studio Mobile project writes the file through the menu", flm.existsAsFile() && flm.getSize() > 0,
  308. flm.getFullPathName());
  309. closeAll();
  310. }
  311. root.deleteRecursively();
  312. setEnv ("LYNX_FILE_DIALOG", oldDialog);
  313. return result;
  314. #endif
  315. }