Files
lightgui/tests/widget_tests.cpp
cnmcdee fbfab38e41 Designer: poll shared XFrame selection, editable ComboBox/Listbox items, Scrollbar adjustment, Text text, Memo preview, border props
- Placement hole: the designer only set g_xframe_selected from its own
  combined palette, so a widget chosen in the separate XFrame palette
  never placed (on_canvas_press saw an empty selection). Poll the
  shared selection file (same path rule as xframe_selection_path) on
  the 200ms timer and at the start of on_canvas_press, so either
  palette works. Verified under openbox: separate-palette flow now
  places (GtkButton 0->1), combined-palette flow still places (1->2).
- ComboBox/Listbox: new editable items prop (pipe-separated, default
  Item 1|Item 2|Item 3); preview rebuilds from it, generator emits one
  append/insert per item. active / selection-mode unchanged.
- Scrollbar: editable lower/upper/step-increment/page-increment/
  page-size; preview reconfigures the adjustment, generator emits them.
  value/inverted unchanged; defaults preserve 0-100/1/10/10.
- Text: new text prop (same as Memo); preview and generator use it,
  falling back to the widget name when empty.
- Memo: preview now uses the text prop (was hardcoded to the name);
  added cursor-visible (same as Text).
- Border (GTK): border-width/border-color/border-radius in the common
  prop set; preview and generator emit border + border-radius CSS.
  Defaults (0/empty/0) preserve the current appearance.
