|
| 1 | +// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +@Tags(['daily']) |
| 6 | +@TestOn('vm') |
| 7 | +@Timeout(Duration(minutes: 5)) |
| 8 | +library; |
| 9 | + |
| 10 | +import 'package:dwds/expression_compiler.dart'; |
| 11 | +import 'package:test/test.dart'; |
| 12 | +import 'package:test_common/logging.dart'; |
| 13 | +import 'package:test_common/test_sdk_configuration.dart'; |
| 14 | +import 'package:vm_service/vm_service.dart'; |
| 15 | + |
| 16 | +import 'fixtures/context.dart'; |
| 17 | +import 'fixtures/project.dart'; |
| 18 | +import 'fixtures/utilities.dart'; |
| 19 | + |
| 20 | +const originalString = 'Hello World!'; |
| 21 | +const newString = 'Bonjour le monde!'; |
| 22 | + |
| 23 | +void main() { |
| 24 | + // Enable verbose logging for debugging. |
| 25 | + final debug = false; |
| 26 | + final provider = TestSdkConfigurationProvider( |
| 27 | + verbose: debug, |
| 28 | + canaryFeatures: true, |
| 29 | + ddcModuleFormat: ModuleFormat.ddc, |
| 30 | + ); |
| 31 | + final project = TestProject.testHotReload; |
| 32 | + final context = TestContext(project, provider); |
| 33 | + |
| 34 | + tearDownAll(provider.dispose); |
| 35 | + |
| 36 | + Future<void> makeEditAndRecompile() async { |
| 37 | + context.makeEditToDartLibFile( |
| 38 | + libFileName: 'library1.dart', |
| 39 | + toReplace: originalString, |
| 40 | + replaceWith: newString, |
| 41 | + ); |
| 42 | + await context.recompile(fullRestart: false); |
| 43 | + } |
| 44 | + |
| 45 | + void undoEdit() { |
| 46 | + context.makeEditToDartLibFile( |
| 47 | + libFileName: 'library1.dart', |
| 48 | + toReplace: newString, |
| 49 | + replaceWith: originalString, |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + group('Injected client', () { |
| 54 | + late VmService fakeClient; |
| 55 | + |
| 56 | + setUp(() async { |
| 57 | + setCurrentLogWriter(debug: debug); |
| 58 | + await context.setUp( |
| 59 | + testSettings: TestSettings( |
| 60 | + enableExpressionEvaluation: true, |
| 61 | + compilationMode: CompilationMode.frontendServer, |
| 62 | + moduleFormat: ModuleFormat.ddc, |
| 63 | + canaryFeatures: true, |
| 64 | + ), |
| 65 | + ); |
| 66 | + fakeClient = await context.connectFakeClient(); |
| 67 | + }); |
| 68 | + |
| 69 | + tearDown(() async { |
| 70 | + undoEdit(); |
| 71 | + await context.tearDown(); |
| 72 | + }); |
| 73 | + |
| 74 | + test('can hot reload', () async { |
| 75 | + final client = context.debugConnection.vmService; |
| 76 | + await makeEditAndRecompile(); |
| 77 | + |
| 78 | + final vm = await client.getVM(); |
| 79 | + final isolate = await client.getIsolate(vm.isolates!.first.id!); |
| 80 | + |
| 81 | + final report = await fakeClient.reloadSources(isolate.id!); |
| 82 | + expect(report.success, true); |
| 83 | + |
| 84 | + var source = await context.webDriver.pageSource; |
| 85 | + // Should not contain the change until the function that updates the page |
| 86 | + // is evaluated in a hot reload. |
| 87 | + expect(source, contains(originalString)); |
| 88 | + expect(source.contains(newString), false); |
| 89 | + |
| 90 | + final rootLib = isolate.rootLib; |
| 91 | + await client.evaluate(isolate.id!, rootLib!.id!, 'evaluate()'); |
| 92 | + source = await context.webDriver.pageSource; |
| 93 | + expect(source, contains(newString)); |
| 94 | + expect(source.contains(originalString), false); |
| 95 | + }); |
| 96 | + }, timeout: Timeout.factor(2)); |
| 97 | +} |
0 commit comments