forked from dart-lang/webdev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug_extension_test.dart
294 lines (261 loc) · 10 KB
/
debug_extension_test.dart
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// When run locally this test may require a manifest key. This makes it easy to
// just skip it.
@Tags(['extension'])
@Timeout(Duration(minutes: 2))
@OnPlatform({
'windows': Skip('https://github.com/dart-lang/webdev/issues/711'),
'linux': Skip('https://github.com/dart-lang/webdev/issues/2114'),
})
library;
import 'package:dwds/src/connections/debug_connection.dart';
import 'package:dwds/src/handlers/injector.dart';
import 'package:http/http.dart' as http;
import 'package:test/test.dart';
import 'package:test_common/test_sdk_configuration.dart';
// ignore: deprecated_member_use
import 'package:webdriver/io.dart';
import 'fixtures/context.dart';
import 'fixtures/project.dart';
import 'fixtures/utilities.dart';
// Instructions for running:
// * From the /dwds, run: dart test test/debug_extension_test.dart
// * See note for Googlers below as well
// [For Googlers]
// A whitelisted developer key is needed to run these tests locally.
// Add a developer key to dwds/debug_extension/build/web_prod/manifest.json.
// Otherwise, you will get 'Error Loading Extension' alert.
// Remove the key before pushing code to GitHub.
// See go/extension-identification.
void main() async {
final provider = TestSdkConfigurationProvider();
tearDownAll(provider.dispose);
final context = TestContext(TestProject.test, provider);
Future<void> waitForDartDevToolsWithRetry({
int retryCount = 6,
Duration retryWait = const Duration(seconds: 1),
}) async {
if (retryCount == 0) return;
final windows = await context.webDriver.windows.toList();
await context.webDriver.driver.switchTo.window(windows.last);
final title = await context.webDriver.title;
if (title == 'Dart DevTools') return;
await Future.delayed(retryWait);
return waitForDartDevToolsWithRetry(
retryCount: retryCount--,
retryWait: retryWait,
);
}
for (final useSse in [true, false]) {
group(useSse ? 'SSE' : 'WebSockets', () {
group('Without encoding', () {
setUp(() async {
await context.setUp(
debugSettings: TestDebugSettings.withDevTools(context).copyWith(
enableDebugExtension: true,
useSse: useSse,
),
);
await context.extensionConnection.sendCommand('Runtime.evaluate', {
'expression': 'fakeClick()',
});
// Wait for DevTools to actually open.
await waitForDartDevToolsWithRetry();
});
tearDown(() async {
await context.tearDown();
});
test('can launch DevTools', () async {
final windows = await context.webDriver.windows.toList();
await context.webDriver.driver.switchTo.window(windows.last);
expect(await context.webDriver.title, contains('Dart DevTools'));
expect(
await context.webDriver.currentUrl,
contains('ide=DebugExtension'),
);
});
test('can close DevTools and relaunch', () async {
for (final window in await context.webDriver.windows.toList()) {
await context.webDriver.driver.switchTo.window(window);
if (await context.webDriver.title == 'Dart DevTools') {
await window.close();
break;
}
}
// Relaunch DevTools by (fake) clicking the extension.
await context.extensionConnection.sendCommand('Runtime.evaluate', {
'expression': 'fakeClick()',
});
await waitForDartDevToolsWithRetry();
expect(await context.webDriver.title, 'Dart DevTools');
});
test('sends script parsed events', () async {
// Check if the extension debugger receives Debugger.ScriptParsed
// events for some important scripts.
final service = fetchChromeProxyService(context.debugConnection);
final scripts = service.remoteDebugger.scripts;
expect(
scripts.values.map((s) => s.url),
containsAllInOrder([
contains('stack_trace_mapper.dart.js'),
contains('hello_world/main.ddc.js'),
contains('packages/path/path.ddc.js'),
contains('dev_compiler/dart_sdk.js'),
contains('dwds/src/injected/client.js'),
]),
);
});
});
group('With a sharded Dart app', () {
setUp(() async {
await context.setUp(
debugSettings: TestDebugSettings.withDevTools(context).copyWith(
enableDebugExtension: true,
useSse: useSse,
),
);
final htmlTag =
await context.webDriver.findElement(const By.tagName('html'));
await context.webDriver.execute(
"arguments[0].setAttribute('data-multiple-dart-apps', 'true');",
[htmlTag],
);
});
tearDown(() async {
await context.tearDown();
});
test('opens an alert', () async {
await context.extensionConnection.sendCommand('Runtime.evaluate', {
'expression': 'fakeClick()',
});
// Wait for the alert to open.
final alert =
await retryFn<Alert>(() => context.webDriver.switchTo.alert);
expect(alert, isNotNull);
});
});
// TODO(elliette): Figure out a way to verify that the Dart panel is added
// to Chrome DevTools. This might not be possible to test with WebDriver,
// because WebDriver doesn't allow you to interact with Chrome DevTools.
group('With an internal Dart app', () {
setUp(() async {
await context.setUp(
debugSettings: TestDebugSettings.withDevTools(context).copyWith(
enableDebugExtension: true,
useSse: false,
),
);
final htmlTag =
await context.webDriver.findElement(const By.tagName('html'));
await context.webDriver.execute(
"arguments[0].setAttribute('data-ddr-dart-app', 'true');",
[htmlTag],
);
await context.extensionConnection.sendCommand('Runtime.evaluate', {
'expression': 'fakeClick()',
});
// Wait for DevTools to actually open.
await waitForDartDevToolsWithRetry();
});
tearDown(() async {
await context.tearDown();
});
test('can launch DevTools', () async {
final windows = await context.webDriver.windows.toList();
await context.webDriver.driver.switchTo.window(windows.last);
expect(await context.webDriver.title, 'Dart DevTools');
});
test('can close DevTools and relaunch', () async {
for (final window in await context.webDriver.windows.toList()) {
await context.webDriver.driver.switchTo.window(window);
if (await context.webDriver.title == 'Dart DevTools') {
await window.close();
break;
}
}
// Relaunch DevTools by (fake) clicking the extension.
await context.extensionConnection.sendCommand('Runtime.evaluate', {
'expression': 'fakeClick()',
});
await waitForDartDevToolsWithRetry();
expect(await context.webDriver.title, 'Dart DevTools');
});
test('sends script parsed events', () async {
// Check if the extension debugger receives Debugger.ScriptParsed
// events for some important scripts.
final service = fetchChromeProxyService(context.debugConnection);
final scripts = service.remoteDebugger.scripts;
expect(
scripts.values.map((s) => s.url),
containsAllInOrder([
contains('stack_trace_mapper.dart.js'),
contains('hello_world/main.ddc.js'),
contains('packages/path/path.ddc.js'),
contains('dev_compiler/dart_sdk.js'),
contains('dwds/src/injected/client.js'),
]),
);
});
});
});
}
group('With encoding', () {
setUp(() async {
await context.setUp(
debugSettings: TestDebugSettings.noDevTools().copyWith(
enableDebugExtension: true,
urlEncoder: (url) async =>
url.endsWith(r'/$debug') ? 'http://some-encoded-url:8081/' : url,
),
);
});
tearDown(() async {
await context.tearDown();
});
test('uses the encoded URI', () async {
final result = await http.get(
Uri.parse(
'http://localhost:${context.port}/hello_world/main.dart$bootstrapJsExtension',
),
);
expect(result.body.contains('dartExtensionUri'), isTrue);
expect(result.body.contains('http://some-encoded-url:8081/'), isTrue);
});
});
group('With "any" hostname', () {
final uriPattern = RegExp(r'dartExtensionUri = "([^"]+)";');
setUp(() async {
await context.setUp(
appMetadata: TestAppMetadata.externalApp().copyWith(hostname: 'any'),
debugSettings:
TestDebugSettings.noDevTools().copyWith(enableDebugExtension: true),
);
});
tearDown(() async {
await context.tearDown();
});
test('generates an extensionUri with a valid valid hostname', () async {
final result = await http.get(
Uri.parse(
'http://localhost:${context.port}/hello_world/main.dart$bootstrapJsExtension',
),
);
expect(result.body.contains('dartExtensionUri'), isTrue);
final extensionUri =
Uri.parse(uriPattern.firstMatch(result.body)!.group(1)!);
expect(
extensionUri.host,
anyOf(
// The hostname should've been mapped from "any" to one of the local
// loopback addresses/IPs.
equals('localhost'),
equals('127.0.0.1'),
equals('::'),
equals('::1'),
),
);
});
});
}