1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
content / test / data / web_ui_mojo_ts_test.ts [blame]
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import {StringDictType, TestNode} from './web_ui_mojo_ts_test_mapped_types.js';
import {OptionalNumericsStruct, TestEnum, WebUITsMojoTestCache} from './web_ui_ts_test.test-mojom-webui.js';
const TEST_DATA: Array<{url: string, contents: string}> = [
{ url: 'https://google.com/', contents: 'i am in fact feeling lucky' },
{ url: 'https://youtube.com/', contents: 'probably cat videos?' },
{ url: 'https://example.com/', contents: 'internets wow' },
];
function assert(condition: any, msg: string) {
if (!condition) {
throw new Error('assertion failed: ' + msg);
}
}
function assertArrayEquals(a: Array<any>, b: Array<any>, msg: string) {
assert(a.length === b.length, msg);
for (let i = 0; i < a.length; ++i) {
assert(a[i] === b[i], msg);
}
}
function assertObjectEquals(a: any, b: any, msg: string) {
assert(Object.keys(a).length === Object.keys(b).length, msg);
for (let key of Object.keys(a)) {
assert(a[key] === b[key], msg);
}
}
async function doTest(): Promise<boolean> {
const cache = WebUITsMojoTestCache.getRemote();
for (const entry of TEST_DATA) {
cache.put({ url: entry.url }, entry.contents);
}
const {items} = await cache.getAll();
if (items.length != TEST_DATA.length) {
return false;
}
const entries: {[key: string]: string } = {};
for (const item of items) {
entries[item.url.url] = item.contents;
}
for (const entry of TEST_DATA) {
if (!(entry.url in entries)) {
return false;
}
if (entries[entry.url] != entry.contents) {
return false;
}
}
{
const testStruct: OptionalNumericsStruct = {
optionalBool: true,
optionalUint8: null,
optionalEnum: TestEnum.kOne,
};
const {
optionalBool,
optionalUint8,
optionalEnum,
optionalNumerics,
optionalBools,
optionalInts,
optionalEnums,
boolMap,
intMap,
enumMap
} =
await cache.echo(
true, null, TestEnum.kOne, testStruct, [], [], [], {}, {}, {}, '',
new TestNode(), null);
if (optionalBool !== false) {
return false;
}
if (optionalUint8 !== null) {
return false;
}
if (optionalEnum !== TestEnum.kTwo) {
return false;
}
if (optionalNumerics.optionalBool !== false) {
return false;
}
if (optionalNumerics.optionalUint8 !== null) {
return false;
}
if (optionalNumerics.optionalEnum !== TestEnum.kTwo) {
return false;
}
for (const arr of [optionalBools, optionalInts, optionalEnums]) {
assertArrayEquals(arr, [], 'empty array');
}
for (const map of [boolMap, intMap, enumMap]) {
assertObjectEquals(map, [], 'empty map');
}
}
{
const testStruct: OptionalNumericsStruct = {
optionalBool: null,
optionalUint8: 1,
optionalEnum: null,
};
const inOptionalBools = [false, null, true];
const inOptionalInts = [null, 0, 1, null];
const inOptionalEnums = [null, 0, null, 1, null];
const inBoolMap = {0: true, 1: false, 2: null};
const inIntMap = {0: 0, 2: null};
const inEnumMap = {0: 0, 1: null};
const {
optionalBool,
optionalUint8,
optionalEnum,
optionalNumerics,
optionalBools,
optionalInts,
optionalEnums,
boolMap,
intMap,
enumMap
} =
await cache.echo(
null, 1, null, testStruct, inOptionalBools, inOptionalInts,
inOptionalEnums, inBoolMap, inIntMap, inEnumMap, '', new TestNode(),
null);
if (optionalBool !== null) {
return false;
}
if (optionalUint8 !== 254) {
return false;
}
if (optionalEnum !== null) {
return false;
}
if (optionalNumerics.optionalBool !== null) {
return false;
}
if (optionalNumerics.optionalUint8 !== 254) {
return false;
}
if (optionalNumerics.optionalEnum !== null) {
return false;
}
assertArrayEquals(inOptionalBools, optionalBools, 'optional bools');
assertArrayEquals(inOptionalInts, optionalInts, 'optional ints');
assertArrayEquals(inOptionalEnums, optionalEnums, 'optional enums');
assertObjectEquals(inBoolMap, boolMap, 'bool map');
assertObjectEquals(inIntMap, intMap, 'bool int');
assertObjectEquals(inEnumMap, enumMap, 'enum map');
}
const testStruct: OptionalNumericsStruct = {
optionalBool: null,
optionalUint8: null,
optionalEnum: null,
};
// Test simple mapped type where a struct is mapped to a string.
{
const str = 'foobear';
const result = await cache.echo(
null, 1, null, testStruct, [], [], [], {}, {}, {}, str, new TestNode(),
null);
if (result.simpleMappedType !== str) {
return false;
}
}
// Tests an empty nested struct to test basic encoding/decoding.
{
const result = await cache.echo(
null, 1, null, testStruct, [], [], [], {}, {}, {}, '', new TestNode(),
null);
assertObjectEquals(
new TestNode(), result.nestedMappedType,
'nested mappped type: got: ' + JSON.stringify(result.nestedMappedType) +
', expected: {next: null}');
}
// Tests a nested type where a struct includes itself.
{
const depth = 10;
const chain: TestNode = new TestNode();
let cursor = chain;
for (let i = 0; i < depth; ++i) {
cursor = cursor!.next = new TestNode();
}
const result = await cache.echo(
null, 1, null, testStruct, [], [], [], {}, {}, {}, '', chain, null);
if (JSON.stringify(chain) !== JSON.stringify(result.nestedMappedType)) {
throw new Error(
'nested mappped type: got: ' +
JSON.stringify(result.nestedMappedType) +
', expected: ' + JSON.stringify(chain));
}
}
{
let map: StringDictType = new Map<string, string>();
map.set('foo', 'bear');
map.set('some', 'where');
const result = await cache.echo(
null, 1, null, testStruct, [], [], [], {}, {}, {}, '', new TestNode(),
map);
for (const key of map.keys()) {
assert(
result.otherMappedType!.get(key) === map.get(key),
`Expected value: ${map.get(key)} for key: ${key}, got: ${
result.otherMappedType!.get(key)}`);
}
}
return true;
}
async function runTest(): Promise<boolean> {
return doTest();
}
Object.assign(window, {runTest});