fix test chrome.close can recover if getTab throws a StateError (#154889)

Fixes https://github.com/flutter/flutter/issues/154857.

Does so by:
* adding `await chromiumLauncher.connect(chrome, false);` before the `close` call to make sure we enter[ the block ](9cd2fc90af/packages/flutter_tools/lib/src/web/chrome.dart (L521-L535))that actually tries to close chromium
* adding an `onGetTab` callback to `FakeChromeConnectionWithTab`, which the test now uses to throw a StateError upon `getTab` getting called.

## How I verified this change

1. Change `Chromium.close` from using the safer `getChromeTabGuarded` function to using the previous method of calling `ChromeConnection.getTab` directly. Do so by applying this diff:

```diff
diff --git a/packages/flutter_tools/lib/src/web/chrome.dart b/packages/flutter_tools/lib/src/web/chrome.dart
index c9a5fdab81..81bc246ff9 100644
--- a/packages/flutter_tools/lib/src/web/chrome.dart
+++ b/packages/flutter_tools/lib/src/web/chrome.dart
@@ -520,7 +520,7 @@ class Chromium {
     Duration sigtermDelay = Duration.zero;
     if (_hasValidChromeConnection) {
       try {
-        final ChromeTab? tab = await getChromeTabGuarded(chromeConnection,
+        final ChromeTab? tab = await chromeConnection.getTab(
             (_) => true, retryFor: const Duration(seconds: 1));
         if (tab != null) {
           final WipConnection wipConnection = await tab.connect();
```

2. Then, run the test, which should correctly fail:
```
dart test test/web.shard/chrome_test.dart --name="chrome.close can recover if getTab throws a StateError"`
```
3. Revert the change from step 1 and run again. The test should now pass.
This commit is contained in:
Andrew Kolos 2024-09-10 10:23:56 -07:00 committed by GitHub
parent 24d0b1db0a
commit d88e692895
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -800,9 +800,10 @@ void main() {
testWithoutContext('chrome.close can recover if getTab throws a StateError', () async {
final BufferLogger logger = BufferLogger.test();
final FakeChromeConnection chromeConnection = FakeChromeConnection(
maxRetries: 4,
error: StateError('Client is closed.'),
final FakeChromeConnectionWithTab chromeConnection = FakeChromeConnectionWithTab(
onGetTab: () {
throw StateError('Client is closed.');
},
);
final ChromiumLauncher chromiumLauncher = ChromiumLauncher(
fileSystem: fileSystem,
@ -813,7 +814,14 @@ void main() {
logger: logger,
);
final FakeProcess process = FakeProcess();
final Chromium chrome = Chromium(0, chromeConnection, chromiumLauncher: chromiumLauncher, process: process, logger: logger,);
final Chromium chrome = Chromium(
0,
chromeConnection,
chromiumLauncher: chromiumLauncher,
process: process,
logger: logger,
);
await chromiumLauncher.connect(chrome, false);
await chrome.close();
expect(logger.errorText, isEmpty);
});
@ -944,14 +952,19 @@ typedef OnSendCommand = void Function(String);
/// Fake chrome connection that returns a tab.
class FakeChromeConnectionWithTab extends Fake implements ChromeConnection {
FakeChromeConnectionWithTab({OnSendCommand? onSendCommand, bool throwWebSocketException = false})
: _tab = FakeChromeTab(onSendCommand, throwWebSocketException);
FakeChromeConnectionWithTab({
OnSendCommand? onSendCommand,
this.onGetTab,
bool throwWebSocketException = false,
}) : _tab = FakeChromeTab(onSendCommand, throwWebSocketException);
final FakeChromeTab _tab;
void Function()? onGetTab;
bool throwSocketExceptions = false;
@override
Future<ChromeTab?> getTab(bool Function(ChromeTab tab) accept, {Duration? retryFor}) async {
onGetTab?.call();
if (throwSocketExceptions) {
throw const io.SocketException('test');
}