| 1 | //go:build browser |
| 2 | |
| 3 | package gallery |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "encoding/json" |
| 8 | "net/http" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "github.com/chromedp/chromedp" |
| 14 | |
| 15 | "amadan.net/rastrillo/fiesta" |
| 16 | "amadan.net/rastrillo/fiesta/internal/harness" |
| 17 | ) |
| 18 | |
| 19 | // The samples in the page's language, as a reader meets them: the |
| 20 | // enhanced controls speak the page's language once their scripts have |
| 21 | // run, and the money sample is written the page's way with no script |
| 22 | // at all. |
| 23 | |
| 24 | // jaStrings is the framework's own Japanese catalog's plain strings. |
| 25 | func jaStrings(t *testing.T) map[string]string { |
| 26 | t.Helper() |
| 27 | b, ok := fiesta.Strings("ja") |
| 28 | if !ok { |
| 29 | t.Fatal("the framework has no ja catalog") |
| 30 | } |
| 31 | var raw map[string]json.RawMessage |
| 32 | if err := json.Unmarshal(b, &raw); err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | out := map[string]string{} |
| 36 | for k, v := range raw { |
| 37 | var s string |
| 38 | if json.Unmarshal(v, &s) == nil { |
| 39 | out[k] = s |
| 40 | } |
| 41 | } |
| 42 | return out |
| 43 | } |
| 44 | |
| 45 | // TestTheEnhancedControlsSpeakThePagesLanguage: in day/ja, the |
| 46 | // filterable select, opened, offers its filter in Japanese, and the |
| 47 | // date field's calendar button is named in Japanese and opens a |
| 48 | // calendar named in Japanese. The frames load no script table, so the |
| 49 | // words can only have come from the attributes the gallery wrote. |
| 50 | func TestTheEnhancedControlsSpeakThePagesLanguage(t *testing.T) { |
| 51 | rig := harness.New(t, func(string) http.Handler { return treeHandler(t) }) |
| 52 | ctx, cancel := context.WithTimeout(rig.Context(), 2*time.Minute) |
| 53 | defer cancel() |
| 54 | ja := jaStrings(t) |
| 55 | sel := mustEntry(t, "field-select") |
| 56 | date := mustEntry(t, "field-date") |
| 57 | selState, dateState := "", "" |
| 58 | for _, st := range sel.States { |
| 59 | if strings.Contains(st.ID, "filterable") { |
| 60 | selState = st.ID |
| 61 | } |
| 62 | } |
| 63 | for _, st := range date.States { |
| 64 | if strings.HasPrefix(st.ID, "enhanced") { |
| 65 | dateState = st.ID |
| 66 | } |
| 67 | } |
| 68 | if selState == "" || dateState == "" { |
| 69 | t.Fatalf("no filterable select (%q) or enhanced date field (%q) to open", selState, dateState) |
| 70 | } |
| 71 | |
| 72 | mustRun(t, ctx, chromedp.EmulateViewport(900, 900), |
| 73 | chromedp.Navigate(rig.Origin+href("day/ja/frames/field-select/"+selState+".html"))) |
| 74 | until(t, ctx, "the select is enhanced", `!!document.querySelector("[rst-combo] input[role=combobox]")`) |
| 75 | mustRun(t, ctx, chromedp.Click(`[rst-combo] input[role=combobox]`, chromedp.ByQuery)) |
| 76 | until(t, ctx, "the select opens", `document.querySelector("[rst-combo] input[role=combobox]").getAttribute("aria-expanded") === "true"`) |
| 77 | var placeholder string |
| 78 | at(t, ctx, `JSON.stringify(document.querySelector("[rst-combo] input[role=combobox]").placeholder)`, &placeholder) |
| 79 | if placeholder != ja["select-filter"] { |
| 80 | t.Errorf("day/ja's filterable select, opened, offers its filter as %q, want %q", placeholder, ja["select-filter"]) |
| 81 | } |
| 82 | |
| 83 | mustRun(t, ctx, chromedp.Navigate(rig.Origin+href("day/ja/frames/field-date/"+dateState+".html"))) |
| 84 | until(t, ctx, "the date field is enhanced", `!!document.querySelector("[rst-dtp] [rst-dtp-pick]")`) |
| 85 | var pick string |
| 86 | at(t, ctx, `JSON.stringify(document.querySelector("[rst-dtp] [rst-dtp-pick]").getAttribute("aria-label"))`, &pick) |
| 87 | if pick != ja["date-pick"] { |
| 88 | t.Errorf("day/ja's date field names its calendar button %q, want %q", pick, ja["date-pick"]) |
| 89 | } |
| 90 | mustRun(t, ctx, chromedp.Click(`[rst-dtp] [rst-dtp-pick]`, chromedp.ByQuery)) |
| 91 | until(t, ctx, "the calendar opens", `(() => { const c = document.querySelector("[rst-cal]"); return !!c && !c.hidden; })()`) |
| 92 | var cal string |
| 93 | at(t, ctx, `JSON.stringify(document.querySelector("[rst-cal]").getAttribute("aria-label"))`, &cal) |
| 94 | if cal != ja["date-calendar"] { |
| 95 | t.Errorf("day/ja's calendar, opened, is named %q, want %q", cal, ja["date-calendar"]) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // TestMoneySamplesWithNoScript: the Money sample is server-rendered |
| 100 | // text, so with no JavaScript the Spanish frame still shows the amounts |
| 101 | // written the Spanish way; and the money field is a plain input with |
| 102 | // its decimal keyboard and its hidden currency, as Rastrillo holds it. |
| 103 | // Each frame is driven at its own URL, as Rastrillo's drive does. |
| 104 | func TestMoneySamplesWithNoScript(t *testing.T) { |
| 105 | rig := harness.New(t, func(string) http.Handler { return treeHandler(t) }) |
| 106 | ctx, cancel := context.WithTimeout(rig.Context(), 60*time.Second) |
| 107 | defer cancel() |
| 108 | off := noScripts(t, ctx) |
| 109 | |
| 110 | money := mustEntry(t, moneyExample) |
| 111 | var formats string |
| 112 | if err := chromedp.Run(off, |
| 113 | chromedp.Navigate(rig.Origin+href("day/es/frames/"+moneyExample+"/"+money.Default+".html")), |
| 114 | chromedp.Text("dl", &formats, chromedp.ByQuery)); err != nil { |
| 115 | t.Fatalf("es money sample, scripts off: %v", err) |
| 116 | } |
| 117 | requireScriptsOff(t, off, "es money sample, scripts off") |
| 118 | if !strings.Contains(formats, "1284,50 €") { |
| 119 | t.Errorf("Spanish money sample with no script: %q", formats) |
| 120 | } |
| 121 | |
| 122 | field := mustEntry(t, "field-money") |
| 123 | var form string |
| 124 | if err := chromedp.Run(off, |
| 125 | chromedp.Navigate(rig.Origin+href("day/es/frames/field-money/"+field.Default+".html")), |
| 126 | chromedp.OuterHTML("input[inputmode]", &form, chromedp.ByQuery)); err != nil { |
| 127 | t.Fatalf("es field-money sample, scripts off: %v", err) |
| 128 | } |
| 129 | requireScriptsOff(t, off, "es field-money sample, scripts off") |
| 130 | if !strings.Contains(form, `inputmode="decimal"`) { |
| 131 | t.Errorf("field-money with no script: %s", form) |
| 132 | } |
| 133 | } |
| 134 | |