22// Use of this source code is governed by a BSD-style license that can be
33// found in the LICENSE file.
44
5- import 'package:mockito/mockito.dart' ;
65import 'package:vm_service/vm_service.dart' as vms;
6+ import 'package:test/fake.dart' ;
77
88import 'package:fuchsia_remote_debug_protocol/fuchsia_remote_debug_protocol.dart' ;
99
1010import 'common.dart' ;
1111
1212void main () {
1313 group ('FuchsiaRemoteConnection.connect' , () {
14- List <MockPortForwarder > forwardedPorts;
15- List <MockVmService > mockVmServices ;
14+ List <FakePortForwarder > forwardedPorts;
15+ List <FakeVmService > fakeVmServices ;
1616 List <Uri > uriConnections;
1717
1818 setUp (() {
@@ -58,69 +58,61 @@ void main() {
5858 },
5959 ];
6060
61- forwardedPorts = < MockPortForwarder > [];
62- mockVmServices = < MockVmService > [];
61+ forwardedPorts = < FakePortForwarder > [];
62+ fakeVmServices = < FakeVmService > [];
6363 uriConnections = < Uri > [];
64- Future <vms.VmService > mockVmConnectionFunction (
64+ Future <vms.VmService > fakeVmConnectionFunction (
6565 Uri uri, {
6666 Duration timeout,
6767 }) {
6868 return Future <vms.VmService >(() async {
69- final MockVmService service = MockVmService ();
70- mockVmServices .add (service);
69+ final FakeVmService service = FakeVmService ();
70+ fakeVmServices .add (service);
7171 uriConnections.add (uri);
72- when (service.callMethod ('_flutter.listViews' ))
73- // The local ports match the desired indices for now, so get the
74- // canned response from the URI port.
75- .thenAnswer ((_) => Future <vms.Response >(
76- () => vms.Response .parse (flutterViewCannedResponses[uri.port])));
72+ service.flutterListViews = vms.Response .parse (flutterViewCannedResponses[uri.port]);
7773 return service;
7874 });
7975 }
8076
81- fuchsiaVmServiceConnectionFunction = mockVmConnectionFunction ;
77+ fuchsiaVmServiceConnectionFunction = fakeVmConnectionFunction ;
8278 });
8379
8480 tearDown (() {
85- /// Most tests will mock out the port forwarding and connection
81+ /// Most tests will fake out the port forwarding and connection
8682 /// functions.
8783 restoreFuchsiaPortForwardingFunction ();
8884 restoreVmServiceConnectionFunction ();
8985 });
9086
9187 test ('end-to-end with three vm connections and flutter view query' , () async {
9288 int port = 0 ;
93- Future <PortForwarder > mockPortForwardingFunction (
89+ Future <PortForwarder > fakePortForwardingFunction (
9490 String address,
9591 int remotePort, [
9692 String interface = '' ,
9793 String configFile,
9894 ]) {
9995 return Future <PortForwarder >(() {
100- final MockPortForwarder pf = MockPortForwarder ();
96+ final FakePortForwarder pf = FakePortForwarder ();
10197 forwardedPorts.add (pf);
102- when ( pf.port). thenReturn ( port++ ) ;
103- when ( pf.remotePort). thenReturn ( remotePort) ;
98+ pf.port = port++ ;
99+ pf.remotePort = remotePort;
104100 return pf;
105101 });
106102 }
107103
108- fuchsiaPortForwardingFunction = mockPortForwardingFunction ;
109- final MockSshCommandRunner mockRunner = MockSshCommandRunner ();
104+ fuchsiaPortForwardingFunction = fakePortForwardingFunction ;
105+ final FakeSshCommandRunner fakeRunner = FakeSshCommandRunner ();
110106 // Adds some extra junk to make sure the strings will be cleaned up.
111- when (mockRunner.run (argThat (startsWith ('/bin/find' )))).thenAnswer (
112- (_) => Future <List <String >>.value (
113- < String > ['/hub/blah/blah/blah/vmservice-port\n ' ]));
114- when (mockRunner.run (argThat (startsWith ('/bin/ls' )))).thenAnswer (
115- (_) => Future <List <String >>.value (
116- < String > ['123\n\n\n ' , '456 ' , '789' ]));
117- when (mockRunner.address).thenReturn ('fe80::8eae:4cff:fef4:9247' );
118- when (mockRunner.interface ).thenReturn ('eno1' );
107+ fakeRunner.findResponse = < String > ['/hub/blah/blah/blah/vmservice-port\n ' ];
108+ fakeRunner.lsResponse = < String > ['123\n\n\n ' , '456 ' , '789' ];
109+ fakeRunner.address = 'fe80::8eae:4cff:fef4:9247' ;
110+ fakeRunner.interface = 'eno1' ;
119111
120112 final FuchsiaRemoteConnection connection =
121- await FuchsiaRemoteConnection .connectWithSshCommandRunner (mockRunner );
113+ await FuchsiaRemoteConnection .connectWithSshCommandRunner (fakeRunner );
122114
123- // [mockPortForwardingFunction ] will have returned three different
115+ // [fakePortForwardingFunction ] will have returned three different
124116 // forwarded ports, incrementing the port each time by one. (Just a sanity
125117 // check that the forwarding port was called).
126118 expect (forwardedPorts.length, 3 );
@@ -132,7 +124,7 @@ void main() {
132124 expect (forwardedPorts[2 ].port, 2 );
133125
134126 // VMs should be accessed via localhost ports given by
135- // [mockPortForwardingFunction ].
127+ // [fakePortForwardingFunction ].
136128 expect (uriConnections[0 ],
137129 Uri (scheme: 'ws' , host: '[::1]' , port: 0 , path: '/ws' ));
138130 expect (uriConnections[1 ],
@@ -154,44 +146,40 @@ void main() {
154146
155147 // Ensure the ports are all closed after stop was called.
156148 await connection.stop ();
157- verify (forwardedPorts[0 ].stop () );
158- verify (forwardedPorts[1 ].stop () );
159- verify (forwardedPorts[2 ].stop () );
149+ expect (forwardedPorts[0 ].stopped, true );
150+ expect (forwardedPorts[1 ].stopped, true );
151+ expect (forwardedPorts[2 ].stopped, true );
160152 });
161153
162154 test ('end-to-end with three vms and remote open port' , () async {
163155 int port = 0 ;
164- Future <PortForwarder > mockPortForwardingFunction (
156+ Future <PortForwarder > fakePortForwardingFunction (
165157 String address,
166158 int remotePort, [
167159 String interface = '' ,
168160 String configFile,
169161 ]) {
170162 return Future <PortForwarder >(() {
171- final MockPortForwarder pf = MockPortForwarder ();
163+ final FakePortForwarder pf = FakePortForwarder ();
172164 forwardedPorts.add (pf);
173- when ( pf.port). thenReturn ( port++ ) ;
174- when ( pf.remotePort). thenReturn ( remotePort) ;
175- when ( pf.openPortAddress). thenReturn ( 'fe80::1:2%eno2' ) ;
165+ pf.port = port++ ;
166+ pf.remotePort = remotePort;
167+ pf.openPortAddress = 'fe80::1:2%eno2' ;
176168 return pf;
177169 });
178170 }
179171
180- fuchsiaPortForwardingFunction = mockPortForwardingFunction ;
181- final MockSshCommandRunner mockRunner = MockSshCommandRunner ();
172+ fuchsiaPortForwardingFunction = fakePortForwardingFunction ;
173+ final FakeSshCommandRunner fakeRunner = FakeSshCommandRunner ();
182174 // Adds some extra junk to make sure the strings will be cleaned up.
183- when (mockRunner.run (argThat (startsWith ('/bin/find' )))).thenAnswer (
184- (_) => Future <List <String >>.value (
185- < String > ['/hub/blah/blah/blah/vmservice-port\n ' ]));
186- when (mockRunner.run (argThat (startsWith ('/bin/ls' )))).thenAnswer (
187- (_) => Future <List <String >>.value (
188- < String > ['123\n\n\n ' , '456 ' , '789' ]));
189- when (mockRunner.address).thenReturn ('fe80::8eae:4cff:fef4:9247' );
190- when (mockRunner.interface ).thenReturn ('eno1' );
175+ fakeRunner.findResponse = < String > ['/hub/blah/blah/blah/vmservice-port\n ' ];
176+ fakeRunner.lsResponse = < String > ['123\n\n\n ' , '456 ' , '789' ];
177+ fakeRunner.address = 'fe80::8eae:4cff:fef4:9247' ;
178+ fakeRunner.interface = 'eno1' ;
191179 final FuchsiaRemoteConnection connection =
192- await FuchsiaRemoteConnection .connectWithSshCommandRunner (mockRunner );
180+ await FuchsiaRemoteConnection .connectWithSshCommandRunner (fakeRunner );
193181
194- // [mockPortForwardingFunction ] will have returned three different
182+ // [fakePortForwardingFunction ] will have returned three different
195183 // forwarded ports, incrementing the port each time by one. (Just a sanity
196184 // check that the forwarding port was called).
197185 expect (forwardedPorts.length, 3 );
@@ -203,7 +191,7 @@ void main() {
203191 expect (forwardedPorts[2 ].port, 2 );
204192
205193 // VMs should be accessed via the alternate adddress given by
206- // [mockPortForwardingFunction ].
194+ // [fakePortForwardingFunction ].
207195 expect (uriConnections[0 ],
208196 Uri (scheme: 'ws' , host: '[fe80::1:2%25eno2]' , port: 0 , path: '/ws' ));
209197 expect (uriConnections[1 ],
@@ -225,43 +213,39 @@ void main() {
225213
226214 // Ensure the ports are all closed after stop was called.
227215 await connection.stop ();
228- verify (forwardedPorts[0 ].stop () );
229- verify (forwardedPorts[1 ].stop () );
230- verify (forwardedPorts[2 ].stop () );
216+ expect (forwardedPorts[0 ].stopped, true );
217+ expect (forwardedPorts[1 ].stopped, true );
218+ expect (forwardedPorts[2 ].stopped, true );
231219 });
232220
233221 test ('end-to-end with three vms and ipv4' , () async {
234222 int port = 0 ;
235- Future <PortForwarder > mockPortForwardingFunction (
223+ Future <PortForwarder > fakePortForwardingFunction (
236224 String address,
237225 int remotePort, [
238226 String interface = '' ,
239227 String configFile,
240228 ]) {
241229 return Future <PortForwarder >(() {
242- final MockPortForwarder pf = MockPortForwarder ();
230+ final FakePortForwarder pf = FakePortForwarder ();
243231 forwardedPorts.add (pf);
244- when ( pf.port). thenReturn ( port++ ) ;
245- when ( pf.remotePort). thenReturn ( remotePort) ;
232+ pf.port = port++ ;
233+ pf.remotePort = remotePort;
246234 return pf;
247235 });
248236 }
249237
250- fuchsiaPortForwardingFunction = mockPortForwardingFunction ;
251- final MockSshCommandRunner mockRunner = MockSshCommandRunner ();
238+ fuchsiaPortForwardingFunction = fakePortForwardingFunction ;
239+ final FakeSshCommandRunner fakeRunner = FakeSshCommandRunner ();
252240 // Adds some extra junk to make sure the strings will be cleaned up.
253- when (mockRunner.run (argThat (startsWith ('/bin/find' )))).thenAnswer (
254- (_) => Future <List <String >>.value (
255- < String > ['/hub/blah/blah/blah/vmservice-port\n ' ]));
256- when (mockRunner.run (argThat (startsWith ('/bin/ls' )))).thenAnswer (
257- (_) => Future <List <String >>.value (
258- < String > ['123\n\n\n ' , '456 ' , '789' ]));
259- when (mockRunner.address).thenReturn ('196.168.1.4' );
241+ fakeRunner.findResponse = < String > ['/hub/blah/blah/blah/vmservice-port\n ' ];
242+ fakeRunner.lsResponse = < String > ['123\n\n\n ' , '456 ' , '789' ];
243+ fakeRunner.address = '196.168.1.4' ;
260244
261245 final FuchsiaRemoteConnection connection =
262- await FuchsiaRemoteConnection .connectWithSshCommandRunner (mockRunner );
246+ await FuchsiaRemoteConnection .connectWithSshCommandRunner (fakeRunner );
263247
264- // [mockPortForwardingFunction ] will have returned three different
248+ // [fakePortForwardingFunction ] will have returned three different
265249 // forwarded ports, incrementing the port each time by one. (Just a sanity
266250 // check that the forwarding port was called).
267251 expect (forwardedPorts.length, 3 );
@@ -294,9 +278,9 @@ void main() {
294278
295279 // Ensure the ports are all closed after stop was called.
296280 await connection.stop ();
297- verify (forwardedPorts[0 ].stop () );
298- verify (forwardedPorts[1 ].stop () );
299- verify (forwardedPorts[2 ].stop () );
281+ expect (forwardedPorts[0 ].stopped, true );
282+ expect (forwardedPorts[1 ].stopped, true );
283+ expect (forwardedPorts[2 ].stopped, true );
300284 });
301285
302286 test ('env variable test without remote addr' , () async {
@@ -311,8 +295,69 @@ void main() {
311295 });
312296}
313297
314- class MockSshCommandRunner extends Mock implements SshCommandRunner {}
298+ class FakeSshCommandRunner extends Fake implements SshCommandRunner {
299+ List <String > findResponse;
300+ List <String > lsResponse;
301+ @override
302+ Future <List <String >> run (String command) async {
303+ if (command.startsWith ('/bin/find' )) {
304+ return findResponse;
305+ }
306+ if (command.startsWith ('/bin/ls' )) {
307+ return lsResponse;
308+ }
309+ throw UnimplementedError (command);
310+ }
311+
312+ @override
313+ String interface ;
314+
315+ @override
316+ String address;
317+
318+ @override
319+ String get sshConfigPath => '~/.ssh' ;
320+ }
321+
322+ class FakePortForwarder extends Fake implements PortForwarder {
323+ @override
324+ int port;
325+
326+ @override
327+ int remotePort;
315328
316- class MockPortForwarder extends Mock implements PortForwarder {}
329+ @override
330+ String openPortAddress;
317331
318- class MockVmService extends Mock implements vms.VmService {}
332+ bool stopped = false ;
333+ @override
334+ Future <void > stop () async {
335+ stopped = true ;
336+ }
337+ }
338+
339+ class FakeVmService extends Fake implements vms.VmService {
340+ bool disposed = false ;
341+ vms.Response flutterListViews;
342+
343+ @override
344+ Future <void > dispose () async {
345+ disposed = true ;
346+ }
347+
348+ @override
349+ Future <vms.Response > callMethod (String method, {String isolateId, Map <String , dynamic > args}) async {
350+ if (method == '_flutter.listViews' ) {
351+ return flutterListViews;
352+ }
353+ throw UnimplementedError (method);
354+ }
355+
356+ @override
357+ Future <void > onDone;
358+
359+ @override
360+ Future <vms.Version > getVersion () async {
361+ return vms.Version (major: - 1 , minor: - 1 );
362+ }
363+ }
0 commit comments