blob: 8d571addc9bb682440b98bff04b8bdc561d25f98 [file] [log] [blame]
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// @dart=2.12
import 'dart:convert';
import 'dart:io';
import 'package:sl4f/sl4f.dart';
import 'package:test/test.dart';
void main(List<String> args) {
late HttpServer fakeServer;
late Sl4f sl4f;
setUp(() async {
fakeServer = await HttpServer.bind('127.0.0.1', 18080);
sl4f = Sl4f('127.0.0.1', null, 18080);
});
tearDown(() async {
await fakeServer.close();
});
test('call Search with Component not running', () {
void handler(HttpRequest req) async {
expect(req.contentLength, greaterThan(0));
final body = jsonDecode(await utf8.decoder.bind(req).join());
expect(body['method'], 'component_facade.Search');
expect(body['params'], {
'name': 'fake_name',
});
req.response.write(
jsonEncode({'id': body['id'], 'result': 'NotFound', 'error': null}));
await req.response.close();
}
fakeServer.listen(handler);
expect(Component(sl4f).search('fake_name'), completion(equals(false)));
});
test('call Search with Component running', () {
void handler(HttpRequest req) async {
expect(req.contentLength, greaterThan(0));
final body = jsonDecode(await utf8.decoder.bind(req).join());
expect(body['method'], 'component_facade.Search');
expect(body['params'], {
'name': 'running_comp.cm',
});
req.response.write(
jsonEncode({'id': body['id'], 'result': 'Success', 'error': null}));
await req.response.close();
}
fakeServer.listen(handler);
expect(Component(sl4f).search('running_comp.cm'), completion(equals(true)));
});
test('call List to list running component', () {
const expectedList = [
'component1.cm',
'component2.cm',
'component3.cm',
];
void handler(HttpRequest req) async {
expect(req.contentLength, greaterThan(0));
final body = jsonDecode(await utf8.decoder.bind(req).join());
expect(body['method'], 'component_facade.List');
req.response.write(jsonEncode(
{'id': body['id'], 'result': expectedList, 'error': null}));
await req.response.close();
}
fakeServer.listen(handler);
expect(Component(sl4f).list(), completion(equals(expectedList)));
});
test('call Launch with packge url', () {
void handler(HttpRequest req) async {
expect(req.contentLength, greaterThan(0));
final body = jsonDecode(await utf8.decoder.bind(req).join());
expect(body['method'], 'component_facade.Launch');
expect(body['params'], {
'url': 'fuchsia-pkg://fuchsia.com/fake_url#meta/fake_url.cm',
'wait_until_stop': true
});
req.response.write(
jsonEncode({'id': body['id'], 'result': 'Success', 'error': null}));
await req.response.close();
}
fakeServer.listen(handler);
expect(
Component(sl4f)
.launch('fuchsia-pkg://fuchsia.com/fake_url#meta/fake_url.cm'),
completion(equals('Success')));
});
test('call launchAndDetach with packge url', () {
void handler(HttpRequest req) async {
expect(req.contentLength, greaterThan(0));
final body = jsonDecode(await utf8.decoder.bind(req).join());
expect(body['method'], 'component_facade.Launch');
expect(body['params'], {
'url': 'fuchsia-pkg://fuchsia.com/fake_url#meta/fake_url.cm',
'wait_until_stop': false
});
req.response.write(
jsonEncode({'id': body['id'], 'result': 'Success', 'error': null}));
await req.response.close();
}
fakeServer.listen(handler);
expect(
Component(sl4f).launchAndDetach(
'fuchsia-pkg://fuchsia.com/fake_url#meta/fake_url.cm'),
completion(equals('Success')));
});
}