1
0
mirror of https://github.com/flutter/samples.git synced 2025-11-08 13:58:47 +00:00

[federated_plugin_sample] modify the sample to expose battery API instead of geolocation API (#526)

This commit is contained in:
Ayush Bherwani
2020-08-25 11:52:42 +05:30
committed by GitHub
parent 033ae11733
commit 60691d00c0
17 changed files with 116 additions and 211 deletions

View File

@@ -67,6 +67,9 @@ build/
**/ios/ServiceDefinitions.json
**/ios/Runner/GeneratedPluginRegistrant.*
# Web related
lib/generated_plugin_registrant.dart
# Exceptions to above rules.
!**/ios/**/default.mode1v3
!**/ios/**/default.mode2v3

View File

@@ -5,17 +5,17 @@
import 'dart:html' as html;
import 'package:federated_plugin_platform_interface/federated_plugin_platform_interface.dart';
import 'package:federated_plugin_platform_interface/location_model.dart';
import 'package:flutter/services.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
/// Web Implementation of [FederatedPluginInterface] to provide location.
/// Web Implementation of [FederatedPluginInterface] to retrieve current battery
/// level of device.
class FederatedPlugin extends FederatedPluginInterface {
final html.Geolocation _geolocation;
final html.Navigator _navigator;
/// Constructor to override the geolocation object for testing purpose.
FederatedPlugin({html.Geolocation geolocation})
: _geolocation = geolocation ?? html.window.navigator.geolocation;
/// Constructor to override the navigator object for testing purpose.
FederatedPlugin({html.Navigator navigator})
: _navigator = navigator ?? html.window.navigator;
/// Method to register the plugin which sets [FederatedPlugin] to be the default
/// instance of [FederatedPluginInterface].
@@ -23,21 +23,20 @@ class FederatedPlugin extends FederatedPluginInterface {
FederatedPluginInterface.instance = FederatedPlugin();
}
/// Returns [Location] to provide latitude and longitude.
/// Returns the current battery level of device.
///
/// If any error, it's assume that user has denied the permission forever.
/// If any error, it's assume that the BatteryManager API is not supported by
/// browser.
@override
Future<Location> getLocation() async {
Future<int> getBatteryLevel() async {
try {
final geoPosition = await _geolocation.getCurrentPosition();
return Location(
longitude: geoPosition.coords.longitude.toDouble(),
latitude: geoPosition.coords.latitude.toDouble(),
);
final battery = await _navigator.getBattery() as html.BatteryManager;
// The battery level retrieved is in range of 0.0 to 1.0.
return battery.level * 100 as int;
} catch (error) {
throw PlatformException(
code: 'PERMISSION_DENIED',
message: 'Permission denied from User',
code: 'STATUS_UNAVAILABLE',
message: 'The plugin is not supported by the browser.',
details: null,
);
}

View File

@@ -1,5 +1,5 @@
name: federated_plugin_web
description: Web implementation of federated_plugin to provide location.
description: Web implementation of federated_plugin to retrieve current battery level.
version: 0.0.1
homepage:
publish_to: none

View File

@@ -9,45 +9,35 @@ import 'package:federated_plugin_web/federated_plugin_web.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
const kLatitude = 4;
const kLongitude = 3;
const kBatteryLevel = 0.49;
class GeoLocationMock extends Mock implements Geolocation {}
class NavigatorMock extends Mock implements Navigator {}
class GeoPositionMock extends Mock implements Geoposition {
class BatteryManagerMock extends Mock implements BatteryManager {
@override
Coordinates get coords => MockCoordinates();
}
class MockCoordinates extends Mock implements Coordinates {
@override
num get latitude => kLatitude;
@override
num get longitude => kLongitude;
num get level => kBatteryLevel;
}
void main() {
E2EWidgetsFlutterBinding.ensureInitialized();
group('FederatedPlugin test', () {
final geoLocationMock = GeoLocationMock();
final navigatorMock = NavigatorMock();
setUp(() {
when(geoLocationMock.getCurrentPosition())
.thenAnswer((realInvocation) async => GeoPositionMock());
when(navigatorMock.getBattery())
.thenAnswer((realInvocation) async => BatteryManagerMock());
});
testWidgets('getLocation Method', (tester) async {
final federatedPlugin = FederatedPlugin(geolocation: geoLocationMock);
final location = await federatedPlugin.getLocation();
testWidgets('getBatteryLevel Method', (tester) async {
final federatedPlugin = FederatedPlugin(navigator: navigatorMock);
final batteryLevel = await federatedPlugin.getBatteryLevel();
// Verify that getCurrentPosition was called.
verify(geoLocationMock.getCurrentPosition());
// Verify that getBattery was called.
verify(navigatorMock.getBattery());
// Verify the values of Location.longitude and Location.latitude.
expect(location.longitude, kLongitude);
expect(location.latitude, kLatitude);
// Verify the battery level.
expect(batteryLevel, kBatteryLevel * 100);
});
});
}