Focus218Shot190ng.inc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // --focus218shot190ng <prefix> (round 190ng, issue #218 "Stuck inside Pattern toolbar icon"): the main window on the
  2. // desktop (Xvfb or a real display), clicks sent through its window (peer): the toolbar's Pattern and snap boxes never
  3. // keep the PC keyboard, any drop-down or button of the main window hands it back (ui/KeyFocus.h), Esc gives it back,
  4. // dialogs keep their own keyboard navigation. <prefix>-checks.txt lists the checks; pictures @100 and @150.
  5. // Included in the app class of Main.cpp.
  6. bool runFocus218Shot190ng (const juce::String& prefix)
  7. {
  8. juce::LookAndFeel::setDefaultLookAndFeel (&lookAndFeel);
  9. const juce::ScopedValueSetter<bool> shown (lynx::ui::showingForShots(), true);
  10. juce::String checks;
  11. bool allOk = true;
  12. const auto check = [&checks, &allOk] (const juce::String& what, bool ok) {
  13. checks << (ok ? "ok " : "FAIL ") << what << "\n";
  14. allOk = allOk && ok;
  15. };
  16. const auto line = [&checks] (const juce::String& what) { checks << " " << what << "\n"; };
  17. using namespace lynx;
  18. AudioEngine offline (false);
  19. MainComponent ui (offline, false);
  20. ui.setSize (1600, 900);
  21. ui.setVisible (true);
  22. ui.addToDesktop (juce::ComponentPeer::windowAppearsOnTaskbar);
  23. ui.toFront (true);
  24. offline.dispatchPendingMessages();
  25. auto* peer = ui.getPeer();
  26. check ("the main window is on the desktop", peer != nullptr && ui.isShowing());
  27. if (peer == nullptr)
  28. {
  29. juce::File (prefix + "-checks.txt").replaceWithText (checks + "result=FAIL\n");
  30. return false;
  31. }
  32. auto t = juce::Time::currentTimeMillis();
  33. const auto click = [&] (juce::Component& c, juce::Point<int> p) { // press + release through the window, as a real click
  34. const auto at = peer->globalToLocal (c.localPointToGlobal (p.toFloat()));
  35. peer->handleMouseEvent (juce::MouseInputSource::InputSourceType::mouse, at, {}, 1.0f, 0.0f, t += 40, {}, 0);
  36. peer->handleMouseEvent (juce::MouseInputSource::InputSourceType::mouse, at, juce::ModifierKeys (juce::ModifierKeys::leftButtonModifier),
  37. 1.0f, 0.0f, t += 40, {}, 0);
  38. peer->handleMouseEvent (juce::MouseInputSource::InputSourceType::mouse, at, {}, 1.0f, 0.0f, t += 60, {}, 0);
  39. };
  40. const auto focused = [] { return juce::Component::getCurrentlyFocusedComponent(); };
  41. const auto name = [] (juce::Component* c) {
  42. return c == nullptr ? juce::String ("nothing") : juce::String (typeid (*c).name()) + " '" + c->getName() + "'";
  43. };
  44. ui.grabKeyboardFocus();
  45. const auto displayFocus = ui.hasKeyboardFocus (false);
  46. line ("display focus: " + juce::String (displayFocus ? "yes" : "no (the window manager gives none: focus checks below use JUCE's own state)"));
  47. auto& patternBox = ui.patternBoxForTest();
  48. auto& gridBox = ui.gridBoxForTest();
  49. check ("the Pattern box doesn't want the keyboard", ! patternBox.getWantsKeyboardFocus() && ! patternBox.getMouseClickGrabsKeyboardFocus());
  50. check ("the snap box doesn't want the keyboard", ! gridBox.getWantsKeyboardFocus() && ! gridBox.getMouseClickGrabsKeyboardFocus());
  51. // the issue's steps: a click on the Pattern box (its list opens), a pick, then Enter
  52. if (patternBox.isShowing() && displayFocus)
  53. {
  54. click (patternBox, patternBox.getLocalBounds().getCentre());
  55. line ("after the click: focus on " + name (focused()) + ", list open: " + juce::String (patternBox.isPopupActive() ? "yes" : "no"));
  56. juce::PopupMenu::dismissAllActiveMenus();
  57. patternBox.hidePopup();
  58. check ("after the click and the list: the Pattern box doesn't hold the keyboard", ! patternBox.hasKeyboardFocus (true));
  59. check ("after the click: the main window keeps the keyboard", ui.hasKeyboardFocus (true) && focused() != &patternBox);
  60. ui.keyPressed (juce::KeyPress (juce::KeyPress::returnKey));
  61. check ("Enter afterwards doesn't open the Pattern list again", ! patternBox.isPopupActive());
  62. juce::PopupMenu::dismissAllActiveMenus();
  63. patternBox.grabKeyboardFocus();
  64. check ("even asked directly, the Pattern box doesn't take the keyboard", ! patternBox.hasKeyboardFocus (true));
  65. gridBox.grabKeyboardFocus();
  66. check ("nor does the snap box", ! gridBox.hasKeyboardFocus (true));
  67. }
  68. else
  69. line ("Pattern box clicks skipped (not showing in this layout or no display focus)");
  70. // any other drop-down or button of the main window that gets the keyboard (as the box did) hands it back
  71. juce::ComboBox box ("190ng box");
  72. box.addItem ("A", 1);
  73. box.addItem ("B", 2);
  74. juce::TextButton button ("190ng button");
  75. juce::TextEditor text ("190ng text");
  76. for (auto* c : std::initializer_list<juce::Component*> { &box, &button, &text })
  77. {
  78. ui.addAndMakeVisible (c);
  79. c->setBounds (10, 200, 120, 24);
  80. }
  81. check ("rules: a drop-down of the main window gives the keyboard back", ui::keyfocus::shouldGiveBack (&box, ui));
  82. check ("rules: a button of the main window gives the keyboard back", ui::keyfocus::shouldGiveBack (&button, ui));
  83. check ("rules: a text field keeps it", ! ui::keyfocus::shouldGiveBack (&text, ui));
  84. if (displayFocus)
  85. {
  86. box.grabKeyboardFocus();
  87. const auto took = box.hasKeyboardFocus (false);
  88. ui::keyfocus::giveBackNow (ui);
  89. check ("a focused drop-down: the main window takes the keyboard back", took && ui.hasKeyboardFocus (false));
  90. button.grabKeyboardFocus();
  91. ui::keyfocus::giveBackNow (ui);
  92. check ("a focused button: the main window takes the keyboard back", ui.hasKeyboardFocus (false));
  93. box.grabKeyboardFocus();
  94. const auto used = ui.keyPressed (juce::KeyPress (juce::KeyPress::escapeKey));
  95. check ("Esc while a drop-down holds the keyboard gives it back (and is used up)", used && ui.hasKeyboardFocus (false));
  96. text.grabKeyboardFocus();
  97. ui::keyfocus::giveBackNow (ui);
  98. check ("a text field keeps the keyboard (typing)", text.hasKeyboardFocus (false));
  99. ui.grabKeyboardFocus();
  100. }
  101. for (auto* c : std::initializer_list<juce::Component*> { &box, &button, &text })
  102. ui.removeChildComponent (c);
  103. // dialogs and other windows keep their own keyboard navigation
  104. {
  105. juce::DocumentWindow dialog ("190ng dialog", juce::Colours::black, 0);
  106. juce::ComboBox inDialog ("in dialog");
  107. inDialog.setSize (120, 24);
  108. dialog.setContentNonOwned (&inDialog, true);
  109. check ("rules: a drop-down in a dialog keeps the keyboard", ! ui::keyfocus::shouldGiveBack (&inDialog, ui));
  110. dialog.getProperties().set ("lynxPanel", true);
  111. check ("rules: a drop-down in a panel window (detached Mixer...) gives it back", ui::keyfocus::shouldGiveBack (&inDialog, ui));
  112. juce::ComboBox typed ("typed");
  113. typed.setEditableText (true);
  114. ui.addChildComponent (typed);
  115. check ("rules: a drop-down one types into keeps the keyboard", ! ui::keyfocus::shouldGiveBack (&typed, ui));
  116. ui.removeChildComponent (&typed);
  117. }
  118. // pictures: the toolbar around the Pattern box after the click (FL draws no focus frame there)
  119. if (patternBox.isShowing())
  120. {
  121. const auto area = ui.getLocalArea (&patternBox, patternBox.getLocalBounds()).expanded (180, 14).getIntersection (ui.getLocalBounds());
  122. savePng (ui.createComponentSnapshot (area, true, 1.0f), prefix + "-toolbar-pattern-after-click.png");
  123. savePng (ui.createComponentSnapshot (area, true, 1.5f), prefix + "-toolbar-pattern-after-click@150.png");
  124. }
  125. ui.removeFromDesktop();
  126. juce::File (prefix + "-checks.txt").replaceWithText (checks + "result=" + (allOk ? "OK" : "FAIL") + "\n");
  127. return allOk;
  128. }