You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
104 lines
3.5 KiB
104 lines
3.5 KiB
import 'dart:convert';
|
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:http/testing.dart';
|
|
import 'package:location_plugin/location_plugin.dart';
|
|
import 'package:location_plugin/location_plugin_platform_interface.dart';
|
|
import 'package:location_plugin/location_plugin_method_channel.dart';
|
|
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
|
|
|
|
class MockLocationPluginPlatform
|
|
with MockPlatformInterfaceMixin
|
|
implements LocationPluginPlatform {
|
|
|
|
@override
|
|
Future<String?> getPlatformVersion() => Future.value('42');
|
|
|
|
@override
|
|
Future<Map<String, double>?> getCurrentLocation() =>
|
|
Future.value({'latitude': 1.0, 'longitude': 2.0});
|
|
}
|
|
|
|
void main() {
|
|
final LocationPluginPlatform initialPlatform = LocationPluginPlatform.instance;
|
|
|
|
test('$MethodChannelLocationPlugin is the default instance', () {
|
|
expect(initialPlatform, isInstanceOf<MethodChannelLocationPlugin>());
|
|
});
|
|
|
|
test('getPlatformVersion', () async {
|
|
LocationPlugin locationPlugin = LocationPlugin();
|
|
MockLocationPluginPlatform fakePlatform = MockLocationPluginPlatform();
|
|
LocationPluginPlatform.instance = fakePlatform;
|
|
|
|
expect(await locationPlugin.getPlatformVersion(), '42');
|
|
});
|
|
|
|
test('getCurrentLocation', () async {
|
|
LocationPlugin locationPlugin = LocationPlugin();
|
|
MockLocationPluginPlatform fakePlatform = MockLocationPluginPlatform();
|
|
LocationPluginPlatform.instance = fakePlatform;
|
|
|
|
expect(await locationPlugin.getCurrentLocation(), {'latitude': 1.0, 'longitude': 2.0});
|
|
});
|
|
|
|
group('getCityInfo', () {
|
|
test('returns city info on success', () async {
|
|
final mockClient = MockClient((request) async {
|
|
expect(request.url.host, 'siteapi.cloud.huawei.com');
|
|
final body = jsonDecode(request.body) as Map<String, dynamic>;
|
|
expect(body['lat'], 12.34);
|
|
expect(body['lng'], 56.78);
|
|
return http.Response(
|
|
jsonEncode({
|
|
'returnCode': '0',
|
|
'sites': [
|
|
{
|
|
'address': {
|
|
'adminCode': '310000000000',
|
|
'adminArea': '上海市',
|
|
'subAdminArea': '上海市',
|
|
'tertiaryAdminArea': '徐汇区',
|
|
'locality': '徐汇区',
|
|
}
|
|
}
|
|
],
|
|
}),
|
|
200,
|
|
);
|
|
});
|
|
|
|
final plugin = LocationPlugin(httpClient: mockClient);
|
|
final cityInfo = await plugin.getCityInfo(latitude: 12.34, longitude: 56.78, apiKey: 'test');
|
|
|
|
expect(cityInfo, isNotNull);
|
|
expect(cityInfo?.code, 310000);
|
|
expect(cityInfo?.region, ['上海市', '上海市', '徐汇区']);
|
|
expect(cityInfo?.cityName, '徐汇区');
|
|
});
|
|
|
|
test('returns null when no sites available', () async {
|
|
final mockClient = MockClient((_) async {
|
|
return http.Response(jsonEncode({'returnCode': '0', 'sites': []}), 200);
|
|
});
|
|
final plugin = LocationPlugin(httpClient: mockClient);
|
|
|
|
final cityInfo = await plugin.getCityInfo(latitude: 0, longitude: 0, apiKey: 'test');
|
|
|
|
expect(cityInfo, isNull);
|
|
});
|
|
|
|
test('throws exception when API fails', () async {
|
|
final mockClient = MockClient((_) async {
|
|
return http.Response(jsonEncode({'returnCode': '1', 'returnDesc': 'error'}), 200);
|
|
});
|
|
final plugin = LocationPlugin(httpClient: mockClient);
|
|
|
|
expect(
|
|
() => plugin.getCityInfo(latitude: 0, longitude: 0, apiKey: 'test'),
|
|
throwsA(isA<CityInfoLookupException>()),
|
|
);
|
|
});
|
|
});
|
|
}
|