Use getResolvedUnit(File) in AbstractContextTest.

Change-Id: Ic6237185b8a8c8b62de470490873703672a203e0
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/308581
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
diff --git a/pkg/analysis_server/test/abstract_context.dart b/pkg/analysis_server/test/abstract_context.dart
index 351cefd..9793b4a 100644
--- a/pkg/analysis_server/test/abstract_context.dart
+++ b/pkg/analysis_server/test/abstract_context.dart
@@ -33,6 +33,10 @@
   final Map<String, String> _declaredVariables = {};
   AnalysisContextCollectionImpl? _analysisContextCollection;
 
+  /// If not `null`, [getResolvedUnit] will use the context that corresponds
+  /// to this file, instead of the given file.
+  File? fileForContextSelection;
+
   /// TODO(scheglov) Stop writing into it. Convert into getter.
   late String testFilePath = '$testPackageLibPath/test.dart';
 
@@ -169,8 +173,12 @@
     return _contextFor(file).driver;
   }
 
-  Future<ResolvedUnitResult> getResolvedUnit(String path) async =>
-      await (await session).getResolvedUnit(path) as ResolvedUnitResult;
+  Future<ResolvedUnitResult> getResolvedUnit(File file) async {
+    final path = file.path;
+    final session = await sessionFor(fileForContextSelection ?? file);
+    final result = await session.getResolvedUnit(path);
+    return result as ResolvedUnitResult;
+  }
 
   @override
   File newFile(String path, String content) {
@@ -183,12 +191,6 @@
     return file;
   }
 
-  Future<ResolvedUnitResult> resolveFile(File file) async {
-    final path = file.path;
-    var session = await sessionFor(file);
-    return await session.getResolvedUnit(path) as ResolvedUnitResult;
-  }
-
   Future<AnalysisSession> sessionFor(File file) async {
     var analysisContext = _contextFor(file);
     await analysisContext.applyPendingFileChanges();
diff --git a/pkg/analysis_server/test/abstract_single_unit.dart b/pkg/analysis_server/test/abstract_single_unit.dart
index 6504a90..fa852ca 100644
--- a/pkg/analysis_server/test/abstract_single_unit.dart
+++ b/pkg/analysis_server/test/abstract_single_unit.dart
@@ -46,19 +46,8 @@
   }
 
   @override
-  File newFile(String path, String content) {
-    content = normalizeSource(content);
-    return super.newFile(path, content);
-  }
-
-  /// Convenient function to normalize newlines in [code] for the current
-  /// platform if [useLineEndingsForPlatform] is `true`.
-  String normalizeSource(String code) =>
-      useLineEndingsForPlatform ? normalizeNewlinesForPlatform(code) : code;
-
-  @override
-  Future<ResolvedUnitResult> resolveFile(File file) async {
-    var result = await super.resolveFile(file);
+  Future<ResolvedUnitResult> getResolvedUnit(File file) async {
+    var result = await super.getResolvedUnit(file);
     testAnalysisResult = result;
     testCode = result.content;
     testUnit = result.unit;
@@ -80,12 +69,23 @@
     return result;
   }
 
+  @override
+  File newFile(String path, String content) {
+    content = normalizeSource(content);
+    return super.newFile(path, content);
+  }
+
+  /// Convenient function to normalize newlines in [code] for the current
+  /// platform if [useLineEndingsForPlatform] is `true`.
+  String normalizeSource(String code) =>
+      useLineEndingsForPlatform ? normalizeNewlinesForPlatform(code) : code;
+
   Future<void> resolveTestCode(String code) async {
     addTestSource(code);
     await resolveTestFile();
   }
 
   Future<void> resolveTestFile() async {
-    await resolveFile(testFile);
+    await getResolvedUnit(testFile);
   }
 }
diff --git a/pkg/analysis_server/test/services/completion/dart/completion_contributor_util.dart b/pkg/analysis_server/test/services/completion/dart/completion_contributor_util.dart
index a34d939..b09f806 100644
--- a/pkg/analysis_server/test/services/completion/dart/completion_contributor_util.dart
+++ b/pkg/analysis_server/test/services/completion/dart/completion_contributor_util.dart
@@ -560,7 +560,7 @@
       DartCompletionRequest request);
 
   Future<void> computeSuggestions({int times = 200}) async {
-    result = await getResolvedUnit(testFile.path);
+    result = await getResolvedUnit(testFile);
 
     // Build the request
     var request = DartCompletionRequest.forResolvedUnit(
diff --git a/pkg/analysis_server/test/services/completion/dart/completion_manager_test.dart b/pkg/analysis_server/test/services/completion/dart/completion_manager_test.dart
index d066fd8..6331be0 100644
--- a/pkg/analysis_server/test/services/completion/dart/completion_manager_test.dart
+++ b/pkg/analysis_server/test/services/completion/dart/completion_manager_test.dart
@@ -46,10 +46,10 @@
 ''');
     addTestSource('part of libB; void f() {^}');
 
-    await resolveFile(b);
+    await getResolvedUnit(b);
 
     // Build the request
-    var resolvedUnit = await getResolvedUnit(testFile.path);
+    var resolvedUnit = await getResolvedUnit(testFile);
     request = DartCompletionRequest.forResolvedUnit(
       resolvedUnit: resolvedUnit,
       offset: completionOffset,
diff --git a/pkg/analysis_server/test/services/correction/organize_directives_test.dart b/pkg/analysis_server/test/services/correction/organize_directives_test.dart
index d1ba193..ac10f74 100644
--- a/pkg/analysis_server/test/services/correction/organize_directives_test.dart
+++ b/pkg/analysis_server/test/services/correction/organize_directives_test.dart
@@ -942,7 +942,8 @@
 
   Future<void> _computeUnitAndErrors(String code) async {
     addTestSource(code);
-    var result = await getResolvedUnit(testFile.path);
+    verifyNoTestUnitErrors = false;
+    var result = await getResolvedUnit(testFile);
     testUnit = result.unit;
     testErrors = result.errors;
   }
diff --git a/pkg/analysis_server/test/services/refactoring/legacy/convert_getter_to_method_test.dart b/pkg/analysis_server/test/services/refactoring/legacy/convert_getter_to_method_test.dart
index b68b707..0ec772c 100644
--- a/pkg/analysis_server/test/services/refactoring/legacy/convert_getter_to_method_test.dart
+++ b/pkg/analysis_server/test/services/refactoring/legacy/convert_getter_to_method_test.dart
@@ -145,11 +145,10 @@
 
   Future<void> test_checkInitialConditions_outsideOfProject() async {
     // File outside of project.
-    var externalPath = convertPath('$workspaceRootPath/aaa/lib/a.dart');
-    newFile(externalPath, r'''
+    var externalFile = newFile('$workspaceRootPath/aaa/lib/a.dart', r'''
 String get foo => '';
 ''');
-    var externalUnit = await getResolvedUnit(externalPath);
+    var externalUnit = await getResolvedUnit(externalFile);
 
     await indexTestUnit(''); // Initialize project.
 
diff --git a/pkg/analysis_server/test/services/refactoring/legacy/convert_method_to_getter_test.dart b/pkg/analysis_server/test/services/refactoring/legacy/convert_method_to_getter_test.dart
index 78edddb..396bc2c 100644
--- a/pkg/analysis_server/test/services/refactoring/legacy/convert_method_to_getter_test.dart
+++ b/pkg/analysis_server/test/services/refactoring/legacy/convert_method_to_getter_test.dart
@@ -177,11 +177,10 @@
 
   Future<void> test_checkInitialConditions_outsideOfProject() async {
     // File outside of project.
-    var externalPath = convertPath('$workspaceRootPath/aaa/lib/a.dart');
-    newFile(externalPath, r'''
+    var externalFile = newFile('$workspaceRootPath/aaa/lib/a.dart', r'''
 String foo() => '';
 ''');
-    var externalUnit = await getResolvedUnit(externalPath);
+    var externalUnit = await getResolvedUnit(externalFile);
 
     await indexTestUnit(''); // Initialize project.
 
diff --git a/pkg/analysis_server/test/services/refactoring/legacy/move_file_test.dart b/pkg/analysis_server/test/services/refactoring/legacy/move_file_test.dart
index 8073a3c..53fec62 100644
--- a/pkg/analysis_server/test/services/refactoring/legacy/move_file_test.dart
+++ b/pkg/analysis_server/test/services/refactoring/legacy/move_file_test.dart
@@ -63,7 +63,7 @@
     // Since the file being refactored isn't the test source, we set the
     // testAnalysisResult manually here, the path is referenced through the
     // referenced File object to run on Windows:
-    testAnalysisResult = await getResolvedUnit(file.path);
+    testAnalysisResult = await getResolvedUnit(file);
 
     _createRefactoring('$testPackageLibPath/222/new_name.dart',
         oldFile: file.path);
@@ -85,7 +85,7 @@
     // Since the file being refactored isn't the test source, we set the
     // testAnalysisResult manually here, the path is referenced through the
     // referenced File object to run on Windows:
-    testAnalysisResult = await getResolvedUnit(file.path);
+    testAnalysisResult = await getResolvedUnit(file);
 
     _createRefactoring('/home/test0/test1/test3/lib/111/name.dart',
         oldFile: file.path);
@@ -107,7 +107,7 @@
     // Since the file being refactored isn't the test source, we set the
     // testAnalysisResult manually here, the path is referenced through the
     // referenced File object to run on Windows:
-    testAnalysisResult = await getResolvedUnit(file.path);
+    testAnalysisResult = await getResolvedUnit(file);
 
     _createRefactoring('/home/test0/test1/test2/test3/lib/111/name.dart',
         oldFile: file.path);
@@ -129,7 +129,7 @@
     // Since the file being refactored isn't the test source, we set the
     // testAnalysisResult manually here, the path is referenced through the
     // referenced File object to run on Windows:
-    testAnalysisResult = await getResolvedUnit(file.path);
+    testAnalysisResult = await getResolvedUnit(file);
 
     _createRefactoring('/home/test0/test1/lib/111/name.dart',
         oldFile: file.path);
@@ -150,7 +150,7 @@
     // Since the file being refactored isn't the test source, we set the
     // testAnalysisResult manually here, the path is referenced through the
     // referenced File object to run on Windows:
-    testAnalysisResult = await getResolvedUnit(file.path);
+    testAnalysisResult = await getResolvedUnit(file);
 
     _createRefactoring('$testPackageLibPath/222/new_name.dart',
         oldFile: file.path);
@@ -171,7 +171,7 @@
     // Since the file being refactored isn't the test source, we set the
     // testAnalysisResult manually here, the path is referenced through the
     // referenced File object to run on Windows:
-    testAnalysisResult = await getResolvedUnit(file.path);
+    testAnalysisResult = await getResolvedUnit(file);
 
     _createRefactoring('$testPackageLibPath/new_name.dart', oldFile: file.path);
     await _assertSuccessfulRefactoring();
diff --git a/pkg/analysis_server/test/services/search/search_engine_test.dart b/pkg/analysis_server/test/services/search/search_engine_test.dart
index 062c625..07d4320 100644
--- a/pkg/analysis_server/test/services/search/search_engine_test.dart
+++ b/pkg/analysis_server/test/services/search/search_engine_test.dart
@@ -36,7 +36,7 @@
 
   /// Resolve the [file] into [result].
   Future<void> resolveFile2(File file) async {
-    result = await resolveFile(file);
+    result = await getResolvedUnit(file);
 
     findNode = FindNode(result.content, result.unit);
     findElement = FindElement(result.unit);
diff --git a/pkg/analysis_server/test/src/computer/call_hierarchy_computer_test.dart b/pkg/analysis_server/test/src/computer/call_hierarchy_computer_test.dart
index 0894168..ba348ef 100644
--- a/pkg/analysis_server/test/src/computer/call_hierarchy_computer_test.dart
+++ b/pkg/analysis_server/test/src/computer/call_hierarchy_computer_test.dart
@@ -73,7 +73,7 @@
     expect(marker, greaterThanOrEqualTo(0));
     addTestSource(withoutMarkers(code));
 
-    final result = await getResolvedUnit(testFile.path);
+    final result = await getResolvedUnit(testFile);
 
     return DartCallHierarchyComputer(result).findTarget(marker);
   }
@@ -287,10 +287,10 @@
 
   Future<void> test_functionCall() async {
     final contents = '''
-import 'other.dart' as f;
+import 'other.dart' as other;
 
 void f() {
-  f.myFun^ction();
+  other.myFun^ction();
 }
     ''';
 
@@ -658,7 +658,8 @@
   Future<List<CallHierarchyCalls>> findIncomingCallsForTarget(
     CallHierarchyItem target,
   ) async {
-    final result = await getResolvedUnit(target.file);
+    final targetFile = getFile(target.file);
+    final result = await getResolvedUnit(targetFile);
     expect(result.errors, isEmpty);
 
     return DartCallHierarchyComputer(result)
@@ -1161,7 +1162,8 @@
   Future<List<CallHierarchyCalls>> findOutgoingCallsForTarget(
     CallHierarchyItem target,
   ) async {
-    final result = await getResolvedUnit(target.file);
+    final targetFile = getFile(target.file);
+    final result = await getResolvedUnit(targetFile);
     expect(result.errors, isEmpty);
 
     return DartCallHierarchyComputer(result).findOutgoingCalls(target);
diff --git a/pkg/analysis_server/test/src/computer/closing_labels_computer_test.dart b/pkg/analysis_server/test/src/computer/closing_labels_computer_test.dart
index a31d4be..72bcc58 100644
--- a/pkg/analysis_server/test/src/computer/closing_labels_computer_test.dart
+++ b/pkg/analysis_server/test/src/computer/closing_labels_computer_test.dart
@@ -395,8 +395,8 @@
   }
 
   Future<List<ClosingLabel>> _computeElements(String sourceContent) async {
-    newFile(sourcePath, sourceContent);
-    var result = await getResolvedUnit(sourcePath);
+    final file = newFile(sourcePath, sourceContent);
+    var result = await getResolvedUnit(file);
     var computer = DartUnitClosingLabelsComputer(result.lineInfo, result.unit);
     return computer.compute();
   }
diff --git a/pkg/analysis_server/test/src/computer/color_computer_test.dart b/pkg/analysis_server/test/src/computer/color_computer_test.dart
index c660341..00d522b 100644
--- a/pkg/analysis_server/test/src/computer/color_computer_test.dart
+++ b/pkg/analysis_server/test/src/computer/color_computer_test.dart
@@ -129,12 +129,12 @@
 
     newFile(testPath, dartCode);
     if (otherCode != null) {
-      newFile(otherPath, otherCode);
-      final otherResult = await getResolvedUnit(otherPath);
+      final otherFile = newFile(otherPath, otherCode);
+      final otherResult = await getResolvedUnit(otherFile);
       expectNoErrors(otherResult);
     }
 
-    final result = await getResolvedUnit(testPath);
+    final result = await getResolvedUnit(testFile);
     expectNoErrors(result);
 
     computer = ColorComputer(result);
diff --git a/pkg/analysis_server/test/src/computer/folding_computer_test.dart b/pkg/analysis_server/test/src/computer/folding_computer_test.dart
index 3e0806c..cfd2856 100644
--- a/pkg/analysis_server/test/src/computer/folding_computer_test.dart
+++ b/pkg/analysis_server/test/src/computer/folding_computer_test.dart
@@ -759,8 +759,8 @@
 
   Future<void> _computeRegions(String sourceContent) async {
     code = TestCode.parse(sourceContent);
-    newFile(sourcePath, code.code);
-    var result = await getResolvedUnit(sourcePath);
+    final file = newFile(sourcePath, code.code);
+    var result = await getResolvedUnit(file);
     var computer = DartUnitFoldingComputer(result.lineInfo, result.unit);
     regions = computer.compute();
   }
diff --git a/pkg/analysis_server/test/src/computer/highlights_computer_test.dart b/pkg/analysis_server/test/src/computer/highlights_computer_test.dart
index a37e514..3e4dfd7 100644
--- a/pkg/analysis_server/test/src/computer/highlights_computer_test.dart
+++ b/pkg/analysis_server/test/src/computer/highlights_computer_test.dart
@@ -111,8 +111,8 @@
     bool hasErrors = false,
   }) async {
     this.content = content;
-    newFile(sourcePath, content);
-    var result = await getResolvedUnit(sourcePath);
+    final file = newFile(sourcePath, content);
+    var result = await getResolvedUnit(file);
 
     if (hasErrors) {
       expect(result.errors, isNotEmpty);
diff --git a/pkg/analysis_server/test/src/computer/import_elements_computer_test.dart b/pkg/analysis_server/test/src/computer/import_elements_computer_test.dart
index 9c60270..e97c66c 100644
--- a/pkg/analysis_server/test/src/computer/import_elements_computer_test.dart
+++ b/pkg/analysis_server/test/src/computer/import_elements_computer_test.dart
@@ -50,8 +50,8 @@
 
   Future<void> createBuilder(String content) async {
     originalContent = content;
-    newFile(path, content);
-    var result = await getResolvedUnit(path);
+    final file = newFile(path, content);
+    var result = await getResolvedUnit(file);
     computer = ImportElementsComputer(resourceProvider, result);
   }
 
diff --git a/pkg/analysis_server/test/src/computer/imported_elements_computer_test.dart b/pkg/analysis_server/test/src/computer/imported_elements_computer_test.dart
index 2d1cbab..706a8aa 100644
--- a/pkg/analysis_server/test/src/computer/imported_elements_computer_test.dart
+++ b/pkg/analysis_server/test/src/computer/imported_elements_computer_test.dart
@@ -468,8 +468,8 @@
 
   Future<void> _computeElements(String content, String selection) async {
     // TODO(brianwilkerson) Automatically extract the selection from the content.
-    newFile(sourcePath, content);
-    var result = await getResolvedUnit(sourcePath);
+    final file = newFile(sourcePath, content);
+    var result = await getResolvedUnit(file);
     var computer = ImportedElementsComputer(
         result.unit, content.indexOf(selection), selection.length);
     importedElements = computer.compute();
diff --git a/pkg/analysis_server/test/src/computer/outline_computer_test.dart b/pkg/analysis_server/test/src/computer/outline_computer_test.dart
index 42e7df8..8a4dae1 100644
--- a/pkg/analysis_server/test/src/computer/outline_computer_test.dart
+++ b/pkg/analysis_server/test/src/computer/outline_computer_test.dart
@@ -31,7 +31,7 @@
   Future<Outline> _computeOutline(String code) async {
     testCode = code;
     newFile(testPath, code);
-    var resolveResult = await getResolvedUnit(testPath);
+    var resolveResult = await getResolvedUnit(testFile);
     return DartUnitOutlineComputer(
       resolveResult,
       withBasicFlutter: true,
diff --git a/pkg/analysis_server/test/src/computer/selection_range_computer_test.dart b/pkg/analysis_server/test/src/computer/selection_range_computer_test.dart
index cf25a77..0f8de58 100644
--- a/pkg/analysis_server/test/src/computer/selection_range_computer_test.dart
+++ b/pkg/analysis_server/test/src/computer/selection_range_computer_test.dart
@@ -305,8 +305,8 @@
   }
 
   Future<List<SelectionRange>?> _computeSelectionRanges(TestCode code) async {
-    newFile(sourcePath, code.code);
-    var result = await getResolvedUnit(sourcePath);
+    final file = newFile(sourcePath, code.code);
+    var result = await getResolvedUnit(file);
     var computer =
         DartSelectionRangeComputer(result.unit, code.position.offset);
     return computer.compute();
diff --git a/pkg/analysis_server/test/src/computer/type_hierarchy_computer_test.dart b/pkg/analysis_server/test/src/computer/type_hierarchy_computer_test.dart
index 9b6ea09..a75ba31 100644
--- a/pkg/analysis_server/test/src/computer/type_hierarchy_computer_test.dart
+++ b/pkg/analysis_server/test/src/computer/type_hierarchy_computer_test.dart
@@ -55,7 +55,7 @@
 
   Future<TypeHierarchyItem?> findTarget() async {
     expect(code, isNotNull, reason: 'addTestSource should be called first');
-    final result = await getResolvedUnit(testFile.path);
+    final result = await getResolvedUnit(testFile);
     return DartLazyTypeHierarchyComputer(result)
         .findTarget(code.position.offset);
   }
@@ -94,7 +94,8 @@
 
   Future<List<TypeHierarchyItem>?> findSubtypes(
       TypeHierarchyItem target) async {
-    final result = await getResolvedUnit(target.file);
+    final file = getFile(target.file);
+    final result = await getResolvedUnit(file);
     return DartLazyTypeHierarchyComputer(result)
         .findSubtypes(target.location, searchEngine);
   }
@@ -285,7 +286,8 @@
     extends AbstractTypeHierarchyTest {
   Future<List<TypeHierarchyItem>?> findSupertypes(
       TypeHierarchyItem target) async {
-    final result = await getResolvedUnit(target.file);
+    final file = getFile(target.file);
+    final result = await getResolvedUnit(file);
     final anchor = target is TypeHierarchyRelatedItem ? target.anchor : null;
     return DartLazyTypeHierarchyComputer(result)
         .findSupertypes(target.location, anchor: anchor);
@@ -348,6 +350,7 @@
 class ^E extends D {}
     ''';
     addTestSource(content);
+    fileForContextSelection = testFile;
 
     // Walk the tree and collect names at each level.
     var names = <String>[];
@@ -607,7 +610,7 @@
   Future<void> test_enum_body() async {
     final content = '''
 /*[0*/enum /*[1*/MyEnum1/*1]*/ {
-  ^
+^  v
 }/*0]*/
     ''';
 
@@ -625,6 +628,7 @@
   Future<void> test_enum_keyword() async {
     final content = '''
 /*[0*/en^um /*[1*/MyEnum1/*1]*/ {
+  v
 }/*0]*/
     ''';
 
@@ -642,6 +646,7 @@
   Future<void> test_enumName() async {
     final content = '''
 /*[0*/enum /*[1*/MyEn^um1/*1]*/ {
+  v
 }/*0]*/
     ''';
 
diff --git a/pkg/analysis_server/test/src/flutter/flutter_outline_computer_test.dart b/pkg/analysis_server/test/src/flutter/flutter_outline_computer_test.dart
index 1f54e97..5f7f8c6 100644
--- a/pkg/analysis_server/test/src/flutter/flutter_outline_computer_test.dart
+++ b/pkg/analysis_server/test/src/flutter/flutter_outline_computer_test.dart
@@ -585,7 +585,7 @@
   Future<FlutterOutline> _computeOutline(String code) async {
     testCode = code;
     newFile(testPath, code);
-    resolveResult = await getResolvedUnit(testPath);
+    resolveResult = await getResolvedUnit(testFile);
     computer = FlutterOutlineComputer(resolveResult);
     return computer.compute();
   }
diff --git a/pkg/analysis_server/test/src/services/correction/fix/import_library_project_test.dart b/pkg/analysis_server/test/src/services/correction/fix/import_library_project_test.dart
index a8364aa..8c77476 100644
--- a/pkg/analysis_server/test/src/services/correction/fix/import_library_project_test.dart
+++ b/pkg/analysis_server/test/src/services/correction/fix/import_library_project_test.dart
@@ -845,7 +845,7 @@
 void f(Test t) {}
 ''');
 
-    await resolveFile(b);
+    await getResolvedUnit(b);
 
     await assertHasFix('''
 import 'package:aaa/a.dart';
@@ -875,7 +875,7 @@
 void f(Test t) {}
 ''');
 
-    await resolveFile(b);
+    await getResolvedUnit(b);
 
     await assertHasFix('''
 import 'package:aaa/a.dart';
@@ -932,7 +932,7 @@
 void f(Test t) {}
 ''');
 
-    await resolveFile(b);
+    await getResolvedUnit(b);
 
     await assertHasFix('''
 import 'a.dart';
@@ -1415,7 +1415,7 @@
 void f(Test t) {}
 ''');
 
-    await resolveFile(b);
+    await getResolvedUnit(b);
 
     await assertHasFix('''
 import 'package:test/src/a.dart';