|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +// Tests for issue #219: parseRawWidget missed ScrollContainer / TabControl |
| 4 | +// children because they live under CenterRegion.Widgets and TabPages[].Widgets |
| 5 | +// respectively, not under the top-level Widgets array that every other |
| 6 | +// container uses. |
| 7 | + |
| 8 | +package executor |
| 9 | + |
| 10 | +import ( |
| 11 | + "bytes" |
| 12 | + "strings" |
| 13 | + "testing" |
| 14 | +) |
| 15 | + |
| 16 | +func TestParseRawWidget_ScrollContainerRecursesIntoCenterRegion(t *testing.T) { |
| 17 | + ctx, _ := newMockCtx(t) |
| 18 | + |
| 19 | + raw := map[string]any{ |
| 20 | + "$Type": "Pages$ScrollContainer", |
| 21 | + "Name": "Scroll1", |
| 22 | + "CenterRegion": map[string]any{ |
| 23 | + "Widgets": []any{ |
| 24 | + map[string]any{"$Type": "Pages$TextBox", "Name": "InnerText"}, |
| 25 | + }, |
| 26 | + }, |
| 27 | + } |
| 28 | + |
| 29 | + got := parseRawWidget(ctx, raw) |
| 30 | + if len(got) != 1 { |
| 31 | + t.Fatalf("expected 1 widget, got %d", len(got)) |
| 32 | + } |
| 33 | + sc := got[0] |
| 34 | + if sc.Type != "Pages$ScrollContainer" || sc.Name != "Scroll1" { |
| 35 | + t.Errorf("outer widget: type=%q name=%q", sc.Type, sc.Name) |
| 36 | + } |
| 37 | + if len(sc.Children) != 1 { |
| 38 | + t.Fatalf("expected 1 child under ScrollContainer, got %d", len(sc.Children)) |
| 39 | + } |
| 40 | + if sc.Children[0].Name != "InnerText" { |
| 41 | + t.Errorf("child name: got %q, want InnerText", sc.Children[0].Name) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func TestParseRawWidget_ScrollContainerFallsBackToWidgets(t *testing.T) { |
| 46 | + // Older/legacy BSON shape where children lived directly under Widgets. |
| 47 | + // parseRawWidget must still recurse so existing projects don't regress. |
| 48 | + ctx, _ := newMockCtx(t) |
| 49 | + |
| 50 | + raw := map[string]any{ |
| 51 | + "$Type": "Forms$ScrollContainer", |
| 52 | + "Name": "LegacyScroll", |
| 53 | + "Widgets": []any{ |
| 54 | + map[string]any{"$Type": "Forms$TextBox", "Name": "LegacyText"}, |
| 55 | + }, |
| 56 | + } |
| 57 | + |
| 58 | + got := parseRawWidget(ctx, raw) |
| 59 | + if len(got) != 1 || len(got[0].Children) != 1 { |
| 60 | + t.Fatalf("expected 1 widget with 1 child, got %+v", got) |
| 61 | + } |
| 62 | + if got[0].Children[0].Name != "LegacyText" { |
| 63 | + t.Errorf("child name: got %q, want LegacyText", got[0].Children[0].Name) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func TestParseRawWidget_TabControlPreservesTabPages(t *testing.T) { |
| 68 | + ctx, _ := newMockCtx(t) |
| 69 | + |
| 70 | + raw := map[string]any{ |
| 71 | + "$Type": "Pages$TabControl", |
| 72 | + "Name": "Tabs1", |
| 73 | + "TabPages": []any{ |
| 74 | + map[string]any{ |
| 75 | + "Name": "GeneralTab", |
| 76 | + "Widgets": []any{ |
| 77 | + map[string]any{"$Type": "Pages$TextBox", "Name": "GeneralField"}, |
| 78 | + }, |
| 79 | + }, |
| 80 | + map[string]any{ |
| 81 | + "Name": "DetailsTab", |
| 82 | + "Widgets": []any{ |
| 83 | + map[string]any{"$Type": "Pages$TextBox", "Name": "DetailsField"}, |
| 84 | + map[string]any{"$Type": "Pages$TextBox", "Name": "DetailsNote"}, |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + } |
| 89 | + |
| 90 | + got := parseRawWidget(ctx, raw) |
| 91 | + if len(got) != 1 { |
| 92 | + t.Fatalf("expected 1 widget, got %d", len(got)) |
| 93 | + } |
| 94 | + tc := got[0] |
| 95 | + if tc.Type != "Pages$TabControl" || tc.Name != "Tabs1" { |
| 96 | + t.Errorf("outer widget: type=%q name=%q", tc.Type, tc.Name) |
| 97 | + } |
| 98 | + if len(tc.Children) != 2 { |
| 99 | + t.Fatalf("expected 2 TabPage children, got %d", len(tc.Children)) |
| 100 | + } |
| 101 | + |
| 102 | + for i, expectedName := range []string{"GeneralTab", "DetailsTab"} { |
| 103 | + if tc.Children[i].Type != "Pages$TabPage" { |
| 104 | + t.Errorf("tab %d type: got %q, want Pages$TabPage", i, tc.Children[i].Type) |
| 105 | + } |
| 106 | + if tc.Children[i].Name != expectedName { |
| 107 | + t.Errorf("tab %d name: got %q, want %q", i, tc.Children[i].Name, expectedName) |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + if len(tc.Children[0].Children) != 1 || tc.Children[0].Children[0].Name != "GeneralField" { |
| 112 | + t.Errorf("GeneralTab children: %+v", tc.Children[0].Children) |
| 113 | + } |
| 114 | + if len(tc.Children[1].Children) != 2 { |
| 115 | + t.Fatalf("DetailsTab expected 2 children, got %d", len(tc.Children[1].Children)) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +func TestOutputWidgetMDLV3_TabControlEmitsTabPageStructure(t *testing.T) { |
| 120 | + var buf bytes.Buffer |
| 121 | + ctx := &ExecContext{Output: &buf} |
| 122 | + |
| 123 | + tab := rawWidget{ |
| 124 | + Type: "Pages$TabControl", |
| 125 | + Name: "Tabs1", |
| 126 | + Children: []rawWidget{ |
| 127 | + { |
| 128 | + Type: "Pages$TabPage", |
| 129 | + Name: "GeneralTab", |
| 130 | + TabCaption: "General", |
| 131 | + Children: []rawWidget{ |
| 132 | + {Type: "Pages$TextBox", Name: "GeneralField"}, |
| 133 | + }, |
| 134 | + }, |
| 135 | + }, |
| 136 | + } |
| 137 | + outputWidgetMDLV3(ctx, tab, 0) |
| 138 | + |
| 139 | + out := buf.String() |
| 140 | + for _, want := range []string{ |
| 141 | + "tabcontainer Tabs1", |
| 142 | + "tabpage GeneralTab", |
| 143 | + "Caption: 'General'", |
| 144 | + } { |
| 145 | + if !strings.Contains(out, want) { |
| 146 | + t.Errorf("output missing %q\nfull output:\n%s", want, out) |
| 147 | + } |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +func TestOutputWidgetMDLV3_ScrollContainerEmitsHeader(t *testing.T) { |
| 152 | + var buf bytes.Buffer |
| 153 | + ctx := &ExecContext{Output: &buf} |
| 154 | + |
| 155 | + sc := rawWidget{ |
| 156 | + Type: "Pages$ScrollContainer", |
| 157 | + Name: "Scroll1", |
| 158 | + Children: []rawWidget{ |
| 159 | + {Type: "Pages$TextBox", Name: "InnerText"}, |
| 160 | + }, |
| 161 | + } |
| 162 | + outputWidgetMDLV3(ctx, sc, 0) |
| 163 | + |
| 164 | + out := buf.String() |
| 165 | + if !strings.Contains(out, "scrollcontainer Scroll1") { |
| 166 | + t.Errorf("expected 'scrollcontainer Scroll1' in output, got:\n%s", out) |
| 167 | + } |
| 168 | +} |
0 commit comments