Skip to content

Commit 4c05fb5

Browse files
OrKoNDevtools-frontend LUCI CQ
authored andcommitted
Move common unit tests to foundation
The only exception is the base64 decoder that requires the browser runtime. The CL uses `@ts-expect-error` where tsc is missing types coming from either DOM or node.js. Bug: 451502260 Change-Id: Idc90ae547d13df490b622dcf36c58f60cecb01a6 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/7112839 Reviewed-by: Simon Zünd <[email protected]> Commit-Queue: Alex Rudenko <[email protected]>
1 parent 64923ad commit 4c05fb5

File tree

16 files changed

+72
-46
lines changed

16 files changed

+72
-46
lines changed

front_end/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ group("unittests") {
276276

277277
group("foundation_unittests") {
278278
deps = [
279+
"core/common:foundation_unittests",
279280
"core/i18n:foundation_unittests",
280281
"models/ai_assistance:foundation_unittests",
281282
]

front_end/core/common/BUILD.gn

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,18 @@ devtools_entrypoint("bundle") {
7676
ts_library("unittests") {
7777
testonly = true
7878

79+
sources = [ "Base64.browser.test.ts" ]
80+
81+
deps = [
82+
":bundle",
83+
"../../testing",
84+
"../i18n:bundle",
85+
]
86+
}
87+
88+
devtools_foundation_module("foundation_unittests") {
89+
testonly = true
90+
7991
sources = [
8092
"Base64.test.ts",
8193
"CharacterIdMap.test.ts",
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2025 The Chromium Authors
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import * as Common from './common.js';
6+
7+
describe('Base64 encoder', () => {
8+
it('encodes ArrayBuffers correctly', async () => {
9+
const buffer1 = new Uint8Array([0]);
10+
assert.strictEqual(await Common.Base64.encode(buffer1.buffer), 'AA==');
11+
12+
const buffer2 = new Uint8Array([0, 1]);
13+
assert.strictEqual(await Common.Base64.encode(buffer2.buffer), 'AAE=');
14+
15+
const buffer3 = new Uint8Array([0, 1, 2]);
16+
assert.strictEqual(await Common.Base64.encode(buffer3.buffer), 'AAEC');
17+
});
18+
});

front_end/core/common/Base64.test.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as Common from './common.js';
66

77
describe('Base64 decoder', () => {
88
function decode(str: string) {
9+
// @ts-expect-error missing types in devtools_foundation_module.
910
const encoded = btoa(str);
1011
const decoded = Common.Base64.decode(encoded);
1112
const view = new DataView(decoded.buffer);
@@ -26,16 +27,3 @@ describe('Base64 decoder', () => {
2627
decode('ABCDEF'); // Unpadded: QUJDREVG
2728
});
2829
});
29-
30-
describe('Base64 encoder', () => {
31-
it('encodes ArrayBuffers correctly', async () => {
32-
const buffer1 = new Uint8Array([0]);
33-
assert.strictEqual(await Common.Base64.encode(buffer1.buffer), 'AA==');
34-
35-
const buffer2 = new Uint8Array([0, 1]);
36-
assert.strictEqual(await Common.Base64.encode(buffer2.buffer), 'AAE=');
37-
38-
const buffer3 = new Uint8Array([0, 1, 2]);
39-
assert.strictEqual(await Common.Base64.encode(buffer3.buffer), 'AAEC');
40-
});
41-
});

front_end/core/common/CharacterIdMap.test.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@ const CharacterIdMap = Common.CharacterIdMap.CharacterIdMap;
88

99
describe('CharacterIdMap class', () => {
1010
it('is able to convert an element to a character', () => {
11-
const testElement = document.createElement('p');
11+
const testElement = {};
1212
const characterIdMap = new CharacterIdMap();
1313
assert.strictEqual(characterIdMap.toChar(testElement), '!', 'element was not converted correctly');
1414
});
1515

1616
it('is able to convert a character to an element', () => {
17-
const testElement = document.createElement('p');
17+
const testElement = {};
1818
const characterIdMap = new CharacterIdMap();
1919
assert.strictEqual(characterIdMap.toChar(testElement), '!', 'element was not converted correctly');
2020
assert.strictEqual(characterIdMap.fromChar('!'), testElement, 'character was not converted correctly');
2121
});
2222

2323
it('returns the same character when trying to convert an element that was already converted', () => {
24-
const testElement = document.createElement('p');
24+
const testElement = {};
2525
const characterIdMap = new CharacterIdMap();
2626
assert.strictEqual(characterIdMap.toChar(testElement), '!', 'element was not converted correctly');
2727
assert.strictEqual(characterIdMap.toChar(testElement), '!', 'element was not converted correctly');
@@ -32,8 +32,9 @@ describe('CharacterIdMap class', () => {
3232
const characterIdMap = new CharacterIdMap();
3333
assert.throws(() => {
3434
for (let index = 0; index <= upperLimit; index++) {
35-
const el = document.createElement('div');
36-
el.setAttribute('id', 'Div' + index);
35+
const el = {
36+
id: 'Div' + index,
37+
};
3738
characterIdMap.toChar(el);
3839
}
3940
}, 'CharacterIdMap ran out of capacity!');

front_end/core/common/Debouncer.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ describe('Debouncer', () => {
1818
debouncedIncrement();
1919

2020
// Then wait a while before checking the value.
21+
// @ts-expect-error missing types in devtools_foundation_module.
2122
setTimeout(() => {
2223
assert.strictEqual(count, 1);
2324
done();

front_end/core/common/Debouncer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
* Debounce utility function, ensures that the function passed in is only called once the function stops being called and the delay has expired.
77
*/
88
export const debounce = function(func: (...args: any[]) => void, delay: number): (...args: any[]) => void {
9-
let timer = 0;
9+
let timer: ReturnType<typeof setTimeout>;
1010
const debounced = (...args: any[]): void => {
1111
clearTimeout(timer);
12-
timer = window.setTimeout(() => func(...args), delay);
12+
timer = setTimeout(() => func(...args), delay);
1313
};
1414
return debounced;
1515
};

front_end/core/common/Gzip.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ describe('Gzip', () => {
1414

1515
it('can compress and decompress a stream', async () => {
1616
const text = 'Hello, world! This is a stream test.';
17+
// @ts-expect-error missing types in devtools_foundation_module.
1718
const textEncoder = new TextEncoder();
19+
// @ts-expect-error missing types in devtools_foundation_module.
1820
const inputStream = new ReadableStream({
19-
start(controller) {
21+
// @ts-expect-error missing types in devtools_foundation_module.
22+
start(controller: ReadableStreamController<Uint8Array<ArrayBuffer>>) {
2023
controller.enqueue(textEncoder.encode(text));
2124
controller.close();
2225
},
@@ -25,7 +28,9 @@ describe('Gzip', () => {
2528
const compressedStream = Common.Gzip.compressStream(inputStream);
2629
const decompressedStream = Common.Gzip.decompressStream(compressedStream);
2730

31+
// @ts-expect-error missing types in devtools_foundation_module.
2832
const buffer = await new Response(decompressedStream).arrayBuffer();
33+
// @ts-expect-error missing types in devtools_foundation_module.
2934
const decodedText = new TextDecoder().decode(buffer);
3035

3136
assert.strictEqual(decodedText, text);
@@ -41,6 +46,7 @@ describe('arrayBufferToString', () => {
4146
});
4247
it('can decode a plaintext buffer', async () => {
4348
const text = 'Hello, buddy!';
49+
// @ts-expect-error missing types in devtools_foundation_module.
4450
const buffer = new TextEncoder().encode(text).buffer;
4551
const result = await Common.Gzip.arrayBufferToString(buffer);
4652
assert.strictEqual(result, text);
@@ -51,11 +57,13 @@ describe('fileToString', () => {
5157
it('can decompress a gzipped file', async () => {
5258
const text = '{"key": "value"}';
5359
const compressed = await Common.Gzip.compress(text);
60+
// @ts-expect-error missing types in devtools_foundation_module.
5461
const result = await Common.Gzip.fileToString(new File([compressed], 'file.json.gz', {type: 'application/gzip'}));
5562
assert.strictEqual(result, text);
5663
});
5764
it('can decode a plaintext file', async () => {
5865
const text = 'Hello, buddy!';
66+
// @ts-expect-error missing types in devtools_foundation_module.
5967
const file = new File([text], 'test.txt');
6068
const result = await Common.Gzip.fileToString(file);
6169
assert.strictEqual(result, text);

front_end/core/common/Gzip.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ async function gzipCodec(
5454
codecStream: CompressionStream|DecompressionStream): Promise<ArrayBuffer> {
5555
const readable = new ReadableStream({
5656
start(controller) {
57-
controller.enqueue(buffer);
57+
controller.enqueue(buffer instanceof ArrayBuffer ? new Uint8Array(buffer) : buffer);
5858
controller.close();
5959
}
6060
});

front_end/core/common/Mutex.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as Common from './common.js';
66

77
describe('Mutex', () => {
88
async function triggerMicroTaskQueue(): Promise<void> {
9+
// @ts-expect-error missing types in devtools_foundation_module.
910
await new Promise(resolve => setTimeout(resolve, 0));
1011
}
1112

0 commit comments

Comments
 (0)