Switch _AnalysisDomainTest to PubPackageAnalysisServerTest.

Change-Id: I65404b3ff29cd5155ce67e4827f4337aef7183f3
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/235520
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
diff --git a/pkg/analysis_server/lib/src/analysis_server.dart b/pkg/analysis_server/lib/src/analysis_server.dart
index 1350c46..12ae415 100644
--- a/pkg/analysis_server/lib/src/analysis_server.dart
+++ b/pkg/analysis_server/lib/src/analysis_server.dart
@@ -703,8 +703,9 @@
 
   @override
   void applyFileRemoved(String file) {
-    sendAnalysisNotificationFlushResults(analysisServer, [file]);
-    filesToFlush.remove(file);
+    if (filesToFlush.remove(file)) {
+      sendAnalysisNotificationFlushResults(analysisServer, [file]);
+    }
   }
 
   @override
diff --git a/pkg/analysis_server/lib/src/utilities/mocks.dart b/pkg/analysis_server/lib/src/utilities/mocks.dart
index bef5102..6ad7371 100644
--- a/pkg/analysis_server/lib/src/utilities/mocks.dart
+++ b/pkg/analysis_server/lib/src/utilities/mocks.dart
@@ -23,7 +23,7 @@
   StreamController<Response> responseController =
       StreamController<Response>.broadcast();
   StreamController<Notification> notificationController =
-      StreamController<Notification>(sync: true);
+      StreamController<Notification>.broadcast(sync: true);
   Completer<Response>? errorCompleter;
 
   List<Response> responsesReceived = [];
@@ -35,6 +35,11 @@
 
   MockServerChannel();
 
+  /// Return the broadcast stream of notifications.
+  Stream<Notification> get notifications {
+    return notificationController.stream;
+  }
+
   @override
   Stream<Request> get requests => requestController.stream;
 
diff --git a/pkg/analysis_server/test/analysis_abstract.dart b/pkg/analysis_server/test/analysis_abstract.dart
index 3045889..df45776 100644
--- a/pkg/analysis_server/test/analysis_abstract.dart
+++ b/pkg/analysis_server/test/analysis_abstract.dart
@@ -202,11 +202,7 @@
     server = createAnalysisServer();
     server.pluginManager = pluginManager;
     handler = analysisHandler;
-    // listen for notifications
-    var notificationStream = serverChannel.notificationController.stream;
-    notificationStream.listen((Notification notification) {
-      processNotification(notification);
-    });
+    serverChannel.notifications.listen(processNotification);
   }
 
   @mustCallSuper
diff --git a/pkg/analysis_server/test/analysis_server_test.dart b/pkg/analysis_server/test/analysis_server_test.dart
index 3546ca2..9ac113c 100644
--- a/pkg/analysis_server/test/analysis_server_test.dart
+++ b/pkg/analysis_server/test/analysis_server_test.dart
@@ -135,10 +135,10 @@
 
     // Track diagnostics that arrive.
     final errorsByFile = <String, List<AnalysisError>>{};
-    channel.notificationController.stream
+    channel.notifications
         .where((notification) => notification.event == 'analysis.errors')
