|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "image/png" |
4 | 7 | "net/http" |
5 | 8 | "net/http/httptest" |
6 | 9 | "regexp" |
@@ -100,3 +103,52 @@ func TestDocsGuidesMoveIntoCompactMenu(t *testing.T) { |
100 | 103 | t.Fatalf("compact menu should list only guides: %s", section) |
101 | 104 | } |
102 | 105 | } |
| 106 | + |
| 107 | +// Navigation must not lose the tab icon on pages hosted by the example handler. |
| 108 | +func TestSiteIconsAcrossSurfaces(t *testing.T) { |
| 109 | + h := handler() |
| 110 | + for _, path := range []string{"/", "/docs", "/examples", "/catalog", "/catalog/button", "/playground", "/forms"} { |
| 111 | + w := httptest.NewRecorder() |
| 112 | + h.ServeHTTP(w, httptest.NewRequest("GET", path, nil)) |
| 113 | + body := w.Body.String() |
| 114 | + for _, want := range []string{brandURL("favicon.ico"), brandURL("favicon-32.png"), brandURL("favicon.svg"), brandURL("apple-touch-icon.png"), `href="/site.webmanifest"`} { |
| 115 | + if strings.Count(body, want) != 1 { |
| 116 | + t.Errorf("%s: expected one icon declaration for %s", path, want) |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + for _, path := range []string{"/favicon.ico", "/apple-touch-icon.png", brandURL("favicon.ico"), brandURL("favicon-32.png")} { |
| 121 | + w := httptest.NewRecorder() |
| 122 | + h.ServeHTTP(w, httptest.NewRequest("GET", path, nil)) |
| 123 | + if w.Code != 200 || w.Body.Len() == 0 { |
| 124 | + t.Errorf("%s: unavailable icon (%d)", path, w.Code) |
| 125 | + } |
| 126 | + } |
| 127 | + w := httptest.NewRecorder() |
| 128 | + h.ServeHTTP(w, httptest.NewRequest("GET", "/site.webmanifest", nil)) |
| 129 | + var manifest struct { |
| 130 | + Name string |
| 131 | + Icons []struct{ Src, Sizes string } |
| 132 | + } |
| 133 | + if err := json.Unmarshal(w.Body.Bytes(), &manifest); err != nil { |
| 134 | + t.Fatal(err) |
| 135 | + } |
| 136 | + if manifest.Name != "Tend UI" || len(manifest.Icons) != 2 { |
| 137 | + t.Fatalf("invalid manifest: %+v", manifest) |
| 138 | + } |
| 139 | + if !strings.Contains(w.Header().Get("Content-Security-Policy"), "manifest-src 'self'") { |
| 140 | + t.Fatal("manifest blocked by CSP") |
| 141 | + } |
| 142 | + for i, icon := range manifest.Icons { |
| 143 | + r := httptest.NewRecorder() |
| 144 | + h.ServeHTTP(r, httptest.NewRequest("GET", icon.Src, nil)) |
| 145 | + config, err := png.DecodeConfig(bytes.NewReader(r.Body.Bytes())) |
| 146 | + if err != nil { |
| 147 | + t.Fatal(err) |
| 148 | + } |
| 149 | + size := []int{192, 512}[i] |
| 150 | + if r.Code != 200 || config.Width != size || config.Height != size { |
| 151 | + t.Errorf("invalid app icon: %+v", config) |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments