blob: aa92cd76221c9cf5c27d77e9ebd1816d5a8ea8a0 [file] [log] [blame] [view]
Danil Somsikovd884ae72025-02-12 07:56:131# Chromium DevTools UI Engineering
2
3## Objective and scope
4
5This document defines how to build Chromium DevTools UI. It aims at improving consistency, maintainability and extensibility of the current code
6
7**Consistency** here means to have one way to do one thing, in the context of this doc to have a single reusable component per repeated task.
8
9**Maintainability** is addressed here through the [separation of concerns](https://en.wikipedia.org/wiki/Separation_of_concerns) while avoiding unnecessary indirection.
10
11**Extensibility** requires the ease of understanding and imitation, i.e. being able to take an existing code, understand and use it as an example to solve another problem.
12
13Additionally, all the changes need to be applicable to the existing code **point-wise** instead of requiring extensive rewriting.
14
15## Reusable web components
16
17Common UI primitives need to be implemented as web components. Examples include buttons, checkboxs, or data grids. Web components should not be used solely for encapsulation, we should not have single-use web components.
18
19Not all reusable code should be a web component: in the Application panel we have several similar views showing key-value pairs in a datagrid with a preview sidebar. Making this a truly reusable component will lead to an unjustifiable complexity. Instead we should have extracted a base class or helpers implementing the common functionality.
20
21In implementation we should prefer wrapping existing code under ui/legacy over new implementation. The former is the most feature-rich (including less obvious aspects like a11y) and has stood the test of time.
22
23We should however strive to expose a HTML-native API: e.g. toolbar doesnt need an `items` setter if its child HTML elements could define its content. Even the data grid doesnt need to expose data grid nodes, when `<tr>` and `<td>`s are enough.
24
25## Model-View-Presenter architecture
26
27We should strictly separate business logic, from UI logic and presentation. This means that most of the UI code should be centered around a presenter (subclass of a `UI.Widget`) that gets a view function injected. All the logic that is not related to the DevTools UI (i.e. that would stay the same if we rewrite DevTools as a command-line tool), should belong to the model layer.
28
29The presenter code should make no assumptions about the details of model or view code. It should not care what race conditions CDP exposes or how many layers of `<div class=”wrapper”>` does the markup have. However, for simplicity, the injected view function should have a default implementation inlined with a presenter (see an example below).
30
31In tests, we use a simple stub as a view function, which allows us to test the presenter logic without any DOM manipulation. To test the view function itself we should use screenshot and e2e tests.
32
33## Declarative and orchestrated DOM updates
34
35We should no longer use imperative API to update DOM. Instead we rely on orchestrated rendering of lit-html templates. The view function described above should be a call to lit-html `render`. The view function should be called from `UI.Widget`s `performUpdate` method, which by default is scheduled using `requestAnimationFrame`.
36
37To embed another presenter (`UI.Widget`) in the lit-html template, use `<devtools-widget .widgetConfig=${widgetConfig(<class>, {foo: 1, bar: 2})}`
38
39This will instantiate a `Widget` class with the web component as its `element` and, optionally, will set the properties provided in the second parameter. The widget wont be re-instantiated on the subsequent template renders, but the properties would be updated. For this to work, the widget needs to accept `HTMLElement` as a sole constructor parameter and properties need to be public members or setters.
40
Simon Zündafade152025-05-06 11:13:3841For backwards compatibility, the first argument to `widgetConfig` can also be a factory function: `<devtools-widget .widgetConfig=${widgetConfig(element => new MyWidget(foo, bar, element))}>`. Similar to the class constructor version, `element` is the actual `<devtools-widget>` so the following two invocations of `widgetConfig` are equivalent: `widgetConfig(MyWidget)` and `widgetConfig(element = new MyWidget(element))`.
42
Danil Somsikovd884ae72025-02-12 07:56:1343## Examples
44
45```html
Alex Rudenko8d6d6c22025-02-27 15:36:4046<devtools-widget .widgetConfig=${widgetConfig(ElementsPanel)}>
Wolfgang Beyerc73e1302025-05-14 15:57:1447 <devtools-split-view>
Alex Rudenko8d6d6c22025-02-27 15:36:4048 <devtools-widget slot="main" .widgetConfig=${widgetConfig(ElementsTree)}></devtools-widget>
Mathias Bynensae705ff2025-02-12 09:51:2349 <devtools-tab-pane slot="sidebar">
Alex Rudenko8d6d6c22025-02-27 15:36:4050 <devtools-widget .widgetConfig=${widgetConfig(StylesPane, {element: input.element})}></devtools-widget>
51 <devtools-widget .widgetConfig=${widgetConfig(ComputedPane, {element: input.element})}></devtools-widget>
Mathias Bynensae705ff2025-02-12 09:51:2352 ...
Danil Somsikovd884ae72025-02-12 07:56:1353 </devtools-tab-pane>
Wolfgang Beyerc73e1302025-05-14 15:57:1454 </devtools-split-view>
Danil Somsikovd884ae72025-02-12 07:56:1355</devtools-widget>
56```
57
58```ts
59class StylesPane extends UI.Widget {
60 constructor(element, view = (input, output, target) => {
61 render(html`
Alex Rudenko8d6d6c22025-02-27 15:36:4062 <devtools-widget .widgetConfig=${widgetConfig(MetricsPane, {element: input.element})}>
Mathias Bynensae705ff2025-02-12 09:51:2363 </devtools-widget>
Danil Somsikovd884ae72025-02-12 07:56:1364 <devtools-toolbar>
Mathias Bynensae705ff2025-02-12 09:51:2365 <devtools-filter-input @change=${input.onFilter}></devtools-filter-input>
66 <devtools-checkbox @change=${input.onShowAll}>Show All</devtools-checkbox>
67 <devtools-checkbox @change=${input.onGroup}>Group</devtools-checkbox>
Danil Somsikovd884ae72025-02-12 07:56:1368 </devtools-toolbar>
69 <devtools-tree-outline>
70 ${input.properties.map(p => html`<li>
71 <dt>${p.key}</dt><dd>${renderValue(p.value)}</dd>
72 <ol>${p.subproperties.map(...)}
73 </li>`)}
74 </devtools-tree-outline>`
75 }
76}
77```
78
79[https://source.chromium.org/chromium/chromium/src/+/main:third\_party/devtools-frontend/src/front\_end/panels/protocol\_monitor/ProtocolMonitor.ts;l=197](https://source.chromium.org/chromium/chromium/src/+/main:third_party/devtools-frontend/src/front_end/panels/protocol_monitor/ProtocolMonitor.ts;l=197)
80
81[https://source.chromium.org/chromium/chromium/src/+/main:third\_party/devtools-frontend/src/front\_end/panels/developer\_resources/DeveloperResourcesListView.ts;l=86](https://source.chromium.org/chromium/chromium/src/+/main:third_party/devtools-frontend/src/front_end/panels/developer_resources/DeveloperResourcesListView.ts;l=86)
82
83[https://source.chromium.org/chromium/chromium/src/+/main:third\_party/devtools-frontend/src/front\_end/panels/timeline/TimelineSelectorStatsView.ts;l=113](https://source.chromium.org/chromium/chromium/src/+/main:third_party/devtools-frontend/src/front_end/panels/timeline/TimelineSelectorStatsView.ts;l=113)
Alex Rudenkoc6499cc2025-02-17 09:08:2584
85
86### Unit tests
87
Alex Rudenkoe5c358e2025-02-17 11:31:5288When testing presenters, rely on observable effects such as view updates or model calls.
89
90#### View stubbing
Alex Rudenkoc6499cc2025-02-17 09:08:2591
92```ts
Alex Rudenko6f29fce2025-03-05 16:15:5193// ✅ recommended: stub the view function using createViewFunctionStub.
94import {createViewFunctionStub} from './ViewFunctionHelpers.js';
95const view = createViewFunctionStub(Presenter);
Alex Rudenkoc6499cc2025-02-17 09:08:2596const presenter = new Presenter(view);
97
98// ✅ recommended: expect a view stub call in response to presenter behavior.
Alex Rudenkoc6499cc2025-02-17 09:08:2599present.show();
Alex Rudenko6f29fce2025-03-05 16:15:51100const input = await view.nextInput;
Alex Rudenkoc6499cc2025-02-17 09:08:25101
102// ✅ recommended: expect a view stub call in response to an event from the view.
Alex Rudenko6f29fce2025-03-05 16:15:51103input.onEvent();
104assert.deepStrictEqual(await view.nextInput, {});
Alex Rudenkoc6499cc2025-02-17 09:08:25105
106// ❌ not recommended: Widget.updateComplete only reports a current view update
107// operation status and might create flakiness depending on doSomething() implementation.
108presenter.doSomething();
109await presenter.updateComplete;
110assert.deepStrictEqual(view.lastCall.args[0], {});
111
112// ❌ not recommended: awaiting for the present logic to finish might
113// not account for async or throttled view updates.
114await presenter.doSomething();
Alex Rudenkoe5c358e2025-02-17 11:31:52115// ❌ not recommended: it is easy for such assertions to
116// rely on the data not caused by the action being tested.
117sinon.assert.calledWith(view, sinon.match({ data: 'smth' }));
118```
119
120#### Model stubbing
121
122```ts
123// ✅ recommended: stub models that the presenter relies on.
124// Note there are many good ways to stub/mock models with sinon
125// depending on the use case and existing model code structure.
126const cssModel = sinon.createStubInstance(SDK.CSSModel.CSSModel);
127
128const presenter = new Presenter();
129// ✅ recommended: expect model calls as the result of invoking
Alex Rudenko6f29fce2025-03-05 16:15:51130// presenter's logic.
Alex Rudenkoe5c358e2025-02-17 11:31:52131const modelCall = expectCall(cssModel.headersForSourceURL, {
132 fakeFn: () => {
133 return false,
134 },
135});
136// ✅ recommended: expect view calls to result in output based
137// on the mocked model.
Alex Rudenko6f29fce2025-03-05 16:15:51138const viewCall = view.nextInput;
Alex Rudenkoe5c358e2025-02-17 11:31:52139
140presenter.doSomething();
141
142// ✅ recommended: assert arguments provided to model calls.
143const [url] = await modelCall;
144assert.strictEqual(url, '...');
145
Alex Rudenko6f29fce2025-03-05 16:15:51146assert.deepStrictEqual((await viewCall).headersForSourceURL, [{...}]);
Alex Rudenkoe5c358e2025-02-17 11:31:52147
148// ❌ not recommended: mocking CDP responses to make the models behave in a certain way
Alex Rudenko6f29fce2025-03-05 16:15:51149// while testing a presenter is fragile.
Alex Rudenkoe5c358e2025-02-17 11:31:52150setMockConnectionResponseHandler('CSS.getHeaders', () => ({}));
151const presenter = new Presenter();
152presenter.doSomething();
Simon Zündafade152025-05-06 11:13:38153```