-        .listen((notificaton) {
-      final params = AnalysisErrorsParams.fromNotification(notificaton);
+        .listen((notification) {
+      final params = AnalysisErrorsParams.fromNotification(notification);
       errorsByFile[params.file] = params.errors;
     });
 
diff --git a/pkg/analysis_server/test/client/impl/completion_driver.dart b/pkg/analysis_server/test/client/impl/completion_driver.dart
index 63d8ec0..095db29 100644
--- a/pkg/analysis_server/test/client/impl/completion_driver.dart
+++ b/pkg/analysis_server/test/client/impl/completion_driver.dart
@@ -84,10 +84,7 @@
     required this.supportsAvailableSuggestions,
     required this.server,
   }) {
-    server.serverChannel.notificationController.stream
-        .listen((Notification notification) {
-      processNotification(notification);
-    });
+    server.serverChannel.notifications.listen(processNotification);
   }
 
   void addTestFile(String content, {int? offset}) {
diff --git a/pkg/analysis_server/test/domain_analysis_test.dart b/pkg/analysis_server/test/domain_analysis_test.dart
index fe0da92..21b79b3 100644
--- a/pkg/analysis_server/test/domain_analysis_test.dart
+++ b/pkg/analysis_server/test/domain_analysis_test.dart
@@ -25,6 +25,7 @@
 import 'package:test_reflective_loader/test_reflective_loader.dart';
 
 import 'analysis_abstract.dart';
+import 'domain_completion_test.dart';
 import 'mocks.dart';
 
 void main() {
@@ -44,8 +45,6 @@
 
   String get myPackageTestFilePath => '$myPackageLibPath/test.dart';
 
-  String get workspaceRootPath => '/workspace';
-
   @override
   void setUp() {
     super.setUp();
@@ -347,15 +346,8 @@
 
 @reflectiveTest
 class AnalysisDomainPubTest extends _AnalysisDomainTest {
-  String get testFilePath => '$testPackageLibPath/test.dart';
-
-  String get testPackageLibPath => '$testPackageRootPath/lib';
-
-  String get testPackageRootPath => '$workspaceRootPath/test';
-
-  String get workspaceRootPath => '/home';
-
   Future<void> test_fileSystem_addFile_analysisOptions() async {
+    deleteTestPackageAnalysisOptionsFile();
     var a_path = '$testPackageLibPath/a.dart';
     var b_path = '$testPackageLibPath/b.dart';
 
@@ -389,6 +381,7 @@
   }
 
   Future<void> test_fileSystem_addFile_analysisOptions_analysis() async {
+    deleteTestPackageAnalysisOptionsFile();
     var a_path = '$testPackageLibPath/a.dart';
     var options_path = '$testPackageRootPath/analysis_options.yaml';
 
@@ -460,16 +453,15 @@
   }
 
   Future<void> test_fileSystem_addFile_dart_dotFolder() async {
-    var a_path = '$projectPath/lib/.foo/a.dart';
-    var b_path = '$projectPath/lib/b.dart';
+    var a_path = '$testPackageLibPath/.foo/a.dart';
+    var b_path = '$testPackageLibPath/b.dart';
 
     newFile(b_path, content: r'''
 import '.foo/a.dart';
 void f(A a) {}
 ''');
 
-    await createProject();
-    await pumpEventQueue();
+    await setRoots(included: [workspaceRootPath], excluded: []);
     await server.onAnalysisComplete;
 
     // We don't have a.dart, so the import cannot be resolved.
@@ -489,10 +481,10 @@
   }
 
   Future<void> test_fileSystem_addFile_dart_excluded() async {
-    var a_path = '$projectPath/lib/a.dart';
-    var b_path = '$projectPath/lib/b.dart';
+    var a_path = '$testPackageLibPath/a.dart';
+    var b_path = '$testPackageLibPath/b.dart';
 
-    newAnalysisOptionsYamlFile(projectPath, content: r'''
+    newAnalysisOptionsYamlFile(testPackageRootPath, content: r'''
 analyzer:
   exclude:
     - "**/a.dart"
@@ -503,8 +495,7 @@
 void f(A a) {}
 ''');
 
-    await createProject();
-    await pumpEventQueue();
+    await setRoots(included: [workspaceRootPath], excluded: []);
     await server.onAnalysisComplete;
 
     // We don't have a.dart, so the import cannot be resolved.
@@ -524,6 +515,7 @@
   }
 
   Future<void> test_fileSystem_addFile_dotPackagesFile() async {
+    deleteTestPackageConfigJsonFile();
     var aaaLibPath = '/packages/aaa/lib';
     var a_path = '$aaaLibPath/a.dart';
 
@@ -562,12 +554,6 @@
 
     newFile('$testPackageLibPath/a.dart', content: '');
 
-    // Make sure that it is a package.
-    writePackageConfig(
-      '$testPackageRootPath/.dart_tool/package_config.json',
-      PackageConfigFileBuilder(),
-    );
-
     await setRoots(included: [workspaceRootPath], excluded: []);
 
     // No `fix_data.yaml` to analyze yet.
@@ -605,9 +591,9 @@
     assertHasErrors(testFilePath);
 
     // Write `package_config.json`, recreate analysis contexts.
-    writePackageConfig(
-      '$testPackageRootPath/.dart_tool/package_config.json',
-      PackageConfigFileBuilder()..add(name: 'aaa', rootPath: aaaRootPath),
+    writeTestPackageConfig(
+      config: PackageConfigFileBuilder()
+        ..add(name: 'aaa', rootPath: aaaRootPath),
     );
 
     await pumpEventQueue();
@@ -857,6 +843,7 @@
   }
 
   Future<void> test_fileSystem_changeFile_dotPackagesFile() async {
+    deleteTestPackageConfigJsonFile();
     var aaaLibPath = '/packages/aaa/lib';
     var a_path = '$aaaLibPath/a.dart';
 
@@ -898,12 +885,6 @@
 
     newFile('$testPackageLibPath/a.dart', content: '');
 
-    // Make sure that it is a package.
-    writePackageConfig(
-      '$testPackageRootPath/.dart_tool/package_config.json',
-      PackageConfigFileBuilder(),
-    );
-
     // This file has an error.
     newFile(path, content: '0: 1');
 
@@ -935,12 +916,6 @@
 class A {}
 ''');
 
-    // Write the empty file, without `package:aaa`.
-    writePackageConfig(
-      '$testPackageRootPath/.dart_tool/package_config.json',
-      PackageConfigFileBuilder(),
-    );
-
     newFile(testFilePath, content: '''
 import 'package:aaa/a.dart';
 void f(A a) {}
@@ -949,13 +924,14 @@
     await setRoots(included: [workspaceRootPath], excluded: []);
     await server.onAnalysisComplete;
 
+    // The default `package_config.json` is without `package:aaa`.
     // We cannot resolve `package:aaa/a.dart`
     assertHasErrors(testFilePath);
 
     // Write `package_config.json`, recreate analysis contexts.
-    writePackageConfig(
-      '$testPackageRootPath/.dart_tool/package_config.json',
-      PackageConfigFileBuilder()..add(name: 'aaa', rootPath: aaaRootPath),
+    writeTestPackageConfig(
+      config: PackageConfigFileBuilder()
+        ..add(name: 'aaa', rootPath: aaaRootPath),
     );
 
     await pumpEventQueue();
@@ -1090,6 +1066,7 @@
   }
 
   Future<void> test_fileSystem_deleteFile_dotPackagesFile() async {
+    deleteTestPackageConfigJsonFile();
     var aaaLibPath = '/packages/aaa/lib';
     var a_path = '$aaaLibPath/a.dart';
 
@@ -1130,12 +1107,6 @@
 
     newFile('$testPackageLibPath/a.dart', content: '');
 
-    // Make sure that it is a package.
-    writePackageConfig(
-      '$testPackageRootPath/.dart_tool/package_config.json',
-      PackageConfigFileBuilder(),
-    );
-
     // This file has an error.
     newFile(path, content: '0: 1');
 
@@ -1161,9 +1132,9 @@
 ''');
 
     // Write the empty file, without `package:aaa`.
-    writePackageConfig(
-      '$testPackageRootPath/.dart_tool/package_config.json',
-      PackageConfigFileBuilder()..add(name: 'aaa', rootPath: aaaRootPath),
+    writeTestPackageConfig(
+      config: PackageConfigFileBuilder()
+        ..add(name: 'aaa', rootPath: aaaRootPath),
     );
 
     newFile(testFilePath, content: '''
@@ -1193,6 +1164,7 @@
   }
 
   Future<void> test_setRoots_dotPackagesFile() async {
+    deleteTestPackageConfigJsonFile();
     var aaaLibPath = '/packages/aaa/lib';
     var a_path = '$aaaLibPath/a.dart';
 
@@ -1373,13 +1345,7 @@
   Future<void> test_setRoots_notDartFile_fixDataYaml() async {
     var path = '$testPackageLibPath/fix_data.yaml';
 
-    // Make sure that it is a package.
-    writePackageConfig(
-      '$testPackageRootPath/.dart_tool/package_config.json',
-      PackageConfigFileBuilder(),
-    );
-
-    // So, `lib/fix_data.yaml` will be analyzed.
+    // `lib/fix_data.yaml` will be analyzed.
     newFile(path, content: '0: 1');
 
     await setRoots(included: [workspaceRootPath], excluded: []);
@@ -1395,9 +1361,9 @@
 class A {}
 ''');
 
-    writePackageConfig(
-      '$testPackageRootPath/.dart_tool/package_config.json',
-      PackageConfigFileBuilder()..add(name: 'aaa', rootPath: aaaRootPath),
+    writeTestPackageConfig(
+      config: PackageConfigFileBuilder()
+        ..add(name: 'aaa', rootPath: aaaRootPath),
     );
 
     newFile(testFilePath, content: '''
@@ -1454,8 +1420,7 @@
         InstrumentationService.NULL_SERVICE);
     handler = AnalysisDomainHandler(server);
     // listen for notifications
-    var notificationStream = serverChannel.notificationController.stream;
-    notificationStream.listen((Notification notification) {
+    serverChannel.notifications.listen((Notification notification) {
       if (notification.event == ANALYSIS_NOTIFICATION_ERRORS) {
         var decoded = AnalysisErrorsParams.fromNotification(notification);
         filesErrors[decoded.file] = decoded.errors;
@@ -1763,7 +1728,7 @@
   }
 }
 
-class _AnalysisDomainTest extends AbstractAnalysisTest {
+class _AnalysisDomainTest extends PubPackageAnalysisServerTest {
   final Map<String, List<AnalysisError>> filesErrors = {};
 
   /// The files for which `analysis.flushResults` was received.
@@ -1801,10 +1766,6 @@
     }
   }
 
-  void writePackageConfig(String path, PackageConfigFileBuilder config) {
-    newFile(path, content: config.toContent(toUriStr: toUriStr));
-  }
-
   void _assertAnalyzedFiles({
     required List<String> hasErrors,
     List<String> noErrors = const [],
diff --git a/pkg/analysis_server/test/domain_completion_test.dart b/pkg/analysis_server/test/domain_completion_test.dart
index 0ecd2f7..37bf033 100644
--- a/pkg/analysis_server/test/domain_completion_test.dart
+++ b/pkg/analysis_server/test/domain_completion_test.dart
@@ -2951,10 +2951,20 @@
 
   String get workspaceRootPath => '/home';
 
+  void deleteTestPackageAnalysisOptionsFile() {
+    deleteAnalysisOptionsYamlFile(testPackageRootPath);
+  }
+
+  void deleteTestPackageConfigJsonFile() {
+    deletePackageConfigJsonFile(testPackageRootPath);
+  }
+
   Future<Response> handleRequest(Request request) async {
     return await serverChannel.sendRequest(request);
   }
 
+  void processNotification(Notification notification) {}
+
   Future<void> setRoots({
     required List<String> included,
     required List<String> excluded,
@@ -2988,6 +2998,8 @@
       ),
     );
 
+    serverChannel.notifications.listen(processNotification);
+
     server = AnalysisServer(
       serverChannel,
       resourceProvider,
diff --git a/pkg/analyzer/lib/src/test_utilities/resource_provider_mixin.dart b/pkg/analyzer/lib/src/test_utilities/resource_provider_mixin.dart
index c082771..867272e 100644
--- a/pkg/analyzer/lib/src/test_utilities/resource_provider_mixin.dart
+++ b/pkg/analyzer/lib/src/test_utilities/resource_provider_mixin.dart
@@ -14,6 +14,11 @@
 
   String convertPath(String path) => resourceProvider.convertPath(path);
 
+  void deleteAnalysisOptionsYamlFile(String directoryPath) {
+    var path = join(directoryPath, file_paths.analysisOptionsYaml);
+    deleteFile(path);
+  }
+
   void deleteFile(String path) {
     String convertedPath = convertPath(path);
     resourceProvider.deleteFile(convertedPath);
@@ -24,6 +29,15 @@
     resourceProvider.deleteFolder(convertedPath);
   }
 
+  void deletePackageConfigJsonFile(String directoryPath) {
+    var path = join(
+      directoryPath,
+      file_paths.dotDartTool,
+      file_paths.packageConfigJson,
+    );
+    deleteFile(path);
+  }
+
   File getFile(String path) {
     String convertedPath = convertPath(path);
     return resourceProvider.getFile(convertedPath);