- Tests: SELFTEST 35 items, widget_tests 58 passed, widget_tests.sh
  5 passed. Border CSS verified in generated code; XFrame border
  verified in the layout (1072 red pixels, state border=3 #ff0000 10).
2026-11-29 19:53:40 +00:00

151 lines
6.1 KiB
C++

// Widget tests: one project holding every Lazarus-style widget with
// non-default properties; generate it, check per-widget markers in the
// emitted code, and report PASS/FAIL per widget. The companion shell
// script (tests/widget_tests.sh) compiles the generated app and runs it.
// Usage: widget_tests [out_dir]
#include <cstdio>
#include <filesystem>
#include <fstream>
#include <map>
#include <string>
#include <vector>
#include "../src/generator.h"
#include "../src/project_io.h"
#include "../src/props.h"
struct Spec {
const char* type;
const char* prop;
const char* value;
const char* marker; // must appear in <Name>.cpp
};
static const Spec kSpecs[] = {
{"Memo", "text", "Hello memo", "gtk_text_buffer_set_text"},
{"GroupBox", "label", "Options", "gtk_frame_new"},
{"Panel", "label", "Panel A", "gtk_frame_new"},
{"Image", "file", "chart_pro_1024x768.png", "gtk_image_set_from_file"},
{"Shape", "shape", "1", "cairo_arc"},
{"Notebook", "pages", "One|Two|Three", "gtk_notebook_append_page"},
{"ScrollBox", "label", "Scroll me", "gtk_scrolled_window_new"},
{"Splitter", "position", "150", "gtk_paned_new"},
{"StatusBar", "text", "All good", "gtk_statusbar_push"},
{"ToolBar", "items", "Cut|Copy|Paste", "gtk_tool_button_new"},
{"MenuBar", "menus", "File|Tools", "gtk_menu_item_new_with_label"},
{"SpinEdit", "value", "42", "gtk_spin_button_new_with_range"},
{"TrackBar", "value", "70", "gtk_scale_new_with_range"},
{"ProgressBar", "fraction", "0.5", "gtk_progress_bar_new"},
{"DateTimePicker", "date", "2026-11-25", "gtk_calendar_select_day"},
{"ColorBox", "color", "#00ff00", "gtk_color_chooser_set_rgba"},
{"ListView", "items", "x,1;y,2", "gtk_list_store_new"},
{"TreeView", "items", "Root/Leaf;Other", "gtk_tree_store_new"},
{"StringGrid", "rows", "3", "gtk_list_store_new"},
{"Plot", "title", "Sine", "sin("},
{"Combobox", "items", "Red|Green|Blue", "Red"},
{"Combobox", "active", "1", "Item 1"},
{"Listbox", "items", "Alpha|Beta|Gamma", "Alpha"},
{"Listbox", "selection-mode", "1", "Item 1"},
{"Scrollbar", "upper", "200", "200.0"},
{"Scrollbar", "step-increment", "5", "5.0"},
{"Text", "text", "Hello from the designer", "Hello from the designer"},
{"Memo", "text", "Memo content here", "Memo content here"},
{"Button", "border-radius", "10", "border-radius: 10px"},
};
static std::string read_file(const std::string& path) {
std::ifstream f(path);
if (!f) return "";
return std::string(std::istreambuf_iterator<char>(f),
std::istreambuf_iterator<char>());
}
int main(int argc, char** argv) {
std::string out = argc > 1 ? argv[1] : "/tmp/lightgui_widgets_gen";
Project p;
p.title = "Widget Test";
p.class_name = "MyWindow";
// Unique name per spec, so two specs for one type (e.g. a
// custom-items ComboBox and a default-items ComboBox) do not collide.
std::map<std::string, int> seen;
std::vector<std::string> spec_names;
int i = 0;
for (const Spec& s : kSpecs) {
Widget w;
char buf[16];
std::snprintf(buf, sizeof buf, "_%03d", ++seen[s.type]);
w.name = std::string(s.type) + buf;
spec_names.push_back(w.name);
w.type = s.type;
w.x = 10 + (i % 4) * 210;
w.y = 10 + (i / 4) * 150;
const TypeInfo* t = find_type(s.type);
w.width = t ? t->def_width : 100;
w.height = t ? t->def_height : 30;
for (const PropDef& d : props_for(s.type))
if (d.def && d.def[0]) w.props[d.name] = d.def;
for (const PropDef& d : common_props())
if (d.def && d.def[0]) w.props[d.name] = d.def;
if (find_prop(s.type, "label")) w.props["label"] = w.name;
w.props[s.prop] = s.value;
p.widgets.push_back(w);
++i;
}
GenResult r = generate_project(p, p.widgets, out);
if (!r.ok) {
std::printf("FAIL generate: %s\n", r.error.c_str());
return 1;
}
int pass = 0, fail = 0;
for (size_t k = 0; k < sizeof(kSpecs) / sizeof(kSpecs[0]); ++k) {
const Spec& s = kSpecs[k];
std::string name = spec_names[k];
std::string code = read_file(out + "/" + name + ".cpp");
bool ok = !code.empty() &&
code.find(s.marker) != std::string::npos;
std::printf("%s %-16s marker '%s' in %s.cpp\n",
ok ? "PASS" : "FAIL", s.type, s.marker, name.c_str());
if (ok) ++pass; else ++fail;
}
// Every widget class file must exist, plus window + main + Makefile.
for (size_t k = 0; k < sizeof(kSpecs) / sizeof(kSpecs[0]); ++k) {
const Spec& s = kSpecs[k];
std::string h = out + "/" + spec_names[k] + ".h";
if (!std::filesystem::exists(h)) {
std::printf("FAIL %-16s missing %s\n", s.type, h.c_str());
++fail;
}
}
// Save/load round-trip (.fdj) for every new widget type.
{
std::string err;
if (!save_project_file("/tmp/lightgui_widgets.fdj", p, err)) {
std::printf("FAIL save %s\n", err.c_str());
++fail;
} else {
Project q;
if (!load_project_file("/tmp/lightgui_widgets.fdj", q, err)) {
std::printf("FAIL load %s\n", err.c_str());
++fail;
} else if (q.widgets.size() != p.widgets.size()) {
std::printf("FAIL round-trip count %zu != %zu\n",
q.widgets.size(), p.widgets.size());
++fail;
} else {
for (size_t k = 0; k < q.widgets.size(); ++k) {
bool ok = q.widgets[k].type == p.widgets[k].type &&
q.widgets[k].name == p.widgets[k].name &&
q.widgets[k].props == p.widgets[k].props;
std::printf("%s %-16s .fdj round-trip\n",
ok ? "PASS" : "FAIL",
q.widgets[k].type.c_str());
if (ok) ++pass; else ++fail;
}
}
}
}
std::printf("== widget_tests: %d passed, %d failed ==\n", pass, fail);
return fail ? 1 : 0;
}