|
| 1 | +// Menu item clicks must reach their listener before Menu::Open() returns. |
| 2 | +// |
| 3 | +// The native Windows backend used to let TrackPopupMenu post WM_COMMAND, which the |
| 4 | +// host's message loop dispatches after Open() has returned; bindings whose callbacks |
| 5 | +// are only valid inside the call (Dart's NativeCallable.isolateLocal) cannot receive |
| 6 | +// an event that late. |
| 7 | +// |
| 8 | +// Without arguments this is a headless no-op, so CTest can run it. With a round name |
| 9 | +// it opens a real menu and waits for a driver to work it with the mouse: |
| 10 | +// tools/gui/core_menu_backend_test.ps1 in the libnativeapi workspace. |
| 11 | +// |
| 12 | +// menu_click_test pick_top_level driver clicks "Alpha" |
| 13 | +// menu_click_test dismiss driver clicks the window, clear of the menu |
| 14 | +// menu_click_test pick_submenu driver opens "More" and clicks "Beta" |
| 15 | +// |
| 16 | +// The driver creates go.flag in the working directory once it has activated the |
| 17 | +// window, so that no activation click can race the menu open. |
| 18 | +#include <windows.h> |
| 19 | +#include <iostream> |
| 20 | +#include <memory> |
| 21 | +#include <string> |
| 22 | +#include "nativeapi.h" |
| 23 | + |
| 24 | +using namespace nativeapi; |
| 25 | + |
| 26 | +namespace { |
| 27 | + |
| 28 | +int alpha_clicks = 0, beta_clicks = 0, alpha_inside = 0, beta_inside = 0; |
| 29 | +int opened = 0, closed = 0; |
| 30 | +bool inside_open = false; |
| 31 | +int failures = 0; |
| 32 | + |
| 33 | +void Pump(int ms) { |
| 34 | + const ULONGLONG end = GetTickCount64() + ms; |
| 35 | + MSG msg; |
| 36 | + while (GetTickCount64() < end) { |
| 37 | + while (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE)) { |
| 38 | + TranslateMessage(&msg); |
| 39 | + DispatchMessage(&msg); |
| 40 | + } |
| 41 | + Sleep(10); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +bool Check(bool condition, const std::string& what) { |
| 46 | + if (!condition) { |
| 47 | + ++failures; |
| 48 | + std::cout << "FAIL " << what << std::endl; |
| 49 | + } else { |
| 50 | + std::cout << "PASS " << what << std::endl; |
| 51 | + } |
| 52 | + return condition; |
| 53 | +} |
| 54 | + |
| 55 | +} // namespace |
| 56 | + |
| 57 | +int main(int argc, char** argv) { |
| 58 | + if (argc <= 1) return 0; // headless: nothing to drive the menu with |
| 59 | + const std::string round = argv[1]; |
| 60 | + if (round != "pick_top_level" && round != "dismiss" && round != "pick_submenu") { |
| 61 | + std::cerr << "unknown round: " << round << '\n'; |
| 62 | + return 2; |
| 63 | + } |
| 64 | + |
| 65 | + Application::GetInstance(); |
| 66 | + |
| 67 | + // A window of our own: the menu behaves like it does in an app only when its owner |
| 68 | + // is the foreground window, and the driver needs something safe to click on. |
| 69 | + WNDCLASSW wc = {}; |
| 70 | + wc.lpfnWndProc = DefWindowProcW; |
| 71 | + wc.hInstance = GetModuleHandleW(nullptr); |
| 72 | + wc.hCursor = LoadCursorW(nullptr, reinterpret_cast<LPCWSTR>(IDC_ARROW)); |
| 73 | + wc.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOW + 1); |
| 74 | + wc.lpszClassName = L"NativeApiMenuClickTest"; |
| 75 | + RegisterClassW(&wc); |
| 76 | + HWND host = CreateWindowExW(0, wc.lpszClassName, L"nativeapi menu click test", |
| 77 | + WS_OVERLAPPEDWINDOW | WS_VISIBLE, 200, 200, 700, 520, nullptr, |
| 78 | + nullptr, wc.hInstance, nullptr); |
| 79 | + if (!host) { |
| 80 | + std::cerr << "could not create the test window\n"; |
| 81 | + return 2; |
| 82 | + } |
| 83 | + std::cout << "READY" << std::endl; |
| 84 | + |
| 85 | + Menu menu; |
| 86 | + if (!Check(menu.SetBackend(MenuBackend::Native), "native backend selected")) return 1; |
| 87 | + auto alpha = std::make_shared<MenuItem>("Alpha"); |
| 88 | + menu.AddItem(alpha); |
| 89 | + auto submenu = std::make_shared<Menu>(); |
| 90 | + auto beta = std::make_shared<MenuItem>("Beta"); |
| 91 | + submenu->AddItem(beta); |
| 92 | + auto more = std::make_shared<MenuItem>("More", MenuItemType::Submenu); |
| 93 | + more->SetSubmenu(submenu); |
| 94 | + menu.AddItem(more); |
| 95 | + |
| 96 | + alpha->AddListener<MenuItemClickedEvent>([&](const MenuItemClickedEvent&) { |
| 97 | + ++alpha_clicks; |
| 98 | + if (inside_open) ++alpha_inside; |
| 99 | + }); |
| 100 | + beta->AddListener<MenuItemClickedEvent>([&](const MenuItemClickedEvent&) { |
| 101 | + ++beta_clicks; |
| 102 | + if (inside_open) ++beta_inside; |
| 103 | + }); |
| 104 | + menu.AddListener<MenuOpenedEvent>([&](const MenuOpenedEvent&) { ++opened; }); |
| 105 | + menu.AddListener<MenuClosedEvent>([&](const MenuClosedEvent&) { ++closed; }); |
| 106 | + |
| 107 | + // Wait for the driver's go flag; it activates the window first. |
| 108 | + const ULONGLONG deadline = GetTickCount64() + 60000; |
| 109 | + while (GetFileAttributesA("go.flag") == INVALID_FILE_ATTRIBUTES && |
| 110 | + GetTickCount64() < deadline) { |
| 111 | + Pump(200); |
| 112 | + } |
| 113 | + Pump(300); |
| 114 | + const bool foreground = GetForegroundWindow() == host; |
| 115 | + std::cout << "GO foreground=" << foreground << std::endl; |
| 116 | + // A popup menu whose owner is not the foreground window ignores mouse input and |
| 117 | + // never closes, which would hang Open(): say so rather than time out silently. |
| 118 | + if (!Check(foreground, "test window is in the foreground")) return 1; |
| 119 | + |
| 120 | + inside_open = true; |
| 121 | + const bool returned = menu.Open(PositioningStrategy::Absolute({300, 300})); |
| 122 | + inside_open = false; |
| 123 | + |
| 124 | + Check(returned, "Open() reported success"); |
| 125 | + Check(opened == 1 && closed == 1, "one opened and one closed event"); |
| 126 | + if (round == "pick_top_level") { |
| 127 | + Check(alpha_inside == 1, "the click reached the listener before Open() returned"); |
| 128 | + Check(beta_clicks == 0, "no other item fired"); |
| 129 | + } else if (round == "pick_submenu") { |
| 130 | + Check(beta_inside == 1, "the submenu click reached the listener before Open() returned"); |
| 131 | + Check(alpha_clicks == 0, "no other item fired"); |
| 132 | + } else { |
| 133 | + Check(alpha_clicks == 0 && beta_clicks == 0, "dismissing fires no click"); |
| 134 | + } |
| 135 | + |
| 136 | + const int before = alpha_clicks + beta_clicks; |
| 137 | + Pump(1500); // a posted WM_COMMAND would be dispatched in here |
| 138 | + Check(alpha_clicks + beta_clicks == before, "no second click arrives after Open() returned"); |
| 139 | + |
| 140 | + DestroyWindow(host); |
| 141 | + std::cout << (failures ? "FAILED" : "OK") << std::endl; |
| 142 | + return failures ? 1 : 0; |
| 143 | +} |
0 commit comments