blob: 31d1a1d4ce3a716807169e637fed28ecde5e4104 [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
41## Examples
42
43```html
44<devtools-widget .config=${widgetConfig(ElementsPanel)}>
45 <devtools-split-widget>
Mathias Bynensae705ff2025-02-12 09:51:2346 <devtools-widget slot="main".config=${widgetConfig(ElementsTree)}></devtools-widget>
47 <devtools-tab-pane slot="sidebar">
48 <devtools-widget .config=${widgetConfig(StylesPane, {element: input.element})}></devtools-widget>
49 <devtools-widget .config=${widgetConfig(ComputedPane, {element: input.element})}></devtools-widget>
50 ...
Danil Somsikovd884ae72025-02-12 07:56:1351 </devtools-tab-pane>
52 </devtools-split-widget>
53</devtools-widget>
54```
55
56```ts
57class StylesPane extends UI.Widget {
58 constructor(element, view = (input, output, target) => {
59 render(html`
Mathias Bynensae705ff2025-02-12 09:51:2360 <devtools-widget .config=${widgetConfig(MetricsPane, {element: input.element})}>
61 </devtools-widget>
Danil Somsikovd884ae72025-02-12 07:56:1362 <devtools-toolbar>
Mathias Bynensae705ff2025-02-12 09:51:2363 <devtools-filter-input @change=${input.onFilter}></devtools-filter-input>
64 <devtools-checkbox @change=${input.onShowAll}>Show All</devtools-checkbox>
65 <devtools-checkbox @change=${input.onGroup}>Group</devtools-checkbox>
Danil Somsikovd884ae72025-02-12 07:56:1366 </devtools-toolbar>
67 <devtools-tree-outline>
68 ${input.properties.map(p => html`<li>
69 <dt>${p.key}</dt><dd>${renderValue(p.value)}</dd>
70 <ol>${p.subproperties.map(...)}
71 </li>`)}
72 </devtools-tree-outline>`
73 }
74}
75```
76
77[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)
78
79[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)
80
81[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:2582
83
84### Unit tests
85
Alex Rudenkoe5c358e2025-02-17 11:31:5286When testing presenters, rely on observable effects such as view updates or model calls.
87
88#### View stubbing
Alex Rudenkoc6499cc2025-02-17 09:08:2589
90```ts
91// ✅ recommended: stub the view function.
92const view = sinon.stub();
93const presenter = new Presenter(view);
94
95// ✅ recommended: expect a view stub call in response to presenter behavior.
96const viewCall = expectCall(view);
97present.show();
98const [{onEvent}] = await viewCall;
99
100// ✅ recommended: expect a view stub call in response to an event from the view.
101const viewCall2 = expectCall(view);
102onEvent();
103const [{data}] = await viewCall2;
Alex Rudenkoe5c358e2025-02-17 11:31:52104// ✅ recommended: check view input data from the expected call.
Alex Rudenkoc6499cc2025-02-17 09:08:25105assert.deepStrictEqual(data, {});
106
107// ❌ not recommended: Widget.updateComplete only reports a current view update
108// operation status and might create flakiness depending on doSomething() implementation.
109presenter.doSomething();
110await presenter.updateComplete;
111assert.deepStrictEqual(view.lastCall.args[0], {});
112
113// ❌ not recommended: awaiting for the present logic to finish might
114// not account for async or throttled view updates.
115await presenter.doSomething();
Alex Rudenkoe5c358e2025-02-17 11:31:52116// ❌ not recommended: it is easy for such assertions to
117// rely on the data not caused by the action being tested.
118sinon.assert.calledWith(view, sinon.match({ data: 'smth' }));
119```
120
121#### Model stubbing
122
123```ts
124// ✅ recommended: stub models that the presenter relies on.
125// Note there are many good ways to stub/mock models with sinon
126// depending on the use case and existing model code structure.
127const cssModel = sinon.createStubInstance(SDK.CSSModel.CSSModel);
128
129const presenter = new Presenter();
130// ✅ recommended: expect model calls as the result of invoking
131// present's logic.
132const modelCall = expectCall(cssModel.headersForSourceURL, {
133 fakeFn: () => {
134 return false,
135 },
136});
137// ✅ recommended: expect view calls to result in output based
138// on the mocked model.
139const viewCall = expectCall(view);
140
141presenter.doSomething();
142
143// ✅ recommended: assert arguments provided to model calls.
144const [url] = await modelCall;
145assert.strictEqual(url, '...');
146
147const [{headersForSourceURL}] = await viewCall;
148assert.deepStrictEqual(headersForSourceURL, [{...}]);
149
150// ❌ not recommended: mocking CDP responses to make the models behave in a certain way
151// is fragile.
152setMockConnectionResponseHandler('CSS.getHeaders', () => ({}));
153const presenter = new Presenter();
154presenter.doSomething();
Alex Rudenkoc6499cc2025-02-17 09:08:25155```