|
| 1 | +// Copyright (c) 2019, 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 | +@Timeout(Duration(minutes: 5)) |
| 6 | +import 'dart:io'; |
| 7 | + |
| 8 | +import 'package:dwds/dwds.dart'; |
| 9 | +import 'package:test/test.dart'; |
| 10 | +import 'package:vm_service_lib/vm_service_lib.dart'; |
| 11 | + |
| 12 | +import 'fixtures/context.dart'; |
| 13 | + |
| 14 | +final context = TestContext( |
| 15 | + path: 'append_body/index.html', |
| 16 | +); |
| 17 | + |
| 18 | +void main() { |
| 19 | + group('Injected client with live reload', () { |
| 20 | + setUp(() async { |
| 21 | + await context.setUp(reloadConfiguration: ReloadConfiguration.liveReload); |
| 22 | + }); |
| 23 | + |
| 24 | + tearDown(() async { |
| 25 | + await context.tearDown(); |
| 26 | + }); |
| 27 | + |
| 28 | + test('can live reload changes ', () async { |
| 29 | + await context.changeInput(); |
| 30 | + |
| 31 | + var source = await context.webDriver.pageSource; |
| 32 | + |
| 33 | + // A full reload should clear the state. |
| 34 | + expect(source.contains('Hello World!'), isFalse); |
| 35 | + expect(source.contains('Gary is awesome!'), isTrue); |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + group('Injected client', () { |
| 40 | + setUp(() async { |
| 41 | + await context.setUp(); |
| 42 | + }); |
| 43 | + |
| 44 | + tearDown(() async { |
| 45 | + await context.tearDown(); |
| 46 | + }); |
| 47 | + |
| 48 | + test('destroys and recreates the isolate during a hot restart', () async { |
| 49 | + var client = context.debugConnection.vmService; |
| 50 | + await client.streamListen('Isolate'); |
| 51 | + await context.changeInput(); |
| 52 | + |
| 53 | + var eventsDone = expectLater( |
| 54 | + client.onIsolateEvent, |
| 55 | + emitsThrough(emitsInOrder([ |
| 56 | + _hasKind(EventKind.kIsolateExit), |
| 57 | + _hasKind(EventKind.kIsolateStart), |
| 58 | + _hasKind(EventKind.kIsolateRunnable), |
| 59 | + ]))); |
| 60 | + |
| 61 | + expect(await client.callServiceExtension('hotRestart'), |
| 62 | + const TypeMatcher<Success>()); |
| 63 | + |
| 64 | + await eventsDone; |
| 65 | + }); |
| 66 | + |
| 67 | + test('destroys and recreates the isolate during a page refresh', () async { |
| 68 | + var client = context.debugConnection.vmService; |
| 69 | + await client.streamListen('Isolate'); |
| 70 | + await context.changeInput(); |
| 71 | + |
| 72 | + var eventsDone = expectLater( |
| 73 | + client.onIsolateEvent, |
| 74 | + emitsThrough(emitsInOrder([ |
| 75 | + _hasKind(EventKind.kIsolateExit), |
| 76 | + _hasKind(EventKind.kIsolateStart), |
| 77 | + _hasKind(EventKind.kIsolateRunnable), |
| 78 | + ]))); |
| 79 | + |
| 80 | + await context.webDriver.driver.refresh(); |
| 81 | + |
| 82 | + await eventsDone; |
| 83 | + }); |
| 84 | + |
| 85 | + test('can hot restart via the service extension', () async { |
| 86 | + var client = context.debugConnection.vmService; |
| 87 | + await context.changeInput(); |
| 88 | + |
| 89 | + expect(await client.callServiceExtension('hotRestart'), |
| 90 | + const TypeMatcher<Success>()); |
| 91 | + await Future.delayed(const Duration(seconds: 2)); |
| 92 | + |
| 93 | + var source = await context.webDriver.pageSource; |
| 94 | + // Main is re-invoked which shouldn't clear the state. |
| 95 | + expect(source, contains('Hello World!')); |
| 96 | + expect(source, contains('Gary is awesome!')); |
| 97 | + }); |
| 98 | + |
| 99 | + test('can refresh the page via the fullReload service extension', () async { |
| 100 | + var client = context.debugConnection.vmService; |
| 101 | + await context.changeInput(); |
| 102 | + |
| 103 | + expect(await client.callServiceExtension('fullReload'), isA<Success>()); |
| 104 | + await Future.delayed(const Duration(seconds: 2)); |
| 105 | + |
| 106 | + var source = await context.webDriver.pageSource; |
| 107 | + // Should see only the new text |
| 108 | + expect(source, isNot(contains('Hello World!'))); |
| 109 | + expect(source, contains('Gary is awesome!')); |
| 110 | + }); |
| 111 | + |
| 112 | + test('can hot restart while paused', () async { |
| 113 | + var client = context.debugConnection.vmService; |
| 114 | + var vm = await client.getVM(); |
| 115 | + var isolateId = vm.isolates.first.id; |
| 116 | + await client.streamListen('Debug'); |
| 117 | + var stream = client.onEvent('Debug'); |
| 118 | + var scriptList = await client.getScripts(isolateId); |
| 119 | + var main = scriptList.scripts |
| 120 | + .firstWhere((script) => script.uri.contains('main.dart')); |
| 121 | + await client.addBreakpoint(isolateId, main.id, 13); |
| 122 | + await stream |
| 123 | + .firstWhere((event) => event.kind == EventKind.kPauseBreakpoint); |
| 124 | + |
| 125 | + await context.changeInput(); |
| 126 | + await client.streamListen('Isolate'); |
| 127 | + stream = client.onEvent('Isolate'); |
| 128 | + await client.callServiceExtension('hotRestart'); |
| 129 | + await stream.firstWhere((event) => event.kind == EventKind.kIsolateStart); |
| 130 | + var source = await context.webDriver.pageSource; |
| 131 | + |
| 132 | + // Main is re-invoked which shouldn't clear the state. |
| 133 | + expect(source.contains('Hello World!'), isTrue); |
| 134 | + expect(source.contains('Gary is awesome!'), isTrue); |
| 135 | + |
| 136 | + // Should not be paused. |
| 137 | + vm = await client.getVM(); |
| 138 | + isolateId = vm.isolates.first.id; |
| 139 | + var isolate = await client.getIsolate(isolateId) as Isolate; |
| 140 | + expect(isolate.pauseEvent.kind, EventKind.kResume); |
| 141 | + expect(isolate.breakpoints.isEmpty, isTrue); |
| 142 | + |
| 143 | + // TODO(sdk/issues/37364) - Remove once corresponding SDK issue is fixed. |
| 144 | + }, skip: Platform.isWindows); |
| 145 | + }); |
| 146 | + |
| 147 | + group('Injected client with hot restart', () { |
| 148 | + setUp(() async { |
| 149 | + await context.setUp(reloadConfiguration: ReloadConfiguration.hotRestart); |
| 150 | + }); |
| 151 | + |
| 152 | + tearDown(() async { |
| 153 | + await context.tearDown(); |
| 154 | + }); |
| 155 | + |
| 156 | + test('can hot restart changes ', () async { |
| 157 | + await context.changeInput(); |
| 158 | + |
| 159 | + var source = await context.webDriver.pageSource; |
| 160 | + |
| 161 | + // Main is re-invoked which shouldn't clear the state. |
| 162 | + expect(source.contains('Hello World!'), isTrue); |
| 163 | + expect(source.contains('Gary is awesome!'), isTrue); |
| 164 | + // The ext.flutter.disassemble callback is invoked and waited for. |
| 165 | + expect(source, |
| 166 | + contains('start disassemble end disassemble Gary is awesome')); |
| 167 | + }); |
| 168 | + |
| 169 | + test('fires isolate create/destroy events during hot restart', () async { |
| 170 | + var client = context.debugConnection.vmService; |
| 171 | + await client.streamListen('Isolate'); |
| 172 | + |
| 173 | + var eventsDone = expectLater( |
| 174 | + client.onIsolateEvent, |
| 175 | + emitsThrough(emitsInOrder([ |
| 176 | + _hasKind(EventKind.kIsolateExit), |
| 177 | + _hasKind(EventKind.kIsolateStart), |
| 178 | + _hasKind(EventKind.kIsolateRunnable), |
| 179 | + ]))); |
| 180 | + |
| 181 | + await context.changeInput(); |
| 182 | + |
| 183 | + await eventsDone; |
| 184 | + }); |
| 185 | + }); |
| 186 | +} |
| 187 | + |
| 188 | +TypeMatcher<Event> _hasKind(String kind) => |
| 189 | + isA<Event>().having((e) => e.kind, 'kind', kind); |
0 commit comments