Debian Codegraph @

addons/web/static/lib/hoot/ui/hoot_debug_toolbar.js

raw
1 /** @odoo-module */
2
3 import { Component, onWillRender, useEffect, useRef, useState, xml } from "@odoo/owl";
4 import { Test } from "../core/test";
5 import { refresh } from "../core/url";
6 import { formatTime, throttle } from "../hoot_utils";
7 import { HootConfigMenu } from "./hoot_config_menu";
8 import { HootTestPath } from "./hoot_test_path";
9 import { HootTestResult } from "./hoot_test_result";
10
11 const {
12 HTMLElement,
13 innerHeight,
14 innerWidth,
15 Math: { max: $max, min: $min },
16 Object: { assign: $assign },
17 } = globalThis;
18 const addWindowListener = window.addEventListener.bind(window);
19 const removeWindowListener = window.removeEventListener.bind(window);
20 const { addEventListener, removeEventListener } = HTMLElement.prototype;
21
22 /**
23 * @param {string} containerRefName
24 * @param {string} handleRefName
25 * @param {() => any} allowDrag
26 */
27 function useMovable(containerRefName, handleRefName, allowDrag) {
28 function computeEffectDependencies() {
29 return [(currentContainer = containerRef.el), (currentHandle = handleRef.el)];
30 }
31
32 /**
33 * @param {PointerEvent} ev
34 */
35 function drag(ev) {
36 if (!currentContainer || !isDragging) {
37 return;
38 }
39
40 ev.preventDefault();
41
42 const x = $max($min(maxX, ev.clientX - offsetX), 0);
43 const y = $max($min(maxY, ev.clientY - offsetY), 0);
44 $assign(currentContainer.style, {
45 left: `${x}px`,
46 top: `${y}px`,
47 });
48 }
49
50 /**
51 * @param {PointerEvent} [ev]
52 */
53 function dragEnd(ev) {
54 if (!currentContainer || !isDragging) {
55 return;
56 }
57 isDragging = false;
58
59 ev?.preventDefault();
60
61 removeWindowListener("pointermove", throttledDrag);
62 removeWindowListener("pointerup", dragEnd);
63 }
64
65 /**
66 * @param {PointerEvent} ev
67 */
68 function dragStart(ev) {
69 if (!currentContainer || !allowDrag()) {
70 return;
71 }
72
73 if (isDragging) {
74 dragEnd(ev);
75 } else {
76 ev.preventDefault();
77 }
78
79 isDragging = true;
80
81 addWindowListener("pointermove", throttledDrag);
82 addWindowListener("pointerup", dragEnd);
83 addWindowListener("keydown", dragEnd);
84
85 const { x, y, width, height } = currentContainer.getBoundingClientRect();
86
87 $assign(currentContainer.style, {
88 left: `${x}px`,
89 top: `${y}px`,
90 width: `${width}px`,
91 height: `${height}px`,
92 });
93
94 offsetX = ev.clientX - x;
95 offsetY = ev.clientY - y;
96 maxX = innerWidth - width;
97 maxY = innerHeight - height;
98 }
99
100 function effectCleanup() {
101 if (currentHandle) {
102 removeEventListener.call(currentHandle, "pointerdown", dragStart);
103 }
104 }
105
106 function onEffect() {
107 if (currentHandle) {
108 addEventListener.call(currentHandle, "pointerdown", dragStart);
109 }
110 return effectCleanup;
111 }
112
113 function resetPosition() {
114 currentContainer?.removeAttribute("style");
115 dragEnd();
116 }
117
118 const throttledDrag = throttle(drag);
119
120 const containerRef = useRef(containerRefName);
121 const handleRef = useRef(handleRefName);
122 /** @type {HTMLElement | null} */
123 let currentContainer = null;
124 /** @type {HTMLElement | null} */
125 let currentHandle = null;
126 let isDragging = false;
127 let maxX = 0;
128 let maxY = 0;
129 let offsetX = 0;
130 let offsetY = 0;
131
132 useEffect(onEffect, computeEffectDependencies);
133
134 return {
135 resetPosition,
136 };
137 }
138
139 /**
140 * @typedef {import("../core/expect").Assertion} Assertion
141 *
142 * @typedef {{
143 * test: Test;
144 * }} HootDebugToolBarProps
145 *
146 * @typedef {import("../core/expect").CaseResult} CaseResult
147 */
148
149 /** @extends {Component<HootDebugToolBarProps, import("../hoot").Environment>} */
150 export class HootDebugToolBar extends Component {
151 static components = { HootConfigMenu, HootTestPath, HootTestResult };
152
153 static props = {
154 test: Test,
155 };
156
157 static template = xml`
158 <div
159 class="${HootDebugToolBar.name} absolute start-0 bottom-0 max-w-full max-h-full flex p-4 z-4"
160 t-att-class="{ 'w-full': state.open }"
161 t-ref="root"
162 >
163 <div class="flex flex-col w-full overflow-hidden rounded shadow bg-gray-200 dark:bg-gray-800">
164 <div class="flex items-center gap-2 px-2">
165 <i
166 class="fa fa-bug text-cyan p-2"
167 t-att-class="{ 'cursor-move': !state.open }"
168 t-ref="handle"
169 />
170 <div class="flex gap-px rounded my-1 overflow-hidden min-w-fit">
171 <button
172 class="bg-btn px-2 py-1"
173 title="Exit debug mode (Ctrl + Esc)"
174 t-on-click.stop="exitDebugMode"
175 >
176 <i class="fa fa-sign-out" />
177 </button>
178 <t t-if="done">
179 <button
180 class="bg-btn px-2 py-1 animate-slide-left"
181 title="Restart test (F5)"
182 t-on-click.stop="refresh"
183 >
184 <i class="fa fa-refresh" />
185 </button>
186 </t>
187 </div>
188 <button
189 class="flex flex-1 items-center gap-1 truncate"
190 t-on-click.stop="toggleOpen"
191 title="Click to toggle details"
192 >
193 status:
194 <strong
195 t-attf-class="text-{{ info.className }}"
196 t-esc="info.status"
197 />
198 <span class="hidden sm:flex items-center gap-1">
199 <span class="text-gray">-</span>
200 assertions:
201 <span class="contents text-emerald">
202 <strong t-esc="info.passed" />
203 passed
204 </span>
205 <t t-if="info.failed">
206 <span class="text-gray">/</span>
207 <span class="contents text-rose">
208 <strong t-esc="info.failed" />
209 failed
210 </span>
211 </t>
212 </span>
213 <span class="text-gray">-</span>
214 time:
215 <span
216 class="text-primary"
217 t-esc="formatTime(props.test.lastResults?.duration, 'ms')"
218 />
219 </button>
220 <button class="p-2" t-on-click="toggleConfig">
221 <i class="fa fa-cog" />
222 </button>
223 </div>
224 <t t-if="state.open">
225 <div class="flex flex-col w-full sm:flex-row overflow-auto">
226 <HootTestResult open="'always'" test="props.test" t-key="done">
227 <HootTestPath canCopy="true" full="true" test="props.test" />
228 </HootTestResult>
229 <t t-if="state.configOpen">
230 <div class="flex flex-col gap-1 p-3 overflow-y-auto">
231 <HootConfigMenu />
232 </div>
233 </t>
234 </div>
235 </t>
236 </div>
237 </div>
238 `;
239
240 formatTime = formatTime;
241 refresh = refresh;
242
243 get done() {
244 return Boolean(this.runnerState.done.size); // subscribe to test being added as done
245 }
246
247 setup() {
248 this.runnerState = useState(this.env.runner.state);
249 this.state = useState({
250 configOpen: false,
251 open: false,
252 });
253
254 onWillRender(this.onWillRender.bind(this));
255
256 this.movable = useMovable("root", "handle", this.allowDrag.bind(this));
257 }
258
259 allowDrag() {
260 return !this.state.open;
261 }
262
263 exitDebugMode() {
264 const { runner } = this.env;
265 runner.config.debugTest = false;
266 runner.stop();
267 }
268
269 getInfo() {
270 const [status, className] = this.getStatus();
271 const [assertPassed, assertFailed] = this.groupAssertions(
272 this.props.test.lastResults?.getEvents("assertion")
273 );
274 return {
275 className,
276 status,
277 passed: assertPassed,
278 failed: assertFailed,
279 };
280 }
281
282 getStatus() {
283 if (this.props.test.lastResults) {
284 switch (this.props.test.status) {
285 case Test.PASSED:
286 return ["passed", "emerald"];
287 case Test.FAILED:
288 return ["failed", "rose"];
289 case Test.ABORTED:
290 return ["aborted", "amber"];
291 }
292 }
293 return ["running", "cyan"];
294 }
295
296 /**
297 * @param {Assertion[]} [assertions]
298 */
299 groupAssertions(assertions) {
300 let passed = 0;
301 let failed = 0;
302 for (const assertion of assertions || []) {
303 if (assertion.pass) {
304 passed++;
305 } else {
306 failed++;
307 }
308 }
309 return [passed, failed];
310 }
311
312 onWillRender() {
313 this.info = this.getInfo();
314 }
315
316 toggleConfig() {
317 this.state.configOpen = !this.state.open || !this.state.configOpen;
318 if (this.state.configOpen && !this.state.open) {
319 this.state.open = true;
320 this.movable.resetPosition();
321 }
322 }
323
324 toggleOpen() {
325 this.state.open = !this.state.open;
326 if (this.state.open) {
327 this.movable.resetPosition();
328 }
329 }
330 }