Debian Codegraph @

browsertests/browser_test.go

raw symbols SCIP
1 // Copyright 2023 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package browsertests
16
17 import (
18 "context"
19 "fmt"
20 "os/exec"
21 "regexp"
22 "runtime"
23 "strings"
24 "testing"
25 "time"
26
27 _ "embed"
28
29 "github.com/chromedp/chromedp"
30 )
31
32 func maybeSkipBrowserTest(t *testing.T) {
33 // Limit to just Linux for now since this is expensive and the
34 // browser interactions should be platform agnostic. If we ever
35 // see a benefit from wider testing, we can relax this.
36 if runtime.GOOS != "linux" || runtime.GOARCH != "amd64" {
37 t.Skip("This test only works on x86-64 Linux")
38 }
39
40 // Check that browser is available.
41 if _, err := exec.LookPath("google-chrome"); err == nil {
42 return
43 }
44 if _, err := exec.LookPath("chrome"); err == nil {
45 return
46 }
47 t.Skip("chrome not available")
48 }
49
50 func TestTopTable(t *testing.T) {
51 maybeSkipBrowserTest(t)
52
53 prof := makeFakeProfile()
54 server := makeTestServer(t, prof)
55 ctx := newContext(context.Background(), t)
56
57 err := chromedp.Run(ctx,
58 chromedp.Navigate(server.URL+"/top"),
59 chromedp.WaitVisible(`#toptable`, chromedp.ByID),
60
61 // Check that fake profile entries show up in the right order.
62 matchRegexp(t, "#node0", `200ms.*F2`),
63 matchInOrder(t, "#toptable", "F2", "F3", "F1"),
64
65 // Check sorting by cumulative count.
66 chromedp.Click(`#cumhdr1`, chromedp.ByID),
67 matchInOrder(t, "#toptable", "F1", "F2", "F3"),
68 )
69 if err != nil {
70 t.Fatal(err)
71 }
72 }
73
74 func TestFlameGraph(t *testing.T) {
75 maybeSkipBrowserTest(t)
76
77 prof := makeFakeProfile()
78 server := makeTestServer(t, prof)
79 ctx := newContext(context.Background(), t)
80
81 var ignored []byte // Some chromedp.Evaluate() versions wants non-nil result argument
82 err := chromedp.Run(ctx,
83 chromedp.Navigate(server.URL),
84 chromedp.Evaluate(jsTestFixture, &ignored),
85 eval(t, jsCheckFlame),
86 )
87 if err != nil {
88 t.Fatal(err)
89 }
90 }
91
92 //go:embed testdata/testflame.js
93 var jsCheckFlame string
94
95 func TestSource(t *testing.T) {
96 maybeSkipBrowserTest(t)
97
98 prof := makeFakeProfile()
99 server := makeTestServer(t, prof)
100 ctx := newContext(context.Background(), t)
101
102 err := chromedp.Run(ctx,
103 chromedp.Navigate(server.URL+"/source?f=F3"),
104 chromedp.WaitVisible(`#content`, chromedp.ByID),
105 matchRegexp(t, "#content", `F3`), // Header
106 matchRegexp(t, "#content", `Total:.*100ms`), // Total for function
107 matchRegexp(t, "#content", `\b22\b.*100ms`), // Line 22
108 )
109 if err != nil {
110 t.Fatal(err)
111 }
112 }
113
114 func newContext(ctx context.Context, t *testing.T) context.Context {
115 opts := append(chromedp.DefaultExecAllocatorOptions[:],
116 // Ubuntu 23+ enables AppArmor in a way that conflicts with Chrome's usage
117 // of unprivileged user namespaces as part of the sandboxing. Since our
118 // test does not visit any external websites, we don't really need the
119 // sandbox, so disable it.
120 chromedp.NoSandbox,
121 )
122
123 // browserDeadline is the deadline to use for browser tests. This is long to
124 // reduce flakiness in CI workflows.
125 const browserDeadline = time.Second * 90
126
127 ctx, cancel := chromedp.NewExecAllocator(ctx, opts...)
128 t.Cleanup(cancel)
129 ctx, cancel = context.WithTimeout(ctx, browserDeadline)
130 t.Cleanup(cancel)
131 ctx, cancel = chromedp.NewContext(ctx)
132 t.Cleanup(cancel)
133 return ctx
134 }
135
136 // matchRegexp is a chromedp.Action that fetches the text of the first
137 // node that matched query and checks that the text matches regexp re.
138 func matchRegexp(t *testing.T, query, re string) chromedp.ActionFunc {
139 return func(ctx context.Context) error {
140 var value string
141 err := chromedp.Text(query, &value, chromedp.ByQuery).Do(ctx)
142 if err != nil {
143 return fmt.Errorf("text %s: %v", query, err)
144 }
145 t.Logf("text %s:\n%s", query, value)
146 m, err := regexp.MatchString(re, value)
147 if err != nil {
148 return err
149 }
150 if !m {
151 return fmt.Errorf("%s: did not find %q in\n%s", query, re, value)
152 }
153 return nil
154 }
155 }
156
157 // matchInOrder is a chromedp.Action that fetches the text of the first
158 // node that matched query and checks that the supplied sequence of
159 // strings occur in order in the text.
160 func matchInOrder(t *testing.T, query string, sequence ...string) chromedp.ActionFunc {
161 return func(ctx context.Context) error {
162 var value string
163 err := chromedp.Text(query, &value, chromedp.ByQuery).Do(ctx)
164 if err != nil {
165 return fmt.Errorf("text %s: %v", query, err)
166 }
167 t.Logf("text %s:\n%s", query, value)
168 remaining := value
169 for _, s := range sequence {
170 pos := strings.Index(remaining, s)
171 if pos < 0 {
172 return fmt.Errorf("%s: did not find %q in expected order %v in\n%s", query, s, sequence, value)
173 }
174 remaining = remaining[pos+len(s):]
175 }
176 return nil
177 }
178 }
179
180 // eval runs the specified javascript in the browser. The javascript must
181 // return an [][]any, where each of the []any starts with either "LOG" or
182 // "ERROR" (see testdata/testfixture.js).
183 func eval(t *testing.T, js string) chromedp.ActionFunc {
184 return func(ctx context.Context) error {
185 var result [][]any
186 err := chromedp.Evaluate(js, &result).Do(ctx)
187 if err != nil {
188 return err
189 }
190 for _, s := range result {
191 if len(s) > 0 && s[0] == "LOG" {
192 t.Log(s[1:]...)
193 } else if len(s) > 0 && s[0] == "ERROR" {
194 t.Error(s[1:]...)
195 } else {
196 t.Error(s...) // Treat missing prefix as an error.
197 }
198 }
199 return nil
200 }
201 }
202
203 //go:embed testdata/testfixture.js
204 var jsTestFixture string