Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions desktop/internal/workbench/new_project_advanced_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package workbench

import (
"encoding/json"
"testing"

"github.com/1lck/Lithe-IDEA/desktop/internal/core"
"github.com/1lck/Lithe-IDEA/desktop/internal/i18n"
"github.com/1lck/Lithe-IDEA/desktop/internal/pluginservice"
"github.com/vivaui/viva/app"
"github.com/vivaui/viva/gfx"
)

// advancedGeneratorPlugin declares a generator whose advanced field starts
// valid, and one whose required advanced field starts empty.
func advancedGeneratorPlugin(t *testing.T) pluginservice.GeneratorPlugin {
t.Helper()
generator := func(id, name string, advanced map[string]any) map[string]any {
return map[string]any{
"id": id, "displayName": name, "section": "generator",
"fields": []map[string]any{{"id": "label", "type": "text", "title": "Label", "default": "hello"}, advanced},
"steps": []map[string]any{{"kind": "command", "command": "scaffold-tool", "mayUseNetwork": false, "timeoutSeconds": 60}},
}
}
manifest, err := json.Marshal(map[string]any{
"schemaVersion": 0, "id": "dev.lithe.test.advanced", "displayName": "Advanced", "version": "1.0.0",
"pluginAPIVersion": 0, "requires": map[string]int{"projectGenerators": 0},
"vendor": map[string]string{"id": "org.example", "displayName": "Example", "signatureRequirement": "publisherPackage"},
"permissions": []string{"project.generate"}, "activationEvents": []string{},
"projectGenerators": []map[string]any{
generator("tuned", "Tuned", map[string]any{"id": "level", "type": "text", "title": "Level", "advanced": true, "default": "3"}),
generator("strict", "Strict", map[string]any{"id": "module", "type": "text", "title": "Module", "advanced": true}),
},
})
if err != nil {
t.Fatal(err)
}
return pluginservice.GeneratorPlugin{PluginID: "dev.lithe.test.advanced", Version: "1.0.0", Path: t.TempDir(), Enabled: true, Manifest: manifest}
}

// advancedChevronPointsDown reads the direction of the chevron the "Advanced
// Settings" button draws: a chevron pointing down is wider than it is tall, one
// pointing right is taller than it is wide.
func (h *harness) advancedChevronPointsDown() bool {
h.t.Helper()
button := h.pageHit("Advanced Settings").Bounds
inside := func(p gfx.Point) bool {
return p.X >= button.X && p.X <= button.X+button.Width && p.Y >= button.Y && p.Y <= button.Y+button.Height
}
left, top, right, bottom := button.X+button.Width, button.Y+button.Height, button.X, button.Y
found := false
for _, primitive := range h.driver.Snapshot().Primitives {
if primitive.Kind != app.TrianglePrimitive || !inside(primitive.Points[0]) || !inside(primitive.Points[1]) || !inside(primitive.Points[2]) {
continue
}
found = true
for _, p := range primitive.Points {
left, top, right, bottom = min(left, p.X), min(top, p.Y), max(right, p.X), max(bottom, p.Y)
}
}
if !found {
h.t.Fatalf("the Advanced Settings button %+v draws no chevron", button)
}
return right-left > bottom-top
}

// The chevron of "Advanced Settings" points down while the group is open and
// right while it is closed.
func TestNewProjectAdvancedSettingsChevronFollowsTheGroup(t *testing.T) {
h := newGeneratorHarness(t, &testKernel{Client: core.NewClient()}, advancedGeneratorPlugin(t))
h.openNewProjectSheet()
h.chooseGenerator("Tuned", "dev.lithe.test.advanced/tuned")
if h.shows("Level") || h.advancedChevronPointsDown() {
t.Fatal("the advanced group of a valid form does not start closed with its chevron pointing right")
}
h.clickNamed("Advanced Settings")
h.await("the advanced field", func() bool { return h.shows("Level") })
if !h.advancedChevronPointsDown() {
t.Fatal("the chevron of the open advanced group does not point down")
}
h.clickNamed("Advanced Settings")
h.await("the advanced field to hide", func() bool { return !h.shows("Level") })
if h.advancedChevronPointsDown() {
t.Fatal("the chevron of the closed advanced group does not point right")
}
}

// A problem in an advanced field disables Create, so the closed group opens to
// show it; the user can still close the group while the problem stays.
func TestNewProjectAdvancedSettingsOpenOnAProblem(t *testing.T) {
h := newGeneratorHarness(t, &testKernel{Client: core.NewClient()}, advancedGeneratorPlugin(t))
h.openNewProjectSheet()
h.chooseGeneratorWithoutWaiting("Strict")
required := i18n.Shared(h.app.language(), "desktop.newProject.problem.required")
h.await("the empty required field to be explained", func() bool { return h.shows("Module") && h.shows(required) })
if !h.advancedChevronPointsDown() || !h.pageHit("Create").Disabled {
t.Fatal("the group opened for the problem does not point its chevron down, or Create is enabled")
}
h.clickNamed("Advanced Settings")
h.await("the advanced group to close", func() bool { return !h.shows("Module") })
h.app.invalidator.Invalidate()
h.settle()
if h.shows("Module") || h.advancedChevronPointsDown() {
t.Fatal("the group reopened for a problem the user has already seen")
}
}
3 changes: 3 additions & 0 deletions desktop/internal/workbench/new_project_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ type generatorForm struct {
// formKey is the request key of the answer above; currentKey is the key of
// what the user has entered now. They agree once Core has answered for it.
formKey, currentKey string
// advancedProblem is whether an advanced field had a problem when the form
// was last drawn, so a new one can open the closed group once.
advancedProblem bool
}

func newGeneratorForm(key string, entry core.ProjectGeneratorEntry, plugin pluginservice.GeneratorPlugin) *generatorForm {
Expand Down
15 changes: 13 additions & 2 deletions desktop/internal/workbench/new_project_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,20 +317,31 @@ func (a *App) generatorFields(c ui.C, language i18n.Language, form *generatorFor
ui.Paragraph(c, i18n.Shared(language, "desktop.newProject.toolsFailed"), 3).Color(a.theme.Danger)
}
a.requirementNotices(c, language, form)
advanced := 0
advanced, problem := 0, false
for _, field := range form.fields {
if field.state.Visible && field.decl.Advanced {
advanced++
problem = problem || field.state.Problem != ""
}
}
for _, field := range form.fields {
if field.state.Visible && !field.decl.Advanced {
a.generatorField(c, language, form, field, locked)
}
}
// A problem in a closed group would leave Create disabled with nothing
// saying why, so the group opens when one appears; it can be closed again.
if problem && !form.advancedProblem {
n.advancedOpen = true
}
form.advancedProblem = problem
if advanced > 0 {
title := i18n.Shared(language, "desktop.newProject.advanced")
if ui.Button(c, title).Ghost().Icon(icons.ChevronRight).Clicked() {
chevron := icons.ChevronRight
if n.advancedOpen {
chevron = icons.ChevronDown
}
if ui.Button(c, title).Ghost().Icon(chevron).Clicked() {
n.advancedOpen = !n.advancedOpen
}
if n.advancedOpen {
Expand Down
47 changes: 47 additions & 0 deletions desktop/internal/workbench/visual_capture_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,53 @@ func TestVisualNewProjectFieldDescriptions(t *testing.T) {
}
}

// TestVisualNewProjectAdvancedSettings captures a generator form with its
// "Advanced Settings" group closed and open, so the chevron of each state shows.
// It needs only LITHE_VISUAL_OUTPUT_DIR.
func TestVisualNewProjectAdvancedSettings(t *testing.T) {
out := os.Getenv("LITHE_VISUAL_OUTPUT_DIR")
if out == "" {
t.Skip("set LITHE_VISUAL_OUTPUT_DIR")
}
if err := os.MkdirAll(out, 0755); err != nil {
t.Fatal(err)
}
for _, theme := range []string{"dark", "light"} {
for _, language := range []string{"zh-Hans", "en-US"} {
t.Run(theme+"-"+language, func(t *testing.T) {
var session *headless.Session
prefs := store.At(t.TempDir())
if err := prefs.Save(settingsFile, map[string]string{"theme": theme}); err != nil {
t.Fatal(err)
}
h := newHarnessWithDriver(t, Options{Kernel: &testKernel{Client: core.NewClient()}, Store: prefs, Languages: []string{language}}, func(config app.Config) (*app.Driver, func(), error) {
config.Width, config.Height = 800, 682
config.MinWidth = 800
var err error
session, err = headless.Open(config, nil, headless.Options{})
if err != nil {
return nil, nil, err
}
return session.Driver(), session.Close, nil
})
h.offerGenerators(advancedGeneratorPlugin(t))
h.openNewProjectSheet()
h.chooseGenerator("Tuned", "dev.lithe.test.advanced/tuned")
advanced := i18n.Shared(h.app.language(), "desktop.newProject.advanced")
for _, state := range []string{"closed", "open"} {
if state == "open" {
h.clickNamed(advanced)
h.await("the advanced field", func() bool { return h.shows("Level") })
}
if err := session.Screenshot(filepath.Join(out, "new-project-advanced-"+state+"-"+theme+"-"+language+".png")); err != nil {
t.Fatal(err)
}
}
})
}
}
}

// TestVisualDebugAnnouncedAdapterExitedFirst captures the debug view after a synthetic
// tcpAnnounced adapter exited before it announced its port: the session ends and the
// view shows why. It needs only LITHE_VISUAL_OUTPUT_DIR.
Expand Down
Loading
Loading